如何更改WebRTC中使用的默认编解码器



我一直在谷歌上搜索一种在Chrome的WebRTC实现中更改编解码器的方法,但似乎没有办法。

如何更改Chrome中WebRTCpeer连接中使用的默认编解码器(音频或视频)?

是的,您可以将编解码器更改为您想要的任何内容...只要 Chrome 支持它。目前,音频方面,唯一支持的编解码器是PCMA,PCMU,ISAC和OPUS(默认)。对于视频,你有VP8(在某些带有FireFox的系统上也是H264)。

若要默认使用这些编解码器中的任何一个,必须先修改 SDP,然后再在对等连接中本地设置SDP,并发送报价/答案。我已经成功测试强制Chrome默认发送PCMA而不是OPUS。

举个例子:

假设您的默认音频 SDP 部分如下所示(括号中的注释不是 SDP 的一部分)

m=audio<media> 49353<port> RTP/SAVPF<proto> 111 103 104 0 8 106 105 13 126 <rtpformats>
c=IN<nettype> IP4<addrtype> 192.168.0.13<address>
a=rtcp:49353<port> IN<nettype> IP4<addresstype> privateIP<connection address>
a=candidate:1204296370 1 udp 2122260223 privateIP 49353 typ host generation 0 <audioIceCandidate>
a=candidate:1204296370 2 udp 2122260223 privateIP 49353 typ host generation 0
a=candidate:155969090 1 tcp 1518280447 privateIP  0 typ host generation 0
a=candidate:155969090 2 tcp 1518280447 privateIP  0 typ host generation 0
a=ice-ufrag:E7VFzFythTIOaQ6X <ice username>
a=ice-pwd:ZMHFqqXEA8JLjItZcRN4FZDJ <ice-password>
a=ice-options:google-ice <iceoptions>
a=fingerprint:sha-256<encryptType> 66:2D:43:3A:31:7B:46:56:50:D7:CC:75:80:79:5D:88:7D:5D:1B:0E:C7:E6:F9:C4:68:6D:51:7F:4B:32:97:A1<print>
a=setup:actpass <dtls setup mode>
a=mid:audio   
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level <extention map>
a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=sendrecv <mediamode>
a=rtcp-mux <says rtcp mux>
a=rtpmap:111 opus/48000/2
a=fmtp:111 minptime=10
a=rtpmap:103 ISAC/16000
a=rtpmap:104 ISAC/32000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:106 CN/32000
a=rtpmap:105 CN/16000
a=rtpmap:13 CN/8000
a=rtpmap:126 telephone-event/8000
a=maxptime:60

如果只想使用 PCMA,请将m=audio行更改为以下内容: m=audio 49353 RTP/SAVPF 8这样,仅考虑PCMA有效载荷。然后,您需要删除所有与该有效负载不对应的 rtpmap 行,即下一个字符不是 8 的任何a=rtpmap:。如果您在本地设置修改后的 sdp 并将其发送给您的对等方(并且如果他们支持 PCMA...不必是他们的默认值,因为如果您只提供 PCMA,协商将强制 PCMA),那么 PCMA 将是您的音频编解码器而不是 OPUS。

几个旁白:

  • 我所说的SDP是通过对等连接的createOffercreateAnswer函数的成功回调生成并传递的SDP
  • 这种类型的想法适用于您知道底层系统(H264、SPEEX 等)支持的 ADD 编解码器。只需确保添加有效载荷以及适当的映射和选项(h264需要fmtp,因为配置文件很重要并且可能sprop-parameter-sets)。
  • 这将适用于任何适当编码的WebRTC系统,即Firefox,Opera等。不仅仅是铬。

当浏览器开始支持 setCodecPreferences,您可以检查默认情况下要使用的编解码器的 mimetype 来设置编解码器首选项。例如,如果您想更喜欢音频作品,您可以检查"audio/opus"mimetype 并将编解码器首选项设置为作品编解码器:

let tcvr = pc.getTransceivers()[0];
let codecs = RTCRtpReceiver.getCapabilities('audio').codecs;
let opus_codecs = [];
// iterate over supported codecs and pull out the codecs we want
for(let i = 0; i < codecs.length; i++)
{
   if(codecs[i].mimeType == "audio/opus")
   {
      opus_codecs .push(codecs[i]);
   }
}
// currently not all browsers support setCodecPreferences
if(tcvr.setCodecPreferences != undefined)
{
   tcvr.setCodecPreferences(opus_codecs);
}

或者对于视频,您可以将编解码器修复为 vp9:

// note the following should be called before before calling either RTCPeerConnection.createOffer() or createAnswer()
let tcvr = pc.getTransceivers()[0];
let codecs = RTCRtpReceiver.getCapabilities('video').codecs;
let vp9_codecs = [];
// iterate over supported codecs and pull out the codecs we want
for(let i = 0; i < codecs.length; i++)
{
   if(codecs[i].mimeType == "video/VP9")
   {
      vp9_codecs.push(codecs[i]);
   }
}
// currently not all browsers support setCodecPreferences
if(tcvr.setCodecPreferences != undefined)
{
   tcvr.setCodecPreferences(vp9_codecs);
}

代码改编自这篇 Pericror 博客文章,以强制使用音频/视频编解码器。

相关内容

最新更新