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+ } ;
0 commit comments