简体   繁体   English

在 webrtc 中即时控制视频发送帧率

[英]Control video send framerate on the fly in webrtc

Right now I use the b=AS:1000 in the offer SDP to set the upper limit(ie 1Mbps) for the upstream video to control the amount of video I am sending to the remote peer.现在,我在 offer SDP 中使用 b=AS:1000 来设置上行视频的上限(即 1Mbps),以控制我发送到远程对等方的视频量。 I am looking into a different approach, so I was wondering if there is a way to control video frame rate on the fly of the current active video session?我正在研究一种不同的方法,所以我想知道是否有办法控制当前活动视频会话中的视频帧速率?

EDIT: I found out that getUserMedia supports minFrameRate and maxFrameRate parameters.编辑:我发现 getUserMedia 支持 minFrameRate 和 maxFrameRate 参数。 So can I call getUserMedia while my peer connection is in session?那么我可以在我的对等连接处于会话中时调用 getUserMedia 吗? Another similar use case, which find reasonable, is to be able to change the camera while I am already in a peer session?另一个类似的合理用例是能够在我已经处于对等会话中时更换相机? Without having to renegotiate SDPs, ICE, ... Is this doable?无需重新协商 SDP、ICE ……这可行吗?

You're asking several questions, and when this answer was first written, the short answer to most of them was: not yet (though I've since updated it thanks to DJ House's answer below !) .你问了几个问题,当这个答案第一次写出来时,对大多数问题的简短回答是:还没有(尽管我已经更新了它,感谢下面DJ House 的回答!)

applyConstraints应用约束

You should be able to alter constraints during an active session, using applyConstraints like this:您应该能够在活动会话期间更改约束,使用如下所示的applyConstraints

const videotrack = stream_from_getUserMedia.getVideoTracks()[0];
videotrack.applyConstraints({ frameRate: { max: 10 } });

Most implementations today are able to decimate frame rates and not just deliver the modes available in the camera.今天的大多数实现都能够降低帧速率,而不仅仅是提供相机中可用的模式。

Try this fiddle .试试这个小提琴

RTCRtpSender RTCRtpSender

You should be able to control encoding & transmission in the sender object using setParameters :您应该能够使用setParameters控制发送方对象中的编码和传输:

const pc = RTCPeerConnection(config);

const videotrack = stream.getVideoTracks()[0];
const sender = pc.addTrack(videotrack, stream);

// get the current parameters first
const params = sender.getParameters();

if (!params.encodings) params.encodings = [{}]; // Firefox workaround!

params.encodings[0].maxBitrate = 60000;
params.encodings[0].scaleResolutionDownBy = 2;
sender.setParameters(params);

encodings is an array, but unless simulcast is used, there's just one entry. encodings是一个数组,但除非使用联播,否则只有一个条目。

Try this fiddle !试试这个小提琴 (tested in Chrome, Firefox, Safari & Edge!) (在 Chrome、Firefox、Safari 和 Edge 中测试!)

RTCRtpSender.replaceTrack RTCRtpSender.replaceTrack

You should also be able to replace the camera track in an ongoing peer session, like this:您还应该能够在正在进行的对等会话中替换摄像机轨道,如下所示:

const videotrack2 = a_different_stream.getVideoTracks()[0];
await sender.replaceTrack(videotrack2);

It alters what the remote sees, without altering things at this end.它改变了远程看到的东西,而不是改变这端的东西。 Try it in this fiddle .这个 fiddle 中尝试一下。

For anyone who ends up here like I did (5 years later), I needed to get the parameters before setting them.对于像我一样(5 年后)来到这里的任何人,我需要在设置参数之前获取参数。 I am assuming since this was such an old question that the API just got a little out dated.我假设这是一个如此古老的问题,以至于 API 有点过时了。

Without getting the parameters first, I always got this error:没有先获取参数,我总是收到这个错误:

Failed to execute 'setParameters' on 'RTCRtpSender': required member codecs is undefined.

Here's a quick way to get and manipulate the current parameters:这是获取和操作当前参数的快速方法:

var pc = RTCPeerConnection(config);

var videotrack = stream.getVideoTracks()[0];
var sender = pc.addTrack(videotrack, stream);

// get the current parameters first
var params = sender.getParameters();

params.encodings[0].maxBitrate = 60000;
params.encodings[0].scaleResolutionDownBy = 2;

sender.setParameters(params);

This was still a great answer for me to quickly debug some Webrtc bandwidth related issues.对于我快速调试一些 Webrtc 带宽相关问题来说,这仍然是一个很好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM