zero sample when no pcm data

This commit is contained in:
TonyChyi 2022-09-27 12:34:57 +08:00
parent 39a89b80e2
commit 7ef721afb7

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"
@ -63,6 +64,12 @@ func (w *WavFIFODriver) Properties() []prop.Media {
} }
func (w *WavFIFODriver) AudioRecord(p prop.Media) (audio.Reader, error) { func (w *WavFIFODriver) AudioRecord(p prop.Media) (audio.Reader, error) {
a := wave.NewInt16Interleaved(wave.ChunkInfo{
Len: BufferSize / int(p.SampleSize/BitsPerByte),
Channels: p.ChannelCount,
SamplingRate: p.SampleRate,
})
reader := func() (wave.Audio, func(), error) { reader := func() (wave.Audio, func(), error) {
select { select {
case <-w.closed: case <-w.closed:
@ -72,15 +79,10 @@ func (w *WavFIFODriver) AudioRecord(p prop.Media) (audio.Reader, error) {
if !ok { if !ok {
return nil, func() {}, io.ErrClosedPipe return nil, func() {}, io.ErrClosedPipe
} }
a := wave.NewInt16Interleaved(wave.ChunkInfo{
Len: BufferSize / int(p.SampleSize/BitsPerByte),
Channels: p.ChannelCount,
SamplingRate: p.SampleRate,
})
copy(a.Data, bytesTo16BitSamples(pcmData[:])) copy(a.Data, bytesTo16BitSamples(pcmData[:]))
return a, func() {}, nil case <-time.After(p.Latency):
} }
return a, func() {}, nil
} }
return audio.ReaderFunc(reader), nil return audio.ReaderFunc(reader), nil
} }