From 41d02ab6742643c755f37665c31afa94c0cc8af5 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 29 Aug 2014 11:29:36 +0100 Subject: [PATCH] More basic functionality for voip calls (like hanging up) --- webclient/components/matrix/matrix-call.js | 70 ++++++++++++++++++- .../components/matrix/matrix-phone-service.js | 2 +- webclient/room/room.html | 2 +- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/webclient/components/matrix/matrix-call.js b/webclient/components/matrix/matrix-call.js index a5f2529b8..3aab6413f 100644 --- a/webclient/components/matrix/matrix-call.js +++ b/webclient/components/matrix/matrix-call.js @@ -16,6 +16,25 @@ limitations under the License. 'use strict'; +var forAllVideoTracksOnStream = function(s, f) { + var tracks = s.getVideoTracks(); + for (var i = 0; i < tracks.length; i++) { + f(tracks[i]); + } +} + +var forAllAudioTracksOnStream = function(s, f) { + var tracks = s.getAudioTracks(); + for (var i = 0; i < tracks.length; i++) { + f(tracks[i]); + } +} + +var forAllTracksOnStream = function(s, f) { + forAllVideoTracksOnStream(s, f); + forAllAudioTracksOnStream(s, f); +} + angular.module('MatrixCall', []) .factory('MatrixCall', ['matrixService', 'matrixPhoneService', function MatrixCallFactory(matrixService, matrixPhoneService) { var MatrixCall = function(room_id) { @@ -55,7 +74,15 @@ angular.module('MatrixCall', []) }; MatrixCall.prototype.hangup = function() { - console.trace("Rejecting call "+this.call_id); + console.trace("Ending call "+this.call_id); + + forAllTracksOnStream(this.localAVStream, function(t) { + t.stop(); + }); + forAllTracksOnStream(this.remoteAVStream, function(t) { + t.stop(); + }); + var content = { msgtype: "m.call.hangup", version: 0, @@ -66,6 +93,7 @@ angular.module('MatrixCall', []) }; MatrixCall.prototype.gotUserMediaForInvite = function(stream) { + this.localAVStream = stream; var audioTracks = stream.getAudioTracks(); for (var i = 0; i < audioTracks.length; i++) { audioTracks[i].enabled = true; @@ -86,6 +114,7 @@ angular.module('MatrixCall', []) }; MatrixCall.prototype.gotUserMediaForAnswer = function(stream) { + this.localAVStream = stream; var audioTracks = stream.getAudioTracks(); for (var i = 0; i < audioTracks.length; i++) { audioTracks[i].enabled = true; @@ -172,7 +201,8 @@ angular.module('MatrixCall', []) MatrixCall.prototype.onIceConnectionStateChanged = function() { console.trace("Ice connection state changed to: "+this.peerConn.iceConnectionState); - if (this.peerConn.iceConnectionState == 'completed') { + // ideally we'd consider the call to be connected when we get media but chrome doesn't implement nay of the 'onstarted' events yet + if (this.peerConn.iceConnectionState == 'completed' || this.peerConn.iceConnectionState == 'connected') { this.state = 'connected'; } }; @@ -191,10 +221,44 @@ angular.module('MatrixCall', []) MatrixCall.prototype.onAddStream = function(event) { console.trace("Stream added"+event); + + var s = event.stream; + + this.remoteAVStream = s; + + var self = this; + forAllTracksOnStream(s, function(t) { + // not currently implemented in chrome + t.onstarted = self.onRemoteStreamTrackStarted; + }); + + // not currently implemented in chrome + event.stream.onstarted = this.onRemoteStreamStarted; var player = new Audio(); - player.src = URL.createObjectURL(event.stream); + player.src = URL.createObjectURL(s); player.play(); }; + MatrixCall.prototype.onRemoteStreamStarted = function(event) { + this.state = 'connected'; + }; + + MatrixCall.prototype.onRemoteStreamTrackStarted = function(event) { + this.state = 'connected'; + }; + + MatrixCall.prototype.onHangupReceived = function() { + this.state = 'ended'; + + forAllTracksOnStream(this.localAVStream, function(t) { + t.stop(); + }); + forAllTracksOnStream(this.remoteAVStream, function(t) { + t.stop(); + }); + + this.onHangup(); + }; + return MatrixCall; }]); diff --git a/webclient/components/matrix/matrix-phone-service.js b/webclient/components/matrix/matrix-phone-service.js index 6f9687510..7f1ff531c 100644 --- a/webclient/components/matrix/matrix-phone-service.js +++ b/webclient/components/matrix/matrix-phone-service.js @@ -59,7 +59,7 @@ angular.module('matrixPhoneService', []) console.trace("Got hangup for unknown call ID "+msg.call_id); return; } - call.onHangup(); + call.onHangupReceived(); matrixPhoneService.allCalls[msg.call_id] = undefined; } }); diff --git a/webclient/room/room.html b/webclient/room/room.html index dceb7322f..bc3eefaf3 100644 --- a/webclient/room/room.html +++ b/webclient/room/room.html @@ -104,6 +104,7 @@ + {{ currentCall.state }} @@ -111,7 +112,6 @@
{{ state.stream_failure.data.error || "Connection failure" }}
-