Enable non-webrtc sampler

This commit is contained in:
Lukas Herman
2020-11-01 15:31:14 -08:00
parent 77ebcecac6
commit 5db4007e73
3 changed files with 21 additions and 19 deletions

1
go.mod
View File

@@ -7,6 +7,7 @@ require (
github.com/jfreymuth/pulse v0.0.0-20201014123913-1e525c426c93 github.com/jfreymuth/pulse v0.0.0-20201014123913-1e525c426c93
github.com/lherman-cs/opus v0.0.2 github.com/lherman-cs/opus v0.0.2
github.com/pion/logging v0.2.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/pion/webrtc/v2 v2.2.26
github.com/satori/go.uuid v1.2.0 github.com/satori/go.uuid v1.2.0
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 golang.org/x/image v0.0.0-20200927104501-e162460cd6b5

View File

@@ -3,34 +3,30 @@ package mediadevices
import ( import (
"math" "math"
"time" "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 // 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. // the codec's clock rate to come up with a duration for each sample.
func newVideoSampler(t *webrtc.Track) samplerFunc { func newVideoSampler(clockRate uint32) samplerFunc {
clockRate := float64(t.Codec().ClockRate) clockRateFloat := float64(clockRate)
lastTimestamp := time.Now() lastTimestamp := time.Now()
return samplerFunc(func(b []byte) error { return samplerFunc(func() uint32 {
now := time.Now() now := time.Now()
duration := now.Sub(lastTimestamp).Seconds() duration := now.Sub(lastTimestamp).Seconds()
samples := uint32(math.Round(clockRate * duration)) samples := uint32(math.Round(clockRateFloat * duration))
lastTimestamp = now lastTimestamp = now
return samples
return t.WriteSample(media.Sample{Data: b, Samples: samples})
}) })
} }
// newAudioSampler creates a audio sampler that uses a fixed latency and // 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. // the codec's clock rate to come up with a duration for each sample.
func newAudioSampler(t *webrtc.Track, latency time.Duration) samplerFunc { func newAudioSampler(clockRate uint32, latency time.Duration) samplerFunc {
samples := uint32(math.Round(float64(t.Codec().ClockRate) * latency.Seconds())) samples := uint32(math.Round(float64(clockRate) * latency.Seconds()))
return samplerFunc(func(b []byte) error { return samplerFunc(func() uint32 {
return t.WriteSample(media.Sample{Data: b, Samples: samples}) return samples
}) })
} }

View File

@@ -12,6 +12,7 @@ import (
"github.com/pion/mediadevices/pkg/io/video" "github.com/pion/mediadevices/pkg/io/video"
"github.com/pion/mediadevices/pkg/wave" "github.com/pion/mediadevices/pkg/wave"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
"github.com/pion/webrtc/v2/pkg/media"
) )
var ( 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() track.mu.Lock()
defer track.mu.Unlock() defer track.mu.Unlock()
@@ -118,7 +119,6 @@ func (track *baseTrack) bind(pc *webrtc.PeerConnection, encodedReader codec.Read
return nil, err return nil, err
} }
sample := sampler(webrtcTrack)
signalCh := make(chan chan<- struct{}) signalCh := make(chan chan<- struct{})
track.activePeerConnections[pc] = signalCh track.activePeerConnections[pc] = signalCh
@@ -147,7 +147,12 @@ func (track *baseTrack) bind(pc *webrtc.PeerConnection, encodedReader codec.Read
return return
} }
if err := sample(buff); err != nil { sampleCount := sample()
err = webrtcTrack.WriteSample(media.Sample{
Data: buff,
Samples: sampleCount,
})
if err != nil {
track.onError(err) track.onError(err)
return return
} }
@@ -247,7 +252,7 @@ func (track *VideoTrack) Bind(pc *webrtc.PeerConnection) (*webrtc.Track, error)
return nil, err 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 { 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 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 { func (track *AudioTrack) Unbind(pc *webrtc.PeerConnection) error {