This commit is contained in:
Sense T
2022-10-02 11:29:57 +00:00
parent b945218c85
commit 708732a37b
8 changed files with 76 additions and 281 deletions

View File

@@ -1,98 +0,0 @@
package audiodriver
import (
"context"
"encoding/binary"
"io"
"time"
"github.com/pion/mediadevices/pkg/io/audio"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pion/mediadevices/pkg/wave"
"github.com/sirupsen/logrus"
)
const (
DataChunkIDSize = 4
DataChunkSizeSize = 4
)
// BufferSize for pcm bytes
const BufferSize = 512
const BitsPerByte = 8
type WavFIFODriver struct {
PCM <-chan [BufferSize]byte
WaveHeader *WavHeader
closed <-chan struct{}
cancel func()
}
func New() *WavFIFODriver {
return &WavFIFODriver{}
}
func (w *WavFIFODriver) Open() error {
defer logrus.Debug("device opened")
ctx, cancel := context.WithCancel(context.Background())
w.closed = ctx.Done()
w.cancel = cancel
return nil
}
func (w *WavFIFODriver) Close() error {
w.cancel()
return nil
}
func (w *WavFIFODriver) Properties() []prop.Media {
logrus.Debugf("wave header: %v", w.WaveHeader)
return []prop.Media{
{
Audio: prop.Audio{
SampleRate: int(w.WaveHeader.SampleRate),
ChannelCount: int(w.WaveHeader.NumChannels),
SampleSize: int(w.WaveHeader.BitsPerSample),
Latency: w.WaveHeader.GetLatnecy(),
IsFloat: false, // just 8bit or 16bit with qemu
IsBigEndian: false, // qemu should be little endian
IsInterleaved: true,
},
},
}
}
func (w *WavFIFODriver) AudioRecord(p prop.Media) (audio.Reader, error) {
logrus.Debug(p)
reader := func() (wave.Audio, func(), error) {
a := wave.NewInt16Interleaved(wave.ChunkInfo{
Len: BufferSize / int(p.SampleSize/BitsPerByte),
Channels: p.ChannelCount,
SamplingRate: p.SampleRate,
})
select {
case <-w.closed:
return nil, func() {}, io.EOF
case pcmData, ok := <-w.PCM:
logrus.Debug("got %d bytes pcm data", len(pcmData))
if !ok {
return nil, func() {}, io.ErrClosedPipe
}
copy(a.Data, bytesTo16BitSamples(pcmData[:]))
case <-time.After(p.Latency):
}
return a, func() {}, nil
}
return audio.ReaderFunc(reader), nil
}
func bytesTo16BitSamples(b []byte) []int16 {
samples := make([]int16, 0)
for i := 0; i < len(b); i += 2 {
sample := binary.LittleEndian.Uint16(b[i : i+1])
samples = append(samples, int16(sample))
}
return samples
}

View File

@@ -1,118 +0,0 @@
package audiodriver
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"io"
"time"
)
// Skip riff header and `fmt ` just 16 bytes
const (
FmtHeaderOffset = 0x0c
FmtHeaderIDSize = 4
FmtHeaderChunkSizeSize = 4
FmtHeaderSizeDefault = 16
)
type WavHeader struct {
ID [4]byte
Size uint32
AudioFormat uint16
NumChannels uint16
SampleRate uint32
ByteRate uint32
BlockAlign uint16
BitsPerSample uint16
}
func NewHeader(f io.Reader) (*WavHeader, error) {
w := &WavHeader{}
if err := w.Parse(f); err != nil {
return nil, err
}
return w, nil
}
func DefaultHeader() *WavHeader {
return &WavHeader{
Size: uint32(FmtHeaderSizeDefault),
AudioFormat: 1,
NumChannels: 2,
SampleRate: 48000, // opus only support 48kHz
BlockAlign: 4,
BitsPerSample: 16,
}
}
func (w *WavHeader) Parse(f io.Reader) error {
// skip headers
var headers [FmtHeaderOffset]byte
if _, err := f.Read(headers[:]); err != nil {
return err
}
var id [4]byte
if _, err := f.Read(id[:]); err != nil {
return err
}
if bytes.Equal(id[:], []byte{'f', 'm', 't', '0'}[:]) {
return errors.New("bad header")
}
w.ID = id
var size [4]byte
if _, err := f.Read(size[:]); err != nil {
return err
}
w.Size = binary.LittleEndian.Uint32(size[:])
var af [2]byte
if _, err := f.Read(af[:]); err != nil {
return err
}
w.AudioFormat = binary.LittleEndian.Uint16(af[:])
var nc [2]byte
if _, err := f.Read(nc[:]); err != nil {
return err
}
w.NumChannels = binary.LittleEndian.Uint16(nc[:])
var sr [4]byte
if _, err := f.Read(sr[:]); err != nil {
return err
}
w.SampleRate = binary.LittleEndian.Uint32(sr[:])
var br [4]byte
if _, err := f.Read(br[:]); err != nil {
return err
}
w.ByteRate = binary.LittleEndian.Uint32(br[:])
var ba [2]byte
if _, err := f.Read(ba[:]); err != nil {
return err
}
w.BlockAlign = binary.LittleEndian.Uint16(ba[:])
var bps [2]byte
if _, err := f.Read(bps[:]); err != nil {
return err
}
w.BitsPerSample = binary.LittleEndian.Uint16(bps[:])
return nil
}
func (w *WavHeader) String() string {
b, _ := json.Marshal(w)
return string(b)
}
func (w *WavHeader) GetLatnecy() time.Duration {
return time.Millisecond * time.Duration(BufferSize) / time.Duration(w.SampleRate) / time.Duration(w.NumChannels)
}

View File

@@ -6,7 +6,6 @@ import (
"github.com/pion/mediadevices/pkg/codec/opus"
"github.com/pion/mediadevices/pkg/codec/x264"
"github.com/pion/mediadevices/pkg/driver"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pion/webrtc/v3"
"github.com/sirupsen/logrus"
)
@@ -24,7 +23,7 @@ func New(o *Options) (*Connection, error) {
connection := &Connection{
option: o,
}
codecSelector, err := setupCodec(o.Video.BPS)
codecSelector, err := setupCodec(o.VideoBPS)
if err != nil {
return nil, err
}
@@ -45,10 +44,7 @@ func New(o *Options) (*Connection, error) {
}
s, err := mediadevices.GetUserMedia(mediadevices.MediaStreamConstraints{
Video: func(mtc *mediadevices.MediaTrackConstraints) {
mtc.Height = prop.Int(o.Video.Height)
mtc.Width = prop.Int(o.Video.Width)
},
Video: func(mtc *mediadevices.MediaTrackConstraints) {},
Audio: func(mtc *mediadevices.MediaTrackConstraints) {},
Codec: codecSelector,
})

View File

@@ -2,11 +2,7 @@ package webrtcconnection
type Options struct {
STUNServers []string `yaml:"stun_servers"`
Video struct {
Height int `yaml:"height"`
Width int `yaml:"width"`
BPS int `yaml:"bps"`
} `yaml:"video"`
VideoBPS int `yaml:"video_bps"`
}
func ExampleOptions() *Options {
@@ -15,9 +11,7 @@ func ExampleOptions() *Options {
"stun:stun.l.google.com:19302",
"stun:wetofu.me:3478",
},
VideoBPS: 500_000,
}
options.Video.BPS = 500_000
options.Video.Height = 768
options.Video.Width = 1024
return options
}