0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-11-09 11:32:01 +01:00
synapse/webclient/components/matrix/matrix-phone-service.js

100 lines
4.4 KiB
JavaScript
Raw Normal View History

2014-08-27 19:57:54 +02:00
/*
Copyright 2014 OpenMarket Ltd
2014-08-27 19:57:54 +02: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 20:03:34 +02:00
.factory('matrixPhoneService', ['$rootScope', '$injector', 'matrixService', 'eventHandlerService', function MatrixPhoneService($rootScope, $injector, matrixService, eventHandlerService) {
2014-08-27 19:57:54 +02:00
var matrixPhoneService = function() {
2014-08-28 20:03:34 +02:00
};
2014-08-27 19:57:54 +02:00
matrixPhoneService.INCOMING_CALL_EVENT = "INCOMING_CALL_EVENT";
matrixPhoneService.REPLACED_CALL_EVENT = "REPLACED_CALL_EVENT";
2014-08-27 19:57:54 +02:00
matrixPhoneService.allCalls = {};
2014-08-28 20:03:34 +02:00
matrixPhoneService.callPlaced = function(call) {
2014-08-27 19:57:54 +02:00
matrixPhoneService.allCalls[call.call_id] = call;
};
$rootScope.$on(eventHandlerService.CALL_EVENT, function(ngEvent, event, isLive) {
2014-08-27 19:57:54 +02:00
if (!isLive) return; // until matrix supports expiring messages
if (event.user_id == matrixService.config().user_id) return;
var msg = event.content;
if (event.type == 'm.call.invite') {
2014-08-28 20:03:34 +02:00
var MatrixCall = $injector.get('MatrixCall');
2014-08-27 19:57:54 +02:00
var call = new MatrixCall(event.room_id);
call.call_id = msg.call_id;
2014-08-28 20:03:34 +02:00
call.initWithInvite(msg);
2014-08-27 19:57:54 +02:00
matrixPhoneService.allCalls[call.call_id] = call;
// 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'
&& (thisCall.state == 'wait_local_media' || thisCall.state == 'create_offer' || thisCall.state == 'invite_sent')) {
existingCall = thisCall;
break;
}
}
if (existingCall) {
// 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) {
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);
} else {
console.log("Glare detected: rejecting incoming call "+call.call_id+" and keeping outgoing call "+existingCall.call_id);
call.hangup();
}
} else {
$rootScope.$broadcast(matrixPhoneService.INCOMING_CALL_EVENT, call);
}
} else if (event.type == 'm.call.answer') {
2014-08-28 20:03:34 +02:00
var call = matrixPhoneService.allCalls[msg.call_id];
if (!call) {
console.log("Got answer for unknown call ID "+msg.call_id);
2014-08-28 20:03:34 +02:00
return;
}
call.receivedAnswer(msg);
} else if (event.type == 'm.call.candidate') {
2014-08-28 20:03:34 +02:00
var call = matrixPhoneService.allCalls[msg.call_id];
2014-08-27 19:57:54 +02:00
if (!call) {
console.log("Got candidate for unknown call ID "+msg.call_id);
2014-08-27 19:57:54 +02:00
return;
}
call.gotRemoteIceCandidate(msg.candidate);
} else if (event.type == 'm.call.hangup') {
2014-08-28 20:03:34 +02:00
var call = matrixPhoneService.allCalls[msg.call_id];
if (!call) {
console.log("Got hangup for unknown call ID "+msg.call_id);
2014-08-28 20:03:34 +02:00
return;
}
call.onHangupReceived();
delete(matrixPhoneService.allCalls[msg.call_id]);
2014-08-27 19:57:54 +02:00
}
});
return matrixPhoneService;
}]);