webrtcopenwebrtc

how i can get audio from localPeer andRemotePeer if i use addTransceiver?[edit]


what i try

to implenet this steps as possiabe as i can from what i understanding and what i reading and what other people move through from question problem that what i can gather and implementing my code AS They do

what i expect

every thing working fine and gathering more knowledge about using webRTC for further project

steps i move through

 1- addTransceiver on localPeer
 
 2-getTransceiver On remote Peer

 

so this my code 2 peers of connection

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

and i'm stuck here after i do addTranceiver and reverse direction in remotePeer to sendrecv

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

how i can after that obtain audio from remotePeer?


Solution

  • i will answer my question

    what i did and working fine is to usning event in ontrackEvent and accessing the transceiver on both lacal and remote with

    for remote peer

    and for sending just reverse the direction and send what u need

    yourConn2.ontrack = await e => {
      /* do something with e.track */
    
    //receive here
       if (e.transceiver.receiver.track) {
              remoteVideo = document.getElementById("wbrtcremote");
              transceiversRemotePeer = new 
              MediaStream([e.transceiver.receiver.track]);
              remoteVideo.srcObject = transceiversRemotePeer  
              }
    
       //send here
      e.transceiver.direction = 'sendrecv';
      await e.transceiver.sender.replaceTrack(localstream);
    };
    

    for more details about how u can do it and why u need to reverse direction to e.transceiver.direction = 'sendrecv';

    that because 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. like so as above

    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.