Compare commits

..

92 Commits

Author SHA1 Message Date
56835d6660 added log for dtls transport-connect 2022-10-05 15:44:46 +03:00
fc42c79210 Fix missing callId 2022-09-27 13:13:29 +03:00
d81bc8582d Merge branch 'master' of https://git.safemobile.org/Safemobile/mediasoup 2022-09-27 13:05:15 +03:00
a4d16998cd Fix call check before call close() 2022-09-27 13:03:32 +03:00
de1458bbde Merge pull request 'LINXD-2197: Added comments; Catch errors; Fix package.json start:run script' (#8) from LINXD-2197-refactor-improving-mediasoup-web-socket-component into master
Reviewed-on: #8
2022-09-27 10:00:25 +00:00
b0fad5f1db LINXD-2197: On peer disconnect delete the call; Added log when call is already deleted; Added log when user send multiple createWebRtcTransport 2022-09-27 12:43:07 +03:00
eb5aa12d65 LINXD-2197: Added the initial demo project used; Check before set producerTransport and consumerTransport if it was set before 2022-09-27 07:55:25 +03:00
52b4794a86 LINXD-2197: Added workflow diagram 2022-09-25 20:29:32 +03:00
5f8f2ab44c LINXD-2197: Added comments; Catch errors; Fix package.json start:run script 2022-09-25 20:03:17 +03:00
55455be8e7 Merge pull request 'LINXD-2222-debugging-for-i-os' (#7) from LINXD-2222-debugging-for-i-os into master
Reviewed-on: #7
2022-09-20 23:16:16 +00:00
62a82dc3a5 LINXD-2222: Removed socketio-wildcard 2022-09-20 14:17:16 +03:00
ac078e72ff LINXD-2222: Removed requestCert and rejectedUnauthorized from server options 2022-09-20 14:15:54 +03:00
be396e1047 LINXD-2222: Set namespate to '/'; Removed httpolyglot; Removed unused code 2022-09-20 14:02:22 +03:00
149876fc70 LINXD-2222: use https instead of httpolyglot; Added logs 2022-09-20 09:36:31 +03:00
adbeb2071b Update to start with defeult port 3000 2022-09-19 23:37:20 +03:00
a6681ffe40 LINXD-2222: Update 2022-09-19 23:32:15 +03:00
efc9bfd114 LINXD-2222: Update 2022-09-19 23:31:36 +03:00
a8afa8a532 LINXD-2222: Update 2022-09-19 23:30:18 +03:00
507c131058 LINXD-2222: Update 2022-09-19 23:28:39 +03:00
043f66eb0c LINXD-2222: Update 2022-09-19 23:24:32 +03:00
cb5716dd5c LINXD-2222: Update 2022-09-19 23:12:24 +03:00
ae39a45f6d LINXD-2222: Update 2022-09-19 23:09:55 +03:00
0ec5769ee0 LINXD-2222: Update 2022-09-19 18:12:37 +03:00
72ee3e43ab LINXD-2222: Update 2022-09-19 18:10:34 +03:00
f20c7fada8 LINXD-2222: Update 2022-09-19 18:06:39 +03:00
53a654c50f LINXD-2222: Update 2022-09-19 18:02:30 +03:00
d54403299f LINXD-2222: Update 2022-09-19 17:55:21 +03:00
177d54ec67 LINXD-2222: Update 2022-09-19 17:45:42 +03:00
649c7a3767 LINXD-2222: Update 2022-09-19 17:45:18 +03:00
08d6ccbb21 LINXD-2222: Update 2022-09-19 17:44:45 +03:00
fd005351b5 LINXD-2222: Update 2022-09-19 17:43:39 +03:00
fc111540d8 LINXD-2222: Update 2022-09-19 17:42:42 +03:00
c4f4be0aa8 LINXD-2222: Update 2022-09-19 17:42:16 +03:00
40c03592df LINXD-2222: Update 2022-09-19 17:40:57 +03:00
a59cbcf8cc LINXD-2222: Update 2022-09-19 17:13:48 +03:00
7cc3a95b38 LINXD-2222: Update 2022-09-19 17:12:22 +03:00
05e3d997f1 LINXD-2222: Update 2022-09-19 17:04:56 +03:00
9c731f4085 LINXD-2222: Update 2022-09-19 17:03:58 +03:00
f6d862966e LINXD-2222: Update 2022-09-19 17:02:13 +03:00
05ccd5cfd4 LINXD-2222: Update 2022-09-19 17:00:43 +03:00
43eee11c7e LINXD-2222: Update 2022-09-19 16:53:32 +03:00
0033cd528d LINXD-2222: Update 2022-09-19 16:53:09 +03:00
5022d88b1d LINXD-2222: Update 2022-09-19 16:51:15 +03:00
52b922825f LINXD-2222: Update 2022-09-19 16:48:46 +03:00
07be8af9ae LINXD-2222: Update 2022-09-19 16:46:43 +03:00
29737fe5d8 LINXD-2222: Fix middleware typo 2022-09-19 16:44:29 +03:00
1f5755b72d LINXD-2222: Added wildcard; Replace httpolyglot with https; Set CORS to * 2022-09-19 16:21:50 +03:00
a2c878f91c Merge pull request 'Delete the whole call(with id) when we call closeCall' (#5) from delete-whole-call-id into master
Reviewed-on: #5
2022-09-17 08:43:57 +00:00
7b6f78725b LINXD-2209: Call closeCall from producerclose and transportclose on consumer handlers; Update README.md 2022-09-16 18:49:56 +03:00
41c6ad281d Delete the whole call(with id) when we call closeCall 2022-09-16 11:08:02 +03:00
f5406f163f Merge pull request 'Allow io3 on server creation' (#4) from Allow-io3 into master
Reviewed-on: #4
2022-09-15 14:54:41 +00:00
28497fda91 Merge with master 2022-09-15 17:53:37 +03:00
4a98a79630 Merge pull request 'LINXD-2209-black-screen-when-2-video-calls-are-answered-simultaneously' (#3) from LINXD-2209-black-screen-when-2-video-calls-are-answered-simultaneously into master
Reviewed-on: #3
2022-09-15 14:49:55 +00:00
22e8b4d364 LINXD-2209: Refactor how we close the call; Check for callId in createRoom event 2022-09-15 17:07:47 +03:00
575dbd69b0 Allow io3 on server creation 2022-09-15 14:49:10 +03:00
a51a757d17 LINXD-2209: Correctly close the call 2022-09-15 09:57:57 +03:00
c059dd5afc LINXD-2209: Correctly close the call 2022-09-15 09:56:32 +03:00
19808da24e LINXD-2209: Get callId from soekct dictionary in consumer-resume case 2022-09-15 09:43:59 +03:00
2f6c25c171 LINXD-2209: Correctly set router to videoCalls 2022-09-15 09:41:24 +03:00
ead0069aa8 LINXD-2209: Correctly set router to videoCalls 2022-09-15 09:39:52 +03:00
434c8f744c LINXD-2209: Correctly set router to videoCalls 2022-09-15 09:35:35 +03:00
7198dc91b1 LINXD-2209: Check for router in videoCalls 2022-09-15 09:33:30 +03:00
f629012712 LINXD-2209: Check for router in videoCalls 2022-09-15 09:32:41 +03:00
41b50d2a11 LINXD-2209: Identify the callId from dictionary 2022-09-15 09:19:04 +03:00
b85ba68c9c LINXD-2209: Added comments 2022-09-13 22:24:10 +03:00
6acd276324 LINXD-2209: Refactor how we save router, consumer, producer, producerTransport and consumerTransport 2022-09-13 22:16:51 +03:00
294dbdf38d LINXD-2209: Added logs 2022-09-13 21:43:16 +03:00
c3d50fdc4e LINXD-2209: Add 4000ms delay between room creation 2022-09-13 21:38:06 +03:00
c12ececf47 LINXD-2209: Add 2000ms delay between room creation 2022-09-13 21:35:26 +03:00
47eb302f5f LINXD-2209: Added logs on consume 2022-09-13 21:33:04 +03:00
accf960aa7 LINXD-2209: Added logs on consume 2022-09-13 21:15:51 +03:00
ab685270f1 LINXD-2209: Add 1000ms delay between room creation 2022-09-13 21:08:14 +03:00
6938e751fe LINXD-2209: Add 100ms delay between room creation 2022-09-13 21:08:03 +03:00
031a7bc4c5 LINXD-2209: Remove console.logs 2022-09-13 21:05:24 +03:00
d7486d0fd6 LINXD-2209: Add 10ms delay between room creation 2022-09-13 21:04:42 +03:00
38931f0654 LINXD-2209: Add 50ms delay between room creation 2022-09-13 21:02:29 +03:00
bb684ca4db LINXD-2209: Add 300ms delay 2022-09-13 20:59:55 +03:00
25a76c343b LINXD-2209: Move getRtpCapa outside of room creation 2022-09-13 20:59:12 +03:00
817a49204d LINXD-2209: Added logs 2022-09-13 20:58:06 +03:00
91b4db1982 LINXD-2209: Added logs 2022-09-13 20:54:35 +03:00
8562f6c58c LINXD-2209: Added logs 2022-09-13 20:49:40 +03:00
5abc309502 LINXD-2209: Added logs 2022-09-13 20:28:07 +03:00
ecb5a88a2c LINXD-2209: Update port 2022-09-13 20:10:34 +03:00
782b749ea3 LINXD-2209: Check queue length 2022-09-13 20:09:25 +03:00
b834016dcb LINXD-2209: Create rooms in sequence 2022-09-13 19:56:06 +03:00
523945271e Remove callback with producer id from transport-produce 2022-08-31 17:06:54 +03:00
0af7ddd786 Remove callback with producer id from transport-produce 2022-08-31 16:53:08 +03:00
ba5add489d Remove callback with producer id from transport-produce 2022-08-31 16:49:23 +03:00
d5cb144799 Remove callback with producer id from transport-produce 2022-08-31 16:47:54 +03:00
4e92f6cdd3 Remove callback with producer id from transport-produce 2022-08-31 16:43:59 +03:00
aaa1c5cea4 Remove callback with producer id from transport-produce 2022-08-31 16:40:38 +03:00
4f302570a2 Remove callback with producer id from transport-produce 2022-08-31 16:24:21 +03:00
8 changed files with 637 additions and 1042 deletions

View File

@ -1,5 +1,11 @@
# Video server # Video server
### Generating certificates
##### To generate SSL certificates you must:
1. Go to `/server/ssl`
2. Execute `openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem`
### Development ### Development
@ -27,3 +33,7 @@ producer = it will always be true because you are the producer
(it's possible to put false, but then you have to have another client with producer true) (it's possible to put false, but then you have to have another client with producer true)
assetName = asset name of the unit on which you are doing the test assetName = asset name of the unit on which you are doing the test
assetType = asset type of the unit on which you are doing the test assetType = asset type of the unit on which you are doing the test
### Demo project
The demo project used initially and then modified for our needs `https://github.com/jamalag/mediasoup2`

828
app.js
View File

@ -1,31 +1,38 @@
import 'dotenv/config' require('dotenv').config()
/** const express = require('express');
* integrating mediasoup server with a node.js application const app = express();
*/ const Server = require('socket.io');
const path = require('node:path');
/* Please follow mediasoup installation requirements */ const fs = require('node:fs');
/* https://mediasoup.org/documentation/v3/mediasoup/installation/ */ let https = require('https');
import express from 'express' try {
const app = express() https = require('node:https');
} catch (err) {
import https from 'httpolyglot' console.log('https support is disabled!');
import fs from 'fs' }
import path from 'path' const mediasoup = require('mediasoup');
const __dirname = path.resolve()
// const FFmpegStatic = require("ffmpeg-static")
import FFmpegStatic from 'ffmpeg-static'
import Server from 'socket.io'
import mediasoup, { getSupportedRtpCapabilities } from 'mediasoup'
import Process from 'child_process'
let worker let worker
let router = {} /**
let producerTransport * videoCalls
let consumerTransport * |-> Router
let producer * |-> Producer
let consumer * |-> Consumer
* |-> Producer Transport
* |-> Consumer Transport
*
* '<callId>': {
* router: Router,
* producer: Producer,
* producerTransport: Producer Transport,
* consumer: Consumer,
* consumerTransport: Consumer Transport
* }
*
**/
let videoCalls = {}
let socketDetails = {}
app.get('/', (_req, res) => { app.get('/', (_req, res) => {
res.send('Hello from mediasoup app!') res.send('Hello from mediasoup app!')
@ -36,294 +43,44 @@ app.use('/sfu', express.static(path.join(__dirname, 'public')))
// SSL cert for HTTPS access // SSL cert for HTTPS access
const options = { const options = {
key: fs.readFileSync('./server/ssl/key.pem', 'utf-8'), key: fs.readFileSync('./server/ssl/key.pem', 'utf-8'),
cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8') cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8'),
} }
const httpsServer = https.createServer(options, app) const httpsServer = https.createServer(options, app);
const io = new Server(httpsServer, {
allowEIO3: true,
origins: ["*:*"],
// allowRequest: (req, next) => {
// console.log('req', req);
// next(null, true)
// }
});
// const io = new Server(server, { origins: '*:*', allowEIO3: true });
httpsServer.listen(process.env.PORT, () => { httpsServer.listen(process.env.PORT, () => {
console.log('Listening on port:', process.env.PORT) console.log('Video server listening on port:', process.env.PORT)
}) })
const startRecordingFfmpeg = () => { const peers = io.of('/')
// Return a Promise that can be awaited
let recResolve;
const promise = new Promise((res, _rej) => {
recResolve = res;
});
// const useAudio = audioEnabled();
// const useVideo = videoEnabled();
// const useH264 = h264Enabled();
// const cmdProgram = "ffmpeg"; // Found through $PATH
const cmdProgram = FFmpegStatic; // From package "ffmpeg-static"
let cmdInputPath = `${__dirname}/recording/input-vp8.sdp`;
let cmdOutputPath = `${__dirname}/recording/output-ffmpeg-vp8.webm`;
let cmdCodec = "";
let cmdFormat = "-f webm -flags +global_header";
// Ensure correct FFmpeg version is installed
const ffmpegOut = Process.execSync(cmdProgram + " -version", {
encoding: "utf8",
});
const ffmpegVerMatch = /ffmpeg version (\d+)\.(\d+)\.(\d+)/.exec(ffmpegOut);
let ffmpegOk = false;
if (ffmpegOut.startsWith("ffmpeg version git")) {
// Accept any Git build (it's up to the developer to ensure that a recent
// enough version of the FFmpeg source code has been built)
ffmpegOk = true;
} else if (ffmpegVerMatch) {
const ffmpegVerMajor = parseInt(ffmpegVerMatch[1], 10);
if (ffmpegVerMajor >= 4) {
ffmpegOk = true;
}
}
if (!ffmpegOk) {
console.error("FFmpeg >= 4.0.0 not found in $PATH; please install it");
process.exit(1);
}
// if (useAudio) {
// cmdCodec += " -map 0:a:0 -c:a copy";
// }
// if (useVideo) {
cmdCodec += " -map 0:v:0 -c:v copy";
// if (useH264) {
cmdInputPath = `${__dirname}/recording/input-h264.sdp`;
cmdOutputPath = `${__dirname}/recording/output-ffmpeg-h264.mp4`;
// "-strict experimental" is required to allow storing
// OPUS audio into MP4 container
cmdFormat = "-f mp4 -strict experimental";
// }
// }
// Run process
const cmdArgStr = [
"-nostdin",
"-protocol_whitelist file,rtp,udp",
"-loglevel debug",
"-analyzeduration 5M",
"-probesize 5M",
"-fflags +genpts",
`-i ${cmdInputPath}`,
cmdCodec,
cmdFormat,
`-y ${cmdOutputPath}`,
]
.join(" ")
.trim();
console.log('💗', cmdCodec);
console.log(`Run command: ${cmdProgram} ${cmdArgStr}`);
let recProcess = Process.spawn(cmdProgram, cmdArgStr.split(/\s+/));
global.recProcess = recProcess;
recProcess.on("error", (err) => {
console.error("Recording process error:", err);
});
recProcess.on("exit", (code, signal) => {
console.log("Recording process exit, code: %d, signal: %s", code, signal);
global.recProcess = null;
stopMediasoupRtp();
if (!signal || signal === "SIGINT") {
console.log("Recording stopped");
} else {
console.warn(
"Recording process didn't exit cleanly, output file might be corrupt"
);
}
});
// FFmpeg writes its logs to stderr
recProcess.stderr.on("data", (chunk) => {
chunk
.toString()
.split(/\r?\n/g)
.filter(Boolean) // Filter out empty strings
.forEach((line) => {
console.log(line);
if (line.startsWith("ffmpeg version")) {
setTimeout(() => {
recResolve();
}, 1000);
}
});
});
return promise;
}
const startRecordingGstreamer = () => {
// Return a Promise that can be awaited
let recResolve;
const promise = new Promise((res, _rej) => {
recResolve = res;
});
// const useAudio = audioEnabled();
// const useVideo = videoEnabled();
// const useH264 = h264Enabled();
let cmdInputPath = `${__dirname}/recording/input-vp8.sdp`;
let cmdOutputPath = `${__dirname}/recording/output-gstreamer-vp8.webm`;
let cmdMux = "webmmux";
let cmdAudioBranch = "";
let cmdVideoBranch = "";
// if (useAudio) {
// // prettier-ignore
// cmdAudioBranch =
// "demux. ! queue \
// ! rtpopusdepay \
// ! opusparse \
// ! mux.";
// }
// if (useVideo) {
// if (useH264) {
cmdInputPath = `${__dirname}/recording/input-h264.sdp`;
cmdOutputPath = `${__dirname}/recording/output-gstreamer-h264.mp4`;
cmdMux = `mp4mux faststart=true faststart-file=${cmdOutputPath}.tmp`;
// prettier-ignore
cmdVideoBranch =
"demux. ! queue \
! rtph264depay \
! h264parse \
! mux.";
// } else {
// // prettier-ignore
// cmdVideoBranch =
// "demux. ! queue \
// ! rtpvp8depay \
// ! mux.";
// }
// }
// Run process
const cmdProgram = "gst-launch-1.0"; // Found through $PATH
const cmdArgStr = [
"--eos-on-shutdown",
`filesrc location=${cmdInputPath}`,
"! sdpdemux timeout=0 name=demux",
`${cmdMux} name=mux`,
`! filesink location=${cmdOutputPath}`,
cmdAudioBranch,
cmdVideoBranch,
]
.join(" ")
.trim();
console.log(
`Run command: ${cmdProgram} ${cmdArgStr}`
);
let recProcess = Process.spawn(cmdProgram, cmdArgStr.split(/\s+/));
global.recProcess = recProcess;
recProcess.on("error", (err) => {
console.error("Recording process error:", err);
});
recProcess.on("exit", (code, signal) => {
console.log("Recording process exit, code: %d, signal: %s", code, signal);
global.recProcess = null;
stopMediasoupRtp();
if (!signal || signal === "SIGINT") {
console.log("Recording stopped");
} else {
console.warn(
"Recording process didn't exit cleanly, output file might be corrupt"
);
}
});
// GStreamer writes some initial logs to stdout
recProcess.stdout.on("data", (chunk) => {
chunk
.toString()
.split(/\r?\n/g)
.filter(Boolean) // Filter out empty strings
.forEach((line) => {
console.log(line);
if (line.startsWith("Setting pipeline to PLAYING")) {
setTimeout(() => {
recResolve();
}, 1000);
}
});
});
// GStreamer writes its progress logs to stderr
recProcess.stderr.on("data", (chunk) => {
chunk
.toString()
.split(/\r?\n/g)
.filter(Boolean) // Filter out empty strings
.forEach((line) => {
console.log(line);
});
});
return promise;
}
function stopMediasoupRtp() {
console.log("Stop mediasoup RTP transport and consumer");
// const useAudio = audioEnabled();
// const useVideo = videoEnabled();
// if (useAudio) {
// global.mediasoup.rtp.audioConsumer.close();
// global.mediasoup.rtp.audioTransport.close();
// }
// if (useVideo) {
// global.mediasoup.rtp.videoConsumer.close();
// global.mediasoup.rtp.videoTransport.close();
// }
}
const io = new Server(httpsServer)
// socket.io namespace (could represent a room?)
const peers = io.of('/mediasoup')
/**
* Worker
* |-> Router(s)
* |-> Producer Transport(s)
* |-> Producer
* |-> Consumer Transport(s)
* |-> Consumer
**/
const createWorker = async () => { const createWorker = async () => {
worker = await mediasoup.createWorker({ try {
rtcMinPort: 32256, worker = await mediasoup.createWorker({
rtcMaxPort: 65535, rtcMinPort: 2000,
}) rtcMaxPort: 2020,
console.log(`[createWorker] worker pid ${worker.pid}`) })
console.log(`[createWorker] worker pid ${worker.pid}`);
worker.on('died', error => { worker.on('died', error => {
// This implies something serious happened, so kill the application // This implies something serious happened, so kill the application
console.error('mediasoup worker has died', error) console.error('mediasoup worker has died', error);
setTimeout(() => process.exit(1), 2000) // exit in 2 seconds setTimeout(() => process.exit(1), 2000); // exit in 2 seconds
}) })
return worker;
return worker } catch (error) {
console.log(`ERROR | createWorker | ${error.message}`);
}
} }
// We create a Worker as soon as our application starts // We create a Worker as soon as our application starts
@ -335,222 +92,264 @@ worker = createWorker()
// https://github.com/versatica/mediasoup/blob/v3/src/supportedRtpCapabilities.ts // https://github.com/versatica/mediasoup/blob/v3/src/supportedRtpCapabilities.ts
const mediaCodecs = [ const mediaCodecs = [
{ {
kind: "audio", kind: 'audio',
mimeType: "audio/opus", mimeType: 'audio/opus',
preferredPayloadType: 111,
clockRate: 48000, clockRate: 48000,
channels: 2, channels: 2,
parameters: {
minptime: 10,
useinbandfec: 1,
},
}, },
{ {
kind: "video", kind: 'video',
mimeType: "video/VP8", mimeType: 'video/VP8',
preferredPayloadType: 96,
clockRate: 90000,
},
{
kind: "video",
mimeType: "video/H264",
preferredPayloadType: 125,
clockRate: 90000, clockRate: 90000,
parameters: { parameters: {
"level-asymmetry-allowed": 1, 'x-google-start-bitrate': 1000,
"packetization-mode": 1,
"profile-level-id": "42e01f",
}, },
}, },
] ]
peers.on('connection', async socket => { const closeCall = (callId) => {
console.log('[connection] socketId:', socket.id) try {
socket.emit('connection-success', { if (videoCalls[callId]) {
socketId: socket.id, videoCalls[callId].producer?.close();
existsProducer: producer ? true : false, videoCalls[callId].consumer?.close();
}) videoCalls[callId]?.consumerTransport?.close();
videoCalls[callId]?.producerTransport?.close();
socket.on('disconnect', () => { videoCalls[callId]?.router?.close();
// do some cleanup delete videoCalls[callId];
console.log('peer disconnected') } else {
}) console.log(`The call with id ${callId} has already been deleted`);
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}`)
} }
} catch (error) {
getRtpCapabilities(callId, callback) console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`);
})
const getRtpCapabilities = (callId, callback) => {
const rtpCapabilities = router[callId].rtpCapabilities
callback({ rtpCapabilities })
} }
}
// Client emits a request to create server side Transport const getRtpCapabilities = (callId, callback) => {
// We need to differentiate between the producer and consumer transports try {
socket.on('createWebRtcTransport', async ({ sender, callId }, callback) => { console.log('[getRtpCapabilities] callId', callId);
console.log(`[createWebRtcTransport] Is this a sender request? ${sender} | callId ${callId}`) const rtpCapabilities = videoCalls[callId].router.rtpCapabilities;
// The client indicates if it is a producer or a consumer callback({ rtpCapabilities });
// if sender is true, indicates a producer else a consumer } catch (error) {
if (sender) console.log(`ERROR | getRtpCapabilities | callId ${callId} | ${error.message}`);
producerTransport = await createWebRtcTransportLayer(callId, callback) }
else }
consumerTransport = await createWebRtcTransportLayer(callId, callback)
})
// see client's socket.emit('transport-connect', ...) /*
socket.on('transport-connect', async ({ dtlsParameters }) => { - Handlers for WS events
console.log('[transport-connect] DTLS PARAMS... ', { dtlsParameters }) - These are created only when we have a connection with a peer
await producerTransport.connect({ dtlsParameters }) */
}) peers.on('connection', async socket => {
console.log('[connection] socketId:', socket.id);
// see client's socket.emit('transport-produce', ...) // After making the connection successfully, we send the client a 'connection-success' event
socket.on('transport-produce', async ({ kind, rtpParameters, callId }, callback) => { socket.emit('connection-success', {
// call produce based on the prameters from the client socketId: socket.id
producer = await producerTransport.produce({ });
kind,
rtpParameters,
})
console.log(`[transport-produce] Producer ID: ${producer.id} | kind: ${producer.kind}`) // It is triggered when the peer is disconnected
socket.on('disconnect', () => {
const callId = socketDetails[socket.id];
console.log(`disconnect | socket ${socket.id} | callId ${callId}`);
delete socketDetails[socket.id];
closeCall(callId);
});
producer.on('transportclose', () => { /*
console.log('transport for this producer closed', callId) - This event creates a room with the roomId and the callId sent
- It will return the rtpCapabilities of that room
// https://mediasoup.org/documentation/v3/mediasoup/api/#producer-close - If the room already exists, it will not create it, but will only return rtpCapabilities
producer.close() */
socket.on('createRoom', async ({ callId }, callback) => {
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
router[callId].close()
delete router[callId]
})
// Send back to the client the Producer's id
callback({
id: producer.id
})
console.log('🔴', callId);
const rtpTransport = await router[callId].createPlainTransport({
comedia: false,
rtcpMux: false,
listenIp: { ip: "127.0.0.1", announcedIp: null }
});
await rtpTransport.connect({
ip: "127.0.0.1",
port: 5006,
rtcpPort: 5007,
});
console.log(
"mediasoup VIDEO RTP SEND transport connected: %s:%d <--> %s:%d (%s)",
rtpTransport.tuple.localIp,
rtpTransport.tuple.localPort,
rtpTransport.tuple.remoteIp,
rtpTransport.tuple.remotePort,
rtpTransport.tuple.protocol
);
console.log(
"mediasoup VIDEO RTCP SEND transport connected: %s:%d <--> %s:%d (%s)",
rtpTransport.rtcpTuple.localIp,
rtpTransport.rtcpTuple.localPort,
rtpTransport.rtcpTuple.remoteIp,
rtpTransport.rtcpTuple.remotePort,
rtpTransport.rtcpTuple.protocol
);
const rtpConsumer = await rtpTransport.consume({
// producerId: global.mediasoup.webrtc.videoProducer.id,
producerId: producer.id,
// rtpCapabilities: router.rtpCapabilities,
rtpCapabilities: router[callId].rtpCapabilities,
paused: true,
});
// console.log('🟡 producerId:', producer.id, 'rtpCapabilities:', router[callId].rtpCapabilities, 'paused:', true);
await startRecordingFfmpeg();
// await startRecordingGstreamer();
rtpConsumer.resume();
})
// see client's socket.emit('transport-recv-connect', ...)
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
console.log(`[transport-recv-connect] DTLS PARAMS: ${dtlsParameters}`)
await consumerTransport.connect({ dtlsParameters })
})
socket.on('consume', async ({ rtpCapabilities, callId }, callback) => {
try { try {
console.log('consume', rtpCapabilities, callId); if (callId) {
// check if the router can consume the specified producer console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`);
if (router[callId].canConsume({ if (!videoCalls[callId]) {
producerId: producer.id, console.log('[createRoom] callId', callId);
rtpCapabilities videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) }
})) { console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
// 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', callId)
// closeRoom(callId)
delete router[callId]
})
consumer.on('producerclose', () => {
console.log('producer of consumer closed', callId)
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-close
router[callId].close()
delete router[callId]
})
// 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,
} }
socketDetails[socket.id] = callId;
// send the parameters to the client getRtpCapabilities(callId, callback);
callback({ params }) } else {
console.log(`[createRoom] missing callId ${callId}`);
} }
} catch (error) { } catch (error) {
console.log(error.message) console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`);
callback({ }
params: { });
error: error
/*
- Client emits a request to create server side Transport
- Depending on the sender, producerTransport or consumerTransport is created on that router
- It will return parameters, these are required for the client to create the RecvTransport
from the client.
- If the client is producer(sender: true) then it will use parameters for device.createSendTransport(params)
- If the client is a consumer(sender: false) then it will use parameters for device.createRecvTransport(params)
*/
socket.on('createWebRtcTransport', async ({ sender }, callback) => {
try {
const callId = socketDetails[socket.id];
console.log(`[createWebRtcTransport] sender ${sender} | callId ${callId}`);
if (sender) {
if (!videoCalls[callId].producerTransport) {
videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback);
} else {
console.log(`producerTransport has already been defined | callId ${callId}`);
} }
}) } else if (!sender) {
if (!videoCalls[callId].consumerTransport) {
videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback);
} else {
console.log(`consumerTransport has already been defined | callId ${callId}`);
}
}
} catch (error) {
console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
}
});
/*
- The client sends this event after successfully creating a createSendTransport(AS PRODUCER)
- The connection is made to the created transport
*/
socket.on('transport-connect', async ({ dtlsParameters }) => {
try {
const callId = socketDetails[socket.id];
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId}`);
await videoCalls[callId].producerTransport.connect({ dtlsParameters });
} catch (error) {
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
}
});
/*
- The event sent by the client (PRODUCER) after successfully connecting to producerTransport
- For the router with the id callId, we make produce on producerTransport
- Create the handler on producer at the 'transportclose' event
*/
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
try {
const callId = socketDetails[socket.id];
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
console.log('[transport-produce] | socket.id', socket.id, '| callId', callId);
videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({
kind,
rtpParameters,
});
console.log(`[transport-produce] Producer ID: ${videoCalls[callId].producer.id} | kind: ${videoCalls[callId].producer.kind}`);
videoCalls[callId].producer.on('transportclose', () => {
const callId = socketDetails[socket.id];
console.log('transport for this producer closed', callId)
closeCall(callId);
});
// Send back to the client the Producer's id
// callback({
// id: videoCalls[callId].producer.id
// });
} catch (error) {
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
}
});
/*
- The client sends this event after successfully creating a createRecvTransport(AS CONSUMER)
- The connection is made to the created consumerTransport
*/
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
try {
const callId = socketDetails[socket.id];
console.log(`[transport-recv-connect] socket.id ${socket.id} | callId ${callId}`);
await videoCalls[callId].consumerTransport.connect({ dtlsParameters });
} catch (error) {
console.log(`ERROR | transport-recv-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
} }
}) })
socket.on('consumer-resume', async () => { /*
console.log(`[consumer-resume]`) - The customer consumes after successfully connecting to consumerTransport
await consumer.resume() - The previous step was 'transport-recv-connect', and before that 'createWebRtcTransport'
}) - This event is only sent by the consumer
}) - The parameters that the consumer consumes are returned
- The consumer does consumerTransport.consume(params)
*/
socket.on('consume', async ({ rtpCapabilities }, callback) => {
try {
const callId = socketDetails[socket.id];
console.log('[consume] callId', callId);
// Check if the router can consume the specified producer
if (videoCalls[callId].router.canConsume({
producerId: videoCalls[callId].producer.id,
rtpCapabilities
})) {
console.log('[consume] Can consume', callId);
// Transport can now consume and return a consumer
videoCalls[callId].consumer = await videoCalls[callId].consumerTransport.consume({
producerId: videoCalls[callId].producer.id,
rtpCapabilities,
paused: true,
});
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-transportclose
videoCalls[callId].consumer.on('transportclose', () => {
const callId = socketDetails[socket.id];
console.log('transport close from consumer', callId);
closeCall();
});
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
videoCalls[callId].consumer.on('producerclose', () => {
const callId = socketDetails[socket.id];
console.log('producer of consumer closed', callId);
closeCall();
});
// From the consumer extract the following params to send back to the Client
const params = {
id: videoCalls[callId].consumer.id,
producerId: videoCalls[callId].producer.id,
kind: videoCalls[callId].consumer.kind,
rtpParameters: videoCalls[callId].consumer.rtpParameters,
};
// Send the parameters to the client
callback({ params });
} else {
console.log(`[canConsume] Can't consume | callId ${callId}`);
}
} catch (error) {
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
callback({ params: { error } });
}
});
/*
- Event sent by the consumer after consuming to resume the pause
- When consuming on consumerTransport, it is initially done with paused: true, here we will resume
*/
socket.on('consumer-resume', async () => {
try {
const callId = socketDetails[socket.id];
console.log(`[consumer-resume] callId ${callId}`)
await videoCalls[callId].consumer.resume();
} catch (error) {
console.log(`ERROR | consumer-resume | callId ${socketDetails[socket.id]} | ${error.message}`);
}
});
});
/*
- Called from at event 'createWebRtcTransport' and assigned to the consumer or producer transport
- It will return parameters, these are required for the client to create the RecvTransport
from the client.
- If the client is producer(sender: true) then it will use parameters for device.createSendTransport(params)
- If the client is a consumer(sender: false) then it will use parameters for device.createRecvTransport(params)
*/
const createWebRtcTransportLayer = async (callId, callback) => { const createWebRtcTransportLayer = async (callId, callback) => {
try { try {
console.log('[createWebRtcTransportLayer] callId', callId); console.log('[createWebRtcTransportLayer] callId', callId);
@ -565,49 +364,40 @@ const createWebRtcTransportLayer = async (callId, callback) => {
enableUdp: true, enableUdp: true,
enableTcp: true, enableTcp: true,
preferUdp: true, preferUdp: true,
initialAvailableOutgoingBitrate: 300000 };
}
// console.log('webRtcTransport_options', webRtcTransport_options);
// console.log('router', router, '| router[callId]', router[callId]);
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport // https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
let transport = await router[callId].createWebRtcTransport(webRtcTransport_options) let transport = await videoCalls[callId].router.createWebRtcTransport(webRtcTransport_options)
console.log(`callId: ${callId} | transport id: ${transport.id}`) console.log(`callId: ${callId} | transport id: ${transport.id}`)
// Handler for when DTLS(Datagram Transport Layer Security) changes
transport.on('dtlsstatechange', dtlsState => { transport.on('dtlsstatechange', dtlsState => {
console.log(`transport | dtlsstatechange | calldId ${callId} | dtlsState ${dtlsState}`);
if (dtlsState === 'closed') { if (dtlsState === 'closed') {
transport.close() transport.close();
} }
}) });
// Handler if the transport layer has closed (for various reasons)
transport.on('close', () => { transport.on('close', () => {
console.log('transport closed') console.log(`transport | closed | calldId ${callId}`);
}) });
const params = { const params = {
id: transport.id, id: transport.id,
iceParameters: transport.iceParameters, iceParameters: transport.iceParameters,
iceCandidates: transport.iceCandidates, iceCandidates: transport.iceCandidates,
dtlsParameters: transport.dtlsParameters, dtlsParameters: transport.dtlsParameters,
} };
console.log('params', params); // Send back to the client the params
callback({ params });
// send back to the client the following prameters // Set transport to producerTransport or consumerTransport
callback({ return transport;
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#TransportOptions
params
})
return transport
} catch (error) { } catch (error) {
console.log('[createWebRtcTransportLayer] ERROR', JSON.stringify(error)); console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
callback({ callback({ params: { error } });
params: {
error: error
}
})
} }
} }

BIN
doc/[video] Workflow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 KiB

805
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,20 +5,17 @@
"main": "app.js", "main": "app.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start:dev": "nodemon app.ts", "start:dev": "nodemon app.js",
"start:prod": "pm2 start ./app.js -n video-server", "start:prod": "pm2 start ./app.js -n video-server",
"watch": "watchify public/index.js -o public/bundle.js -v" "watch": "watchify public/index.js -o public/bundle.js -v"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"type": "module",
"dependencies": { "dependencies": {
"@types/express": "^4.17.13", "@types/express": "^4.17.13",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",
"express": "^4.18.1", "express": "^4.18.1",
"ffmpeg-static": "^5.0.2",
"httpolyglot": "^0.1.2",
"mediasoup": "^3.10.4", "mediasoup": "^3.10.4",
"mediasoup-client": "^3.6.54", "mediasoup-client": "^3.6.54",
"parcel": "^2.7.0", "parcel": "^2.7.0",

View File

@ -20808,7 +20808,7 @@ const getLocalStream = () => {
}) })
.then(streamSuccess) .then(streamSuccess)
.catch(error => { .catch(error => {
console.log('getLocalStream', error) console.log(error.message)
}) })
} }
@ -20903,7 +20903,7 @@ const createSendTransport = () => {
}) })
producerTransport.on('produce', async (parameters, callback, errback) => { producerTransport.on('produce', async (parameters, callback, errback) => {
console.log('produce', parameters) console.log(parameters)
try { try {
// tell the server to create a Producer // tell the server to create a Producer
@ -20913,7 +20913,7 @@ const createSendTransport = () => {
await socket.emit('transport-produce', { await socket.emit('transport-produce', {
kind: parameters.kind, kind: parameters.kind,
rtpParameters: parameters.rtpParameters, rtpParameters: parameters.rtpParameters,
callId: callId appData: parameters.appData,
}, ({ id }) => { }, ({ id }) => {
// Tell the transport that parameters were transmitted and provide it with the // Tell the transport that parameters were transmitted and provide it with the
// server side producer's id. // server side producer's id.
@ -21009,6 +21009,7 @@ const createRecvTransport = async () => {
} }
const resetCallSettings = () => { const resetCallSettings = () => {
socket.emit('transportclose', { callId })
localVideo.srcObject = null localVideo.srcObject = null
remoteVideo.srcObject = null remoteVideo.srcObject = null
consumer = null consumer = null
@ -21059,9 +21060,9 @@ const closeCall = () => {
// Emit 'notify-end' to Hub so the consumer will know to close the video // Emit 'notify-end' to Hub so the consumer will know to close the video
const notifyEnd = { const notifyEnd = {
origin_asset_id: ASSET_ID, origin_asset_id: ASSET_ID,
dest_asset_id: originAssetId || parseInt(urlParams.get('dest_asset_id')), dest_asset_id: originAssetId || parseInt(urlParams.get('dest_asset_id')),
type: 'notify-end', type: 'notify-end',
video_call_id: callId video_call_id: callId
} }
console.log('notifyEnd', notifyEnd) console.log('notifyEnd', notifyEnd)
@ -21071,7 +21072,7 @@ const closeCall = () => {
const closeCallBtn = document.getElementById('btnCloseCall') const closeCallBtn = document.getElementById('btnCloseCall')
closeCallBtn.setAttribute('disabled', '') closeCallBtn.setAttribute('disabled', '')
// Reset settings // Reset settings and send closeTransport to video server
resetCallSettings() resetCallSettings()
} }

View File

@ -1,5 +1,5 @@
module.exports = { module.exports = {
hubAddress: 'https://hub.dev.linx.safemobile.com/', hubAddress: 'https://hub.dev.linx.safemobile.com/',
// mediasoupAddress: 'https://video.safemobile.org/mediasoup', mediasoupAddress: 'https://video.safemobile.org/mediasoup',
mediasoupAddress: 'http://localhost:3000/mediasoup', // mediasoupAddress: 'http://localhost:3000/mediasoup',
} }

View File

@ -149,7 +149,7 @@ const getLocalStream = () => {
}) })
.then(streamSuccess) .then(streamSuccess)
.catch(error => { .catch(error => {
console.log('getLocalStream', error) console.log(error.message)
}) })
} }
@ -244,7 +244,7 @@ const createSendTransport = () => {
}) })
producerTransport.on('produce', async (parameters, callback, errback) => { producerTransport.on('produce', async (parameters, callback, errback) => {
console.log('produce', parameters) console.log(parameters)
try { try {
// tell the server to create a Producer // tell the server to create a Producer
@ -254,7 +254,7 @@ const createSendTransport = () => {
await socket.emit('transport-produce', { await socket.emit('transport-produce', {
kind: parameters.kind, kind: parameters.kind,
rtpParameters: parameters.rtpParameters, rtpParameters: parameters.rtpParameters,
callId: callId appData: parameters.appData,
}, ({ id }) => { }, ({ id }) => {
// Tell the transport that parameters were transmitted and provide it with the // Tell the transport that parameters were transmitted and provide it with the
// server side producer's id. // server side producer's id.