diff --git a/go.mod b/go.mod index 7ad2392..a71637d 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/jfreymuth/pulse v0.0.0-20201014123913-1e525c426c93 github.com/lherman-cs/opus v0.0.2 github.com/pion/logging v0.2.2 + github.com/pion/rtp v1.6.0 github.com/pion/webrtc/v2 v2.2.26 github.com/satori/go.uuid v1.2.0 golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 diff --git a/sampler.go b/sampler.go index 513e5f2..2cd4d23 100644 --- a/sampler.go +++ b/sampler.go @@ -3,34 +3,30 @@ package mediadevices import ( "math" "time" - - "github.com/pion/webrtc/v2" - "github.com/pion/webrtc/v2/pkg/media" ) -type samplerFunc func(b []byte) error +type samplerFunc func() uint32 // newVideoSampler creates a video sampler that uses the actual video frame rate and // the codec's clock rate to come up with a duration for each sample. -func newVideoSampler(t *webrtc.Track) samplerFunc { - clockRate := float64(t.Codec().ClockRate) +func newVideoSampler(clockRate uint32) samplerFunc { + clockRateFloat := float64(clockRate) lastTimestamp := time.Now() - return samplerFunc(func(b []byte) error { + return samplerFunc(func() uint32 { now := time.Now() duration := now.Sub(lastTimestamp).Seconds() - samples := uint32(math.Round(clockRate * duration)) + samples := uint32(math.Round(clockRateFloat * duration)) lastTimestamp = now - - return t.WriteSample(media.Sample{Data: b, Samples: samples}) + return samples }) } // newAudioSampler creates a audio sampler that uses a fixed latency and // the codec's clock rate to come up with a duration for each sample. -func newAudioSampler(t *webrtc.Track, latency time.Duration) samplerFunc { - samples := uint32(math.Round(float64(t.Codec().ClockRate) * latency.Seconds())) - return samplerFunc(func(b []byte) error { - return t.WriteSample(media.Sample{Data: b, Samples: samples}) +func newAudioSampler(clockRate uint32, latency time.Duration) samplerFunc { + samples := uint32(math.Round(float64(clockRate) * latency.Seconds())) + return samplerFunc(func() uint32 { + return samples }) } diff --git a/track.go b/track.go index 6dae6eb..3e83e7f 100644 --- a/track.go +++ b/track.go @@ -12,6 +12,7 @@ import ( "github.com/pion/mediadevices/pkg/io/video" "github.com/pion/mediadevices/pkg/wave" "github.com/pion/webrtc/v2" + "github.com/pion/webrtc/v2/pkg/media" ) var ( @@ -109,7 +110,7 @@ func (track *baseTrack) onError(err error) { } } -func (track *baseTrack) bind(pc *webrtc.PeerConnection, encodedReader codec.ReadCloser, selectedCodec *codec.RTPCodec, sampler func(*webrtc.Track) samplerFunc) (*webrtc.Track, error) { +func (track *baseTrack) bind(pc *webrtc.PeerConnection, encodedReader codec.ReadCloser, selectedCodec *codec.RTPCodec, sample samplerFunc) (*webrtc.Track, error) { track.mu.Lock() defer track.mu.Unlock() @@ -118,7 +119,6 @@ func (track *baseTrack) bind(pc *webrtc.PeerConnection, encodedReader codec.Read return nil, err } - sample := sampler(webrtcTrack) signalCh := make(chan chan<- struct{}) track.activePeerConnections[pc] = signalCh @@ -147,7 +147,12 @@ func (track *baseTrack) bind(pc *webrtc.PeerConnection, encodedReader codec.Read return } - if err := sample(buff); err != nil { + sampleCount := sample() + err = webrtcTrack.WriteSample(media.Sample{ + Data: buff, + Samples: sampleCount, + }) + if err != nil { track.onError(err) return } @@ -247,7 +252,7 @@ func (track *VideoTrack) Bind(pc *webrtc.PeerConnection) (*webrtc.Track, error) return nil, err } - return track.bind(pc, encodedReader, selectedCodec, newVideoSampler) + return track.bind(pc, encodedReader, selectedCodec, newVideoSampler(selectedCodec.ClockRate)) } func (track *VideoTrack) Unbind(pc *webrtc.PeerConnection) error { @@ -317,7 +322,7 @@ func (track *AudioTrack) Bind(pc *webrtc.PeerConnection) (*webrtc.Track, error) return nil, err } - return track.bind(pc, encodedReader, selectedCodec, func(t *webrtc.Track) samplerFunc { return newAudioSampler(t, inputProp.Latency) }) + return track.bind(pc, encodedReader, selectedCodec, newAudioSampler(selectedCodec.ClockRate, inputProp.Latency)) } func (track *AudioTrack) Unbind(pc *webrtc.PeerConnection) error {