Last active
April 10, 2017 15:57
-
-
Save MatthieuLemoine/71d21ad38f094e0e0bf7b82dd2e2091e to your computer and use it in GitHub Desktop.
Mediasoup : Link audio/video streams with participants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 })); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, just one consideration. I've modified the API, so
rtpSender.associatedPeer
no longer exists. Instead, now there is:The doc is updated: https://mediasoup.org/api/