Skip to content

Instantly share code, notes, and snippets.

@MatthieuLemoine
Last active April 10, 2017 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthieuLemoine/71d21ad38f094e0e0bf7b82dd2e2091e to your computer and use it in GitHub Desktop.
Save MatthieuLemoine/71d21ad38f094e0e0bf7b82dd2e2091e to your computer and use it in GitHub Desktop.
Mediasoup : Link audio/video streams with participants
// You need to setup a working mediasoup SFU server first
// see : https://mediasoup.org/api/#webrtc
// Map userId => streamId
// Local to a room
const userStreams = {};
// Mediasoup peer
const mediaPeer = mediaRoom.Peer(participant.id);
// New stream added to the room. Can be either audio or video.
// Will be call twice if video & audio
mediaPeer.on('newrtpsender', (rtpSender) => {
// Stream id i.e client MediaStream.id
const msid = rtpSender.rtpParameters.userParameters.msid.split(/\s/)[0];
// Stream's owner
const ownerPeer = rtpSender.associatedPeer;
// Owner's id
const ownerId = ownerPeer.name;
// Add stream to map
const added = newStream({ streamId : msid, ownerId });
// Send map only if something has changed in userStreams map
if (added) {
userStreamMapUpdate(appRoom.getParticipantsWithoutUser({ id : ownerId }));
}
});
function newStream({ streamId, ownerId }) {
if (!this.userStreams[ownerId]) {
this.userStreams[ownerId] = [streamId];
return true;
}
const streams = this.userStreams[ownerId];
// Avoid duplicates
// Handle audio & video as an unique stream
if (!streams.includes(streamId)) {
streams.push(streamId);
return true;
}
return false;
}
function userStreamMapUpdate(participants) {
// Send new map value to all room participants
participants.forEach(({ socket }) => socket.emit('stream:update', { userStreams }));
}
@ibc
Copy link

ibc commented Apr 10, 2017

Hi, just one consideration. I've modified the API, so rtpSender.associatedPeer no longer exists. Instead, now there is:

rtpReceiver.peer  // The Peer owner of the RtpReceiver
rtpSender.peer  // The Peer owner of the RtpSender
rtpSender.associatedRtpReceiver  // The associated RtpReceiver that triggered the creation of this RtpSender

The doc is updated: https://mediasoup.org/api/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment