音频流仍然存在问题,后续debug

This commit is contained in:
TonyChyi 2022-09-27 16:59:47 +08:00
parent b7b7e78614
commit de24203100
5 changed files with 29 additions and 22 deletions

View File

@ -11,9 +11,10 @@ import (
// Skip riff header and `fmt ` just 16 bytes // Skip riff header and `fmt ` just 16 bytes
const ( const (
FmtHeaderOffset int64 = 0x0c FmtHeaderOffset = 0x0c
FmtHeaderIDSize int64 = 4 FmtHeaderIDSize = 4
FmtHeaderChunkSizeSize int64 = 4 FmtHeaderChunkSizeSize = 4
FmtHeaderSizeDefault = 16
) )
type WavHeader struct { type WavHeader struct {
@ -35,6 +36,17 @@ func NewHeader(f io.Reader) (*WavHeader, error) {
return w, nil 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 { func (w *WavHeader) Parse(f io.Reader) error {
// skip headers // skip headers
var headers [FmtHeaderOffset]byte var headers [FmtHeaderOffset]byte

View File

@ -24,7 +24,7 @@ func New(o *Options) (*Connection, error) {
connection := &Connection{ connection := &Connection{
option: o, option: o,
} }
codecSelector, err := setupCodec(o.Video.BPS, o.Audio.BPS) codecSelector, err := setupCodec(o.Video.BPS)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -123,7 +123,7 @@ func (c *Connection) Regist(offer *webrtc.SessionDescription) (*webrtc.SessionDe
return rtc.LocalDescription(), nil return rtc.LocalDescription(), nil
} }
func setupCodec(videoBPS, audioBPS int) (*mediadevices.CodecSelector, error) { func setupCodec(videoBPS int) (*mediadevices.CodecSelector, error) {
x264Prarm, err := x264.NewParams() x264Prarm, err := x264.NewParams()
if err != nil { if err != nil {
return nil, err return nil, err
@ -134,7 +134,6 @@ func setupCodec(videoBPS, audioBPS int) (*mediadevices.CodecSelector, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
opusParam.BitRate = audioBPS
codecSelector := mediadevices.NewCodecSelector( codecSelector := mediadevices.NewCodecSelector(
mediadevices.WithAudioEncoders(&opusParam), mediadevices.WithAudioEncoders(&opusParam),

View File

@ -7,9 +7,6 @@ type Options struct {
Width int `yaml:"width"` Width int `yaml:"width"`
BPS int `yaml:"bps"` BPS int `yaml:"bps"`
} `yaml:"video"` } `yaml:"video"`
Audio struct {
BPS int `yaml:"bps"`
} `yaml:"audio"`
} }
func ExampleOptions() *Options { func ExampleOptions() *Options {
@ -22,6 +19,5 @@ func ExampleOptions() *Options {
options.Video.BPS = 500_000 options.Video.BPS = 500_000
options.Video.Height = 768 options.Video.Height = 768
options.Video.Width = 1024 options.Video.Width = 1024
options.Audio.BPS = 96_000
return options return options
} }

View File

@ -15,15 +15,12 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const waveHeaderSize = 16 const waveHeaderSize = audiodriver.FmtHeaderSizeDefault
var waveHeader = &audiodriver.WavHeader{ var waveHeader *audiodriver.WavHeader
Size: waveHeaderSize,
AudioFormat: 1, func init() {
NumChannels: 2, waveHeader = audiodriver.DefaultHeader()
SampleRate: 48000, // opus only support 48kHz
BlockAlign: 4,
BitsPerSample: 16,
} }
type Server struct { type Server struct {

View File

@ -1,7 +1,10 @@
<template> <template>
<div> <div>
<canvas id="vnc" /> <canvas id="vnc" />
<div id="data"></div> <div id="data">
<video id="video" muted autoplay />
<audio id="audio" autoplay />
</div>
</div> </div>
</template> </template>
@ -90,11 +93,11 @@ onMounted(() => {
}; };
pc.ontrack = (ev) => { pc.ontrack = (ev) => {
console.log(ev); console.log(ev);
const el = document.createElement(ev.track.kind); const el = document.querySelector(`${ev.track.kind}#${ev.track.kind}`);
el.id = ev.track.kind;
el.srcObject = ev.streams[0]; el.srcObject = ev.streams[0];
el.autoplay = true; el.autoplay = true;
el.controls = true; el.controls = false;
el.oncontextmenu = () => false;
document.getElementById("data").appendChild(el); document.getElementById("data").appendChild(el);
}; };