Added rooms and connect to existing publisher

This commit is contained in:
Sergiu Toma 2022-07-27 09:55:07 +03:00
parent bb24a296ec
commit 5c88cd966d
4 changed files with 1493 additions and 1368 deletions

264
app.js
View File

@ -1,3 +1,9 @@
/**
* integrating mediasoup server with a node.js application
*/
/* Please follow mediasoup installation requirements */
/* https://mediasoup.org/documentation/v3/mediasoup/installation/ */
import express from 'express' import express from 'express'
const app = express() const app = express()
@ -6,9 +12,15 @@ import fs from 'fs'
import path from 'path' import path from 'path'
const __dirname = path.resolve() const __dirname = path.resolve()
// import { Server } from 'socket.io'
import Server from 'socket.io' import Server from 'socket.io'
import mediasoup from 'mediasoup' import mediasoup, { getSupportedRtpCapabilities } from 'mediasoup'
let worker
let router = {}
let producerTransport
let consumerTransport
let producer
let consumer
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send('Hello from mediasoup app!') res.send('Hello from mediasoup app!')
@ -27,9 +39,9 @@ httpsServer.listen(3000, () => {
console.log('listening on port: ' + 3000) console.log('listening on port: ' + 3000)
}) })
// const io = new Server(httpsServer)
const io = new Server(httpsServer) const io = new Server(httpsServer)
// socket.io namespace (could represent a room?)
const peers = io.of('/mediasoup') const peers = io.of('/mediasoup')
/** /**
@ -40,12 +52,6 @@ const peers = io.of('/mediasoup')
* |-> Consumer Transport(s) * |-> Consumer Transport(s)
* |-> Consumer * |-> Consumer
**/ **/
let worker
let routers = {}
let producerTransport
let consumerTransport
let producer
let consumer
const createWorker = async () => { const createWorker = async () => {
worker = await mediasoup.createWorker({ worker = await mediasoup.createWorker({
@ -88,145 +94,135 @@ const mediaCodecs = [
] ]
peers.on('connection', async socket => { peers.on('connection', async socket => {
console.log(socket.id)
console.log('[connection]');
socket.emit('connection-success', { socket.emit('connection-success', {
socketId: socket.id socketId: socket.id,
existsProducer: producer ? true : false,
}) })
socket.on('disconnect', () => { socket.on('disconnect', () => {
// do some cleanup // do some cleanup
console.log('[disconnect]') console.log('peer disconnected')
}) })
socket.on('create-router', async (data) => { socket.on('createRoom', async ({ callId }, callback) => {
const routerId = data.assetId; if (router[callId] === undefined) {
// worker.createRouter(options)
// options = { mediaCodecs, appData }
// mediaCodecs -> defined above
// appData -> custom application data - we are not supplying any
// none of the two are required
router[callId] = await worker.createRouter({ mediaCodecs })
console.log(`Router ID: ${router[callId].id}`)
}
console.log('[create-router]', routerId) getRtpCapabilities(callId, callback)
})
routers[routerId] = await worker.createRouter({ mediaCodecs }) const getRtpCapabilities = (callId, callback) => {
// console.log('🔴 Create routers', routers); const rtpCapabilities = router[callId].rtpCapabilities
// for (const key in routers) { callback({ rtpCapabilities })
// if (Object.hasOwnProperty.call(routers, key)) { }
// const element = routers[key];
// console.log('🔴', key, element); // Client emits a request to create server side Transport
// } // We need to differentiate between the producer and consumer transports
// } socket.on('createWebRtcTransport', async ({ sender, callId }, callback) => {
console.log(`Is this a sender request? ${sender} | callId ${callId}`)
// Client emits a request for RTP Capabilities // The client indicates if it is a producer or a consumer
// This event responds to the request // if sender is true, indicates a producer else a consumer
socket.on('getRtpCapabilities', (callback) => { if (sender)
console.log('[getRtpCapabilities]') producerTransport = await createWebRtcTransportLayer(callId, callback)
else
const rtpCapabilities = routers[routerId].rtpCapabilities consumerTransport = await createWebRtcTransportLayer(callId, callback)
})
// call callback from the client and send back the rtpCapabilities
callback({ rtpCapabilities }) // see client's socket.emit('transport-connect', ...)
socket.on('transport-connect', async ({ dtlsParameters }) => {
console.log('DTLS PARAMS... ', { dtlsParameters })
await producerTransport.connect({ dtlsParameters })
})
// see client's socket.emit('transport-produce', ...)
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
// call produce based on the prameters from the client
producer = await producerTransport.produce({
kind,
rtpParameters,
}) })
// Client emits a request to create server side Transport console.log('Producer ID: ', producer.id, producer.kind)
// We need to differentiate between the producer and consumer transports
socket.on('createWebRtcTransport', async ({ sender }, callback) => { producer.on('transportclose', () => {
console.log(`Is this a sender request? ${sender}`) console.log('transport for this producer closed ')
// The client indicates if it is a producer or a consumer producer.close()
// if sender is true, indicates a producer else a consumer
if (sender)
producerTransport = await createWebRtcTransport(routerId, callback)
else
consumerTransport = await createWebRtcTransport(routerId, callback)
}) })
// see client's socket.emit('transport-connect', ...) // Send back to the client the Producer's id
socket.on('transport-connect', async ({ dtlsParameters }) => { callback({
console.log('DTLS PARAMS... ', { dtlsParameters }) id: producer.id
await producerTransport.connect({ dtlsParameters })
})
// see client's socket.emit('transport-produce', ...)
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
// call produce based on the prameters from the client
producer = await producerTransport.produce({
kind,
rtpParameters,
})
console.log('Producer ID:', producer.id, producer.kind)
producer.on('transportclose', () => {
console.log('transport for this producer closed ')
producer.close()
})
// Send back to the client the Producer's id
callback({
id: producer.id
})
}) })
// see client's socket.emit('transport-recv-connect', ...) })
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
console.log(`DTLS PARAMS: ${dtlsParameters}`) // see client's socket.emit('transport-recv-connect', ...)
await consumerTransport.connect({ dtlsParameters }) socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
}) console.log(`DTLS PARAMS: ${dtlsParameters}`)
await consumerTransport.connect({ dtlsParameters })
socket.on('consume', async ({ rtpCapabilities }, callback) => { })
try {
console.log('[consume] Producer ID:', producer.id, producer.kind) socket.on('consume', async ({ rtpCapabilities, callId }, callback) => {
// check if the routers can consume the specified producer try {
if (routers[routerId].canConsume({ // check if the router can consume the specified producer
if (router[callId].canConsume({
producerId: producer.id,
rtpCapabilities
})) {
// transport can now consume and return a consumer
consumer = await consumerTransport.consume({
producerId: producer.id, producerId: producer.id,
rtpCapabilities rtpCapabilities,
})) { paused: true,
// transport can now consume and return a consumer
consumer = await consumerTransport.consume({
producerId: producer.id,
rtpCapabilities,
paused: true,
})
consumer.on('transportclose', () => {
console.log('transport close from consumer')
})
consumer.on('producerclose', () => {
console.log('producer of consumer closed')
})
// from the consumer extract the following params
// to send back to the Client
const params = {
id: consumer.id,
producerId: producer.id,
kind: consumer.kind,
rtpParameters: consumer.rtpParameters,
}
// send the parameters to the client
callback({ params })
} else {
console.log("Can't consume!!!", routers[routerId].canConsume());
}
} catch (error) {
console.log(error.message)
callback({
params: {
error: error
}
}) })
}
})
socket.on('consumer-resume', async () => {
console.log('consumer resume')
await consumer.resume()
})
consumer.on('transportclose', () => {
console.log('transport close from consumer')
})
consumer.on('producerclose', () => {
console.log('producer of consumer closed')
})
// from the consumer extract the following params
// to send back to the Client
const params = {
id: consumer.id,
producerId: producer.id,
kind: consumer.kind,
rtpParameters: consumer.rtpParameters,
}
// send the parameters to the client
callback({ params })
}
} catch (error) {
console.log(error.message)
callback({
params: {
error: error
}
})
}
})
socket.on('consumer-resume', async () => {
console.log('consumer resume')
await consumer.resume()
}) })
}) })
const createWebRtcTransportLayer = async (callId, callback) => {
const createWebRtcTransport = async (routerId, callback) => {
try { try {
console.log('createWebRtcTransportLayer | callId', callId);
// https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions // https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
const webRtcTransport_options = { const webRtcTransport_options = {
listenIps: [ listenIps: [
@ -239,9 +235,9 @@ const createWebRtcTransport = async (routerId, callback) => {
enableTcp: true, enableTcp: true,
preferUdp: true, preferUdp: true,
} }
console.log('🔴 router[callId]', JSON.stringify(router));
// https://mediasoup.org/documentation/v3/mediasoup/api/#routers-createWebRtcTransport // https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
let transport = await routers[routerId].createWebRtcTransport(webRtcTransport_options) let transport = await router[callId].createWebRtcTransport(webRtcTransport_options)
console.log(`transport id: ${transport.id}`) console.log(`transport id: ${transport.id}`)
transport.on('dtlsstatechange', dtlsState => { transport.on('dtlsstatechange', dtlsState => {
@ -268,7 +264,7 @@ const createWebRtcTransport = async (routerId, callback) => {
return transport return transport
} catch (error) { } catch (error) {
console.log(error) console.log('11111', error)
callback({ callback({
params: { params: {
error: error error: error

File diff suppressed because it is too large Load Diff

View File

@ -25,8 +25,6 @@
</head> </head>
<body> <body>
<body> <body>
<label for="assetId">My asset id:</label><br>
<input type="number" id="assetId" name="assetId"><br>
<div id="video"> <div id="video">
<table> <table>
<thead> <thead>
@ -49,11 +47,16 @@
<tr> <tr>
<td> <td>
<div id="sharedBtns"> <div id="sharedBtns">
<button id="btnLocalVideo">1. Get Local Video</button> <button id="btnLocalVideo">Publish</button>
</div>
</td>
<td>
<div id="sharedBtns">
<button id="btnRecvSendTransport">Consume</button>
</div> </div>
</td> </td>
</tr> </tr>
<tr> <!-- <tr>
<td colspan="2"> <td colspan="2">
<div id="sharedBtns"> <div id="sharedBtns">
<button id="btnRtpCapabilities">2. Get Rtp Capabilities</button> <button id="btnRtpCapabilities">2. Get Rtp Capabilities</button>
@ -76,7 +79,7 @@
<button id="btnConnectRecvTransport">7. Connect Recv Transport & Consume</button> <button id="btnConnectRecvTransport">7. Connect Recv Transport & Consume</button>
</div> </div>
</td> </td>
</tr> </tr> -->
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -1,57 +1,86 @@
const io = require("socket.io-client"); const io = require('socket.io-client')
const mediasoupClient = require('mediasoup-client') const mediasoupClient = require('mediasoup-client')
const urlParams = new URLSearchParams(location.search);
const ASSET_ID = 71 const ASSET_ID = parseInt(urlParams.get('assetId')) || null;
const ACCOUNT_ID = 1 const ACCOUNT_ID = parseInt(urlParams.get('accountId')) || null;
let callId = parseInt(urlParams.get('callId')) || null;
const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false
console.log('ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER)
let hub = io('https://hub.dev.linx.safemobile.com/', { let socket
reconnect: false, hub = io('https://hub.dev.linx.safemobile.com/')
transports: ['websocket'],
})
hub.on('connect', async () => { if (IS_PRODUCER === true) {
console.log('hub.connected', hub.connected); hub.on('connect', async () => {
hub.emit( console.log('hub.connected', hub.connected);
'ars', hub.emit(
JSON.stringify({ 'ars',
ars: true, JSON.stringify({
asset_id: ASSET_ID, ars: true,
account_id: ACCOUNT_ID, asset_id: ASSET_ID,
account_id: ACCOUNT_ID,
})
);
hub.on('video', (data) => {
const parsedData = JSON.parse(data);
console.log('video', parsedData)
originAssetId = parsedData.origin_asset_id;
originAssetName = parsedData.origin_asset_name;
originAssetTypeName = parsedData.origin_asset_type_name;
callId = parsedData.video_call_id;
console.log('IS_PRODUCER', IS_PRODUCER);
if (parsedData.type === 'notify-request' && IS_PRODUCER) {
getLocalStream()
}
// setTimeout(() => {
// !IS_PRODUCER && goConnect()
// }, 1000);
// getRtpCapabilities()
}) })
);
});
hub.on("connect_error", (error) => {
console.log('connect_error', error);
});
hub.on('connection', async socket => {
console.log('connection')
})
hub.on('disconnect', async socket => {
console.log('disconnect')
})
// ------------------------------------------------
const mediasoup = io('/mediasoup')
mediasoup.on('connection-success', ({ socketId }) => {
console.log('mediasoup.connected', mediasoup.connected)
mediasoup.emit('create-router', {
assetId: ASSET_ID
}) })
hub.on('connect_error', (error) => {
console.log('connect_error', error);
});
hub.on('connection', () => {
console.log('connection')
})
hub.on('disconnect', () => {
console.log('disconnect')
})
}
socket = io('https://localhost:3000/mediasoup')
socket.on('connection-success', ({ socketId, existsProducer }) => {
console.log(socketId, existsProducer)
// if (IS_PRODUCER) { getLocalStream() }
// setInterval(() => {
// if (!IS_PRODUCER && existsProducer) { goConnect() }
// }, 5000);
if (!IS_PRODUCER && existsProducer && consumer === undefined) { goConnect() }
}) })
let device let device
let rtpCapabilities let rtpCapabilities
let producerTransport let producerTransport
let consumerTransport let consumerTransport
let producer let producer
let consumer let consumer
let originAssetId
let originAssetName
let originAssetTypeName
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#ProducerOptions // https://mediasoup.org/documentation/v3/mediasoup-client/api/#ProducerOptions
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce // https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
@ -80,18 +109,19 @@ let params = {
} }
} }
const streamSuccess = async (stream) => { const streamSuccess = (stream) => {
localVideo.srcObject = stream localVideo.srcObject = stream
const track = stream.getVideoTracks()[0] const track = stream.getVideoTracks()[0]
params = { params = {
track, track,
...params ...params
} }
goConnect()
} }
const getLocalStream = () => { const getLocalStream = () => {
navigator.mediaDevices.getUserMedia({
navigator.getUserMedia({
audio: false, audio: false,
video: { video: {
width: { width: {
@ -103,11 +133,21 @@ const getLocalStream = () => {
max: 1080, max: 1080,
} }
} }
}, streamSuccess, error => { })
.then(streamSuccess)
.catch(error => {
console.log(error.message) console.log(error.message)
}) })
} }
const goConnect = () => {
device === undefined ? getRtpCapabilities() : goCreateTransport()
}
const goCreateTransport = () => {
IS_PRODUCER ? createSendTransport() : createRecvTransport()
}
// A device is an endpoint connecting to a Router on the // A device is an endpoint connecting to a Router on the
// server side to send/recive media // server side to send/recive media
const createDevice = async () => { const createDevice = async () => {
@ -121,7 +161,10 @@ const createDevice = async () => {
routerRtpCapabilities: rtpCapabilities routerRtpCapabilities: rtpCapabilities
}) })
console.log('RTP Capabilities', device.rtpCapabilities) console.log('Device RTP Capabilities', device.rtpCapabilities)
// once the device loads, create transport
goCreateTransport()
} catch (error) { } catch (error) {
console.log(error) console.log(error)
@ -134,19 +177,22 @@ const getRtpCapabilities = () => {
// make a request to the server for Router RTP Capabilities // make a request to the server for Router RTP Capabilities
// see server's socket.on('getRtpCapabilities', ...) // see server's socket.on('getRtpCapabilities', ...)
// the server sends back data object which contains rtpCapabilities // the server sends back data object which contains rtpCapabilities
mediasoup.emit('getRtpCapabilities', (data) => { socket.emit('createRoom', { callId }, (data) => {
console.log(`Router RTP Capabilities... ${data.rtpCapabilities}`) console.log(`Router RTP Capabilities... ${data.rtpCapabilities}`)
// we assign to local variable and will be used when // we assign to local variable and will be used when
// loading the client Device (see createDevice above) // loading the client Device (see createDevice above)
rtpCapabilities = data.rtpCapabilities rtpCapabilities = data.rtpCapabilities
// once we have rtpCapabilities from the Router, create Device
createDevice()
}) })
} }
const createSendTransport = () => { const createSendTransport = () => {
// see server's socket.on('createWebRtcTransport', sender?, ...) // see server's socket.on('createWebRtcTransport', sender?, ...)
// this is a call from Producer, so sender = true // this is a call from Producer, so sender = true
mediasoup.emit('createWebRtcTransport', { sender: true }, ({ params }) => { socket.emit('createWebRtcTransport', { sender: true, callId }, ({ params }) => {
// The server sends back params needed // The server sends back params needed
// to create Send Transport on the client side // to create Send Transport on the client side
if (params.error) { if (params.error) {
@ -168,7 +214,7 @@ const createSendTransport = () => {
try { try {
// Signal local DTLS parameters to the server side transport // Signal local DTLS parameters to the server side transport
// see server's socket.on('transport-connect', ...) // see server's socket.on('transport-connect', ...)
await mediasoup.emit('transport-connect', { await socket.emit('transport-connect', {
dtlsParameters, dtlsParameters,
}) })
@ -188,7 +234,7 @@ const createSendTransport = () => {
// with the following parameters and produce // with the following parameters and produce
// and expect back a server side producer id // and expect back a server side producer id
// see server's socket.on('transport-produce', ...) // see server's socket.on('transport-produce', ...)
await mediasoup.emit('transport-produce', { await socket.emit('transport-produce', {
kind: parameters.kind, kind: parameters.kind,
rtpParameters: parameters.rtpParameters, rtpParameters: parameters.rtpParameters,
appData: parameters.appData, appData: parameters.appData,
@ -201,6 +247,8 @@ const createSendTransport = () => {
errback(error) errback(error)
} }
}) })
connectSendTransport()
}) })
} }
@ -219,15 +267,32 @@ const connectSendTransport = async () => {
producer.on('transportclose', () => { producer.on('transportclose', () => {
console.log('transport ended') console.log('transport ended')
// close video track // close video track
}) })
const answer = {
origin_asset_id: ASSET_ID,
dest_asset_id: originAssetId,
type: 'notify-answer',
origin_asset_priority: 1,
origin_asset_type_name: originAssetTypeName,
origin_asset_name: originAssetName,
video_call_id: callId,
answer: 'accepted', // answer: 'rejected'
};
console.log('SEND answer', answer);
hub.emit(
'video',
JSON.stringify(answer)
);
} }
const createRecvTransport = async () => { const createRecvTransport = async () => {
console.log('createRecvTransport');
// see server's socket.on('consume', sender?, ...) // see server's socket.on('consume', sender?, ...)
// this is a call from Consumer, so sender = false // this is a call from Consumer, so sender = false
await mediasoup.emit('createWebRtcTransport', { sender: false }, ({ params }) => { await socket.emit('createWebRtcTransport', { sender: false, callId }, ({ params }) => {
// The server sends back params needed // The server sends back params needed
// to create Send Transport on the client side // to create Send Transport on the client side
if (params.error) { if (params.error) {
@ -249,7 +314,7 @@ const createRecvTransport = async () => {
try { try {
// Signal local DTLS parameters to the server side transport // Signal local DTLS parameters to the server side transport
// see server's socket.on('transport-recv-connect', ...) // see server's socket.on('transport-recv-connect', ...)
await mediasoup.emit('transport-recv-connect', { await socket.emit('transport-recv-connect', {
dtlsParameters, dtlsParameters,
}) })
@ -260,6 +325,8 @@ const createRecvTransport = async () => {
errback(error) errback(error)
} }
}) })
connectRecvTransport()
}) })
} }
@ -268,15 +335,16 @@ const connectRecvTransport = async () => {
// for consumer, we need to tell the server first // for consumer, we need to tell the server first
// to create a consumer based on the rtpCapabilities and consume // to create a consumer based on the rtpCapabilities and consume
// if the router can consume, it will send back a set of params as below // if the router can consume, it will send back a set of params as below
await mediasoup.emit('consume', { await socket.emit('consume', {
rtpCapabilities: device.rtpCapabilities, rtpCapabilities: device.rtpCapabilities,
callId
}, async ({ params }) => { }, async ({ params }) => {
if (params.error) { if (params.error) {
console.log('Cannot Consume') console.log('Cannot Consume')
return return
} }
console.log('params', params) console.log(params)
// then consume with the local consumer transport // then consume with the local consumer transport
// which creates a consumer // which creates a consumer
consumer = await consumerTransport.consume({ consumer = await consumerTransport.consume({
@ -289,19 +357,15 @@ const connectRecvTransport = async () => {
// destructure and retrieve the video track from the producer // destructure and retrieve the video track from the producer
const { track } = consumer const { track } = consumer
remoteVideo.srcObject = new MediaStream([track]) const stream = new MediaStream([track])
remoteVideo.srcObject = stream
// the server consumer started with media paused // the server consumer started with media paused
// so we need to inform the server to resume // so we need to inform the server to resume
console.log('consumer-resume'); socket.emit('consumer-resume')
mediasoup.emit('consumer-resume') console.log('consumer', consumer);
}) })
} }
btnLocalVideo.addEventListener('click', getLocalStream) btnLocalVideo.addEventListener('click', getLocalStream)
btnRtpCapabilities.addEventListener('click', getRtpCapabilities) btnRecvSendTransport.addEventListener('click', goConnect)
btnDevice.addEventListener('click', createDevice)
btnCreateSendTransport.addEventListener('click', createSendTransport)
btnConnectSendTransport.addEventListener('click', connectSendTransport)
btnRecvSendTransport.addEventListener('click', createRecvTransport)
btnConnectRecvTransport.addEventListener('click', connectRecvTransport)