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/removed from this list:
23+ * {
24+ * action: 'call_state'
25+ * room_id: <room ID of the call>
26+ * }
27+ *
28+ * To know if the call was added/removed, this handler exposes a getter to
29+ * obtain the call for a room:
30+ * CallHandler.getCall(roomId)
31+ *
32+ * This handler listens for and handles the following actions:
33+ * {
34+ * action: 'place_call',
35+ * type: 'voice|video',
36+ * room_id: <room that the place call button was pressed in>
37+ * }
38+ *
39+ * {
40+ * action: 'incoming_call'
41+ * call: MatrixCall
42+ * }
43+ *
44+ * {
45+ * action: 'hangup'
46+ * room_id: <room that the hangup button was pressed in>
47+ * }
48+ *
49+ * {
50+ * action: 'answer'
51+ * room_id: <room that the answer button was pressed in>
52+ * }
53+ */
54+
55+ var MatrixClientPeg = require ( "./MatrixClientPeg" ) ;
56+ var Matrix = require ( "matrix-js-sdk" ) ;
57+ var dis = require ( "./dispatcher" ) ;
58+
59+ var calls = {
60+ //room_id: MatrixCall
61+ } ;
62+
63+ function _setCallListeners ( call ) {
64+ call . on ( "error" , function ( err ) {
65+ console . error ( "Call error: %s" , err ) ;
66+ console . error ( err . stack ) ;
67+ call . hangup ( ) ;
68+ _setCallState ( undefined , call . roomId ) ;
69+ } ) ;
70+ call . on ( "hangup" , function ( ) {
71+ _setCallState ( undefined , call . roomId ) ;
72+ } ) ;
73+ }
74+
75+ function _setCallState ( call , roomId ) {
76+ console . log ( "_setState >>> %s >>> %s " , call , roomId ) ;
77+ calls [ roomId ] = call ;
78+ dis . dispatch ( {
79+ action : 'call_state' ,
80+ room_id : roomId
81+ } ) ;
82+ }
83+
84+ dis . register ( function ( payload ) {
85+ switch ( payload . action ) {
86+ case 'place_call' :
87+ if ( calls [ payload . room_id ] ) {
88+ return ; // don't allow >1 call to be placed.
89+ }
90+ console . log ( "Place %s call in %s" , payload . type , payload . room_id ) ;
91+ var call = Matrix . createNewMatrixCall (
92+ MatrixClientPeg . get ( ) , payload . room_id
93+ ) ;
94+ _setCallListeners ( call ) ;
95+ _setCallState ( call , call . roomId ) ;
96+ if ( payload . type === 'voice' ) {
97+ call . placeVoiceCall ( ) ;
98+ }
99+ else if ( payload . type === 'video' ) {
100+ call . placeVideoCall (
101+ payload . remote_element ,
102+ payload . local_element
103+ ) ;
104+ }
105+ else {
106+ console . error ( "Unknown call type: %s" , payload . type ) ;
107+ }
108+
109+ break ;
110+ case 'incoming_call' :
111+ if ( calls [ payload . call . roomId ] ) {
112+ payload . call . hangup ( "busy" ) ;
113+ return ; // don't allow >1 call to be received, hangup newer one.
114+ }
115+ var call = payload . call ;
116+ _setCallListeners ( call ) ;
117+ _setCallState ( call , call . roomId ) ;
118+ break ;
119+ case 'hangup' :
120+ if ( ! calls [ payload . room_id ] ) {
121+ return ; // no call to hangup
122+ }
123+ calls [ payload . room_id ] . hangup ( ) ;
124+ _setCallState ( null , payload . room_id ) ;
125+ break ;
126+ case 'answer' :
127+ if ( ! calls [ payload . room_id ] ) {
128+ return ; // no call to answer
129+ }
130+ calls [ payload . room_id ] . answer ( ) ;
131+ break ;
132+ }
133+ } ) ;
134+
135+ module . exports = {
136+ getCall : function ( roomId ) {
137+ return calls [ roomId ] || null ;
138+ }
139+ } ;
0 commit comments