bridge_builtin_interval_features.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 Built in bridging interval features
00022  *
00023  * \author Jonathan Rose <jrose@digium.com>
00024  *
00025  * \ingroup bridges
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 397577 $")
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <sys/types.h>
00040 
00041 #include "asterisk/module.h"
00042 #include "asterisk/channel.h"
00043 #include "asterisk/bridge.h"
00044 #include "asterisk/file.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/astobj2.h"
00047 #include "asterisk/test.h"
00048 #include "asterisk/say.h"
00049 #include "asterisk/stringfields.h"
00050 #include "asterisk/musiconhold.h"
00051 #include "asterisk/causes.h"
00052 
00053 static int bridge_features_duration_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
00054 {
00055    struct ast_bridge_features_limits *limits = hook_pvt;
00056 
00057    if (!ast_strlen_zero(limits->duration_sound)) {
00058       ast_stream_and_wait(bridge_channel->chan, limits->duration_sound, AST_DIGIT_NONE);
00059    }
00060 
00061    ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END,
00062       AST_CAUSE_NORMAL_CLEARING);
00063 
00064    ast_test_suite_event_notify("BRIDGE_TIMELIMIT", "Channel1: %s",
00065       ast_channel_name(bridge_channel->chan));
00066    return -1;
00067 }
00068 
00069 static void limits_interval_playback(struct ast_bridge_channel *bridge_channel, struct ast_bridge_features_limits *limits, const char *file)
00070 {
00071    if (!strcasecmp(file, "timeleft")) {
00072       unsigned int remaining = ast_tvdiff_ms(limits->quitting_time, ast_tvnow()) / 1000;
00073       unsigned int min;
00074       unsigned int sec;
00075 
00076       if (remaining <= 0) {
00077          return;
00078       }
00079 
00080       if ((remaining / 60) > 1) {
00081          min = remaining / 60;
00082          sec = remaining % 60;
00083       } else {
00084          min = 0;
00085          sec = remaining;
00086       }
00087 
00088       ast_stream_and_wait(bridge_channel->chan, "vm-youhave", AST_DIGIT_NONE);
00089       if (min) {
00090          ast_say_number(bridge_channel->chan, min, AST_DIGIT_NONE,
00091             ast_channel_language(bridge_channel->chan), NULL);
00092          ast_stream_and_wait(bridge_channel->chan, "queue-minutes", AST_DIGIT_NONE);
00093       }
00094       if (sec) {
00095          ast_say_number(bridge_channel->chan, sec, AST_DIGIT_NONE,
00096             ast_channel_language(bridge_channel->chan), NULL);
00097          ast_stream_and_wait(bridge_channel->chan, "queue-seconds", AST_DIGIT_NONE);
00098       }
00099    } else {
00100       ast_stream_and_wait(bridge_channel->chan, file, AST_DIGIT_NONE);
00101    }
00102 
00103    /*
00104     * It may be necessary to resume music on hold after we finish
00105     * playing the announcment.
00106     */
00107    if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
00108       const char *latest_musicclass;
00109 
00110       ast_channel_lock(bridge_channel->chan);
00111       latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
00112       ast_channel_unlock(bridge_channel->chan);
00113       ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
00114    }
00115 }
00116 
00117 static int bridge_features_connect_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
00118 {
00119    struct ast_bridge_features_limits *limits = hook_pvt;
00120 
00121    limits_interval_playback(bridge_channel, limits, limits->connect_sound);
00122    return -1;
00123 }
00124 
00125 static int bridge_features_warning_callback(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
00126 {
00127    struct ast_bridge_features_limits *limits = hook_pvt;
00128 
00129    limits_interval_playback(bridge_channel, limits, limits->warning_sound);
00130    return limits->frequency ?: -1;
00131 }
00132 
00133 static void bridge_features_limits_copy(struct ast_bridge_features_limits *dst, struct ast_bridge_features_limits *src)
00134 {
00135    ast_string_fields_copy(dst, src);
00136    dst->quitting_time = src->quitting_time;
00137    dst->duration = src->duration;
00138    dst->warning = src->warning;
00139    dst->frequency = src->frequency;
00140 }
00141 
00142 static void bridge_features_limits_dtor(void *vdoomed)
00143 {
00144    struct ast_bridge_features_limits *doomed = vdoomed;
00145 
00146    ast_bridge_features_limits_destroy(doomed);
00147    ast_module_unref(ast_module_info->self);
00148 }
00149 
00150 static int bridge_builtin_set_limits(struct ast_bridge_features *features,
00151    struct ast_bridge_features_limits *limits,
00152    enum ast_bridge_hook_remove_flags remove_flags)
00153 {
00154    RAII_VAR(struct ast_bridge_features_limits *, feature_limits, NULL, ao2_cleanup);
00155 
00156    if (!limits->duration) {
00157       return -1;
00158    }
00159 
00160    /* Create limits hook_pvt data. */
00161    ast_module_ref(ast_module_info->self);
00162    feature_limits = ao2_alloc_options(sizeof(*feature_limits),
00163       bridge_features_limits_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK);
00164    if (!feature_limits) {
00165       ast_module_unref(ast_module_info->self);
00166       return -1;
00167    }
00168    if (ast_bridge_features_limits_construct(feature_limits)) {
00169       return -1;
00170    }
00171    bridge_features_limits_copy(feature_limits, limits);
00172    feature_limits->quitting_time = ast_tvadd(ast_tvnow(),
00173       ast_samp2tv(feature_limits->duration, 1000));
00174 
00175    /* Install limit hooks. */
00176    ao2_ref(feature_limits, +1);
00177    if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
00178       feature_limits->duration,
00179       bridge_features_duration_callback, feature_limits, __ao2_cleanup, remove_flags)) {
00180       ast_log(LOG_ERROR, "Failed to schedule the duration limiter to the bridge channel.\n");
00181       ao2_ref(feature_limits, -1);
00182       return -1;
00183    }
00184    if (!ast_strlen_zero(feature_limits->connect_sound)) {
00185       ao2_ref(feature_limits, +1);
00186       if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA, 1,
00187          bridge_features_connect_callback, feature_limits, __ao2_cleanup, remove_flags)) {
00188          ast_log(LOG_WARNING, "Failed to schedule connect sound to the bridge channel.\n");
00189          ao2_ref(feature_limits, -1);
00190       }
00191    }
00192    if (feature_limits->warning && feature_limits->warning < feature_limits->duration) {
00193       ao2_ref(feature_limits, +1);
00194       if (ast_bridge_interval_hook(features, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA,
00195          feature_limits->duration - feature_limits->warning,
00196          bridge_features_warning_callback, feature_limits, __ao2_cleanup, remove_flags)) {
00197          ast_log(LOG_WARNING, "Failed to schedule warning sound playback to the bridge channel.\n");
00198          ao2_ref(feature_limits, -1);
00199       }
00200    }
00201 
00202    return 0;
00203 }
00204 
00205 static int unload_module(void)
00206 {
00207    ast_bridge_interval_unregister(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS);
00208    return 0;
00209 }
00210 
00211 static int load_module(void)
00212 {
00213    return ast_bridge_interval_register(AST_BRIDGE_BUILTIN_INTERVAL_LIMITS,
00214       bridge_builtin_set_limits);
00215 }
00216 
00217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging interval features");

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