driver should no stuck

This commit is contained in:
Sense T 2022-10-02 10:50:08 +00:00
parent c22c399f40
commit b945218c85
2 changed files with 13 additions and 16 deletions

View File

@ -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
}

View File

@ -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)
r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
canvas := image.NewYCbCr(
image.Rect(0, 0, p.Width, p.Height),
image.YCbCrSubsampleRatio420,
)
r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
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 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