Peerjs: How do I call without a MediaStream?

Created on 8 Jan 2016  ·  5Comments  ·  Source: peers/peerjs

I have a use-case where:

  • there are two people, Presenter and Viewer
  • Presenter opens the page and gets a peer ID
  • Presenter sends that peer ID to Viewer
  • Viewer uses that peer ID to call Presenter
  • Presenter sends his shared screen to the viewer

For this use-case I need, for the Viewer side:

var call = peer.call(presenterPeerID, null);
call.on('stream', function(theirWebcamStream) {
  showWebcamStream(theirWebcamStream);
});

This does not work, because peer.call(presenterPeerID, null) returns undefined.

How do I call without sending a MediaStream?

Most helpful comment

You can create empty stream with any track, or with both:

export const createEmptyAudioTrack = () => {
  const ctx = new AudioContext();
  const oscillator = ctx.createOscillator();
  const dst = oscillator.connect(ctx.createMediaStreamDestination());
  oscillator.start();
  const track = dst.stream.getAudioTracks()[0];
  return Object.assign(track, { enabled: false });
};

export const createEmptyVideoTrack = ({ width, height }) => {
  const canvas = Object.assign(document.createElement('canvas'), { width, height });
  canvas.getContext('2d').fillRect(0, 0, width, height);

  const stream = canvas.captureStream();
  const track = stream.getVideoTracks()[0];

  return Object.assign(track, { enabled: false });
};

...

const audioTrack = createEmptyAudioTrack();
const videoTrack = createEmptyVideoTrack({ width:640, height:480 });
const mediaStream = new MediaStream([audioTrack, videoTrack]);

peer.call('id', mediaStream);

All 5 comments

Subscribe to this issue

Have you tried initiating the call from the presenter to the viewer, providing the presenters mediastream in the call and answering the call with null as mediastream?

I'm also hoping to do the same thing. See issue #158

+1

You can create empty stream with any track, or with both:

export const createEmptyAudioTrack = () => {
  const ctx = new AudioContext();
  const oscillator = ctx.createOscillator();
  const dst = oscillator.connect(ctx.createMediaStreamDestination());
  oscillator.start();
  const track = dst.stream.getAudioTracks()[0];
  return Object.assign(track, { enabled: false });
};

export const createEmptyVideoTrack = ({ width, height }) => {
  const canvas = Object.assign(document.createElement('canvas'), { width, height });
  canvas.getContext('2d').fillRect(0, 0, width, height);

  const stream = canvas.captureStream();
  const track = stream.getVideoTracks()[0];

  return Object.assign(track, { enabled: false });
};

...

const audioTrack = createEmptyAudioTrack();
const videoTrack = createEmptyVideoTrack({ width:640, height:480 });
const mediaStream = new MediaStream([audioTrack, videoTrack]);

peer.call('id', mediaStream);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kidandcat picture kidandcat  ·  8Comments

kahrkunne picture kahrkunne  ·  4Comments

bilo1967 picture bilo1967  ·  7Comments

lucastwong picture lucastwong  ·  3Comments

nhducseuit picture nhducseuit  ·  5Comments