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;
|
2022-08-07 13:54:39 +00:00
|
|
|
const ASSET_NAME = urlParams.get('assetName') || null;
|
|
|
|
const ASSET_TYPE = urlParams.get('assetType') || null;
|
2022-07-27 06:55:07 +00:00
|
|
|
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
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('🟩 config', config)
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
let socket, hub
|
2022-07-23 07:32:54 +00:00
|
|
|
let device
|
|
|
|
let rtpCapabilities
|
|
|
|
let producerTransport
|
|
|
|
let consumerTransport
|
2022-11-29 12:19:02 +00:00
|
|
|
let producerVideo
|
|
|
|
let producerAudio
|
2022-07-23 07:32:54 +00:00
|
|
|
let consumer
|
2022-07-27 06:55:07 +00:00
|
|
|
let originAssetId
|
2022-12-19 10:13:48 +00:00
|
|
|
let consumerVideo // local consumer video(consumer not transport)
|
|
|
|
let consumerAudio // local consumer audio(consumer not transport)
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-12-19 10:30:25 +00:00
|
|
|
const remoteSoundControl = document.getElementById('remoteSoundControl');
|
|
|
|
|
|
|
|
remoteSoundControl.addEventListener('click', function handleClick() {
|
2022-12-19 10:33:19 +00:00
|
|
|
console.log('remoteSoundControl.textContent', remoteSoundControl.textContent);
|
|
|
|
remoteSoundControl.textContent = 'Button clicked';
|
|
|
|
// let soundElementIcon = document.getElementById('sound')
|
|
|
|
// if (isSoundOn) {
|
|
|
|
// soundElementIcon.src = noSoundImage
|
|
|
|
// soundElementIcon.alt = 'no-sound-image'
|
|
|
|
// isSoundOn = false
|
|
|
|
// vid.muted = !isSoundOn
|
|
|
|
// } else {
|
|
|
|
// soundElementIcon.src = soundImage
|
|
|
|
// soundElementIcon.alt = 'sound-image'
|
|
|
|
// isSoundOn = true
|
|
|
|
// vid.muted = !isSoundOn
|
|
|
|
// }
|
2022-12-19 10:30:25 +00:00
|
|
|
});
|
|
|
|
|
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
|
2022-11-29 12:19:02 +00:00
|
|
|
let videoParams = {
|
2022-07-23 07:32:54 +00:00
|
|
|
encodings: [
|
2022-11-29 12:19:02 +00:00
|
|
|
{ scaleResolutionDownBy: 4, maxBitrate: 500000 },
|
|
|
|
{ scaleResolutionDownBy: 2, maxBitrate: 1000000 },
|
|
|
|
{ scaleResolutionDownBy: 1, maxBitrate: 5000000 },
|
|
|
|
{ scalabilityMode: 'S3T3_KEY' }
|
2022-07-23 07:32:54 +00:00
|
|
|
],
|
|
|
|
codecOptions: {
|
|
|
|
videoGoogleStartBitrate: 1000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
let audioParams = {
|
|
|
|
codecOptions :
|
|
|
|
{
|
|
|
|
opusStereo : true,
|
|
|
|
opusDtx : true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
hub = io(config.hubAddress)
|
|
|
|
|
|
|
|
const connectToMediasoup = () => {
|
|
|
|
|
|
|
|
socket = io(config.mediasoupAddress, {
|
|
|
|
reconnection: true,
|
|
|
|
reconnectionDelay: 1000,
|
|
|
|
reconnectionDelayMax : 5000,
|
|
|
|
reconnectionAttempts: Infinity
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('connection-success', ({ _socketId, existsProducer }) => {
|
|
|
|
console.log(`[MEDIA] ${config.mediasoupAddress} | connected: ${socket.connected} | existsProducer: ${existsProducer}`)
|
|
|
|
if (!IS_PRODUCER && existsProducer && consumer === undefined) {
|
|
|
|
goConnect()
|
|
|
|
// document.getElementById('btnRecvSendTransport').click();
|
|
|
|
}
|
|
|
|
if (IS_PRODUCER && urlParams.get('testing') === 'true') { getLocalStream() }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_PRODUCER === true) {
|
|
|
|
hub.on('connect', async () => {
|
|
|
|
console.log(`[HUB]! ${config.hubAddress} | connected: ${hub.connected}`)
|
|
|
|
connectToMediasoup()
|
|
|
|
|
|
|
|
hub.emit(
|
|
|
|
'ars',
|
|
|
|
JSON.stringify({
|
|
|
|
ars: true,
|
|
|
|
asset_id: ASSET_ID,
|
|
|
|
account_id: ACCOUNT_ID,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
hub.on('video', (data) => {
|
|
|
|
const parsedData = JSON.parse(data);
|
|
|
|
|
|
|
|
if (parsedData.type === 'notify-request') {
|
|
|
|
console.log('video', parsedData)
|
|
|
|
originAssetId = parsedData.origin_asset_id;
|
|
|
|
// originAssetName = parsedData.origin_asset_name;
|
|
|
|
// originAssetTypeName = parsedData.origin_asset_type_name;
|
|
|
|
callId = parsedData.video_call_id;
|
|
|
|
|
|
|
|
console.log('[VIDEO] notify-request | IS_PRODUCER', IS_PRODUCER, 'callId', callId);
|
|
|
|
getLocalStream()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedData.type === 'notify-end') {
|
|
|
|
console.log('[VIDEO] notify-end | IS_PRODUCER', IS_PRODUCER, 'callId', callId);
|
|
|
|
resetCallSettings()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
hub.on('connect_error', (error) => {
|
|
|
|
console.log('connect_error', error);
|
|
|
|
});
|
|
|
|
|
|
|
|
hub.on('connection', () => {
|
|
|
|
console.log('connection')
|
|
|
|
})
|
|
|
|
|
|
|
|
hub.on('disconnect', () => {
|
|
|
|
console.log('disconnect')
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
connectToMediasoup()
|
|
|
|
}
|
|
|
|
|
|
|
|
}, 1600);
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
const streamSuccess = (stream) => {
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('[streamSuccess] device', device);
|
2022-07-23 07:32:54 +00:00
|
|
|
localVideo.srcObject = stream
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('stream', stream);
|
|
|
|
const videoTrack = stream.getVideoTracks()[0]
|
|
|
|
const audioTrack = stream.getAudioTracks()[0]
|
|
|
|
|
|
|
|
videoParams = {
|
|
|
|
track: videoTrack,
|
|
|
|
...videoParams
|
|
|
|
}
|
|
|
|
|
|
|
|
audioParams = {
|
|
|
|
track: audioTrack,
|
|
|
|
...audioParams
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
2022-11-29 12:19:02 +00:00
|
|
|
|
|
|
|
console.log('[streamSuccess] videoParams', videoParams, ' | audioParams', audioParams);
|
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-11-29 12:19:02 +00:00
|
|
|
audio: true,
|
2022-07-23 07:32:54 +00:00
|
|
|
video: {
|
2022-11-29 12:19:02 +00:00
|
|
|
qvga : { width: { ideal: 320 }, height: { ideal: 240 } },
|
|
|
|
vga : { width: { ideal: 640 }, height: { ideal: 480 } },
|
|
|
|
hd : { width: { ideal: 1280 }, height: { ideal: 720 } }
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
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-11-29 12:19:02 +00:00
|
|
|
|
|
|
|
navigator.permissions.query(
|
|
|
|
{ name: 'microphone' }
|
|
|
|
).then((permissionStatus) =>{
|
|
|
|
console.log('🟨 [PERMISSION] permissionStatus', permissionStatus); // granted, denied, prompt
|
|
|
|
// It will block the code from execution and display "Permission denied" if we don't have microphone permissions
|
|
|
|
})
|
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
const goConnect = () => {
|
2022-08-05 08:43:37 +00:00
|
|
|
console.log('[goConnect] device:', device);
|
2022-07-27 06:55:07 +00:00
|
|
|
device === undefined ? getRtpCapabilities() : goCreateTransport()
|
|
|
|
}
|
|
|
|
|
|
|
|
const goCreateTransport = () => {
|
2022-08-05 08:43:37 +00:00
|
|
|
console.log('[goCreateTransport] IS_PRODUCER:', IS_PRODUCER);
|
2022-07-27 06:55:07 +00:00
|
|
|
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)
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('[createDevice] device', device);
|
|
|
|
|
2022-07-27 06:55:07 +00:00
|
|
|
// 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 = () => {
|
2022-08-05 08:32:40 +00:00
|
|
|
console.log('[getRtpCapabilities]');
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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 = () => {
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('[createSendTransport');
|
2022-07-23 07:32:54 +00:00
|
|
|
// see server's socket.on('createWebRtcTransport', sender?, ...)
|
|
|
|
// this is a call from Producer, so sender = true
|
2022-12-16 09:59:10 +00:00
|
|
|
socket.emit('createWebRtcTransport', { sender: true }, (value) => {
|
2022-11-29 12:19:02 +00:00
|
|
|
|
|
|
|
console.log(`[createWebRtcTransport] value: ${JSON.stringify(value)}`);
|
|
|
|
|
|
|
|
const params = value.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
|
|
|
|
}
|
|
|
|
// 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) => {
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('[produce] parameters', parameters)
|
2022-07-23 07:32:54 +00:00
|
|
|
|
|
|
|
try {
|
2022-11-29 12:19:02 +00:00
|
|
|
// Tell the server to create a Producer
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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 () => {
|
2022-11-29 12:19:02 +00:00
|
|
|
|
|
|
|
console.log('[connectSendTransport] producerTransport');
|
|
|
|
|
|
|
|
// We now call produce() to instruct the producer transport
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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
|
2022-11-29 12:19:02 +00:00
|
|
|
|
|
|
|
// Produce video
|
|
|
|
producerVideo = await producerTransport.produce(videoParams)
|
|
|
|
console.log('videoParams', videoParams);
|
|
|
|
console.log('producerVideo', producerVideo);
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
producerVideo.on('trackended', () => {
|
2022-07-23 07:32:54 +00:00
|
|
|
console.log('track ended')
|
|
|
|
// close video track
|
2022-11-29 12:19:02 +00:00
|
|
|
})
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
producerVideo.on('transportclose', () => {
|
2022-07-23 07:32:54 +00:00
|
|
|
console.log('transport ended')
|
|
|
|
// close video track
|
2022-11-29 12:19:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Produce audio
|
|
|
|
producerAudio = await producerTransport.produce(audioParams)
|
|
|
|
console.log('audioParams', audioParams);
|
|
|
|
console.log('producerAudio', producerAudio);
|
|
|
|
|
|
|
|
producerAudio.on('trackended', () => {
|
|
|
|
console.log('track ended')
|
|
|
|
// close audio track
|
|
|
|
})
|
|
|
|
|
|
|
|
producerAudio.on('transportclose', () => {
|
|
|
|
console.log('transport ended')
|
|
|
|
// close audio track
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
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,
|
2022-08-07 13:54:39 +00:00
|
|
|
origin_asset_type_name: ASSET_TYPE,
|
|
|
|
origin_asset_name: ASSET_NAME,
|
2022-07-27 06:55:07 +00:00
|
|
|
video_call_id: callId,
|
2022-11-29 12:19:02 +00:00
|
|
|
answer: 'accepted', // answer: accepted/rejected
|
2022-07-27 06:55:07 +00:00
|
|
|
};
|
|
|
|
console.log('SEND answer', answer);
|
|
|
|
|
2022-12-16 10:01:20 +00:00
|
|
|
hub.emit(
|
|
|
|
'video',
|
|
|
|
JSON.stringify(answer)
|
|
|
|
);
|
2022-08-06 07:05:43 +00:00
|
|
|
|
|
|
|
// Enable Close call button
|
|
|
|
const closeCallBtn = document.getElementById('btnCloseCall');
|
|
|
|
closeCallBtn.removeAttribute('disabled');
|
2022-07-23 07:32:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const createRecvTransport = async () => {
|
2022-07-27 06:55:07 +00:00
|
|
|
console.log('createRecvTransport');
|
2022-11-29 12:19:02 +00:00
|
|
|
// See server's socket.on('consume', sender?, ...)
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
console.log('[createRecvTransport] params', params)
|
2022-07-23 07:32:54 +00:00
|
|
|
|
2022-11-29 12:19:02 +00:00
|
|
|
// Creates a new WebRTC Transport to receive media
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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
|
2022-11-29 12:19:02 +00:00
|
|
|
// This event is raised when a first call to transport.produce() is made
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-06 07:05:43 +00:00
|
|
|
const resetCallSettings = () => {
|
|
|
|
localVideo.srcObject = null
|
|
|
|
remoteVideo.srcObject = null
|
|
|
|
consumer = null
|
2022-11-29 12:19:02 +00:00
|
|
|
producerVideo = null
|
|
|
|
producerAudio = null
|
2022-08-06 07:05:43 +00:00
|
|
|
producerTransport = null
|
|
|
|
consumerTransport = null
|
|
|
|
device = undefined
|
|
|
|
}
|
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
const connectRecvTransport = async () => {
|
|
|
|
console.log('connectRecvTransport');
|
2022-11-29 12:19:02 +00:00
|
|
|
// For consumer, we need to tell the server first
|
2022-07-23 07:32:54 +00:00
|
|
|
// 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-12-19 09:24:19 +00:00
|
|
|
}, async ({videoParams, audioParams}) => {
|
2022-12-19 10:01:21 +00:00
|
|
|
console.log(`[consume] 🟩 videoParams`, videoParams)
|
|
|
|
console.log(`[consume] 🟩 audioParams`, audioParams)
|
|
|
|
console.log('[consume] 🟩 consumerTransport', consumerTransport)
|
2022-12-19 09:26:08 +00:00
|
|
|
|
2022-12-19 10:01:21 +00:00
|
|
|
let stream = new MediaStream()
|
2022-12-19 09:26:08 +00:00
|
|
|
|
2022-12-19 10:01:21 +00:00
|
|
|
// Maybe the unit does not produce video or audio, so we must only consume what is produced
|
|
|
|
if (videoParams) {
|
|
|
|
stream.addTrack(await getVideoTrask(videoParams))
|
|
|
|
} else {
|
2022-12-19 10:22:21 +00:00
|
|
|
console.log('❗ Have VIDEO stream to consume');
|
2022-12-19 10:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audioParams) {
|
2022-12-19 10:22:21 +00:00
|
|
|
console.log('❗ Have AUDIO stream to consume');
|
2022-12-19 10:01:21 +00:00
|
|
|
let audioTrack = await getAudioTrask(audioParams)
|
|
|
|
stream.addTrack(audioTrack)
|
|
|
|
} else {
|
|
|
|
console.log('❗ Don\'t have AUDIO stream to consume');
|
|
|
|
// const soundElement = document.getElementById('sound');
|
|
|
|
// soundElement.remove();
|
|
|
|
}
|
2022-12-19 09:26:08 +00:00
|
|
|
|
|
|
|
socket.emit('consumer-resume')
|
2022-07-29 08:32:51 +00:00
|
|
|
|
2022-12-19 10:01:21 +00:00
|
|
|
let remoteVideo = document.getElementById('remoteVideo')
|
|
|
|
remoteVideo.srcObject = stream
|
|
|
|
remoteVideo.setAttribute('autoplay', true)
|
|
|
|
remoteVideo.setAttribute('muted', false)
|
|
|
|
|
|
|
|
remoteVideo.play()
|
|
|
|
.then(() => {
|
|
|
|
console.log('remoteVideo PLAY')
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
displayError(`remoteVideo PLAY ERROR | ${error.message}`)
|
|
|
|
})
|
2022-07-23 07:32:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:13:48 +00:00
|
|
|
const getVideoTrask = async (videoParams) => {
|
|
|
|
consumerVideo = await consumerTransport.consume({
|
|
|
|
id: videoParams.id,
|
|
|
|
producerId: videoParams.producerId,
|
|
|
|
kind: videoParams.kind,
|
|
|
|
rtpParameters: videoParams.rtpParameters
|
|
|
|
})
|
|
|
|
|
|
|
|
consumerVideo.on('transportclose', () => {
|
|
|
|
console.log('transport closed so consumer closed')
|
|
|
|
})
|
|
|
|
|
|
|
|
return consumerVideo.track
|
|
|
|
}
|
|
|
|
|
|
|
|
const getAudioTrask = async (audioParams) => {
|
|
|
|
consumerAudio = await consumerTransport.consume({
|
|
|
|
id: audioParams.id,
|
|
|
|
producerId: audioParams.producerId,
|
|
|
|
kind: audioParams.kind,
|
|
|
|
rtpParameters: audioParams.rtpParameters
|
|
|
|
})
|
|
|
|
|
|
|
|
consumerAudio.on('transportclose', () => {
|
|
|
|
console.log('transport closed so consumer closed')
|
|
|
|
})
|
|
|
|
|
|
|
|
const audioTrack = consumerAudio.track
|
|
|
|
|
|
|
|
audioTrack.applyConstraints({
|
|
|
|
audio: {
|
|
|
|
advanced: [
|
|
|
|
{
|
|
|
|
echoCancellation: {exact: true}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
autoGainControl: {exact: true}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
noiseSuppression: {exact: true}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
highpassFilter: {exact: true}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return audioTrack
|
|
|
|
}
|
|
|
|
|
2022-08-06 07:05:43 +00:00
|
|
|
const closeCall = () => {
|
2022-08-11 10:17:44 +00:00
|
|
|
console.log('closeCall');
|
2022-08-11 08:55:11 +00:00
|
|
|
|
2022-08-11 10:17:44 +00:00
|
|
|
// Emit 'notify-end' to Hub so the consumer will know to close the video
|
|
|
|
const notifyEnd = {
|
|
|
|
origin_asset_id: ASSET_ID,
|
|
|
|
dest_asset_id: originAssetId || parseInt(urlParams.get('dest_asset_id')),
|
|
|
|
type: 'notify-end',
|
|
|
|
video_call_id: callId
|
|
|
|
}
|
|
|
|
console.log('notifyEnd', notifyEnd)
|
|
|
|
hub.emit('video', JSON.stringify(notifyEnd))
|
|
|
|
|
|
|
|
// Disable Close call button
|
|
|
|
const closeCallBtn = document.getElementById('btnCloseCall')
|
|
|
|
closeCallBtn.setAttribute('disabled', '')
|
|
|
|
|
|
|
|
// Reset settings
|
|
|
|
resetCallSettings()
|
2022-08-06 07:05:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 11:33:13 +00:00
|
|
|
const consume = async () => {
|
|
|
|
console.log('[consume]')
|
|
|
|
console.log('createRecvTransport Consumer')
|
|
|
|
await socket.emit('createWebRtcTransport', { sender: false, callId, dispatcher: true }, ({ params }) => {
|
|
|
|
if (params.error) {
|
|
|
|
console.log('createRecvTransport | createWebRtcTransport | Error', params.error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
consumerTransport = device.createRecvTransport(params)
|
|
|
|
consumerTransport.on('connect', async ({ dtlsParameters }, callback, errback) => {
|
|
|
|
try {
|
|
|
|
await socket.emit('transport-recv-connect', {
|
|
|
|
dtlsParameters,
|
|
|
|
})
|
|
|
|
callback()
|
|
|
|
} catch (error) {
|
|
|
|
errback(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
connectRecvTransport()
|
|
|
|
})
|
|
|
|
}
|
2022-11-29 12:19:02 +00:00
|
|
|
|
2022-07-23 07:32:54 +00:00
|
|
|
btnLocalVideo.addEventListener('click', getLocalStream)
|
2022-12-16 11:33:13 +00:00
|
|
|
btnRecvSendTransport.addEventListener('click', consume)
|
2022-08-06 07:05:43 +00:00
|
|
|
btnCloseCall.addEventListener('click', closeCall)
|