app_meetme: Refactor manager events to use stasis
[asterisk/asterisk.git] / apps / confbridge / confbridge_manager.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jonathan Rose <jrose@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Confbridge manager events for stasis messages
22  *
23  * \author Jonathan Rose <jrose@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/channel.h"
31 #include "asterisk/bridging.h"
32 #include "asterisk/stasis.h"
33 #include "asterisk/stasis_channels.h"
34 #include "asterisk/stasis_bridging.h"
35 #include "asterisk/manager.h"
36 #include "asterisk/stasis_message_router.h"
37 #include "include/confbridge.h"
38
39 /*** DOCUMENTATION
40         <managerEvent language="en_US" name="ConfbridgeStart">
41                 <managerEventInstance class="EVENT_FLAG_CALL">
42                         <synopsis>Raised when a conference starts.</synopsis>
43                         <syntax>
44                                 <parameter name="Conference">
45                                         <para>The name of the Confbridge conference.</para>
46                                 </parameter>
47                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
48                         </syntax>
49                         <see-also>
50                                 <ref type="managerEvent">ConfbridgeEnd</ref>
51                                 <ref type="application">ConfBridge</ref>
52                         </see-also>
53                 </managerEventInstance>
54         </managerEvent>
55         <managerEvent language="en_US" name="ConfbridgeEnd">
56                 <managerEventInstance class="EVENT_FLAG_CALL">
57                         <synopsis>Raised when a conference ends.</synopsis>
58                         <syntax>
59                                 <parameter name="Conference">
60                                         <para>The name of the Confbridge conference.</para>
61                                 </parameter>
62                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
63                         </syntax>
64                         <see-also>
65                                 <ref type="managerEvent">ConfbridgeStart</ref>
66                                 <ref type="application">ConfBridge</ref>
67                         </see-also>
68                 </managerEventInstance>
69         </managerEvent>
70         <managerEvent language="en_US" name="ConfbridgeJoin">
71                 <managerEventInstance class="EVENT_FLAG_CALL">
72                         <synopsis>Raised when a channel joins a Confbridge conference.</synopsis>
73                         <syntax>
74                                 <parameter name="Conference">
75                                         <para>The name of the Confbridge conference.</para>
76                                 </parameter>
77                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
78                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
79                         </syntax>
80                         <see-also>
81                                 <ref type="managerEvent">ConfbridgeLeave</ref>
82                                 <ref type="application">ConfBridge</ref>
83                         </see-also>
84                 </managerEventInstance>
85         </managerEvent>
86         <managerEvent language="en_US" name="ConfbridgeLeave">
87                 <managerEventInstance class="EVENT_FLAG_CALL">
88                         <synopsis>Raised when a channel leaves a Confbridge conference.</synopsis>
89                         <syntax>
90                                 <parameter name="Conference">
91                                         <para>The name of the Confbridge conference.</para>
92                                 </parameter>
93                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
94                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
95                         </syntax>
96                         <see-also>
97                                 <ref type="managerEvent">ConfbridgeJoin</ref>
98                                 <ref type="application">ConfBridge</ref>
99                         </see-also>
100                 </managerEventInstance>
101         </managerEvent>
102         <managerEvent language="en_US" name="ConfbridgeRecord">
103                 <managerEventInstance class="EVENT_FLAG_CALL">
104                         <synopsis>Raised when a conference starts recording.</synopsis>
105                         <syntax>
106                                 <parameter name="Conference">
107                                         <para>The name of the Confbridge conference.</para>
108                                 </parameter>
109                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
110                         </syntax>
111                         <see-also>
112                                 <ref type="managerEvent">ConfbridgeStopRecord</ref>
113                                 <ref type="application">ConfBridge</ref>
114                         </see-also>
115                 </managerEventInstance>
116         </managerEvent>
117         <managerEvent language="en_US" name="ConfbridgeStopRecord">
118                 <managerEventInstance class="EVENT_FLAG_CALL">
119                         <synopsis>Raised when a conference that was recording stops recording.</synopsis>
120                         <syntax>
121                                 <parameter name="Conference">
122                                         <para>The name of the Confbridge conference.</para>
123                                 </parameter>
124                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
125                         </syntax>
126                         <see-also>
127                                 <ref type="managerEvent">ConfbridgeRecord</ref>
128                                 <ref type="application">ConfBridge</ref>
129                         </see-also>
130                 </managerEventInstance>
131         </managerEvent>
132         <managerEvent language="en_US" name="ConfbridgeMute">
133                 <managerEventInstance class="EVENT_FLAG_CALL">
134                         <synopsis>Raised when a Confbridge participant mutes.</synopsis>
135                         <syntax>
136                                 <parameter name="Conference">
137                                         <para>The name of the Confbridge conference.</para>
138                                 </parameter>
139                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
140                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
141                         </syntax>
142                         <see-also>
143                                 <ref type="managerEvent">ConfbridgeUnmute</ref>
144                                 <ref type="application">ConfBridge</ref>
145                         </see-also>
146                 </managerEventInstance>
147         </managerEvent>
148         <managerEvent language="en_US" name="ConfbridgeUnmute">
149                 <managerEventInstance class="EVENT_FLAG_CALL">
150                         <synopsis>Raised when a confbridge participant unmutes.</synopsis>
151                         <syntax>
152                                 <parameter name="Conference">
153                                         <para>The name of the Confbridge conference.</para>
154                                 </parameter>
155                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
156                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
157                         </syntax>
158                         <see-also>
159                                 <ref type="managerEvent">ConfbridgeMute</ref>
160                                 <ref type="application">ConfBridge</ref>
161                         </see-also>
162                 </managerEventInstance>
163         </managerEvent>
164
165         <managerEvent language="en_US" name="ConfbridgeTalking">
166                 <managerEventInstance class="EVENT_FLAG_CALL">
167                         <synopsis>Raised when a confbridge participant unmutes.</synopsis>
168                         <syntax>
169                                 <parameter name="Conference">
170                                         <para>The name of the Confbridge conference.</para>
171                                 </parameter>
172                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
173                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
174                                 <parameter name="TalkingStatus">
175                                         <enumlist>
176                                                 <enum name="on"/>
177                                                 <enum name="off"/>
178                                         </enumlist>
179                                 </parameter>
180                         </syntax>
181                         <see-also>
182                                 <ref type="application">ConfBridge</ref>
183                         </see-also>
184                 </managerEventInstance>
185         </managerEvent>
186 ***/
187
188 static struct stasis_message_router *bridge_state_router;
189 static struct stasis_message_router *channel_state_router;
190
191 static void confbridge_publish_manager_event(
192         struct stasis_message *message,
193         const char *event,
194         struct ast_str *extra_text)
195 {
196         struct ast_bridge_blob *blob = stasis_message_data(message);
197         const char *conference_name;
198         RAII_VAR(struct ast_str *, bridge_text,
199                 ast_manager_build_bridge_state_string(blob->bridge, ""),
200                 ast_free);
201         RAII_VAR(struct ast_str *, channel_text, NULL, ast_free);
202
203         ast_assert(blob != NULL);
204         ast_assert(event != NULL);
205
206         conference_name = ast_json_string_get(ast_json_object_get(blob->blob, "conference"));
207         ast_assert(conference_name != NULL);
208
209         if (blob->channel) {
210                 channel_text = ast_manager_build_channel_state_string(blob->channel);
211         }
212
213         manager_event(EVENT_FLAG_CALL, event,
214                 "Conference: %s\r\n"
215                 "%s"
216                 "%s"
217                 "%s",
218                 conference_name,
219                 ast_str_buffer(bridge_text),
220                 S_COR(channel_text, ast_str_buffer(channel_text), ""),
221                 S_COR(extra_text, ast_str_buffer(extra_text), ""));
222 }
223
224 static void confbridge_start_cb(void *data, struct stasis_subscription *sub,
225         struct stasis_topic *topic,
226         struct stasis_message *message)
227 {
228         confbridge_publish_manager_event(message, "ConfbridgeStart", NULL);
229 }
230
231 static void confbridge_end_cb(void *data, struct stasis_subscription *sub,
232         struct stasis_topic *topic,
233         struct stasis_message *message)
234 {
235         confbridge_publish_manager_event(message, "ConfbridgeEnd", NULL);
236 }
237
238 static void confbridge_leave_cb(void *data, struct stasis_subscription *sub,
239         struct stasis_topic *topic,
240         struct stasis_message *message)
241 {
242         confbridge_publish_manager_event(message, "ConfbridgeLeave", NULL);
243 }
244
245 static void confbridge_join_cb(void *data, struct stasis_subscription *sub,
246         struct stasis_topic *topic,
247         struct stasis_message *message)
248 {
249         confbridge_publish_manager_event(message, "ConfbridgeJoin", NULL);
250 }
251
252 static void confbridge_start_record_cb(void *data, struct stasis_subscription *sub,
253         struct stasis_topic *topic,
254         struct stasis_message *message)
255 {
256         confbridge_publish_manager_event(message, "ConfbridgeRecord", NULL);
257 }
258
259 static void confbridge_stop_record_cb(void *data, struct stasis_subscription *sub,
260         struct stasis_topic *topic,
261         struct stasis_message *message)
262 {
263         confbridge_publish_manager_event(message, "ConfbridgeStopRecord", NULL);
264 }
265
266 static void confbridge_mute_cb(void *data, struct stasis_subscription *sub,
267         struct stasis_topic *topic,
268         struct stasis_message *message)
269 {
270         confbridge_publish_manager_event(message, "ConfbridgeMute", NULL);
271 }
272
273 static void confbridge_unmute_cb(void *data, struct stasis_subscription *sub,
274         struct stasis_topic *topic,
275         struct stasis_message *message)
276 {
277         confbridge_publish_manager_event(message, "ConfbridgeUnmute", NULL);
278 }
279
280 static void confbridge_talking_cb(void *data, struct stasis_subscription *sub,
281         struct stasis_topic *topic,
282         struct stasis_message *message)
283 {
284         RAII_VAR(struct ast_str *, extra_text, NULL, ast_free);
285         struct ast_bridge_blob *blob = stasis_message_data(message);
286         const char *talking_status = ast_json_string_get(ast_json_object_get(blob->blob, "talking_status"));
287         if (!talking_status) {
288                 return;
289         }
290
291         ast_str_append_event_header(&extra_text, "TalkingStatus", talking_status);
292         if (!extra_text) {
293                 return;
294         }
295
296         confbridge_publish_manager_event(message, "ConfbridgeTalking", extra_text);
297 }
298
299 STASIS_MESSAGE_TYPE_DEFN(confbridge_start_type);
300 STASIS_MESSAGE_TYPE_DEFN(confbridge_end_type);
301 STASIS_MESSAGE_TYPE_DEFN(confbridge_join_type);
302 STASIS_MESSAGE_TYPE_DEFN(confbridge_leave_type);
303 STASIS_MESSAGE_TYPE_DEFN(confbridge_start_record_type);
304 STASIS_MESSAGE_TYPE_DEFN(confbridge_stop_record_type);
305 STASIS_MESSAGE_TYPE_DEFN(confbridge_mute_type);
306 STASIS_MESSAGE_TYPE_DEFN(confbridge_unmute_type);
307 STASIS_MESSAGE_TYPE_DEFN(confbridge_talking_type);
308
309 void manager_confbridge_shutdown(void) {
310         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_start_type);
311         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_end_type);
312         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_join_type);
313         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_leave_type);
314         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_start_record_type);
315         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_stop_record_type);
316         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_mute_type);
317         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_unmute_type);
318         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_talking_type);
319
320         if (bridge_state_router) {
321                 stasis_message_router_unsubscribe(bridge_state_router);
322                 bridge_state_router = NULL;
323         }
324
325         if (channel_state_router) {
326                 stasis_message_router_unsubscribe(channel_state_router);
327                 channel_state_router = NULL;
328         }
329 }
330
331 int manager_confbridge_init(void)
332 {
333         STASIS_MESSAGE_TYPE_INIT(confbridge_start_type);
334         STASIS_MESSAGE_TYPE_INIT(confbridge_end_type);
335         STASIS_MESSAGE_TYPE_INIT(confbridge_join_type);
336         STASIS_MESSAGE_TYPE_INIT(confbridge_leave_type);
337         STASIS_MESSAGE_TYPE_INIT(confbridge_start_record_type);
338         STASIS_MESSAGE_TYPE_INIT(confbridge_stop_record_type);
339         STASIS_MESSAGE_TYPE_INIT(confbridge_mute_type);
340         STASIS_MESSAGE_TYPE_INIT(confbridge_unmute_type);
341         STASIS_MESSAGE_TYPE_INIT(confbridge_talking_type);
342
343         bridge_state_router = stasis_message_router_create(
344                 stasis_caching_get_topic(ast_bridge_topic_all_cached()));
345
346         if (!bridge_state_router) {
347                 return -1;
348         }
349
350         if (stasis_message_router_add(bridge_state_router,
351                         confbridge_start_type(),
352                         confbridge_start_cb,
353                         NULL)) {
354                 manager_confbridge_shutdown();
355                 return -1;
356         }
357         if (stasis_message_router_add(bridge_state_router,
358                         confbridge_end_type(),
359                         confbridge_end_cb,
360                         NULL)) {
361                 manager_confbridge_shutdown();
362                 return -1;
363         }
364         if (stasis_message_router_add(bridge_state_router,
365                         confbridge_join_type(),
366                         confbridge_join_cb,
367                         NULL)) {
368                 manager_confbridge_shutdown();
369                 return -1;
370         }
371         if (stasis_message_router_add(bridge_state_router,
372                         confbridge_leave_type(),
373                         confbridge_leave_cb,
374                         NULL)) {
375                 manager_confbridge_shutdown();
376                 return -1;
377         }
378         if (stasis_message_router_add(bridge_state_router,
379                         confbridge_start_record_type(),
380                         confbridge_start_record_cb,
381                         NULL)) {
382                 manager_confbridge_shutdown();
383                 return -1;
384         }
385         if (stasis_message_router_add(bridge_state_router,
386                         confbridge_stop_record_type(),
387                         confbridge_stop_record_cb,
388                         NULL)) {
389                 manager_confbridge_shutdown();
390                 return -1;
391         }
392         if (stasis_message_router_add(bridge_state_router,
393                         confbridge_mute_type(),
394                         confbridge_mute_cb,
395                         NULL)) {
396                 manager_confbridge_shutdown();
397                 return -1;
398         }
399         if (stasis_message_router_add(bridge_state_router,
400                         confbridge_unmute_type(),
401                         confbridge_unmute_cb,
402                         NULL)) {
403                 manager_confbridge_shutdown();
404                 return -1;
405         }
406         if (stasis_message_router_add(bridge_state_router,
407                         confbridge_talking_type(),
408                         confbridge_talking_cb,
409                         NULL)) {
410                 manager_confbridge_shutdown();
411                 return -1;
412         }
413
414         channel_state_router = stasis_message_router_create(
415                 stasis_caching_get_topic(ast_channel_topic_all_cached()));
416
417         if (!channel_state_router) {
418                 manager_confbridge_shutdown();
419                 return -1;
420         }
421
422         if (stasis_message_router_add(channel_state_router,
423                         confbridge_start_type(),
424                         confbridge_start_cb,
425                         NULL)) {
426                 manager_confbridge_shutdown();
427                 return -1;
428         }
429         if (stasis_message_router_add(channel_state_router,
430                         confbridge_end_type(),
431                         confbridge_end_cb,
432                         NULL)) {
433                 manager_confbridge_shutdown();
434                 return -1;
435         }
436         if (stasis_message_router_add(channel_state_router,
437                         confbridge_join_type(),
438                         confbridge_join_cb,
439                         NULL)) {
440                 manager_confbridge_shutdown();
441                 return -1;
442         }
443         if (stasis_message_router_add(channel_state_router,
444                         confbridge_leave_type(),
445                         confbridge_leave_cb,
446                         NULL)) {
447                 manager_confbridge_shutdown();
448                 return -1;
449         }
450         if (stasis_message_router_add(channel_state_router,
451                         confbridge_start_record_type(),
452                         confbridge_start_record_cb,
453                         NULL)) {
454                 manager_confbridge_shutdown();
455                 return -1;
456         }
457         if (stasis_message_router_add(channel_state_router,
458                         confbridge_stop_record_type(),
459                         confbridge_stop_record_cb,
460                         NULL)) {
461                 manager_confbridge_shutdown();
462                 return -1;
463         }
464         if (stasis_message_router_add(channel_state_router,
465                         confbridge_mute_type(),
466                         confbridge_mute_cb,
467                         NULL)) {
468                 manager_confbridge_shutdown();
469                 return -1;
470         }
471         if (stasis_message_router_add(channel_state_router,
472                         confbridge_unmute_type(),
473                         confbridge_unmute_cb,
474                         NULL)) {
475                 manager_confbridge_shutdown();
476                 return -1;
477         }
478         if (stasis_message_router_add(channel_state_router,
479                         confbridge_talking_type(),
480                         confbridge_talking_cb,
481                         NULL)) {
482                 manager_confbridge_shutdown();
483                 return -1;
484         }
485
486         return 0;
487 }