mediasoup client side JavaScript SDK
Clone or download
Latest commit 08bac84 Dec 11, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist 2.4.6 Dec 11, 2018
lib ReactNative.js: fix removing a sending track Dec 11, 2018
.babelrc Update deps Dec 4, 2018
.esdoc.json Let's move the project to GitHub Jul 21, 2017
.eslintrc.js Update deps Sep 27, 2018
.gitignore Export a ES5 source tree (fixes #11) Dec 18, 2017
.npmignore Custom device (#55) Nov 16, 2018
MEDIASOUP_PROTOCOL.md Custom device (#55) Nov 16, 2018
README.md Update README.md Jan 4, 2018
TODO.md 2.0.7: Initial support for react-native + react-native-webrtc Jan 22, 2018
banner.txt 0.2.0 Aug 21, 2017
bower.json 2.0.2: fix bower.json Nov 6, 2017
gulpfile.js Update deps Sep 27, 2018
package-lock.json 2.4.6 Dec 11, 2018
package.json 2.4.6 Dec 11, 2018

README.md

mediasoup-client

JavaScript client side SDK for building mediasoup based applications.

Website and documentation

Usage example

import * as mediasoupClient from 'mediasoup-client';
import mySignalingChannel from './mySignalingChannel';

// mySignalingChannel is our app custom signaling mechanism to communicate
// with the server running the mediasoup Node.js app.
// 
// Here we assume that a mediasoup Room and a Peer (named 'alice') already
// exist in the server. This channel will be used to communicate with our
// associated remote Peer (among other custom messages exhange up to the app).
const channel = new mySignalingChannel(
  {
    url      : 'wss://myserver.test',
    peerName : 'alice',
    roomId   : 'demo1'
  }); 

// Create a local Room instance associated to the remote Room.
const room = new mediasoupClient.Room();

// Transport for sending our media.
let sendTransport;

// Transport for receiving media from remote Peers.
let recvTransport;


// Join the remote Room.
room.join('alice')
  .then((peers) =>
  {
    // Create the Transport for sending our media.
    sendTransport = room.createTransport('send');

    // Create the Transport for receiving media from remote Peers.
    recvTransport = room.createTransport('recv');

    // Handle Peers already in to the Room.
    for (const peer of peers)
    {
      handlePeer(peer);
    }
  })
  .then(() =>
  {
    // Get our mic and webcam.
    return navigator.mediaDevices.getUserMedia(
      {
        audio : true,
        video : true
      });
  })
  .then((stream) =>
  {
    const audioTrack = stream.getAudioTracks()[0];
    const videoTrack = stream.getVideoTracks()[0];

    // Create Producers for audio and video.
    const audioProducer = room.createProducer(audioTrack);
    const videoProducer = room.createProducer(videoTrack);

    // Send our audio.
    audioProducer.send(sendTransport)
      .then(() => console.log('sending our mic'));

    // Send our video.
    videoProducer.send(sendTransport)
      .then(() => console.log('sending our webcam'));
  });


// Event fired when a new remote Peer joins the Room.
room.on('newpeer', (peer) =>
{
  console.log('a new Peer joined the Room: %s', peer.name);

  // Handle the Peer.
  handlePeer(peer);
});


// Be ready to send mediasoup client requests to our remote mediasoup Peer in
// the server, and also deal with their associated responses.
room.on('request', (request, callback, errback) =>
{
  channel.send({ type: 'mediasoup-request', body: request })
    .then((response) =>
    {
      // Success response, so pass the mediasoup response to the local Room.
      callback(response.body);
    })
    .catch((error) =>
    {
      // Error response, so pass the error to the local Room.
      errback(error);
    });
});


// Be ready to send mediasoup client notifications to our remote mediasoup
// Peer in the server
room.on('notify', (notification) =>
{
  channel.send({ type: 'mediasoup-notification', body: notification });
});


// Be ready to receive mediasoup notifications from our remote mediasoup Peer
// in the server.
channel.on('message', (message) =>
{
  if (message.type === 'mediasoup-notification')
  {
    // Pass the mediasoup notification to the local Room.
    room.receiveNotification(message.body);
  }
  else
  {
    // Handle here app custom messages (chat, etc).
  }
});


function handlePeer(peer)
{
  // Handle all the Consumers in the Peer.
  for (const consumer of peer.consumers)
  {
    handleConsumer(consumer);
  }

  // Event fired when the remote Room or Peer is closed.
  peer.on('close', () =>
  {
    console.log('Peer closed');
  });

  // Event fired when the remote Peer sends a new media to mediasoup server.
  peer.on('newconsumer', (consumer) =>
  {
    console.log('Got a new Consumer');

    // Handle the Consumer.
    handleConsumer(consumer);
  });
}


function handleConsumer(consumer)
{
  // Receive the media over our receiving Transport.
  consumer.receive(recvTransport)
    .then((track) =>
    {
      console.log('receiving a new remote MediaStreamTrack');

      // Attach the track to a MediaStream and play it.
    });

  // Event fired when the Consumer is closed.
  consumer.on('close', () =>
  {
    console.log('Consumer closed');
  });
}

Authors

License

ISC