Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Allowing SFU to work with multiple streamers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcottontensor committed Oct 25, 2023
1 parent 01d8056 commit c0e715c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Frontend/implementations/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Frontend/library/src/WebRtcPlayer/WebRtcPlayerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,10 @@ export class WebRtcPlayerController {
) {
// If there's a streamer ID in the URL and a streamer with this ID is connected, set it as the selected streamer
autoSelectedStreamerId = urlParams.get(OptionParameters.StreamerId);
} else if (messageStreamerList.ids.length > 0 && this.config.isFlagEnabled(Flags.WaitForStreamer)) {
// we're waiting for a streamer and there are multiple connected but none were auto selected
// select the first
autoSelectedStreamerId = messageStreamerList.ids[0];
}
if (autoSelectedStreamerId !== null) {
this.config.setOptionSettingValue(
Expand All @@ -1407,7 +1411,8 @@ export class WebRtcPlayerController {
} else {
// no auto selected streamer
if (this.config.isFlagEnabled(Flags.WaitForStreamer)) {
this.startAutoJoinTimer()
this.closeSignalingServer();
this.startAutoJoinTimer();
}
}
this.pixelStreaming.dispatchEvent(
Expand Down
7 changes: 7 additions & 0 deletions SFU/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ for(let arg of process.argv){
}

const config = {
// The URL of the signalling server to connect to
signallingURL: "ws://localhost:8889",

// The ID for this SFU to use. This will show up as a streamer ID on the signalling server
SFUId: "SFU",

// The ID of the streamer to subscribe to. If you leave this blank it will subscribe to the first streamer it sees.
subscribeStreamerId: "DefaultStreamer",

// Delay between list requests when looking for a specifc streamer.
retrySubscribeDelaySecs: 10,

mediasoup: {
Expand Down
14 changes: 7 additions & 7 deletions SFU/sfu_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ function connectSignalling(server) {

async function onSignallingConnected() {
console.log(`Connected to signalling server`);
//signalServer.send(JSON.stringify({type: 'listStreamers'}));
}

async function onStreamerList(msg) {
let success = false;

// subscribe to either the configured streamer, or if not configured, just grab the first id
if (msg.ids.length > 0) {
if (!!config.subscribeStreamerId) {
if (!!config.subscribeStreamerId && config.subscribeStreamerId.length != 0) {
if (msg.ids.includes(config.subscribeStreamerId)) {
signalServer.send(JSON.stringify({type: 'subscribe', streamerId: config.subscribeStreamerId}));
success = true;
Expand All @@ -51,15 +50,13 @@ async function onStreamerList(msg) {

if (!success) {
// did not subscribe to anything
console.log(`No subscribe (${config.retrySubscribeDelaySecs}`)
setTimeout(function() {
signalServer.send(JSON.stringify({type: 'listStreamers'}));
}, config.retrySubscribeDelaySecs * 1000);
}
}

async function onIdentify(msg) {
console.log(JSON.stringify({type: 'endpointId', id: config.SFUId}));
signalServer.send(JSON.stringify({type: 'endpointId', id: config.SFUId}));
signalServer.send(JSON.stringify({type: 'listStreamers'}));
}
Expand Down Expand Up @@ -97,6 +94,11 @@ function onStreamerDisconnected() {
}
streamer.transport.close();
streamer = null;
signalServer.send(JSON.stringify({type: 'stopStreaming'}));

setTimeout(function() {
signalServer.send(JSON.stringify({type: 'listStreamers'}));
}, config.retrySubscribeDelaySecs * 1000);
}
}

Expand Down Expand Up @@ -268,7 +270,7 @@ function onLayerPreference(msg) {
}

async function onSignallingMessage(message) {
console.log(`Got MSG: ${message}`);
//console.log(`Got MSG: ${message}`);
const msg = JSON.parse(message);

if (msg.type == 'offer') {
Expand Down Expand Up @@ -296,11 +298,9 @@ async function onSignallingMessage(message) {
onLayerPreference(msg);
}
else if (msg.type == 'streamerList') {
console.log('WA WA WEE WOO ----------------------------------------------------------------------');
onStreamerList(msg);
}
else if (msg.type == 'identify') {
console.log('identifying...');
onIdentify(msg);
}
}
Expand Down
5 changes: 4 additions & 1 deletion SignallingWebServer/cirrus.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ function onSFUMessageId(sfuPlayer, msg) {
}

function onSFUMessageStartStreaming(sfuPlayer, msg) {
logIncoming(sfuPlayer.streamer.id, msg);
if (streamers.has(sfuPlayer.streamer.id)) {
console.error(`SFU ${sfuPlayer.streamer.id} is already registered as a streamer and streaming.`)
return;
Expand All @@ -667,6 +668,7 @@ function onSFUMessageStartStreaming(sfuPlayer, msg) {
}

function onSFUMessageStopStreaming(sfuPlayer, msg) {
logIncoming(sfuPlayer.streamer.id, msg);
if (!streamers.has(sfuPlayer.streamer.id)) {
console.error(`SFU ${sfuPlayer.streamer.id} is not registered as a streamer or streaming.`)
return;
Expand All @@ -678,6 +680,7 @@ if (!streamers.has(sfuPlayer.streamer.id)) {
function onSFUDisconnected(sfuPlayer) {
console.log("disconnecting SFU from streamer");
disconnectAllPlayers(sfuPlayer.id);
onStreamerDisconnected(sfuPlayer.streamer);
sfuPlayer.unsubscribe();
sfuPlayer.ws.close(4000, "SFU Disconnected");
players.delete(sfuPlayer.id);
Expand Down Expand Up @@ -891,7 +894,7 @@ function disconnectAllPlayers(streamerId) {
if (player.streamerId == streamerId) {
// disconnect players but just unsubscribe the SFU
const sfuPlayer = getSFUForStreamer(streamerId);
if (player.id == sfuPlayer.id) {
if (sfuPlayer && player.id == sfuPlayer.id) {
sfuPlayer.unsubscribe();
} else {
player.ws.close();
Expand Down

0 comments on commit c0e715c

Please sign in to comment.