Compare commits
19 Commits
LINXD-2180
...
031a7bc4c5
Author | SHA1 | Date | |
---|---|---|---|
031a7bc4c5 | |||
d7486d0fd6 | |||
38931f0654 | |||
bb684ca4db | |||
25a76c343b | |||
817a49204d | |||
91b4db1982 | |||
8562f6c58c | |||
5abc309502 | |||
ecb5a88a2c | |||
782b749ea3 | |||
b834016dcb | |||
523945271e | |||
0af7ddd786 | |||
ba5add489d | |||
d5cb144799 | |||
4e92f6cdd3 | |||
aaa1c5cea4 | |||
4f302570a2 |
84
app.js
84
app.js
@ -96,6 +96,38 @@ const mediaCodecs = [
|
||||
},
|
||||
]
|
||||
|
||||
let queue = []
|
||||
|
||||
// queue.push({
|
||||
// callId: 1,
|
||||
// callback: () => console.log('callback')
|
||||
// })
|
||||
// queue.push({
|
||||
// callId: 2,
|
||||
// callback: () => console.log('callback')
|
||||
// })
|
||||
// queue.push({
|
||||
// callId: 3,
|
||||
// callback: () => console.log('callback')
|
||||
// })
|
||||
|
||||
|
||||
const getRtpCapabilities = (callId, callback) => {
|
||||
const rtpCapabilities = router[callId].rtpCapabilities
|
||||
callback({ rtpCapabilities })
|
||||
}
|
||||
|
||||
setInterval(async () => {
|
||||
if (queue.length > 0) {
|
||||
const { callId, callback } = queue.shift();
|
||||
if (router[callId] === undefined) {
|
||||
router[callId] = await worker.createRouter({ mediaCodecs })
|
||||
console.log(`[createRoom] Router ID: ${router[callId].id}`)
|
||||
}
|
||||
getRtpCapabilities(callId, callback)
|
||||
}
|
||||
}, 1);
|
||||
|
||||
peers.on('connection', async socket => {
|
||||
console.log('[connection] socketId:', socket.id)
|
||||
socket.emit('connection-success', {
|
||||
@ -110,26 +142,24 @@ peers.on('connection', async socket => {
|
||||
|
||||
socket.on('createRoom', async ({ callId }, callback) => {
|
||||
console.log('[createRoom] callId', callId);
|
||||
console.log('Router length:', Object.keys(router).length);
|
||||
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(`[createRoom] Router ID: ${router[callId].id}`)
|
||||
}
|
||||
// console.log('Router length:', Object.keys(router).length);
|
||||
// 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(`[createRoom] Router ID: ${router[callId].id}`)
|
||||
// }
|
||||
|
||||
getRtpCapabilities(callId, callback)
|
||||
// getRtpCapabilities(callId, callback)
|
||||
queue.push({
|
||||
callId,
|
||||
callback
|
||||
})
|
||||
})
|
||||
|
||||
const getRtpCapabilities = (callId, callback) => {
|
||||
const rtpCapabilities = router[callId].rtpCapabilities
|
||||
|
||||
callback({ rtpCapabilities })
|
||||
}
|
||||
|
||||
// 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) => {
|
||||
@ -149,13 +179,12 @@ peers.on('connection', async socket => {
|
||||
})
|
||||
|
||||
// see client's socket.emit('transport-produce', ...)
|
||||
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
|
||||
socket.on('transport-produce', async ({ kind, rtpParameters, appData }) => {
|
||||
// call produce based on the prameters from the client
|
||||
producer = await producerTransport.produce({
|
||||
kind,
|
||||
rtpParameters,
|
||||
})
|
||||
|
||||
console.log(`[transport-produce] Producer ID: ${producer.id} | kind: ${producer.kind}`)
|
||||
|
||||
producer.on('transportclose', () => {
|
||||
@ -168,11 +197,6 @@ peers.on('connection', async socket => {
|
||||
router[callId].close()
|
||||
delete router[callId]
|
||||
})
|
||||
|
||||
// Send back to the client the Producer's id
|
||||
callback({
|
||||
id: producer.id
|
||||
})
|
||||
})
|
||||
|
||||
// see client's socket.emit('transport-recv-connect', ...)
|
||||
@ -183,7 +207,7 @@ peers.on('connection', async socket => {
|
||||
|
||||
socket.on('consume', async ({ rtpCapabilities, callId }, callback) => {
|
||||
try {
|
||||
console.log('consume', rtpCapabilities, callId);
|
||||
// console.log('consume', rtpCapabilities, callId);
|
||||
// check if the router can consume the specified producer
|
||||
if (router[callId].canConsume({
|
||||
producerId: producer.id,
|
||||
@ -198,8 +222,12 @@ peers.on('connection', async socket => {
|
||||
|
||||
consumer.on('transportclose', () => {
|
||||
console.log('transport close from consumer', callId)
|
||||
// closeRoom(callId)
|
||||
|
||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
|
||||
router[callId].close()
|
||||
delete router[callId]
|
||||
producer.close()
|
||||
consumer.close()
|
||||
})
|
||||
|
||||
consumer.on('producerclose', () => {
|
||||
@ -208,6 +236,8 @@ peers.on('connection', async socket => {
|
||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
|
||||
router[callId].close()
|
||||
delete router[callId]
|
||||
producer.close()
|
||||
consumer.close()
|
||||
})
|
||||
|
||||
// from the consumer extract the following params
|
||||
@ -278,7 +308,7 @@ const createWebRtcTransportLayer = async (callId, callback) => {
|
||||
dtlsParameters: transport.dtlsParameters,
|
||||
}
|
||||
|
||||
console.log('params', params);
|
||||
// console.log('params', params);
|
||||
|
||||
// send back to the client the following prameters
|
||||
callback({
|
||||
|
Reference in New Issue
Block a user