func_jitterbuffer.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2011, Digium, Inc.
00005  *
00006  * David Vossel <dvossel@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 Put a jitterbuffer on the read side of a channel
00022  *
00023  * \author David Vossel <dvossel@digium.com>
00024  *
00025  * \ingroup functions
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 420640 $")
00035 
00036 #include "asterisk/module.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/framehook.h"
00039 #include "asterisk/frame.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/abstract_jb.h"
00042 #include "asterisk/timing.h"
00043 #include "asterisk/app.h"
00044 
00045 /*** DOCUMENTATION
00046    <function name="JITTERBUFFER" language="en_US">
00047       <synopsis>
00048          Add a Jitterbuffer to the Read side of the channel. This dejitters the audio stream before it reaches the Asterisk core. This is a write only function.
00049       </synopsis>
00050       <syntax>
00051          <parameter name="jitterbuffer type" required="true">
00052             <optionlist>
00053                <option name="fixed">
00054                   <para>Set a fixed jitterbuffer on the channel.</para>
00055                </option>
00056                <option name="adaptive">
00057                   <para>Set an adaptive jitterbuffer on the channel.</para>
00058                </option>
00059                <option name="disabled">
00060                   <para>Remove a previously set jitterbuffer from the channel.</para>
00061                </option>
00062             </optionlist>
00063          </parameter>
00064       </syntax>
00065       <description>
00066          <para>Jitterbuffers are constructed in two different ways.
00067          The first always take three arguments: <replaceable>max_size</replaceable>,
00068          <replaceable>resync_threshold</replaceable>, and <replaceable>target_extra</replaceable>.
00069          Alternatively, a single argument of <literal>default</literal> can be provided,
00070          which will construct the default jitterbuffer for the given
00071          <replaceable>jitterbuffer type</replaceable>.</para>
00072          <para>The arguments are:</para>
00073          <para><replaceable>max_size</replaceable>: Length in milliseconds of the buffer.
00074          Defaults to 200 ms.</para>
00075          <para><replaceable>resync_threshold</replaceable>: The length in milliseconds over
00076          which a timestamp difference will result in resyncing the jitterbuffer.
00077          Defaults to 1000ms.</para>
00078          <para>target_extra: This option only affects the adaptive jitterbuffer. It represents
00079          the amount time in milliseconds by which the new jitter buffer will pad its size.
00080          Defaults to 40ms.</para>
00081          <example title="Fixed with defaults" language="text">
00082          exten => 1,1,Set(JITTERBUFFER(fixed)=default)
00083          </example>
00084          <example title="Fixed with 200ms max size" language="text">
00085          exten => 1,1,Set(JITTERBUFFER(fixed)=200)
00086          </example>
00087          <example title="Fixed with 200ms max size, resync threshold 1500" language="text">
00088          exten => 1,1,Set(JITTERBUFFER(fixed)=200,1500)
00089          </example>
00090          <example title="Adaptive with defaults" language="text">
00091          exten => 1,1,Set(JITTERBUFFER(adaptive)=default)
00092          </example>
00093          <example title="Adaptive with 200ms max size, 60ms target extra" language="text">
00094          exten => 1,1,Set(JITTERBUFFER(adaptive)=200,,60)
00095          </example>
00096          <example title="Set a fixed jitterbuffer with defaults; then remove it" language="text">
00097          exten => 1,1,Set(JITTERBUFFER(fixed)=default)
00098          exten => 1,n,Set(JITTERBUFFER(disabled)=)
00099          </example>
00100          <note><para>If a channel specifies a jitterbuffer due to channel driver configuration and
00101          the JITTERBUFFER function has set a jitterbuffer for that channel, the jitterbuffer set by
00102          the JITTERBUFFER function will take priority and the jitterbuffer set by the channel
00103          configuration will not be applied.</para></note>
00104       </description>
00105    </function>
00106  ***/
00107 
00108 static int jb_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00109 {
00110    struct ast_jb_conf jb_conf;
00111 
00112    if (!chan) {
00113       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00114       return -1;
00115    }
00116 
00117    /* Initialize and set jb_conf */
00118    ast_jb_conf_default(&jb_conf);
00119 
00120    /* Now check user options to see if any of the defaults need to change. */
00121    if (!ast_strlen_zero(data)) {
00122       if (strcasecmp(data, "fixed") &&
00123             strcasecmp(data, "adaptive") &&
00124             strcasecmp(data, "disabled")) {
00125          ast_log(LOG_WARNING, "Unknown Jitterbuffer type %s. Failed to create jitterbuffer.\n", data);
00126          return -1;
00127       }
00128       ast_copy_string(jb_conf.impl, data, sizeof(jb_conf.impl));
00129    }
00130 
00131    if (!ast_strlen_zero(value) && strcasecmp(value, "default")) {
00132       char *parse = ast_strdupa(value);
00133       int res = 0;
00134       AST_DECLARE_APP_ARGS(args,
00135          AST_APP_ARG(max_size);
00136          AST_APP_ARG(resync_threshold);
00137          AST_APP_ARG(target_extra);
00138       );
00139 
00140       AST_STANDARD_APP_ARGS(args, parse);
00141       if (!ast_strlen_zero(args.max_size)) {
00142          res |= ast_jb_read_conf(&jb_conf,
00143             "jbmaxsize",
00144             args.max_size);
00145       }
00146       if (!ast_strlen_zero(args.resync_threshold)) {
00147          res |= ast_jb_read_conf(&jb_conf,
00148             "jbresyncthreshold",
00149             args.resync_threshold);
00150       }
00151       if (!ast_strlen_zero(args.target_extra)) {
00152          res |= ast_jb_read_conf(&jb_conf,
00153             "jbtargetextra",
00154             args.target_extra);
00155       }
00156       if (res) {
00157          ast_log(LOG_WARNING, "Invalid jitterbuffer parameters %s\n", value);
00158       }
00159    }
00160 
00161    ast_jb_create_framehook(chan, &jb_conf, 0);
00162 
00163    return 0;
00164 }
00165 
00166 
00167 static struct ast_custom_function jb_function = {
00168    .name = "JITTERBUFFER",
00169    .write = jb_helper,
00170 };
00171 
00172 static int unload_module(void)
00173 {
00174    return ast_custom_function_unregister(&jb_function);
00175 }
00176 
00177 static int load_module(void)
00178 {
00179    int res = ast_custom_function_register(&jb_function);
00180    return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00181 }
00182 
00183 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Jitter buffer for read side of channel.");
00184 

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