Added rooms and connect to existing publisher
This commit is contained in:
parent
bb24a296ec
commit
5c88cd966d
104
app.js
104
app.js
@ -1,3 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* integrating mediasoup server with a node.js application
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Please follow mediasoup installation requirements */
|
||||||
|
/* https://mediasoup.org/documentation/v3/mediasoup/installation/ */
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
@ -6,9 +12,15 @@ import fs from 'fs'
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
const __dirname = path.resolve()
|
const __dirname = path.resolve()
|
||||||
|
|
||||||
// import { Server } from 'socket.io'
|
|
||||||
import Server from 'socket.io'
|
import Server from 'socket.io'
|
||||||
import mediasoup from 'mediasoup'
|
import mediasoup, { getSupportedRtpCapabilities } from 'mediasoup'
|
||||||
|
|
||||||
|
let worker
|
||||||
|
let router = {}
|
||||||
|
let producerTransport
|
||||||
|
let consumerTransport
|
||||||
|
let producer
|
||||||
|
let consumer
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.send('Hello from mediasoup app!')
|
res.send('Hello from mediasoup app!')
|
||||||
@ -27,9 +39,9 @@ httpsServer.listen(3000, () => {
|
|||||||
console.log('listening on port: ' + 3000)
|
console.log('listening on port: ' + 3000)
|
||||||
})
|
})
|
||||||
|
|
||||||
// const io = new Server(httpsServer)
|
|
||||||
const io = new Server(httpsServer)
|
const io = new Server(httpsServer)
|
||||||
|
|
||||||
|
// socket.io namespace (could represent a room?)
|
||||||
const peers = io.of('/mediasoup')
|
const peers = io.of('/mediasoup')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,12 +52,6 @@ const peers = io.of('/mediasoup')
|
|||||||
* |-> Consumer Transport(s)
|
* |-> Consumer Transport(s)
|
||||||
* |-> Consumer
|
* |-> Consumer
|
||||||
**/
|
**/
|
||||||
let worker
|
|
||||||
let routers = {}
|
|
||||||
let producerTransport
|
|
||||||
let consumerTransport
|
|
||||||
let producer
|
|
||||||
let consumer
|
|
||||||
|
|
||||||
const createWorker = async () => {
|
const createWorker = async () => {
|
||||||
worker = await mediasoup.createWorker({
|
worker = await mediasoup.createWorker({
|
||||||
@ -88,54 +94,47 @@ const mediaCodecs = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
peers.on('connection', async socket => {
|
peers.on('connection', async socket => {
|
||||||
|
console.log(socket.id)
|
||||||
console.log('[connection]');
|
|
||||||
|
|
||||||
socket.emit('connection-success', {
|
socket.emit('connection-success', {
|
||||||
socketId: socket.id
|
socketId: socket.id,
|
||||||
|
existsProducer: producer ? true : false,
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on('disconnect', () => {
|
socket.on('disconnect', () => {
|
||||||
// do some cleanup
|
// do some cleanup
|
||||||
console.log('[disconnect]')
|
console.log('peer disconnected')
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on('create-router', async (data) => {
|
socket.on('createRoom', async ({ callId }, callback) => {
|
||||||
const routerId = data.assetId;
|
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(`Router ID: ${router[callId].id}`)
|
||||||
|
}
|
||||||
|
|
||||||
console.log('[create-router]', routerId)
|
getRtpCapabilities(callId, callback)
|
||||||
|
})
|
||||||
|
|
||||||
routers[routerId] = await worker.createRouter({ mediaCodecs })
|
const getRtpCapabilities = (callId, callback) => {
|
||||||
// console.log('🔴 Create routers', routers);
|
const rtpCapabilities = router[callId].rtpCapabilities
|
||||||
|
|
||||||
// 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 })
|
callback({ rtpCapabilities })
|
||||||
})
|
}
|
||||||
|
|
||||||
// Client emits a request to create server side Transport
|
// Client emits a request to create server side Transport
|
||||||
// We need to differentiate between the producer and consumer transports
|
// We need to differentiate between the producer and consumer transports
|
||||||
socket.on('createWebRtcTransport', async ({ sender }, callback) => {
|
socket.on('createWebRtcTransport', async ({ sender, callId }, callback) => {
|
||||||
console.log(`Is this a sender request? ${sender}`)
|
console.log(`Is this a sender request? ${sender} | callId ${callId}`)
|
||||||
// The client indicates if it is a producer or a consumer
|
// The client indicates if it is a producer or a consumer
|
||||||
// if sender is true, indicates a producer else a consumer
|
// if sender is true, indicates a producer else a consumer
|
||||||
if (sender)
|
if (sender)
|
||||||
producerTransport = await createWebRtcTransport(routerId, callback)
|
producerTransport = await createWebRtcTransportLayer(callId, callback)
|
||||||
else
|
else
|
||||||
consumerTransport = await createWebRtcTransport(routerId, callback)
|
consumerTransport = await createWebRtcTransportLayer(callId, callback)
|
||||||
})
|
})
|
||||||
|
|
||||||
// see client's socket.emit('transport-connect', ...)
|
// see client's socket.emit('transport-connect', ...)
|
||||||
@ -152,7 +151,7 @@ peers.on('connection', async socket => {
|
|||||||
rtpParameters,
|
rtpParameters,
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('Producer ID:', producer.id, producer.kind)
|
console.log('Producer ID: ', producer.id, producer.kind)
|
||||||
|
|
||||||
producer.on('transportclose', () => {
|
producer.on('transportclose', () => {
|
||||||
console.log('transport for this producer closed ')
|
console.log('transport for this producer closed ')
|
||||||
@ -164,17 +163,17 @@ peers.on('connection', async socket => {
|
|||||||
id: producer.id
|
id: producer.id
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// see client's socket.emit('transport-recv-connect', ...)
|
// see client's socket.emit('transport-recv-connect', ...)
|
||||||
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
|
socket.on('transport-recv-connect', async ({ dtlsParameters }) => {
|
||||||
console.log(`DTLS PARAMS: ${dtlsParameters}`)
|
console.log(`DTLS PARAMS: ${dtlsParameters}`)
|
||||||
await consumerTransport.connect({ dtlsParameters })
|
await consumerTransport.connect({ dtlsParameters })
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on('consume', async ({ rtpCapabilities }, callback) => {
|
socket.on('consume', async ({ rtpCapabilities, callId }, callback) => {
|
||||||
try {
|
try {
|
||||||
console.log('[consume] Producer ID:', producer.id, producer.kind)
|
// check if the router can consume the specified producer
|
||||||
// check if the routers can consume the specified producer
|
if (router[callId].canConsume({
|
||||||
if (routers[routerId].canConsume({
|
|
||||||
producerId: producer.id,
|
producerId: producer.id,
|
||||||
rtpCapabilities
|
rtpCapabilities
|
||||||
})) {
|
})) {
|
||||||
@ -201,10 +200,9 @@ peers.on('connection', async socket => {
|
|||||||
kind: consumer.kind,
|
kind: consumer.kind,
|
||||||
rtpParameters: consumer.rtpParameters,
|
rtpParameters: consumer.rtpParameters,
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the parameters to the client
|
// send the parameters to the client
|
||||||
callback({ params })
|
callback({ params })
|
||||||
} else {
|
|
||||||
console.log("Can't consume!!!", routers[routerId].canConsume());
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error.message)
|
console.log(error.message)
|
||||||
@ -220,13 +218,11 @@ peers.on('connection', async socket => {
|
|||||||
console.log('consumer resume')
|
console.log('consumer resume')
|
||||||
await consumer.resume()
|
await consumer.resume()
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const createWebRtcTransportLayer = async (callId, callback) => {
|
||||||
const createWebRtcTransport = async (routerId, callback) => {
|
|
||||||
try {
|
try {
|
||||||
|
console.log('createWebRtcTransportLayer | callId', callId);
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportOptions
|
||||||
const webRtcTransport_options = {
|
const webRtcTransport_options = {
|
||||||
listenIps: [
|
listenIps: [
|
||||||
@ -239,9 +235,9 @@ const createWebRtcTransport = async (routerId, callback) => {
|
|||||||
enableTcp: true,
|
enableTcp: true,
|
||||||
preferUdp: true,
|
preferUdp: true,
|
||||||
}
|
}
|
||||||
|
console.log('🔴 router[callId]', JSON.stringify(router));
|
||||||
// https://mediasoup.org/documentation/v3/mediasoup/api/#routers-createWebRtcTransport
|
// https://mediasoup.org/documentation/v3/mediasoup/api/#router-createWebRtcTransport
|
||||||
let transport = await routers[routerId].createWebRtcTransport(webRtcTransport_options)
|
let transport = await router[callId].createWebRtcTransport(webRtcTransport_options)
|
||||||
console.log(`transport id: ${transport.id}`)
|
console.log(`transport id: ${transport.id}`)
|
||||||
|
|
||||||
transport.on('dtlsstatechange', dtlsState => {
|
transport.on('dtlsstatechange', dtlsState => {
|
||||||
@ -268,7 +264,7 @@ const createWebRtcTransport = async (routerId, callback) => {
|
|||||||
return transport
|
return transport
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log('11111', error)
|
||||||
callback({
|
callback({
|
||||||
params: {
|
params: {
|
||||||
error: error
|
error: error
|
||||||
|
2148
public/bundle.js
2148
public/bundle.js
File diff suppressed because it is too large
Load Diff
@ -25,8 +25,6 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<body>
|
<body>
|
||||||
<label for="assetId">My asset id:</label><br>
|
|
||||||
<input type="number" id="assetId" name="assetId"><br>
|
|
||||||
<div id="video">
|
<div id="video">
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
@ -49,11 +47,16 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<div id="sharedBtns">
|
<div id="sharedBtns">
|
||||||
<button id="btnLocalVideo">1. Get Local Video</button>
|
<button id="btnLocalVideo">Publish</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<div id="sharedBtns">
|
||||||
|
<button id="btnRecvSendTransport">Consume</button>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<!-- <tr>
|
||||||
<td colspan="2">
|
<td colspan="2">
|
||||||
<div id="sharedBtns">
|
<div id="sharedBtns">
|
||||||
<button id="btnRtpCapabilities">2. Get Rtp Capabilities</button>
|
<button id="btnRtpCapabilities">2. Get Rtp Capabilities</button>
|
||||||
@ -76,7 +79,7 @@
|
|||||||
<button id="btnConnectRecvTransport">7. Connect Recv Transport & Consume</button>
|
<button id="btnConnectRecvTransport">7. Connect Recv Transport & Consume</button>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr> -->
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
164
public/index.js
164
public/index.js
@ -1,15 +1,18 @@
|
|||||||
const io = require("socket.io-client");
|
const io = require('socket.io-client')
|
||||||
const mediasoupClient = require('mediasoup-client')
|
const mediasoupClient = require('mediasoup-client')
|
||||||
|
const urlParams = new URLSearchParams(location.search);
|
||||||
|
|
||||||
const ASSET_ID = 71
|
const ASSET_ID = parseInt(urlParams.get('assetId')) || null;
|
||||||
const ACCOUNT_ID = 1
|
const ACCOUNT_ID = parseInt(urlParams.get('accountId')) || null;
|
||||||
|
let callId = parseInt(urlParams.get('callId')) || null;
|
||||||
|
const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false
|
||||||
|
console.log('ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER)
|
||||||
|
|
||||||
let hub = io('https://hub.dev.linx.safemobile.com/', {
|
let socket
|
||||||
reconnect: false,
|
hub = io('https://hub.dev.linx.safemobile.com/')
|
||||||
transports: ['websocket'],
|
|
||||||
})
|
|
||||||
|
|
||||||
hub.on('connect', async () => {
|
if (IS_PRODUCER === true) {
|
||||||
|
hub.on('connect', async () => {
|
||||||
console.log('hub.connected', hub.connected);
|
console.log('hub.connected', hub.connected);
|
||||||
hub.emit(
|
hub.emit(
|
||||||
'ars',
|
'ars',
|
||||||
@ -19,39 +22,65 @@ hub.on('connect', async () => {
|
|||||||
account_id: ACCOUNT_ID,
|
account_id: ACCOUNT_ID,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
hub.on("connect_error", (error) => {
|
hub.on('video', (data) => {
|
||||||
console.log('connect_error', error);
|
const parsedData = JSON.parse(data);
|
||||||
});
|
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;
|
||||||
|
|
||||||
hub.on('connection', async socket => {
|
console.log('IS_PRODUCER', IS_PRODUCER);
|
||||||
console.log('connection')
|
|
||||||
})
|
|
||||||
|
|
||||||
hub.on('disconnect', async socket => {
|
if (parsedData.type === 'notify-request' && IS_PRODUCER) {
|
||||||
console.log('disconnect')
|
getLocalStream()
|
||||||
})
|
}
|
||||||
|
|
||||||
// ------------------------------------------------
|
// setTimeout(() => {
|
||||||
|
// !IS_PRODUCER && goConnect()
|
||||||
const mediasoup = io('/mediasoup')
|
// }, 1000);
|
||||||
|
// getRtpCapabilities()
|
||||||
mediasoup.on('connection-success', ({ socketId }) => {
|
})
|
||||||
console.log('mediasoup.connected', mediasoup.connected)
|
|
||||||
|
|
||||||
mediasoup.emit('create-router', {
|
|
||||||
assetId: ASSET_ID
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
hub.on('connect_error', (error) => {
|
||||||
|
console.log('connect_error', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
hub.on('connection', () => {
|
||||||
|
console.log('connection')
|
||||||
|
})
|
||||||
|
|
||||||
|
hub.on('disconnect', () => {
|
||||||
|
console.log('disconnect')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
socket = io('https://localhost:3000/mediasoup')
|
||||||
|
|
||||||
|
socket.on('connection-success', ({ socketId, existsProducer }) => {
|
||||||
|
console.log(socketId, existsProducer)
|
||||||
|
// if (IS_PRODUCER) { getLocalStream() }
|
||||||
|
// setInterval(() => {
|
||||||
|
// if (!IS_PRODUCER && existsProducer) { goConnect() }
|
||||||
|
|
||||||
|
// }, 5000);
|
||||||
|
if (!IS_PRODUCER && existsProducer && consumer === undefined) { goConnect() }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let device
|
let device
|
||||||
let rtpCapabilities
|
let rtpCapabilities
|
||||||
let producerTransport
|
let producerTransport
|
||||||
let consumerTransport
|
let consumerTransport
|
||||||
let producer
|
let producer
|
||||||
let consumer
|
let consumer
|
||||||
|
let originAssetId
|
||||||
|
let originAssetName
|
||||||
|
let originAssetTypeName
|
||||||
|
|
||||||
// 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
|
||||||
@ -80,18 +109,19 @@ let params = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const streamSuccess = async (stream) => {
|
const streamSuccess = (stream) => {
|
||||||
localVideo.srcObject = stream
|
localVideo.srcObject = stream
|
||||||
const track = stream.getVideoTracks()[0]
|
const track = stream.getVideoTracks()[0]
|
||||||
params = {
|
params = {
|
||||||
track,
|
track,
|
||||||
...params
|
...params
|
||||||
}
|
}
|
||||||
|
|
||||||
|
goConnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
const getLocalStream = () => {
|
const getLocalStream = () => {
|
||||||
|
navigator.mediaDevices.getUserMedia({
|
||||||
navigator.getUserMedia({
|
|
||||||
audio: false,
|
audio: false,
|
||||||
video: {
|
video: {
|
||||||
width: {
|
width: {
|
||||||
@ -103,11 +133,21 @@ const getLocalStream = () => {
|
|||||||
max: 1080,
|
max: 1080,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, streamSuccess, error => {
|
})
|
||||||
|
.then(streamSuccess)
|
||||||
|
.catch(error => {
|
||||||
console.log(error.message)
|
console.log(error.message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const goConnect = () => {
|
||||||
|
device === undefined ? getRtpCapabilities() : goCreateTransport()
|
||||||
|
}
|
||||||
|
|
||||||
|
const goCreateTransport = () => {
|
||||||
|
IS_PRODUCER ? createSendTransport() : createRecvTransport()
|
||||||
|
}
|
||||||
|
|
||||||
// A device is an endpoint connecting to a Router on the
|
// A device is an endpoint connecting to a Router on the
|
||||||
// server side to send/recive media
|
// server side to send/recive media
|
||||||
const createDevice = async () => {
|
const createDevice = async () => {
|
||||||
@ -121,7 +161,10 @@ const createDevice = async () => {
|
|||||||
routerRtpCapabilities: rtpCapabilities
|
routerRtpCapabilities: rtpCapabilities
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('RTP Capabilities', device.rtpCapabilities)
|
console.log('Device RTP Capabilities', device.rtpCapabilities)
|
||||||
|
|
||||||
|
// once the device loads, create transport
|
||||||
|
goCreateTransport()
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
@ -134,19 +177,22 @@ const getRtpCapabilities = () => {
|
|||||||
// make a request to the server for Router RTP Capabilities
|
// make a request to the server for Router RTP Capabilities
|
||||||
// see server's socket.on('getRtpCapabilities', ...)
|
// see server's socket.on('getRtpCapabilities', ...)
|
||||||
// the server sends back data object which contains rtpCapabilities
|
// the server sends back data object which contains rtpCapabilities
|
||||||
mediasoup.emit('getRtpCapabilities', (data) => {
|
socket.emit('createRoom', { callId }, (data) => {
|
||||||
console.log(`Router RTP Capabilities... ${data.rtpCapabilities}`)
|
console.log(`Router RTP Capabilities... ${data.rtpCapabilities}`)
|
||||||
|
|
||||||
// we assign to local variable and will be used when
|
// we assign to local variable and will be used when
|
||||||
// loading the client Device (see createDevice above)
|
// loading the client Device (see createDevice above)
|
||||||
rtpCapabilities = data.rtpCapabilities
|
rtpCapabilities = data.rtpCapabilities
|
||||||
|
|
||||||
|
// once we have rtpCapabilities from the Router, create Device
|
||||||
|
createDevice()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const createSendTransport = () => {
|
const 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
|
||||||
mediasoup.emit('createWebRtcTransport', { sender: true }, ({ params }) => {
|
socket.emit('createWebRtcTransport', { sender: true, callId }, ({ params }) => {
|
||||||
// The server sends back params needed
|
// The server sends back params needed
|
||||||
// to create Send Transport on the client side
|
// to create Send Transport on the client side
|
||||||
if (params.error) {
|
if (params.error) {
|
||||||
@ -168,7 +214,7 @@ const createSendTransport = () => {
|
|||||||
try {
|
try {
|
||||||
// Signal local DTLS parameters to the server side transport
|
// Signal local DTLS parameters to the server side transport
|
||||||
// see server's socket.on('transport-connect', ...)
|
// see server's socket.on('transport-connect', ...)
|
||||||
await mediasoup.emit('transport-connect', {
|
await socket.emit('transport-connect', {
|
||||||
dtlsParameters,
|
dtlsParameters,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -188,7 +234,7 @@ const createSendTransport = () => {
|
|||||||
// with the following parameters and produce
|
// with the following parameters and produce
|
||||||
// and expect back a server side producer id
|
// and expect back a server side producer id
|
||||||
// see server's socket.on('transport-produce', ...)
|
// see server's socket.on('transport-produce', ...)
|
||||||
await mediasoup.emit('transport-produce', {
|
await socket.emit('transport-produce', {
|
||||||
kind: parameters.kind,
|
kind: parameters.kind,
|
||||||
rtpParameters: parameters.rtpParameters,
|
rtpParameters: parameters.rtpParameters,
|
||||||
appData: parameters.appData,
|
appData: parameters.appData,
|
||||||
@ -201,6 +247,8 @@ const createSendTransport = () => {
|
|||||||
errback(error)
|
errback(error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
connectSendTransport()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,15 +267,32 @@ const connectSendTransport = async () => {
|
|||||||
|
|
||||||
producer.on('transportclose', () => {
|
producer.on('transportclose', () => {
|
||||||
console.log('transport ended')
|
console.log('transport ended')
|
||||||
|
|
||||||
// close video track
|
// close video track
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const answer = {
|
||||||
|
origin_asset_id: ASSET_ID,
|
||||||
|
dest_asset_id: originAssetId,
|
||||||
|
type: 'notify-answer',
|
||||||
|
origin_asset_priority: 1,
|
||||||
|
origin_asset_type_name: originAssetTypeName,
|
||||||
|
origin_asset_name: originAssetName,
|
||||||
|
video_call_id: callId,
|
||||||
|
answer: 'accepted', // answer: 'rejected'
|
||||||
|
};
|
||||||
|
console.log('SEND answer', answer);
|
||||||
|
|
||||||
|
hub.emit(
|
||||||
|
'video',
|
||||||
|
JSON.stringify(answer)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const createRecvTransport = async () => {
|
const createRecvTransport = async () => {
|
||||||
|
console.log('createRecvTransport');
|
||||||
// see server's socket.on('consume', sender?, ...)
|
// see server's socket.on('consume', sender?, ...)
|
||||||
// this is a call from Consumer, so sender = false
|
// this is a call from Consumer, so sender = false
|
||||||
await mediasoup.emit('createWebRtcTransport', { sender: false }, ({ params }) => {
|
await socket.emit('createWebRtcTransport', { sender: false, callId }, ({ params }) => {
|
||||||
// The server sends back params needed
|
// The server sends back params needed
|
||||||
// to create Send Transport on the client side
|
// to create Send Transport on the client side
|
||||||
if (params.error) {
|
if (params.error) {
|
||||||
@ -249,7 +314,7 @@ const createRecvTransport = async () => {
|
|||||||
try {
|
try {
|
||||||
// Signal local DTLS parameters to the server side transport
|
// Signal local DTLS parameters to the server side transport
|
||||||
// see server's socket.on('transport-recv-connect', ...)
|
// see server's socket.on('transport-recv-connect', ...)
|
||||||
await mediasoup.emit('transport-recv-connect', {
|
await socket.emit('transport-recv-connect', {
|
||||||
dtlsParameters,
|
dtlsParameters,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -260,6 +325,8 @@ const createRecvTransport = async () => {
|
|||||||
errback(error)
|
errback(error)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
connectRecvTransport()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,15 +335,16 @@ const connectRecvTransport = async () => {
|
|||||||
// for consumer, we need to tell the server first
|
// for consumer, we need to tell the server first
|
||||||
// to create a consumer based on the rtpCapabilities and consume
|
// 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
|
// if the router can consume, it will send back a set of params as below
|
||||||
await mediasoup.emit('consume', {
|
await socket.emit('consume', {
|
||||||
rtpCapabilities: device.rtpCapabilities,
|
rtpCapabilities: device.rtpCapabilities,
|
||||||
|
callId
|
||||||
}, async ({ params }) => {
|
}, async ({ params }) => {
|
||||||
if (params.error) {
|
if (params.error) {
|
||||||
console.log('Cannot Consume')
|
console.log('Cannot Consume')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('params', params)
|
console.log(params)
|
||||||
// then consume with the local consumer transport
|
// then consume with the local consumer transport
|
||||||
// which creates a consumer
|
// which creates a consumer
|
||||||
consumer = await consumerTransport.consume({
|
consumer = await consumerTransport.consume({
|
||||||
@ -289,19 +357,15 @@ const connectRecvTransport = async () => {
|
|||||||
// destructure and retrieve the video track from the producer
|
// destructure and retrieve the video track from the producer
|
||||||
const { track } = consumer
|
const { track } = consumer
|
||||||
|
|
||||||
remoteVideo.srcObject = new MediaStream([track])
|
const stream = new MediaStream([track])
|
||||||
|
remoteVideo.srcObject = stream
|
||||||
|
|
||||||
// the server consumer started with media paused
|
// the server consumer started with media paused
|
||||||
// so we need to inform the server to resume
|
// so we need to inform the server to resume
|
||||||
console.log('consumer-resume');
|
socket.emit('consumer-resume')
|
||||||
mediasoup.emit('consumer-resume')
|
console.log('consumer', consumer);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
btnLocalVideo.addEventListener('click', getLocalStream)
|
btnLocalVideo.addEventListener('click', getLocalStream)
|
||||||
btnRtpCapabilities.addEventListener('click', getRtpCapabilities)
|
btnRecvSendTransport.addEventListener('click', goConnect)
|
||||||
btnDevice.addEventListener('click', createDevice)
|
|
||||||
btnCreateSendTransport.addEventListener('click', createSendTransport)
|
|
||||||
btnConnectSendTransport.addEventListener('click', connectSendTransport)
|
|
||||||
btnRecvSendTransport.addEventListener('click', createRecvTransport)
|
|
||||||
btnConnectRecvTransport.addEventListener('click', connectRecvTransport)
|
|
Loading…
Reference in New Issue
Block a user