jitterbuf.h 5.8 KB

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