【问题标题】:How to increase the bitrate of webrtc?如何提高webrtc的码率?
【发布时间】:2019-08-26 07:57:56
【问题描述】:

我正在使用 webrtc 将 1080p 视频流从一个选项卡发送到同一台计算机上的另一个选项卡(windows10,chrome 76)。而且接收方的视频质量不如发送方。比特率只有2400kbps(300kb/s)左右,1080p和720p没有区别。摄像机移动时视频分辨率也会变低。
如何提高 webrtc 视频流的质量?

我尝试修改 sdp 以提高比特率。 http://www.rtcbits.com/2016/11/controlling-bandwidth-usage-in-webrtc.html

设置x-google-max-bitrate

peer.createAnswer().then(sdp => {
  var arr = sdp.sdp.split('\r\n');
  arr.forEach((str, i) => {
    if (/^a=fmtp:\d*/.test(str)) {
      arr[i] = str + ';x-google-max-bitrate=28000;x-google-min-bitrate=0;x-google-start-bitrate=20000';
    }
  });
  sdp = new RTCSessionDescription({
    type: 'answer',
    sdp: arr.join('\r\n'),
  })
  peer.setLocalDescription(sdp);
  socket.emit('message_send', { type: 'answer', sdp: sdp.sdp });
});

输出接收速率(kb/s)

var prevReport = null;
var t = setInterval(function() {
  if (!peer) {
    prevReport = null;
    return;
  }
  peer.getStats(null).then(reporter => {
    reporter.forEach(report => {
      if (report.type === 'inbound-rtp' && report.mediaType === 'video') {
        if (!prevReport) {
          prevReport = report;
        } else {
          console.log((report.bytesReceived - prevReport.bytesReceived) / (report.timestamp - prevReport.timestamp));
        }
      }
    });
  });
}, 1000);

我希望 1080p 的码率可以明显大于 720p。
有没有办法让 webrtc 传输无损或低损耗的视频流?


仅当 chrome 选项卡将视频发送到另一个 chrome 选项卡时,才存在 300kb/s 限制。当 chrome 选项卡将视频发送到 Firefox 选项卡时,x-google-max-bitrate 起作用。

【问题讨论】:

标签: javascript webrtc


【解决方案1】:

我尝试设置b=AS:10000,它可以工作。

peer.createAnswer().then(sdp => {
  var arr = sdp.sdp.split('\r\n');
  arr.forEach((str, i) => {
    if (/^a=fmtp:\d*/.test(str)) {
      arr[i] = str + ';x-google-max-bitrate=10000;x-google-min-bitrate=0;x-google-start-bitrate=6000';
    } else if (/^a=mid:(1|video)/.test(str)) {
      arr[i] += '\r\nb=AS:10000';
    }
  });
  sdp = new RTCSessionDescription({
    type: 'answer',
    sdp: arr.join('\r\n'),
  })
  peer.setLocalDescription(sdp);
  socket.emit('message_send', { type: 'answer', sdp: sdp.sdp });
});

【讨论】:

  • 谢谢!帮助我用 Kurento 媒体服务器录制高清视频。我试着打电话给webRtcEndpoint.setMaxVideoRecvBandwidth() 和recorderEndpoint.setMaxOuputBitrate(),但还是不够。
【解决方案2】:

调整音视频带宽限制的正确方法见官方示例 https://webrtc.github.io/samples/src/content/peerconnection/bandwidth

以下是使用RTCRtpEncodingParameters 的maxBitrate 属性设置视频带宽限制1 Mbps 的简化示例:

const pc1 = new RTCPeerConnection(servers);

...

const sender = pc1.getSenders()[0];
const parameters = sender.getParameters();
parameters.encodings[0].maxBitrate = 1 * 1000 * 100;
sender.setParameters(parameters);

为了调整音频带宽限制,应该更新相应的发送者,例如

const sender = pc1.getSenders()[1];

【讨论】:

    猜你喜欢
    • 2017-04-21
    • 2020-02-20
    • 2012-03-21
    • 2019-04-27
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    相关资源
    最近更新 更多