javascriptwebrtcscreensharingdesktop-sharingsipjs

How to send (Screen sharing stream) via SIPJS to the other caller


I'm using SIPJS to make calls between 2 callers using web browser.

Now i want to add (Screen sharing) feature , so far i managed to open chrome screen sharing window and i get the stream and played it in video element.

But what i really need is to send this stream to the other caller so he can see my screen sharing.

What I've tried so far:

video: {
mandatory: {
chromeMediaSource: 'desktop',
// chromeMediaSourceId: event.data.sourceId,
maxWidth: window.screen.width > 1920 ? window.screen.width : 1920,
maxHeight: window.screen.height > 1080 ? window.screen.height : 1080
},
optional: []
}
navigator.mediaDevices.getDisplayMedia(constraints)
    .then(function(stream) {
      //We've got media stream
      console.log("----------then triggered-------------");
      var options = {
            sessionDescriptionHandlerOptions: {
              constraints: {
                  audio: true,
                  video: stream
              }
            }
          } 
        pub_session = userAgent.invite(reciver_name,options);
    })
    .catch(function(error) {
      console.log("----------catch-------------");
      console.log(error);
    });

also didn't work.

Here is my Code

First get the screen sharing stream and send it to the other user

// Get screen sharing and send it.
 navigator.mediaDevices.getDisplayMedia(constraints)
    .then(function(stream) {
      //We've got media stream
      console.log("----------then triggered-------------");
      var pc = session.sessionDescriptionHandler.peerConnection;
      stream.getTracks().forEach(function(track) {
        pc.addTrack(track, stream);
      });      
    })
    .catch(function(error) {
      console.log("----------catch-------------");
      console.log(error);
    });

Then catch that stream at the other side

// Reciving stream or track
userAgent.on('invite', function (session) {
    session.on('trackAdded', function() {
        console.log('-------------trackAdded triggered--------------');
      });

      session.on('addTrack', function (track) {
        console.log('-------------addTrack triggered--------------');
      });

      session.on('addStream', function (stream) {
        console.log('-------------addStream triggered--------------');
      });

      session.on('stream', function (stream) {
        console.log('-------------stream triggered--------------');
      });
});

But still get nothing from that code above

So how can i pass that stream or track to the other caller after the call starts ?

thank you so much


Solution

  • I Found the solution from some great gentlemen in SIPJS groups

    Hope the answer will help someone as it helped me

    var option = {video: {mediaSource: 'screen'}, audio: true};
    navigator.mediaDevices.getDisplayMedia(option)
    .then(function(streams){
      var pc = session.sessionDescriptionHandler.peerConnection;
      var videoTrack = streams.getVideoTracks()[0];
      var sender = pc.getSenders().find(function(s) {   
        return s.track.kind == videoTrack.kind;
      }); 
      console.log('found sender:', sender);
      sender.replaceTrack(videoTrack);
    }, function(error){
      console.log("error ", error);
    });