jbuf.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * Based on implementation kindly contributed by Switchlab, Ltd.
  21. */
  22. #ifndef __PJMEDIA_JBUF_H__
  23. #define __PJMEDIA_JBUF_H__
  24. /**
  25. * @file jbuf.h
  26. * @brief Adaptive jitter buffer implementation.
  27. */
  28. #include <pjmedia/types.h>
  29. /**
  30. * @defgroup PJMED_JBUF Adaptive jitter buffer
  31. * @ingroup PJMEDIA_FRAME_OP
  32. * @brief Adaptive de-jitter buffering implementation
  33. * @{
  34. *
  35. * This section describes PJMEDIA's implementation of de-jitter buffer.
  36. * The de-jitter buffer may be set to operate in adaptive mode or fixed
  37. * delay mode.
  38. */
  39. PJ_BEGIN_DECL
  40. /**
  41. * Types of frame returned by the jitter buffer.
  42. */
  43. typedef enum pjmedia_jb_frame_type
  44. {
  45. PJMEDIA_JB_MISSING_FRAME = 0, /**< No frame because it's missing */
  46. PJMEDIA_JB_NORMAL_FRAME = 1, /**< Normal frame is being returned */
  47. PJMEDIA_JB_ZERO_PREFETCH_FRAME = 2, /**< Zero frame is being returned
  48. because JB is bufferring. */
  49. PJMEDIA_JB_ZERO_EMPTY_FRAME = 3 /**< Zero frame is being returned
  50. because JB is empty. */
  51. } pjmedia_jb_frame_type;
  52. /**
  53. * Enumeration of jitter buffer discard algorithm. The jitter buffer
  54. * continuously calculates the jitter level to get the optimum latency at
  55. * any time and in order to adjust the latency, the jitter buffer may need
  56. * to discard some frames.
  57. */
  58. typedef enum pjmedia_jb_discard_algo
  59. {
  60. /**
  61. * Jitter buffer should not discard any frame, except when the jitter
  62. * buffer is full and a new frame arrives, one frame will be discarded
  63. * to make space for the new frame.
  64. */
  65. PJMEDIA_JB_DISCARD_NONE = 0,
  66. /**
  67. * Only discard one frame in at least 200ms when the latency is considered
  68. * much higher than it should be. When the jitter buffer is full and a new
  69. * frame arrives, one frame will be discarded to make space for the new
  70. * frame.
  71. */
  72. PJMEDIA_JB_DISCARD_STATIC,
  73. /**
  74. * The discard rate is dynamically calculated based on actual parameters
  75. * such as jitter level and latency. When the jitter buffer is full and
  76. * a new frame arrives, one frame will be discarded to make space for the
  77. * new frame.
  78. */
  79. PJMEDIA_JB_DISCARD_PROGRESSIVE
  80. } pjmedia_jb_discard_algo;
  81. /**
  82. * This structure describes jitter buffer state.
  83. */
  84. typedef struct pjmedia_jb_state
  85. {
  86. /* Setting */
  87. unsigned frame_size; /**< Individual frame size, in bytes. */
  88. unsigned min_prefetch; /**< Minimum allowed prefetch, in frms. */
  89. unsigned max_prefetch; /**< Maximum allowed prefetch, in frms. */
  90. unsigned max_count; /**< Jitter buffer capacity, in frames. */
  91. /* Status */
  92. unsigned burst; /**< Current burst level, in frames */
  93. unsigned prefetch; /**< Current prefetch value, in frames */
  94. unsigned size; /**< Current buffer size, in frames. */
  95. /* Statistic */
  96. unsigned avg_delay; /**< Average delay, in ms. */
  97. unsigned min_delay; /**< Minimum delay, in ms. */
  98. unsigned max_delay; /**< Maximum delay, in ms. */
  99. unsigned dev_delay; /**< Standard deviation of delay, in ms.*/
  100. unsigned avg_burst; /**< Average burst, in frames. */
  101. unsigned lost; /**< Number of lost frames. */
  102. unsigned discard; /**< Number of discarded frames. */
  103. unsigned empty; /**< Number of empty on GET events. */
  104. } pjmedia_jb_state;
  105. /**
  106. * The constant PJMEDIA_JB_DEFAULT_INIT_DELAY specifies default jitter
  107. * buffer prefetch count during jitter buffer creation.
  108. */
  109. #define PJMEDIA_JB_DEFAULT_INIT_DELAY 15
  110. /**
  111. * Opaque declaration for jitter buffer.
  112. */
  113. typedef struct pjmedia_jbuf pjmedia_jbuf;
  114. /**
  115. * Create an adaptive jitter buffer according to the specification. If
  116. * application wants to have a fixed jitter buffer, it may call
  117. * #pjmedia_jbuf_set_fixed() after the jitter buffer is created. Also
  118. * if application wants to alter the discard algorithm, which the default
  119. * PJMEDIA_JB_DISCARD_PROGRESSIVE, it may call #pjmedia_jbuf_set_discard().
  120. *
  121. * This function may allocate large chunk of memory to keep the frames in
  122. * the buffer.
  123. *
  124. * @param pool The pool to allocate memory.
  125. * @param name Name to identify the jitter buffer for logging
  126. * purpose.
  127. * @param frame_size The size of each frame that will be kept in the
  128. * jitter buffer, in bytes. This should correspond
  129. * to the minimum frame size supported by the codec.
  130. * For example, a 10ms frame (80 bytes) would be
  131. * recommended for G.711 codec.
  132. * @param max_count Maximum number of frames that can be kept in the
  133. * jitter buffer. This effectively means the maximum
  134. * delay that may be introduced by this jitter
  135. * buffer.
  136. * @param ptime Indication of frame duration, used to calculate
  137. * the interval between jitter recalculation.
  138. * @param p_jb Pointer to receive jitter buffer instance.
  139. *
  140. * @return PJ_SUCCESS on success.
  141. */
  142. PJ_DECL(pj_status_t) pjmedia_jbuf_create(pj_pool_t *pool,
  143. const pj_str_t *name,
  144. unsigned frame_size,
  145. unsigned ptime,
  146. unsigned max_count,
  147. pjmedia_jbuf **p_jb);
  148. /**
  149. * Set the jitter buffer's frame duration.
  150. *
  151. * @param jb The jitter buffer.
  152. * @param ptime Frame ptime.
  153. *
  154. * @return PJ_SUCCESS on success.
  155. */
  156. PJ_DECL(pj_status_t) pjmedia_jbuf_set_ptime( pjmedia_jbuf *jb,
  157. unsigned ptime);
  158. /**
  159. * Set the jitter buffer's frame duration.
  160. *
  161. * @param jb The jitter buffer.
  162. * @param ptime Frame ptime.
  163. * @param ptime_denum Frame ptime denumerator.
  164. *
  165. * @return PJ_SUCCESS on success.
  166. */
  167. PJ_DECL(pj_status_t) pjmedia_jbuf_set_ptime2(pjmedia_jbuf *jb,
  168. unsigned ptime,
  169. unsigned ptime_denum);
  170. /**
  171. * Set the jitter buffer to fixed delay mode. The default behavior
  172. * is to adapt the delay with actual packet delay.
  173. *
  174. * @param jb The jitter buffer
  175. * @param prefetch The fixed delay value, in number of frames.
  176. *
  177. * @return PJ_SUCCESS on success.
  178. */
  179. PJ_DECL(pj_status_t) pjmedia_jbuf_set_fixed( pjmedia_jbuf *jb,
  180. unsigned prefetch);
  181. /**
  182. * Set the jitter buffer to adaptive mode.
  183. *
  184. * @param jb The jitter buffer.
  185. * @param prefetch The initial prefetch value to be applied to the
  186. * jitter buffer. Setting this to other than 0 will
  187. * activate prefetch buffering, a jitter buffer feature
  188. * that each time it gets empty, it won't return a
  189. * normal frame until its size reaches the number
  190. * specified here.
  191. * @param min_prefetch The minimum delay that must be applied to each
  192. * incoming packets, in number of frames.
  193. * @param max_prefetch The maximum allowable value for prefetch delay,
  194. * in number of frames.
  195. *
  196. * @return PJ_SUCCESS on success.
  197. */
  198. PJ_DECL(pj_status_t) pjmedia_jbuf_set_adaptive( pjmedia_jbuf *jb,
  199. unsigned prefetch,
  200. unsigned min_prefetch,
  201. unsigned max_prefetch);
  202. /**
  203. * Set the jitter buffer discard algorithm. The default discard algorithm,
  204. * set in jitter buffer creation, is PJMEDIA_JB_DISCARD_PROGRESSIVE.
  205. *
  206. * @param jb The jitter buffer.
  207. * @param algo The discard algorithm to be used.
  208. *
  209. * @return PJ_SUCCESS on success.
  210. */
  211. PJ_DECL(pj_status_t) pjmedia_jbuf_set_discard(pjmedia_jbuf *jb,
  212. pjmedia_jb_discard_algo algo);
  213. /**
  214. * Destroy jitter buffer instance.
  215. *
  216. * @param jb The jitter buffer.
  217. *
  218. * @return PJ_SUCCESS on success.
  219. */
  220. PJ_DECL(pj_status_t) pjmedia_jbuf_destroy(pjmedia_jbuf *jb);
  221. /**
  222. * Restart jitter. This function flushes all packets in the buffer and
  223. * reset the internal sequence number.
  224. *
  225. * @param jb The jitter buffer.
  226. *
  227. * @return PJ_SUCCESS on success.
  228. */
  229. PJ_DECL(pj_status_t) pjmedia_jbuf_reset(pjmedia_jbuf *jb);
  230. /**
  231. * Put a frame to the jitter buffer. If the frame can be accepted (based
  232. * on the sequence number), the jitter buffer will copy the frame and put
  233. * it in the appropriate position in the buffer.
  234. *
  235. * Application MUST manage it's own synchronization when multiple threads
  236. * are accessing the jitter buffer at the same time.
  237. *
  238. * @param jb The jitter buffer.
  239. * @param frame Pointer to frame buffer to be stored in the jitter
  240. * buffer.
  241. * @param size The frame size.
  242. * @param frame_seq The frame sequence number.
  243. */
  244. PJ_DECL(void) pjmedia_jbuf_put_frame( pjmedia_jbuf *jb,
  245. const void *frame,
  246. pj_size_t size,
  247. int frame_seq);
  248. /**
  249. * Put a frame to the jitter buffer. If the frame can be accepted (based
  250. * on the sequence number), the jitter buffer will copy the frame and put
  251. * it in the appropriate position in the buffer.
  252. *
  253. * Application MUST manage it's own synchronization when multiple threads
  254. * are accessing the jitter buffer at the same time.
  255. *
  256. * @param jb The jitter buffer.
  257. * @param frame Pointer to frame buffer to be stored in the jitter
  258. * buffer.
  259. * @param size The frame size.
  260. * @param bit_info Bit precise info of the frame, e.g: a frame may not
  261. * exactly start and end at the octet boundary, so this
  262. * field may be used for specifying start & end bit
  263. * offset.
  264. * @param frame_seq The frame sequence number.
  265. * @param discarded Flag whether the frame is discarded by jitter buffer.
  266. */
  267. PJ_DECL(void) pjmedia_jbuf_put_frame2( pjmedia_jbuf *jb,
  268. const void *frame,
  269. pj_size_t size,
  270. pj_uint32_t bit_info,
  271. int frame_seq,
  272. pj_bool_t *discarded);
  273. /**
  274. * Put a frame to the jitter buffer. If the frame can be accepted (based
  275. * on the sequence number), the jitter buffer will copy the frame and put
  276. * it in the appropriate position in the buffer.
  277. *
  278. * Application MUST manage it's own synchronization when multiple threads
  279. * are accessing the jitter buffer at the same time.
  280. *
  281. * @param jb The jitter buffer.
  282. * @param frame Pointer to frame buffer to be stored in the jitter
  283. * buffer.
  284. * @param size The frame size.
  285. * @param bit_info Bit precise info of the frame, e.g: a frame may not
  286. * exactly start and end at the octet boundary, so this
  287. * field may be used for specifying start & end bit
  288. * offset.
  289. * @param frame_seq The frame sequence number.
  290. * @param frame_ts The frame timestamp.
  291. * @param discarded Flag whether the frame is discarded by jitter buffer.
  292. */
  293. PJ_DECL(void) pjmedia_jbuf_put_frame3( pjmedia_jbuf *jb,
  294. const void *frame,
  295. pj_size_t size,
  296. pj_uint32_t bit_info,
  297. int frame_seq,
  298. pj_uint32_t frame_ts,
  299. pj_bool_t *discarded);
  300. /**
  301. * Get a frame from the jitter buffer. The jitter buffer will return the
  302. * oldest frame from it's buffer, when it is available.
  303. *
  304. * Application MUST manage it's own synchronization when multiple threads
  305. * are accessing the jitter buffer at the same time.
  306. *
  307. * @param jb The jitter buffer.
  308. * @param frame Buffer to receive the payload from the jitter buffer.
  309. * Application MUST make sure that the buffer has
  310. * appropriate size (i.e. not less than the frame size,
  311. * as specified when the jitter buffer was created).
  312. * The jitter buffer only copied a frame to this
  313. * buffer when the frame type returned by this function
  314. * is PJMEDIA_JB_NORMAL_FRAME.
  315. * @param p_frm_type Pointer to receive frame type. If jitter buffer is
  316. * currently empty or bufferring, the frame type will
  317. * be set to PJMEDIA_JB_ZERO_FRAME, and no frame will
  318. * be copied. If the jitter buffer detects that frame is
  319. * missing with current sequence number, the frame type
  320. * will be set to PJMEDIA_JB_MISSING_FRAME, and no
  321. * frame will be copied. If there is a frame, the jitter
  322. * buffer will copy the frame to the buffer, and frame
  323. * type will be set to PJMEDIA_JB_NORMAL_FRAME.
  324. */
  325. PJ_DECL(void) pjmedia_jbuf_get_frame( pjmedia_jbuf *jb,
  326. void *frame,
  327. char *p_frm_type);
  328. /**
  329. * Get a frame from the jitter buffer. The jitter buffer will return the
  330. * oldest frame from it's buffer, when it is available.
  331. *
  332. * @param jb The jitter buffer.
  333. * @param frame Buffer to receive the payload from the jitter buffer.
  334. * @see pjmedia_jbuf_get_frame().
  335. * @param size On input, it points to maximum buffer length.
  336. * On output, it will be filled with received frame size.
  337. * @param p_frm_type Pointer to receive frame type.
  338. * @see pjmedia_jbuf_get_frame().
  339. * @param bit_info Bit precise info of the frame, e.g: a frame may not
  340. * exactly start and end at the octet boundary, so this
  341. * field may be used for specifying start & end bit
  342. * offset.
  343. */
  344. PJ_DECL(void) pjmedia_jbuf_get_frame2(pjmedia_jbuf *jb,
  345. void *frame,
  346. pj_size_t *size,
  347. char *p_frm_type,
  348. pj_uint32_t *bit_info);
  349. /**
  350. * Get a frame from the jitter buffer. The jitter buffer will return the
  351. * oldest frame from it's buffer, when it is available.
  352. *
  353. * @param jb The jitter buffer.
  354. * @param frame Buffer to receive the payload from the jitter buffer.
  355. * @see pjmedia_jbuf_get_frame().
  356. * @param size On input, it points to maximum buffer length.
  357. * On output, it will be filled with received frame size.
  358. * @param p_frm_type Pointer to receive frame type.
  359. * @see pjmedia_jbuf_get_frame().
  360. * @param bit_info Bit precise info of the frame, e.g: a frame may not
  361. * exactly start and end at the octet boundary, so this
  362. * field may be used for specifying start & end bit
  363. * offset.
  364. * @param ts Frame timestamp.
  365. * @param seq Frame sequence number.
  366. */
  367. PJ_DECL(void) pjmedia_jbuf_get_frame3(pjmedia_jbuf *jb,
  368. void *frame,
  369. pj_size_t *size,
  370. char *p_frm_type,
  371. pj_uint32_t *bit_info,
  372. pj_uint32_t *ts,
  373. int *seq);
  374. /**
  375. * Peek a frame from the jitter buffer. The jitter buffer state will not be
  376. * modified.
  377. *
  378. * @param jb The jitter buffer.
  379. * @param offset Offset from the oldest frame to be peeked.
  380. * @param frame Buffer to receive the payload from the jitter buffer.
  381. * @see pjmedia_jbuf_get_frame().
  382. * @param size Pointer to receive frame size.
  383. * @param p_frm_type Pointer to receive frame type.
  384. * @see pjmedia_jbuf_get_frame().
  385. * @param bit_info Bit precise info of the frame, e.g: a frame may not
  386. * exactly start and end at the octet boundary, so this
  387. * field may be used for specifying start & end bit
  388. * offset.
  389. * @param ts Frame timestamp.
  390. * @param seq Frame sequence number.
  391. */
  392. PJ_DECL(void) pjmedia_jbuf_peek_frame(pjmedia_jbuf *jb,
  393. unsigned offset,
  394. const void **frame,
  395. pj_size_t *size,
  396. char *p_frm_type,
  397. pj_uint32_t *bit_info,
  398. pj_uint32_t *ts,
  399. int *seq);
  400. /**
  401. * Remove frames from the jitter buffer.
  402. *
  403. * @param jb The jitter buffer.
  404. * @param frame_cnt Number of frames to be removed.
  405. *
  406. * @return The number of frame successfully removed.
  407. */
  408. PJ_DECL(unsigned) pjmedia_jbuf_remove_frame(pjmedia_jbuf *jb,
  409. unsigned frame_cnt);
  410. /**
  411. * Check if the jitter buffer is full.
  412. *
  413. * @param jb The jitter buffer.
  414. *
  415. * @return PJ_TRUE if it is full.
  416. */
  417. PJ_DECL(pj_bool_t) pjmedia_jbuf_is_full(const pjmedia_jbuf *jb);
  418. /**
  419. * Get jitter buffer current state/settings.
  420. *
  421. * @param jb The jitter buffer.
  422. * @param state Buffer to receive jitter buffer state.
  423. *
  424. * @return PJ_SUCCESS on success.
  425. */
  426. PJ_DECL(pj_status_t) pjmedia_jbuf_get_state( const pjmedia_jbuf *jb,
  427. pjmedia_jb_state *state );
  428. PJ_END_DECL
  429. /**
  430. * @}
  431. */
  432. #endif /* __PJMEDIA_JBUF_H__ */