webrtcsimple-peer

How to add Video track and remove it using simple-peer


I am using simple-peer in my video chat web application. If both the users are in audio call how can I add Video track and how can I disable it. If I use replaceTrack I am again which is giving this issue

error Error: [object RTCErrorEvent]
at makeError (index.js:17)
at RTCDataChannel._channel.onerror (index.js:490)

I am showing a profile picture if the video is not enabled for users. if Video is enabled I want to replace this picture with video and replace it for all people in the call


Solution

  • If both the users enabled audio only, stream contain only audio track so here we can add black space (ended video track ).so we can easily solve this issue for more info visit this https://blog.mozilla.org/webrtc/warm-up-with-replacetrack/

    Code from the above link

    
        let silence = () => {
          let ctx = new AudioContext(), oscillator = ctx.createOscillator();
          let dst = oscillator.connect(ctx.createMediaStreamDestination());
          oscillator.start();
          return Object.assign(dst.stream.getAudioTracks()[0], {enabled: false});
        }
        
        let black = ({width = 640, height = 480} = {}) => {
          let canvas = Object.assign(document.createElement("canvas"), {width, height});
          canvas.getContext('2d').fillRect(0, 0, width, height);
          let stream = canvas.captureStream();
          return Object.assign(stream.getVideoTracks()[0], {enabled: false});
        }
        
        let blackSilence = (...args) => new MediaStream([black(...args), silence()]);
        
        video.srcObject = blackSilence();