Bind mediaStream to <Video> element with JsSIP Bad Media Description

1.6k views Asked by At

I've been trying to make a simple video-calling interface with JsSIP, so far I've only managed to init a videocall and the receiver gets my audio and video streams, but when I'm trying to add the stream (local or remote) to a <video> element in my page I get the error "Bad Media Description".

My code goes something like this:

    const address = "my.address.com";
    const pass = "pass";
    const callee = "id2";
    const user = "id1";
    const sockets = [];
    const localStream = new MediaStream();
    const config = {
      sockets: sockets,
      uri: `sip:${user}@${address}`,
      password: pass
    };
        const agent = new JsSIP.UA(config);
    const servers = {
      iceServers: [
        {urls:"stun:stun.l.google.com:19302"}
      ]
    };
    const options = {
      pcConfig: servers,
      mediaConstraints: {
        audio: true,
        video: true
      }
    };
    
    document.getElementById("localVideo").srcObject = localStream;
    document.getElementById("buttonCall").addEventListener("click", call);
    
    sockets.push(
      new JsSIP
      .WebSocketInterface(`wss://${address}:443/ws`)
    );
    
    agent.start();
    
        agent.on("newRTCSession", function(data){
          let dataSession = data.session;
          
          dataSession.on("confirmed",function(e){
            let localTracks = dataSession.connection.getSenders();
            localStream.addTrack(localTracks[0].track);
            localStream.addTrack(localTracks[1].track);
            console.log(e);
            document.getElementById("localVideo").play();
          });
        });
        function call() {
            agent
            .call(
            `sip:${callee}@${address}`,
            options
            );
        }

I would appreciate if anybody can point me the right way to make this possible.

1

There are 1 answers

0
Eduardo Galeano On

I've solved it, my problem was that I was using Vue, so by having functions inside my mounted() function the reference to this was being overwritten, the solution was change those functions by arrow functions () => {} like this:

    agent.on("newRTCSession", (data) => {
      let dataSession = data.session;
      
      dataSession.on("confirmed", () => {
        let localTracks = dataSession.connection.getSenders();
        localStream.addTrack(localTracks[0].track);
        localStream.addTrack(localTracks[1].track);
        console.log(e);
        this.$refs.localVideo.play();
      });
    });