Compare commits
80 Commits
LINXD-2197
...
458342c0d2
Author | SHA1 | Date | |
---|---|---|---|
458342c0d2 | |||
fa5a1a5ae7 | |||
9fbe01ae1d | |||
e5bcc6262b | |||
c758a9106c | |||
fcbc28c801 | |||
ba63fb20bf | |||
e8bd6837cf | |||
d386915ff2 | |||
2479f58e21 | |||
d49b8e42ff | |||
a3ae874f8e | |||
c2dbef1918 | |||
b41b8f2d64 | |||
c089e91fba | |||
c63aee83a1 | |||
a97ec24148 | |||
3c23c6791d | |||
1a7b44807d | |||
daa2c556e4 | |||
22656722e8 | |||
f5b9067b7e | |||
0b3a45ae45 | |||
dfe4630839 | |||
d18041cadd | |||
fa42caeeb2 | |||
4dbb7ad554 | |||
d1063803b9 | |||
3cbd31b49c | |||
a39e0eaa17 | |||
b63fb39fd4 | |||
0dfbd296a7 | |||
233f49a998 | |||
127f17cd97 | |||
d1ad8b4d3a | |||
f20e1ad260 | |||
27151a26d1 | |||
544e9e59ab | |||
4e4cd6f893 | |||
e9ff060544 | |||
7d677f4a34 | |||
8f96b8c98b | |||
1084a808c7 | |||
3838f774bf | |||
06bb275f0d | |||
a05f7cc987 | |||
c5c8bc5bb3 | |||
d6bc4e51e5 | |||
4ae02f70d6 | |||
d593d6dc83 | |||
1a1fa9450e | |||
0d24604f2a | |||
1d7c994036 | |||
bc2bf24a65 | |||
cdbfc7891d | |||
c730341674 | |||
b621b76e37 | |||
39ad9cad27 | |||
8860423e21 | |||
9179a67f64 | |||
75d0e3aee7 | |||
30ac997634 | |||
5aea138f6a | |||
5b01ddc2a8 | |||
084ff36ebe | |||
f4ebf92783 | |||
b59a157b18 | |||
9f8347bec5 | |||
24390c98e5 | |||
1a7371fe18 | |||
be5f97762a | |||
03a11126c4 | |||
fafbee6e4c | |||
bbf23c33d4 | |||
5c2808e75a | |||
2aea7497cc | |||
56835d6660 | |||
fc42c79210 | |||
d81bc8582d | |||
de1458bbde |
4
.env
4
.env
@ -1,3 +1,7 @@
|
|||||||
PORT=3000
|
PORT=3000
|
||||||
IP=0.0.0.0 # Listening IPv4 or IPv6.
|
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).
|
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
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/node_modules
|
/node_modules
|
||||||
|
/dist
|
||||||
|
137
app.js
137
app.js
@ -5,7 +5,7 @@ const app = express();
|
|||||||
const Server = require('socket.io');
|
const Server = require('socket.io');
|
||||||
const path = require('node:path');
|
const path = require('node:path');
|
||||||
const fs = require('node:fs');
|
const fs = require('node:fs');
|
||||||
let https = require('https');
|
let https;
|
||||||
try {
|
try {
|
||||||
https = require('node:https');
|
https = require('node:https');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -42,8 +42,8 @@ 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(process.env.SERVER_KEY, 'utf-8'),
|
||||||
cert: fs.readFileSync('./server/ssl/cert.pem', 'utf-8'),
|
cert: fs.readFileSync(process.env.SERVER_CERT, 'utf-8'),
|
||||||
}
|
}
|
||||||
|
|
||||||
const httpsServer = https.createServer(options, app);
|
const httpsServer = https.createServer(options, app);
|
||||||
@ -59,16 +59,16 @@ const io = new Server(httpsServer, {
|
|||||||
// const io = new Server(server, { origins: '*:*', allowEIO3: true });
|
// const io = new Server(server, { origins: '*:*', allowEIO3: true });
|
||||||
|
|
||||||
httpsServer.listen(process.env.PORT, () => {
|
httpsServer.listen(process.env.PORT, () => {
|
||||||
console.log('Video server listening on port:', process.env.PORT)
|
console.log('Video server listening on port:', process.env.PORT);
|
||||||
})
|
});
|
||||||
|
|
||||||
const peers = io.of('/')
|
const peers = io.of('/');
|
||||||
|
|
||||||
const createWorker = async () => {
|
const createWorker = async () => {
|
||||||
try {
|
try {
|
||||||
worker = await mediasoup.createWorker({
|
worker = await mediasoup.createWorker({
|
||||||
rtcMinPort: 2000,
|
rtcMinPort: parseInt(process.env.RTC_MIN_PORT),
|
||||||
rtcMaxPort: 2020,
|
rtcMaxPort: parseInt(process.env.RTC_MAX_PORT),
|
||||||
})
|
})
|
||||||
console.log(`[createWorker] worker pid ${worker.pid}`);
|
console.log(`[createWorker] worker pid ${worker.pid}`);
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ const createWorker = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We create a Worker as soon as our application starts
|
// We create a Worker as soon as our application starts
|
||||||
worker = createWorker()
|
worker = createWorker();
|
||||||
|
|
||||||
// This is an Array of RtpCapabilities
|
// This is an Array of RtpCapabilities
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
|
// https://mediasoup.org/documentation/v3/mediasoup/rtp-parameters-and-capabilities/#RtpCodecCapability
|
||||||
@ -92,24 +92,73 @@ 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',
|
||||||
clockRate: 48000,
|
clockRate : 48000,
|
||||||
channels: 2,
|
channels : 2
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
kind: 'video',
|
kind : 'video',
|
||||||
mimeType: 'video/VP8',
|
mimeType : 'video/VP8',
|
||||||
clockRate: 90000,
|
clockRate : 90000,
|
||||||
parameters: {
|
parameters :
|
||||||
'x-google-start-bitrate': 1000,
|
{
|
||||||
|
'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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// {
|
||||||
|
// kind: 'audio',
|
||||||
|
// mimeType: 'audio/opus',
|
||||||
|
// clockRate: 48000,
|
||||||
|
// channels: 2,
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// kind: 'video',
|
||||||
|
// mimeType: 'video/VP8',
|
||||||
|
// clockRate: 90000,
|
||||||
|
// parameters: {
|
||||||
|
// 'x-google-start-bitrate': 1000,
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
];
|
||||||
|
|
||||||
const closeCall = (callId) => {
|
const closeCall = (callId) => {
|
||||||
try {
|
try {
|
||||||
if (videoCalls[callId]) {
|
if (callId && videoCalls[callId]) {
|
||||||
videoCalls[callId].producer?.close();
|
videoCalls[callId].producer?.close();
|
||||||
videoCalls[callId].consumer?.close();
|
videoCalls[callId].consumer?.close();
|
||||||
videoCalls[callId]?.consumerTransport?.close();
|
videoCalls[callId]?.consumerTransport?.close();
|
||||||
@ -124,16 +173,6 @@ const closeCall = (callId) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getRtpCapabilities = (callId, callback) => {
|
|
||||||
try {
|
|
||||||
console.log('[getRtpCapabilities] callId', callId);
|
|
||||||
const rtpCapabilities = videoCalls[callId].router.rtpCapabilities;
|
|
||||||
callback({ rtpCapabilities });
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`ERROR | getRtpCapabilities | callId ${callId} | ${error.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
- Handlers for WS events
|
- Handlers for WS events
|
||||||
- These are created only when we have a connection with a peer
|
- These are created only when we have a connection with a peer
|
||||||
@ -160,7 +199,9 @@ peers.on('connection', async socket => {
|
|||||||
- If the room already exists, it will not create it, but will only return rtpCapabilities
|
- If the room already exists, it will not create it, but will only return rtpCapabilities
|
||||||
*/
|
*/
|
||||||
socket.on('createRoom', async ({ callId }, callback) => {
|
socket.on('createRoom', async ({ callId }, callback) => {
|
||||||
|
let callbackResponse = null;
|
||||||
try {
|
try {
|
||||||
|
// We can continue with the room creation process only if we have a callId
|
||||||
if (callId) {
|
if (callId) {
|
||||||
console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`);
|
console.log(`[createRoom] socket.id ${socket.id} callId ${callId}`);
|
||||||
if (!videoCalls[callId]) {
|
if (!videoCalls[callId]) {
|
||||||
@ -169,12 +210,19 @@ peers.on('connection', async socket => {
|
|||||||
console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
|
console.log(`[createRoom] Router ID: ${videoCalls[callId].router.id}`);
|
||||||
}
|
}
|
||||||
socketDetails[socket.id] = callId;
|
socketDetails[socket.id] = callId;
|
||||||
getRtpCapabilities(callId, callback);
|
|
||||||
|
// rtpCapabilities is set for callback
|
||||||
|
console.log('[getRtpCapabilities] callId', callId);
|
||||||
|
callbackResponse = {
|
||||||
|
rtpCapabilities :videoCalls[callId].router.rtpCapabilities
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
console.log(`[createRoom] missing callId ${callId}`);
|
console.log(`[createRoom] missing callId ${callId}`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`);
|
console.log(`ERROR | createRoom | callId ${callId} | ${error.message}`);
|
||||||
|
} finally {
|
||||||
|
callback(callbackResponse);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -195,16 +243,19 @@ peers.on('connection', async socket => {
|
|||||||
videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback);
|
videoCalls[callId].producerTransport = await createWebRtcTransportLayer(callId, callback);
|
||||||
} else {
|
} else {
|
||||||
console.log(`producerTransport has already been defined | callId ${callId}`);
|
console.log(`producerTransport has already been defined | callId ${callId}`);
|
||||||
|
callback(null);
|
||||||
}
|
}
|
||||||
} else if (!sender) {
|
} else if (!sender) {
|
||||||
if (!videoCalls[callId].consumerTransport) {
|
if (!videoCalls[callId].consumerTransport) {
|
||||||
videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback);
|
videoCalls[callId].consumerTransport = await createWebRtcTransportLayer(callId, callback);
|
||||||
} else {
|
} else {
|
||||||
console.log(`consumerTransport has already been defined | callId ${callId}`);
|
console.log(`consumerTransport has already been defined | callId ${callId}`);
|
||||||
|
callback(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | createWebRtcTransport | callId ${callId} | sender ${sender} | ${error.message}`);
|
console.log(`ERROR | createWebRtcTransport | callId ${socketDetails[socket.id]} | sender ${sender} | ${error.message}`);
|
||||||
|
callback(error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -215,7 +266,9 @@ peers.on('connection', async socket => {
|
|||||||
socket.on('transport-connect', async ({ dtlsParameters }) => {
|
socket.on('transport-connect', async ({ dtlsParameters }) => {
|
||||||
try {
|
try {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId}`)
|
if (typeof dtlsParameters === 'string') dtlsParameters = JSON.parse(dtlsParameters);
|
||||||
|
|
||||||
|
console.log(`[transport-connect] socket.id ${socket.id} | callId ${callId}`);
|
||||||
await videoCalls[callId].producerTransport.connect({ dtlsParameters });
|
await videoCalls[callId].producerTransport.connect({ dtlsParameters });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
console.log(`ERROR | transport-connect | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
@ -227,9 +280,11 @@ peers.on('connection', async socket => {
|
|||||||
- For the router with the id callId, we make produce on producerTransport
|
- For the router with the id callId, we make produce on producerTransport
|
||||||
- Create the handler on producer at the 'transportclose' event
|
- Create the handler on producer at the 'transportclose' event
|
||||||
*/
|
*/
|
||||||
socket.on('transport-produce', async ({ kind, rtpParameters, appData }) => {
|
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
|
||||||
try {
|
try {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
|
if (typeof rtpParameters === 'string') rtpParameters = JSON.parse(rtpParameters);
|
||||||
|
|
||||||
console.log('[transport-produce] | socket.id', socket.id, '| callId', callId);
|
console.log('[transport-produce] | socket.id', socket.id, '| callId', callId);
|
||||||
videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({
|
videoCalls[callId].producer = await videoCalls[callId].producerTransport.produce({
|
||||||
kind,
|
kind,
|
||||||
@ -242,6 +297,11 @@ peers.on('connection', async socket => {
|
|||||||
console.log('transport for this producer closed', callId)
|
console.log('transport for this producer closed', callId)
|
||||||
closeCall(callId);
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Send back to the client the Producer's id
|
||||||
|
callback && callback({
|
||||||
|
id: videoCalls[callId].producer.id
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
|
console.log(`ERROR | transport-produce | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
}
|
}
|
||||||
@ -290,14 +350,14 @@ peers.on('connection', async socket => {
|
|||||||
videoCalls[callId].consumer.on('transportclose', () => {
|
videoCalls[callId].consumer.on('transportclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('transport close from consumer', callId);
|
console.log('transport close from consumer', callId);
|
||||||
closeCall();
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#consumer-on-producerclose
|
||||||
videoCalls[callId].consumer.on('producerclose', () => {
|
videoCalls[callId].consumer.on('producerclose', () => {
|
||||||
const callId = socketDetails[socket.id];
|
const callId = socketDetails[socket.id];
|
||||||
console.log('producer of consumer closed', callId);
|
console.log('producer of consumer closed', callId);
|
||||||
closeCall();
|
closeCall(callId);
|
||||||
});
|
});
|
||||||
|
|
||||||
// From the consumer extract the following params to send back to the Client
|
// From the consumer extract the following params to send back to the Client
|
||||||
@ -312,9 +372,10 @@ peers.on('connection', async socket => {
|
|||||||
callback({ params });
|
callback({ params });
|
||||||
} else {
|
} else {
|
||||||
console.log(`[canConsume] Can't consume | callId ${callId}`);
|
console.log(`[canConsume] Can't consume | callId ${callId}`);
|
||||||
|
callback(null);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | consume | callId ${callId} | ${error.message}`)
|
console.log(`ERROR | consume | callId ${socketDetails[socket.id]} | ${error.message}`)
|
||||||
callback({ params: { error } });
|
callback({ params: { error } });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -388,7 +449,7 @@ const createWebRtcTransportLayer = async (callId, callback) => {
|
|||||||
return transport;
|
return transport;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(`ERROR | createWebRtcTransportLayer | callId ${callId} | ${error.message}`);
|
console.log(`ERROR | createWebRtcTransportLayer | callId ${socketDetails[socket.id]} | ${error.message}`);
|
||||||
callback({ params: { error } });
|
callback({ params: { error } });
|
||||||
}
|
}
|
||||||
}
|
}
|
48
build.sh
Executable file
48
build.sh
Executable 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 -
|
||||||
|
|
||||||
|
|
1012
public/bundle.js
1012
public/bundle.js
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,4 @@
|
|||||||
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',
|
||||||
// mediasoupAddress: 'http://localhost:3000/mediasoup',
|
|
||||||
}
|
}
|
116
public/index.js
116
public/index.js
@ -12,10 +12,14 @@ let callId = parseInt(urlParams.get('callId')) || null;
|
|||||||
const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false
|
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)
|
console.log('[URL] ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER)
|
||||||
|
|
||||||
let socket
|
console.log('🟩 config', config)
|
||||||
hub = io(config.hubAddress)
|
|
||||||
|
|
||||||
const connectToMediasoup = () => {
|
let socket, hub
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
hub = io(config.hubAddress)
|
||||||
|
|
||||||
|
const connectToMediasoup = () => {
|
||||||
|
|
||||||
socket = io(config.mediasoupAddress, {
|
socket = io(config.mediasoupAddress, {
|
||||||
reconnection: true,
|
reconnection: true,
|
||||||
@ -32,11 +36,11 @@ const connectToMediasoup = () => {
|
|||||||
}
|
}
|
||||||
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() }
|
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IS_PRODUCER === true) {
|
if (IS_PRODUCER === true) {
|
||||||
hub.on('connect', async () => {
|
hub.on('connect', async () => {
|
||||||
console.log(`[HUB] ${config.hubAddress} | connected: ${hub.connected}`)
|
console.log(`[HUB]! ${config.hubAddress} | connected: ${hub.connected}`)
|
||||||
connectToMediasoup()
|
connectToMediasoup()
|
||||||
|
|
||||||
hub.emit(
|
hub.emit(
|
||||||
@ -80,24 +84,34 @@ if (IS_PRODUCER === true) {
|
|||||||
hub.on('disconnect', () => {
|
hub.on('disconnect', () => {
|
||||||
console.log('disconnect')
|
console.log('disconnect')
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
connectToMediasoup()
|
connectToMediasoup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}, 1600);
|
||||||
|
|
||||||
|
|
||||||
let device
|
let device
|
||||||
let rtpCapabilities
|
let rtpCapabilities
|
||||||
let producerTransport
|
let producerTransport
|
||||||
let consumerTransport
|
let consumerTransport
|
||||||
let producer
|
let producerVideo
|
||||||
|
let producerAudio
|
||||||
let consumer
|
let consumer
|
||||||
let originAssetId
|
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/#ProducerOptions
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
|
||||||
let params = {
|
let videoParams = {
|
||||||
// mediasoup params
|
// encodings: [
|
||||||
|
// { scaleResolutionDownBy: 4, maxBitrate: 500000 },
|
||||||
|
// { scaleResolutionDownBy: 2, maxBitrate: 1000000 },
|
||||||
|
// { scaleResolutionDownBy: 1, maxBitrate: 5000000 },
|
||||||
|
// { scalabilityMode: 'S3T3_KEY' }
|
||||||
|
// ],
|
||||||
|
// codecOptions: {
|
||||||
|
// videoGoogleStartBitrate: 1000
|
||||||
|
// }
|
||||||
encodings: [
|
encodings: [
|
||||||
{
|
{
|
||||||
rid: 'r0',
|
rid: 'r0',
|
||||||
@ -121,19 +135,55 @@ let params = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let audioParams = {
|
||||||
|
codecOptions :
|
||||||
|
{
|
||||||
|
opusStereo : true,
|
||||||
|
opusDtx : true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const streamSuccess = (stream) => {
|
const streamSuccess = (stream) => {
|
||||||
|
// console.log('[streamSuccess] device', device);
|
||||||
|
// localVideo.srcObject = stream
|
||||||
|
// 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]');
|
console.log('[streamSuccess]');
|
||||||
localVideo.srcObject = stream
|
localVideo.srcObject = stream
|
||||||
const track = stream.getVideoTracks()[0]
|
const track = stream.getVideoTracks()[0]
|
||||||
params = {
|
videoParams = {
|
||||||
track,
|
track,
|
||||||
...params
|
...videoParams
|
||||||
}
|
}
|
||||||
goConnect()
|
goConnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
const getLocalStream = () => {
|
const getLocalStream = () => {
|
||||||
console.log('[getLocalStream]');
|
console.log('[getLocalStream]');
|
||||||
|
// navigator.mediaDevices.getUserMedia({
|
||||||
|
// audio: false,
|
||||||
|
// video: {
|
||||||
|
// qvga : { width: { ideal: 320 }, height: { ideal: 240 } },
|
||||||
|
// vga : { width: { ideal: 640 }, height: { ideal: 480 } },
|
||||||
|
// hd : { width: { ideal: 1280 }, height: { ideal: 720 } }
|
||||||
|
// }
|
||||||
|
// })
|
||||||
navigator.mediaDevices.getUserMedia({
|
navigator.mediaDevices.getUserMedia({
|
||||||
audio: false,
|
audio: false,
|
||||||
video: {
|
video: {
|
||||||
@ -167,7 +217,7 @@ const goCreateTransport = () => {
|
|||||||
// server side to send/recive media
|
// server side to send/recive media
|
||||||
const createDevice = async () => {
|
const createDevice = async () => {
|
||||||
try {
|
try {
|
||||||
console.log('[createDevice]');
|
console.log('[createDevice] 1 device', device);
|
||||||
device = new mediasoupClient.Device()
|
device = new mediasoupClient.Device()
|
||||||
|
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#device-load
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#device-load
|
||||||
@ -178,6 +228,7 @@ const createDevice = async () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
console.log('Device RTP Capabilities', device.rtpCapabilities)
|
console.log('Device RTP Capabilities', device.rtpCapabilities)
|
||||||
|
console.log('[createDevice] 2 device', device);
|
||||||
|
|
||||||
// once the device loads, create transport
|
// once the device loads, create transport
|
||||||
goCreateTransport()
|
goCreateTransport()
|
||||||
@ -207,6 +258,7 @@ const getRtpCapabilities = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const createSendTransport = () => {
|
const createSendTransport = () => {
|
||||||
|
console.log('[createSendTransport');
|
||||||
// see server's socket.on('createWebRtcTransport', sender?, ...)
|
// see server's socket.on('createWebRtcTransport', sender?, ...)
|
||||||
// this is a call from Producer, so sender = true
|
// this is a call from Producer, so sender = true
|
||||||
socket.emit('createWebRtcTransport', { sender: true, callId }, ({ params }) => {
|
socket.emit('createWebRtcTransport', { sender: true, callId }, ({ params }) => {
|
||||||
@ -217,7 +269,7 @@ const createSendTransport = () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(params)
|
console.log('[createWebRtcTransport] params', params)
|
||||||
|
|
||||||
// creates a new WebRTC Transport to send media
|
// creates a new WebRTC Transport to send media
|
||||||
// based on the server's producer transport params
|
// based on the server's producer transport params
|
||||||
@ -244,7 +296,7 @@ const createSendTransport = () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
producerTransport.on('produce', async (parameters, callback, errback) => {
|
producerTransport.on('produce', async (parameters, callback, errback) => {
|
||||||
console.log(parameters)
|
console.log('[produce] parameters', parameters)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// tell the server to create a Producer
|
// tell the server to create a Producer
|
||||||
@ -270,22 +322,37 @@ const createSendTransport = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const connectSendTransport = async () => {
|
const connectSendTransport = async () => {
|
||||||
|
|
||||||
|
console.log('[connectSendTransport] producerTransport');
|
||||||
|
|
||||||
// we now call produce() to instruct the producer transport
|
// we now call produce() to instruct the producer transport
|
||||||
// to send media to the Router
|
// to send media to the Router
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
|
||||||
// this action will trigger the 'connect' and 'produce' events above
|
// this action will trigger the 'connect' and 'produce' events above
|
||||||
producer = await producerTransport.produce(params)
|
producerVideo = await producerTransport.produce(videoParams)
|
||||||
|
console.log('producerVideo', producerVideo);
|
||||||
producer.on('trackended', () => {
|
producerVideo.on('trackended', () => {
|
||||||
console.log('track ended')
|
console.log('track ended')
|
||||||
// close video track
|
// close video track
|
||||||
})
|
})
|
||||||
|
|
||||||
producer.on('transportclose', () => {
|
producerVideo.on('transportclose', () => {
|
||||||
console.log('transport ended')
|
console.log('transport ended')
|
||||||
// close video track
|
// 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 = {
|
const answer = {
|
||||||
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')),
|
||||||
@ -320,7 +387,7 @@ const createRecvTransport = async () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(params)
|
console.log('[createRecvTransport] params', params)
|
||||||
|
|
||||||
// creates a new WebRTC Transport to receive media
|
// creates a new WebRTC Transport to receive media
|
||||||
// based on server's consumer transport params
|
// based on server's consumer transport params
|
||||||
@ -353,7 +420,8 @@ const resetCallSettings = () => {
|
|||||||
localVideo.srcObject = null
|
localVideo.srcObject = null
|
||||||
remoteVideo.srcObject = null
|
remoteVideo.srcObject = null
|
||||||
consumer = null
|
consumer = null
|
||||||
producer = null
|
producerVideo = null
|
||||||
|
producerAudio = null
|
||||||
producerTransport = null
|
producerTransport = null
|
||||||
consumerTransport = null
|
consumerTransport = null
|
||||||
device = undefined
|
device = undefined
|
||||||
|
Reference in New Issue
Block a user