Skip to content

Commit a6f857e

Browse files
committed
Merge pull request element-hq#6 from matrix-org/voip
VoIP addition
2 parents 73a1c2b + 5f3721f commit a6f857e

12 files changed

Lines changed: 527 additions & 3 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require('react');
20+
21+
var VideoFeedController = require("../../../../../src/controllers/atoms/voip/VideoFeed");
22+
23+
module.exports = React.createClass({
24+
displayName: 'VideoFeed',
25+
mixins: [VideoFeedController],
26+
27+
render: function() {
28+
return (
29+
<video>
30+
</video>
31+
);
32+
},
33+
});
34+

‎skins/base/views/molecules/RoomHeader.js‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ module.exports = React.createClass({
3030
var topic = this.props.room.currentState.getStateEvents('m.room.topic', '');
3131
topic = topic ? <div className="mx_RoomHeader_topic">{ topic.getContent().topic }</div> : null;
3232

33+
var callButtons;
34+
if (this.state) {
35+
switch (this.state.call_state) {
36+
case "ringing":
37+
callButtons = (
38+
<div>
39+
<div className="mx_RoomHeader_button" onClick={this.onAnswerClick}>
40+
YUP
41+
</div>
42+
<div className="mx_RoomHeader_button" onClick={this.onHangupClick}>
43+
NOPE
44+
</div>
45+
</div>
46+
);
47+
break;
48+
case "ringback":
49+
case "connected":
50+
callButtons = (
51+
<div className="mx_RoomHeader_button" onClick={this.onHangupClick}>
52+
BYEBYE
53+
</div>
54+
);
55+
break;
56+
}
57+
}
58+
3359
return (
3460
<div className="mx_RoomHeader">
3561
<div className="mx_RoomHeader_wrapper">
@@ -49,10 +75,11 @@ module.exports = React.createClass({
4975
<div className="mx_RoomHeader_button">
5076
<img src="img/search.png" width="32" height="32"/>
5177
</div>
52-
<div className="mx_RoomHeader_button">
78+
{callButtons}
79+
<div className="mx_RoomHeader_button" onClick={this.onVideoClick}>
5380
<img src="img/video.png" width="32" height="32"/>
5481
</div>
55-
<div className="mx_RoomHeader_button">
82+
<div className="mx_RoomHeader_button" onClick={this.onVoiceClick}>
5683
<img src="img/voip.png" width="32" height="32"/>
5784
</div>
5885
</div>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require('react');
20+
21+
var MatrixClientPeg = require("../../../../../src/MatrixClientPeg");
22+
var ComponentBroker = require('../../../../../src/ComponentBroker');
23+
var CallViewController = require(
24+
"../../../../../src/controllers/molecules/voip/CallView"
25+
);
26+
var VideoView = ComponentBroker.get('molecules/voip/VideoView');
27+
28+
module.exports = React.createClass({
29+
displayName: 'CallView',
30+
mixins: [CallViewController],
31+
32+
getVideoView: function() {
33+
return this.refs.video;
34+
},
35+
36+
render: function(){
37+
return (
38+
<VideoView ref="video"/>
39+
);
40+
}
41+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
var React = require('react');
20+
21+
var MatrixClientPeg = require("../../../../../src/MatrixClientPeg");
22+
var ComponentBroker = require('../../../../../src/ComponentBroker');
23+
var VideoViewController = require("../../../../../src/controllers/molecules/voip/VideoView");
24+
var VideoFeed = ComponentBroker.get('atoms/voip/VideoFeed');
25+
26+
module.exports = React.createClass({
27+
displayName: 'VideoView',
28+
mixins: [VideoViewController],
29+
30+
getRemoteVideoElement: function() {
31+
return this.refs.remote.getDOMNode();
32+
},
33+
34+
getLocalVideoElement: function() {
35+
return this.refs.local.getDOMNode();
36+
},
37+
38+
render: function() {
39+
return (
40+
<div>
41+
<div>
42+
<VideoFeed ref="remote"/>
43+
</div>
44+
<div>
45+
<VideoFeed ref="local"/>
46+
</div>
47+
</div>
48+
);
49+
}
50+
});

‎skins/base/views/organisms/RoomView.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var classNames = require("classnames");
2626
var MessageTile = ComponentBroker.get('molecules/MessageTile');
2727
var RoomHeader = ComponentBroker.get('molecules/RoomHeader');
2828
var MessageComposer = ComponentBroker.get('molecules/MessageComposer');
29+
var CallView = ComponentBroker.get("molecules/voip/CallView");
2930

3031
var RoomViewController = require("../../../../src/controllers/organisms/RoomView");
3132

@@ -73,7 +74,9 @@ module.exports = React.createClass({
7374
return (
7475
<div className="mx_RoomView">
7576
<RoomHeader room={this.state.room} />
76-
<div className="mx_RoomView_auxPanel"></div>
77+
<div className="mx_RoomView_auxPanel">
78+
<CallView room={this.state.room}/>
79+
</div>
7780
<div ref="messageWrapper" className="mx_RoomView_messagePanel" onScroll={this.onMessageListScroll}>
7881
<div className="mx_RoomView_messageListWrapper">
7982
<div className="mx_RoomView_MessageList" aria-live="polite">

‎src/CallHandler.js‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
Copyright 2015 OpenMarket Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
/*
20+
* Manages a list of all the currently active calls.
21+
*
22+
* This handler dispatches when voip calls are added/updated/removed from this list:
23+
* {
24+
* action: 'call_state'
25+
* room_id: <room ID of the call>,
26+
* status: ringing|ringback|connected|ended|busy|stop_ringback|stop_ringing
27+
* }
28+
*
29+
* To know if the call was added/removed, this handler exposes a getter to
30+
* obtain the call for a room:
31+
* CallHandler.getCall(roomId)
32+
*
33+
* This handler listens for and handles the following actions:
34+
* {
35+
* action: 'place_call',
36+
* type: 'voice|video',
37+
* room_id: <room that the place call button was pressed in>
38+
* }
39+
*
40+
* {
41+
* action: 'incoming_call'
42+
* call: MatrixCall
43+
* }
44+
*
45+
* {
46+
* action: 'hangup'
47+
* room_id: <room that the hangup button was pressed in>
48+
* }
49+
*
50+
* {
51+
* action: 'answer'
52+
* room_id: <room that the answer button was pressed in>
53+
* }
54+
*/
55+
56+
var MatrixClientPeg = require("./MatrixClientPeg");
57+
var Matrix = require("matrix-js-sdk");
58+
var dis = require("./dispatcher");
59+
60+
var calls = {
61+
//room_id: MatrixCall
62+
};
63+
64+
function _setCallListeners(call) {
65+
call.on("error", function(err) {
66+
console.error("Call error: %s", err);
67+
console.error(err.stack);
68+
call.hangup();
69+
_setCallState(undefined, call.roomId, "ended");
70+
});
71+
call.on("hangup", function() {
72+
_setCallState(undefined, call.roomId, "ended");
73+
});
74+
// map web rtc states to dummy UI state
75+
// ringing|ringback|connected|ended|busy|stop_ringback|stop_ringing
76+
call.on("state", function(newState, oldState) {
77+
if (newState === "ringing") {
78+
_setCallState(call, call.roomId, "ringing");
79+
}
80+
else if (newState === "invite_sent") {
81+
_setCallState(call, call.roomId, "ringback");
82+
}
83+
else if (newState === "ended" && oldState === "connected") {
84+
_setCallState(call, call.roomId, "ended");
85+
}
86+
else if (newState === "ended" && oldState === "invite_sent" &&
87+
(call.hangupParty === "remote" ||
88+
(call.hangupParty === "local" && call.hangupReason === "invite_timeout")
89+
)) {
90+
_setCallState(call, call.roomId, "busy");
91+
}
92+
else if (oldState === "invite_sent") {
93+
_setCallState(call, call.roomId, "stop_ringback");
94+
}
95+
else if (oldState === "ringing") {
96+
_setCallState(call, call.roomId, "stop_ringing");
97+
}
98+
else if (newState === "connected") {
99+
_setCallState(call, call.roomId, "connected");
100+
}
101+
});
102+
}
103+
104+
function _setCallState(call, roomId, status) {
105+
console.log(
106+
"Call state in %s changed to %s (%s)", roomId, status, (call ? call.state : "-")
107+
);
108+
calls[roomId] = call;
109+
if (call) {
110+
call.call_state = status;
111+
}
112+
dis.dispatch({
113+
action: 'call_state',
114+
room_id: roomId,
115+
status: status
116+
});
117+
}
118+
119+
dis.register(function(payload) {
120+
switch (payload.action) {
121+
case 'place_call':
122+
if (calls[payload.room_id]) {
123+
return; // don't allow >1 call to be placed.
124+
}
125+
var room = MatrixClientPeg.get().getRoom(payload.room_id);
126+
if (!room) {
127+
console.error("Room %s does not exist.", payload.room_id);
128+
return;
129+
}
130+
if (room.getJoinedMembers().length !== 2) {
131+
console.error(
132+
"Fail: There are %s joined members in this room, not 2.",
133+
room.getJoinedMembers().length
134+
);
135+
return;
136+
}
137+
console.log("Place %s call in %s", payload.type, payload.room_id);
138+
var call = Matrix.createNewMatrixCall(
139+
MatrixClientPeg.get(), payload.room_id
140+
);
141+
_setCallListeners(call);
142+
_setCallState(call, call.roomId, "ringback");
143+
if (payload.type === 'voice') {
144+
call.placeVoiceCall();
145+
}
146+
else if (payload.type === 'video') {
147+
call.placeVideoCall(
148+
payload.remote_element,
149+
payload.local_element
150+
);
151+
}
152+
else {
153+
console.error("Unknown call type: %s", payload.type);
154+
}
155+
156+
break;
157+
case 'incoming_call':
158+
if (calls[payload.call.roomId]) {
159+
payload.call.hangup("busy");
160+
return; // don't allow >1 call to be received, hangup newer one.
161+
}
162+
var call = payload.call;
163+
_setCallListeners(call);
164+
_setCallState(call, call.roomId, "ringing");
165+
break;
166+
case 'hangup':
167+
if (!calls[payload.room_id]) {
168+
return; // no call to hangup
169+
}
170+
calls[payload.room_id].hangup();
171+
_setCallState(null, payload.room_id, "ended");
172+
break;
173+
case 'answer':
174+
if (!calls[payload.room_id]) {
175+
return; // no call to answer
176+
}
177+
calls[payload.room_id].answer();
178+
_setCallState(calls[payload.room_id], payload.room_id, "connected");
179+
break;
180+
}
181+
});
182+
183+
module.exports = {
184+
getCall: function(roomId) {
185+
return calls[roomId] || null;
186+
}
187+
};

‎src/ComponentBroker.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,9 @@ require('../skins/base/views/organisms/RightPanel');
9898
require('../skins/base/views/molecules/RoomCreate');
9999
require('../skins/base/views/molecules/RoomDropTarget');
100100
require('../skins/base/views/molecules/DirectoryMenu');
101+
require('../skins/base/views/atoms/voip/VideoFeed');
102+
require('../skins/base/views/molecules/voip/VideoView');
103+
require('../skins/base/views/molecules/voip/CallView');
104+
101105

102106
}

0 commit comments

Comments
 (0)