WebRTC - 在通信过程中更改视频分辨率

WebRTC - change video resolution in the middle of communication

我知道我可以在初始化状态下定义视频流分辨率:

var video_constraints = {
  mandatory: {
    maxHeight: 480,
    maxWidth: 640 
  },
  optional: []
};

navigator.getUserMedia({
  audio: false,
  video: video_constraints
}, onsuccess);

我想知道有什么方法可以在通信过程中更改视频流分辨率,即在初始化之后?

规范中有 MediaStreamTrack.applyConstraints(),但目前浏览器似乎不支持它,或者它可能已被删除?对我来说,目前看来这是不可能的。另请查看 this question at SO.

只能按照上面的问题改流了。一种可能性是创建一个具有更高分辨率的新流,添加该流并替换另一侧的流。之后你可以stop/detach流。

我也遇到过同样的情况。所以我用了这个技巧。

(01) 用新的视频约束替换本地流。

(02) 用新的流轨道替换了发送者轨道。

function gotLocalStream(mediaStream) {
    localVideo.srcObject = mediaStream;

    const track = mediaStream.getVideoTracks()[0];
    const constraints = track.getConstraints();

    changePeerTrack()
};


async function startMyLocalStream(constraints){

   return new Promise((resolve,reject)=>{
     try{
         if (localStream) {
            localStream.getTracks().forEach(track => {
                track.stop();
            });
         }

         navigator.mediaDevices.getUserMedia(constraints)
            .then(gotLocalStream)
            .then(()=>{
                resolve(true)
            })
            .catch(e => {
                mediaError('getUserMedia', e.message, e.name);
            });
       }
       catch (e){
          //todo over constraints error
          console.error(e);
          reject(e);
       }
    })
}

function changePeerTrack(){
   if(localPeerConnection){
      localStream.getTracks().forEach( (track)=>{
         var sender = localPeerConnection.getSenders().find(function(s) {
             return s.track.kind == track.kind;
         });
         sender.replaceTrack(track);
      });
   }
 }

/**
 * change the video quality
**/
let constraints = {
    video: {width: {exact: 1280}, height: {exact: 720}, facingMode: "user"}
}
startMyLocalStream(constraints)

它对我有用。 You can check your browser compatibilities here