v1.3.0-beta/voice call implemented
This commit is contained in:
+122
-38
@@ -1,65 +1,148 @@
|
||||
import { VoxeetSDK } from '@voxeet/voxeet-web-sdk';
|
||||
import { GetVoxeetTokenApi, GetRefreshedVoxeetTokenApi } from './Api';
|
||||
import VoxeetSDK from '@voxeet/voxeet-web-sdk';
|
||||
import {
|
||||
GetVoxeetTokenApi,
|
||||
GetRefreshedVoxeetTokenApi,
|
||||
LocalData,
|
||||
UpdateVoxeetApi,
|
||||
updateLocalData,
|
||||
} from './Api';
|
||||
|
||||
export default class CallManager {
|
||||
constructor(listener) {
|
||||
constructor(listener, notify) {
|
||||
this.listener = listener;
|
||||
this.__refreshToken = null;
|
||||
this.__accessToken = null;
|
||||
this.notify = notify;
|
||||
this.__refreshToken = LocalData().vaT;
|
||||
this.__accessToken = LocalData().vrT;
|
||||
this.__sessionInitialized = false;
|
||||
this.__admin = false;
|
||||
}
|
||||
toggleCall(onCall, conferenceRoomId) {
|
||||
toggleCall(onCall, conference, admin) {
|
||||
this.__admin = admin;
|
||||
if (onCall === true) this.leaveCall();
|
||||
else this.boot(conferenceRoomId);
|
||||
else this.boot(conference);
|
||||
}
|
||||
async boot(conferenceRoomId) {
|
||||
if (
|
||||
this.__accessToken !== undefined ||
|
||||
this.__refreshToken !== undefined ||
|
||||
this.__accessToken !== null ||
|
||||
this.__refreshToken !== null
|
||||
)
|
||||
await this.initialize();
|
||||
toggleMute(muted) {
|
||||
VoxeetSDK.conference.mute(VoxeetSDK.session.participant, !muted);
|
||||
this.listener({ muted: !muted });
|
||||
}
|
||||
async boot(conference) {
|
||||
await this.initialize();
|
||||
const opened = await this.openSession();
|
||||
if (opened && conferenceRoomId) this.joinCall(conferenceRoomId);
|
||||
else if (opened && !conferenceRoomId) this.startCall(conferenceRoomId);
|
||||
else console.log('Unable to open Voxeet Session');
|
||||
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 console.error('Unable to open Voxeet Session');
|
||||
}
|
||||
async startCall(conferenceRoomId) {
|
||||
VoxeetSDK.conference.create({ alias: window.localStorage.getItem('watchPartyRoomId') });
|
||||
async startCall() {
|
||||
if (this.__admin === true) {
|
||||
VoxeetSDK.conference
|
||||
.create({ alias: 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) => console.error('Update Voxeet API Error', err));
|
||||
if (updatedVoxeet === true) {
|
||||
this.joinCall(conference);
|
||||
} else {
|
||||
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);
|
||||
if (!joined) {
|
||||
this.startCall();
|
||||
}
|
||||
}
|
||||
async leaveCall() {
|
||||
VoxeetSDK.conference.leave().then(() => {
|
||||
VoxeetSDK.session.close();
|
||||
this.listener({ onCall: false });
|
||||
});
|
||||
}
|
||||
async joinCall(conferenceRoomId) {}
|
||||
async leaveCall() {}
|
||||
async openSession() {
|
||||
if (!this.__sessionInitialized) {
|
||||
try {
|
||||
await VoxeetSDK.session.open({
|
||||
externalId: window.localStorage.getItem('watchPartyUserUid'),
|
||||
name: window.localStorage.getItem('watchPartyUserName'),
|
||||
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 initialize() {
|
||||
GetVoxeetTokenApi()
|
||||
.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, this.refreshToken);
|
||||
} else {
|
||||
console.error('Get Voxeet Token API Result Error', tokenResult);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Get Voxeet Token API error', err);
|
||||
});
|
||||
VoxeetSDK.conference.on('joined', () => {
|
||||
this.listener({ onCall: true });
|
||||
});
|
||||
|
||||
VoxeetSDK.conference.on('left', () => {
|
||||
this.listener({ onCall: false });
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
VoxeetSDK.conference.on('streamRemoved', (participant, stream) => {
|
||||
document.getElementById(participant.id).remove();
|
||||
});
|
||||
|
||||
if (
|
||||
this.__accessToken !== undefined ||
|
||||
this.__refreshToken !== undefined ||
|
||||
this.__accessToken !== null ||
|
||||
this.__refreshToken !== null
|
||||
) {
|
||||
await GetVoxeetTokenApi()
|
||||
.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) => {
|
||||
@@ -79,5 +162,6 @@ export default class CallManager {
|
||||
updateTokens(aT, rT) {
|
||||
this.__accessToken = aT;
|
||||
this.__refreshToken = rT;
|
||||
updateLocalData({ vaT: aT, vrT: rT });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user