| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#ifdef HAVE_CONFIG_H |
| 36 |
#include "config.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifndef NULL |
| 40 |
#define NULL 0 |
| 41 |
#endif |
| 42 |
|
| 43 |
#include "misc.h" |
| 44 |
#include <speex/speex.h> |
| 45 |
#include <speex/speex_bits.h> |
| 46 |
#include <speex/speex_jitter.h> |
| 47 |
#include <stdio.h> |
| 48 |
|
| 49 |
#define LATE_BINS 4 |
| 50 |
|
| 51 |
void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate) |
| 52 |
{ |
| 53 |
int i; |
| 54 |
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++) |
| 55 |
{ |
| 56 |
jitter->len[i]=-1; |
| 57 |
jitter->timestamp[i]=-1; |
| 58 |
} |
| 59 |
|
| 60 |
jitter->dec = decoder; |
| 61 |
speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &jitter->frame_size); |
| 62 |
jitter->frame_time = 1000*jitter->frame_size / sampling_rate; |
| 63 |
|
| 64 |
speex_bits_init(&jitter->current_packet); |
| 65 |
jitter->valid_bits = 0; |
| 66 |
|
| 67 |
jitter->buffer_size = 4; |
| 68 |
|
| 69 |
jitter->pointer_timestamp = -jitter->frame_time * jitter->buffer_size; |
| 70 |
jitter->reset_state = 1; |
| 71 |
jitter->lost_count = 0; |
| 72 |
jitter->loss_rate = 0; |
| 73 |
} |
| 74 |
|
| 75 |
void speex_jitter_destroy(SpeexJitter *jitter) |
| 76 |
{ |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp) |
| 81 |
{ |
| 82 |
int i,j; |
| 83 |
int arrival_margin; |
| 84 |
|
| 85 |
if (jitter->reset_state) |
| 86 |
{ |
| 87 |
jitter->reset_state=0; |
| 88 |
jitter->pointer_timestamp = timestamp-jitter->frame_time * jitter->buffer_size; |
| 89 |
for (i=0;i<MAX_MARGIN;i++) |
| 90 |
{ |
| 91 |
jitter->shortterm_margin[i] = 0; |
| 92 |
jitter->longterm_margin[i] = 0; |
| 93 |
} |
| 94 |
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++) |
| 95 |
{ |
| 96 |
jitter->len[i]=-1; |
| 97 |
jitter->timestamp[i]=-1; |
| 98 |
} |
| 99 |
fprintf(stderr, "reset to %d\n", timestamp); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++) |
| 104 |
{ |
| 105 |
if (jitter->timestamp[i]<jitter->pointer_timestamp) |
| 106 |
{ |
| 107 |
jitter->len[i]=-1; |
| 108 |
|
| 109 |
|
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++) |
| 115 |
{ |
| 116 |
if (jitter->len[i]==-1) |
| 117 |
break; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
if (i==SPEEX_JITTER_MAX_BUFFER_SIZE) |
| 122 |
{ |
| 123 |
int earliest=jitter->timestamp[0]; |
| 124 |
i=0; |
| 125 |
for (j=1;j<SPEEX_JITTER_MAX_BUFFER_SIZE;j++) |
| 126 |
{ |
| 127 |
if (jitter->timestamp[j]<earliest) |
| 128 |
{ |
| 129 |
earliest = jitter->timestamp[j]; |
| 130 |
i=j; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if (len>SPEEX_JITTER_MAX_PACKET_SIZE) |
| 142 |
len=SPEEX_JITTER_MAX_PACKET_SIZE; |
| 143 |
for (j=0;j<len/BYTES_PER_CHAR;j++) |
| 144 |
jitter->buf[i][j]=packet[j]; |
| 145 |
jitter->timestamp[i]=timestamp; |
| 146 |
jitter->len[i]=len; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
arrival_margin = (timestamp - jitter->pointer_timestamp - jitter->frame_time); |
| 156 |
|
| 157 |
if (arrival_margin >= -LATE_BINS*jitter->frame_time) |
| 158 |
{ |
| 159 |
int int_margin; |
| 160 |
for (i=0;i<MAX_MARGIN;i++) |
| 161 |
{ |
| 162 |
jitter->shortterm_margin[i] *= .98; |
| 163 |
jitter->longterm_margin[i] *= .995; |
| 164 |
} |
| 165 |
int_margin = (arrival_margin + LATE_BINS*jitter->frame_time)/jitter->frame_time; |
| 166 |
if (int_margin>MAX_MARGIN-1) |
| 167 |
int_margin = MAX_MARGIN-1; |
| 168 |
if (int_margin>=0) |
| 169 |
{ |
| 170 |
jitter->shortterm_margin[int_margin] += .02; |
| 171 |
jitter->longterm_margin[int_margin] += .005; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
void speex_jitter_get(SpeexJitter *jitter, short *out, int *current_timestamp) |
| 179 |
{ |
| 180 |
int i; |
| 181 |
int ret; |
| 182 |
float late_ratio_short; |
| 183 |
float late_ratio_long; |
| 184 |
float ontime_ratio_short; |
| 185 |
float ontime_ratio_long; |
| 186 |
float early_ratio_short; |
| 187 |
float early_ratio_long; |
| 188 |
|
| 189 |
late_ratio_short = 0; |
| 190 |
late_ratio_long = 0; |
| 191 |
for (i=0;i<LATE_BINS;i++) |
| 192 |
{ |
| 193 |
late_ratio_short += jitter->shortterm_margin[i]; |
| 194 |
late_ratio_long += jitter->longterm_margin[i]; |
| 195 |
} |
| 196 |
ontime_ratio_short = jitter->shortterm_margin[LATE_BINS]; |
| 197 |
ontime_ratio_long = jitter->longterm_margin[LATE_BINS]; |
| 198 |
early_ratio_short = early_ratio_long = 0; |
| 199 |
for (i=LATE_BINS+1;i<MAX_MARGIN;i++) |
| 200 |
{ |
| 201 |
early_ratio_short += jitter->shortterm_margin[i]; |
| 202 |
early_ratio_long += jitter->longterm_margin[i]; |
| 203 |
} |
| 204 |
if (0&&jitter->pointer_timestamp%1000==0) |
| 205 |
{ |
| 206 |
fprintf (stderr, "%f %f %f %f %f %f\n", early_ratio_short, early_ratio_long, ontime_ratio_short, ontime_ratio_long, late_ratio_short, late_ratio_long); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
if (late_ratio_short > .1 || late_ratio_long > .03) |
| 211 |
{ |
| 212 |
jitter->shortterm_margin[MAX_MARGIN-1] += jitter->shortterm_margin[MAX_MARGIN-2]; |
| 213 |
jitter->longterm_margin[MAX_MARGIN-1] += jitter->longterm_margin[MAX_MARGIN-2]; |
| 214 |
for (i=MAX_MARGIN-2;i>=0;i--) |
| 215 |
{ |
| 216 |
jitter->shortterm_margin[i+1] = jitter->shortterm_margin[i]; |
| 217 |
jitter->longterm_margin[i+1] = jitter->longterm_margin[i]; |
| 218 |
} |
| 219 |
jitter->shortterm_margin[0] = 0; |
| 220 |
jitter->longterm_margin[0] = 0; |
| 221 |
|
| 222 |
speex_decode_int(jitter->dec, NULL, out); |
| 223 |
if (current_timestamp) |
| 224 |
*current_timestamp = jitter->pointer_timestamp; |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
jitter->pointer_timestamp += jitter->frame_time; |
| 230 |
|
| 231 |
if (late_ratio_short + ontime_ratio_short < .005 && late_ratio_long + ontime_ratio_long < .01 && early_ratio_short > .8) |
| 232 |
{ |
| 233 |
jitter->shortterm_margin[0] += jitter->shortterm_margin[1]; |
| 234 |
jitter->longterm_margin[0] += jitter->longterm_margin[1]; |
| 235 |
for (i=1;i<MAX_MARGIN-1;i++) |
| 236 |
{ |
| 237 |
jitter->shortterm_margin[i] = jitter->shortterm_margin[i+1]; |
| 238 |
jitter->longterm_margin[i] = jitter->longterm_margin[i+1]; |
| 239 |
} |
| 240 |
jitter->shortterm_margin[MAX_MARGIN-1] = 0; |
| 241 |
jitter->longterm_margin[MAX_MARGIN-1] = 0; |
| 242 |
|
| 243 |
jitter->pointer_timestamp += jitter->frame_time; |
| 244 |
} |
| 245 |
|
| 246 |
if (current_timestamp) |
| 247 |
*current_timestamp = jitter->pointer_timestamp; |
| 248 |
|
| 249 |
|
| 250 |
if (jitter->pointer_timestamp<0) |
| 251 |
{ |
| 252 |
for (i=0;i<jitter->frame_size;i++) |
| 253 |
out[i]=0; |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++) |
| 259 |
{ |
| 260 |
if (jitter->len[i]!=-1 && jitter->timestamp[i]==jitter->pointer_timestamp) |
| 261 |
break; |
| 262 |
} |
| 263 |
|
| 264 |
if (i==SPEEX_JITTER_MAX_BUFFER_SIZE) |
| 265 |
{ |
| 266 |
|
| 267 |
if (jitter->valid_bits) |
| 268 |
{ |
| 269 |
|
| 270 |
ret = speex_decode_int(jitter->dec, &jitter->current_packet, out); |
| 271 |
if (ret == 0) |
| 272 |
{ |
| 273 |
jitter->lost_count = 0; |
| 274 |
return; |
| 275 |
} else { |
| 276 |
jitter->valid_bits = 0; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
speex_decode_int(jitter->dec, NULL, out); |
| 283 |
jitter->lost_count++; |
| 284 |
if (jitter->lost_count>=25) |
| 285 |
{ |
| 286 |
jitter->lost_count = 0; |
| 287 |
jitter->reset_state = 1; |
| 288 |
speex_decoder_ctl(jitter->dec, SPEEX_RESET_STATE, NULL); |
| 289 |
} |
| 290 |
jitter->loss_rate = .999*jitter->loss_rate + .001; |
| 291 |
} else { |
| 292 |
jitter->lost_count = 0; |
| 293 |
|
| 294 |
speex_bits_read_from(&jitter->current_packet, jitter->buf[i], jitter->len[i]); |
| 295 |
jitter->len[i]=-1; |
| 296 |
|
| 297 |
ret = speex_decode_int(jitter->dec, &jitter->current_packet, out); |
| 298 |
if (ret == 0) |
| 299 |
{ |
| 300 |
jitter->valid_bits = 1; |
| 301 |
} else { |
| 302 |
|
| 303 |
for (i=0;i<jitter->frame_size;i++) |
| 304 |
out[i]=0; |
| 305 |
} |
| 306 |
jitter->loss_rate = .999*jitter->loss_rate; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
int speex_jitter_get_pointer_timestamp(SpeexJitter *jitter) |
| 313 |
{ |
| 314 |
return jitter->pointer_timestamp; |
| 315 |
} |