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

246
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'
const app = express()
@ -6,9 +12,15 @@ import fs from 'fs'
import path from 'path'
const __dirname = path.resolve()
// 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) => {
res.send('Hello from mediasoup app!')
@ -27,9 +39,9 @@ httpsServer.listen(3000, () => {
console.log('listening on port: ' + 3000)
})
// const io = new Server(httpsServer)
const io = new Server(httpsServer)
// socket.io namespace (could represent a room?)
const peers = io.of('/mediasoup')
/**
@ -40,12 +52,6 @@ const peers = io.of('/mediasoup')
* |-> Consumer Transport(s)
* |-> Consumer
**/
let worker
let routers = {}
let producerTransport
let consumerTransport
let producer
let consumer
const createWorker = async () => {
worker = await mediasoup.createWorker({
@ -88,145 +94,135 @@ const mediaCodecs = [
]
peers.on('connection', async socket => {
console.log('[connection]');
console.log(socket.id)
socket.emit('connection-success', {
socketId: socket.id
socketId: socket.id,
existsProducer: producer ? true : false,
})
socket.on('disconnect', () => {
// do some cleanup
console.log('[disconnect]')
console.log('peer disconnected')
})
socket.on('create-router', async (data) => {
const routerId = data.assetId;
socket.on('createRoom', async ({ callId }, callback) => {
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 })
// console.log('🔴 Create routers', routers);
const getRtpCapabilities = (callId, callback) => {
const rtpCapabilities = router[callId].rtpCapabilities
// for (const key in routers) {
// if (Object.hasOwnProperty.call(routers, key)) {
// const element = routers[key];
// console.log('🔴', key, element);
// }
// }
callback({ rtpCapabilities })
}
// Client emits a request for RTP Capabilities
// This event responds to the request
socket.on('getRtpCapabilities', (callback) => {
console.log('[getRtpCapabilities]')
// 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}`)
// The client indicates if it is a producer or a consumer
// if sender is true, indicates a producer else a consumer
if (sender)
producerTransport = await createWebRtcTransportLayer(callId, callback)
else
consumerTransport = await createWebRtcTransportLayer(callId, callback)
})
const rtpCapabilities = routers[routerId].rtpCapabilities
// see client's socket.emit('transport-connect', ...)
socket.on('transport-connect', async ({ dtlsParameters }) => {
console.log('DTLS PARAMS... ', { dtlsParameters })
await producerTransport.connect({ dtlsParameters })
})
// call callback from the client and send back the rtpCapabilities
callback({ rtpCapabilities })
// 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
// We need to differentiate between the producer and consumer transports
socket.on('createWebRtcTransport', async ({ sender }, callback) => {
console.log(`Is this a sender request? ${sender}`)
// The client indicates if it is a producer or a consumer
// if sender is true, indicates a producer else a consumer
if (sender)
producerTransport = await createWebRtcTransport(routerId, callback)
else
consumerTransport = await createWebRtcTransport(routerId, callback)
console.log('Producer ID: ', producer.id, producer.kind)
producer.on('transportclose', () => {
console.log('transport for this producer closed ')
producer.close()
})
// 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,
})
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}`)
await consumerTransport.connect({ dtlsParameters })
// Send back to the client the Producer's id
callback({
id: producer.id
})
})
socket.on('consume', async ({ rtpCapabilities }, callback) => {
try {
console.log('[consume] Producer ID:', producer.id, producer.kind)
// check if the routers can consume the specified producer
if (routers[routerId].canConsume({
// see client's socket.emit('transport-recv-connect', ...)
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
console.log(`DTLS PARAMS: ${dtlsParameters}`)
await consumerTransport.connect({ dtlsParameters })
})
socket.on('consume', async ({ rtpCapabilities, callId }, callback) => {
try {
// 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,
rtpCapabilities
})) {
// 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
}
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 })
}
})
socket.on('consumer-resume', async () => {
console.log('consumer resume')
await consumer.resume()
})
} catch (error) {
console.log(error.message)
callback({
params: {
error: error
}
})
}
})
socket.on('consumer-resume', async () => {
console.log('consumer resume')
await consumer.resume()
})
})
const createWebRtcTransport = async (routerId, callback) => {
const createWebRtcTransportLayer = async (callId, callback) => {
try {
console.log('createWebRtcTransportLayer | callId', callId);
// https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
const webRtcTransport_options = {
listenIps: [
@ -239,9 +235,9 @@ const createWebRtcTransport = async (routerId, callback) => {
enableTcp: true,
preferUdp: true,
}
// https://mediasoup.org/documentation/v3/mediasoup/api/#routers-createWebRtcTransport
let transport = await routers[routerId].createWebRtcTransport(webRtcTransport_options)
console.log('🔴 router[callId]', JSON.stringify(router));
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
let transport = await router[callId].createWebRtcTransport(webRtcTransport_options)
console.log(`transport id: ${transport.id}`)
transport.on('dtlsstatechange', dtlsState => {
@ -268,7 +264,7 @@ const createWebRtcTransport = async (routerId, callback) => {
return transport
} catch (error) {
console.log(error)
console.log('11111', error)
callback({
params: {
error: error

File diff suppressed because it is too large Load Diff

View File

@ -25,8 +25,6 @@
</head>
<body>
<body>
<label for="assetId">My asset id:</label><br>
<input type="number" id="assetId" name="assetId"><br>
<div id="video">
<table>
<thead>
@ -49,11 +47,16 @@
<tr>
<td>
<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>
</td>
</tr>
<tr>
<!-- <tr>
<td colspan="2">
<div id="sharedBtns">
<button id="btnRtpCapabilities">2. Get Rtp Capabilities</button>
@ -76,7 +79,7 @@
<button id="btnConnectRecvTransport">7. Connect Recv Transport & Consume</button>
</div>
</td>
</tr>
</tr> -->
</tbody>
</table>
</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 urlParams = new URLSearchParams(location.search);
const ASSET_ID = 71
const ACCOUNT_ID = 1
const ASSET_ID = parseInt(urlParams.get('assetId')) || null;
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/', {
reconnect: false,
transports: ['websocket'],
})
let socket
hub = io('https://hub.dev.linx.safemobile.com/')
hub.on('connect', async () => {
console.log('hub.connected', hub.connected);
hub.emit(
'ars',
JSON.stringify({
ars: true,
asset_id: ASSET_ID,
account_id: ACCOUNT_ID,
if (IS_PRODUCER === true) {
hub.on('connect', async () => {
console.log('hub.connected', hub.connected);
hub.emit(
'ars',
JSON.stringify({
ars: true,
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 rtpCapabilities
let producerTransport
let consumerTransport
let producer
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/#transport-produce
@ -80,18 +109,19 @@ let params = {
}
}
const streamSuccess = async (stream) => {
const streamSuccess = (stream) => {
localVideo.srcObject = stream
const track = stream.getVideoTracks()[0]
params = {
track,
...params
}
goConnect()
}
const getLocalStream = () => {
navigator.getUserMedia({
navigator.mediaDevices.getUserMedia({
audio: false,
video: {
width: {
@ -103,11 +133,21 @@ const getLocalStream = () => {
max: 1080,
}
}
}, streamSuccess, error => {
})
.then(streamSuccess)
.catch(error => {
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
// server side to send/recive media
const createDevice = async () => {
@ -121,7 +161,10 @@ const createDevice = async () => {
routerRtpCapabilities: rtpCapabilities
})
console.log('RTP Capabilities', device.rtpCapabilities)
console.log('Device RTP Capabilities', device.rtpCapabilities)
// once the device loads, create transport
goCreateTransport()
} catch (error) {
console.log(error)
@ -134,19 +177,22 @@ const getRtpCapabilities = () => {
// make a request to the server for Router RTP Capabilities
// see server's socket.on('getRtpCapabilities', ...)
// 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}`)
// we assign to local variable and will be used when
// loading the client Device (see createDevice above)
rtpCapabilities = data.rtpCapabilities
// once we have rtpCapabilities from the Router, create Device
createDevice()
})
}
const createSendTransport = () => {
// see server's socket.on('createWebRtcTransport', sender?, ...)
// 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
// to create Send Transport on the client side
if (params.error) {
@ -168,7 +214,7 @@ const createSendTransport = () => {
try {
// Signal local DTLS parameters to the server side transport
// see server's socket.on('transport-connect', ...)
await mediasoup.emit('transport-connect', {
await socket.emit('transport-connect', {
dtlsParameters,
})
@ -188,7 +234,7 @@ const createSendTransport = () => {
// with the following parameters and produce
// and expect back a server side producer id
// see server's socket.on('transport-produce', ...)
await mediasoup.emit('transport-produce', {
await socket.emit('transport-produce', {
kind: parameters.kind,
rtpParameters: parameters.rtpParameters,
appData: parameters.appData,
@ -201,6 +247,8 @@ const createSendTransport = () => {
errback(error)
}
})
connectSendTransport()
})
}
@ -219,15 +267,32 @@ const connectSendTransport = async () => {
producer.on('transportclose', () => {
console.log('transport ended')
// 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 () => {
console.log('createRecvTransport');
// see server's socket.on('consume', sender?, ...)
// 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
// to create Send Transport on the client side
if (params.error) {
@ -249,7 +314,7 @@ const createRecvTransport = async () => {
try {
// Signal local DTLS parameters to the server side transport
// see server's socket.on('transport-recv-connect', ...)
await mediasoup.emit('transport-recv-connect', {
await socket.emit('transport-recv-connect', {
dtlsParameters,
})
@ -260,6 +325,8 @@ const createRecvTransport = async () => {
errback(error)
}
})
connectRecvTransport()
})
}
@ -268,15 +335,16 @@ const connectRecvTransport = async () => {
// for consumer, we need to tell the server first
// 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
await mediasoup.emit('consume', {
await socket.emit('consume', {
rtpCapabilities: device.rtpCapabilities,
callId
}, async ({ params }) => {
if (params.error) {
console.log('Cannot Consume')
return
}
console.log('params', params)
console.log(params)
// then consume with the local consumer transport
// which creates a consumer
consumer = await consumerTransport.consume({
@ -289,19 +357,15 @@ const connectRecvTransport = async () => {
// destructure and retrieve the video track from the producer
const { track } = consumer
remoteVideo.srcObject = new MediaStream([track])
const stream = new MediaStream([track])
remoteVideo.srcObject = stream
// the server consumer started with media paused
// so we need to inform the server to resume
console.log('consumer-resume');
mediasoup.emit('consumer-resume')
socket.emit('consumer-resume')
console.log('consumer', consumer);
})
}
btnLocalVideo.addEventListener('click', getLocalStream)
btnRtpCapabilities.addEventListener('click', getRtpCapabilities)
btnDevice.addEventListener('click', createDevice)
btnCreateSendTransport.addEventListener('click', createSendTransport)
btnConnectSendTransport.addEventListener('click', connectSendTransport)
btnRecvSendTransport.addEventListener('click', createRecvTransport)
btnConnectRecvTransport.addEventListener('click', connectRecvTransport)
btnRecvSendTransport.addEventListener('click', goConnect)