Skip to content

Commit 6316f1b

Browse files
committed
Add call handling logic. Outbound voice calls work!
1 parent 7e30c0f commit 6316f1b

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎skins/base/views/molecules/voip/CallHandler.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,25 @@ var VideoView = ComponentBroker.get('molecules/voip/VideoView');
2828
module.exports = React.createClass({
2929
displayName: 'CallHandler',
3030
mixins: [CallHandlerController],
31+
32+
getVideoView: function() {
33+
return this.refs.video;
34+
},
35+
3136
render: function(){
37+
if (this.state && this.state.call) {
38+
if (this.state.call.type === "video") {
39+
return (
40+
<VideoView ref="video"/>
41+
);
42+
}
43+
else if (this.state.call.type === "voice") {
44+
// <WaveformView /> in the future.
45+
return (
46+
<div></div>
47+
);
48+
}
49+
}
3250
return (
3351
<div></div>
3452
);

‎src/controllers/molecules/voip/CallHandler.js‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,25 @@ limitations under the License.
1515
*/
1616

1717
'use strict';
18-
18+
var MatrixClientPeg = require("../../../MatrixClientPeg");
19+
var Matrix = require("matrix-js-sdk");
1920
var dis = require("../../../dispatcher");
2021

22+
/*
23+
* State vars:
24+
* this.state.call = MatrixCall|null
25+
*
26+
* Props:
27+
* this.props.room = Room (JS SDK) - can be null (for singleton views)
28+
*/
29+
2130
module.exports = {
2231

2332
componentDidMount: function() {
2433
this.dispatcherRef = dis.register(this.onAction);
34+
this.setState({
35+
call: null
36+
});
2537
},
2638

2739
componentWillUnmount: function() {
@@ -37,12 +49,75 @@ module.exports = {
3749

3850
switch (payload.action) {
3951
case 'place_call':
52+
if (this.state.call) {
53+
return; // don't allow >1 call to be placed.
54+
}
4055
console.log("Place %s call in %s", payload.type, payload.room_id);
56+
var call = Matrix.createNewMatrixCall(
57+
MatrixClientPeg.get(), payload.room_id
58+
);
59+
this._setCallListeners(call);
60+
this.setState({
61+
call: call
62+
});
63+
if (payload.type === 'voice') {
64+
call.placeVoiceCall();
65+
}
66+
else if (payload.type === 'video') {
67+
var videoView = this.getVideoView();
68+
call.placeVideoCall(
69+
videoView.getRemoteVideoElement(),
70+
videoView.getLocalVideoElement()
71+
);
72+
}
73+
else {
74+
console.error("Unknown call type: %s", payload.type);
75+
}
4176
break;
4277
case 'incoming_call':
78+
if (this.state.call) {
79+
payload.call.hangup("busy");
80+
return; // don't allow >1 call to be received.
81+
}
82+
this._setCallListeners(call);
83+
this.setState({
84+
call: call
85+
});
4386
console.log("Incoming call: %s", payload.call);
4487
break;
88+
case 'hangup':
89+
if (!this.state.call) {
90+
return; // no call to hangup
91+
}
92+
this.state.call.hangup();
93+
this.setState({
94+
call: null
95+
});
96+
break;
97+
case 'answer':
98+
if (!this.state.call) {
99+
return; // no call to answer
100+
}
101+
this.state.call.answer();
102+
break;
45103
}
104+
},
105+
106+
_setCallListeners: function(call) {
107+
var self = this;
108+
call.on("error", function(err) {
109+
console.error("Call error: %s", err);
110+
console.error(err.stack);
111+
call.hangup();
112+
self.setState({
113+
call: null
114+
});
115+
});
116+
call.on("hangup", function() {
117+
self.setState({
118+
call: null
119+
});
120+
})
46121
}
47122
};
48123

0 commit comments

Comments
 (0)