abstract_jb.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * abstract_jb: common implementation-independent jitterbuffer stuff
  3. *
  4. * Copyright (C) 2005, Attractel OOD
  5. *
  6. * Contributors:
  7. * Slav Klenov <slav@securax.org>
  8. *
  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.
  14. *
  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.
  18. *
  19. * A license has been granted to Digium (via disclaimer) for the use of
  20. * this code.
  21. */
  22. /*! \file
  23. *
  24. * \brief Common implementation-independent jitterbuffer stuff.
  25. *
  26. * \author Slav Klenov <slav@securax.org>
  27. */
  28. #ifndef _ABSTRACT_JB_H_
  29. #define _ABSTRACT_JB_H_
  30. #include <sys/time.h>
  31. #include "asterisk/format.h"
  32. #if defined(__cplusplus) || defined(c_plusplus)
  33. extern "C" {
  34. #endif
  35. struct ast_frame;
  36. /* Configuration flags */
  37. enum {
  38. AST_JB_ENABLED = (1 << 0),
  39. AST_JB_FORCED = (1 << 1),
  40. AST_JB_LOG = (1 << 2)
  41. };
  42. enum ast_jb_type {
  43. AST_JB_FIXED,
  44. AST_JB_ADAPTIVE,
  45. };
  46. /*! Abstract return codes */
  47. enum {
  48. AST_JB_IMPL_OK,
  49. AST_JB_IMPL_DROP,
  50. AST_JB_IMPL_INTERP,
  51. AST_JB_IMPL_NOFRAME
  52. };
  53. #define AST_JB_IMPL_NAME_SIZE 12
  54. /*!
  55. * \brief General jitterbuffer configuration.
  56. */
  57. struct ast_jb_conf
  58. {
  59. /*! \brief Combination of the AST_JB_ENABLED, AST_JB_FORCED and AST_JB_LOG flags. */
  60. unsigned int flags;
  61. /*! \brief Max size of the jitterbuffer implementation. */
  62. long max_size;
  63. /*! \brief Resynchronization threshold of the jitterbuffer implementation. */
  64. long resync_threshold;
  65. /*! \brief Name of the jitterbuffer implementation to be used. */
  66. char impl[AST_JB_IMPL_NAME_SIZE];
  67. /*! \brief amount of additional jitterbuffer adjustment */
  68. long target_extra;
  69. };
  70. /* Jitterbuffer configuration property names */
  71. #define AST_JB_CONF_PREFIX "jb"
  72. #define AST_JB_CONF_ENABLE "enable"
  73. #define AST_JB_CONF_FORCE "force"
  74. #define AST_JB_CONF_MAX_SIZE "maxsize"
  75. #define AST_JB_CONF_RESYNCH_THRESHOLD "resyncthreshold"
  76. #define AST_JB_CONF_TARGET_EXTRA "targetextra"
  77. #define AST_JB_CONF_IMPL "impl"
  78. #define AST_JB_CONF_LOG "log"
  79. /* Hooks for the abstract jb implementation */
  80. /*! \brief Create */
  81. typedef void * (*jb_create_impl)(struct ast_jb_conf *general_config);
  82. /*! \brief Destroy */
  83. typedef void (*jb_destroy_impl)(void *jb);
  84. /*! \brief Put first frame */
  85. typedef int (*jb_put_first_impl)(void *jb, struct ast_frame *fin, long now);
  86. /*! \brief Put frame */
  87. typedef int (*jb_put_impl)(void *jb, struct ast_frame *fin, long now);
  88. /*! \brief Get frame for now */
  89. typedef int (*jb_get_impl)(void *jb, struct ast_frame **fout, long now, long interpl);
  90. /*! \brief Get next */
  91. typedef long (*jb_next_impl)(void *jb);
  92. /*! \brief Remove first frame */
  93. typedef int (*jb_remove_impl)(void *jb, struct ast_frame **fout);
  94. /*! \brief Force resynch */
  95. typedef void (*jb_force_resynch_impl)(void *jb);
  96. /*! \brief Empty and reset jb */
  97. typedef void (*jb_empty_and_reset_impl)(void *jb);
  98. /*! \brief Check if late */
  99. typedef int (*jb_is_late_impl)(void *jb, long ts);
  100. /*!
  101. * \brief Jitterbuffer implementation struct.
  102. */
  103. struct ast_jb_impl
  104. {
  105. char name[AST_JB_IMPL_NAME_SIZE];
  106. enum ast_jb_type type;
  107. jb_create_impl create;
  108. jb_destroy_impl destroy;
  109. jb_put_first_impl put_first;
  110. jb_put_impl put;
  111. jb_get_impl get;
  112. jb_next_impl next;
  113. jb_remove_impl remove;
  114. jb_force_resynch_impl force_resync;
  115. jb_empty_and_reset_impl empty_and_reset;
  116. jb_is_late_impl is_late;
  117. };
  118. /*!
  119. * \brief General jitterbuffer state.
  120. */
  121. struct ast_jb
  122. {
  123. /*! \brief Jitterbuffer configuration. */
  124. struct ast_jb_conf conf;
  125. /*! \brief Jitterbuffer implementation to be used. */
  126. const struct ast_jb_impl *impl;
  127. /*! \brief Jitterbuffer object, passed to the implementation. */
  128. void *jbobj;
  129. /*! \brief The time the jitterbuffer was created. */
  130. struct timeval timebase;
  131. /*! \brief The time the next frame should be played. */
  132. long next;
  133. /*! \brief Voice format of the last frame in. */
  134. struct ast_format *last_format;
  135. /*! \brief File for frame timestamp tracing. */
  136. FILE *logfile;
  137. /*! \brief Jitterbuffer internal state flags. */
  138. unsigned int flags;
  139. };
  140. /*!
  141. * \brief Checks the need of a jb use in a generic bridge.
  142. * \param c0 first bridged channel.
  143. * \param c1 second bridged channel.
  144. *
  145. * Called from ast_generic_bridge() when two channels are entering in a bridge.
  146. * The function checks the need of a jitterbuffer, depending on both channel's
  147. * configuration and technology properties. As a result, this function sets
  148. * appropriate internal jb flags to the channels, determining further behaviour
  149. * of the bridged jitterbuffers.
  150. *
  151. * \retval zero if there are no jitter buffers in use
  152. * \retval non-zero if there are
  153. */
  154. int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1);
  155. /*!
  156. * \brief Calculates the time, left to the closest delivery moment in a bridge.
  157. * \param c0 first bridged channel.
  158. * \param c1 second bridged channel.
  159. * \param time_left bridge time limit, or -1 if not set.
  160. *
  161. * Called from ast_generic_bridge() to determine the maximum time to wait for
  162. * activity in ast_waitfor_n() call. If neihter of the channels is using jb,
  163. * this function returns the time limit passed.
  164. *
  165. * \return maximum time to wait.
  166. */
  167. int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left);
  168. /*!
  169. * \brief Puts a frame into a channel jitterbuffer.
  170. * \param chan channel.
  171. * \param f frame.
  172. *
  173. * Called from ast_generic_bridge() to put a frame into a channel's jitterbuffer.
  174. * The function will successfuly enqueue a frame if and only if:
  175. * 1. the channel is using a jitterbuffer (as determined by ast_jb_do_usecheck()),
  176. * 2. the frame's type is AST_FRAME_VOICE,
  177. * 3. the frame has timing info set and has length >= 2 ms,
  178. * 4. there is no some internal error happened (like failed memory allocation).
  179. * Frames, successfuly queued, should be delivered by the channel's jitterbuffer,
  180. * when their delivery time has came.
  181. * Frames, not successfuly queued, should be delivered immediately.
  182. * Dropped by the jb implementation frames are considered successfuly enqueued as
  183. * far as they should not be delivered at all.
  184. *
  185. * \retval 0 if the frame was queued
  186. * \retval -1 if not
  187. */
  188. int ast_jb_put(struct ast_channel *chan, struct ast_frame *f);
  189. /*!
  190. * \brief Deliver the queued frames that should be delivered now for both channels.
  191. * \param c0 first bridged channel.
  192. * \param c1 second bridged channel.
  193. *
  194. * Called from ast_generic_bridge() to deliver any frames, that should be delivered
  195. * for the moment of invocation. Does nothing if neihter of the channels is using jb
  196. * or has any frames currently queued in. The function delivers frames usig ast_write()
  197. * each of the channels.
  198. */
  199. void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1);
  200. /*!
  201. * \brief Destroys jitterbuffer on a channel.
  202. * \param chan channel.
  203. *
  204. * Called from ast_channel_free() when a channel is destroyed.
  205. */
  206. void ast_jb_destroy(struct ast_channel *chan);
  207. /*!
  208. * \brief Sets jitterbuffer configuration property.
  209. * \param conf configuration to store the property in.
  210. * \param varname property name.
  211. * \param value property value.
  212. *
  213. * Called from a channel driver to build a jitterbuffer configuration typically when
  214. * reading a configuration file. It is not necessary for a channel driver to know
  215. * each of the jb configuration property names. The jitterbuffer itself knows them.
  216. * The channel driver can pass each config var it reads through this function. It will
  217. * return 0 if the variable was consumed from the jb conf.
  218. *
  219. * \return zero if the property was set to the configuration, -1 if not.
  220. */
  221. int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value);
  222. /*!
  223. * \since 12.0
  224. * \brief Sets a jitterbuffer frame hook on the channel based on the channel's stored
  225. * jitterbuffer configuration
  226. *
  227. * \param chan Which channel is being set up
  228. */
  229. void ast_jb_enable_for_channel(struct ast_channel *chan);
  230. /*!
  231. * \brief Configures a jitterbuffer on a channel.
  232. * \param chan channel to configure.
  233. * \param conf configuration to apply.
  234. *
  235. * Called from a channel driver when a channel is created and its jitterbuffer needs
  236. * to be configured.
  237. */
  238. void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf);
  239. /*!
  240. * \brief Copies a channel's jitterbuffer configuration.
  241. * \param chan channel.
  242. * \param conf destination.
  243. */
  244. void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf);
  245. /*!
  246. * \brief drops all frames from a jitterbuffer and resets it
  247. * \param c0 one channel of a bridge
  248. * \param c1 the other channel of the bridge
  249. */
  250. void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1);
  251. const struct ast_jb_impl *ast_jb_get_impl(enum ast_jb_type type);
  252. /*!
  253. * \since 12
  254. * \brief Sets the contents of an ast_jb_conf struct to the default jitterbuffer settings
  255. *
  256. * \param conf Which jitterbuffer is being set
  257. */
  258. void ast_jb_conf_default(struct ast_jb_conf *conf);
  259. /*!
  260. * \since 12
  261. * \brief Applies a jitterbuffer framehook to a channel based on a provided jitterbuffer config
  262. *
  263. * \param chan Which channel the jitterbuffer is being set on
  264. * \param jb_conf Configuration to use for the jitterbuffer
  265. * \param prefer_existing If this is true and a jitterbuffer already exists for the channel,
  266. * use the existing jitterbuffer
  267. */
  268. void ast_jb_create_framehook(struct ast_channel *chan, struct ast_jb_conf *jb_conf, int prefer_existing);
  269. #if defined(__cplusplus) || defined(c_plusplus)
  270. }
  271. #endif
  272. #endif /* _ABSTRACT_JB_H_ */