2015-07-15 11:52:23 -04:00
|
|
|
/*
|
|
|
|
Copyright 2015 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Manages a list of all the currently active calls.
|
|
|
|
*
|
2015-07-15 12:48:26 -04:00
|
|
|
* This handler dispatches when voip calls are added/updated/removed from this list:
|
2015-07-15 11:52:23 -04:00
|
|
|
* {
|
|
|
|
* action: 'call_state'
|
2015-07-16 07:57:59 -04:00
|
|
|
* room_id: <room ID of the call>
|
2015-07-15 11:52:23 -04:00
|
|
|
* }
|
|
|
|
*
|
2015-07-16 07:57:59 -04:00
|
|
|
* To know the state of the call, this handler exposes a getter to
|
2015-07-15 11:52:23 -04:00
|
|
|
* obtain the call for a room:
|
2015-07-16 07:57:59 -04:00
|
|
|
* var call = CallHandler.getCall(roomId)
|
|
|
|
* var state = call.call_state; // ringing|ringback|connected|ended|busy|stop_ringback|stop_ringing
|
2015-07-15 11:52:23 -04:00
|
|
|
*
|
|
|
|
* This handler listens for and handles the following actions:
|
|
|
|
* {
|
|
|
|
* action: 'place_call',
|
|
|
|
* type: 'voice|video',
|
|
|
|
* room_id: <room that the place call button was pressed in>
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* action: 'incoming_call'
|
|
|
|
* call: MatrixCall
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* action: 'hangup'
|
|
|
|
* room_id: <room that the hangup button was pressed in>
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* action: 'answer'
|
|
|
|
* room_id: <room that the answer button was pressed in>
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
var MatrixClientPeg = require("./MatrixClientPeg");
|
2015-07-20 11:26:47 -04:00
|
|
|
var Modal = require("./Modal");
|
|
|
|
var ComponentBroker = require('./ComponentBroker');
|
|
|
|
var ErrorDialog = ComponentBroker.get("organisms/ErrorDialog");
|
2015-07-15 11:52:23 -04:00
|
|
|
var Matrix = require("matrix-js-sdk");
|
|
|
|
var dis = require("./dispatcher");
|
|
|
|
|
|
|
|
var calls = {
|
|
|
|
//room_id: MatrixCall
|
|
|
|
};
|
|
|
|
|
2015-07-17 12:05:11 -04:00
|
|
|
function play(audioId) {
|
|
|
|
// TODO: Attach an invisible element for this instead
|
|
|
|
// which listens?
|
|
|
|
var audio = document.getElementById(audioId);
|
|
|
|
if (audio) {
|
|
|
|
audio.load();
|
|
|
|
audio.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function pause(audioId) {
|
|
|
|
// TODO: Attach an invisible element for this instead
|
|
|
|
// which listens?
|
|
|
|
var audio = document.getElementById(audioId);
|
|
|
|
if (audio) {
|
|
|
|
audio.pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 11:52:23 -04:00
|
|
|
function _setCallListeners(call) {
|
|
|
|
call.on("error", function(err) {
|
|
|
|
console.error("Call error: %s", err);
|
|
|
|
console.error(err.stack);
|
|
|
|
call.hangup();
|
2015-07-16 06:05:09 -04:00
|
|
|
_setCallState(undefined, call.roomId, "ended");
|
2015-07-15 11:52:23 -04:00
|
|
|
});
|
|
|
|
call.on("hangup", function() {
|
2015-07-16 06:05:09 -04:00
|
|
|
_setCallState(undefined, call.roomId, "ended");
|
|
|
|
});
|
|
|
|
// map web rtc states to dummy UI state
|
|
|
|
// ringing|ringback|connected|ended|busy|stop_ringback|stop_ringing
|
|
|
|
call.on("state", function(newState, oldState) {
|
|
|
|
if (newState === "ringing") {
|
|
|
|
_setCallState(call, call.roomId, "ringing");
|
2015-07-17 12:05:11 -04:00
|
|
|
pause("ringbackAudio");
|
2015-07-16 06:05:09 -04:00
|
|
|
}
|
|
|
|
else if (newState === "invite_sent") {
|
|
|
|
_setCallState(call, call.roomId, "ringback");
|
2015-07-17 12:05:11 -04:00
|
|
|
play("ringbackAudio");
|
2015-07-16 06:05:09 -04:00
|
|
|
}
|
|
|
|
else if (newState === "ended" && oldState === "connected") {
|
|
|
|
_setCallState(call, call.roomId, "ended");
|
2015-07-17 12:05:11 -04:00
|
|
|
pause("ringbackAudio");
|
|
|
|
play("callendAudio");
|
2015-07-16 06:05:09 -04:00
|
|
|
}
|
|
|
|
else if (newState === "ended" && oldState === "invite_sent" &&
|
|
|
|
(call.hangupParty === "remote" ||
|
|
|
|
(call.hangupParty === "local" && call.hangupReason === "invite_timeout")
|
|
|
|
)) {
|
|
|
|
_setCallState(call, call.roomId, "busy");
|
2015-07-17 12:05:11 -04:00
|
|
|
pause("ringbackAudio");
|
|
|
|
play("busyAudio");
|
2015-07-16 06:05:09 -04:00
|
|
|
}
|
|
|
|
else if (oldState === "invite_sent") {
|
|
|
|
_setCallState(call, call.roomId, "stop_ringback");
|
2015-07-17 12:05:11 -04:00
|
|
|
pause("ringbackAudio");
|
2015-07-16 06:05:09 -04:00
|
|
|
}
|
|
|
|
else if (oldState === "ringing") {
|
|
|
|
_setCallState(call, call.roomId, "stop_ringing");
|
2015-07-17 12:05:11 -04:00
|
|
|
pause("ringbackAudio");
|
2015-07-16 06:05:09 -04:00
|
|
|
}
|
|
|
|
else if (newState === "connected") {
|
|
|
|
_setCallState(call, call.roomId, "connected");
|
2015-07-17 12:05:11 -04:00
|
|
|
pause("ringbackAudio");
|
2015-07-16 06:05:09 -04:00
|
|
|
}
|
2015-07-15 11:52:23 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-16 06:05:09 -04:00
|
|
|
function _setCallState(call, roomId, status) {
|
2015-07-16 06:30:34 -04:00
|
|
|
console.log(
|
|
|
|
"Call state in %s changed to %s (%s)", roomId, status, (call ? call.state : "-")
|
|
|
|
);
|
2015-07-15 11:52:23 -04:00
|
|
|
calls[roomId] = call;
|
2015-07-16 06:05:09 -04:00
|
|
|
if (call) {
|
|
|
|
call.call_state = status;
|
|
|
|
}
|
2015-07-15 11:52:23 -04:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'call_state',
|
2015-07-16 07:57:59 -04:00
|
|
|
room_id: roomId
|
2015-07-15 11:52:23 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dis.register(function(payload) {
|
|
|
|
switch (payload.action) {
|
|
|
|
case 'place_call':
|
|
|
|
if (calls[payload.room_id]) {
|
|
|
|
return; // don't allow >1 call to be placed.
|
|
|
|
}
|
2015-07-16 06:34:39 -04:00
|
|
|
var room = MatrixClientPeg.get().getRoom(payload.room_id);
|
|
|
|
if (!room) {
|
|
|
|
console.error("Room %s does not exist.", payload.room_id);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 11:26:47 -04:00
|
|
|
var members = room.getJoinedMembers();
|
|
|
|
if (members.length !== 2) {
|
|
|
|
var text = members.length === 1 ? "yourself." : "more than 2 people.";
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
|
|
|
description: "You cannot place a call with " + text
|
|
|
|
});
|
2015-07-16 06:34:39 -04:00
|
|
|
console.error(
|
|
|
|
"Fail: There are %s joined members in this room, not 2.",
|
|
|
|
room.getJoinedMembers().length
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-15 11:52:23 -04:00
|
|
|
console.log("Place %s call in %s", payload.type, payload.room_id);
|
|
|
|
var call = Matrix.createNewMatrixCall(
|
|
|
|
MatrixClientPeg.get(), payload.room_id
|
|
|
|
);
|
|
|
|
_setCallListeners(call);
|
2015-07-16 06:05:09 -04:00
|
|
|
_setCallState(call, call.roomId, "ringback");
|
2015-07-15 11:52:23 -04:00
|
|
|
if (payload.type === 'voice') {
|
|
|
|
call.placeVoiceCall();
|
|
|
|
}
|
|
|
|
else if (payload.type === 'video') {
|
|
|
|
call.placeVideoCall(
|
|
|
|
payload.remote_element,
|
|
|
|
payload.local_element
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error("Unknown call type: %s", payload.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'incoming_call':
|
|
|
|
if (calls[payload.call.roomId]) {
|
|
|
|
payload.call.hangup("busy");
|
|
|
|
return; // don't allow >1 call to be received, hangup newer one.
|
|
|
|
}
|
|
|
|
var call = payload.call;
|
|
|
|
_setCallListeners(call);
|
2015-07-16 06:05:09 -04:00
|
|
|
_setCallState(call, call.roomId, "ringing");
|
2015-07-15 11:52:23 -04:00
|
|
|
break;
|
|
|
|
case 'hangup':
|
|
|
|
if (!calls[payload.room_id]) {
|
|
|
|
return; // no call to hangup
|
|
|
|
}
|
|
|
|
calls[payload.room_id].hangup();
|
2015-07-16 06:05:09 -04:00
|
|
|
_setCallState(null, payload.room_id, "ended");
|
2015-07-15 11:52:23 -04:00
|
|
|
break;
|
|
|
|
case 'answer':
|
|
|
|
if (!calls[payload.room_id]) {
|
|
|
|
return; // no call to answer
|
|
|
|
}
|
|
|
|
calls[payload.room_id].answer();
|
2015-07-16 06:05:09 -04:00
|
|
|
_setCallState(calls[payload.room_id], payload.room_id, "connected");
|
2015-07-17 09:25:41 -04:00
|
|
|
dis.dispatch({
|
|
|
|
action: "view_room",
|
|
|
|
room_id: payload.room_id
|
|
|
|
});
|
2015-07-15 11:52:23 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getCall: function(roomId) {
|
|
|
|
return calls[roomId] || null;
|
|
|
|
}
|
|
|
|
};
|