Rifat
Rifat

Reputation: 1888

How make webrtc fake media streaming work for one way communication?

I need to make one way webrtc communication. So a fake media stream from one way can be useful. So far I was not able to put a fake stream in RTCPeerConnection. Sending stream from both way works. I tried multiple ways. Can anyone tell me a good way to achieve this?

  1. Tried to get media devices with configuration all set to false. But at least one is required.
    navigator.mediaDevices
        .getUserMedia({
          audio: false,
        })
        .then(getUserMediaSuccess)
        .catch(errorHandler);
  1. Tried to create fake media capture with canvas from here, but could not succeed.

  2. Tried to add new RTCPeerConnection().addTransceiver().receiver.track. But not sure what it is and how to use it properly.


     function start(isCaller) {
     
      //createMediaStreamFake();
      peerConnection.current = new RTCPeerConnection(peerConnectionConfig);
      peerConnection.current.onicecandidate = gotIceCandidate;
      peerConnection.current.ontrack = gotRemoteStream;
      //peerConnection.current.addStream(localStream.current);
      peerConnection.current.addStream(
       new RTCPeerConnection().addTransceiver().receiver.track
      );
     } 

Upvotes: 1

Views: 1158

Answers (1)

Philipp Hancke
Philipp Hancke

Reputation: 17275

You do not need any fake media stream. You can call

pc.createOffer({offerToReceiveAudio: true})

which will indicate to the other side that even though you are not going to send audio, you still want to negotiate and receive audio. This works in Chrome and Firefox, however since that API is declared "legacy" Safari could not be bothered to implemented it, requiring you to call

const t = pc.addTransceiver('audio');
t.direction = 'recvonly';

instead. The adapter.js polyfill does this automatically for you.

Upvotes: 5

Related Questions