confbridge_manager.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2013, Digium, Inc.
00005  *
00006  * Jonathan Rose <jrose@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Confbridge manager events for stasis messages
00022  *
00023  * \author Jonathan Rose <jrose@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 422178 $")
00029 
00030 #include "asterisk/channel.h"
00031 #include "asterisk/bridge.h"
00032 #include "asterisk/stasis.h"
00033 #include "asterisk/stasis_channels.h"
00034 #include "asterisk/stasis_bridges.h"
00035 #include "asterisk/manager.h"
00036 #include "asterisk/stasis_message_router.h"
00037 #include "include/confbridge.h"
00038 
00039 /*** DOCUMENTATION
00040    <managerEvent language="en_US" name="ConfbridgeStart">
00041       <managerEventInstance class="EVENT_FLAG_CALL">
00042          <synopsis>Raised when a conference starts.</synopsis>
00043          <syntax>
00044             <parameter name="Conference">
00045                <para>The name of the Confbridge conference.</para>
00046             </parameter>
00047             <bridge_snapshot/>
00048          </syntax>
00049          <see-also>
00050             <ref type="managerEvent">ConfbridgeEnd</ref>
00051             <ref type="application">ConfBridge</ref>
00052          </see-also>
00053       </managerEventInstance>
00054    </managerEvent>
00055    <managerEvent language="en_US" name="ConfbridgeEnd">
00056       <managerEventInstance class="EVENT_FLAG_CALL">
00057          <synopsis>Raised when a conference ends.</synopsis>
00058          <syntax>
00059             <parameter name="Conference">
00060                <para>The name of the Confbridge conference.</para>
00061             </parameter>
00062             <bridge_snapshot/>
00063          </syntax>
00064          <see-also>
00065             <ref type="managerEvent">ConfbridgeStart</ref>
00066             <ref type="application">ConfBridge</ref>
00067          </see-also>
00068       </managerEventInstance>
00069    </managerEvent>
00070    <managerEvent language="en_US" name="ConfbridgeJoin">
00071       <managerEventInstance class="EVENT_FLAG_CALL">
00072          <synopsis>Raised when a channel joins a Confbridge conference.</synopsis>
00073          <syntax>
00074             <parameter name="Conference">
00075                <para>The name of the Confbridge conference.</para>
00076             </parameter>
00077             <bridge_snapshot/>
00078             <channel_snapshot/>
00079             <parameter name="Admin">
00080                <para>Identifies this user as an admin user.</para>
00081                <enumlist>
00082                   <enum name="Yes"/>
00083                   <enum name="No"/>
00084                </enumlist>
00085             </parameter>
00086          </syntax>
00087          <see-also>
00088             <ref type="managerEvent">ConfbridgeLeave</ref>
00089             <ref type="application">ConfBridge</ref>
00090          </see-also>
00091       </managerEventInstance>
00092    </managerEvent>
00093    <managerEvent language="en_US" name="ConfbridgeLeave">
00094       <managerEventInstance class="EVENT_FLAG_CALL">
00095          <synopsis>Raised when a channel leaves a Confbridge conference.</synopsis>
00096          <syntax>
00097             <parameter name="Conference">
00098                <para>The name of the Confbridge conference.</para>
00099             </parameter>
00100             <bridge_snapshot/>
00101             <channel_snapshot/>
00102             <parameter name="Admin">
00103                <para>Identifies this user as an admin user.</para>
00104                <enumlist>
00105                   <enum name="Yes"/>
00106                   <enum name="No"/>
00107                </enumlist>
00108             </parameter>
00109          </syntax>
00110          <see-also>
00111             <ref type="managerEvent">ConfbridgeJoin</ref>
00112             <ref type="application">ConfBridge</ref>
00113          </see-also>
00114       </managerEventInstance>
00115    </managerEvent>
00116    <managerEvent language="en_US" name="ConfbridgeRecord">
00117       <managerEventInstance class="EVENT_FLAG_CALL">
00118          <synopsis>Raised when a conference starts recording.</synopsis>
00119          <syntax>
00120             <parameter name="Conference">
00121                <para>The name of the Confbridge conference.</para>
00122             </parameter>
00123             <bridge_snapshot/>
00124          </syntax>
00125          <see-also>
00126             <ref type="managerEvent">ConfbridgeStopRecord</ref>
00127             <ref type="application">ConfBridge</ref>
00128          </see-also>
00129       </managerEventInstance>
00130    </managerEvent>
00131    <managerEvent language="en_US" name="ConfbridgeStopRecord">
00132       <managerEventInstance class="EVENT_FLAG_CALL">
00133          <synopsis>Raised when a conference that was recording stops recording.</synopsis>
00134          <syntax>
00135             <parameter name="Conference">
00136                <para>The name of the Confbridge conference.</para>
00137             </parameter>
00138             <bridge_snapshot/>
00139          </syntax>
00140          <see-also>
00141             <ref type="managerEvent">ConfbridgeRecord</ref>
00142             <ref type="application">ConfBridge</ref>
00143          </see-also>
00144       </managerEventInstance>
00145    </managerEvent>
00146    <managerEvent language="en_US" name="ConfbridgeMute">
00147       <managerEventInstance class="EVENT_FLAG_CALL">
00148          <synopsis>Raised when a Confbridge participant mutes.</synopsis>
00149          <syntax>
00150             <parameter name="Conference">
00151                <para>The name of the Confbridge conference.</para>
00152             </parameter>
00153             <bridge_snapshot/>
00154             <channel_snapshot/>
00155             <parameter name="Admin">
00156                <para>Identifies this user as an admin user.</para>
00157                <enumlist>
00158                   <enum name="Yes"/>
00159                   <enum name="No"/>
00160                </enumlist>
00161             </parameter>
00162          </syntax>
00163          <see-also>
00164             <ref type="managerEvent">ConfbridgeUnmute</ref>
00165             <ref type="application">ConfBridge</ref>
00166          </see-also>
00167       </managerEventInstance>
00168    </managerEvent>
00169    <managerEvent language="en_US" name="ConfbridgeUnmute">
00170       <managerEventInstance class="EVENT_FLAG_CALL">
00171          <synopsis>Raised when a confbridge participant unmutes.</synopsis>
00172          <syntax>
00173             <parameter name="Conference">
00174                <para>The name of the Confbridge conference.</para>
00175             </parameter>
00176             <bridge_snapshot/>
00177             <channel_snapshot/>
00178             <parameter name="Admin">
00179                <para>Identifies this user as an admin user.</para>
00180                <enumlist>
00181                   <enum name="Yes"/>
00182                   <enum name="No"/>
00183                </enumlist>
00184             </parameter>
00185          </syntax>
00186          <see-also>
00187             <ref type="managerEvent">ConfbridgeMute</ref>
00188             <ref type="application">ConfBridge</ref>
00189          </see-also>
00190       </managerEventInstance>
00191    </managerEvent>
00192    <managerEvent language="en_US" name="ConfbridgeTalking">
00193       <managerEventInstance class="EVENT_FLAG_CALL">
00194          <synopsis>Raised when a confbridge participant unmutes.</synopsis>
00195          <syntax>
00196             <parameter name="Conference">
00197                <para>The name of the Confbridge conference.</para>
00198             </parameter>
00199             <bridge_snapshot/>
00200             <channel_snapshot/>
00201             <parameter name="TalkingStatus">
00202                <enumlist>
00203                   <enum name="on"/>
00204                   <enum name="off"/>
00205                </enumlist>
00206             </parameter>
00207             <parameter name="Admin">
00208                <para>Identifies this user as an admin user.</para>
00209                <enumlist>
00210                   <enum name="Yes"/>
00211                   <enum name="No"/>
00212                </enumlist>
00213             </parameter>
00214          </syntax>
00215          <see-also>
00216             <ref type="application">ConfBridge</ref>
00217          </see-also>
00218       </managerEventInstance>
00219    </managerEvent>
00220 ***/
00221 
00222 static struct stasis_message_router *bridge_state_router;
00223 static struct stasis_message_router *channel_state_router;
00224 
00225 static void confbridge_publish_manager_event(
00226    struct stasis_message *message,
00227    const char *event,
00228    struct ast_str *extra_text)
00229 {
00230    struct ast_bridge_blob *blob = stasis_message_data(message);
00231    const char *conference_name;
00232    RAII_VAR(struct ast_str *, bridge_text, NULL, ast_free);
00233    RAII_VAR(struct ast_str *, channel_text, NULL, ast_free);
00234 
00235    ast_assert(blob != NULL);
00236    ast_assert(event != NULL);
00237 
00238    bridge_text = ast_manager_build_bridge_state_string(blob->bridge);
00239    if (!bridge_text) {
00240       return;
00241    }
00242 
00243    conference_name = ast_json_string_get(ast_json_object_get(blob->blob, "conference"));
00244    ast_assert(conference_name != NULL);
00245 
00246    if (blob->channel) {
00247       channel_text = ast_manager_build_channel_state_string(blob->channel);
00248    }
00249 
00250    manager_event(EVENT_FLAG_CALL, event,
00251       "Conference: %s\r\n"
00252       "%s"
00253       "%s"
00254       "%s",
00255       conference_name,
00256       ast_str_buffer(bridge_text),
00257       S_COR(channel_text, ast_str_buffer(channel_text), ""),
00258       S_COR(extra_text, ast_str_buffer(extra_text), ""));
00259 }
00260 
00261 static int get_admin_header(struct ast_str **extra_text, struct stasis_message *message)
00262 {
00263    const struct ast_bridge_blob *blob = stasis_message_data(message);
00264    const struct ast_json *admin = ast_json_object_get(blob->blob, "admin");
00265    if (!admin) {
00266       return -1;
00267    }
00268 
00269    return ast_str_append_event_header(extra_text, "Admin",
00270       S_COR(ast_json_is_true(admin), "Yes", "No"));
00271 }
00272 
00273 static void confbridge_start_cb(void *data, struct stasis_subscription *sub,
00274    struct stasis_message *message)
00275 {
00276    confbridge_publish_manager_event(message, "ConfbridgeStart", NULL);
00277 }
00278 
00279 static void confbridge_end_cb(void *data, struct stasis_subscription *sub,
00280    struct stasis_message *message)
00281 {
00282    confbridge_publish_manager_event(message, "ConfbridgeEnd", NULL);
00283 }
00284 
00285 static void confbridge_leave_cb(void *data, struct stasis_subscription *sub,
00286    struct stasis_message *message)
00287 {
00288    struct ast_str *extra_text = NULL;
00289 
00290    if (!get_admin_header(&extra_text, message)) {
00291       confbridge_publish_manager_event(message, "ConfbridgeLeave", extra_text);
00292    }
00293    ast_free(extra_text);
00294 }
00295 
00296 static void confbridge_join_cb(void *data, struct stasis_subscription *sub,
00297    struct stasis_message *message)
00298 {
00299    struct ast_str *extra_text = NULL;
00300 
00301    if (!get_admin_header(&extra_text, message)) {
00302       confbridge_publish_manager_event(message, "ConfbridgeJoin", extra_text);
00303    }
00304    ast_free(extra_text);
00305 }
00306 
00307 static void confbridge_start_record_cb(void *data, struct stasis_subscription *sub,
00308    struct stasis_message *message)
00309 {
00310    confbridge_publish_manager_event(message, "ConfbridgeRecord", NULL);
00311 }
00312 
00313 static void confbridge_stop_record_cb(void *data, struct stasis_subscription *sub,
00314    struct stasis_message *message)
00315 {
00316    confbridge_publish_manager_event(message, "ConfbridgeStopRecord", NULL);
00317 }
00318 
00319 static void confbridge_mute_cb(void *data, struct stasis_subscription *sub,
00320    struct stasis_message *message)
00321 {
00322    struct ast_str *extra_text = NULL;
00323 
00324    if (!get_admin_header(&extra_text, message)) {
00325       confbridge_publish_manager_event(message, "ConfbridgeMute", extra_text);
00326    }
00327    ast_free(extra_text);
00328 }
00329 
00330 static void confbridge_unmute_cb(void *data, struct stasis_subscription *sub,
00331    struct stasis_message *message)
00332 {
00333    struct ast_str *extra_text = NULL;
00334 
00335    if (!get_admin_header(&extra_text, message)) {
00336       confbridge_publish_manager_event(message, "ConfbridgeUnmute", extra_text);
00337    }
00338    ast_free(extra_text);
00339 }
00340 
00341 static void confbridge_talking_cb(void *data, struct stasis_subscription *sub,
00342    struct stasis_message *message)
00343 {
00344    RAII_VAR(struct ast_str *, extra_text, NULL, ast_free);
00345    const struct ast_bridge_blob *blob = stasis_message_data(message);
00346    const char *talking_status = ast_json_string_get(ast_json_object_get(blob->blob, "talking_status"));
00347    if (!talking_status) {
00348       return;
00349    }
00350 
00351    ast_str_append_event_header(&extra_text, "TalkingStatus", talking_status);
00352    if (!extra_text) {
00353       return;
00354    }
00355 
00356    if (!get_admin_header(&extra_text, message)) {
00357       confbridge_publish_manager_event(message, "ConfbridgeTalking", extra_text);
00358    }
00359 }
00360 
00361 STASIS_MESSAGE_TYPE_DEFN(confbridge_start_type);
00362 STASIS_MESSAGE_TYPE_DEFN(confbridge_end_type);
00363 STASIS_MESSAGE_TYPE_DEFN(confbridge_join_type);
00364 STASIS_MESSAGE_TYPE_DEFN(confbridge_leave_type);
00365 STASIS_MESSAGE_TYPE_DEFN(confbridge_start_record_type);
00366 STASIS_MESSAGE_TYPE_DEFN(confbridge_stop_record_type);
00367 STASIS_MESSAGE_TYPE_DEFN(confbridge_mute_type);
00368 STASIS_MESSAGE_TYPE_DEFN(confbridge_unmute_type);
00369 STASIS_MESSAGE_TYPE_DEFN(confbridge_talking_type);
00370 
00371 void manager_confbridge_shutdown(void) {
00372    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_start_type);
00373    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_end_type);
00374    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_join_type);
00375    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_leave_type);
00376    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_start_record_type);
00377    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_stop_record_type);
00378    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_mute_type);
00379    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_unmute_type);
00380    STASIS_MESSAGE_TYPE_CLEANUP(confbridge_talking_type);
00381 
00382    if (bridge_state_router) {
00383       stasis_message_router_unsubscribe(bridge_state_router);
00384       bridge_state_router = NULL;
00385    }
00386 
00387    if (channel_state_router) {
00388       stasis_message_router_unsubscribe(channel_state_router);
00389       channel_state_router = NULL;
00390    }
00391 }
00392 
00393 int manager_confbridge_init(void)
00394 {
00395    STASIS_MESSAGE_TYPE_INIT(confbridge_start_type);
00396    STASIS_MESSAGE_TYPE_INIT(confbridge_end_type);
00397    STASIS_MESSAGE_TYPE_INIT(confbridge_join_type);
00398    STASIS_MESSAGE_TYPE_INIT(confbridge_leave_type);
00399    STASIS_MESSAGE_TYPE_INIT(confbridge_start_record_type);
00400    STASIS_MESSAGE_TYPE_INIT(confbridge_stop_record_type);
00401    STASIS_MESSAGE_TYPE_INIT(confbridge_mute_type);
00402    STASIS_MESSAGE_TYPE_INIT(confbridge_unmute_type);
00403    STASIS_MESSAGE_TYPE_INIT(confbridge_talking_type);
00404 
00405    bridge_state_router = stasis_message_router_create(
00406       ast_bridge_topic_all_cached());
00407 
00408    if (!bridge_state_router) {
00409       return -1;
00410    }
00411 
00412    if (stasis_message_router_add(bridge_state_router,
00413          confbridge_start_type(),
00414          confbridge_start_cb,
00415          NULL)) {
00416       manager_confbridge_shutdown();
00417       return -1;
00418    }
00419    if (stasis_message_router_add(bridge_state_router,
00420          confbridge_end_type(),
00421          confbridge_end_cb,
00422          NULL)) {
00423       manager_confbridge_shutdown();
00424       return -1;
00425    }
00426    if (stasis_message_router_add(bridge_state_router,
00427          confbridge_join_type(),
00428          confbridge_join_cb,
00429          NULL)) {
00430       manager_confbridge_shutdown();
00431       return -1;
00432    }
00433    if (stasis_message_router_add(bridge_state_router,
00434          confbridge_leave_type(),
00435          confbridge_leave_cb,
00436          NULL)) {
00437       manager_confbridge_shutdown();
00438       return -1;
00439    }
00440    if (stasis_message_router_add(bridge_state_router,
00441          confbridge_start_record_type(),
00442          confbridge_start_record_cb,
00443          NULL)) {
00444       manager_confbridge_shutdown();
00445       return -1;
00446    }
00447    if (stasis_message_router_add(bridge_state_router,
00448          confbridge_stop_record_type(),
00449          confbridge_stop_record_cb,
00450          NULL)) {
00451       manager_confbridge_shutdown();
00452       return -1;
00453    }
00454    if (stasis_message_router_add(bridge_state_router,
00455          confbridge_mute_type(),
00456          confbridge_mute_cb,
00457          NULL)) {
00458       manager_confbridge_shutdown();
00459       return -1;
00460    }
00461    if (stasis_message_router_add(bridge_state_router,
00462          confbridge_unmute_type(),
00463          confbridge_unmute_cb,
00464          NULL)) {
00465       manager_confbridge_shutdown();
00466       return -1;
00467    }
00468    if (stasis_message_router_add(bridge_state_router,
00469          confbridge_talking_type(),
00470          confbridge_talking_cb,
00471          NULL)) {
00472       manager_confbridge_shutdown();
00473       return -1;
00474    }
00475 
00476    channel_state_router = stasis_message_router_create(
00477       ast_channel_topic_all_cached());
00478 
00479    if (!channel_state_router) {
00480       manager_confbridge_shutdown();
00481       return -1;
00482    }
00483 
00484    if (stasis_message_router_add(channel_state_router,
00485          confbridge_start_type(),
00486          confbridge_start_cb,
00487          NULL)) {
00488       manager_confbridge_shutdown();
00489       return -1;
00490    }
00491    if (stasis_message_router_add(channel_state_router,
00492          confbridge_end_type(),
00493          confbridge_end_cb,
00494          NULL)) {
00495       manager_confbridge_shutdown();
00496       return -1;
00497    }
00498    if (stasis_message_router_add(channel_state_router,
00499          confbridge_join_type(),
00500          confbridge_join_cb,
00501          NULL)) {
00502       manager_confbridge_shutdown();
00503       return -1;
00504    }
00505    if (stasis_message_router_add(channel_state_router,
00506          confbridge_leave_type(),
00507          confbridge_leave_cb,
00508          NULL)) {
00509       manager_confbridge_shutdown();
00510       return -1;
00511    }
00512    if (stasis_message_router_add(channel_state_router,
00513          confbridge_start_record_type(),
00514          confbridge_start_record_cb,
00515          NULL)) {
00516       manager_confbridge_shutdown();
00517       return -1;
00518    }
00519    if (stasis_message_router_add(channel_state_router,
00520          confbridge_stop_record_type(),
00521          confbridge_stop_record_cb,
00522          NULL)) {
00523       manager_confbridge_shutdown();
00524       return -1;
00525    }
00526    if (stasis_message_router_add(channel_state_router,
00527          confbridge_mute_type(),
00528          confbridge_mute_cb,
00529          NULL)) {
00530       manager_confbridge_shutdown();
00531       return -1;
00532    }
00533    if (stasis_message_router_add(channel_state_router,
00534          confbridge_unmute_type(),
00535          confbridge_unmute_cb,
00536          NULL)) {
00537       manager_confbridge_shutdown();
00538       return -1;
00539    }
00540    if (stasis_message_router_add(channel_state_router,
00541          confbridge_talking_type(),
00542          confbridge_talking_cb,
00543          NULL)) {
00544       manager_confbridge_shutdown();
00545       return -1;
00546    }
00547 
00548    return 0;
00549 }

Generated on Thu Apr 16 06:27:31 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6