mirror of
https://github.com/pion/mediadevices.git
synced 2025-09-26 20:41:46 +08:00
29 lines
614 B
Go
29 lines
614 B
Go
package video
|
|
|
|
import (
|
|
"image"
|
|
"time"
|
|
)
|
|
|
|
// Throttle returns video throttling transform.
|
|
// This transform drops some of the incoming frames to achieve given framerate in fps.
|
|
func Throttle(rate float32) TransformFunc {
|
|
return func(r Reader) Reader {
|
|
ticker := time.NewTicker(time.Duration(int64(float64(time.Second) / float64(rate))))
|
|
return ReaderFunc(func() (image.Image, func(), error) {
|
|
for {
|
|
img, _, err := r.Read()
|
|
if err != nil {
|
|
ticker.Stop()
|
|
return nil, func() {}, err
|
|
}
|
|
select {
|
|
case <-ticker.C:
|
|
return img, func() {}, nil
|
|
default:
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|