mirror of
https://github.com/pion/mediadevices.git
synced 2025-09-26 20:41:46 +08:00

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
36 lines
1.0 KiB
Go
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})
|
|
})
|
|
}
|