2020-04-12 14:25:42 +00:00
|
|
|
'use strict'
|
|
|
|
const User = use('App/Models/User');
|
|
|
|
const UserChildUtils = use('App/Utils/UserChildUtils');
|
2020-04-29 23:45:50 +00:00
|
|
|
const CallUtils = use('App/Utils/CallUtils');
|
2020-04-13 01:52:08 +00:00
|
|
|
const Child = use('App/Models/Child');
|
2020-04-12 14:25:42 +00:00
|
|
|
const IceServer = use('App/Models/IceServer');
|
2020-04-13 01:28:33 +00:00
|
|
|
const UserChannel = use('App/Controllers/Ws/UserChannelController')
|
2020-04-12 14:25:42 +00:00
|
|
|
const calls = {};
|
|
|
|
|
|
|
|
class SignalingController {
|
|
|
|
constructor({socket, request, auth, call}) {
|
|
|
|
this.callId = socket.topic.split(':')[1];
|
|
|
|
this.user = auth.user;
|
|
|
|
this.socket = socket;
|
|
|
|
this.request = request;
|
2020-04-13 01:52:08 +00:00
|
|
|
this.register(call)
|
|
|
|
.then(_ => {
|
|
|
|
console.log(`User #${this.user.id} connected to call ${this.callId}`);
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
2020-04-13 01:28:33 +00:00
|
|
|
async register(callModel) {
|
2020-04-13 01:52:08 +00:00
|
|
|
if (!calls[this.callId])
|
2020-04-12 23:33:24 +00:00
|
|
|
calls[this.callId] =
|
|
|
|
new CallSession(callModel, this.onCallEnded.bind(this));
|
2020-04-13 01:52:08 +00:00
|
|
|
else
|
2020-04-12 14:25:42 +00:00
|
|
|
console.log(`Call #${this.callId} Already Found`);
|
|
|
|
const callSession = calls[this.callId];
|
2020-04-14 01:40:10 +00:00
|
|
|
const success = await callSession.registerUser(this.user, this.socket)
|
2020-04-13 01:52:08 +00:00
|
|
|
if (!success) throw new Error('Invalid User');
|
|
|
|
return true;
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
2020-04-12 23:33:24 +00:00
|
|
|
onCallEnded(callId) {
|
|
|
|
console.log(`Call ${callId} Ended`);
|
|
|
|
delete calls[callId];
|
|
|
|
}
|
2020-04-12 14:25:42 +00:00
|
|
|
onClose() {
|
2020-04-12 23:33:24 +00:00
|
|
|
calls[this.callId].removeUser(this.user);
|
2020-04-12 14:25:42 +00:00
|
|
|
console.log(`User #${this.user.id} left call ${this.callId}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CallSession {
|
|
|
|
// states: NEW -> STARTED -> IN_PROGRESS -> ENDED
|
2020-04-12 23:33:24 +00:00
|
|
|
constructor(callModel, onCallEndedCallback) {
|
|
|
|
this.onCallEndedCallback = onCallEndedCallback;
|
2020-04-12 14:25:42 +00:00
|
|
|
this.callId = callModel.id;
|
|
|
|
this.callModel = callModel;
|
2020-04-29 23:45:50 +00:00
|
|
|
this.callBooks = null;
|
2020-05-01 05:55:26 +00:00
|
|
|
this.hostId = callModel.guest_id;
|
2020-04-12 14:25:42 +00:00
|
|
|
this.state = callModel.state;
|
2020-04-28 03:04:09 +00:00
|
|
|
this.sessionState = {page: 'lobby', activity: {type: null, model: null}};
|
|
|
|
this.parent = {
|
|
|
|
id: callModel.parent_id,
|
|
|
|
socket: null,
|
|
|
|
userModel: null,
|
|
|
|
isParent: true
|
|
|
|
};
|
|
|
|
this.guest = {
|
|
|
|
id: callModel.guest_id,
|
|
|
|
socket: null,
|
|
|
|
userModel: null,
|
|
|
|
isParent: false
|
|
|
|
};
|
2020-04-12 14:25:42 +00:00
|
|
|
this.startTime = Date.now();
|
2020-04-12 23:33:24 +00:00
|
|
|
this.heartbeat =
|
|
|
|
setInterval(this.onHeartbeat.bind(this), 1000); // Every second
|
2020-04-28 03:04:09 +00:00
|
|
|
this.userMap = new Map(); // Reference to this.parent/guest by userId;
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
|
|
|
onHeartbeat() {
|
2020-04-12 23:33:24 +00:00
|
|
|
const now = Date.now();
|
|
|
|
const elapsed = ((now - this.startTime) / 60000).toPrecision(1);
|
|
|
|
const newStateTimeout = 5; // 5 min
|
|
|
|
const startedStateTimeout = 10; // 10 min
|
2020-04-13 01:52:08 +00:00
|
|
|
// console.log(`Heartbeat for call #${this.callId} State: ${
|
|
|
|
// this.state}, time-elapsed: ${(elapsed)}min`);
|
2020-04-12 23:33:24 +00:00
|
|
|
if (this.state === 'ENDED') return this.endCall();
|
|
|
|
if (this.state === 'NEW' && elapsed >= newStateTimeout)
|
|
|
|
return this.endCall();
|
|
|
|
if (this.state === 'STARTED' && elapsed >= startedStateTimeout)
|
|
|
|
return this.endCall();
|
|
|
|
}
|
|
|
|
removeUser(user) {
|
2020-04-28 03:04:09 +00:00
|
|
|
let userToRemove = this.userMap.get(user.id);
|
|
|
|
userToRemove.userModel = null;
|
|
|
|
userToRemove.socket = null;
|
|
|
|
this.userMap.delete(user.id);
|
2020-04-12 23:33:24 +00:00
|
|
|
this.updateState();
|
|
|
|
if (this.state === 'ENDED') this.endCall();
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
|
|
|
async registerUser(user, socket) {
|
2020-04-28 03:04:09 +00:00
|
|
|
if (!this.child) this.child = await this.callModel.child().fetch();
|
2020-04-29 23:45:50 +00:00
|
|
|
if (!this.callBooks)
|
|
|
|
this.callBooks = await CallUtils.getBooks(
|
|
|
|
this.callModel.parent_id, this.callModel.guest_id);
|
2020-04-28 03:04:09 +00:00
|
|
|
let isParent = this.parent.id === user.id;
|
|
|
|
let peerId = isParent ? this.guest.id : this.parent.id;
|
|
|
|
if (isParent) {
|
|
|
|
this.parent.userModel = user;
|
|
|
|
this.parent.socket = socket;
|
|
|
|
this.userMap.set(user.id, this.parent);
|
|
|
|
} else {
|
|
|
|
this.guest.userModel = user;
|
|
|
|
this.guest.socket = socket;
|
|
|
|
this.userMap.set(user.id, this.guest);
|
|
|
|
peerId = this.parent.id;
|
|
|
|
}
|
|
|
|
|
2020-04-12 14:25:42 +00:00
|
|
|
socket.on('wrtc:sdp:offer', this.onSdpOffer.bind(this));
|
|
|
|
socket.on('wrtc:sdp:answer', this.onSdpAnswer.bind(this));
|
|
|
|
socket.on('wrtc:ice', this.onIceCandidate.bind(this));
|
2020-04-14 15:06:09 +00:00
|
|
|
socket.on('call:host:changed', this.onHostChanged.bind(this));
|
2020-04-30 02:34:14 +00:00
|
|
|
socket.on('call:view:lobby', this.onCallViewLobby.bind(this));
|
|
|
|
socket.on('call:view:book', this.onCallViewBook.bind(this));
|
2020-04-28 03:04:09 +00:00
|
|
|
socket.on('book:action:flip-page', this.onActionBookFlip.bind(this));
|
|
|
|
|
|
|
|
await this.updateState();
|
2020-04-13 01:28:33 +00:00
|
|
|
if (this.state === 'STARTED') {
|
2020-04-28 03:04:09 +00:00
|
|
|
await this.sendStandby(socket, user.id, peerId);
|
2020-04-13 01:28:33 +00:00
|
|
|
// Send event to other user about the call
|
2020-04-28 03:04:09 +00:00
|
|
|
console.log(`trying to find peer's ${peerId} channel...`);
|
|
|
|
const otherUserChannel = UserChannel.getUserChannel(peerId);
|
2020-04-13 01:52:08 +00:00
|
|
|
if (otherUserChannel) {
|
|
|
|
// console.log(otherUserChannel);
|
2020-04-28 03:04:09 +00:00
|
|
|
console.log(`Sending notification to peer ${peerId}`);
|
2020-04-13 01:52:08 +00:00
|
|
|
const payload = {callId: this.callId, child: this.child.toJSON()};
|
|
|
|
console.dir(payload);
|
|
|
|
otherUserChannel.emit('call:incoming', payload);
|
|
|
|
}
|
2020-04-28 03:04:09 +00:00
|
|
|
} else if (this.state === 'IN_PROGRESS') {
|
|
|
|
await this.sendStart(socket, user.id, peerId);
|
|
|
|
}
|
2020-04-12 14:25:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-12 23:33:24 +00:00
|
|
|
endCall() {
|
|
|
|
this.state = 'ENDED';
|
|
|
|
if (this.callModel.state != 'ENDED') {
|
|
|
|
this.callModel.state = this.state;
|
|
|
|
this.callModel.save();
|
|
|
|
}
|
2020-04-28 03:04:09 +00:00
|
|
|
if (this.parent.socket) this.parent.socket.close();
|
|
|
|
if (this.guest.socket) this.guest.socket.close();
|
2020-04-12 23:33:24 +00:00
|
|
|
clearInterval(this.heartbeat);
|
|
|
|
this.onCallEndedCallback(this.callId);
|
|
|
|
}
|
2020-04-14 15:06:09 +00:00
|
|
|
|
2020-04-28 03:04:09 +00:00
|
|
|
async sendStandby(socket, userId, peerId) {
|
|
|
|
console.log(`Call #${this.callId} sendStandby -> ${userId}`);
|
2020-04-12 14:25:42 +00:00
|
|
|
const iceServers = (await IceServer.all()).rows.map(i => i.toJSON());
|
2020-04-28 03:04:09 +00:00
|
|
|
console.log(await this.callModel.parent().fetch());
|
2020-04-14 15:06:09 +00:00
|
|
|
socket.emit('call:standby', {
|
|
|
|
iceServers,
|
2020-04-28 03:04:09 +00:00
|
|
|
peerId,
|
2020-04-29 23:45:50 +00:00
|
|
|
books: this.callBooks,
|
2020-04-14 15:06:09 +00:00
|
|
|
child: this.child.toJSON(),
|
2020-04-28 03:04:09 +00:00
|
|
|
users: await Promise.all(
|
|
|
|
[this.callModel.parent().fetch(), this.callModel.guest().fetch()]),
|
2020-04-14 15:06:09 +00:00
|
|
|
hostId: this.hostId
|
|
|
|
});
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
2020-04-28 03:04:09 +00:00
|
|
|
async sendStart(socket, userId, peerId) {
|
|
|
|
console.log(`Call #${this.callId} sendStart -> ${userId}`);
|
2020-04-12 14:25:42 +00:00
|
|
|
const iceServers = (await IceServer.all()).rows.map(i => i.toJSON());
|
2020-04-14 15:06:09 +00:00
|
|
|
socket.emit('call:start', {
|
|
|
|
iceServers,
|
2020-04-28 03:04:09 +00:00
|
|
|
peerId,
|
2020-04-29 23:45:50 +00:00
|
|
|
books: this.callBooks,
|
2020-04-14 15:06:09 +00:00
|
|
|
child: this.child.toJSON(),
|
2020-04-28 03:04:09 +00:00
|
|
|
users: await Promise.all(
|
|
|
|
[this.callModel.parent().fetch(), this.callModel.guest().fetch()]),
|
2020-04-14 15:06:09 +00:00
|
|
|
hostId: this.hostId
|
|
|
|
});
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
|
|
|
async updateState() {
|
|
|
|
console.log(`Call #${this.callId} state=${this.state}`);
|
|
|
|
switch (this.state) {
|
|
|
|
case 'NEW':
|
|
|
|
if (this.areAllPartnersConnected())
|
|
|
|
this.state = 'IN_PROGRESS';
|
|
|
|
else
|
|
|
|
this.state = 'STARTED';
|
2020-04-12 23:33:24 +00:00
|
|
|
break;
|
2020-04-12 14:25:42 +00:00
|
|
|
case 'STARTED':
|
|
|
|
if (this.areAllPartnersConnected()) this.state = 'IN_PROGRESS';
|
2020-04-12 23:33:24 +00:00
|
|
|
break;
|
|
|
|
case 'IN_PROGRESS':
|
|
|
|
if (!this.areAllPartnersConnected()) this.state = 'ENDED';
|
|
|
|
break;
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
2020-04-12 23:33:24 +00:00
|
|
|
this.callModel.state = this.state;
|
|
|
|
await this.callModel.save();
|
2020-04-12 14:25:42 +00:00
|
|
|
console.log(`Call #${this.callId} state=${this.state}`);
|
|
|
|
}
|
|
|
|
areAllPartnersConnected() {
|
2020-04-28 03:04:09 +00:00
|
|
|
return !!this.parent.socket && !!this.guest.socket;
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
|
|
|
async onIceCandidate(payload) {
|
2020-05-01 05:55:26 +00:00
|
|
|
if (!this.areAllPartnersConnected()) return true;
|
2020-04-28 03:04:09 +00:00
|
|
|
const {peerId, userId, ice} = payload;
|
|
|
|
this.userMap.get(peerId).socket.emit('wrtc:ice', {ice});
|
|
|
|
console.log(`[Signal] [onIceCandidate] ${userId} -> ${peerId}`);
|
2020-04-12 14:25:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
async onSdpOffer(payload) {
|
2020-05-01 05:55:26 +00:00
|
|
|
if (!this.areAllPartnersConnected()) return true;
|
2020-04-28 03:04:09 +00:00
|
|
|
const {peerId, userId, sdp} = payload;
|
|
|
|
this.userMap.get(peerId).socket.emit('wrtc:sdp:offer', {sdp});
|
|
|
|
console.log(`[Signal] [onSdpOffer] ${userId} -> ${peerId}`);
|
2020-04-12 14:25:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onSdpAnswer(payload) {
|
2020-05-01 05:55:26 +00:00
|
|
|
if (!this.areAllPartnersConnected()) return true;
|
2020-04-28 03:04:09 +00:00
|
|
|
const {peerId, userId, sdp} = payload;
|
|
|
|
this.userMap.get(peerId).socket.emit('wrtc:sdp:answer', {sdp});
|
|
|
|
console.log(`[Signal] [onSdpAnswer] ${userId} -> ${peerId}`);
|
2020-04-14 00:56:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
onActionBookFlip(payload) {
|
2020-05-01 05:55:26 +00:00
|
|
|
if (!this.areAllPartnersConnected()) return true;
|
2020-04-28 03:04:09 +00:00
|
|
|
const {peerId, userId, direction} = payload;
|
|
|
|
this.userMap.get(peerId).socket.emit('book:action:flip-page', {direction});
|
|
|
|
console.log(`[Signal] [book] [action] [flip] [${direction}] ${userId} -> ${
|
|
|
|
peerId}`);
|
2020-04-12 14:25:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-30 02:34:14 +00:00
|
|
|
onCallViewLobby(payload) {
|
2020-05-01 05:55:26 +00:00
|
|
|
if (!this.areAllPartnersConnected()) return true;
|
2020-04-30 02:34:14 +00:00
|
|
|
const {peerId, userId, direction} = payload;
|
|
|
|
this.userMap.get(peerId).socket.emit('call:view:lobby', {});
|
|
|
|
console.log(
|
|
|
|
`[Signal] [call] [view] [lobby] [${direction}] ${userId} -> ${peerId}`);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
onCallViewBook(payload) {
|
2020-05-01 05:55:26 +00:00
|
|
|
if (!this.areAllPartnersConnected()) return true;
|
2020-04-30 02:34:14 +00:00
|
|
|
const {peerId, userId, direction, bookId} = payload;
|
|
|
|
this.userMap.get(peerId).socket.emit('call:view:book', {bookId});
|
|
|
|
console.log(
|
|
|
|
`[Signal] [call] [view] [book] [${direction}] ${userId} -> ${peerId}`);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-14 15:06:09 +00:00
|
|
|
async onHostChanged(payload) {
|
2020-04-29 23:45:50 +00:00
|
|
|
const {peerId, userId} = payload;
|
|
|
|
this.hostId = this.hostId === userId ? peerId : userId;
|
|
|
|
console.log('Host: ', this.hostId);
|
|
|
|
this.userMap.get(userId).socket.emit(
|
|
|
|
'call:host:changed', {hostId: this.hostId});
|
2020-05-01 05:55:26 +00:00
|
|
|
if (this.userMap.get(peerId) && this.userMap.get(peerId).socket)
|
2020-04-29 23:45:50 +00:00
|
|
|
this.userMap.get(peerId).socket.emit(
|
|
|
|
'call:host:changed', {hostId: this.hostId});
|
2020-04-14 15:06:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-12 14:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SignalingController
|