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" "context"
"encoding/binary" "encoding/binary"
"io" "io"
"time"
"github.com/pion/mediadevices/pkg/io/audio" "github.com/pion/mediadevices/pkg/io/audio"
"github.com/pion/mediadevices/pkg/prop" "github.com/pion/mediadevices/pkg/prop"
@ -70,11 +71,10 @@ func (w *PCMStreamDriver) AudioRecord(p prop.Media) (audio.Reader, error) {
select { select {
case <-w.closed: case <-w.closed:
return nil, func() {}, io.EOF return nil, func() {}, io.EOF
case pcmData, ok := <-w.PCM: case pcmData := <-w.PCM:
if !ok {
return nil, func() {}, io.ErrClosedPipe
}
copy(a.Data, bytesTo16BitSamples(pcmData[:])) copy(a.Data, bytesTo16BitSamples(pcmData[:]))
case <-time.After(p.Latency):
// no stuck
} }
return a, func() {}, nil return a, func() {}, nil
} }

View File

@ -5,6 +5,7 @@ import (
"image" "image"
"image/color" "image/color"
"io" "io"
"time"
"github.com/nfnt/resize" "github.com/nfnt/resize"
"github.com/pion/mediadevices/pkg/frame" "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) { func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) {
logrus.Debug(p) logrus.Debug(p)
r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
canvas := image.NewYCbCr( canvas := image.NewYCbCr(
image.Rect(0, 0, p.Width, p.Height), image.Rect(0, 0, p.Width, p.Height),
image.YCbCrSubsampleRatio420, image.YCbCrSubsampleRatio420,
) )
r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
select { select {
case <-v.closed: case <-v.closed:
return nil, func() {}, io.EOF return nil, func() {}, io.EOF
case ppmF, ok := <-v.PPMImage: case ppmF := <-v.PPMImage:
if !ok {
return nil, func() {}, io.ErrClosedPipe
}
defer ppmF.Close() defer ppmF.Close()
img, _, err := image.Decode(ppmF) 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 // resize image and draw it to canvas
resized := resize.Resize(uint(p.Width), uint(p.Height), img, resize.Lanczos3) 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() r, g, b, _ := resized.At(x, y).RGBA()
Y, Cb, Cr := color.RGBToYCbCr(uint8(r), uint8(g), uint8(b)) 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 canvas.Cr[canvas.COffset(x, y)] = Cr
} }
} }
case <-time.After(time.Second / time.Duration(p.FrameRate)):
} }
return canvas, func() {}, nil return canvas, func() {}, nil
}) })
return r, nil return r, nil