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 <stdio.h> 00034 #include <sys/time.h> 00035 00036 #if defined(__cplusplus) || defined(c_plusplus) 00037 extern "C" { 00038 #endif 00039 00040 struct ast_channel; 00041 struct ast_frame; 00042 00043 /* Configuration flags */ 00044 enum { 00045 AST_JB_ENABLED = (1 << 0), 00046 AST_JB_FORCED = (1 << 1), 00047 AST_JB_LOG = (1 << 2) 00048 }; 00049 00050 #define AST_JB_IMPL_NAME_SIZE 12 00051 00052 /*! 00053 * \brief General jitterbuffer configuration. 00054 */ 00055 struct ast_jb_conf 00056 { 00057 /*! \brief Combination of the AST_JB_ENABLED, AST_JB_FORCED and AST_JB_LOG flags. */ 00058 unsigned int flags; 00059 /*! \brief Max size of the jitterbuffer implementation. */ 00060 long max_size; 00061 /*! \brief Resynchronization threshold of the jitterbuffer implementation. */ 00062 long resync_threshold; 00063 /*! \brief Name of the jitterbuffer implementation to be used. */ 00064 char impl[AST_JB_IMPL_NAME_SIZE]; 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_IMPL "impl" 00075 #define AST_JB_CONF_LOG "log" 00076 00077 00078 struct ast_jb_impl; 00079 00080 00081 /*! 00082 * \brief General jitterbuffer state. 00083 */ 00084 struct ast_jb 00085 { 00086 /*! \brief Jitterbuffer configuration. */ 00087 struct ast_jb_conf conf; 00088 /*! \brief Jitterbuffer implementation to be used. */ 00089 struct ast_jb_impl *impl; 00090 /*! \brief Jitterbuffer object, passed to the implementation. */ 00091 void *jbobj; 00092 /*! \brief The time the jitterbuffer was created. */ 00093 struct timeval timebase; 00094 /*! \brief The time the next frame should be played. */ 00095 long next; 00096 /*! \brief Voice format of the last frame in. */ 00097 int last_format; 00098 /*! \brief File for frame timestamp tracing. */ 00099 FILE *logfile; 00100 /*! \brief Jitterbuffer internal state flags. */ 00101 unsigned int flags; 00102 }; 00103 00104 00105 /*! 00106 * \brief Checks the need of a jb use in a generic bridge. 00107 * \param c0 first bridged channel. 00108 * \param c1 second bridged channel. 00109 * 00110 * Called from ast_generic_bridge() when two channels are entering in a bridge. 00111 * The function checks the need of a jitterbuffer, depending on both channel's 00112 * configuration and technology properties. As a result, this function sets 00113 * appropriate internal jb flags to the channels, determining further behaviour 00114 * of the bridged jitterbuffers. 00115 * 00116 * \return zero if there are no jitter buffers in use, non-zero if there are 00117 */ 00118 int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1); 00119 00120 00121 /*! 00122 * \brief Calculates the time, left to the closest delivery moment in a bridge. 00123 * \param c0 first bridged channel. 00124 * \param c1 second bridged channel. 00125 * \param time_left bridge time limit, or -1 if not set. 00126 * 00127 * Called from ast_generic_bridge() to determine the maximum time to wait for 00128 * activity in ast_waitfor_n() call. If neihter of the channels is using jb, 00129 * this function returns the time limit passed. 00130 * 00131 * \return maximum time to wait. 00132 */ 00133 int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left); 00134 00135 00136 /*! 00137 * \brief Puts a frame into a channel jitterbuffer. 00138 * \param chan channel. 00139 * \param f frame. 00140 * 00141 * Called from ast_generic_bridge() to put a frame into a channel's jitterbuffer. 00142 * The function will successfuly enqueue a frame if and only if: 00143 * 1. the channel is using a jitterbuffer (as determined by ast_jb_do_usecheck()), 00144 * 2. the frame's type is AST_FRAME_VOICE, 00145 * 3. the frame has timing info set and has length >= 2 ms, 00146 * 4. there is no some internal error happened (like failed memory allocation). 00147 * Frames, successfuly queued, should be delivered by the channel's jitterbuffer, 00148 * when their delivery time has came. 00149 * Frames, not successfuly queued, should be delivered immediately. 00150 * Dropped by the jb implementation frames are considered successfuly enqueued as 00151 * far as they should not be delivered at all. 00152 * 00153 * \return zero if the frame was queued, -1 if not. 00154 */ 00155 int ast_jb_put(struct ast_channel *chan, struct ast_frame *f); 00156 00157 00158 /*! 00159 * \brief Deliver the queued frames that should be delivered now for both channels. 00160 * \param c0 first bridged channel. 00161 * \param c1 second bridged channel. 00162 * 00163 * Called from ast_generic_bridge() to deliver any frames, that should be delivered 00164 * for the moment of invocation. Does nothing if neihter of the channels is using jb 00165 * or has any frames currently queued in. The function delivers frames usig ast_write() 00166 * each of the channels. 00167 */ 00168 void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1); 00169 00170 00171 /*! 00172 * \brief Destroys jitterbuffer on a channel. 00173 * \param chan channel. 00174 * 00175 * Called from ast_channel_free() when a channel is destroyed. 00176 */ 00177 void ast_jb_destroy(struct ast_channel *chan); 00178 00179 00180 /*! 00181 * \brief Sets jitterbuffer configuration property. 00182 * \param conf configuration to store the property in. 00183 * \param varname property name. 00184 * \param value property value. 00185 * 00186 * Called from a channel driver to build a jitterbuffer configuration tipically when 00187 * reading a configuration file. It is not neccessary for a channel driver to know 00188 * each of the jb configuration property names. The jitterbuffer itself knows them. 00189 * The channel driver can pass each config var it reads through this function. It will 00190 * return 0 if the variable was consumed from the jb conf. 00191 * 00192 * \return zero if the property was set to the configuration, -1 if not. 00193 */ 00194 int ast_jb_read_conf(struct ast_jb_conf *conf, char *varname, char *value); 00195 00196 00197 /*! 00198 * \brief Configures a jitterbuffer on a channel. 00199 * \param chan channel to configure. 00200 * \param conf configuration to apply. 00201 * 00202 * Called from a channel driver when a channel is created and its jitterbuffer needs 00203 * to be configured. 00204 */ 00205 void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf); 00206 00207 00208 /*! 00209 * \brief Copies a channel's jitterbuffer configuration. 00210 * \param chan channel. 00211 * \param conf destination. 00212 */ 00213 void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf); 00214 00215 /*! 00216 * \brief drops all frames from a jitterbuffer and resets it 00217 * \param c0 one channel of a bridge 00218 * \param c1 the other channel of the bridge 00219 */ 00220 void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1); 00221 00222 #if defined(__cplusplus) || defined(c_plusplus) 00223 } 00224 #endif 00225 00226 #endif /* _ABSTRACT_JB_H_ */