summary refs log tree commit diff
path: root/webclient/components/matrix/matrix-phone-service.js
diff options
context:
space:
mode:
authorDavid Baker <dbkr@matrix.org>2014-09-11 15:23:06 +0100
committerDavid Baker <dbkr@matrix.org>2014-09-11 15:24:18 +0100
commitfb082cf50ff1c48f651eb46fdf7398bacb85ad16 (patch)
tree612d909a44fb86e75da3395b60a62090939d4bfa /webclient/components/matrix/matrix-phone-service.js
parentAdded support of copy/paste of multi lines content (diff)
downloadsynapse-fb082cf50ff1c48f651eb46fdf7398bacb85ad16.tar.xz
start towards glare support (currently not much better but no worse than before) including fixing a lot of self/var self/this fails that caused chaos when we started to have more than one call in play.
Diffstat (limited to '')
-rw-r--r--webclient/components/matrix/matrix-phone-service.js38
1 files changed, 33 insertions, 5 deletions
diff --git a/webclient/components/matrix/matrix-phone-service.js b/webclient/components/matrix/matrix-phone-service.js
index ca86b473e7..26be4bf2ad 100644
--- a/webclient/components/matrix/matrix-phone-service.js
+++ b/webclient/components/matrix/matrix-phone-service.js
@@ -22,6 +22,7 @@ angular.module('matrixPhoneService', [])
     };
 
     matrixPhoneService.INCOMING_CALL_EVENT = "INCOMING_CALL_EVENT";
+    matrixPhoneService.REPLACED_CALL_EVENT = "REPLACED_CALL_EVENT";
     matrixPhoneService.allCalls = {};
 
     matrixPhoneService.callPlaced = function(call) {
@@ -38,29 +39,56 @@ angular.module('matrixPhoneService', [])
             call.call_id = msg.call_id;
             call.initWithInvite(msg);
             matrixPhoneService.allCalls[call.call_id] = call;
-            $rootScope.$broadcast(matrixPhoneService.INCOMING_CALL_EVENT, 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 == 'invite_sent' || thisCall.state == 'create_offer')) {
+                    existingCall = thisCall;
+                    break;
+                }
+            }
+
+            if (existingCall) {
+                if (existingCall.call_id < call.call_id) {
+                    console.log("Glare detected: rejecting incoming call "+call.call_id+" and keeping outgoing call "+existingCall.call_id);
+                    call.hangup();
+                } else {
+                    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 {
+                $rootScope.$broadcast(matrixPhoneService.INCOMING_CALL_EVENT, call);
+            }
         } else if (event.type == 'm.call.answer') {
             var call = matrixPhoneService.allCalls[msg.call_id];
             if (!call) {
-                console.trace("Got answer for unknown call ID "+msg.call_id);
+                console.log("Got answer for unknown call ID "+msg.call_id);
                 return;
             }
             call.receivedAnswer(msg);
         } else if (event.type == 'm.call.candidate') {
             var call = matrixPhoneService.allCalls[msg.call_id];
             if (!call) {
-                console.trace("Got candidate for unknown call ID "+msg.call_id);
+                console.log("Got candidate for unknown call ID "+msg.call_id);
                 return;
             }
             call.gotRemoteIceCandidate(msg.candidate);
         } else if (event.type == 'm.call.hangup') {
             var call = matrixPhoneService.allCalls[msg.call_id];
             if (!call) {
-                console.trace("Got hangup for unknown call ID "+msg.call_id);
+                console.log("Got hangup for unknown call ID "+msg.call_id);
                 return;
             }
             call.onHangupReceived();
-            matrixPhoneService.allCalls[msg.call_id] = undefined;
+            delete(matrixPhoneService.allCalls[msg.call_id]);
         }
     });