Files
watchparty/app/src/CallManager.js
T
ayushya 86ee168045 v1.6.0/conference call upgrade
- anyone can start call
- colision check for calls
- better error handing for calls
2020-08-02 01:11:48 -04:00

223 lines
7.8 KiB
JavaScript

/************************************************************************
Copyright (C) Ayushya Amitabh - All Rights Reserved
Unauthorized copying of this file, via any medium is strictly prohibited
Proprietary and confidential
Written by Ayushya Amitabh <admin@chromewatchparty.com>, 2020
https://www.chromewatchparty.com/copyright
=========================================================================
ALL CONTENT ON IN WATCH PARTY'S CHROME EXTENSION APP, UNLESS OTHERWISE
CITED ARE THE SOLE INTELECTUAL PROPERTY OF AYUSHYA AMITABH, AND MAY NOT BE
COPIED, EDITED, OR REPRODUCED WITHOUT PERMISSION.
Netflix logo is a trademark and intellectual property of Netflix, Inc.
Disney+ logo is a trademark and intellectual property of The Walt Disney Company
Prime logo is a trademark and intellectual property of Amazon.com, Inc.
YouTube logo is a trademark and intellectual property of Google, Inc.
Hulu logo is a trademark and intellectual property of Hulu, LLC.
HBO Now and HBO Max logos are trademark and intellectual property of WarnerMedia Direct,LLC.
************************************************************************/
import VoxeetSDK from '@voxeet/voxeet-web-sdk';
import {
GetVoxeetTokenApi,
GetRefreshedVoxeetTokenApi,
LocalData,
UpdateVoxeetApi,
updateLocalData,
} from './Api';
export default class CallManager {
constructor(listener, notify) {
this.listener = listener;
this.notify = notify;
this.__refreshToken = LocalData().vaT;
this.__accessToken = LocalData().vrT;
this.__sessionInitialized = false;
this.__admin = false;
this.__adminOnly = true;
}
toggleCall(onCall, conference, admin, adminOnly) {
this.listener({ callActionPending: true });
this.__admin = admin;
this.__adminOnly = adminOnly;
if (onCall === true) this.leaveCall();
else this.boot(conference);
}
toggleMute(muted) {
this.listener({ callActionPending: true });
VoxeetSDK.conference.mute(VoxeetSDK.session.participant, !muted);
this.listener({ muted: !muted, callActionPending: false });
}
async boot(conference) {
await this.initialize();
const opened = await this.openSession();
if (opened && conference) {
await VoxeetSDK.conference
.fetch(conference._id)
.then(this.joinCall)
.catch((err) => {
console.error('Failed to fetch conference', err);
this.startCall();
});
} else if (opened && !conference) {
this.startCall();
} else {
this.listener({ onCall: false, muted: false, callActionPending: false });
console.error('Unable to open Voxeet Session');
}
}
async startCall() {
if (!this.__adminOnly || (this.__adminOnly && this.__admin)) {
VoxeetSDK.conference
.create({ alias: `AUTODELETE_${LocalData().partyName}`, pin: LocalData().partyPin })
.then(async (conference) => {
const updatedVoxeet = await UpdateVoxeetApi({
conference,
partyName: LocalData().partyName,
partyPin: LocalData().partyPin,
userName: LocalData().userName,
})
.then((res) => res.json())
.then((res) => res.status <= 299)
.catch((err) => {
if (err.conference) {
this.joinCall(err.conference);
return true;
} else {
this.notify(err.message);
this.listener({ onCall: false, muted: false, callActionPending: false });
console.error('Update Voxeet API Error', err);
return false;
}
});
if (updatedVoxeet === true) {
this.joinCall(conference);
} else {
this.listener({ onCall: false, muted: false, callActionPending: false });
console.error('Failed to update party Voxeet');
}
});
} else {
this.notify('Ask your party admin to start the call.');
this.leaveCall();
}
}
async joinCall(conference) {
const joined = await VoxeetSDK.conference
.join(conference, { constraints: { audio: true, video: false } })
.then(() => true)
.catch(e => {
this.listener({ onCall: false, muted: false, callActionPending: false });
this.notify('Unable to join call. Try again in a moment.');
console.error('Error joining call', e);
return true;
});
if (!joined) {
this.startCall();
}
}
async leaveCall() {
VoxeetSDK.conference.leave().then(() => {
VoxeetSDK.session.close();
this.listener({ onCall: false, muted: false, callActionPending: false });
});
}
async openSession() {
if (!this.__sessionInitialized) {
try {
await VoxeetSDK.session.open({
externalId: LocalData().userUid,
name: LocalData().userName,
});
return true;
} catch (error) {
console.error('Error Opening Voxeet Session', error);
}
console.error('Session not initialized');
return false;
}
return true;
}
async usersListHandle() {
var usersOnCall = {};
VoxeetSDK.conference.participants.forEach((v, k, m) => {
usersOnCall[k] = v;
});
this.listener({ usersOnCall });
}
async initialize() {
VoxeetSDK.conference.on('participantAdded', () => this.usersListHandle());
VoxeetSDK.conference.on('participantUpdated', () => this.usersListHandle());
VoxeetSDK.conference.on('joined', () => {
this.listener({ onCall: true, callActionPending: false });
});
VoxeetSDK.conference.on('left', () => {
this.listener({ onCall: false, callActionPending: false, usersOnCall: {} });
});
VoxeetSDK.conference.on('streamAdded', (participant, stream) => {
if (participant !== VoxeetSDK.session.participant) {
const voiceNode = document.createElement('video');
voiceNode.srcObject = stream;
voiceNode.id = participant.id;
voiceNode.width = 0;
voiceNode.height = 0;
voiceNode.autoplay = true;
voiceNode.classList.add('voxeetAudioOut');
document.body.appendChild(voiceNode);
}
this.listener({ callActionPending: false });
});
VoxeetSDK.conference.on('streamRemoved', (participant, stream) => {
if (document.getElementById(participant.id)) document.getElementById(participant.id).remove();
this.listener({ callActionPending: false });
});
if (
this.__accessToken !== undefined ||
this.__refreshToken !== undefined ||
this.__accessToken !== null ||
this.__refreshToken !== null
) {
await GetVoxeetTokenApi({ partyName: LocalData().partyName })
.then((res) => res.json())
.then((tokenResult) => {
if (tokenResult.status <= 299) {
const { access_token, refresh_token } = tokenResult;
this.updateTokens(access_token, refresh_token);
VoxeetSDK.initializeToken(access_token, refresh_token);
} else {
console.error('Get Voxeet Token API Result Error', tokenResult);
}
})
.catch((err) => {
console.error('Get Voxeet Token API error', err);
});
} else {
VoxeetSDK.initializeToken(this.__accessToken, this.__refreshToken);
}
}
async refreshToken() {
return new Promise((resolve, reject) => {
GetRefreshedVoxeetTokenApi({ token: this.__refreshToken })
.then((res) => res.json())
.then((tokenResult) => {
const { access_token, refresh_token } = tokenResult;
this.updateTokens(access_token, refresh_token);
resolve(access_token);
})
.catch((err) => {
console.error('Get Voxeet Token API error', err);
reject(err);
});
});
}
updateTokens(aT, rT) {
this.__accessToken = aT;
this.__refreshToken = rT;
updateLocalData({ vaT: aT, vrT: rT });
}
}