Raise the ConfBridgeMute/Unmute events when a CLI or AMI action triggers the change
[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         <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                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='BridgeCreate']/managerEventInstance/syntax/parameter)" />
172                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
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 append_event_header(struct ast_str **fields_string,
191                                         const char *header, const char *value)
192 {
193         struct ast_str *working_str = *fields_string;
194
195         if (!working_str) {
196                 working_str = ast_str_create(128);
197                 if (!working_str) {
198                         return;
199                 }
200                 *fields_string = working_str;
201         }
202
203         ast_str_append(&working_str, 0,
204                 "%s: %s\r\n",
205                 header, value);
206 }
207
208 static void stasis_confbridge_cb(void *data, struct stasis_subscription *sub,
209                                         struct stasis_topic *topic,
210                                         struct stasis_message *message)
211 {
212         struct ast_bridge_blob *blob = stasis_message_data(message);
213         const char *type = ast_bridge_blob_json_type(blob);
214         const char *conference_name;
215         RAII_VAR(struct ast_str *, bridge_text, NULL, ast_free);
216         RAII_VAR(struct ast_str *, channel_text, NULL, ast_free);
217         RAII_VAR(struct ast_str *, extra_text, NULL, ast_free);
218         char *event;
219
220         if (!blob || !type) {
221                 ast_assert(0);
222                 return;
223         }
224
225         if (!strcmp("confbridge_start", type)) {
226                 event = "ConfbridgeStart";
227         } else if (!strcmp("confbridge_end", type)) {
228                 event = "ConfbridgeEnd";
229         } else if (!strcmp("confbridge_leave", type)) {
230                 event = "ConfbridgeLeave";
231         } else if (!strcmp("confbridge_join", type)) {
232                 event = "ConfbridgeJoin";
233         } else if (!strcmp("confbridge_record", type)) {
234                 event = "ConfbridgeRecord";
235         } else if (!strcmp("confbridge_stop_record", type)) {
236                 event = "ConfbridgeStopRecord";
237         } else if (!strcmp("confbridge_mute", type)) {
238                 event = "ConfbridgeMute";
239         } else if (!strcmp("confbridge_unmute", type)) {
240                 event = "ConfbridgeUnmute";
241         } else if (!strcmp("confbridge_talking", type)) {
242                 const char *talking_status = ast_json_string_get(ast_json_object_get(blob->blob, "talking_status"));
243                 event = "ConfbridgeTalking";
244
245                 if (!talking_status) {
246                         return;
247                 }
248
249                 append_event_header(&extra_text, "TalkingStatus", talking_status);
250
251         } else {
252                 return;
253         }
254
255         conference_name = ast_json_string_get(ast_json_object_get(blob->blob, "conference"));
256
257         if (!conference_name) {
258                 ast_assert(0);
259                 return;
260         }
261
262         bridge_text = ast_manager_build_bridge_state_string(blob->bridge, "");
263         if (blob->channel) {
264                 channel_text = ast_manager_build_channel_state_string(blob->channel);
265         }
266
267         manager_event(EVENT_FLAG_CALL, event,
268                 "Conference: %s\r\n"
269                 "%s"
270                 "%s"
271                 "%s",
272                 conference_name,
273                 ast_str_buffer(bridge_text),
274                 channel_text ? ast_str_buffer(channel_text) : "",
275                 extra_text ? ast_str_buffer(extra_text) : "");
276 }
277
278 static struct stasis_message_type *confbridge_msg_type;
279
280 struct stasis_message_type *confbridge_message_type(void)
281 {
282         return confbridge_msg_type;
283 }
284
285 void manager_confbridge_shutdown(void) {
286         ao2_cleanup(confbridge_msg_type);
287         confbridge_msg_type = NULL;
288
289         if (bridge_state_router) {
290                 stasis_message_router_unsubscribe(bridge_state_router);
291                 bridge_state_router = NULL;
292         }
293
294         if (channel_state_router) {
295                 stasis_message_router_unsubscribe(channel_state_router);
296                 channel_state_router = NULL;
297         }
298 }
299
300 int manager_confbridge_init(void)
301 {
302         if (!(confbridge_msg_type = stasis_message_type_create("confbridge"))) {
303                 return -1;
304         }
305
306         bridge_state_router = stasis_message_router_create(
307                 stasis_caching_get_topic(ast_bridge_topic_all_cached()));
308
309         if (!bridge_state_router) {
310                 return -1;
311         }
312
313         if (stasis_message_router_add(bridge_state_router,
314                                          confbridge_message_type(),
315                                          stasis_confbridge_cb,
316                                          NULL)) {
317                 manager_confbridge_shutdown();
318                 return -1;
319         }
320
321         channel_state_router = stasis_message_router_create(
322                 stasis_caching_get_topic(ast_channel_topic_all_cached()));
323
324         if (!channel_state_router) {
325                 manager_confbridge_shutdown();
326                 return -1;
327         }
328
329         if (stasis_message_router_add(channel_state_router,
330                                          confbridge_message_type(),
331                                          stasis_confbridge_cb,
332                                          NULL)) {
333                 manager_confbridge_shutdown();
334                 return -1;
335         }
336
337         return 0;
338 }