diff --git a/drivers/audio/wavfifo.go b/drivers/audio/wavfifo.go index ff59fd3..5be1806 100644 --- a/drivers/audio/wavfifo.go +++ b/drivers/audio/wavfifo.go @@ -4,6 +4,7 @@ import ( "context" "encoding/binary" "io" + "time" "github.com/pion/mediadevices/pkg/io/audio" "github.com/pion/mediadevices/pkg/prop" @@ -70,11 +71,10 @@ func (w *PCMStreamDriver) AudioRecord(p prop.Media) (audio.Reader, error) { select { case <-w.closed: return nil, func() {}, io.EOF - case pcmData, ok := <-w.PCM: - if !ok { - return nil, func() {}, io.ErrClosedPipe - } + case pcmData := <-w.PCM: copy(a.Data, bytesTo16BitSamples(pcmData[:])) + case <-time.After(p.Latency): + // no stuck } return a, func() {}, nil } diff --git a/drivers/video/ppm.go b/drivers/video/ppm.go index f0aad12..76b76c2 100644 --- a/drivers/video/ppm.go +++ b/drivers/video/ppm.go @@ -5,6 +5,7 @@ import ( "image" "image/color" "io" + "time" "github.com/nfnt/resize" "github.com/pion/mediadevices/pkg/frame" @@ -53,19 +54,15 @@ func (v *PPMStreamDriver) Properties() []prop.Media { func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { logrus.Debug(p) + canvas := image.NewYCbCr( + image.Rect(0, 0, p.Width, p.Height), + image.YCbCrSubsampleRatio420, + ) r := video.ReaderFunc(func() (img image.Image, release func(), err error) { - canvas := image.NewYCbCr( - image.Rect(0, 0, p.Width, p.Height), - image.YCbCrSubsampleRatio420, - ) - select { case <-v.closed: return nil, func() {}, io.EOF - case ppmF, ok := <-v.PPMImage: - if !ok { - return nil, func() {}, io.ErrClosedPipe - } + case ppmF := <-v.PPMImage: defer ppmF.Close() img, _, err := image.Decode(ppmF) @@ -75,8 +72,8 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { // resize image and draw it to canvas resized := resize.Resize(uint(p.Width), uint(p.Height), img, resize.Lanczos3) - for x := 0; x < resized.Bounds().Dx(); x++ { - for y := 0; y < resized.Bounds().Dy(); y++ { + for y := 0; y < resized.Bounds().Dy(); y++ { + for x := 0; x < resized.Bounds().Dx(); x++ { r, g, b, _ := resized.At(x, y).RGBA() Y, Cb, Cr := color.RGBToYCbCr(uint8(r), uint8(g), uint8(b)) @@ -85,8 +82,8 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { canvas.Cr[canvas.COffset(x, y)] = Cr } } + case <-time.After(time.Second / time.Duration(p.FrameRate)): } - return canvas, func() {}, nil }) return r, nil