2014-08-27 21:57:54 +04:00
|
|
|
/*
|
2014-09-03 20:29:13 +04:00
|
|
|
Copyright 2014 OpenMarket Ltd
|
2014-08-27 21:57:54 +04: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 22:03:34 +04:00
|
|
|
.factory('matrixPhoneService', ['$rootScope', '$injector', 'matrixService', 'eventHandlerService', function MatrixPhoneService($rootScope, $injector, matrixService, eventHandlerService) {
|
2014-08-27 21:57:54 +04:00
|
|
|
var matrixPhoneService = function() {
|
2014-08-28 22:03:34 +04:00
|
|
|
};
|
2014-08-27 21:57:54 +04:00
|
|
|
|
2014-08-29 16:23:01 +04:00
|
|
|
matrixPhoneService.INCOMING_CALL_EVENT = "INCOMING_CALL_EVENT";
|
2014-08-27 21:57:54 +04:00
|
|
|
matrixPhoneService.allCalls = {};
|
|
|
|
|
2014-08-28 22:03:34 +04:00
|
|
|
matrixPhoneService.callPlaced = function(call) {
|
2014-08-27 21:57:54 +04:00
|
|
|
matrixPhoneService.allCalls[call.call_id] = call;
|
|
|
|
};
|
|
|
|
|
2014-08-29 16:23:01 +04:00
|
|
|
$rootScope.$on(eventHandlerService.CALL_EVENT, function(ngEvent, event, isLive) {
|
2014-08-27 21:57:54 +04:00
|
|
|
if (!isLive) return; // until matrix supports expiring messages
|
|
|
|
if (event.user_id == matrixService.config().user_id) return;
|
|
|
|
var msg = event.content;
|
2014-08-29 16:23:01 +04:00
|
|
|
if (event.type == 'm.call.invite') {
|
2014-08-28 22:03:34 +04:00
|
|
|
var MatrixCall = $injector.get('MatrixCall');
|
2014-08-27 21:57:54 +04:00
|
|
|
var call = new MatrixCall(event.room_id);
|
|
|
|
call.call_id = msg.call_id;
|
2014-08-28 22:03:34 +04:00
|
|
|
call.initWithInvite(msg);
|
2014-08-27 21:57:54 +04:00
|
|
|
matrixPhoneService.allCalls[call.call_id] = call;
|
2014-08-29 16:23:01 +04:00
|
|
|
$rootScope.$broadcast(matrixPhoneService.INCOMING_CALL_EVENT, call);
|
|
|
|
} else if (event.type == 'm.call.answer') {
|
2014-08-28 22:03:34 +04:00
|
|
|
var call = matrixPhoneService.allCalls[msg.call_id];
|
|
|
|
if (!call) {
|
|
|
|
console.trace("Got answer for unknown call ID "+msg.call_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
call.receivedAnswer(msg);
|
2014-08-29 16:23:01 +04:00
|
|
|
} else if (event.type == 'm.call.candidate') {
|
2014-08-28 22:03:34 +04:00
|
|
|
var call = matrixPhoneService.allCalls[msg.call_id];
|
2014-08-27 21:57:54 +04:00
|
|
|
if (!call) {
|
|
|
|
console.trace("Got candidate for unknown call ID "+msg.call_id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
call.gotRemoteIceCandidate(msg.candidate);
|
2014-08-29 16:23:01 +04:00
|
|
|
} else if (event.type == 'm.call.hangup') {
|
2014-08-28 22:03:34 +04:00
|
|
|
var call = matrixPhoneService.allCalls[msg.call_id];
|
|
|
|
if (!call) {
|
|
|
|
console.trace("Got hangup for unknown call ID "+msg.call_id);
|
|
|
|
return;
|
|
|
|
}
|
2014-08-29 14:29:36 +04:00
|
|
|
call.onHangupReceived();
|
2014-08-28 22:03:34 +04:00
|
|
|
matrixPhoneService.allCalls[msg.call_id] = undefined;
|
2014-08-27 21:57:54 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return matrixPhoneService;
|
|
|
|
}]);
|