Skip to content

Commit

Permalink
Merge pull request #12 from keianhzo/get-peers-support
Browse files Browse the repository at this point in the history
Support for getting peers and closing transports
  • Loading branch information
keianhzo authored Mar 10, 2021
2 parents 758058a + 35ee983 commit eaee7b8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 36 deletions.
2 changes: 1 addition & 1 deletion habitat/plan.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pkg_name=janus-gateway
pkg_origin=mozillareality
pkg_maintainer="Mozilla Mixed Reality <mixreality@mozilla.com>"
pkg_version="2.0.0"
pkg_version="2.0.1"
pkg_description="A simple mediasoup based SFU"

pkg_deps=(
Expand Down
111 changes: 76 additions & 35 deletions lib/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,44 @@ class Room extends EventEmitter
});
}

async _consumeExistingProducers(peer, joinedPeers) {
for (const joinedPeer of joinedPeers)
{
// Create Consumers for existing Producers.
for (const producer of joinedPeer.data.producers.values())
{
await this._createConsumer(
{
consumerPeer : peer,
producerPeer : joinedPeer,
producer
});
}

// Create DataConsumers for existing DataProducers.
for (const dataProducer of joinedPeer.data.dataProducers.values())
{
if (dataProducer.label === 'bot')
continue;

await this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : joinedPeer,
dataProducer
});
}
}

// Create DataConsumers for bot DataProducer.
await this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : null,
dataProducer : this._bot.dataProducer
});
}

/**
* Handle protoo requests from browsers.
*
Expand Down Expand Up @@ -764,41 +802,7 @@ class Room extends EventEmitter
// Mark the new Peer as joined.
peer.data.joined = true;

for (const joinedPeer of joinedPeers)
{
// Create Consumers for existing Producers.
for (const producer of joinedPeer.data.producers.values())
{
this._createConsumer(
{
consumerPeer : peer,
producerPeer : joinedPeer,
producer
});
}

// Create DataConsumers for existing DataProducers.
for (const dataProducer of joinedPeer.data.dataProducers.values())
{
if (dataProducer.label === 'bot')
continue;

this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : joinedPeer,
dataProducer
});
}
}

// Create DataConsumers for bot DataProducer.
this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : null,
dataProducer : this._bot.dataProducer
});
await this._consumeExistingProducers(peer, joinedPeers);

// Notify the new Peer to all other Peers.
for (const otherPeer of this._getJoinedPeers({ excludePeer: peer }))
Expand All @@ -816,6 +820,25 @@ class Room extends EventEmitter
break;
}

case 'refreshConsumers':
{
// Ensure the Peer is already joined.
if (!peer.data.joined)
throw new Error('Peer not joined');

accept();

const joinedPeers =
[
...this._getJoinedPeers({ excludePeer: peer }),
...this._broadcasters.values()
];

await this._consumeExistingProducers(peer, joinedPeers);

break;
}

case 'createWebRtcTransport':
{
// NOTE: Don't require that the Peer is joined here, so the client can
Expand Down Expand Up @@ -882,6 +905,10 @@ class Room extends EventEmitter
// Store the WebRtcTransport into the protoo Peer data Object.
peer.data.transports.set(transport.id, transport);

transport.observer.on('close', () => {
peer.data.transports.delete(transport.id);
});

accept(
{
id : transport.id,
Expand All @@ -903,6 +930,20 @@ class Room extends EventEmitter
break;
}

case 'closeWebRtcTransport': {
const { transportId } = request.data;
const transport = peer.data.transports.get(transportId);

if (!transport)
throw new Error(`transport with id "${transportId}" not found`);

transport.close();

accept();

break;
}

case 'connectWebRtcTransport':
{
const { transportId, dtlsParameters } = request.data;
Expand Down

0 comments on commit eaee7b8

Please sign in to comment.