2022-07-27 06:55:07 +00:00
|
|
|
const io = require('socket.io-client')
|
2022-07-23 07:32:54 +00:00
|
|
|
const mediasoupClient = require('mediasoup-client')
|
2022-07-27 06:55:07 +00:00
|
|
|
const urlParams = new URLSearchParams(location.search);
|
2022-07-29 08:32:51 +00:00
|
|
|
const config = require('./config')
|
|
|
|
console.log('[CONFIG]', config);
|
2022-07-27 06:55:07 +00:00
|
|
|
|
|
|
|
const ASSET_ID = parseInt(urlParams.get('assetId')) || null;
|
|
|
|
const ACCOUNT_ID = parseInt(urlParams.get('accountId')) || null;
|
|
|
|
let callId = parseInt(urlParams.get('callId')) || null;
|
|
|
|
const IS_PRODUCER = urlParams.get('producer') === 'true' ? true : false
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log('[URL] ASSET_ID', ASSET_ID, '| ACCOUNT_ID', ACCOUNT_ID, '| callId', callId, ' | IS_PRODUCER', IS_PRODUCER)
|
2022-07-27 06:55:07 +00:00
|
|
|
|
|
|
|
let socket
|
2022-07-29 08:32:51 +00:00
|
|
|
hub = io(config.hubAddress)
|
|
|
|
|
|
|
|
const connectToMediasoup = () => {
|
|
|
|
|
|
|
|
socket = io(config.mediasoupAddress)
|
|
|
|
|
|
|
|
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() }
|
|
|
|
})
|
|
|
|
}
|
2022-07-27 06:55:07 +00:00
|
|
|
|
|
|
|
if (IS_PRODUCER === true) {
|
|
|
|
hub.on('connect', async () => {
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log(`[HUB] ${config.hubAddress} | connected: ${hub.connected}`)
|
|
|
|
connectToMediasoup()
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
hub.emit(
|
|
|
|
'ars',
|
|
|
|
JSON.stringify({
|
|
|
|
ars: true,
|
|
|
|
asset_id: ASSET_ID,
|
|
|
|
account_id: ACCOUNT_ID,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
hub.on('video', (data) => {
|
|
|
|
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;
|
|
|
|
|
|
|
|
console.log('IS_PRODUCER', IS_PRODUCER);
|
|
|
|
|
|
|
|
if (parsedData.type === 'notify-request' && IS_PRODUCER) {
|
|
|
|
getLocalStream()
|
|
|
|
}
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
})
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
hub.on('connect_error', (error) => {
|
|
|
|
console.log('connect_error', error);
|
|
|
|
});
|
|
|
|
|
|
|
|
hub.on('connection', () => {
|
|
|
|
console.log('connection')
|
|
|
|
})
|
|
|
|
|
|
|
|
hub.on('disconnect', () => {
|
|
|
|
console.log('disconnect')
|
|
|
|
})
|
2022-07-29 08:32:51 +00:00
|
|
|
} else {
|
|
|
|
connectToMediasoup()
|
2022-07-27 06:55:07 +00:00
|
|
|
}
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
let device
|
|
|
|
let rtpCapabilities
|
|
|
|
let producerTransport
|
|
|
|
let consumerTransport
|
|
|
|
let producer
|
|
|
|
let consumer
|
2022-07-27 06:55:07 +00:00
|
|
|
let originAssetId
|
|
|
|
let originAssetName
|
|
|
|
let originAssetTypeName
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
const streamSuccess = (stream) => {
|
2022-07-23 07:32:54 +00:00
|
|
|
localVideo.srcObject = stream
|
|
|
|
const track = stream.getVideoTracks()[0]
|
|
|
|
params = {
|
|
|
|
track,
|
|
|
|
...params
|
|
|
|
}
|
2022-07-27 06:55:07 +00:00
|
|
|
goConnect()
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getLocalStream = () => {
|
2022-07-29 08:32:51 +00:00
|
|
|
console.log('[getLocalStream]');
|
2022-07-27 06:55:07 +00:00
|
|
|
navigator.mediaDevices.getUserMedia({
|
2022-07-23 07:32:54 +00:00
|
|
|
audio: false,
|
|
|
|
video: {
|
|
|
|
width: {
|
|
|
|
min: 640,
|
|
|
|
max: 1920,
|
|
|
|
},
|
|
|
|
height: {
|
|
|
|
min: 400,
|
|
|
|
max: 1080,
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 06:55:07 +00:00
|
|
|
})
|
|
|
|
.then(streamSuccess)
|
|
|
|
.catch(error => {
|
2022-07-23 07:32:54 +00:00
|
|
|
console.log(error.message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
const goConnect = () => {
|
|
|
|
device === undefined ? getRtpCapabilities() : goCreateTransport()
|
|
|
|
}
|
|
|
|
|
|
|
|
const goCreateTransport = () => {
|
|
|
|
IS_PRODUCER ? createSendTransport() : createRecvTransport()
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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
|
|
|
|
})
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
console.log('Device RTP Capabilities', device.rtpCapabilities)
|
|
|
|
|
|
|
|
// once the device loads, create transport
|
|
|
|
goCreateTransport()
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
} 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
|
2022-07-27 06:55:07 +00:00
|
|
|
socket.emit('createRoom', { callId }, (data) => {
|
2022-07-23 07:32:54 +00:00
|
|
|
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
|
2022-07-27 06:55:07 +00:00
|
|
|
|
|
|
|
// once we have rtpCapabilities from the Router, create Device
|
|
|
|
createDevice()
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const createSendTransport = () => {
|
|
|
|
// see server's socket.on('createWebRtcTransport', sender?, ...)
|
|
|
|
// this is a call from Producer, so sender = true
|
2022-07-27 06:55:07 +00:00
|
|
|
socket.emit('createWebRtcTransport', { sender: true, callId }, ({ params }) => {
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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', ...)
|
2022-07-27 06:55:07 +00:00
|
|
|
await socket.emit('transport-connect', {
|
2022-07-23 07:32:54 +00:00
|
|
|
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', ...)
|
2022-07-27 06:55:07 +00:00
|
|
|
await socket.emit('transport-produce', {
|
2022-07-23 07:32:54 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
|
|
|
|
connectSendTransport()
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
|
|
|
|
const answer = {
|
|
|
|
origin_asset_id: ASSET_ID,
|
2022-07-29 08:32:51 +00:00
|
|
|
dest_asset_id: originAssetId || parseInt(urlParams.get('dest_asset_id')),
|
2022-07-27 06:55:07 +00:00
|
|
|
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)
|
|
|
|
);
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const createRecvTransport = async () => {
|
2022-07-27 06:55:07 +00:00
|
|
|
console.log('createRecvTransport');
|
2022-07-23 07:32:54 +00:00
|
|
|
// see server's socket.on('consume', sender?, ...)
|
|
|
|
// this is a call from Consumer, so sender = false
|
2022-07-27 06:55:07 +00:00
|
|
|
await socket.emit('createWebRtcTransport', { sender: false, callId }, ({ params }) => {
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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', ...)
|
2022-07-27 06:55:07 +00:00
|
|
|
await socket.emit('transport-recv-connect', {
|
2022-07-23 07:32:54 +00:00
|
|
|
dtlsParameters,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Tell the transport that parameters were transmitted.
|
|
|
|
callback()
|
|
|
|
} catch (error) {
|
|
|
|
// Tell the transport that something was wrong
|
|
|
|
errback(error)
|
|
|
|
}
|
|
|
|
})
|
2022-07-27 06:55:07 +00:00
|
|
|
connectRecvTransport()
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-07-27 06:55:07 +00:00
|
|
|
await socket.emit('consume', {
|
2022-07-23 07:32:54 +00:00
|
|
|
rtpCapabilities: device.rtpCapabilities,
|
2022-07-27 06:55:07 +00:00
|
|
|
callId
|
2022-07-23 07:32:54 +00:00
|
|
|
}, async ({ params }) => {
|
|
|
|
if (params.error) {
|
|
|
|
console.log('Cannot Consume')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2022-07-29 08:32:51 +00:00
|
|
|
let stream = new MediaStream()
|
|
|
|
stream.addTrack(track)
|
|
|
|
// stream.removeTrack(track)
|
2022-07-27 06:55:07 +00:00
|
|
|
remoteVideo.srcObject = stream
|
|
|
|
socket.emit('consumer-resume')
|
|
|
|
console.log('consumer', consumer);
|
2022-07-29 08:32:51 +00:00
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
btnLocalVideo.addEventListener('click', getLocalStream)
|
2022-07-27 06:55:07 +00:00
|
|
|
btnRecvSendTransport.addEventListener('click', goConnect)
|