我一直在尝试通过 WebRTC 传输一些高质量的音频流。Opus,主要宣传的编解码器似乎很完美,因为它可以支持高达 510kbit/s,远远超过需要。问题是,设置 Webrtc SDP 并不像看起来那么明显。感谢 Muaz Khan 的出色工作,我能够将其强制为 128kbit/s。基本上代码如下所示:
function setBandwidth(sdp) {
var sdpLines = sdp.split('\r\n');
// Find opus payload.
var opusIndex = findLine(sdpLines, 'a=rtpmap', 'opus/48000');
var opusPayload;
if (opusIndex) {
opusPayload = '109';
}
sdpLines[opusIndex]='a=rtpmap:'+opusPayload+' opus/48000/2';
var mediaIndex = findLine(sdpLines, 'm=audio');
sdpLines[mediaIndex]=(sdpLines[mediaIndex].slice(0,(sdpLines[mediaIndex].indexOf("RTP/SAVPF")+10))).concat(opusPayload);
var abIndex = findLine(sdpLines, 'a=mid:');
sdpLines[abIndex]='a=mid:audio\r\nb=AS:300000';
// Find the payload in fmtp line.
var fmtpLineIndex = findLine(sdpLines, 'a=fmtp:' + opusPayload.toString());
if (fmtpLineIndex == null) {
sdpLines[opusIndex] = sdpLines[opusIndex].concat('\r\n'+'a=fmtp:' + opusPayload.toString()+ ' minptime=10; useinbandfec=1; maxaveragebitrate='+128*1024+'; stereo=1; sprop-stereo=1 ; cbr=1');
sdp = sdpLines.join('\r\n');
return sdp;
}
// Append stereo=1 to fmtp line.
// added maxaveragebitrate here; about 50 kbits/s
// added stereo=1 here for stereo audio
// x-google-min-bitrate=50; x-google-max-bitrate=50
sdpLines[fmtpLineIndex] = sdpLines[fmtpLineIndex].concat('; maxaveragebitrate='+128*1024+'; stereo=1; sprop-stereo=1 ; cbr=1');
sdp = sdpLines.join('\r\n');
return sdp;
}
所以现在一切都设置好了,firefox 和 chrome 都为发送者和接收者显示正确的值,通信打开,音乐播放!
adding answer-sdp v=0
o=mozilla...THIS_IS_SDPARTA-42.0 502631676322875352 0 IN IP4 0.0.0.0
s=-
t=0 0
a=fingerprint:sha-256.....
a=ice-options:trickle
a=msid-semantic:WMS *
m=audio 9 RTP/SAVPF 109
c=IN IP4 0.0.0.0
a=recvonly
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=ice-pwd:c56d106030599efe08cfa2a4f9b3ad5a
a=ice-ufrag:93982a76
a=mid:audio
b=AS:300000
a=rtcp-mux
a=rtpmap:109 opus/48000/2
a=fmtp:109 minptime=10; useinbandfec=1; maxaveragebitrate=131072; stereo=1; sprop-stereo=1 ; cbr=1
a=setup:active
a=ssrc:1948755120 cname:{208483df-13c9-e347-ba4a-c71604df3ad9}
但是质量很糟糕。Chrome 在 chrome://webrtc-internals/ 上显示大约 30kbit/s 并且声音严重失真且音量可变......关于这个问题的任何线索?