Skip to content

Commit 81831cf

Browse files
authored
Add libmediasoup data channel demo (#101)
1 parent cd7cbb2 commit 81831cf

File tree

3 files changed

+186
-3
lines changed

3 files changed

+186
-3
lines changed

app/lib/RoomClient.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,6 @@ export default class RoomClient
21472147

21482148
setTimeout(() => audioTrack.stop(), 120000);
21492149
}
2150-
21512150
// Create mediasoup Transport for sending (unless we don't want to produce).
21522151
if (this._produce)
21532152
{

server/lib/Room.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,128 @@ class Room extends EventEmitter
643643
};
644644
}
645645

646+
/**
647+
* Create a mediasoup DataConsumer associated to a Broadcaster.
648+
*
649+
* @async
650+
*
651+
* @type {String} broadcasterId
652+
* @type {String} transportId
653+
* @type {String} dataProducerId
654+
*/
655+
async createBroadcasterDataConsumer(
656+
{
657+
broadcasterId,
658+
transportId,
659+
dataProducerId
660+
}
661+
)
662+
{
663+
const broadcaster = this._broadcasters.get(broadcasterId);
664+
665+
if (!broadcaster)
666+
throw new Error(`broadcaster with id "${broadcasterId}" does not exist`);
667+
668+
if (!broadcaster.data.rtpCapabilities)
669+
throw new Error('broadcaster does not have rtpCapabilities');
670+
671+
const transport = broadcaster.data.transports.get(transportId);
672+
673+
if (!transport)
674+
throw new Error(`transport with id "${transportId}" does not exist`);
675+
676+
const dataConsumer = await transport.consumeData(
677+
{
678+
dataProducerId
679+
});
680+
681+
// Store it.
682+
broadcaster.data.dataConsumers.set(consumer.id, consumer);
683+
684+
// Set Consumer events.
685+
dataConsumer.on('transportclose', () =>
686+
{
687+
// Remove from its map.
688+
broadcaster.data.dataConsumers.delete(consumer.id);
689+
});
690+
691+
dataConsumer.on('dataproducerclose', () =>
692+
{
693+
// Remove from its map.
694+
broadcaster.data.dataConsumers.delete(consumer.id);
695+
});
696+
697+
return {
698+
id : dataConsumer.id
699+
};
700+
}
701+
702+
/**
703+
* Create a mediasoup DataProducer associated to a Broadcaster.
704+
*
705+
* @async
706+
*
707+
* @type {String} broadcasterId
708+
* @type {String} transportId
709+
*/
710+
async createBroadcasterDataProducer(
711+
{
712+
broadcasterId,
713+
transportId,
714+
label,
715+
protocol,
716+
sctpStreamParameters,
717+
appData
718+
}
719+
)
720+
{
721+
const broadcaster = this._broadcasters.get(broadcasterId);
722+
723+
if (!broadcaster)
724+
throw new Error(`broadcaster with id "${broadcasterId}" does not exist`);
725+
726+
// if (!broadcaster.data.sctpCapabilities)
727+
// throw new Error('broadcaster does not have sctpCapabilities');
728+
729+
const transport = broadcaster.data.transports.get(transportId);
730+
731+
if (!transport)
732+
throw new Error(`transport with id "${transportId}" does not exist`);
733+
734+
const dataProducer = await transport.produceData(
735+
{
736+
sctpStreamParameters,
737+
label,
738+
protocol,
739+
appData
740+
});
741+
742+
// Store it.
743+
broadcaster.data.dataProducers.set(consumer.id, consumer);
744+
745+
// Set Consumer events.
746+
dataProducer.on('transportclose', () =>
747+
{
748+
// Remove from its map.
749+
broadcaster.data.dataProducers.delete(consumer.id);
750+
});
751+
752+
// // Optimization: Create a server-side Consumer for each Peer.
753+
// for (const peer of this._getJoinedPeers())
754+
// {
755+
// this._createDataConsumer(
756+
// {
757+
// dataConsumerPeer : peer,
758+
// dataProducerPeer : broadcaster,
759+
// dataProducer: dataProducer
760+
// });
761+
// }
762+
763+
return {
764+
id : dataProducer.id
765+
};
766+
}
767+
646768
_handleAudioLevelObserver()
647769
{
648770
this._audioLevelObserver.on('volumes', (volumes) =>

server/server.js

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ async function createExpressApp()
224224
async (req, res, next) =>
225225
{
226226
const { broadcasterId } = req.params;
227-
const { type, rtcpMux, comedia } = req.body;
227+
const { type, rtcpMux, comedia, sctpCapabilities } = req.body;
228228

229229
try
230230
{
@@ -233,7 +233,8 @@ async function createExpressApp()
233233
broadcasterId,
234234
type,
235235
rtcpMux,
236-
comedia
236+
comedia,
237+
sctpCapabilities
237238
});
238239

239240
res.status(200).json(data);
@@ -333,6 +334,67 @@ async function createExpressApp()
333334
}
334335
});
335336

337+
/**
338+
* POST API to create a mediasoup DataConsumer associated to a Broadcaster.
339+
* The exact Transport in which the DataConsumer must be created is signaled in
340+
* the URL path. Query body must include the desired producerId to
341+
* consume.
342+
*/
343+
expressApp.post(
344+
'/rooms/:roomId/broadcasters/:broadcasterId/transports/:transportId/consume/data',
345+
async (req, res, next) =>
346+
{
347+
const { broadcasterId, transportId } = req.params;
348+
const { dataProducerId } = req.body;
349+
350+
try
351+
{
352+
const data = await req.room.createBroadcasterDataConsumer(
353+
{
354+
broadcasterId,
355+
transportId,
356+
dataProducerId
357+
});
358+
359+
res.status(200).json(data);
360+
}
361+
catch (error)
362+
{
363+
next(error);
364+
}
365+
});
366+
367+
/**
368+
* POST API to create a mediasoup DataProducer associated to a Broadcaster.
369+
* The exact Transport in which the DataProducer must be created is signaled in
370+
*/
371+
expressApp.post(
372+
'/rooms/:roomId/broadcasters/:broadcasterId/transports/:transportId/produce/data',
373+
async (req, res, next) =>
374+
{
375+
const { broadcasterId, transportId } = req.params;
376+
const { label, protocol, sctpStreamParameters, appData } = req.body;
377+
378+
try
379+
{
380+
const data = await req.room.createBroadcasterDataProducer(
381+
{
382+
broadcasterId,
383+
transportId,
384+
label,
385+
protocol,
386+
sctpStreamParameters,
387+
appData
388+
});
389+
390+
res.status(200).json(data);
391+
}
392+
catch (error)
393+
{
394+
next(error);
395+
}
396+
});
397+
336398
/**
337399
* Error handler.
338400
*/

0 commit comments

Comments
 (0)