Skip to content

Commit 77401e2

Browse files
committed
First working outbound conference calling
This has a number of failings currently: 1) It needs to hide the 1:1 conference room, 2) Swapping tabs on the outbound call mutes audio (this just seems to be a vector bug since I can repro this on a normal 1:1 voip call), 3) Needs a big plinth/etc to say the conf call is in progress.
1 parent ee4da24 commit 77401e2

2 files changed

Lines changed: 106 additions & 23 deletions

File tree

‎src/CallHandler.js‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ var MatrixClientPeg = require("./MatrixClientPeg");
5757
var Modal = require("./Modal");
5858
var ComponentBroker = require('./ComponentBroker');
5959
var ErrorDialog = ComponentBroker.get("organisms/ErrorDialog");
60+
var ConferenceHandler = require("./ConferenceHandler");
6061
var Matrix = require("matrix-js-sdk");
6162
var dis = require("./dispatcher");
6263

@@ -161,37 +162,49 @@ dis.register(function(payload) {
161162
console.error("Room %s does not exist.", payload.room_id);
162163
return;
163164
}
165+
166+
function placeCall(newCall) {
167+
_setCallListeners(newCall);
168+
_setCallState(newCall, newCall.roomId, "ringback");
169+
if (payload.type === 'voice') {
170+
newCall.placeVoiceCall();
171+
}
172+
else if (payload.type === 'video') {
173+
newCall.placeVideoCall(
174+
payload.remote_element,
175+
payload.local_element
176+
);
177+
}
178+
else {
179+
console.error("Unknown conf call type: %s", payload.type);
180+
}
181+
}
182+
164183
var members = room.getJoinedMembers();
165-
if (members.length !== 2) {
166-
var text = members.length === 1 ? "yourself." : "more than 2 people.";
184+
if (members.length <= 1) {
167185
Modal.createDialog(ErrorDialog, {
168-
description: "You cannot place a call with " + text
186+
description: "You cannot place a call with yourself."
169187
});
170-
console.error(
171-
"Fail: There are %s joined members in this room, not 2.",
172-
room.getJoinedMembers().length
173-
);
174188
return;
175189
}
176-
console.log("Place %s call in %s", payload.type, payload.room_id);
177-
var call = Matrix.createNewMatrixCall(
178-
MatrixClientPeg.get(), payload.room_id
179-
);
180-
_setCallListeners(call);
181-
_setCallState(call, call.roomId, "ringback");
182-
if (payload.type === 'voice') {
183-
call.placeVoiceCall();
184-
}
185-
else if (payload.type === 'video') {
186-
call.placeVideoCall(
187-
payload.remote_element,
188-
payload.local_element
190+
else if (members.length === 2) {
191+
console.log("Place %s call in %s", payload.type, payload.room_id);
192+
var call = Matrix.createNewMatrixCall(
193+
MatrixClientPeg.get(), payload.room_id
189194
);
195+
placeCall(call);
190196
}
191-
else {
192-
console.error("Unknown call type: %s", payload.type);
197+
else { // > 2
198+
console.log("Place conference call in %s", payload.room_id);
199+
var confHandler = new ConferenceHandler(
200+
MatrixClientPeg.get(), payload.room_id
201+
);
202+
confHandler.setup().done(function(call) {
203+
placeCall(call);
204+
}, function(err) {
205+
console.error("Failed to setup conference call: %s", err);
206+
});
193207
}
194-
195208
break;
196209
case 'incoming_call':
197210
if (calls[payload.call.roomId]) {

‎src/ConferenceHandler.js‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
var q = require("q");
3+
var Matrix = require("matrix-js-sdk");
4+
var Room = Matrix.Room;
5+
6+
var USER_PREFIX = "fs_";
7+
var DOMAIN = "matrix.org";
8+
9+
function ConferenceHandler(matrixClient, groupChatRoomId) {
10+
this.client = matrixClient;
11+
this.groupRoomId = groupChatRoomId;
12+
// abuse browserify's core node Buffer support (strip padding ='s)
13+
this.base64RoomId = new Buffer(this.groupRoomId).toString("base64").replace(/=/g, "");
14+
this.confUserId = "@" + USER_PREFIX + this.base64RoomId + ":" + DOMAIN;
15+
}
16+
17+
ConferenceHandler.prototype.setup = function() {
18+
var self = this;
19+
return this._joinConferenceUser().then(function() {
20+
return self._getConferenceUserRoom();
21+
}).then(function(room) {
22+
// return a call for *this* room to be placed.
23+
return Matrix.createNewMatrixCall(self.client, room.roomId);
24+
});
25+
};
26+
27+
ConferenceHandler.prototype._joinConferenceUser = function() {
28+
// Make sure the conference user is in the group chat room
29+
var groupRoom = this.client.getRoom(this.groupRoomId);
30+
if (!groupRoom) {
31+
return q.reject("Bad group room ID");
32+
}
33+
var members = groupRoom.getJoinedMembers();
34+
var confUserExists = false;
35+
for (var i = 0; i < members.length; i++) {
36+
if (members[i].userId === this.confUserId) {
37+
confUserExists = true;
38+
break;
39+
}
40+
}
41+
if (confUserExists) {
42+
return q();
43+
}
44+
return this.client.invite(this.groupRoomId, this.confUserId);
45+
};
46+
47+
ConferenceHandler.prototype._getConferenceUserRoom = function() {
48+
// Use an existing 1:1 with the conference user; else make one
49+
var rooms = this.client.getRooms();
50+
var confRoom = null;
51+
for (var i = 0; i < rooms.length; i++) {
52+
if (rooms[i].hasMembershipState(this.confUserId, "join") &&
53+
rooms[i].getJoinedMembers().length === 2) {
54+
confRoom = rooms[i];
55+
break;
56+
}
57+
}
58+
if (confRoom) {
59+
return q(confRoom);
60+
}
61+
return this.client.createRoom({
62+
preset: "private_chat",
63+
invite: [this.confUserId]
64+
}).then(function(res) {
65+
return new Room(res.room_id);
66+
});
67+
};
68+
69+
module.exports = ConferenceHandler;
70+

0 commit comments

Comments
 (0)