Changeset 4728 for pjproject/trunk/pjmedia/src/pjmedia/jbuf.c
- Timestamp:
- Feb 4, 2014 10:13:56 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/src/pjmedia/jbuf.c
r4537 r4728 1 1 /* $Id$ */ 2 /* 2 /* 3 3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) 4 4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> … … 16 16 * You should have received a copy of the GNU General Public License 17 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 19 */ 20 20 /* … … 36 36 #define INVALID_OFFSET -9999 37 37 38 /* Maximum burst length, whenever an operation is bursting longer than 38 /* Maximum burst length, whenever an operation is bursting longer than 39 39 * this value, JB will assume that the opposite operation was idle. 40 40 */ … … 47 47 48 48 49 /* Minimal difference between JB size and 2*burst-level to perform 50 * JB shrinking in static discard algorithm. 49 /* Minimal difference between JB size and 2*burst-level to perform 50 * JB shrinking in static discard algorithm. 51 51 */ 52 52 #define STA_DISC_SAFE_SHRINKING_DIFF 1 … … 68 68 pj_uint32_t *bit_info; /**< frame bit info array */ 69 69 pj_uint32_t *ts; /**< timestamp array */ 70 70 71 71 /* States */ 72 72 unsigned head; /**< index of head, pointed frame 73 73 will be returned by next GET */ 74 unsigned size; /**< current size of framelist, 74 unsigned size; /**< current size of framelist, 75 75 including discarded frames. */ 76 unsigned discarded_num; /**< current number of discarded 76 unsigned discarded_num; /**< current number of discarded 77 77 frames. */ 78 78 int origin; /**< original index of flist_head */ … … 92 92 pj_size_t jb_frame_size; /**< frame size */ 93 93 unsigned jb_frame_ptime; /**< frame duration. */ 94 pj_size_t jb_max_count; /**< capacity of jitter buffer, 94 pj_size_t jb_max_count; /**< capacity of jitter buffer, 95 95 in frames */ 96 96 int jb_init_prefetch; /**< Initial prefetch */ … … 98 98 int jb_max_prefetch; /**< Maximum allowable prefetch */ 99 99 int jb_max_burst; /**< maximum possible burst, whenever 100 burst exceeds this value, it 101 won't be included in level 100 burst exceeds this value, it 101 won't be included in level 102 102 calculation */ 103 103 int jb_min_shrink_gap; /**< How often can we shrink */ … … 108 108 109 109 /* States */ 110 int jb_level; /**< delay between source & 111 destination (calculated according 112 of the number of burst get/put 110 int jb_level; /**< delay between source & 111 destination (calculated according 112 of the number of burst get/put 113 113 operations) */ 114 int jb_max_hist_level; /**< max level during the last level 114 int jb_max_hist_level; /**< max level during the last level 115 115 calculations */ 116 int jb_stable_hist; /**< num of times the delay has been 116 int jb_stable_hist; /**< num of times the delay has been 117 117 lower then the prefetch num */ 118 int jb_last_op; /**< last operation executed 118 int jb_last_op; /**< last operation executed 119 119 (put/get) */ 120 120 int jb_eff_level; /**< effective burst level */ 121 int jb_prefetch; /**< no. of frame to insert before 122 removing some (at the beginning 123 of the framelist->content 121 int jb_prefetch; /**< no. of frame to insert before 122 removing some (at the beginning 123 of the framelist->content 124 124 operation), the value may be 125 125 continuously updated based on 126 126 current frame burst level. */ 127 127 pj_bool_t jb_prefetching; /**< flag if jbuf is prefetching. */ 128 int jb_status; /**< status is 'init' until the first 128 int jb_status; /**< status is 'init' until the first 129 129 'put' operation */ 130 int jb_init_cycle_cnt; /**< status is 'init' until the first 130 int jb_init_cycle_cnt; /**< status is 'init' until the first 131 131 'put' operation */ 132 132 … … 137 137 138 138 /* Statistics */ 139 pj_math_stat jb_delay; /**< Delay statistics of jitter buffer 139 pj_math_stat jb_delay; /**< Delay statistics of jitter buffer 140 140 (in ms) */ 141 141 pj_math_stat jb_burst; /**< Burst statistics (in frames) */ … … 165 165 166 166 167 /* Enabling this would log the jitter buffer state about once per 167 /* Enabling this would log the jitter buffer state about once per 168 168 * second. 169 169 */ … … 181 181 jb_framelist_t *framelist, 182 182 unsigned frame_size, 183 unsigned max_count) 183 unsigned max_count) 184 184 { 185 185 PJ_ASSERT_RETURN(pool && framelist, PJ_EINVAL); … … 189 189 framelist->frame_size = frame_size; 190 190 framelist->max_count = max_count; 191 framelist->content = (char*) 191 framelist->content = (char*) 192 192 pj_pool_alloc(pool, 193 framelist->frame_size* 193 framelist->frame_size* 194 194 framelist->max_count); 195 195 framelist->frame_type = (int*) 196 pj_pool_alloc(pool, 196 pj_pool_alloc(pool, 197 197 sizeof(framelist->frame_type[0])* 198 198 framelist->max_count); 199 199 framelist->content_len = (pj_size_t*) 200 pj_pool_alloc(pool, 200 pj_pool_alloc(pool, 201 201 sizeof(framelist->content_len[0])* 202 202 framelist->max_count); 203 203 framelist->bit_info = (pj_uint32_t*) 204 pj_pool_alloc(pool, 204 pj_pool_alloc(pool, 205 205 sizeof(framelist->bit_info[0])* 206 206 framelist->max_count); 207 207 framelist->ts = (pj_uint32_t*) 208 pj_pool_alloc(pool, 208 pj_pool_alloc(pool, 209 209 sizeof(framelist->ts[0])* 210 210 framelist->max_count); … … 214 214 } 215 215 216 static pj_status_t jb_framelist_destroy(jb_framelist_t *framelist) 216 static pj_status_t jb_framelist_destroy(jb_framelist_t *framelist) 217 217 { 218 218 PJ_UNUSED_ARG(framelist); … … 220 220 } 221 221 222 static pj_status_t jb_framelist_reset(jb_framelist_t *framelist) 222 static pj_status_t jb_framelist_reset(jb_framelist_t *framelist) 223 223 { 224 224 framelist->head = 0; … … 228 228 229 229 230 //pj_bzero(framelist->content, 231 // framelist->frame_size * 230 //pj_bzero(framelist->content, 231 // framelist->frame_size * 232 232 // framelist->max_count); 233 233 234 234 pj_memset(framelist->frame_type, 235 235 PJMEDIA_JB_MISSING_FRAME, 236 sizeof(framelist->frame_type[0]) * 236 sizeof(framelist->frame_type[0]) * 237 237 framelist->max_count); 238 238 239 pj_bzero(framelist->content_len, 240 sizeof(framelist->content_len[0]) * 239 pj_bzero(framelist->content_len, 240 sizeof(framelist->content_len[0]) * 241 241 framelist->max_count); 242 242 243 243 //pj_bzero(framelist->bit_info, 244 // sizeof(framelist->bit_info[0]) * 244 // sizeof(framelist->bit_info[0]) * 245 245 // framelist->max_count); 246 246 … … 249 249 250 250 251 static unsigned jb_framelist_size(const jb_framelist_t *framelist) 251 static unsigned jb_framelist_size(const jb_framelist_t *framelist) 252 252 { 253 253 return framelist->size; … … 255 255 256 256 257 static unsigned jb_framelist_eff_size(const jb_framelist_t *framelist) 257 static unsigned jb_framelist_eff_size(const jb_framelist_t *framelist) 258 258 { 259 259 return (framelist->size - framelist->discarded_num); 260 260 } 261 261 262 static int jb_framelist_origin(const jb_framelist_t *framelist) 262 static int jb_framelist_origin(const jb_framelist_t *framelist) 263 263 { 264 264 return framelist->origin; … … 271 271 pj_uint32_t *bit_info, 272 272 pj_uint32_t *ts, 273 int *seq) 273 int *seq) 274 274 { 275 275 if (framelist->size) { … … 296 296 *bit_info = 0; 297 297 } else { 298 pj_memcpy(frame, 299 framelist->content + 298 pj_memcpy(frame, 299 framelist->content + 300 300 framelist->head * framelist->frame_size, 301 301 framelist->frame_size); 302 *p_type = (pjmedia_jb_frame_type) 302 *p_type = (pjmedia_jb_frame_type) 303 303 framelist->frame_type[framelist->head]; 304 304 if (size) … … 312 312 *seq = framelist->origin; 313 313 314 //pj_bzero(framelist->content + 314 //pj_bzero(framelist->content + 315 315 // framelist->head * framelist->frame_size, 316 316 // framelist->frame_size); … … 323 323 framelist->head = (framelist->head + 1) % framelist->max_count; 324 324 framelist->size--; 325 325 326 326 return PJ_TRUE; 327 327 } … … 342 342 pj_uint32_t *bit_info, 343 343 pj_uint32_t *ts, 344 int *seq) 344 int *seq) 345 345 { 346 346 unsigned pos, idx; … … 367 367 *frame = framelist->content + pos*framelist->frame_size; 368 368 if (type) 369 *type = (pjmedia_jb_frame_type) 369 *type = (pjmedia_jb_frame_type) 370 370 framelist->frame_type[pos]; 371 371 if (size) … … 384 384 /* Remove oldest frames as many as param 'count' */ 385 385 static unsigned jb_framelist_remove_head(jb_framelist_t *framelist, 386 unsigned count) 387 { 388 if (count > framelist->size) 386 unsigned count) 387 { 388 if (count > framelist->size) 389 389 count = framelist->size; 390 390 … … 410 410 } 411 411 412 //pj_bzero(framelist->content + 412 //pj_bzero(framelist->content + 413 413 // framelist->head * framelist->frame_size, 414 414 // step1*framelist->frame_size); … … 440 440 framelist->size -= count; 441 441 } 442 442 443 443 return count; 444 444 } … … 549 549 550 550 551 PJ_DEF(pj_status_t) pjmedia_jbuf_create(pj_pool_t *pool, 551 PJ_DEF(pj_status_t) pjmedia_jbuf_create(pj_pool_t *pool, 552 552 const pj_str_t *name, 553 unsigned frame_size, 553 unsigned frame_size, 554 554 unsigned ptime, 555 555 unsigned max_count, … … 597 597 PJ_ASSERT_RETURN(prefetch <= jb->jb_max_count, PJ_EINVAL); 598 598 599 jb->jb_min_prefetch = jb->jb_max_prefetch = 599 jb->jb_min_prefetch = jb->jb_max_prefetch = 600 600 jb->jb_prefetch = jb->jb_init_prefetch = prefetch; 601 601 … … 676 676 " burst (min/max/avg/dev)=%d/%d/%d/%d frames\n" 677 677 " lost=%d discard=%d empty=%d", 678 jb_framelist_size(&jb->jb_framelist), 679 jb_framelist_eff_size(&jb->jb_framelist), 678 jb_framelist_size(&jb->jb_framelist), 679 jb_framelist_eff_size(&jb->jb_framelist), 680 680 jb->jb_prefetch, jb->jb_eff_level, 681 jb->jb_delay.min, jb->jb_delay.max, jb->jb_delay.mean, 681 jb->jb_delay.min, jb->jb_delay.max, jb->jb_delay.mean, 682 682 pj_math_stat_get_stddev(&jb->jb_delay), 683 jb->jb_burst.min, jb->jb_burst.max, jb->jb_burst.mean, 683 jb->jb_burst.min, jb->jb_burst.max, jb->jb_burst.mean, 684 684 pj_math_stat_get_stddev(&jb->jb_burst), 685 685 jb->jb_lost, jb->jb_discard, jb->jb_empty)); … … 705 705 706 706 enum { STABLE_HISTORY_LIMIT = 20 }; 707 707 708 708 jb->jb_stable_hist++; 709 710 /* Only update the effective level (and prefetch) if 'stable' 709 710 /* Only update the effective level (and prefetch) if 'stable' 711 711 * condition is reached (not just short time impulse) 712 712 */ 713 713 if (jb->jb_stable_hist > STABLE_HISTORY_LIMIT) { 714 714 715 715 diff = (jb->jb_eff_level - jb->jb_max_hist_level) / 3; 716 716 … … 724 724 if (jb->jb_init_prefetch) { 725 725 jb->jb_prefetch = jb->jb_eff_level; 726 if (jb->jb_prefetch < jb->jb_min_prefetch) 726 if (jb->jb_prefetch < jb->jb_min_prefetch) 727 727 jb->jb_prefetch = jb->jb_min_prefetch; 728 728 if (jb->jb_prefetch > jb->jb_max_prefetch) … … 736 736 TRACE__((jb->jb_name.ptr,"jb updated(1), lvl=%d pre=%d, size=%d", 737 737 jb->jb_eff_level, jb->jb_prefetch, cur_size)); 738 PJ_UNUSED_ARG(cur_size); /* Warning about unused var */ 738 739 } 739 740 } … … 759 760 //jb->jb_max_hist_level = 0; 760 761 761 TRACE__((jb->jb_name.ptr,"jb updated(2), lvl=%d pre=%d, size=%d", 762 TRACE__((jb->jb_name.ptr,"jb updated(2), lvl=%d pre=%d, size=%d", 762 763 jb->jb_eff_level, jb->jb_prefetch, cur_size)); 763 764 } … … 778 779 * 779 780 * Moreover, normally drift level is quite low, so JB shouldn't need 780 * to shrink aggresively, it will shrink maximum one frame per 781 * PJMEDIA_JBUF_DISC_MIN_GAP ms. Theoritically, JB may handle drift level 781 * to shrink aggresively, it will shrink maximum one frame per 782 * PJMEDIA_JBUF_DISC_MIN_GAP ms. Theoritically, JB may handle drift level 782 783 * as much as = FRAME_PTIME/PJMEDIA_JBUF_DISC_MIN_GAP * 100% 783 784 * 784 * Whenever there is drift, where PUT > GET, this method will keep 785 * Whenever there is drift, where PUT > GET, this method will keep 785 786 * the latency (JB size) as much as twice of burst level. 786 787 */ … … 797 798 int seq_origin; 798 799 799 /* Check and adjust jb_discard_ref, in case there was 800 * seq restart 800 /* Check and adjust jb_discard_ref, in case there was 801 * seq restart 801 802 */ 802 803 seq_origin = jb_framelist_origin(&jb->jb_framelist); … … 814 815 jb->jb_discard += diff; 815 816 816 TRACE__((jb->jb_name.ptr, 817 TRACE__((jb->jb_name.ptr, 817 818 "JB shrinking %d frame(s), cur size=%d", diff, 818 819 jb_framelist_eff_size(&jb->jb_framelist))); … … 846 847 T = PJMEDIA_JBUF_PRO_DISC_T2; 847 848 else 848 T = PJMEDIA_JBUF_PRO_DISC_T1 + 849 T = PJMEDIA_JBUF_PRO_DISC_T1 + 849 850 (PJMEDIA_JBUF_PRO_DISC_T2 - PJMEDIA_JBUF_PRO_DISC_T1) * 850 851 (burst_level - PJMEDIA_JBUF_PRO_DISC_MIN_BURST) / … … 874 875 if (last_seq >= (jb->jb_discard_ref + (int)jb->jb_discard_dist)) { 875 876 int discard_seq; 876 877 877 878 discard_seq = jb->jb_discard_ref + jb->jb_discard_dist; 878 879 if (discard_seq < jb_framelist_origin(&jb->jb_framelist)) … … 881 882 jb_framelist_discard(&jb->jb_framelist, discard_seq); 882 883 883 TRACE__((jb->jb_name.ptr, 884 TRACE__((jb->jb_name.ptr, 884 885 "Discard #%d: ref=#%d dist=%d orig=%d size=%d/%d " 885 886 "burst=%d/%d", … … 897 898 } 898 899 } 899 900 900 901 901 902 PJ_INLINE(void) jbuf_update(pjmedia_jbuf *jb, int oper) … … 905 906 906 907 if (jb->jb_status == JB_STATUS_INITIALIZING) { 907 /* Switch status 'initializing' -> 'processing' after some OP 908 * switch cycles and current OP is GET (burst level is calculated 908 /* Switch status 'initializing' -> 'processing' after some OP 909 * switch cycles and current OP is GET (burst level is calculated 909 910 * based on PUT burst), so burst calculation is guaranted to be 910 911 * performed right after the status switching. … … 922 923 } 923 924 924 /* Perform jitter calculation based on PUT burst-level only, since 925 /* Perform jitter calculation based on PUT burst-level only, since 925 926 * GET burst-level may not be accurate, e.g: when VAD is active. 926 927 * Note that when burst-level is too big, i.e: exceeds jb_max_burst, 927 * the GET op may be idle, in this case, we better skip the jitter 928 * the GET op may be idle, in this case, we better skip the jitter 928 929 * calculation. 929 930 */ … … 940 941 } 941 942 942 PJ_DEF(void) pjmedia_jbuf_put_frame( pjmedia_jbuf *jb, 943 const void *frame, 944 pj_size_t frame_size, 943 PJ_DEF(void) pjmedia_jbuf_put_frame( pjmedia_jbuf *jb, 944 const void *frame, 945 pj_size_t frame_size, 945 946 int frame_seq) 946 947 { … … 948 949 } 949 950 950 PJ_DEF(void) pjmedia_jbuf_put_frame2(pjmedia_jbuf *jb, 951 const void *frame, 952 pj_size_t frame_size, 951 PJ_DEF(void) pjmedia_jbuf_put_frame2(pjmedia_jbuf *jb, 952 const void *frame, 953 pj_size_t frame_size, 953 954 pj_uint32_t bit_info, 954 955 int frame_seq, 955 956 pj_bool_t *discarded) 956 957 { 957 pjmedia_jbuf_put_frame3(jb, frame, frame_size, bit_info, frame_seq, 0, 958 pjmedia_jbuf_put_frame3(jb, frame, frame_size, bit_info, frame_seq, 0, 958 959 discarded); 959 960 } 960 961 961 PJ_DEF(void) pjmedia_jbuf_put_frame3(pjmedia_jbuf *jb, 962 const void *frame, 963 pj_size_t frame_size, 962 PJ_DEF(void) pjmedia_jbuf_put_frame3(pjmedia_jbuf *jb, 963 const void *frame, 964 pj_size_t frame_size, 964 965 pj_uint32_t bit_info, 965 966 int frame_seq, … … 978 979 (unsigned)min_frame_size, bit_info, ts, 979 980 PJMEDIA_JB_NORMAL_FRAME); 980 981 981 982 /* Jitter buffer is full, remove some older frames */ 982 983 while (status == PJ_ETOOMANY) { … … 1010 1011 if (status == PJ_SUCCESS) { 1011 1012 if (jb->jb_prefetching) { 1012 TRACE__((jb->jb_name.ptr, "PUT prefetch_cnt=%d/%d", 1013 TRACE__((jb->jb_name.ptr, "PUT prefetch_cnt=%d/%d", 1013 1014 new_size, jb->jb_prefetch)); 1014 1015 if (new_size >= jb->jb_prefetch) … … 1024 1025 * Get frame from jitter buffer. 1025 1026 */ 1026 PJ_DEF(void) pjmedia_jbuf_get_frame( pjmedia_jbuf *jb, 1027 void *frame, 1027 PJ_DEF(void) pjmedia_jbuf_get_frame( pjmedia_jbuf *jb, 1028 void *frame, 1028 1029 char *p_frame_type) 1029 1030 { … … 1035 1036 * Get frame from jitter buffer. 1036 1037 */ 1037 PJ_DEF(void) pjmedia_jbuf_get_frame2(pjmedia_jbuf *jb, 1038 void *frame, 1038 PJ_DEF(void) pjmedia_jbuf_get_frame2(pjmedia_jbuf *jb, 1039 void *frame, 1039 1040 pj_size_t *size, 1040 1041 char *p_frame_type, … … 1048 1049 * Get frame from jitter buffer. 1049 1050 */ 1050 PJ_DEF(void) pjmedia_jbuf_get_frame3(pjmedia_jbuf *jb, 1051 void *frame, 1051 PJ_DEF(void) pjmedia_jbuf_get_frame3(pjmedia_jbuf *jb, 1052 void *frame, 1052 1053 pj_size_t *size, 1053 1054 char *p_frame_type, … … 1078 1079 1079 1080 /* Try to retrieve a frame from frame list */ 1080 res = jb_framelist_get(&jb->jb_framelist, frame, size, &ftype, 1081 res = jb_framelist_get(&jb->jb_framelist, frame, size, &ftype, 1081 1082 bit_info, ts, seq); 1082 1083 if (res) { … … 1097 1098 /* We've just retrieved one frame, so add one to cur_size */ 1098 1099 cur_size = jb_framelist_eff_size(&jb->jb_framelist) + 1; 1099 pj_math_stat_update(&jb->jb_delay, 1100 pj_math_stat_update(&jb->jb_delay, 1100 1101 cur_size*jb->jb_frame_ptime); 1101 1102 } … … 1129 1130 state->min_prefetch = jb->jb_min_prefetch; 1130 1131 state->max_prefetch = jb->jb_max_prefetch; 1131 1132 1132 1133 state->burst = jb->jb_eff_level; 1133 1134 state->prefetch = jb->jb_prefetch; 1134 1135 state->size = jb_framelist_eff_size(&jb->jb_framelist); 1135 1136 1136 1137 state->avg_delay = jb->jb_delay.mean; 1137 1138 state->min_delay = jb->jb_delay.min; 1138 1139 state->max_delay = jb->jb_delay.max; 1139 1140 state->dev_delay = pj_math_stat_get_stddev(&jb->jb_delay); 1140 1141 1141 1142 state->avg_burst = jb->jb_burst.mean; 1142 1143 state->empty = jb->jb_empty; … … 1150 1151 PJ_DEF(void) pjmedia_jbuf_peek_frame( pjmedia_jbuf *jb, 1151 1152 unsigned offset, 1152 const void **frame, 1153 pj_size_t *size, 1153 const void **frame, 1154 pj_size_t *size, 1154 1155 char *p_frm_type, 1155 1156 pj_uint32_t *bit_info, … … 1171 1172 1172 1173 1173 PJ_DEF(unsigned) pjmedia_jbuf_remove_frame(pjmedia_jbuf *jb, 1174 PJ_DEF(unsigned) pjmedia_jbuf_remove_frame(pjmedia_jbuf *jb, 1174 1175 unsigned frame_cnt) 1175 1176 {
Note: See TracChangeset
for help on using the changeset viewer.