| 1 | /* |
| 2 | * Asterisk -- An open source telephony toolkit. |
| 3 | * |
| 4 | * Copyright (C) 2011, Digium, Inc. |
| 5 | * |
| 6 | * David Vossel <[email protected]> |
| 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 Put a jitterbuffer on the read side of a channel |
| 22 | * |
| 23 | * \author David Vossel <[email protected]> |
| 24 | * |
| 25 | * \ingroup functions |
| 26 | */ |
| 27 | |
| 28 | /*** MODULEINFO |
| 29 | <support_level>core</support_level> |
| 30 | ***/ |
| 31 | |
| 32 | #include "asterisk.h" |
| 33 | |
| 34 | #include "asterisk/module.h" |
| 35 | #include "asterisk/channel.h" |
| 36 | #include "asterisk/framehook.h" |
| 37 | #include "asterisk/frame.h" |
| 38 | #include "asterisk/pbx.h" |
| 39 | #include "asterisk/abstract_jb.h" |
| 40 | #include "asterisk/timing.h" |
| 41 | #include "asterisk/app.h" |
| 42 | |
| 43 | /*** DOCUMENTATION |
| 44 | <function name="JITTERBUFFER" language="en_US"> |
| 45 | <since> |
| 46 | <version>10.0.0</version> |
| 47 | </since> |
| 48 | <synopsis> |
| 49 | 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. |
| 50 | </synopsis> |
| 51 | <syntax> |
| 52 | <parameter name="jitterbuffer type" required="true"> |
| 53 | <optionlist> |
| 54 | <option name="fixed"> |
| 55 | <para>Set a fixed jitterbuffer on the channel.</para> |
| 56 | </option> |
| 57 | <option name="adaptive"> |
| 58 | <para>Set an adaptive jitterbuffer on the channel.</para> |
| 59 | </option> |
| 60 | <option name="disabled"> |
| 61 | <para>Remove a previously set jitterbuffer from the channel.</para> |
| 62 | </option> |
| 63 | </optionlist> |
| 64 | </parameter> |
| 65 | </syntax> |
| 66 | <description> |
| 67 | <para>Jitterbuffers are constructed in two different ways. |
| 68 | The first always take four arguments: <replaceable>max_size</replaceable>, |
| 69 | <replaceable>resync_threshold</replaceable>, <replaceable>target_extra</replaceable>, |
| 70 | and <replaceable>sync_video</replaceable>. |
| 71 | Alternatively, a single argument of <literal>default</literal> can be provided, |
| 72 | which will construct the default jitterbuffer for the given |
| 73 | <replaceable>jitterbuffer type</replaceable>.</para> |
| 74 | <para>The arguments are:</para> |
| 75 | <para><replaceable>max_size</replaceable>: Length in milliseconds of the buffer. |
| 76 | Defaults to 200 ms.</para> |
| 77 | <para><replaceable>resync_threshold</replaceable>: The length in milliseconds over |
| 78 | which a timestamp difference will result in resyncing the jitterbuffer. |
| 79 | Defaults to 1000ms.</para> |
| 80 | <para>target_extra: This option only affects the adaptive jitterbuffer. It represents |
| 81 | the amount time in milliseconds by which the new jitter buffer will pad its size. |
| 82 | Defaults to 40ms.</para> |
| 83 | <para>sync_video: This option enables video synchronization with the audio stream. It can be |
| 84 | turned on and off. Defaults to off.</para> |
| 85 | <example title="Fixed with defaults" language="text"> |
| 86 | exten => 1,1,Set(JITTERBUFFER(fixed)=default) |
| 87 | </example> |
| 88 | <example title="Fixed with 200ms max size" language="text"> |
| 89 | exten => 1,1,Set(JITTERBUFFER(fixed)=200) |
| 90 | </example> |
| 91 | <example title="Fixed with 200ms max size and video sync support" language="text"> |
| 92 | exten => 1,1,Set(JITTERBUFFER(fixed)=200,,,yes) |
| 93 | </example> |
| 94 | <example title="Fixed with 200ms max size, resync threshold 1500" language="text"> |
| 95 | exten => 1,1,Set(JITTERBUFFER(fixed)=200,1500) |
| 96 | </example> |
| 97 | <example title="Adaptive with defaults" language="text"> |
| 98 | exten => 1,1,Set(JITTERBUFFER(adaptive)=default) |
| 99 | </example> |
| 100 | <example title="Adaptive with 200ms max size, 60ms target extra" language="text"> |
| 101 | exten => 1,1,Set(JITTERBUFFER(adaptive)=200,,60) |
| 102 | </example> |
| 103 | <example title="Adaptive with 200ms max size and video sync support" language="text"> |
| 104 | exten => 1,1,Set(JITTERBUFFER(adaptive)=200,,,yes) |
| 105 | </example> |
| 106 | <example title="Set a fixed jitterbuffer with defaults; then remove it" language="text"> |
| 107 | exten => 1,1,Set(JITTERBUFFER(fixed)=default) |
| 108 | exten => 1,n,Set(JITTERBUFFER(disabled)=) |
| 109 | </example> |
| 110 | <note><para>If a channel specifies a jitterbuffer due to channel driver configuration and |
| 111 | the JITTERBUFFER function has set a jitterbuffer for that channel, the jitterbuffer set by |
| 112 | the JITTERBUFFER function will take priority and the jitterbuffer set by the channel |
| 113 | configuration will not be applied.</para></note> |
| 114 | </description> |
| 115 | </function> |
| 116 | ***/ |
| 117 | |
| 118 | static int jb_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 119 | { |
| 120 | struct ast_jb_conf jb_conf; |
| 121 | |
| 122 | if (!chan) { |
| 123 | ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | /* Initialize and set jb_conf */ |
| 128 | ast_jb_conf_default(&jb_conf); |
| 129 | |
| 130 | /* Now check user options to see if any of the defaults need to change. */ |
| 131 | if (!ast_strlen_zero(data)) { |
| 132 | if (strcasecmp(data, "fixed") && |
| 133 | strcasecmp(data, "adaptive") && |
| 134 | strcasecmp(data, "disabled")) { |
| 135 | ast_log(LOG_WARNING, "Unknown Jitterbuffer type %s. Failed to create jitterbuffer.\n", data); |
| 136 | return -1; |
| 137 | } |
| 138 | ast_copy_string(jb_conf.impl, data, sizeof(jb_conf.impl)); |
| 139 | } |
| 140 | |
| 141 | if (!ast_strlen_zero(value) && strcasecmp(value, "default")) { |
| 142 | char *parse = ast_strdupa(value); |
| 143 | int res = 0; |
| 144 | AST_DECLARE_APP_ARGS(args, |
| 145 | AST_APP_ARG(max_size); |
| 146 | AST_APP_ARG(resync_threshold); |
| 147 | AST_APP_ARG(target_extra); |
| 148 | AST_APP_ARG(sync_video); |
| 149 | ); |
| 150 | |
| 151 | AST_STANDARD_APP_ARGS(args, parse); |
| 152 | if (!ast_strlen_zero(args.max_size)) { |
| 153 | res |= ast_jb_read_conf(&jb_conf, |
| 154 | "jbmaxsize", |
| 155 | args.max_size); |
| 156 | } |
| 157 | if (!ast_strlen_zero(args.resync_threshold)) { |
| 158 | res |= ast_jb_read_conf(&jb_conf, |
| 159 | "jbresyncthreshold", |
| 160 | args.resync_threshold); |
| 161 | } |
| 162 | if (!ast_strlen_zero(args.target_extra)) { |
| 163 | res |= ast_jb_read_conf(&jb_conf, |
| 164 | "jbtargetextra", |
| 165 | args.target_extra); |
| 166 | } |
| 167 | if (!ast_strlen_zero(args.sync_video)) { |
| 168 | res |= ast_jb_read_conf(&jb_conf, |
| 169 | "jbsyncvideo", |
| 170 | args.sync_video); |
| 171 | } |
| 172 | if (res) { |
| 173 | ast_log(LOG_WARNING, "Invalid jitterbuffer parameters %s\n", value); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | ast_jb_create_framehook(chan, &jb_conf, 0); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static struct ast_custom_function jb_function = { |
| 184 | .name = "JITTERBUFFER", |
| 185 | .write = jb_helper, |
| 186 | }; |
| 187 | |
| 188 | static int unload_module(void) |
| 189 | { |
| 190 | return ast_custom_function_unregister(&jb_function); |
| 191 | } |
| 192 | |
| 193 | static int load_module(void) |
| 194 | { |
| 195 | int res = ast_custom_function_register(&jb_function); |
| 196 | return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 197 | } |
| 198 | |
| 199 | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Jitter buffer for read side of channel."); |