00001 /* 00002 * abstract_jb: common implementation-independent jitterbuffer stuff 00003 * 00004 * Copyright (C) 2005, Attractel OOD 00005 * 00006 * Contributors: 00007 * Slav Klenov <slav@securax.org> 00008 * 00009 * See http://www.asterisk.org for more information about 00010 * the Asterisk project. Please do not directly contact 00011 * any of the maintainers of this project for assistance; 00012 * the project provides a web site, mailing lists and IRC 00013 * channels for your use. 00014 * 00015 * This program is free software, distributed under the terms of 00016 * the GNU General Public License Version 2. See the LICENSE file 00017 * at the top of the source tree. 00018 * 00019 * A license has been granted to Digium (via disclaimer) for the use of 00020 * this code. 00021 */ 00022 00023 /*! \file 00024 * 00025 * \brief Common implementation-independent jitterbuffer stuff. 00026 * 00027 * \author Slav Klenov <slav@securax.org> 00028 */ 00029 00030 #ifndef _ABSTRACT_JB_H_ 00031 #define _ABSTRACT_JB_H_ 00032 00033 #include <sys/time.h> 00034 00035 #if defined(__cplusplus) || defined(c_plusplus) 00036 extern "C" { 00037 #endif 00038 00039 struct ast_frame; 00040 00041 /* Configuration flags */ 00042 enum { 00043 AST_JB_ENABLED = (1 << 0), 00044 AST_JB_FORCED = (1 << 1), 00045 AST_JB_LOG = (1 << 2) 00046 }; 00047 00048 #define AST_JB_IMPL_NAME_SIZE 12 00049 00050 /*! 00051 * \brief General jitterbuffer configuration. 00052 */ 00053 struct ast_jb_conf 00054 { 00055 /*! \brief Combination of the AST_JB_ENABLED, AST_JB_FORCED and AST_JB_LOG flags. */ 00056 unsigned int flags; 00057 /*! \brief Max size of the jitterbuffer implementation. */ 00058 long max_size; 00059 /*! \brief Resynchronization threshold of the jitterbuffer implementation. */ 00060 long resync_threshold; 00061 /*! \brief Name of the jitterbuffer implementation to be used. */ 00062 char impl[AST_JB_IMPL_NAME_SIZE]; 00063 /*! \brief amount of additional jitterbuffer adjustment */ 00064 long target_extra; 00065 }; 00066 00067 00068 /* Jitterbuffer configuration property names */ 00069 #define AST_JB_CONF_PREFIX "jb" 00070 #define AST_JB_CONF_ENABLE "enable" 00071 #define AST_JB_CONF_FORCE "force" 00072 #define AST_JB_CONF_MAX_SIZE "maxsize" 00073 #define AST_JB_CONF_RESYNCH_THRESHOLD "resyncthreshold" 00074 #define AST_JB_CONF_TARGET_EXTRA "targetextra" 00075 #define AST_JB_CONF_IMPL "impl" 00076 #define AST_JB_CONF_LOG "log" 00077 00078 00079 struct ast_jb_impl; 00080 00081 00082 /*! 00083 * \brief General jitterbuffer state. 00084 */ 00085 struct ast_jb 00086 { 00087 /*! \brief Jitterbuffer configuration. */ 00088 struct ast_jb_conf conf; 00089 /*! \brief Jitterbuffer implementation to be used. */ 00090 struct ast_jb_impl *impl; 00091 /*! \brief Jitterbuffer object, passed to the implementation. */ 00092 void *jbobj; 00093 /*! \brief The time the jitterbuffer was created. */ 00094 struct timeval timebase; 00095 /*! \brief The time the next frame should be played. */ 00096 long next; 00097 /*! \brief Voice format of the last frame in. */ 00098 int last_format; 00099 /*! \brief File for frame timestamp tracing. */ 00100 FILE *logfile; 00101 /*! \brief Jitterbuffer internal state flags. */ 00102 unsigned int flags; 00103 }; 00104 00105 00106 /*! 00107 * \brief Checks the need of a jb use in a generic bridge. 00108 * \param c0 first bridged channel. 00109 * \param c1 second bridged channel. 00110 * 00111 * Called from ast_generic_bridge() when two channels are entering in a bridge. 00112 * The function checks the need of a jitterbuffer, depending on both channel's 00113 * configuration and technology properties. As a result, this function sets 00114 * appropriate internal jb flags to the channels, determining further behaviour 00115 * of the bridged jitterbuffers. 00116 * 00117 * \retval zero if there are no jitter buffers in use 00118 * \retval non-zero if there are 00119 */ 00120 int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1); 00121 00122 00123 /*! 00124 * \brief Calculates the time, left to the closest delivery moment in a bridge. 00125 * \param c0 first bridged channel. 00126 * \param c1 second bridged channel. 00127 * \param time_left bridge time limit, or -1 if not set. 00128 * 00129 * Called from ast_generic_bridge() to determine the maximum time to wait for 00130 * activity in ast_waitfor_n() call. If neihter of the channels is using jb, 00131 * this function returns the time limit passed. 00132 * 00133 * \return maximum time to wait. 00134 */ 00135 int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left); 00136 00137 00138 /*! 00139 * \brief Puts a frame into a channel jitterbuffer. 00140 * \param chan channel. 00141 * \param f frame. 00142 * 00143 * Called from ast_generic_bridge() to put a frame into a channel's jitterbuffer. 00144 * The function will successfuly enqueue a frame if and only if: 00145 * 1. the channel is using a jitterbuffer (as determined by ast_jb_do_usecheck()), 00146 * 2. the frame's type is AST_FRAME_VOICE, 00147 * 3. the frame has timing info set and has length >= 2 ms, 00148 * 4. there is no some internal error happened (like failed memory allocation). 00149 * Frames, successfuly queued, should be delivered by the channel's jitterbuffer, 00150 * when their delivery time has came. 00151 * Frames, not successfuly queued, should be delivered immediately. 00152 * Dropped by the jb implementation frames are considered successfuly enqueued as 00153 * far as they should not be delivered at all. 00154 * 00155 * \retval 0 if the frame was queued 00156 * \retval -1 if not 00157 */ 00158 int ast_jb_put(struct ast_channel *chan, struct ast_frame *f); 00159 00160 00161 /*! 00162 * \brief Deliver the queued frames that should be delivered now for both channels. 00163 * \param c0 first bridged channel. 00164 * \param c1 second bridged channel. 00165 * 00166 * Called from ast_generic_bridge() to deliver any frames, that should be delivered 00167 * for the moment of invocation. Does nothing if neihter of the channels is using jb 00168 * or has any frames currently queued in. The function delivers frames usig ast_write() 00169 * each of the channels. 00170 */ 00171 void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1); 00172 00173 00174 /*! 00175 * \brief Destroys jitterbuffer on a channel. 00176 * \param chan channel. 00177 * 00178 * Called from ast_channel_free() when a channel is destroyed. 00179 */ 00180 void ast_jb_destroy(struct ast_channel *chan); 00181 00182 00183 /*! 00184 * \brief Sets jitterbuffer configuration property. 00185 * \param conf configuration to store the property in. 00186 * \param varname property name. 00187 * \param value property value. 00188 * 00189 * Called from a channel driver to build a jitterbuffer configuration typically when 00190 * reading a configuration file. It is not necessary for a channel driver to know 00191 * each of the jb configuration property names. The jitterbuffer itself knows them. 00192 * The channel driver can pass each config var it reads through this function. It will 00193 * return 0 if the variable was consumed from the jb conf. 00194 * 00195 * \return zero if the property was set to the configuration, -1 if not. 00196 */ 00197 int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value); 00198 00199 00200 /*! 00201 * \brief Configures a jitterbuffer on a channel. 00202 * \param chan channel to configure. 00203 * \param conf configuration to apply. 00204 * 00205 * Called from a channel driver when a channel is created and its jitterbuffer needs 00206 * to be configured. 00207 */ 00208 void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf); 00209 00210 00211 /*! 00212 * \brief Copies a channel's jitterbuffer configuration. 00213 * \param chan channel. 00214 * \param conf destination. 00215 */ 00216 void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf); 00217 00218 /*! 00219 * \brief drops all frames from a jitterbuffer and resets it 00220 * \param c0 one channel of a bridge 00221 * \param c1 the other channel of the bridge 00222 */ 00223 void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1); 00224 00225 #if defined(__cplusplus) || defined(c_plusplus) 00226 } 00227 #endif 00228 00229 #endif /* _ABSTRACT_JB_H_ */