tdav_jitterbuffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* File from: http://cms.speakup.nl/tech/opensource/jitterbuffer/verslag-20051209.pdf/ */
  2. /*******************************************************
  3. * jitterbuffer:
  4. * an application-independent jitterbuffer, which tries
  5. * to achieve the maximum user perception during a call.
  6. * For more information look at:
  7. * http://www.speakup.nl/opensource/jitterbuffer/
  8. *
  9. * Copyright on this file is held by:
  10. * - Jesse Kaijen <jesse@speakup.nl>
  11. * - SpeakUp <info@speakup.nl>
  12. *
  13. * Contributors:
  14. * Jesse Kaijen <jesse@speakup.nl>
  15. *
  16. * Version: 1.1
  17. *
  18. * Changelog:
  19. * 1.0 => 1.1 (2006-03-24) (thanks to Micheal Jerris, freeswitch.org)
  20. * - added MSVC 2005 project files
  21. * - added JB_NOJB as return value
  22. *
  23. *
  24. * This program is free software, distributed under the terms of:
  25. * - the GNU Lesser (Library) General Public License
  26. * - the Mozilla Public License
  27. *
  28. * if you are interested in an different licence type, please contact us.
  29. *
  30. * How to use the jitterbuffer, please look at the comments
  31. * in the headerfile.
  32. *
  33. * Further details on specific implementations,
  34. * please look at the comments in the code file.
  35. */
  36. #ifndef TINYDAV_JITTERBUFFER_H_
  37. #define TINYDAV_JITTERBUFFER_H_
  38. #include "tinydav_config.h"
  39. #if !(HAVE_SPEEX_DSP && HAVE_SPEEX_JB)
  40. TDAV_BEGIN_DECLS
  41. /***********
  42. * The header file consists of four parts.
  43. * - configuration constants, structs and parameter definitions
  44. * - functions
  45. * - How to use the jitterbuffer and
  46. * which responsibilities do YOU have
  47. * - debug messages explained
  48. */
  49. // configuration constants
  50. /* Number of historical timestamps to use in calculating jitter and jitterbuffer size */
  51. #ifndef JB_HISTORY_SIZE
  52. # define JB_HISTORY_SIZE 500
  53. #endif
  54. /* minimum jitterbuffer size, disabled if 0 */
  55. #define JB_MIN_SIZE 0
  56. /* maximum jitterbuffer size, disabled if 0 */
  57. #define JB_MAX_SIZE 0
  58. /* maximum successive interpolating frames, disabled if 0 */
  59. #define JB_MAX_SUCCESSIVE_INTERP 0
  60. /* amount of extra delay allowed before shrinking */
  61. #define JB_ALLOW_EXTRA_DELAY 30
  62. /* ms between growing */
  63. #define JB_WAIT_GROW 60
  64. /* ms between shrinking */
  65. #define JB_WAIT_SHRINK 250
  66. /* ms that the JB max may be off */
  67. #define JB_MAX_DIFF 6000 //in a RTP stream the max_diff may be 3000 packets (most packets are 20ms)
  68. //structs
  69. typedef struct jb_info {
  70. long frames_received; /* Number of frames received by the jitterbuffer */
  71. long frames_late; /* Number of frames that were late */
  72. long frames_lost; /* Number of frames that were lost */
  73. long frames_ooo; /* Number of frames that were Out Of Order */
  74. long frames_dropped; /* Number of frames that were dropped due shrinkage of the jitterbuffer */
  75. long frames_dropped_twice; /* Number of frames that were dropped because this timestamp was already in the jitterbuffer */
  76. long delay; /* Current delay due the jitterbuffer */
  77. long jitter; /* jitter measured within current history interval*/
  78. long losspct; /* recent lost frame percentage (network and jitterbuffer loss) */
  79. long delay_target; /* The delay where we want to grow to */
  80. long losspct_jb; /* recent lost percentage due the jitterbuffer */
  81. long last_voice_ms; /* the duration of the last voice frame */
  82. short silence; /* If we are in silence 1-yes 0-no */
  83. long iqr; /* Inter Quartile Range of current history, if the squareroot is taken it is a good estimate of jitter */
  84. } jb_info;
  85. typedef struct jb_frame {
  86. void *data; /* the frame data */
  87. long ts; /* the senders timestamp */
  88. long ms; /* length of this frame in ms */
  89. int type; /* the type of frame */
  90. int codec; /* codec of this frame, undefined if nonvoice */
  91. struct jb_frame *next, *prev; /* pointers to the next and previous frames in the queue */
  92. } jb_frame;
  93. typedef struct jb_hist_element {
  94. long delay; /* difference between time of arrival and senders timestamp */
  95. long ts; /* senders timestamp */
  96. long ms; /* length of this frame in ms */
  97. int codec; /* wich codec this frame has */
  98. } jb_hist_element;
  99. typedef struct jb_settings {
  100. /* settings */
  101. long min_jb; /* defines a hard clamp to use in setting the jitterbuffer delay */
  102. long max_jb; /* defines a hard clamp to use in setting the jitterbuffer delay */
  103. long max_successive_interp; /* the maximum count of successive interpolations before assuming silence */
  104. long extra_delay; /* amount of extra delay allowed before shrinking */
  105. long wait_grow; /* ms between growing */
  106. long wait_shrink; /* ms between shrinking */
  107. long max_diff; /* maximum number of milliseconds the jitterbuffer may be off */
  108. } jb_settings;
  109. typedef struct jitterbuffer {
  110. struct jb_hist_element hist[JB_HISTORY_SIZE]; /* the history of the last received frames */
  111. long hist_sorted_delay[JB_HISTORY_SIZE]; /* a sorted buffer of the delays (lowest first) */
  112. long hist_sorted_timestamp[JB_HISTORY_SIZE]; /* a sorted buffer of the timestamps (lowest first) */
  113. int hist_pointer; /* points to index in history for next entry */
  114. long last_adjustment; /* the time of the last adjustment (growing or shrinking) */
  115. long next_voice_time; /* the next ts is to be read from the jb (senders timestamp) */
  116. long cnt_successive_interp; /* the count of consecutive interpolation frames */
  117. long silence_begin_ts; /* the time of the last CNG frame, when in silence */
  118. long min; /* the clock difference within current history interval */
  119. long current; /* the present jitterbuffer adjustment */
  120. long target; /* the target jitterbuffer adjustment */
  121. long last_delay; /* the delay of the last packet, used for calc. jitter */
  122. jb_frame *voiceframes; /* queued voiceframes */
  123. jb_frame *controlframes; /* queued controlframes */
  124. jb_settings settings; /* the settings of the jitterbuffer */
  125. jb_info info; /* the statistics of the jitterbuffer */
  126. } jitterbuffer;
  127. //parameter definitions
  128. /* return codes */
  129. #define JB_OK 0
  130. #define JB_EMPTY 1
  131. #define JB_NOFRAME 2
  132. #define JB_INTERP 3
  133. #define JB_NOJB 4
  134. /* frame types */
  135. #define JB_TYPE_CONTROL 1
  136. #define JB_TYPE_VOICE 2
  137. #define JB_TYPE_SILENCE 3
  138. /* the jitterbuffer behaives different for each codec. */
  139. /* Look in the code if a codec has his function defined */
  140. /* default is g711x behaiviour */
  141. #define JB_CODEC_SPEEX 10 //NOT defined
  142. #define JB_CODEC_ILBC 9 //NOT defined
  143. #define JB_CODEC_GSM_EFR 8
  144. #define JB_CODEC_GSM_FR 7 //NOT defined
  145. #define JB_CODEC_G723_1 6
  146. #define JB_CODEC_G729A 5
  147. #define JB_CODEC_G729 4
  148. #define JB_CODEC_G711x_PLC 3
  149. #define JB_CODEC_G711x 2
  150. #define JB_CODEC_OTHER 1 //NOT defined
  151. /*
  152. * Creates a new jitterbuffer and sets the default settings.
  153. * Always use this function for creating a new jitterbuffer.
  154. */
  155. jitterbuffer *jb_new();
  156. /*
  157. * The control frames and possible personal settings are kept.
  158. * History and voice/silence frames are destroyed.
  159. */
  160. void jb_reset(jitterbuffer *jb);
  161. /*
  162. * Resets the jitterbuffer totally, all the control/voice/silence frames are destroyed
  163. * default settings are put as well.
  164. */
  165. void jb_reset_all(jitterbuffer *jb);
  166. /*
  167. * Destroy the jitterbuffer and any frame within.
  168. * Always use this function for destroying a jitterbuffer,
  169. * otherwise there is a chance of memory leaking.
  170. */
  171. void jb_destroy(jitterbuffer *jb);
  172. /*
  173. * Define your own settings for the jitterbuffer. Only settings !=0
  174. * are put in the jitterbuffer.
  175. */
  176. void jb_set_settings(jitterbuffer *jb, jb_settings *settings);
  177. /*
  178. * Get the statistics for the jitterbuffer.
  179. * Copying the statistics directly for the jitterbuffer won't work because
  180. * The statistics are only calculated when calling this function.
  181. */
  182. void jb_get_info(jitterbuffer *jb, jb_info *stats);
  183. /*
  184. * Get the current settings of the jitterbuffer.
  185. */
  186. void jb_get_settings(jitterbuffer *jb, jb_settings *settings);
  187. /*
  188. * Gives an estimation of the MOS of a call given the
  189. * packetloss p, delay d, and wich codec is used.
  190. * The assumption is made that the echo cancelation is around 37dB.
  191. */
  192. float jb_guess_mos(float p, long d, int codec);
  193. /*
  194. * returns JB_OK if there are still frames left in the jitterbuffer
  195. * otherwise JB_EMPTY is returned.
  196. */
  197. int jb_has_frames(jitterbuffer *jb);
  198. /*
  199. * put a packet(frame) into the jitterbuffer.
  200. * *data - points to the packet
  201. * type - type of packet, JB_CONTROL|JB_VOICE|JB_SILENCE
  202. * ms - duration of frame (only voice)
  203. * ts - timestamp sender
  204. * now - current timestamp (timestamp of arrival)
  205. * codec - which codec the frame holds (only voice), if not defined, g711x will be used
  206. *
  207. * if type==control @REQUIRE: *data, type, ts, now
  208. * if type==voice @REQUIRE: *data, type, ms, ts, now @OPTIONAL: codec
  209. * if type==silence @REQUIRE: *data, type, ts, now
  210. * on return *data is undefined
  211. */
  212. void jb_put(jitterbuffer *jb, void *data, int type, long ms, long ts, long now, int codec);
  213. /*
  214. * Get a packet from the jitterbuffer if it's available.
  215. * control packets have a higher priority above voice and silence packets
  216. * they are always delivered as fast as possible. The delay of the jitterbuffer
  217. * doesn't work for these packets.
  218. * @REQUIRE 1<interpl <= jb->settings->extra_delay (=default JB_ALLOW_EXTRA_DELAY)
  219. *
  220. * return will be:
  221. * JB_OK, *data points to the packet
  222. * JB_INTERP, please interpolate for interpl milliseconds
  223. * JB_NOFRAME, no frame scheduled
  224. * JB_EMPTY, the jitterbuffer is empty
  225. */
  226. int jb_get(jitterbuffer *jb, void **data, long now, long interpl);
  227. /* debug functions */
  228. typedef void (*jb_output_function_t)(const char *fmt, ...);
  229. void jb_setoutput(jb_output_function_t warn, jb_output_function_t err, jb_output_function_t dbg);
  230. /*******************************
  231. * The use of the jitterbuffer *
  232. *******************************
  233. * Always create a new jitterbuffer with jb_new().
  234. * Always destroy a jitterbuffer with jb_destroy().
  235. *
  236. * There is no lock(mutex) mechanism, that your responsibility.
  237. * The reason for this is that different environments require
  238. * different ways of implementing a lock.
  239. *
  240. * The following functions require a lock on the jitterbuffer:
  241. * jb_reset(), jb_reset_all(), jb_destroy(), jb_set_settings(),
  242. * jb_get_info(), jb_get_settings(), jb_has_frames(), jb_put(),
  243. * jb_get()
  244. *
  245. * The following functions do NOT require a lock on the jitterbuffer:
  246. * jb_new(), jb_guess_mos()
  247. *
  248. * Since control packets have a higher priority above any other packet
  249. * a call may already be ended while there is audio left to play. We
  250. * advice that you poll the jitterbuffer if there are frames left.
  251. *
  252. * If the audiopath is oneway (eg. voicemailbox) and the latency doesn't
  253. * matter, we advice to set a minimum jitterbuffer size. Then there is
  254. * less loss and the quality is better.
  255. */
  256. /****************************
  257. * debug messages explained *
  258. ****************************
  259. * N - jb_new()
  260. * R - jb_reset()
  261. * r - jb_reset_all()
  262. * D - jb_destroy()
  263. * S - jb_set_settings()
  264. * H - jb_has_frames()
  265. * I - jb_get_info()
  266. * S - jb_get_settings()
  267. * pC - jb_put() put Control packet
  268. * pT - jb_put() Timestamp was already in the queue
  269. * pV - jb_put() put Voice packet
  270. * pS - jb_put() put Silence packet
  271. *
  272. * A - jb_get()
  273. * // below are all the possible debug info when trying to get a packet
  274. * gC - get_control() - there is a control message
  275. * gs - get_voice() - there is a silence frame
  276. * gS - get_voice() - we are in silence
  277. * gL - get_voice() - are in silence, frame is late
  278. * gP - get_voice() - are in silence, play frame (end of silence)
  279. * ag - get_voicecase() - grow little bit (diff < interpl/2)
  280. * aG - get_voicecase() - grow interpl
  281. * as - get_voicecase() - shrink by voiceframe we throw out
  282. * aS - get_voicecase() - shrink by interpl
  283. * aN - get_voicecase() - no time yet
  284. * aL - get_voicecase() - frame is late
  285. * aP - get_voicecase() - play frame
  286. * aI - get_voicecase() - interpolate
  287. */
  288. TDAV_END_DECLS
  289. #endif /* !(HAVE_SPEEX_DSP && HAVE_SPEEX_JB) */
  290. #endif /* TINYDAV_JITTERBUFFER_H_ */