Search code examples
webrtcopenwebrtc

what is the appropriate way to achieve offer to recieve and send in webRTC?


in my case after get everything done and running i want to migrate from add track to addTranciever

i have 2 peerConnection

yourConn = new RTCPeerConnection(servers);
yourConn2 = new RTCPeerConnection(servers);

and with following steps i see in many example casses i addTransciever like so

yourConn.addTransceiver(streams.getAudeoTracks()[0]);

how to recieve from yourConn peer ? and can i achieve that with send from peer 1 to peer 2 and p1 recieve from p2 with no need to negotiation again

what should i do also in ontrack event on both side with , should i use addTrack there or not if i wish

here yourConn2 event side here offer to send what about offer to recieve?

    yourConn2.ontrack = (e) => {
         e.transceiver.direction = 'sendrecv';
    await e.transceiver.sender.replaceTrack(remoteStream);
      
};

should i grap

 RemoteAudioFromlocal = yourConn2.getTransceivers()[0];

and i upgrade" the direction to sendrecv like so ?

RemoteAudioFromlocal.direction = "sendrecv"
      await RemoteAudioFromlocal.reciever.replaceTrack(remotePeerStramIn);

Solution

  • i will answer my question since i figuer it out from [Jan-Ivar Bruaroey blog1 i've discover all my question that i ask for with addTransceiver() in one side i can get Transceivers within onTrackEvent like so

     if (e.transceiver.receiver.track) {
              remoteVideo = document.getElementById("wbrtcremote");
              transceiversRemotePeer = new MediaStream([e.transceiver.receiver.track]);
                  remoteVideo.srcObject = transceiversRemotePeer
                
              }
    

    that's all what i need to know the same on other side but here with a minor differnce like you need to change the direction since

    The transceiver created by the sender is sendrecv by default with addtranciever side

    yourConn.addTransceiver(streams.getAudeoTracks()[0]);
    

    . This gets mirrored by a transceiver on the receiver side for the same mid. Here it's exposed in the ontrack event,

    yourConn2.ontrack = await e => {
      /* do something with e.track */
      e.transceiver.direction = 'sendrecv';
      await e.transceiver.sender.replaceTrack(receiverTrack);
    };
    

    but in an "offer to receive" use case you could obtain it via getTransceivers() or like above code with e.transceiver.sender

    on the receiver side(yourConn2), the direction is "downgraded" from sendrecv to recvonly because by default this transceiver is not configured to send anything back from receiverPc(yourConn2) to senderPc(yourConn).

    After all, it was just created in response to setRemoteDescription(offer). To fix this, you "upgrade" the direction to sendrecv and set a track to send.

    e.transceiver.direction = 'sendrecv';
    e.transceiver.sender.replaceTrack(localStream.getAudioTracks()[0]).then(() => {
          
            }); 
    

    If you do this prior to creating the local SDP answer on receiverPc, you should be able to achieve "sendrecv" without more SDP negotiations. The ontrack event is fired before the SRD promise is resolved, so any modification you do in that event should have completed before the SDP answer is created.