Fix documentation replication issues
[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/bridge.h"
32 #include "asterisk/stasis.h"
33 #include "asterisk/stasis_channels.h"
34 #include "asterisk/stasis_bridges.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                                 <bridge_snapshot/>
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                                 <bridge_snapshot/>
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                                 <bridge_snapshot/>
78                                 <channel_snapshot/>
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                                 <bridge_snapshot/>
94                                 <channel_snapshot/>
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                                 <bridge_snapshot/>
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                                 <bridge_snapshot/>
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                                 <bridge_snapshot/>
140                                 <channel_snapshot/>
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                                 <bridge_snapshot/>
156                                 <channel_snapshot/>
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         <managerEvent language="en_US" name="ConfbridgeTalking">
165                 <managerEventInstance class="EVENT_FLAG_CALL">
166                         <synopsis>Raised when a confbridge participant unmutes.</synopsis>
167                         <syntax>
168                                 <parameter name="Conference">
169                                         <para>The name of the Confbridge conference.</para>
170                                 </parameter>
171                                 <bridge_snapshot/>
172                                 <channel_snapshot/>
173                                 <parameter name="TalkingStatus">
174                                         <enumlist>
175                                                 <enum name="on"/>
176                                                 <enum name="off"/>
177                                         </enumlist>
178                                 </parameter>
179                         </syntax>
180                         <see-also>
181                                 <ref type="application">ConfBridge</ref>
182                         </see-also>
183                 </managerEventInstance>
184         </managerEvent>
185 ***/
186
187 static struct stasis_message_router *bridge_state_router;
188 static struct stasis_message_router *channel_state_router;
189
190 static void confbridge_publish_manager_event(
191         struct stasis_message *message,
192         const char *event,
193         struct ast_str *extra_text)
194 {
195         struct ast_bridge_blob *blob = stasis_message_data(message);
196         const char *conference_name;
197         RAII_VAR(struct ast_str *, bridge_text, NULL, ast_free);
198         RAII_VAR(struct ast_str *, channel_text, NULL, ast_free);
199
200         ast_assert(blob != NULL);
201         ast_assert(event != NULL);
202
203         bridge_text = ast_manager_build_bridge_state_string(blob->bridge, "");
204         if (!bridge_text) {
205                 return;
206         }
207
208         conference_name = ast_json_string_get(ast_json_object_get(blob->blob, "conference"));
209         ast_assert(conference_name != NULL);
210
211         if (blob->channel) {
212                 channel_text = ast_manager_build_channel_state_string(blob->channel);
213         }
214
215         manager_event(EVENT_FLAG_CALL, event,
216                 "Conference: %s\r\n"
217                 "%s"
218                 "%s"
219                 "%s",
220                 conference_name,
221                 ast_str_buffer(bridge_text),
222                 S_COR(channel_text, ast_str_buffer(channel_text), ""),
223                 S_COR(extra_text, ast_str_buffer(extra_text), ""));
224 }
225
226 static void confbridge_start_cb(void *data, struct stasis_subscription *sub,
227         struct stasis_topic *topic,
228         struct stasis_message *message)
229 {
230         confbridge_publish_manager_event(message, "ConfbridgeStart", NULL);
231 }
232
233 static void confbridge_end_cb(void *data, struct stasis_subscription *sub,
234         struct stasis_topic *topic,
235         struct stasis_message *message)
236 {
237         confbridge_publish_manager_event(message, "ConfbridgeEnd", NULL);
238 }
239
240 static void confbridge_leave_cb(void *data, struct stasis_subscription *sub,
241         struct stasis_topic *topic,
242         struct stasis_message *message)
243 {
244         confbridge_publish_manager_event(message, "ConfbridgeLeave", NULL);
245 }
246
247 static void confbridge_join_cb(void *data, struct stasis_subscription *sub,
248         struct stasis_topic *topic,
249         struct stasis_message *message)
250 {
251         confbridge_publish_manager_event(message, "ConfbridgeJoin", NULL);
252 }
253
254 static void confbridge_start_record_cb(void *data, struct stasis_subscription *sub,
255         struct stasis_topic *topic,
256         struct stasis_message *message)
257 {
258         confbridge_publish_manager_event(message, "ConfbridgeRecord", NULL);
259 }
260
261 static void confbridge_stop_record_cb(void *data, struct stasis_subscription *sub,
262         struct stasis_topic *topic,
263         struct stasis_message *message)
264 {
265         confbridge_publish_manager_event(message, "ConfbridgeStopRecord", NULL);
266 }
267
268 static void confbridge_mute_cb(void *data, struct stasis_subscription *sub,
269         struct stasis_topic *topic,
270         struct stasis_message *message)
271 {
272         confbridge_publish_manager_event(message, "ConfbridgeMute", NULL);
273 }
274
275 static void confbridge_unmute_cb(void *data, struct stasis_subscription *sub,
276         struct stasis_topic *topic,
277         struct stasis_message *message)
278 {
279         confbridge_publish_manager_event(message, "ConfbridgeUnmute", NULL);
280 }
281
282 static void confbridge_talking_cb(void *data, struct stasis_subscription *sub,
283         struct stasis_topic *topic,
284         struct stasis_message *message)
285 {
286         RAII_VAR(struct ast_str *, extra_text, NULL, ast_free);
287         struct ast_bridge_blob *blob = stasis_message_data(message);
288         const char *talking_status = ast_json_string_get(ast_json_object_get(blob->blob, "talking_status"));
289         if (!talking_status) {
290                 return;
291         }
292
293         ast_str_append_event_header(&extra_text, "TalkingStatus", talking_status);
294         if (!extra_text) {
295                 return;
296         }
297
298         confbridge_publish_manager_event(message, "ConfbridgeTalking", extra_text);
299 }
300
301 STASIS_MESSAGE_TYPE_DEFN(confbridge_start_type);
302 STASIS_MESSAGE_TYPE_DEFN(confbridge_end_type);
303 STASIS_MESSAGE_TYPE_DEFN(confbridge_join_type);
304 STASIS_MESSAGE_TYPE_DEFN(confbridge_leave_type);
305 STASIS_MESSAGE_TYPE_DEFN(confbridge_start_record_type);
306 STASIS_MESSAGE_TYPE_DEFN(confbridge_stop_record_type);
307 STASIS_MESSAGE_TYPE_DEFN(confbridge_mute_type);
308 STASIS_MESSAGE_TYPE_DEFN(confbridge_unmute_type);
309 STASIS_MESSAGE_TYPE_DEFN(confbridge_talking_type);
310
311 void manager_confbridge_shutdown(void) {
312         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_start_type);
313         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_end_type);
314         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_join_type);
315         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_leave_type);
316         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_start_record_type);
317         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_stop_record_type);
318         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_mute_type);
319         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_unmute_type);
320         STASIS_MESSAGE_TYPE_CLEANUP(confbridge_talking_type);
321
322         if (bridge_state_router) {
323                 stasis_message_router_unsubscribe(bridge_state_router);
324                 bridge_state_router = NULL;
325         }
326
327         if (channel_state_router) {
328                 stasis_message_router_unsubscribe(channel_state_router);
329                 channel_state_router = NULL;
330         }
331 }
332
333 int manager_confbridge_init(void)
334 {
335         STASIS_MESSAGE_TYPE_INIT(confbridge_start_type);
336         STASIS_MESSAGE_TYPE_INIT(confbridge_end_type);
337         STASIS_MESSAGE_TYPE_INIT(confbridge_join_type);
338         STASIS_MESSAGE_TYPE_INIT(confbridge_leave_type);
339         STASIS_MESSAGE_TYPE_INIT(confbridge_start_record_type);
340         STASIS_MESSAGE_TYPE_INIT(confbridge_stop_record_type);
341         STASIS_MESSAGE_TYPE_INIT(confbridge_mute_type);
342         STASIS_MESSAGE_TYPE_INIT(confbridge_unmute_type);
343         STASIS_MESSAGE_TYPE_INIT(confbridge_talking_type);
344
345         bridge_state_router = stasis_message_router_create(
346                 ast_bridge_topic_all_cached());
347
348         if (!bridge_state_router) {
349                 return -1;
350         }
351
352         if (stasis_message_router_add(bridge_state_router,
353                         confbridge_start_type(),
354                         confbridge_start_cb,
355                         NULL)) {
356                 manager_confbridge_shutdown();
357                 return -1;
358         }
359         if (stasis_message_router_add(bridge_state_router,
360                         confbridge_end_type(),
361                         confbridge_end_cb,
362                         NULL)) {
363                 manager_confbridge_shutdown();
364                 return -1;
365         }
366         if (stasis_message_router_add(bridge_state_router,
367                         confbridge_join_type(),
368                         confbridge_join_cb,
369                         NULL)) {
370                 manager_confbridge_shutdown();
371                 return -1;
372         }
373         if (stasis_message_router_add(bridge_state_router,
374                         confbridge_leave_type(),
375                         confbridge_leave_cb,
376                         NULL)) {
377                 manager_confbridge_shutdown();
378                 return -1;
379         }
380         if (stasis_message_router_add(bridge_state_router,
381                         confbridge_start_record_type(),
382                         confbridge_start_record_cb,
383                         NULL)) {
384                 manager_confbridge_shutdown();
385                 return -1;
386         }
387         if (stasis_message_router_add(bridge_state_router,
388                         confbridge_stop_record_type(),
389                         confbridge_stop_record_cb,
390                         NULL)) {
391                 manager_confbridge_shutdown();
392                 return -1;
393         }
394         if (stasis_message_router_add(bridge_state_router,
395                         confbridge_mute_type(),
396                         confbridge_mute_cb,
397                         NULL)) {
398                 manager_confbridge_shutdown();
399                 return -1;
400         }
401         if (stasis_message_router_add(bridge_state_router,
402                         confbridge_unmute_type(),
403                         confbridge_unmute_cb,
404                         NULL)) {
405                 manager_confbridge_shutdown();
406                 return -1;
407         }
408         if (stasis_message_router_add(bridge_state_router,
409                         confbridge_talking_type(),
410                         confbridge_talking_cb,
411                         NULL)) {
412                 manager_confbridge_shutdown();
413                 return -1;
414         }
415
416         channel_state_router = stasis_message_router_create(
417                 ast_channel_topic_all_cached());
418
419         if (!channel_state_router) {
420                 manager_confbridge_shutdown();
421                 return -1;
422         }
423
424         if (stasis_message_router_add(channel_state_router,
425                         confbridge_start_type(),
426                         confbridge_start_cb,
427                         NULL)) {
428                 manager_confbridge_shutdown();
429                 return -1;
430         }
431         if (stasis_message_router_add(channel_state_router,
432                         confbridge_end_type(),
433                         confbridge_end_cb,
434                         NULL)) {
435                 manager_confbridge_shutdown();
436                 return -1;
437         }
438         if (stasis_message_router_add(channel_state_router,
439                         confbridge_join_type(),
440                         confbridge_join_cb,
441                         NULL)) {
442                 manager_confbridge_shutdown();
443                 return -1;
444         }
445         if (stasis_message_router_add(channel_state_router,
446                         confbridge_leave_type(),
447                         confbridge_leave_cb,
448                         NULL)) {
449                 manager_confbridge_shutdown();
450                 return -1;
451         }
452         if (stasis_message_router_add(channel_state_router,
453                         confbridge_start_record_type(),
454                         confbridge_start_record_cb,
455                         NULL)) {
456                 manager_confbridge_shutdown();
457                 return -1;
458         }
459         if (stasis_message_router_add(channel_state_router,
460                         confbridge_stop_record_type(),
461                         confbridge_stop_record_cb,
462                         NULL)) {
463                 manager_confbridge_shutdown();
464                 return -1;
465         }
466         if (stasis_message_router_add(channel_state_router,
467                         confbridge_mute_type(),
468                         confbridge_mute_cb,
469                         NULL)) {
470                 manager_confbridge_shutdown();
471                 return -1;
472         }
473         if (stasis_message_router_add(channel_state_router,
474                         confbridge_unmute_type(),
475                         confbridge_unmute_cb,
476                         NULL)) {
477                 manager_confbridge_shutdown();
478                 return -1;
479         }
480         if (stasis_message_router_add(channel_state_router,
481                         confbridge_talking_type(),
482                         confbridge_talking_cb,
483                         NULL)) {
484                 manager_confbridge_shutdown();
485                 return -1;
486         }
487
488         return 0;
489 }