Skip to content

Commit 7ffd97b

Browse files
committed
Implement call FSM. All works.
1 parent ecd1f09 commit 7ffd97b

3 files changed

Lines changed: 56 additions & 52 deletions

File tree

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ module.exports = React.createClass({
3232

3333
var callButtons;
3434
if (this.state) {
35-
switch (this.state.callState) {
36-
case "INBOUND":
35+
switch (this.state.call_state) {
36+
case "ringing":
3737
callButtons = (
3838
<div>
3939
<div className="mx_RoomHeader_button" onClick={this.onAnswerClick}>
@@ -45,14 +45,8 @@ module.exports = React.createClass({
4545
</div>
4646
);
4747
break;
48-
case "OUTBOUND":
49-
callButtons = (
50-
<div className="mx_RoomHeader_button" onClick={this.onHangupClick}>
51-
BYEBYE
52-
</div>
53-
);
54-
break;
55-
case "IN_CALL":
48+
case "ringback":
49+
case "connected":
5650
callButtons = (
5751
<div className="mx_RoomHeader_button" onClick={this.onHangupClick}>
5852
BYEBYE

‎src/CallHandler.js‎

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ limitations under the License.
2222
* This handler dispatches when voip calls are added/updated/removed from this list:
2323
* {
2424
* action: 'call_state'
25-
* room_id: <room ID of the call>
25+
* room_id: <room ID of the call>,
26+
* status: ringing|ringback|connected|ended|busy|stop_ringback|stop_ringing
2627
* }
2728
*
2829
* To know if the call was added/removed, this handler exposes a getter to
@@ -33,8 +34,6 @@ limitations under the License.
3334
* {
3435
* action: 'place_call',
3536
* type: 'voice|video',
36-
* remote_element: DOMVideoElement, // only if type: video
37-
* local_element: DOMVideoElement, // only if type: video
3837
* room_id: <room that the place call button was pressed in>
3938
* }
4039
*
@@ -67,19 +66,51 @@ function _setCallListeners(call) {
6766
console.error("Call error: %s", err);
6867
console.error(err.stack);
6968
call.hangup();
70-
_setCallState(undefined, call.roomId);
69+
_setCallState(undefined, call.roomId, "ended");
7170
});
7271
call.on("hangup", function() {
73-
_setCallState(undefined, call.roomId);
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+
}
74101
});
75102
}
76103

77-
function _setCallState(call, roomId) {
78-
console.log("_setState >>> %s >>> %s ", call, roomId);
104+
function _setCallState(call, roomId, status) {
105+
console.log("_setState >>> %s >>> %s >> %s", call, roomId, status);
79106
calls[roomId] = call;
107+
if (call) {
108+
call.call_state = status;
109+
}
80110
dis.dispatch({
81111
action: 'call_state',
82-
room_id: roomId
112+
room_id: roomId,
113+
status: status
83114
});
84115
}
85116

@@ -94,7 +125,7 @@ dis.register(function(payload) {
94125
MatrixClientPeg.get(), payload.room_id
95126
);
96127
_setCallListeners(call);
97-
_setCallState(call, call.roomId);
128+
_setCallState(call, call.roomId, "ringback");
98129
if (payload.type === 'voice') {
99130
call.placeVoiceCall();
100131
}
@@ -116,21 +147,21 @@ dis.register(function(payload) {
116147
}
117148
var call = payload.call;
118149
_setCallListeners(call);
119-
_setCallState(call, call.roomId);
150+
_setCallState(call, call.roomId, "ringing");
120151
break;
121152
case 'hangup':
122153
if (!calls[payload.room_id]) {
123154
return; // no call to hangup
124155
}
125156
calls[payload.room_id].hangup();
126-
_setCallState(null, payload.room_id);
157+
_setCallState(null, payload.room_id, "ended");
127158
break;
128159
case 'answer':
129160
if (!calls[payload.room_id]) {
130161
return; // no call to answer
131162
}
132163
calls[payload.room_id].answer();
133-
_setCallState(calls[payload.room_id], payload.room_id);
164+
_setCallState(calls[payload.room_id], payload.room_id, "connected");
134165
break;
135166
}
136167
});

‎src/controllers/molecules/RoomHeader.js‎

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,16 @@ var dis = require("../../dispatcher");
2525
var CallHandler = require("../../CallHandler");
2626

2727
module.exports = {
28-
_setCallState: function(call) {
29-
if (!call) {
30-
this.setState({
31-
callState: "NO_CALL"
32-
});
33-
return;
34-
}
35-
var callState = 'NO_CALL';
36-
if (call.state !== 'ended') {
37-
if (call.state === 'connected') {
38-
callState = "IN_CALL";
39-
}
40-
else if (call.direction === 'outbound') {
41-
callState = "OUTBOUND";
42-
}
43-
else if (call.direction === 'inbound') {
44-
callState = "INBOUND";
45-
}
46-
else {
47-
console.error("Cannot determine call state.");
48-
}
49-
}
50-
this.setState({
51-
callState: callState
52-
});
53-
},
5428

5529
componentDidMount: function() {
5630
this.dispatcherRef = dis.register(this.onAction);
57-
var call;
5831
if (this.props.room) {
59-
call = CallHandler.getCall(this.props.room.roomId);
32+
var call = CallHandler.getCall(this.props.room.roomId);
33+
var callState = call ? call.call_state : "ended";
34+
this.setState({
35+
call_state: callState
36+
});
6037
}
61-
this._setCallState(call);
6238
},
6339

6440
componentWillUnmount: function() {
@@ -75,7 +51,10 @@ module.exports = {
7551
return;
7652
}
7753
var call = CallHandler.getCall(payload.room_id);
78-
this._setCallState(call);
54+
var callState = call ? call.call_state : "ended";
55+
this.setState({
56+
call_state: callState
57+
});
7958
},
8059

8160
onVideoClick: function() {

0 commit comments

Comments
 (0)