use tzafrir's patch to fix this problem properly... i made the previous set of change...
[asterisk-bristuff.git] / include / asterisk / abstract_jb.h
blobfc2bf5c90a3b4e23ef0e701d4b588bb6529326a2
1 /*
2 * abstract_jb: common implementation-independent jitterbuffer stuff
4 * Copyright (C) 2005, Attractel OOD
6 * Contributors:
7 * Slav Klenov <slav@securax.org>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
19 * A license has been granted to Digium (via disclaimer) for the use of
20 * this code.
23 /*! \file
25 * \brief Common implementation-independent jitterbuffer stuff.
27 * \author Slav Klenov <slav@securax.org>
30 #ifndef _ABSTRACT_JB_H_
31 #define _ABSTRACT_JB_H_
33 #include <stdio.h>
34 #include <sys/time.h>
36 #if defined(__cplusplus) || defined(c_plusplus)
37 extern "C" {
38 #endif
40 struct ast_channel;
41 struct ast_frame;
43 /* Configuration flags */
44 enum {
45 AST_JB_ENABLED = (1 << 0),
46 AST_JB_FORCED = (1 << 1),
47 AST_JB_LOG = (1 << 2)
50 #define AST_JB_IMPL_NAME_SIZE 12
52 /*!
53 * \brief General jitterbuffer configuration.
55 struct ast_jb_conf
57 /*! \brief Combination of the AST_JB_ENABLED, AST_JB_FORCED and AST_JB_LOG flags. */
58 unsigned int flags;
59 /*! \brief Max size of the jitterbuffer implementation. */
60 long max_size;
61 /*! \brief Resynchronization threshold of the jitterbuffer implementation. */
62 long resync_threshold;
63 /*! \brief Name of the jitterbuffer implementation to be used. */
64 char impl[AST_JB_IMPL_NAME_SIZE];
68 /* Jitterbuffer configuration property names */
69 #define AST_JB_CONF_PREFIX "jb"
70 #define AST_JB_CONF_ENABLE "enable"
71 #define AST_JB_CONF_FORCE "force"
72 #define AST_JB_CONF_MAX_SIZE "maxsize"
73 #define AST_JB_CONF_RESYNCH_THRESHOLD "resyncthreshold"
74 #define AST_JB_CONF_IMPL "impl"
75 #define AST_JB_CONF_LOG "log"
78 struct ast_jb_impl;
81 /*!
82 * \brief General jitterbuffer state.
84 struct ast_jb
86 /*! \brief Jitterbuffer configuration. */
87 struct ast_jb_conf conf;
88 /*! \brief Jitterbuffer implementation to be used. */
89 struct ast_jb_impl *impl;
90 /*! \brief Jitterbuffer object, passed to the implementation. */
91 void *jbobj;
92 /*! \brief The time the jitterbuffer was created. */
93 struct timeval timebase;
94 /*! \brief The time the next frame should be played. */
95 long next;
96 /*! \brief Voice format of the last frame in. */
97 int last_format;
98 /*! \brief File for frame timestamp tracing. */
99 FILE *logfile;
100 /*! \brief Jitterbuffer internal state flags. */
101 unsigned int flags;
106 * \brief Checks the need of a jb use in a generic bridge.
107 * \param c0 first bridged channel.
108 * \param c1 second bridged channel.
110 * Called from ast_generic_bridge() when two channels are entering in a bridge.
111 * The function checks the need of a jitterbuffer, depending on both channel's
112 * configuration and technology properties. As a result, this function sets
113 * appropriate internal jb flags to the channels, determining further behaviour
114 * of the bridged jitterbuffers.
116 * \return zero if there are no jitter buffers in use, non-zero if there are
118 int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1);
122 * \brief Calculates the time, left to the closest delivery moment in a bridge.
123 * \param c0 first bridged channel.
124 * \param c1 second bridged channel.
125 * \param time_left bridge time limit, or -1 if not set.
127 * Called from ast_generic_bridge() to determine the maximum time to wait for
128 * activity in ast_waitfor_n() call. If neihter of the channels is using jb,
129 * this function returns the time limit passed.
131 * \return maximum time to wait.
133 int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left);
137 * \brief Puts a frame into a channel jitterbuffer.
138 * \param chan channel.
139 * \param f frame.
141 * Called from ast_generic_bridge() to put a frame into a channel's jitterbuffer.
142 * The function will successfuly enqueue a frame if and only if:
143 * 1. the channel is using a jitterbuffer (as determined by ast_jb_do_usecheck()),
144 * 2. the frame's type is AST_FRAME_VOICE,
145 * 3. the frame has timing info set and has length >= 2 ms,
146 * 4. there is no some internal error happened (like failed memory allocation).
147 * Frames, successfuly queued, should be delivered by the channel's jitterbuffer,
148 * when their delivery time has came.
149 * Frames, not successfuly queued, should be delivered immediately.
150 * Dropped by the jb implementation frames are considered successfuly enqueued as
151 * far as they should not be delivered at all.
153 * \return zero if the frame was queued, -1 if not.
155 int ast_jb_put(struct ast_channel *chan, struct ast_frame *f);
159 * \brief Deliver the queued frames that should be delivered now for both channels.
160 * \param c0 first bridged channel.
161 * \param c1 second bridged channel.
163 * Called from ast_generic_bridge() to deliver any frames, that should be delivered
164 * for the moment of invocation. Does nothing if neihter of the channels is using jb
165 * or has any frames currently queued in. The function delivers frames usig ast_write()
166 * each of the channels.
168 void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1);
172 * \brief Destroys jitterbuffer on a channel.
173 * \param chan channel.
175 * Called from ast_channel_free() when a channel is destroyed.
177 void ast_jb_destroy(struct ast_channel *chan);
181 * \brief Sets jitterbuffer configuration property.
182 * \param conf configuration to store the property in.
183 * \param varname property name.
184 * \param value property value.
186 * Called from a channel driver to build a jitterbuffer configuration tipically when
187 * reading a configuration file. It is not neccessary for a channel driver to know
188 * each of the jb configuration property names. The jitterbuffer itself knows them.
189 * The channel driver can pass each config var it reads through this function. It will
190 * return 0 if the variable was consumed from the jb conf.
192 * \return zero if the property was set to the configuration, -1 if not.
194 int ast_jb_read_conf(struct ast_jb_conf *conf, char *varname, char *value);
198 * \brief Configures a jitterbuffer on a channel.
199 * \param chan channel to configure.
200 * \param conf configuration to apply.
202 * Called from a channel driver when a channel is created and its jitterbuffer needs
203 * to be configured.
205 void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf);
209 * \brief Copies a channel's jitterbuffer configuration.
210 * \param chan channel.
211 * \param conf destination.
213 void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf);
216 #if defined(__cplusplus) || defined(c_plusplus)
218 #endif
220 #endif /* _ABSTRACT_JB_H_ */