jitterbuf.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * jitterbuf: an application-independent jitterbuffer
  3. *
  4. * Copyrights:
  5. * Copyright (C) 2004-2005, Horizon Wimba, Inc.
  6. *
  7. * Contributors:
  8. * Steve Kann <stevek@stevek.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU Lesser (Library) General Public License
  12. *
  13. * Copyright on this file is disclaimed to Digium for inclusion in Asterisk
  14. */
  15. /*! \file
  16. * \brief
  17. * jitterbuf: an application-independent jitterbuffer
  18. * \ref jitterbuf.c
  19. */
  20. #ifndef _JITTERBUF_H_
  21. #define _JITTERBUF_H_
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /*! \name configuration constants */
  26. /*@{ */
  27. /*! Number of historical timestamps to use in calculating jitter and drift */
  28. #define JB_HISTORY_SZ 500
  29. /*! what percentage of timestamps should we drop from the history when we examine it;
  30. * this might eventually be something made configurable */
  31. #define JB_HISTORY_DROPPCT 3
  32. /*! the maximum droppct we can handle (say it was configurable). */
  33. #define JB_HISTORY_DROPPCT_MAX 4
  34. /*! the size of the buffer we use to keep the top and botton timestamps for dropping */
  35. #define JB_HISTORY_MAXBUF_SZ JB_HISTORY_SZ * JB_HISTORY_DROPPCT_MAX / 100
  36. /*! amount of additional jitterbuffer adjustment */
  37. #define JB_TARGET_EXTRA 40
  38. /*! ms between growing and shrinking; may not be honored if jitterbuffer runs out of space */
  39. #define JB_ADJUST_DELAY 40
  40. /*@} */
  41. enum jb_return_code {
  42. /* return codes */
  43. JB_OK, /* 0 */
  44. JB_EMPTY, /* 1 */
  45. JB_NOFRAME, /* 2 */
  46. JB_INTERP, /* 3 */
  47. JB_DROP, /* 4 */
  48. JB_SCHED /* 5 */
  49. };
  50. enum jb_frame_type {
  51. /* frame types */
  52. JB_TYPE_CONTROL, /*!< 0 */
  53. JB_TYPE_VOICE, /*!< 1 */
  54. JB_TYPE_VIDEO, /*!< 2 - reserved */
  55. JB_TYPE_SILENCE /*!< 3 */
  56. };
  57. typedef struct jb_conf {
  58. /* settings */
  59. long max_jitterbuf; /*!< defines a hard clamp to use in setting the jitter buffer delay */
  60. long resync_threshold; /*!< the jb will resync when delay increases to (2 * jitter) + this param */
  61. long max_contig_interp; /*!< the max interp frames to return in a row */
  62. long target_extra ; /*!< amount of additional jitterbuffer adjustment, overrides JB_TARGET_EXTRA */
  63. } jb_conf;
  64. typedef struct jb_info {
  65. jb_conf conf;
  66. /* statistics */
  67. long frames_in; /*!< number of frames input to the jitterbuffer.*/
  68. long frames_out; /*!< number of frames output from the jitterbuffer.*/
  69. long frames_late; /*!< number of frames which were too late, and dropped.*/
  70. long frames_lost; /*!< number of missing frames.*/
  71. long frames_dropped; /*!< number of frames dropped (shrinkage) */
  72. long frames_ooo; /*!< number of frames received out-of-order */
  73. long frames_cur; /*!< number of frames presently in jb, awaiting delivery.*/
  74. long jitter; /*!< jitter measured within current history interval*/
  75. long min; /*!< minimum lateness within current history interval */
  76. long current; /*!< the present jitterbuffer adjustment */
  77. long target; /*!< the target jitterbuffer adjustment */
  78. long losspct; /*!< recent lost frame percentage (* 1000) */
  79. long next_voice_ts; /*!< the ts of the next frame to be read from the jb - in receiver's time */
  80. long last_voice_ms; /*!< the duration of the last voice frame */
  81. long silence_begin_ts; /*!< the time of the last CNG frame, when in silence */
  82. long last_adjustment; /*!< the time of the last adjustment */
  83. long last_delay; /*!< the last now added to history */
  84. long cnt_delay_discont; /*!< the count of discontinuous delays */
  85. long resync_offset; /*!< the amount to offset ts to support resyncs */
  86. long cnt_contig_interp; /*!< the number of contiguous interp frames returned */
  87. } jb_info;
  88. typedef struct jb_frame {
  89. void *data; /* the frame data */
  90. long ts; /* the relative delivery time expected */
  91. long ms; /* the time covered by this frame, in sec/8000 */
  92. enum jb_frame_type type; /* the type of frame */
  93. struct jb_frame *next, *prev;
  94. } jb_frame;
  95. typedef struct jitterbuf {
  96. jb_info info;
  97. /* history */
  98. long history[JB_HISTORY_SZ]; /*!< history */
  99. int hist_ptr; /*!< points to index in history for next entry */
  100. long hist_maxbuf[JB_HISTORY_MAXBUF_SZ]; /*!< a sorted buffer of the max delays (highest first) */
  101. long hist_minbuf[JB_HISTORY_MAXBUF_SZ]; /*!< a sorted buffer of the min delays (lowest first) */
  102. int hist_maxbuf_valid; /*!< are the "maxbuf"/minbuf valid? */
  103. unsigned int dropem:1; /*!< flag to indicate dropping frames (overload) */
  104. jb_frame *frames; /*!< queued frames */
  105. jb_frame *free; /*!< free frames (avoid malloc?) */
  106. } jitterbuf;
  107. /*! \brief new jitterbuf */
  108. jitterbuf * jb_new(void);
  109. /*! \brief destroy jitterbuf */
  110. void jb_destroy(jitterbuf *jb);
  111. /*! \brief reset jitterbuf
  112. * \note The jitterbuffer should be empty before you call this, otherwise
  113. * you will leak queued frames, and some internal structures */
  114. void jb_reset(jitterbuf *jb);
  115. /*!\brief queue a frame
  116. *
  117. * data=frame data, timings (in ms): ms=length of frame (for voice), ts=ts (sender's time)
  118. * now=now (in receiver's time) return value is one of
  119. * JB_OK: Frame added. Last call to jb_next() still valid
  120. * JB_DROP: Drop this frame immediately
  121. * JB_SCHED: Frame added. Call jb_next() to get a new time for the next frame
  122. */
  123. enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now);
  124. /*! \brief get a frame for time now (receiver's time) return value is one of
  125. * JB_OK: You've got frame!
  126. * JB_DROP: Here's an audio frame you should just drop. Ask me again for this time..
  127. * JB_NOFRAME: There's no frame scheduled for this time.
  128. * JB_INTERP: Please interpolate an interpl-length frame for this time (either we need to grow, or there was a lost frame)
  129. * JB_EMPTY: The jb is empty.
  130. */
  131. enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl);
  132. /*! \brief unconditionally get frames from jitterbuf until empty */
  133. enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout);
  134. /*! \brief when is the next frame due out, in receiver's time (0=EMPTY)
  135. * This value may change as frames are added (esp non-audio frames) */
  136. long jb_next(jitterbuf *jb);
  137. /*! \brief get jitterbuf info: only "statistics" may be valid */
  138. enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats);
  139. /*! \brief set jitterbuf conf */
  140. enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf);
  141. typedef void __attribute__((format(printf, 1, 2))) (*jb_output_function_t)(const char *fmt, ...);
  142. void jb_setoutput(jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg);
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. #endif