2014-08-27 11:57:54 -06:00
|
|
|
/*
|
2014-09-03 10:29:13 -06:00
|
|
|
Copyright 2014 OpenMarket Ltd
|
2014-08-27 11:57:54 -06:00
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
angular.module('matrixPhoneService', [])
|
2014-08-28 12:03:34 -06:00
|
|
|
.factory('matrixPhoneService', ['$rootScope', '$injector', 'matrixService', 'eventHandlerService', function MatrixPhoneService($rootScope, $injector, matrixService, eventHandlerService) {
|
2014-08-27 11:57:54 -06:00
|
|
|
var matrixPhoneService = function() {
|
2014-08-28 12:03:34 -06:00
|
|
|
};
|
2014-08-27 11:57:54 -06:00
|
|
|
|
2014-08-29 06:23:01 -06:00
|
|
|
matrixPhoneService.INCOMING_CALL_EVENT = "INCOMING_CALL_EVENT";
|
2014-09-11 08:23:06 -06:00
|
|
|
matrixPhoneService.REPLACED_CALL_EVENT = "REPLACED_CALL_EVENT";
|
2014-08-27 11:57:54 -06:00
|
|
|
matrixPhoneService.allCalls = {};
|
2014-09-16 07:46:13 -06:00
|
|
|
// a place to save candidates that come in for calls we haven't got invites for yet (when paginating backwards)
|
|
|
|
matrixPhoneService.candidatesByCall = {};
|
2014-08-27 11:57:54 -06:00
|
|
|
|
2014-08-28 12:03:34 -06:00
|
|
|
matrixPhoneService.callPlaced = function(call) {
|
2014-08-27 11:57:54 -06:00
|
|
|
matrixPhoneService.allCalls[call.call_id] = call;
|
|
|
|
};
|
|
|
|
|
2014-08-29 06:23:01 -06:00
|
|
|
$rootScope.$on(eventHandlerService.CALL_EVENT, function(ngEvent, event, isLive) {
|
2014-08-27 11:57:54 -06:00
|
|
|
if (event.user_id == matrixService.config().user_id) return;
|
2014-09-16 07:46:13 -06:00
|
|
|
|
2014-08-27 11:57:54 -06:00
|
|
|
var msg = event.content;
|
2014-09-16 07:46:13 -06:00
|
|
|
|
2014-08-29 06:23:01 -06:00
|
|
|
if (event.type == 'm.call.invite') {
|
2014-09-16 07:46:13 -06:00
|
|
|
if (event.age == undefined || msg.lifetime == undefined) {
|
|
|
|
// if the event doesn't have either an age (the HS is too old) or a lifetime
|
|
|
|
// (the sending client was too old when it sent it) then fall back to old behaviour
|
|
|
|
if (!isLive) return; // until matrix supports expiring messages
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.age > msg.lifetime) {
|
|
|
|
console.log("Ignoring expired call event of type "+event.type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var call = undefined;
|
|
|
|
if (!isLive) {
|
|
|
|
// if this event wasn't live then this call may already be over
|
|
|
|
call = matrixPhoneService.allCalls[msg.call_id];
|
|
|
|
if (call && call.state == 'ended') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 12:03:34 -06:00
|
|
|
var MatrixCall = $injector.get('MatrixCall');
|
2014-08-27 11:57:54 -06:00
|
|
|
var call = new MatrixCall(event.room_id);
|
|
|
|
call.call_id = msg.call_id;
|
2014-09-16 08:25:51 -06:00
|
|
|
call.initWithInvite(event);
|
2014-08-27 11:57:54 -06:00
|
|
|
matrixPhoneService.allCalls[call.call_id] = call;
|
2014-09-11 08:23:06 -06:00
|
|
|
|
2014-09-16 07:46:13 -06:00
|
|
|
// if we stashed candidate events for that call ID, play them back now
|
|
|
|
if (!isLive && matrixPhoneService.candidatesByCall[call.call_id] != undefined) {
|
|
|
|
for (var i = 0; i < matrixPhoneService.candidatesByCall[call.call_id].length; ++i) {
|
|
|
|
call.gotRemoteIceCandidate(matrixPhoneService.candidatesByCall[call.call_id][i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 08:23:06 -06:00
|
|
|
// Were we trying to call that user (room)?
|
|
|
|
var existingCall;
|
|
|
|
var callIds = Object.keys(matrixPhoneService.allCalls);
|
|
|
|
for (var i = 0; i < callIds.length; ++i) {
|
|
|
|
var thisCallId = callIds[i];
|
|
|
|
var thisCall = matrixPhoneService.allCalls[thisCallId];
|
|
|
|
|
|
|
|
if (call.room_id == thisCall.room_id && thisCall.direction == 'outbound'
|
2014-09-12 07:06:35 -06:00
|
|
|
&& (thisCall.state == 'wait_local_media' || thisCall.state == 'create_offer' || thisCall.state == 'invite_sent')) {
|
2014-09-11 08:23:06 -06:00
|
|
|
existingCall = thisCall;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingCall) {
|
2014-09-12 07:06:35 -06:00
|
|
|
// If we've only got to wait_local_media or create_offer and we've got an invite,
|
|
|
|
// pick the incoming call because we know we haven't sent our invite yet
|
|
|
|
// otherwise, pick whichever call has the lowest call ID (by string comparison)
|
|
|
|
if (existingCall.state == 'wait_local_media' || existingCall.state == 'create_offer' || existingCall.call_id > call.call_id) {
|
2014-09-11 08:23:06 -06:00
|
|
|
console.log("Glare detected: answering incoming call "+call.call_id+" and canceling outgoing call "+existingCall.call_id);
|
|
|
|
existingCall.replacedBy(call);
|
|
|
|
call.answer();
|
|
|
|
$rootScope.$broadcast(matrixPhoneService.REPLACED_CALL_EVENT, existingCall, call);
|
2014-09-12 07:06:35 -06:00
|
|
|
} else {
|
|
|
|
console.log("Glare detected: rejecting incoming call "+call.call_id+" and keeping outgoing call "+existingCall.call_id);
|
|
|
|
call.hangup();
|
2014-09-11 08:23:06 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$rootScope.$broadcast(matrixPhoneService.INCOMING_CALL_EVENT, call);
|
|
|
|
}
|
2014-08-29 06:23:01 -06:00
|
|
|
} else if (event.type == 'm.call.answer') {
|
2014-08-28 12:03:34 -06:00
|
|
|
var call = matrixPhoneService.allCalls[msg.call_id];
|
|
|
|
if (!call) {
|
2014-09-11 08:23:06 -06:00
|
|
|
console.log("Got answer for unknown call ID "+msg.call_id);
|
2014-08-28 12:03:34 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
call.receivedAnswer(msg);
|
2014-09-12 11:16:24 -06:00
|
|
|
} else if (event.type == 'm.call.candidates') {
|
2014-08-28 12:03:34 -06:00
|
|
|
var call = matrixPhoneService.allCalls[msg.call_id];
|
2014-09-16 07:46:13 -06:00
|
|
|
if (!call && isLive) {
|
2014-09-12 11:16:24 -06:00
|
|
|
console.log("Got candidates for unknown call ID "+msg.call_id);
|
2014-08-27 11:57:54 -06:00
|
|
|
return;
|
2014-09-16 07:46:13 -06:00
|
|
|
} else if (!call) {
|
|
|
|
if (matrixPhoneService.candidatesByCall[msg.call_id] == undefined) {
|
|
|
|
matrixPhoneService.candidatesByCall[msg.call_id] = [];
|
|
|
|
}
|
|
|
|
matrixPhoneService.candidatesByCall[msg.call_id] = matrixPhoneService.candidatesByCall[msg.call_id].concat(msg.candidates);
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < msg.candidates.length; ++i) {
|
|
|
|
call.gotRemoteIceCandidate(msg.candidates[i]);
|
|
|
|
}
|
2014-09-12 11:16:24 -06:00
|
|
|
}
|
2014-08-29 06:23:01 -06:00
|
|
|
} else if (event.type == 'm.call.hangup') {
|
2014-08-28 12:03:34 -06:00
|
|
|
var call = matrixPhoneService.allCalls[msg.call_id];
|
2014-09-16 07:46:13 -06:00
|
|
|
if (!call && isLive) {
|
2014-09-11 08:23:06 -06:00
|
|
|
console.log("Got hangup for unknown call ID "+msg.call_id);
|
2014-09-16 07:46:13 -06:00
|
|
|
} else if (!call) {
|
|
|
|
// if not live, store the fact that the call has ended because we're probably getting events backwards so
|
|
|
|
// the hangup will come before the invite
|
|
|
|
var MatrixCall = $injector.get('MatrixCall');
|
|
|
|
var call = new MatrixCall(event.room_id);
|
|
|
|
call.call_id = msg.call_id;
|
2014-09-16 08:25:51 -06:00
|
|
|
call.initWithHangup(event);
|
2014-09-16 07:46:13 -06:00
|
|
|
matrixPhoneService.allCalls[msg.call_id] = call;
|
|
|
|
} else {
|
|
|
|
call.onHangupReceived();
|
|
|
|
delete(matrixPhoneService.allCalls[msg.call_id]);
|
2014-08-28 12:03:34 -06:00
|
|
|
}
|
2014-08-27 11:57:54 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return matrixPhoneService;
|
|
|
|
}]);
|