Files
mediadevices/sampler.go
Lukas Herman 8127ce3be6 Refractor track to be more DRY
Resolves https://github.com/pion/mediadevices/issues/141

 * Remove newVideoTrack and newAudioTrack
 * Add a generic encoderBuilder struct
 * Add newVideoEncoderBuilders and newAudioEncoderBuilders helpers
 * Update sampler to be functional and add support for audio sampler
 * Reduce boilerplates by using closure
2020-04-18 23:51:09 -04:00

36 lines
1.0 KiB
Go

package mediadevices
import (
"math"
"time"
"github.com/pion/webrtc/v2/pkg/media"
)
type samplerFunc func(b []byte) error
// 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 LocalTrack) samplerFunc {
clockRate := float64(t.Codec().ClockRate)
lastTimestamp := time.Now()
return samplerFunc(func(b []byte) error {
now := time.Now()
duration := now.Sub(lastTimestamp).Seconds()
samples := uint32(math.Round(clockRate * duration))
lastTimestamp = now
return t.WriteSample(media.Sample{Data: b, Samples: 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 LocalTrack, 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})
})
}