Compare commits

..

176 Commits

Author SHA1 Message Date
950298c4f6 Update server 2022-11-24 16:21:22 +02:00
6e74083733 Update server 2022-11-24 13:49:04 +02:00
8ef6c2abb0 Update server 2022-11-24 13:43:33 +02:00
2a86042c80 Update server 2022-11-24 13:41:24 +02:00
56b8e2ea74 Update server 2022-11-24 13:38:28 +02:00
6c42814229 Update server 2022-11-24 13:37:42 +02:00
e65b7e0d7c Update server 2022-11-24 13:36:21 +02:00
aa7c2aea90 Update server 2022-11-24 13:35:32 +02:00
458342c0d2 Update server 2022-11-24 13:32:45 +02:00
fa5a1a5ae7 Update server 2022-11-23 17:56:18 +02:00
9fbe01ae1d Update server 2022-11-23 17:19:55 +02:00
e5bcc6262b Update server 2022-11-23 17:11:09 +02:00
c758a9106c Update server 2022-11-23 16:27:13 +02:00
fcbc28c801 Update server 2022-11-23 16:26:28 +02:00
ba63fb20bf Update server 2022-11-23 16:24:12 +02:00
e8bd6837cf Update server 2022-11-23 16:21:48 +02:00
d386915ff2 Update server 2022-11-23 16:16:17 +02:00
2479f58e21 Update server 2022-11-23 16:09:26 +02:00
d49b8e42ff Update server 2022-11-23 16:03:13 +02:00
a3ae874f8e Update server 2022-11-23 16:01:51 +02:00
c2dbef1918 Update server 2022-11-23 16:00:54 +02:00
b41b8f2d64 Update server 2022-11-23 15:57:27 +02:00
c089e91fba Update server 2022-11-23 13:20:49 +02:00
c63aee83a1 Update server 2022-11-23 13:19:56 +02:00
a97ec24148 Update server 2022-11-23 00:54:03 +02:00
3c23c6791d Update server 2022-11-23 00:34:12 +02:00
1a7b44807d Update server 2022-11-23 00:32:24 +02:00
daa2c556e4 Update server 2022-11-23 00:25:16 +02:00
22656722e8 Update server 2022-11-23 00:22:00 +02:00
f5b9067b7e Update server 2022-11-23 00:21:26 +02:00
0b3a45ae45 Update server 2022-11-23 00:13:56 +02:00
dfe4630839 Update server 2022-11-23 00:11:26 +02:00
d18041cadd Update server 2022-11-23 00:11:16 +02:00
fa42caeeb2 Update server 2022-11-22 23:14:01 +02:00
4dbb7ad554 Update server 2022-11-22 23:12:55 +02:00
d1063803b9 Update server 2022-11-22 23:12:21 +02:00
3cbd31b49c Update server 2022-11-22 23:11:45 +02:00
a39e0eaa17 Update server 2022-11-22 23:11:14 +02:00
b63fb39fd4 Update server 2022-11-22 23:09:33 +02:00
0dfbd296a7 Update server 2022-11-22 20:44:22 +02:00
233f49a998 Update server 2022-11-22 20:43:30 +02:00
127f17cd97 Update server 2022-11-22 20:42:55 +02:00
d1ad8b4d3a Update server 2022-11-22 20:38:16 +02:00
f20e1ad260 Update build 2022-11-22 20:04:43 +02:00
27151a26d1 Update build 2022-11-22 20:03:36 +02:00
544e9e59ab Update build 2022-11-22 20:00:44 +02:00
4e4cd6f893 Update build 2022-11-22 19:55:55 +02:00
e9ff060544 Update build 2022-11-22 19:52:25 +02:00
7d677f4a34 Update build 2022-11-22 19:52:11 +02:00
8f96b8c98b Update build 2022-11-22 19:51:09 +02:00
1084a808c7 Update build 2022-11-22 19:40:02 +02:00
3838f774bf Update build 2022-11-22 19:33:38 +02:00
06bb275f0d Update build 2022-11-22 19:18:48 +02:00
a05f7cc987 Update build 2022-11-22 19:15:34 +02:00
c5c8bc5bb3 Update build 2022-11-22 19:14:50 +02:00
d6bc4e51e5 Update build 2022-11-22 19:11:00 +02:00
4ae02f70d6 Update build 2022-11-22 18:54:22 +02:00
d593d6dc83 Update build 2022-11-22 18:35:36 +02:00
1a1fa9450e Update build 2022-11-22 18:34:12 +02:00
0d24604f2a Update build 2022-11-22 18:33:46 +02:00
1d7c994036 Update build 2022-11-22 18:33:06 +02:00
bc2bf24a65 Update build 2022-11-22 18:32:47 +02:00
cdbfc7891d Update build 2022-11-22 18:30:25 +02:00
c730341674 Update build 2022-11-22 18:28:50 +02:00
b621b76e37 Connect to mediasoup with timeout(fix when it appears offline) 2022-11-22 18:27:56 +02:00
39ad9cad27 Update bundle 2022-11-22 18:10:05 +02:00
8860423e21 LH-265: Update client config 2022-11-22 10:28:45 +02:00
9179a67f64 LH-265: Enable audio on video server 2022-11-21 22:59:41 +02:00
75d0e3aee7 exe right for build.sh 2022-10-31 22:26:08 +00:00
30ac997634 Merge pull request 'added build.sh' (#14) from temp-build into develop
Reviewed-on: #14
2022-10-31 22:22:20 +00:00
5aea138f6a added build.sh 2022-10-31 12:17:07 +02:00
5b01ddc2a8 Merge pull request 'LH-259-mediasoup-always-return-a-callback-response-to-clients' (#13) from LH-259-mediasoup-always-return-a-callback-response-to-clients into develop
Reviewed-on: #13
2022-10-25 16:18:54 +00:00
084ff36ebe LH-259: Refactor createRoom 2022-10-24 22:38:06 +03:00
f4ebf92783 LH-259: Added callback from transport-produce 2022-10-24 22:35:22 +03:00
b59a157b18 LH-259: Comment callback from transport-produce 2022-10-24 22:19:37 +03:00
9f8347bec5 LH-259: Update createRoom callback 2022-10-24 22:16:47 +03:00
24390c98e5 LH-259: Added callbacks 2022-10-24 22:11:14 +03:00
1a7371fe18 Parse RTC_MIN_PORT and RTC_MAX_PORT 2022-10-18 18:27:02 +03:00
be5f97762a Merge pull request 'LH-253: Added callId for transportclose and producerclose events' (#12) from LH-253-mediasoup-handle-callid-undefined into master
Reviewed-on: #12
2022-10-18 07:53:33 +00:00
03a11126c4 LH-253: Check if we have callId in closeCall 2022-10-18 10:51:20 +03:00
fafbee6e4c LH-253: Added callId for transportclose and producerclose events 2022-10-18 02:05:22 +03:00
bbf23c33d4 Merge pull request 'LH-252: Update .env variables' (#11) from LH-252-mediasoup-add-a-config-file-with-keys-and-ports into master
Reviewed-on: #11
2022-10-09 06:50:45 +00:00
5c2808e75a LH-252: Update .env variables 2022-10-06 15:21:54 +03:00
2aea7497cc Merge pull request 'added log for dtls transport-connect' (#10) from LH-249-debug-for-i-os-dtls-problems into master
Reviewed-on: #10
2022-10-06 06:41:07 +00:00
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
12 changed files with 1495 additions and 1946 deletions

4
.env
View File

@ -1,3 +1,7 @@
PORT=3000
IP=0.0.0.0 # Listening IPv4 or IPv6.
ANNOUNCED_IP=185.8.154.190 # Announced IPv4 or IPv6 (useful when running mediasoup behind NAT with private IP).
RTC_MIN_PORT=2000
RTC_MAX_PORT=2020
SERVER_CERT="./server/ssl/cert.pem"
SERVER_KEY="./server/ssl/key.pem"

1
.gitignore vendored
View File

@ -1 +1,2 @@
/node_modules
/dist

View File

@ -1,5 +1,11 @@
# 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
@ -26,4 +32,8 @@ accountId = account id of the unit on which you are doing the test
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)
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`

906
app.js
View File

@ -1,31 +1,38 @@
import 'dotenv/config'
require('dotenv').config()
/**
* 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()
import https from 'httpolyglot'
import fs from 'fs'
import path from 'path'
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'
const express = require('express');
const app = express();
const Server = require('socket.io');
const path = require('node:path');
const fs = require('node:fs');
let https;
try {
https = require('node:https');
} catch (err) {
console.log('https support is disabled!');
}
const mediasoup = require('mediasoup');
let worker
let router = {}
let producerTransport
let consumerTransport
let producer
let consumer
/**
* videoCalls
* |-> Router
* |-> Producer
* |-> 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) => {
res.send('Hello from mediasoup app!')
@ -35,522 +42,368 @@ app.use('/sfu', express.static(path.join(__dirname, 'public')))
// SSL cert for HTTPS access
const options = {
key: fs.readFileSync('./server/ssl/key.pem', 'utf-8'),
cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8')
key: fs.readFileSync(process.env.SERVER_KEY, 'utf-8'),
cert: fs.readFileSync(process.env.SERVER_CERT, '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, () => {
console.log('Listening on port:', process.env.PORT)
})
console.log('Video server listening on port:', process.env.PORT);
});
const startRecordingFfmpeg = () => {
// 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 peers = io.of('/');
const createWorker = async () => {
worker = await mediasoup.createWorker({
rtcMinPort: 32256,
rtcMaxPort: 65535,
})
console.log(`[createWorker] worker pid ${worker.pid}`)
worker.on('died', error => {
// This implies something serious happened, so kill the application
console.error('mediasoup worker has died', error)
setTimeout(() => process.exit(1), 2000) // exit in 2 seconds
})
return worker
try {
worker = await mediasoup.createWorker({
rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
})
console.log(`[createWorker] worker pid ${worker.pid}`);
worker.on('died', error => {
// This implies something serious happened, so kill the application
console.error('mediasoup worker has died', error);
setTimeout(() => process.exit(1), 2000); // exit in 2 seconds
})
return worker;
} catch (error) {
console.log(`ERROR | createWorker | ${error.message}`);
}
}
// We create a Worker as soon as our application starts
worker = createWorker()
worker = createWorker();
// This is an Array of RtpCapabilities
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
// list of media codecs supported by mediasoup ...
// https://github.com/versatica/mediasoup/blob/v3/src/supportedRtpCapabilities.ts
const mediaCodecs = [
const mediaCodecs = [
{
kind: "audio",
mimeType: "audio/opus",
preferredPayloadType: 111,
clockRate: 48000,
channels: 2,
parameters: {
minptime: 10,
useinbandfec: 1,
},
kind : 'audio',
mimeType : 'audio/opus',
clockRate : 48000,
channels : 2
},
{
kind: "video",
mimeType: "video/VP8",
preferredPayloadType: 96,
clockRate: 90000,
},
{
kind: "video",
mimeType: "video/H264",
preferredPayloadType: 125,
clockRate: 90000,
parameters: {
"level-asymmetry-allowed": 1,
"packetization-mode": 1,
"profile-level-id": "42e01f",
},
},
]
peers.on('connection', async socket => {
console.log('[connection] socketId:', socket.id)
socket.emit('connection-success', {
socketId: socket.id,
existsProducer: producer ? true : false,
})
socket.on('disconnect', () => {
// do some cleanup
console.log('peer disconnected')
})
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}`)
kind : 'video',
mimeType : 'video/VP8',
clockRate : 90000,
parameters :
{
'x-google-start-bitrate' : 1000
}
},
{
kind : 'video',
mimeType : 'video/VP9',
clockRate : 90000,
parameters :
{
'profile-id' : 2,
'x-google-start-bitrate' : 1000
}
},
{
kind : 'video',
mimeType : 'video/h264',
clockRate : 90000,
parameters :
{
'packetization-mode' : 1,
'profile-level-id' : '4d0032',
'level-asymmetry-allowed' : 1,
'x-google-start-bitrate' : 1000
}
},
{
kind : 'video',
mimeType : 'video/h264',
clockRate : 90000,
parameters :
{
'packetization-mode' : 1,
'profile-level-id' : '42e01f',
'level-asymmetry-allowed' : 1,
'x-google-start-bitrate' : 1000
}
getRtpCapabilities(callId, callback)
})
const getRtpCapabilities = (callId, callback) => {
const rtpCapabilities = router[callId].rtpCapabilities
callback({ rtpCapabilities })
}
// {
// kind: 'audio',
// mimeType: 'audio/opus',
// clockRate: 48000,
// channels: 2,
// },
// {
// kind: 'video',
// mimeType: 'video/VP8',
// clockRate: 90000,
// parameters: {
// 'x-google-start-bitrate': 1000,
// },
// },
];
// 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(`[createWebRtcTransport] 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 closeCall = (callId) => {
try {
if (callId && videoCalls[callId]) {
videoCalls[callId].producer?.close();
videoCalls[callId].consumer?.close();
videoCalls[callId]?.consumerTransport?.close();
videoCalls[callId]?.producerTransport?.close();
videoCalls[callId]?.router?.close();
delete videoCalls[callId];
} else {
console.log(`The call with id ${callId} has already been deleted`);
}
} catch (error) {
console.log(`ERROR | closeCall | callid ${callId} | ${error.message}`);
}
}
// see client's socket.emit('transport-connect', ...)
socket.on('transport-connect', async ({ dtlsParameters }) => {
console.log('[transport-connect] DTLS PARAMS... ', { dtlsParameters })
await producerTransport.connect({ dtlsParameters })
})
/*
- Handlers for WS events
- These are created only when we have a connection with a peer
*/
peers.on('connection', async socket => {
console.log('[connection] socketId:', socket.id);
// see client's socket.emit('transport-produce', ...)
socket.on('transport-produce', async ({ kind, rtpParameters, callId }, callback) => {
// call produce based on the prameters from the client
producer = await producerTransport.produce({
kind,
rtpParameters,
})
// After making the connection successfully, we send the client a 'connection-success' event
socket.emit('connection-success', {
socketId: socket.id
});
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)
// https://mediasoup.org/documentation/v3/mediasoup/api/#producer-close
producer.close()
// 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) => {
/*
- This event creates a room with the roomId and the callId sent
- It will return the rtpCapabilities of that room
- If the room already exists, it will not create it, but will only return rtpCapabilities
*/
socket.on('createRoom', async ({ callId }, callback) => {
let callbackResponse = null;
try {
console.log('consume', rtpCapabilities, callId);
// 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,
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,
// We can continue with the room creation process only if we have a callId
if (callId) {
console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`);
if (!videoCalls[callId]) {
console.log('[createRoom] callId', callId);
videoCalls[callId] = { router: await worker.createRouter({ mediaCodecs }) }
console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
}
socketDetails[socket.id] = callId;
// send the parameters to the client
callback({ params })
// rtpCapabilities is set for callback
console.log('[getRtpCapabilities] callId', callId);
callbackResponse = {
rtpCapabilities :videoCalls[callId].router.rtpCapabilities
};
} else {
console.log(`[createRoom] missing callId ${callId}`);
}
} catch (error) {
console.log(error.message)
callback({
params: {
error: error
console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`);
} finally {
callback(callbackResponse);
}
});
/*
- 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}`);
callback(null);
}
})
} else if (!sender) {
if (!videoCalls[callId].consumerTransport) {
videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback);
} else {
console.log(`consumerTransport has already been defined | callId ${callId}`);
callback(null);
}
}
} catch (error) {
console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
callback(error);
}
});
/*
- 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] kind: ${kind} | socket.id: ${socket.id} | callId: ${callId}`);
console.log('kind', kind);
console.log('rtpParameters', rtpParameters);
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 && 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]`)
await consumer.resume()
})
})
/*
- The customer consumes after successfully connecting to consumerTransport
- 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(callId);
});
// 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(callId);
});
// 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}`);
callback(null);
}
} 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) => {
try {
console.log('[createWebRtcTransportLayer] callId', callId);
@ -565,49 +418,40 @@ const createWebRtcTransportLayer = async (callId, callback) => {
enableUdp: true,
enableTcp: 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
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}`)
// Handler for when DTLS(Datagram Transport Layer Security) changes
transport.on('dtlsstatechange', dtlsState => {
console.log(`transport | dtlsstatechange | calldId ${callId} | dtlsState ${dtlsState}`);
if (dtlsState === 'closed') {
transport.close()
transport.close();
}
})
});
// Handler if the transport layer has closed (for various reasons)
transport.on('close', () => {
console.log('transport closed')
})
console.log(`transport | closed | calldId ${callId}`);
});
const params = {
id: transport.id,
iceParameters: transport.iceParameters,
iceCandidates: transport.iceCandidates,
dtlsParameters: transport.dtlsParameters,
}
};
console.log('params', params);
// Send back to the client the params
callback({ params });
// send back to the client the following prameters
callback({
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#TransportOptions
params
})
return transport
// Set transport to producerTransport or consumerTransport
return transport;
} catch (error) {
console.log('[createWebRtcTransportLayer] ERROR', JSON.stringify(error));
callback({
params: {
error: error
}
})
console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
callback({ params: { error } });
}
}

48
build.sh Executable file
View File

@ -0,0 +1,48 @@
#/!bin/bash
## PREBUILD PROCESS
# check dist dir to be present and empty
if [ ! -d "dist" ]; then
## MAKE DIR
mkdir "dist"
echo "Directory dist created."
else
## CLEANUP
rm -fr dist/*
fi
# Install dependencies
#npm install
## PROJECT NEEDS
echo "Building app... from $(git rev-parse --abbrev-ref HEAD)"
#npm run-script build
cp -r {.env,app.js,package.json,server,public} dist/
#Add version control for pm2
cd dist
#Add version control for pm2
version=$(git describe)
file_pkg="package.json"
key=" \"version\": \""
count=$(echo ${version%%-*} | grep -o "\." | wc -l)
if (( $count > 1 )); then
version=${version%%-*}
else
version="${version%%-*}.0"
fi
if [ -f "$file_pkg" ] && [ ! -z "$version" ]; then
version=" \"version\": \"$version\","
sed -i "s|^.*$key.*|${version//\//\\/}|g" $file_pkg
text=$(cat $file_pkg | grep -c "$version")
if [ $text -eq 0 ]; then
echo "Version couldn't be set"
else
echo "Version $version successfully applied to App"
fi
fi
## POST BUILD
cd -

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",
"scripts": {
"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",
"watch": "watchify public/index.js -o public/bundle.js -v"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"@types/express": "^4.17.13",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"ffmpeg-static": "^5.0.2",
"httpolyglot": "^0.1.2",
"mediasoup": "^3.10.4",
"mediasoup-client": "^3.6.54",
"parcel": "^2.7.0",

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -43,7 +43,7 @@
<tr>
<td>
<div id="sharedBtns">
<video id="localVideo" autoplay class="video" ></video>
<video id="localVideo" autoplay class="video" muted></video>
</div>
</td>
<td>

View File

@ -12,144 +12,194 @@ let callId = parseInt(urlParams.get('callId')) || null;
const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false
console.log('[URL] ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER)
let socket
hub = io(config.hubAddress)
console.log('🟩 config', config)
const connectToMediasoup = () => {
let socket, hub
socket = io(config.mediasoupAddress, {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax : 5000,
reconnectionAttempts: Infinity
})
socket.on('connection-success', ({ _socketId, existsProducer }) => {
console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`)
if (!IS_PRODUCER && existsProducer && consumer === undefined) {
goConnect()
// document.getElementById('btnRecvSendTransport').click();
}
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() }
})
}
if (IS_PRODUCER === true) {
hub.on('connect', async () => {
console.log(`[HUB] ${config.hubAddress} | connected: ${hub.connected}`)
connectToMediasoup()
hub.emit(
'ars',
JSON.stringify({
ars: true,
asset_id: ASSET_ID,
account_id: ACCOUNT_ID,
})
)
setTimeout(() => {
hub = io(config.hubAddress)
hub.on('video', (data) => {
const parsedData = JSON.parse(data);
if (parsedData.type === 'notify-request') {
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;
const connectToMediasoup = () => {
console.log('[VIDEO] notify-request | IS_PRODUCER', IS_PRODUCER, 'callId', callId);
getLocalStream()
}
if (parsedData.type === 'notify-end') {
console.log('[VIDEO] notify-end | IS_PRODUCER', IS_PRODUCER, 'callId', callId);
resetCallSettings()
}
socket = io(config.mediasoupAddress, {
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax : 5000,
reconnectionAttempts: Infinity
})
})
socket.on('connection-success', ({ _socketId, existsProducer }) => {
console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`)
if (!IS_PRODUCER && existsProducer && consumer === undefined) {
goConnect()
// document.getElementById('btnRecvSendTransport').click();
}
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() }
})
}
if (IS_PRODUCER === true) {
hub.on('connect', async () => {
console.log(`[HUB]! ${config.hubAddress} | connected: ${hub.connected}`)
connectToMediasoup()
hub.emit(
'ars',
JSON.stringify({
ars: true,
asset_id: ASSET_ID,
account_id: ACCOUNT_ID,
})
)
hub.on('video', (data) => {
const parsedData = JSON.parse(data);
if (parsedData.type === 'notify-request') {
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('[VIDEO] notify-request | IS_PRODUCER', IS_PRODUCER, 'callId', callId);
getLocalStream()
}
if (parsedData.type === 'notify-end') {
console.log('[VIDEO] notify-end | IS_PRODUCER', IS_PRODUCER, 'callId', callId);
resetCallSettings()
}
})
})
hub.on('connect_error', (error) => {
console.log('connect_error', error);
});
hub.on('connection', () => {
console.log('connection')
})
hub.on('disconnect', () => {
console.log('disconnect')
})
} else {
connectToMediasoup()
}
}, 1600);
hub.on('connect_error', (error) => {
console.log('connect_error', error);
});
hub.on('connection', () => {
console.log('connection')
})
hub.on('disconnect', () => {
console.log('disconnect')
})
} else {
connectToMediasoup()
}
let device
let rtpCapabilities
let producerTransport
let consumerTransport
let producer
let producerVideo
let producerAudio
let consumer
let originAssetId
// let originAssetName = 'Adi'
// let originAssetTypeName = 'linx'
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#ProducerOptions
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
let params = {
// mediasoup params
let videoParams = {
encodings: [
{
rid: 'r0',
maxBitrate: 100000,
scalabilityMode: 'S1T3',
},
{
rid: 'r1',
maxBitrate: 300000,
scalabilityMode: 'S1T3',
},
{
rid: 'r2',
maxBitrate: 900000,
scalabilityMode: 'S1T3',
},
{ scaleResolutionDownBy: 4, maxBitrate: 500000 },
{ scaleResolutionDownBy: 2, maxBitrate: 1000000 },
{ scaleResolutionDownBy: 1, maxBitrate: 5000000 },
{ scalabilityMode: 'S3T3_KEY' }
],
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#ProducerCodecOptions
codecOptions: {
videoGoogleStartBitrate: 1000
}
// encodings: [
// {
// rid: 'r0',
// maxBitrate: 100000,
// scalabilityMode: 'S1T3',
// },
// {
// rid: 'r1',
// maxBitrate: 300000,
// scalabilityMode: 'S1T3',
// },
// {
// rid: 'r2',
// maxBitrate: 900000,
// scalabilityMode: 'S1T3',
// },
// ],
// // https://mediasoup.org/documentation/v3/mediasoup-client/api/#ProducerCodecOptions
// codecOptions: {
// videoGoogleStartBitrate: 1000
// }
}
let audioParams = {
codecOptions :
{
opusStereo : true,
opusDtx : true
}
}
const streamSuccess = (stream) => {
console.log('[streamSuccess]');
console.log('[streamSuccess] device', device);
localVideo.srcObject = stream
const track = stream.getVideoTracks()[0]
params = {
track,
...params
console.log('stream', stream);
const videoTrack = stream.getVideoTracks()[0]
const audioTrack = stream.getAudioTracks()[0]
videoParams = {
track: videoTrack,
// codec : device.rtpCapabilities.codecs.find((codec) => codec.mimeType.toLowerCase() === 'video/vp9'),
// codec : 'video/vp9',
...videoParams
}
audioParams = {
track: audioTrack,
...audioParams
}
console.log('[streamSuccess] videoParams', videoParams, ' | audioParams', audioParams);
goConnect()
// console.log('[streamSuccess]');
// localVideo.srcObject = stream
// const track = stream.getVideoTracks()[0]
// videoParams = {
// track,
// ...videoParams
// }
// goConnect()
}
const getLocalStream = () => {
console.log('[getLocalStream]');
navigator.mediaDevices.getUserMedia({
audio: false,
audio: true,
video: {
width: {
min: 640,
max: 1920,
},
height: {
min: 400,
max: 1080,
}
qvga : { width: { ideal: 320 }, height: { ideal: 240 } },
vga : { width: { ideal: 640 }, height: { ideal: 480 } },
hd : { width: { ideal: 1280 }, height: { ideal: 720 } }
}
})
// navigator.mediaDevices.getUserMedia({
// audio: false,
// video: {
// width: {
// min: 640,
// max: 1920,
// },
// height: {
// min: 400,
// max: 1080,
// }
// }
// })
.then(streamSuccess)
.catch(error => {
console.log('getLocalStream', error)
console.log(error.message)
})
}
@ -167,7 +217,7 @@ const goCreateTransport = () => {
// server side to send/recive media
const createDevice = async () => {
try {
console.log('[createDevice]');
console.log('[createDevice] 1 device', device);
device = new mediasoupClient.Device()
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#device-load
@ -178,7 +228,8 @@ const createDevice = async () => {
})
console.log('Device RTP Capabilities', device.rtpCapabilities)
console.log('[createDevice] 2 device', device);
// once the device loads, create transport
goCreateTransport()
@ -207,6 +258,7 @@ const getRtpCapabilities = () => {
}
const createSendTransport = () => {
console.log('[createSendTransport');
// see server's socket.on('createWebRtcTransport', sender?, ...)
// this is a call from Producer, so sender = true
socket.emit('createWebRtcTransport', { sender: true, callId }, ({ params }) => {
@ -217,7 +269,7 @@ const createSendTransport = () => {
return
}
console.log(params)
console.log('[createWebRtcTransport] params', params)
// creates a new WebRTC Transport to send media
// based on the server's producer transport params
@ -244,7 +296,7 @@ const createSendTransport = () => {
})
producerTransport.on('produce', async (parameters, callback, errback) => {
console.log('produce', parameters)
console.log('[produce] parameters', parameters)
try {
// tell the server to create a Producer
@ -254,7 +306,7 @@ const createSendTransport = () => {
await socket.emit('transport-produce', {
kind: parameters.kind,
rtpParameters: parameters.rtpParameters,
callId: callId
appData: parameters.appData,
}, ({ id }) => {
// Tell the transport that parameters were transmitted and provide it with the
// server side producer's id.
@ -270,22 +322,44 @@ const createSendTransport = () => {
}
const connectSendTransport = async () => {
console.log('[connectSendTransport] producerTransport');
// we now call produce() to instruct the producer transport
// to send media to the Router
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
// this action will trigger the 'connect' and 'produce' events above
producer = await producerTransport.produce(params)
producer.on('trackended', () => {
producerVideo = await producerTransport.produce(videoParams)
console.log('producerVideo', producerVideo);
producerVideo.on('trackended', () => {
console.log('track ended')
// close video track
})
producer.on('transportclose', () => {
producerVideo.on('transportclose', () => {
console.log('transport ended')
// close video track
})
producerAudio = await producerTransport.produce(audioParams)
console.log('producerAudio', producerAudio);
producerAudio.on('trackended', () => {
console.log('track ended')
// close video track
})
producerAudio.on('transportclose', () => {
console.log('transport ended')
// close video track
})
const answer = {
origin_asset_id: ASSET_ID,
dest_asset_id: originAssetId || parseInt(urlParams.get('dest_asset_id')),
@ -320,7 +394,7 @@ const createRecvTransport = async () => {
return
}
console.log(params)
console.log('[createRecvTransport] params', params)
// creates a new WebRTC Transport to receive media
// based on server's consumer transport params
@ -353,7 +427,8 @@ const resetCallSettings = () => {
localVideo.srcObject = null
remoteVideo.srcObject = null
consumer = null
producer = null
producerVideo = null
producerAudio = null
producerTransport = null
consumerTransport = null
device = undefined