first commit
This commit is contained in:
commit
bb24a296ec
8
.prettierrc
Normal file
8
.prettierrc
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"singleQuote": true,
|
||||||
|
"semi": true,
|
||||||
|
"jsxSingleQuote": true,
|
||||||
|
"printWidth": 120,
|
||||||
|
"bracketSpacing": true,
|
||||||
|
"bracketSameLine": false
|
||||||
|
}
|
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Generate keys command:
|
||||||
|
openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout key.pem -out cert.pem
|
278
app.js
Normal file
278
app.js
Normal file
@ -0,0 +1,278 @@
|
|||||||
|
import express from 'express'
|
||||||
|
const app = express()
|
||||||
|
|
||||||
|
import https from 'httpolyglot'
|
||||||
|
import fs from 'fs'
|
||||||
|
import path from 'path'
|
||||||
|
const __dirname = path.resolve()
|
||||||
|
|
||||||
|
// import { Server } from 'socket.io'
|
||||||
|
import Server from 'socket.io'
|
||||||
|
import mediasoup from 'mediasoup'
|
||||||
|
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
res.send('Hello from mediasoup app!')
|
||||||
|
})
|
||||||
|
|
||||||
|
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')
|
||||||
|
}
|
||||||
|
|
||||||
|
const httpsServer = https.createServer(options, app)
|
||||||
|
httpsServer.listen(3000, () => {
|
||||||
|
console.log('listening on port: ' + 3000)
|
||||||
|
})
|
||||||
|
|
||||||
|
// const io = new Server(httpsServer)
|
||||||
|
const io = new Server(httpsServer)
|
||||||
|
|
||||||
|
const peers = io.of('/mediasoup')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Worker
|
||||||
|
* |-> Router(s)
|
||||||
|
* |-> Producer Transport(s)
|
||||||
|
* |-> Producer
|
||||||
|
* |-> Consumer Transport(s)
|
||||||
|
* |-> Consumer
|
||||||
|
**/
|
||||||
|
let worker
|
||||||
|
let routers = {}
|
||||||
|
let producerTransport
|
||||||
|
let consumerTransport
|
||||||
|
let producer
|
||||||
|
let consumer
|
||||||
|
|
||||||
|
const createWorker = async () => {
|
||||||
|
worker = await mediasoup.createWorker({
|
||||||
|
rtcMinPort: 2000,
|
||||||
|
rtcMaxPort: 2020,
|
||||||
|
})
|
||||||
|
console.log(`worker pid ${worker.pid}`)
|
||||||
|
|
||||||
|
worker.on('died', error => {
|
||||||
|
// This implies something serious happened, so kill the application
|
||||||
|
console.error('mediasoup worker has died')
|
||||||
|
setTimeout(() => process.exit(1), 2000) // exit in 2 seconds
|
||||||
|
})
|
||||||
|
|
||||||
|
return worker
|
||||||
|
}
|
||||||
|
|
||||||
|
// We create a Worker as soon as our application starts
|
||||||
|
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 = [
|
||||||
|
{
|
||||||
|
kind: 'audio',
|
||||||
|
mimeType: 'audio/opus',
|
||||||
|
clockRate: 48000,
|
||||||
|
channels: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: 'video',
|
||||||
|
mimeType: 'video/VP8',
|
||||||
|
clockRate: 90000,
|
||||||
|
parameters: {
|
||||||
|
'x-google-start-bitrate': 1000,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
peers.on('connection', async socket => {
|
||||||
|
|
||||||
|
console.log('[connection]');
|
||||||
|
|
||||||
|
socket.emit('connection-success', {
|
||||||
|
socketId: socket.id
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('disconnect', () => {
|
||||||
|
// do some cleanup
|
||||||
|
console.log('[disconnect]')
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('create-router', async (data) => {
|
||||||
|
const routerId = data.assetId;
|
||||||
|
|
||||||
|
console.log('[create-router]', routerId)
|
||||||
|
|
||||||
|
routers[routerId] = await worker.createRouter({ mediaCodecs })
|
||||||
|
// console.log('🔴 Create routers', routers);
|
||||||
|
|
||||||
|
// for (const key in routers) {
|
||||||
|
// if (Object.hasOwnProperty.call(routers, key)) {
|
||||||
|
// const element = routers[key];
|
||||||
|
// console.log('🔴', key, element);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Client emits a request for RTP Capabilities
|
||||||
|
// This event responds to the request
|
||||||
|
socket.on('getRtpCapabilities', (callback) => {
|
||||||
|
console.log('[getRtpCapabilities]')
|
||||||
|
|
||||||
|
const rtpCapabilities = routers[routerId].rtpCapabilities
|
||||||
|
|
||||||
|
// call callback from the client and send back the rtpCapabilities
|
||||||
|
callback({ rtpCapabilities })
|
||||||
|
})
|
||||||
|
|
||||||
|
// Client emits a request to create server side Transport
|
||||||
|
// We need to differentiate between the producer and consumer transports
|
||||||
|
socket.on('createWebRtcTransport', async ({ sender }, callback) => {
|
||||||
|
console.log(`Is this a sender request? ${sender}`)
|
||||||
|
// The client indicates if it is a producer or a consumer
|
||||||
|
// if sender is true, indicates a producer else a consumer
|
||||||
|
if (sender)
|
||||||
|
producerTransport = await createWebRtcTransport(routerId, callback)
|
||||||
|
else
|
||||||
|
consumerTransport = await createWebRtcTransport(routerId, callback)
|
||||||
|
})
|
||||||
|
|
||||||
|
// see client's socket.emit('transport-connect', ...)
|
||||||
|
socket.on('transport-connect', async ({ dtlsParameters }) => {
|
||||||
|
console.log('DTLS PARAMS... ', { dtlsParameters })
|
||||||
|
await producerTransport.connect({ dtlsParameters })
|
||||||
|
})
|
||||||
|
|
||||||
|
// see client's socket.emit('transport-produce', ...)
|
||||||
|
socket.on('transport-produce', async ({ kind, rtpParameters, appData }, callback) => {
|
||||||
|
// call produce based on the prameters from the client
|
||||||
|
producer = await producerTransport.produce({
|
||||||
|
kind,
|
||||||
|
rtpParameters,
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Producer ID:', producer.id, producer.kind)
|
||||||
|
|
||||||
|
producer.on('transportclose', () => {
|
||||||
|
console.log('transport for this producer closed ')
|
||||||
|
producer.close()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Send back to the client the Producer's id
|
||||||
|
callback({
|
||||||
|
id: producer.id
|
||||||
|
})
|
||||||
|
})
|
||||||
|
// see client's socket.emit('transport-recv-connect', ...)
|
||||||
|
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
|
||||||
|
console.log(`DTLS PARAMS: ${dtlsParameters}`)
|
||||||
|
await consumerTransport.connect({ dtlsParameters })
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('consume', async ({ rtpCapabilities }, callback) => {
|
||||||
|
try {
|
||||||
|
console.log('[consume] Producer ID:', producer.id, producer.kind)
|
||||||
|
// check if the routers can consume the specified producer
|
||||||
|
if (routers[routerId].canConsume({
|
||||||
|
producerId: producer.id,
|
||||||
|
rtpCapabilities
|
||||||
|
})) {
|
||||||
|
// transport can now consume and return a consumer
|
||||||
|
consumer = await consumerTransport.consume({
|
||||||
|
producerId: producer.id,
|
||||||
|
rtpCapabilities,
|
||||||
|
paused: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
consumer.on('transportclose', () => {
|
||||||
|
console.log('transport close from consumer')
|
||||||
|
})
|
||||||
|
|
||||||
|
consumer.on('producerclose', () => {
|
||||||
|
console.log('producer of consumer closed')
|
||||||
|
})
|
||||||
|
|
||||||
|
// from the consumer extract the following params
|
||||||
|
// to send back to the Client
|
||||||
|
const params = {
|
||||||
|
id: consumer.id,
|
||||||
|
producerId: producer.id,
|
||||||
|
kind: consumer.kind,
|
||||||
|
rtpParameters: consumer.rtpParameters,
|
||||||
|
}
|
||||||
|
// send the parameters to the client
|
||||||
|
callback({ params })
|
||||||
|
} else {
|
||||||
|
console.log("Can't consume!!!", routers[routerId].canConsume());
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error.message)
|
||||||
|
callback({
|
||||||
|
params: {
|
||||||
|
error: error
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.on('consumer-resume', async () => {
|
||||||
|
console.log('consumer resume')
|
||||||
|
await consumer.resume()
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
const createWebRtcTransport = async (routerId, callback) => {
|
||||||
|
try {
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
|
||||||
|
const webRtcTransport_options = {
|
||||||
|
listenIps: [
|
||||||
|
{
|
||||||
|
ip: '0.0.0.0', // replace with relevant IP address
|
||||||
|
announcedIp: '127.0.0.1',
|
||||||
|
}
|
||||||
|
],
|
||||||
|
enableUdp: true,
|
||||||
|
enableTcp: true,
|
||||||
|
preferUdp: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#routers-createWebRtcTransport
|
||||||
|
let transport = await routers[routerId].createWebRtcTransport(webRtcTransport_options)
|
||||||
|
console.log(`transport id: ${transport.id}`)
|
||||||
|
|
||||||
|
transport.on('dtlsstatechange', dtlsState => {
|
||||||
|
if (dtlsState === 'closed') {
|
||||||
|
transport.close()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
transport.on('close', () => {
|
||||||
|
console.log('transport closed')
|
||||||
|
})
|
||||||
|
|
||||||
|
// send back to the client the following prameters
|
||||||
|
callback({
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#TransportOptions
|
||||||
|
params: {
|
||||||
|
id: transport.id,
|
||||||
|
iceParameters: transport.iceParameters,
|
||||||
|
iceCandidates: transport.iceCandidates,
|
||||||
|
dtlsParameters: transport.dtlsParameters,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return transport
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
callback({
|
||||||
|
params: {
|
||||||
|
error: error
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
6304
package-lock.json
generated
Normal file
6304
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
package.json
Normal file
27
package.json
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"name": "mediasoup",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "app.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"start": "nodemon app.js",
|
||||||
|
"watch": "watchify public/index.js -o public/bundle.js -v"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"type": "module",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.18.1",
|
||||||
|
"httpolyglot": "^0.1.2",
|
||||||
|
"mediasoup": "^3.10.4",
|
||||||
|
"mediasoup-client": "^3.6.54",
|
||||||
|
"socket.io": "^2.0.3",
|
||||||
|
"socket.io-client": "^2.0.3"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^2.0.19",
|
||||||
|
"watchify": "^4.0.0"
|
||||||
|
}
|
||||||
|
}
|
20963
public/bundle.js
Normal file
20963
public/bundle.js
Normal file
File diff suppressed because one or more lines are too long
88
public/index.html
Normal file
88
public/index.html
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style>
|
||||||
|
tr {
|
||||||
|
vertical-align: top;
|
||||||
|
}
|
||||||
|
|
||||||
|
.video {
|
||||||
|
width: 360px;
|
||||||
|
background-color: black;
|
||||||
|
margin: 2px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
margin: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
#sharedBtns {
|
||||||
|
padding: 5;
|
||||||
|
background-color: papayawhip;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<body>
|
||||||
|
<label for="assetId">My asset id:</label><br>
|
||||||
|
<input type="number" id="assetId" name="assetId"><br>
|
||||||
|
<div id="video">
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<th>Local Video</th>
|
||||||
|
<th>Remote Video</th>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<div id="sharedBtns">
|
||||||
|
<video id="localVideo" autoplay class="video" ></video>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div id="sharedBtns">
|
||||||
|
<video id="remoteVideo" autoplay class="video" ></video>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<div id="sharedBtns">
|
||||||
|
<button id="btnLocalVideo">1. Get Local Video</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td colspan="2">
|
||||||
|
<div id="sharedBtns">
|
||||||
|
<button id="btnRtpCapabilities">2. Get Rtp Capabilities</button>
|
||||||
|
<br />
|
||||||
|
<button id="btnDevice">3. Create Device</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<div id="sharedBtns">
|
||||||
|
<button id="btnCreateSendTransport">4. Create Send Transport</button>
|
||||||
|
<br />
|
||||||
|
<button id="btnConnectSendTransport">5. Connect Send Transport & Produce</button></td>
|
||||||
|
</div>
|
||||||
|
<td>
|
||||||
|
<div id="sharedBtns">
|
||||||
|
<button id="btnRecvSendTransport">6. Create Recv Transport</button>
|
||||||
|
<br />
|
||||||
|
<button id="btnConnectRecvTransport">7. Connect Recv Transport & Consume</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</body>
|
||||||
|
<footer>
|
||||||
|
<script src="bundle.js"></script>
|
||||||
|
</footer>
|
||||||
|
</html>
|
307
public/index.js
Normal file
307
public/index.js
Normal file
@ -0,0 +1,307 @@
|
|||||||
|
const io = require("socket.io-client");
|
||||||
|
const mediasoupClient = require('mediasoup-client')
|
||||||
|
|
||||||
|
const ASSET_ID = 71
|
||||||
|
const ACCOUNT_ID = 1
|
||||||
|
|
||||||
|
let hub = io('https://hub.dev.linx.safemobile.com/', {
|
||||||
|
reconnect: false,
|
||||||
|
transports: ['websocket'],
|
||||||
|
})
|
||||||
|
|
||||||
|
hub.on('connect', async () => {
|
||||||
|
console.log('hub.connected', hub.connected);
|
||||||
|
hub.emit(
|
||||||
|
'ars',
|
||||||
|
JSON.stringify({
|
||||||
|
ars: true,
|
||||||
|
asset_id: ASSET_ID,
|
||||||
|
account_id: ACCOUNT_ID,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
hub.on("connect_error", (error) => {
|
||||||
|
console.log('connect_error', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
hub.on('connection', async socket => {
|
||||||
|
console.log('connection')
|
||||||
|
})
|
||||||
|
|
||||||
|
hub.on('disconnect', async socket => {
|
||||||
|
console.log('disconnect')
|
||||||
|
})
|
||||||
|
|
||||||
|
// ------------------------------------------------
|
||||||
|
|
||||||
|
const mediasoup = io('/mediasoup')
|
||||||
|
|
||||||
|
mediasoup.on('connection-success', ({ socketId }) => {
|
||||||
|
console.log('mediasoup.connected', mediasoup.connected)
|
||||||
|
|
||||||
|
mediasoup.emit('create-router', {
|
||||||
|
assetId: ASSET_ID
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
let device
|
||||||
|
let rtpCapabilities
|
||||||
|
let producerTransport
|
||||||
|
let consumerTransport
|
||||||
|
let producer
|
||||||
|
let consumer
|
||||||
|
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#ProducerOptions
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#transport-produce
|
||||||
|
let params = {
|
||||||
|
// mediasoup params
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const streamSuccess = async (stream) => {
|
||||||
|
localVideo.srcObject = stream
|
||||||
|
const track = stream.getVideoTracks()[0]
|
||||||
|
params = {
|
||||||
|
track,
|
||||||
|
...params
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getLocalStream = () => {
|
||||||
|
|
||||||
|
navigator.getUserMedia({
|
||||||
|
audio: false,
|
||||||
|
video: {
|
||||||
|
width: {
|
||||||
|
min: 640,
|
||||||
|
max: 1920,
|
||||||
|
},
|
||||||
|
height: {
|
||||||
|
min: 400,
|
||||||
|
max: 1080,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, streamSuccess, error => {
|
||||||
|
console.log(error.message)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// A device is an endpoint connecting to a Router on the
|
||||||
|
// server side to send/recive media
|
||||||
|
const createDevice = async () => {
|
||||||
|
try {
|
||||||
|
device = new mediasoupClient.Device()
|
||||||
|
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#device-load
|
||||||
|
// Loads the device with RTP capabilities of the Router (server side)
|
||||||
|
await device.load({
|
||||||
|
// see getRtpCapabilities() below
|
||||||
|
routerRtpCapabilities: rtpCapabilities
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('RTP Capabilities', device.rtpCapabilities)
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
if (error.name === 'UnsupportedError')
|
||||||
|
console.warn('browser not supported')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getRtpCapabilities = () => {
|
||||||
|
// make a request to the server for Router RTP Capabilities
|
||||||
|
// see server's socket.on('getRtpCapabilities', ...)
|
||||||
|
// the server sends back data object which contains rtpCapabilities
|
||||||
|
mediasoup.emit('getRtpCapabilities', (data) => {
|
||||||
|
console.log(`Router RTP Capabilities... ${data.rtpCapabilities}`)
|
||||||
|
|
||||||
|
// we assign to local variable and will be used when
|
||||||
|
// loading the client Device (see createDevice above)
|
||||||
|
rtpCapabilities = data.rtpCapabilities
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const createSendTransport = () => {
|
||||||
|
// see server's socket.on('createWebRtcTransport', sender?, ...)
|
||||||
|
// this is a call from Producer, so sender = true
|
||||||
|
mediasoup.emit('createWebRtcTransport', { sender: true }, ({ params }) => {
|
||||||
|
// The server sends back params needed
|
||||||
|
// to create Send Transport on the client side
|
||||||
|
if (params.error) {
|
||||||
|
console.log(params.error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(params)
|
||||||
|
|
||||||
|
// creates a new WebRTC Transport to send media
|
||||||
|
// based on the server's producer transport params
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#TransportOptions
|
||||||
|
producerTransport = device.createSendTransport(params)
|
||||||
|
|
||||||
|
// https://mediasoup.org/documentation/v3/communication-between-client-and-server/#producing-media
|
||||||
|
// this event is raised when a first call to transport.produce() is made
|
||||||
|
// see connectSendTransport() below
|
||||||
|
producerTransport.on('connect', async ({ dtlsParameters }, callback, errback) => {
|
||||||
|
try {
|
||||||
|
// Signal local DTLS parameters to the server side transport
|
||||||
|
// see server's socket.on('transport-connect', ...)
|
||||||
|
await mediasoup.emit('transport-connect', {
|
||||||
|
dtlsParameters,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Tell the transport that parameters were transmitted.
|
||||||
|
callback()
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
errback(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
producerTransport.on('produce', async (parameters, callback, errback) => {
|
||||||
|
console.log(parameters)
|
||||||
|
|
||||||
|
try {
|
||||||
|
// tell the server to create a Producer
|
||||||
|
// with the following parameters and produce
|
||||||
|
// and expect back a server side producer id
|
||||||
|
// see server's socket.on('transport-produce', ...)
|
||||||
|
await mediasoup.emit('transport-produce', {
|
||||||
|
kind: parameters.kind,
|
||||||
|
rtpParameters: parameters.rtpParameters,
|
||||||
|
appData: parameters.appData,
|
||||||
|
}, ({ id }) => {
|
||||||
|
// Tell the transport that parameters were transmitted and provide it with the
|
||||||
|
// server side producer's id.
|
||||||
|
callback({ id })
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
errback(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const connectSendTransport = async () => {
|
||||||
|
// 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', () => {
|
||||||
|
console.log('track ended')
|
||||||
|
|
||||||
|
// close video track
|
||||||
|
})
|
||||||
|
|
||||||
|
producer.on('transportclose', () => {
|
||||||
|
console.log('transport ended')
|
||||||
|
|
||||||
|
// close video track
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const createRecvTransport = async () => {
|
||||||
|
// see server's socket.on('consume', sender?, ...)
|
||||||
|
// this is a call from Consumer, so sender = false
|
||||||
|
await mediasoup.emit('createWebRtcTransport', { sender: false }, ({ params }) => {
|
||||||
|
// The server sends back params needed
|
||||||
|
// to create Send Transport on the client side
|
||||||
|
if (params.error) {
|
||||||
|
console.log(params.error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(params)
|
||||||
|
|
||||||
|
// creates a new WebRTC Transport to receive media
|
||||||
|
// based on server's consumer transport params
|
||||||
|
// https://mediasoup.org/documentation/v3/mediasoup-client/api/#device-createRecvTransport
|
||||||
|
consumerTransport = device.createRecvTransport(params)
|
||||||
|
|
||||||
|
// https://mediasoup.org/documentation/v3/communication-between-client-and-server/#producing-media
|
||||||
|
// this event is raised when a first call to transport.produce() is made
|
||||||
|
// see connectRecvTransport() below
|
||||||
|
consumerTransport.on('connect', async ({ dtlsParameters }, callback, errback) => {
|
||||||
|
try {
|
||||||
|
// Signal local DTLS parameters to the server side transport
|
||||||
|
// see server's socket.on('transport-recv-connect', ...)
|
||||||
|
await mediasoup.emit('transport-recv-connect', {
|
||||||
|
dtlsParameters,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Tell the transport that parameters were transmitted.
|
||||||
|
callback()
|
||||||
|
} catch (error) {
|
||||||
|
// Tell the transport that something was wrong
|
||||||
|
errback(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const connectRecvTransport = async () => {
|
||||||
|
console.log('connectRecvTransport');
|
||||||
|
// for consumer, we need to tell the server first
|
||||||
|
// to create a consumer based on the rtpCapabilities and consume
|
||||||
|
// if the router can consume, it will send back a set of params as below
|
||||||
|
await mediasoup.emit('consume', {
|
||||||
|
rtpCapabilities: device.rtpCapabilities,
|
||||||
|
}, async ({ params }) => {
|
||||||
|
if (params.error) {
|
||||||
|
console.log('Cannot Consume')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('params', params)
|
||||||
|
// then consume with the local consumer transport
|
||||||
|
// which creates a consumer
|
||||||
|
consumer = await consumerTransport.consume({
|
||||||
|
id: params.id,
|
||||||
|
producerId: params.producerId,
|
||||||
|
kind: params.kind,
|
||||||
|
rtpParameters: params.rtpParameters
|
||||||
|
})
|
||||||
|
|
||||||
|
// destructure and retrieve the video track from the producer
|
||||||
|
const { track } = consumer
|
||||||
|
|
||||||
|
remoteVideo.srcObject = new MediaStream([track])
|
||||||
|
|
||||||
|
// the server consumer started with media paused
|
||||||
|
// so we need to inform the server to resume
|
||||||
|
console.log('consumer-resume');
|
||||||
|
mediasoup.emit('consumer-resume')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
btnLocalVideo.addEventListener('click', getLocalStream)
|
||||||
|
btnRtpCapabilities.addEventListener('click', getRtpCapabilities)
|
||||||
|
btnDevice.addEventListener('click', createDevice)
|
||||||
|
btnCreateSendTransport.addEventListener('click', createSendTransport)
|
||||||
|
btnConnectSendTransport.addEventListener('click', connectSendTransport)
|
||||||
|
btnRecvSendTransport.addEventListener('click', createRecvTransport)
|
||||||
|
btnConnectRecvTransport.addEventListener('click', connectRecvTransport)
|
24
server/ssl/cert.pem
Normal file
24
server/ssl/cert.pem
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIEDzCCAvegAwIBAgIUaoTnmT3ltr9S5ysxQIXoLOHM6IYwDQYJKoZIhvcNAQEL
|
||||||
|
BQAwgZYxCzAJBgNVBAYTAlJPMQ0wCwYDVQQIDARBTEJBMRMwEQYDVQQHDApBTEJB
|
||||||
|
IElVTElBMRMwEQYDVQQKDApTQUZFTU9CSUxFMRIwEAY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W/jMYxehHJ9jxB7AVoq+ice8kyhipFYYOPRm9WEouF+BvFyQD1bhrD1
|
||||||
|
b5kJMKOTcTuqqlRWgECrIZT/oumgujkAT6xulPu+jSbV3XyEp4rJq4E6lks1y8RG
|
||||||
|
4hdllhfLtmE06hqBgtSVxHg4y0PgKkn0rU+2V7ZaPDc5IViGKbFo/lZI1iW3nD9Y
|
||||||
|
UmATM2co6zImrpt/bfBByfwpdZevpEKzSilbQ06JcGEtUm90U1Qzi0YLSv40aUYD
|
||||||
|
jSYmcpuMoI4vLSIFpPYRAIRbvEwmdwiwCd0CcPWAY98CzhQ=
|
||||||
|
-----END CERTIFICATE-----
|
28
server/ssl/key.pem
Normal file
28
server/ssl/key.pem
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
-----BEGIN PRIVATE KEY-----
|
||||||
|
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC/igic1Y14qYpl
|
||||||
|
XMGDsHpsEkEbBf+KpFpYmSQXYLOn6AnfJRRtiVKjKxz67oTiQN1Hv+V8Nx14vLN+
|
||||||
|
FxA1cUamRDgvSkmYNeWf/XbRfNz60oUdE7JakviSH3DRwQ68mIoc7jKECy7nKgVc
|
||||||
|
xXY/kSkU7aK/FnrDSC2uIrxp6+eU6tuqgFQL+glerchkaR0nwDgnY6/VYix0LDbx
|
||||||
|
XfjQXAiGwAQFsl38xi5eIhHXLu82cUxNkPjEVEUzyTSHAy669kC+1Rf+726e4ril
|
||||||
|
BLD4v4l8mbcjE5I4MkAPzkmNSV1lUlrSGEx7pm8dcUP40dwgsEz8Vy+WjLk+n6uH
|
||||||
|
guxmYaHRAgMBAAECggEAC3GIRTJ/EsvyUE8D4sXK8qT+jcpEc7iRC0UPAnSNF0WG
|
||||||
|
PXY+K7MuJECBqT5R4yCj99Lvt4WldZ60jJ59IexEH8/omW6zSexCbVYb4LU4kKW+
|
||||||
|
PTf9imrIfhZxy55groTgj4ztqLTwVvBMRLgpce/OyAjQleE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EOJZOZ6KDTMcTyh6uqIYQiu9eENMtZjWfA0Q
|
||||||
|
l0p3yZjLMHh5lTVhbJTsMfp1Etut9yVZPG+7P9+XevTac/t2CNAs+xrXES/b5M8Y
|
||||||
|
Eq7JQiVhWZZ4CGNRcQb+y34=
|
||||||
|
-----END PRIVATE KEY-----
|
Loading…
Reference in New Issue
Block a user