Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
func_jitterbuffer.c
Go to the documentation of this file.
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"
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
118static 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 */
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;
148 AST_APP_ARG(sync_video);
149 );
150
152 if (!ast_strlen_zero(args.max_size)) {
154 "jbmaxsize",
155 args.max_size);
156 }
157 if (!ast_strlen_zero(args.resync_threshold)) {
159 "jbresyncthreshold",
160 args.resync_threshold);
161 }
162 if (!ast_strlen_zero(args.target_extra)) {
164 "jbtargetextra",
165 args.target_extra);
166 }
167 if (!ast_strlen_zero(args.sync_video)) {
169 "jbsyncvideo",
170 args.sync_video);
171 }
172 if (res) {
173 ast_log(LOG_WARNING, "Invalid jitterbuffer parameters %s\n", value);
174 }
175 }
176
178
179 return 0;
180}
181
182
184 .name = "JITTERBUFFER",
185 .write = jb_helper,
186};
187
188static int unload_module(void)
189{
191}
192
193static int load_module(void)
194{
197}
198
199AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Jitter buffer for read side of channel.");
Common implementation-independent jitterbuffer stuff.
void ast_jb_create_framehook(struct ast_channel *chan, struct ast_jb_conf *jb_conf, int prefer_existing)
Applies a jitterbuffer framehook to a channel based on a provided jitterbuffer config.
Definition: abstract_jb.c:1267
void ast_jb_conf_default(struct ast_jb_conf *conf)
Sets the contents of an ast_jb_conf struct to the default jitterbuffer settings.
Definition: abstract_jb.c:890
int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value)
Sets jitterbuffer configuration property.
Definition: abstract_jb.c:545
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
FrameHook Architecture.
static struct ast_custom_function jb_function
static int jb_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int load_module(void)
static int unload_module(void)
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
Asterisk internal frame definitions.
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1559
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
const char * name
Definition: pbx.h:119
General jitterbuffer configuration.
Definition: abstract_jb.h:70
long target_extra
amount of additional jitterbuffer adjustment
Definition: abstract_jb.h:80
long max_size
Max size of the jitterbuffer implementation.
Definition: abstract_jb.h:74
long resync_threshold
Resynchronization threshold of the jitterbuffer implementation.
Definition: abstract_jb.h:76
int value
Definition: syslog.c:37
const char * args
Timing source management.