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