diff --git a/webclient/components/matrix/event-handler-service.js b/webclient/components/matrix/event-handler-service.js
index df61429db5..b6e5c2eaac 100644
--- a/webclient/components/matrix/event-handler-service.js
+++ b/webclient/components/matrix/event-handler-service.js
@@ -31,6 +31,7 @@ angular.module('eventHandlerService', [])
var MSG_EVENT = "MSG_EVENT";
var MEMBER_EVENT = "MEMBER_EVENT";
var PRESENCE_EVENT = "PRESENCE_EVENT";
+ var CALL_EVENT = "CALL_EVENT";
var InitialSyncDeferred = $q.defer();
@@ -94,12 +95,16 @@ angular.module('eventHandlerService', [])
$rootScope.presence[event.content.user_id] = event;
$rootScope.$broadcast(PRESENCE_EVENT, event, isLiveEvent);
};
-
+
+ var handleCallEvent = function(event, isLiveEvent) {
+ $rootScope.$broadcast(CALL_EVENT, event, isLiveEvent);
+ };
return {
MSG_EVENT: MSG_EVENT,
MEMBER_EVENT: MEMBER_EVENT,
PRESENCE_EVENT: PRESENCE_EVENT,
+ CALL_EVENT: CALL_EVENT,
handleEvent: function(event, isLiveEvent) {
@@ -117,6 +122,9 @@ angular.module('eventHandlerService', [])
console.log("Unable to handle event type " + event.type);
break;
}
+ if (event.type.indexOf('m.call.') == 0) {
+ handleCallEvent(event, isLiveEvent);
+ }
},
// isLiveEvents determines whether notifications should be shown, whether
diff --git a/webclient/components/matrix/event-stream-service.js b/webclient/components/matrix/event-stream-service.js
index 4cc2bf4c4e..441148670e 100644
--- a/webclient/components/matrix/event-stream-service.js
+++ b/webclient/components/matrix/event-stream-service.js
@@ -25,7 +25,8 @@ the eventHandlerService.
angular.module('eventStreamService', [])
.factory('eventStreamService', ['$q', '$timeout', 'matrixService', 'eventHandlerService', function($q, $timeout, matrixService, eventHandlerService) {
var END = "END";
- var TIMEOUT_MS = 30000;
+ var SERVER_TIMEOUT_MS = 30000;
+ var CLIENT_TIMEOUT_MS = 40000;
var ERR_TIMEOUT_MS = 5000;
var settings = {
@@ -55,7 +56,7 @@ angular.module('eventStreamService', [])
deferred = deferred || $q.defer();
// run the stream from the latest token
- matrixService.getEventStream(settings.from, TIMEOUT_MS).then(
+ matrixService.getEventStream(settings.from, SERVER_TIMEOUT_MS, CLIENT_TIMEOUT_MS).then(
function(response) {
if (!settings.isActive) {
console.log("[EventStream] Got response but now inactive. Dropping data.");
@@ -80,7 +81,7 @@ angular.module('eventStreamService', [])
}
},
function(error) {
- if (error.status == 403) {
+ if (error.status === 403) {
settings.shouldPoll = false;
}
diff --git a/webclient/components/matrix/matrix-call.js b/webclient/components/matrix/matrix-call.js
new file mode 100644
index 0000000000..c0a7735a7c
--- /dev/null
+++ b/webclient/components/matrix/matrix-call.js
@@ -0,0 +1,268 @@
+/*
+Copyright 2014 matrix.org
+
+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';
+
+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) {
+ this.room_id = room_id;
+ this.call_id = "c" + new Date().getTime();
+ this.state = 'fledgling';
+ }
+
+ navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
+
+ window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
+
+ MatrixCall.prototype.placeCall = function() {
+ self = this;
+ matrixPhoneService.callPlaced(this);
+ navigator.getUserMedia({audio: true, video: false}, function(s) { self.gotUserMediaForInvite(s); }, function(e) { self.getUserMediaFailed(e); });
+ self.state = 'wait_local_media';
+ };
+
+ MatrixCall.prototype.initWithInvite = function(msg) {
+ this.msg = msg;
+ this.peerConn = new window.RTCPeerConnection({"iceServers":[{"urls":"stun:stun.l.google.com:19302"}]})
+ self= this;
+ this.peerConn.oniceconnectionstatechange = function() { self.onIceConnectionStateChanged(); };
+ this.peerConn.onicecandidate = function(c) { self.gotLocalIceCandidate(c); };
+ this.peerConn.onsignalingstatechange = function() { self.onSignallingStateChanged(); };
+ this.peerConn.onaddstream = function(s) { self.onAddStream(s); };
+ this.peerConn.setRemoteDescription(new RTCSessionDescription(this.msg.offer), self.onSetRemoteDescriptionSuccess, self.onSetRemoteDescriptionError);
+ this.state = 'ringing';
+ };
+
+ MatrixCall.prototype.answer = function() {
+ console.trace("Answering call "+this.call_id);
+ self = this;
+ navigator.getUserMedia({audio: true, video: false}, function(s) { self.gotUserMediaForAnswer(s); }, function(e) { self.getUserMediaFailed(e); });
+ this.state = 'wait_local_media';
+ };
+
+ MatrixCall.prototype.stopAllMedia = function() {
+ if (this.localAVStream) {
+ forAllTracksOnStream(this.localAVStream, function(t) {
+ t.stop();
+ });
+ }
+ if (this.remoteAVStream) {
+ forAllTracksOnStream(this.remoteAVStream, function(t) {
+ t.stop();
+ });
+ }
+ };
+
+ MatrixCall.prototype.hangup = function() {
+ console.trace("Ending call "+this.call_id);
+
+ this.stopAllMedia();
+
+ var content = {
+ version: 0,
+ call_id: this.call_id,
+ };
+ matrixService.sendEvent(this.room_id, 'm.call.hangup', undefined, content).then(this.messageSent, this.messageSendFailed);
+ this.state = 'ended';
+ };
+
+ MatrixCall.prototype.gotUserMediaForInvite = function(stream) {
+ this.localAVStream = stream;
+ var audioTracks = stream.getAudioTracks();
+ for (var i = 0; i < audioTracks.length; i++) {
+ audioTracks[i].enabled = true;
+ }
+ this.peerConn = new window.RTCPeerConnection({"iceServers":[{"urls":"stun:stun.l.google.com:19302"}]})
+ self = this;
+ this.peerConn.oniceconnectionstatechange = function() { self.onIceConnectionStateChanged(); };
+ this.peerConn.onsignalingstatechange = function() { self.onSignallingStateChanged(); };
+ this.peerConn.onicecandidate = function(c) { self.gotLocalIceCandidate(c); };
+ this.peerConn.onaddstream = function(s) { self.onAddStream(s); };
+ this.peerConn.addStream(stream);
+ this.peerConn.createOffer(function(d) {
+ self.gotLocalOffer(d);
+ }, function(e) {
+ self.getLocalOfferFailed(e);
+ });
+ this.state = 'create_offer';
+ };
+
+ MatrixCall.prototype.gotUserMediaForAnswer = function(stream) {
+ this.localAVStream = stream;
+ var audioTracks = stream.getAudioTracks();
+ for (var i = 0; i < audioTracks.length; i++) {
+ audioTracks[i].enabled = true;
+ }
+ this.peerConn.addStream(stream);
+ self = this;
+ var constraints = {
+ 'mandatory': {
+ 'OfferToReceiveAudio': true,
+ 'OfferToReceiveVideo': false
+ },
+ };
+ this.peerConn.createAnswer(function(d) { self.createdAnswer(d); }, function(e) {}, constraints);
+ this.state = 'create_answer';
+ };
+
+ MatrixCall.prototype.gotLocalIceCandidate = function(event) {
+ console.trace(event);
+ if (event.candidate) {
+ var content = {
+ version: 0,
+ call_id: this.call_id,
+ candidate: event.candidate
+ };
+ matrixService.sendEvent(this.room_id, 'm.call.candidate', undefined, content).then(this.messageSent, this.messageSendFailed);
+ }
+ }
+
+ MatrixCall.prototype.gotRemoteIceCandidate = function(cand) {
+ console.trace("Got ICE candidate from remote: "+cand);
+ var candidateObject = new RTCIceCandidate({
+ sdpMLineIndex: cand.label,
+ candidate: cand.candidate
+ });
+ this.peerConn.addIceCandidate(candidateObject, function() {}, function(e) {});
+ };
+
+ MatrixCall.prototype.receivedAnswer = function(msg) {
+ this.peerConn.setRemoteDescription(new RTCSessionDescription(msg.answer), self.onSetRemoteDescriptionSuccess, self.onSetRemoteDescriptionError);
+ this.state = 'connecting';
+ };
+
+ MatrixCall.prototype.gotLocalOffer = function(description) {
+ console.trace("Created offer: "+description);
+ this.peerConn.setLocalDescription(description);
+
+ var content = {
+ version: 0,
+ call_id: this.call_id,
+ offer: description
+ };
+ matrixService.sendEvent(this.room_id, 'm.call.invite', undefined, content).then(this.messageSent, this.messageSendFailed);
+ this.state = 'invite_sent';
+ };
+
+ MatrixCall.prototype.createdAnswer = function(description) {
+ console.trace("Created answer: "+description);
+ this.peerConn.setLocalDescription(description);
+ var content = {
+ version: 0,
+ call_id: this.call_id,
+ answer: description
+ };
+ matrixService.sendEvent(this.room_id, 'm.call.answer', undefined, content).then(this.messageSent, this.messageSendFailed);
+ this.state = 'connecting';
+ };
+
+ MatrixCall.prototype.messageSent = function() {
+ };
+
+ MatrixCall.prototype.messageSendFailed = function(error) {
+ };
+
+ MatrixCall.prototype.getLocalOfferFailed = function(error) {
+ this.onError("Failed to start audio for call!");
+ };
+
+ MatrixCall.prototype.getUserMediaFailed = function() {
+ this.onError("Couldn't start capturing audio! Is your microphone set up?");
+ };
+
+ MatrixCall.prototype.onIceConnectionStateChanged = function() {
+ console.trace("Ice connection state changed to: "+this.peerConn.iceConnectionState);
+ // 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';
+ }
+ };
+
+ MatrixCall.prototype.onSignallingStateChanged = function() {
+ console.trace("Signalling state changed to: "+this.peerConn.signalingState);
+ };
+
+ MatrixCall.prototype.onSetRemoteDescriptionSuccess = function() {
+ console.trace("Set remote description");
+ };
+
+ MatrixCall.prototype.onSetRemoteDescriptionError = function(e) {
+ console.trace("Failed to set remote description"+e);
+ };
+
+ 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;
+ });
+
+ event.stream.onended = function(e) { self.onRemoteStreamEnded(e); };
+ // not currently implemented in chrome
+ event.stream.onstarted = function(e) { self.onRemoteStreamStarted(e); };
+ var player = new Audio();
+ player.src = URL.createObjectURL(s);
+ player.play();
+ };
+
+ MatrixCall.prototype.onRemoteStreamStarted = function(event) {
+ this.state = 'connected';
+ };
+
+ MatrixCall.prototype.onRemoteStreamEnded = function(event) {
+ this.state = 'ended';
+ this.stopAllMedia();
+ this.onHangup();
+ };
+
+ MatrixCall.prototype.onRemoteStreamTrackStarted = function(event) {
+ this.state = 'connected';
+ };
+
+ MatrixCall.prototype.onHangupReceived = function() {
+ this.state = 'ended';
+ this.stopAllMedia();
+ this.onHangup();
+ };
+
+ return MatrixCall;
+}]);
diff --git a/webclient/components/matrix/matrix-phone-service.js b/webclient/components/matrix/matrix-phone-service.js
new file mode 100644
index 0000000000..d9e2e8baa3
--- /dev/null
+++ b/webclient/components/matrix/matrix-phone-service.js
@@ -0,0 +1,68 @@
+/*
+Copyright 2014 matrix.org
+
+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', [])
+.factory('matrixPhoneService', ['$rootScope', '$injector', 'matrixService', 'eventHandlerService', function MatrixPhoneService($rootScope, $injector, matrixService, eventHandlerService) {
+ var matrixPhoneService = function() {
+ };
+
+ matrixPhoneService.INCOMING_CALL_EVENT = "INCOMING_CALL_EVENT";
+ matrixPhoneService.allCalls = {};
+
+ matrixPhoneService.callPlaced = function(call) {
+ matrixPhoneService.allCalls[call.call_id] = call;
+ };
+
+ $rootScope.$on(eventHandlerService.CALL_EVENT, function(ngEvent, event, isLive) {
+ 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') {
+ var MatrixCall = $injector.get('MatrixCall');
+ var call = new MatrixCall(event.room_id);
+ call.call_id = msg.call_id;
+ call.initWithInvite(msg);
+ matrixPhoneService.allCalls[call.call_id] = call;
+ $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);
+ 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);
+ 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);
+ return;
+ }
+ call.onHangupReceived();
+ matrixPhoneService.allCalls[msg.call_id] = undefined;
+ }
+ });
+
+ return matrixPhoneService;
+}]);
diff --git a/webclient/components/matrix/matrix-service.js b/webclient/components/matrix/matrix-service.js
index 2feddac5d8..8543491dca 100644
--- a/webclient/components/matrix/matrix-service.js
+++ b/webclient/components/matrix/matrix-service.js
@@ -41,7 +41,7 @@ angular.module('matrixService', [])
var prefixPath = "/matrix/client/api/v1";
var MAPPING_PREFIX = "alias_for_";
- var doRequest = function(method, path, params, data) {
+ var doRequest = function(method, path, params, data, $httpParams) {
if (!config) {
console.warn("No config exists. Cannot perform request to "+path);
return;
@@ -58,7 +58,7 @@ angular.module('matrixService', [])
path = prefixPath + path;
}
- return doBaseRequest(config.homeserver, method, path, params, data, undefined);
+ return doBaseRequest(config.homeserver, method, path, params, data, undefined, $httpParams);
};
var doBaseRequest = function(baseUrl, method, path, params, data, headers, $httpParams) {
@@ -172,9 +172,9 @@ angular.module('matrixService', [])
return doRequest("GET", path, undefined, {});
},
- sendMessage: function(room_id, txn_id, content) {
+ sendEvent: function(room_id, eventType, txn_id, content) {
// The REST path spec
- var path = "/rooms/$room_id/send/m.room.message/$txn_id";
+ var path = "/rooms/$room_id/send/"+eventType+"/$txn_id";
if (!txn_id) {
txn_id = "m" + new Date().getTime();
@@ -190,6 +190,10 @@ angular.module('matrixService', [])
return doRequest("PUT", path, undefined, content);
},
+ sendMessage: function(room_id, txn_id, content) {
+ return this.sendEvent(room_id, 'm.room.message', txn_id, content);
+ },
+
// Send a text message
sendTextMessage: function(room_id, body, msg_id) {
var content = {
@@ -343,15 +347,31 @@ angular.module('matrixService', [])
return doBaseRequest(config.homeserver, "POST", path, params, file, headers, $httpParams);
},
-
- // start listening on /events
- getEventStream: function(from, timeout) {
+
+ /**
+ * Start listening on /events
+ * @param {String} from the token from which to listen events to
+ * @param {Integer} serverTimeout the time in ms the server will hold open the connection
+ * @param {Integer} clientTimeout the timeout in ms used at the client HTTP request level
+ * @returns a promise
+ */
+ getEventStream: function(from, serverTimeout, clientTimeout) {
var path = "/events";
var params = {
from: from,
- timeout: timeout
+ timeout: serverTimeout
};
- return doRequest("GET", path, params);
+
+ var $httpParams;
+ if (clientTimeout) {
+ // If the Internet connection is lost, this timeout is used to be able to
+ // cancel the current request and notify the client so that it can retry with a new request.
+ $httpParams = {
+ timeout: clientTimeout
+ };
+ }
+
+ return doRequest("GET", path, params, undefined, $httpParams);
},
// Indicates if user authentications details are stored in cache
@@ -420,34 +440,38 @@ angular.module('matrixService', [])
/****** Room aliases management ******/
/**
- * Enhance data returned by rooms() and publicRooms() by adding room_alias
- * & room_display_name which are computed from data already retrieved from the server.
- * @param {Array} data the response of rooms() and publicRooms()
- * @returns {Array} the same array with enriched objects
+ * Get the room_alias & room_display_name which are computed from data
+ * already retrieved from the server.
+ * @param {Room object} room one element of the array returned by the response
+ * of rooms() and publicRooms()
+ * @returns {Object} {room_alias: "...", room_display_name: "..."}
*/
- assignRoomAliases: function(data) {
- for (var i=0; i<data.length; i++) {
- var alias = this.getRoomIdToAliasMapping(data[i].room_id);
- if (alias) {
- // use the existing alias from storage
- data[i].room_alias = alias;
- data[i].room_display_name = alias;
- }
- else if (data[i].aliases && data[i].aliases[0]) {
- // save the mapping
- // TODO: select the smarter alias from the array
- this.createRoomIdToAliasMapping(data[i].room_id, data[i].aliases[0]);
- data[i].room_display_name = data[i].aliases[0];
- }
- else if (data[i].membership == "invite" && "inviter" in data[i]) {
- data[i].room_display_name = data[i].inviter + "'s room"
- }
- else {
- // last resort use the room id
- data[i].room_display_name = data[i].room_id;
- }
+ getRoomAliasAndDisplayName: function(room) {
+ var result = {
+ room_alias: undefined,
+ room_display_name: undefined
+ };
+
+ var alias = this.getRoomIdToAliasMapping(room.room_id);
+ if (alias) {
+ // use the existing alias from storage
+ result.room_alias = alias;
+ result.room_display_name = alias;
+ }
+ else if (room.aliases && room.aliases[0]) {
+ // save the mapping
+ // TODO: select the smarter alias from the array
+ this.createRoomIdToAliasMapping(room.room_id, room.aliases[0]);
+ result.room_display_name = room.aliases[0];
+ }
+ else if (room.membership === "invite" && "inviter" in room) {
+ result.room_display_name = room.inviter + "'s room";
+ }
+ else {
+ // last resort use the room id
+ result.room_display_name = room.room_id;
}
- return data;
+ return result;
},
createRoomIdToAliasMapping: function(roomId, alias) {
diff --git a/webclient/components/matrix/presence-service.js b/webclient/components/matrix/presence-service.js
index 6a1edcaf43..555118133b 100644
--- a/webclient/components/matrix/presence-service.js
+++ b/webclient/components/matrix/presence-service.js
@@ -23,9 +23,9 @@
angular.module('mPresence', [])
.service('mPresence', ['$timeout', 'matrixService', function ($timeout, matrixService) {
- // Time in ms after that a user is considered as offline/away
- var OFFLINE_TIME = 5 * 60000; // 5 mins
-
+ // Time in ms after that a user is considered as unavailable/away
+ var UNAVAILABLE_TIME = 5 * 60000; // 5 mins
+
// The current presence state
var state = undefined;
@@ -88,11 +88,11 @@ angular.module('mPresence', [])
};
/**
- * Callback called when the user made no action on the page for OFFLINE_TIME ms.
+ * Callback called when the user made no action on the page for UNAVAILABLE_TIME ms.
* @private
*/
- function onOfflineTimerFire() {
- self.setState(matrixService.presence.offline);
+ function onUnvailableTimerFire() {
+ self.setState(matrixService.presence.unavailable);
}
/**
@@ -105,7 +105,7 @@ angular.module('mPresence', [])
// Re-arm the timer
$timeout.cancel(timer);
- timer = $timeout(onOfflineTimerFire, OFFLINE_TIME);
+ timer = $timeout(onUnvailableTimerFire, UNAVAILABLE_TIME);
}
}]);
|