本文介绍了更改RTCPeerConnection的MediaStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从音频/视频流更改为屏幕共享流:



  peerConnection.removeStream(streamA) // __o_j_sep ...下面的截图
peerConnection.addStream(streamB)//下面的截图中的SSTREAM




  • streamA 是来自我的相机和麦克风的视频/音频流。 code> streamB 是我从我的扩展中获得的screencapture。

  • 它们都是做了反向(从屏幕截图切换到带相机的音频/视频)但不能发现有什么重大区别。



    peerConnection 对象实际上是由 。我可以这样访问它:

      var peerConnection = stack.o_stack.o_layer_dialog.ao_dialogs [1] .o_msession_mgr.ao_sessions [0 ] .o_pc 

    (是的,这看起来不正确,但没有官方的方式获得访问权限到同行连接)和。



    最初我试图用( streamB )的videoTrack改变 streamA 的videoTracks。 。有人建议我应该尝试重新协商Peer Connection(通过删除/添加Streams到它),因为addTrack不会触发。

    ,但维护者似乎很忙,没有有机会回应。






    * 1 备注: streamB 没有 videoTracks 属性?该流以HTML < video> 元素播放,似乎是工作。下面是我如何得到它:

      navigator.webkitGetUserMedia({
    audio:false,
    video: {
    必填:{
    chromeMediaSource:'desktop',
    chromeMediaSourceId:streamId,
    maxWidth:window.screen.width,
    maxHeight:window.screen.height
    //,maxFrameRate:3
    }
    }
    //成功回调
    },函数(localMediaStream){
    SSTREAM = localMediaStream; // streamB

    //失败回调
    },函数(错误){
    console.log(error);
    });

    它似乎还有一个 videoTrack :





    我正在运行:




    • OS X 10.9.3

    • Chrome版本35.0.1916.153

    解决方案

    要解答第一个问题,在修改MediaStream在主动对等连接中,peerconnection对象将触发 onnegotiationneeded 事件。您需要处理该事件并重新交换您的SDP。这背后的主要原因是为了让双方知道他们之间发送了什么流。当SDP被交换时,mediaStream ID被包括在内,并且如果有新的流具有新的ID(所有其他事物都相同的事件),则必须进行重新协商。

    对于第二个问题(大约 SSTREAM )。它确实包含视频轨道,但是 webkitMediaStreams 没有videotrack属性。但是,您可以通过自己的ID来抓取曲目。



    由于每种媒体类型都有多个曲目的可能性,因此录像机或录音机没有单一属性,但而是一个这样的数组。 .getVideoTracks()调用返回当前视频轨道的数组。因此,您可以通过指示其索引 .getVideoTracks()[0] 来抓取特定的视频轨道。

    I want to change from a audio/video stream to a "screensharing" stream:

    peerConnection.removeStream(streamA) // __o_j_sep... in Screenshots below
    peerConnection.addStream(streamB)  // SSTREAM in Screenshots below
    

    • streamA is a video/audio stream coming from my camera and microphone.
    • streamB is the screencapture I get from my extension.
    • They are both MediaStream objects that look like this:

    * 1 Remark

    But if I remove streamA from the peerConnection and addStream(streamB) like above nothing seems to happen.

    The following works as expected (the stream on both ends is removed and re-added)

    peerConnection.removeStream(streamA) // __o_j_sep...
    peerConnection.addStream(streamA) // __o_j_sep...
    

    More Details

    I have found this example which does "the reverse" (Switch from screen capture to audio/video with camera) but can't spot a significant difference.

    The peerConnection RTCPeerConnection object is actually created by this SIPML library source code available here. And I access it like this:

    var peerConnection = stack.o_stack.o_layer_dialog.ao_dialogs[1].o_msession_mgr.ao_sessions[0].o_pc
    

    (Yes, this does not look right, but there is no official way to get access to the Peer Connection see discussion here) and here.

    Originally I tried to just (ex)change the videoTracks of streamA with the videoTrack of streamB. See question here. It was suggested to me that I should try to renegotiate the Peer Connection (by removing/adding Streams to it), because the addTrack does not trigger a re-negotitation.

    I've also asked for help here but the maintainer seems very busy and didn't have a chance to respond yet.


    * 1 Remark: Why does streamB not have a videoTracks property? The stream plays in an HTML <video> element and seems to "work". Here is how I get it:

    navigator.webkitGetUserMedia({
      audio: false,
      video: {
        mandatory: {
          chromeMediaSource:  'desktop',
          chromeMediaSourceId: streamId,
          maxWidth: window.screen.width,
          maxHeight: window.screen.height
          //,   maxFrameRate: 3
        }
      }
      // success callback
    }, function(localMediaStream) {
      SSTREAM = localMediaStream; //streamB
    
      // fail callback
    }, function(error) {
      console.log(error);
    });
    

    it also seems to have a videoTrack:

    I'm running:

    • OS X 10.9.3
    • Chrome Version 35.0.1916.153

    解决方案

    To answer your first question, when modifying the MediaStream in an active peerconnection, the peerconnection object will fire an onnegotiationneeded event. You need to handle that event and re-exchange your SDPs. The main reason behind this is so that both parties know what streams are being sent between them. When the SDPs are exchanged, the mediaStream ID is included, and if there is a new stream with a new ID(event with all other things being equal), a re-negotiation must take place.

    For you second question(about SSTREAM). It does indeed contain video tracks but there is no videotrack attribute for webkitMediaStreams. You can grab tracks via their ID, however.

    Since there is the possibility of having numerous tracks for each media type, there is no single attribute for a videotrack or audiotrack but instead an array of such. The .getVideoTracks() call returns an array of the current videoTracks. So, you COULD grab a particular video track through indicating its index .getVideoTracks()[0].

    这篇关于更改RTCPeerConnection的MediaStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:50