| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * jitterbuf: an application-independent jitterbuffer | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyrights: | 
					
						
							|  |  |  |  * Copyright (C) 2004-2005, Horizon Wimba, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributors: | 
					
						
							|  |  |  |  * Steve Kann <stevek@stevek.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU Lesser (Library) General Public License | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright on this file is disclaimed to Digium for inclusion in Asterisk | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \file
 | 
					
						
							|  |  |  |  * \brief  | 
					
						
							|  |  |  |  * jitterbuf: an application-independent jitterbuffer | 
					
						
							|  |  |  |  * \ref jitterbuf.c | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | #ifndef _JITTERBUF_H_
 | 
					
						
							|  |  |  | #define _JITTERBUF_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \name configuration constants */ | 
					
						
							|  |  |  | /*@{ */ | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	/*! Number of historical timestamps to use in calculating jitter and drift */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | #define JB_HISTORY_SZ		500	
 | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	/*! what percentage of timestamps should we drop from the history when we examine it;
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 	 * this might eventually be something made configurable */ | 
					
						
							|  |  |  | #define JB_HISTORY_DROPPCT	3
 | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	/*! the maximum droppct we can handle (say it was configurable). */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | #define JB_HISTORY_DROPPCT_MAX	4
 | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	/*! the size of the buffer we use to keep the top and botton timestamps for dropping */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | #define JB_HISTORY_MAXBUF_SZ	JB_HISTORY_SZ * JB_HISTORY_DROPPCT_MAX / 100 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	/*! amount of additional jitterbuffer adjustment  */ | 
					
						
							| 
									
										
										
										
											2005-05-09 13:56:43 +00:00
										 |  |  | #define JB_TARGET_EXTRA 40
 | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	/*! ms between growing and shrinking; may not be honored if jitterbuffer runs out of space */ | 
					
						
							| 
									
										
										
										
											2005-05-09 13:56:43 +00:00
										 |  |  | #define JB_ADJUST_DELAY 40
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*@} */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | enum jb_return_code { | 
					
						
							|  |  |  | 	/* return codes */ | 
					
						
							|  |  |  | 	JB_OK,            /* 0 */ | 
					
						
							|  |  |  | 	JB_EMPTY,         /* 1 */ | 
					
						
							|  |  |  | 	JB_NOFRAME,       /* 2 */ | 
					
						
							|  |  |  | 	JB_INTERP,        /* 3 */ | 
					
						
							|  |  |  | 	JB_DROP,          /* 4 */ | 
					
						
							|  |  |  | 	JB_SCHED          /* 5 */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum jb_frame_type { | 
					
						
							|  |  |  | 	/* frame types */ | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | 	JB_TYPE_CONTROL,  /*!< 0            */ | 
					
						
							|  |  |  | 	JB_TYPE_VOICE,    /*!< 1            */ | 
					
						
							|  |  |  | 	JB_TYPE_VIDEO,    /*!< 2 - reserved */ | 
					
						
							|  |  |  | 	JB_TYPE_SILENCE   /*!< 3            */ | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2005-06-02 17:45:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct jb_conf { | 
					
						
							|  |  |  | 	/* settings */ | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	long max_jitterbuf;	/*!< defines a hard clamp to use in setting the jitter buffer delay */ | 
					
						
							|  |  |  |  	long resync_threshold;  /*!< the jb will resync when delay increases to (2 * jitter) + this param */ | 
					
						
							|  |  |  | 	long max_contig_interp; /*!< the max interp frames to return in a row */ | 
					
						
							|  |  |  | 	long target_extra ;      /*!< amount of additional jitterbuffer adjustment, overrides JB_TARGET_EXTRA */ | 
					
						
							| 
									
										
										
										
											2005-06-02 17:45:38 +00:00
										 |  |  | } jb_conf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | typedef struct jb_info { | 
					
						
							| 
									
										
										
										
											2005-06-02 17:45:38 +00:00
										 |  |  | 	jb_conf conf; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 	/* statistics */ | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	long frames_in;  	/*!< number of frames input to the jitterbuffer.*/ | 
					
						
							|  |  |  | 	long frames_out;  	/*!< number of frames output from the jitterbuffer.*/ | 
					
						
							|  |  |  | 	long frames_late; 	/*!< number of frames which were too late, and dropped.*/ | 
					
						
							|  |  |  | 	long frames_lost; 	/*!< number of missing frames.*/ | 
					
						
							|  |  |  | 	long frames_dropped; 	/*!< number of frames dropped (shrinkage) */ | 
					
						
							|  |  |  | 	long frames_ooo; 	/*!< number of frames received out-of-order */ | 
					
						
							|  |  |  | 	long frames_cur; 	/*!< number of frames presently in jb, awaiting delivery.*/ | 
					
						
							|  |  |  | 	long jitter; 		/*!< jitter measured within current history interval*/ | 
					
						
							|  |  |  | 	long min;		/*!< minimum lateness within current history interval */ | 
					
						
							|  |  |  | 	long current; 		/*!< the present jitterbuffer adjustment */ | 
					
						
							|  |  |  | 	long target; 		/*!< the target jitterbuffer adjustment */ | 
					
						
							|  |  |  | 	long losspct; 		/*!< recent lost frame percentage (* 1000) */ | 
					
						
							|  |  |  | 	long next_voice_ts;	/*!< the ts of the next frame to be read from the jb - in receiver's time */ | 
					
						
							|  |  |  | 	long last_voice_ms;	/*!< the duration of the last voice frame */ | 
					
						
							|  |  |  | 	long silence_begin_ts;	/*!< the time of the last CNG frame, when in silence */ | 
					
						
							|  |  |  | 	long last_adjustment;   /*!< the time of the last adjustment */ | 
					
						
							|  |  |  |  	long last_delay;        /*!< the last now added to history */ | 
					
						
							|  |  |  |  	long cnt_delay_discont;	/*!< the count of discontinuous delays */ | 
					
						
							|  |  |  |  	long resync_offset;     /*!< the amount to offset ts to support resyncs */ | 
					
						
							|  |  |  | 	long cnt_contig_interp; /*!< the number of contiguous interp frames returned */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } jb_info; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct jb_frame { | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | 	void *data;               /* the frame data */ | 
					
						
							|  |  |  | 	long ts;                  /* the relative delivery time expected */ | 
					
						
							|  |  |  | 	long ms;                  /* the time covered by this frame, in sec/8000 */ | 
					
						
							|  |  |  | 	enum jb_frame_type type;  /* the type of frame */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 	struct jb_frame *next, *prev; | 
					
						
							|  |  |  | } jb_frame; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct jitterbuf { | 
					
						
							|  |  |  | 	jb_info info; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* history */ | 
					
						
							| 
									
										
										
										
											2007-07-23 14:32:04 +00:00
										 |  |  | 	long history[JB_HISTORY_SZ];   		/*!< history */ | 
					
						
							|  |  |  | 	int  hist_ptr;				/*!< points to index in history for next entry */ | 
					
						
							|  |  |  | 	long hist_maxbuf[JB_HISTORY_MAXBUF_SZ];	/*!< a sorted buffer of the max delays (highest first) */ | 
					
						
							|  |  |  | 	long hist_minbuf[JB_HISTORY_MAXBUF_SZ];	/*!< a sorted buffer of the min delays (lowest first) */ | 
					
						
							|  |  |  | 	int  hist_maxbuf_valid;			/*!< are the "maxbuf"/minbuf valid? */ | 
					
						
							|  |  |  | 	unsigned int dropem:1;                  /*!< flag to indicate dropping frames (overload) */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jb_frame *frames; 		/*!< queued frames */ | 
					
						
							|  |  |  | 	jb_frame *free; 		/*!< free frames (avoid malloc?) */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | } jitterbuf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief new jitterbuf */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | jitterbuf *		jb_new(void); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief destroy jitterbuf */ | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | void			jb_destroy(jitterbuf *jb); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief reset jitterbuf
 | 
					
						
							|  |  |  |  * \note The jitterbuffer should be empty before you call this, otherwise | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  * you will leak queued frames, and some internal structures */ | 
					
						
							|  |  |  | void			jb_reset(jitterbuf *jb); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*!\brief  queue a frame 
 | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * data=frame data, timings (in ms): ms=length of frame (for voice), ts=ts (sender's time)  | 
					
						
							| 
									
										
										
										
											2005-05-19 01:24:09 +00:00
										 |  |  |  * now=now (in receiver's time) return value is one of  | 
					
						
							|  |  |  |  * JB_OK: Frame added. Last call to jb_next() still valid | 
					
						
							|  |  |  |  * JB_DROP: Drop this frame immediately | 
					
						
							|  |  |  |  * JB_SCHED: Frame added. Call jb_next() to get a new time for the next frame | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief get a frame for time now (receiver's time)  return value is one of
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  * JB_OK:  You've got frame! | 
					
						
							|  |  |  |  * JB_DROP: Here's an audio frame you should just drop.  Ask me again for this time.. | 
					
						
							|  |  |  |  * JB_NOFRAME: There's no frame scheduled for this time. | 
					
						
							| 
									
										
										
										
											2005-05-12 19:01:03 +00:00
										 |  |  |  * JB_INTERP: Please interpolate an interpl-length frame for this time (either we need to grow, or there was a lost frame)  | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  * JB_EMPTY: The jb is empty. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frame, long now, long interpl); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief unconditionally get frames from jitterbuf until empty */ | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief when is the next frame due out, in receiver's time (0=EMPTY) 
 | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  |  * This value may change as frames are added (esp non-audio frames) */ | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | long	jb_next(jitterbuf *jb); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief get jitterbuf info: only "statistics" may be valid */ | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | /*! \brief set jitterbuf conf */ | 
					
						
							| 
									
										
										
										
											2006-02-27 01:37:56 +00:00
										 |  |  | enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 17:57:39 +00:00
										 |  |  | typedef void __attribute__((format(printf, 1, 2))) (*jb_output_function_t)(const char *fmt, ...); | 
					
						
							| 
									
										
										
										
											2007-12-11 22:20:22 +00:00
										 |  |  | void jb_setoutput(jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg); | 
					
						
							| 
									
										
										
										
											2005-03-17 21:30:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |