| 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 |
|
| 36 |
#include "asterisk.h" |
| 37 |
|
| 38 |
#include "asterisk/frame.h" |
| 39 |
#include "asterisk/channel.h" |
| 40 |
#include "asterisk/term.h" |
| 41 |
#include "asterisk/utils.h" |
| 42 |
#include "asterisk/pbx.h" |
| 43 |
#include "asterisk/timing.h" |
| 44 |
#include "asterisk/rtp_engine.h" |
| 45 |
#include "asterisk/format_cache.h" |
| 46 |
|
| 47 |
#include "asterisk/abstract_jb.h" |
| 48 |
#include "fixedjitterbuf.h" |
| 49 |
#include "jitterbuf.h" |
| 50 |
|
| 51 |
|
| 52 |
enum { |
| 53 |
JB_USE = (1 << 0), |
| 54 |
JB_TIMEBASE_INITIALIZED = (1 << 1), |
| 55 |
JB_CREATED = (1 << 2) |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
#define MAXIMUM_EARLY_FRAME_COUNT 200 |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static void *jb_create_fixed(struct ast_jb_conf *general_config); |
| 65 |
static void jb_destroy_fixed(void *jb); |
| 66 |
static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now); |
| 67 |
static int jb_put_fixed(void *jb, struct ast_frame *fin, long now); |
| 68 |
static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl); |
| 69 |
static long jb_next_fixed(void *jb); |
| 70 |
static int jb_remove_fixed(void *jb, struct ast_frame **fout); |
| 71 |
static void jb_force_resynch_fixed(void *jb); |
| 72 |
static void jb_empty_and_reset_fixed(void *jb); |
| 73 |
static int jb_is_late_fixed(void *jb, long ts); |
| 74 |
|
| 75 |
static void * jb_create_adaptive(struct ast_jb_conf *general_config); |
| 76 |
static void jb_destroy_adaptive(void *jb); |
| 77 |
static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now); |
| 78 |
static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now); |
| 79 |
static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl); |
| 80 |
static long jb_next_adaptive(void *jb); |
| 81 |
static int jb_remove_adaptive(void *jb, struct ast_frame **fout); |
| 82 |
static void jb_force_resynch_adaptive(void *jb); |
| 83 |
static void jb_empty_and_reset_adaptive(void *jb); |
| 84 |
static int jb_is_late_adaptive(void *jb, long ts); |
| 85 |
|
| 86 |
|
| 87 |
static const struct ast_jb_impl avail_impl[] = { |
| 88 |
{ |
| 89 |
.name = "fixed", |
| 90 |
.type = AST_JB_FIXED, |
| 91 |
.create = jb_create_fixed, |
| 92 |
.destroy = jb_destroy_fixed, |
| 93 |
.put_first = jb_put_first_fixed, |
| 94 |
.put = jb_put_fixed, |
| 95 |
.get = jb_get_fixed, |
| 96 |
.next = jb_next_fixed, |
| 97 |
.remove = jb_remove_fixed, |
| 98 |
.force_resync = jb_force_resynch_fixed, |
| 99 |
.empty_and_reset = jb_empty_and_reset_fixed, |
| 100 |
.is_late = jb_is_late_fixed, |
| 101 |
}, |
| 102 |
{ |
| 103 |
.name = "adaptive", |
| 104 |
.type = AST_JB_ADAPTIVE, |
| 105 |
.create = jb_create_adaptive, |
| 106 |
.destroy = jb_destroy_adaptive, |
| 107 |
.put_first = jb_put_first_adaptive, |
| 108 |
.put = jb_put_adaptive, |
| 109 |
.get = jb_get_adaptive, |
| 110 |
.next = jb_next_adaptive, |
| 111 |
.remove = jb_remove_adaptive, |
| 112 |
.force_resync = jb_force_resynch_adaptive, |
| 113 |
.empty_and_reset = jb_empty_and_reset_adaptive, |
| 114 |
.is_late = jb_is_late_adaptive, |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
static int default_impl = 0; |
| 119 |
|
| 120 |
|
| 121 |
static const int fixed_to_abstract_code[] = |
| 122 |
{AST_JB_IMPL_OK, AST_JB_IMPL_DROP, AST_JB_IMPL_INTERP, AST_JB_IMPL_NOFRAME}; |
| 123 |
static const int adaptive_to_abstract_code[] = |
| 124 |
{AST_JB_IMPL_OK, AST_JB_IMPL_NOFRAME, AST_JB_IMPL_NOFRAME, AST_JB_IMPL_INTERP, AST_JB_IMPL_DROP, AST_JB_IMPL_OK}; |
| 125 |
|
| 126 |
|
| 127 |
static const char * const jb_get_actions[] = {"Delivered", "Dropped", "Interpolated", "No"}; |
| 128 |
|
| 129 |
|
| 130 |
#define jb_framelog(...) do { \ |
| 131 |
if (jb->logfile) { \ |
| 132 |
fprintf(jb->logfile, __VA_ARGS__); \ |
| 133 |
fflush(jb->logfile); \ |
| 134 |
} \ |
| 135 |
} while (0) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static void jb_choose_impl(struct ast_channel *chan); |
| 140 |
static void jb_get_and_deliver(struct ast_channel *chan); |
| 141 |
static int create_jb(struct ast_channel *chan, struct ast_frame *first_frame); |
| 142 |
static long get_now(struct ast_jb *jb, struct timeval *tv); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static void jb_choose_impl(struct ast_channel *chan) |
| 149 |
{ |
| 150 |
struct ast_jb *jb = ast_channel_jb(chan); |
| 151 |
struct ast_jb_conf *jbconf = &jb->conf; |
| 152 |
const struct ast_jb_impl *test_impl; |
| 153 |
int i, avail_impl_count = ARRAY_LEN(avail_impl); |
| 154 |
|
| 155 |
jb->impl = &avail_impl[default_impl]; |
| 156 |
|
| 157 |
if (ast_strlen_zero(jbconf->impl)) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
for (i = 0; i < avail_impl_count; i++) { |
| 162 |
test_impl = &avail_impl[i]; |
| 163 |
if (!strcasecmp(jbconf->impl, test_impl->name)) { |
| 164 |
jb->impl = test_impl; |
| 165 |
return; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
int ast_jb_do_usecheck(struct ast_channel *c0, struct ast_channel *c1) |
| 171 |
{ |
| 172 |
struct ast_jb *jb0 = ast_channel_jb(c0); |
| 173 |
struct ast_jb *jb1 = ast_channel_jb(c1); |
| 174 |
struct ast_jb_conf *conf0 = &jb0->conf; |
| 175 |
struct ast_jb_conf *conf1 = &jb1->conf; |
| 176 |
int c0_wants_jitter = ast_channel_tech(c0)->properties & AST_CHAN_TP_WANTSJITTER; |
| 177 |
int c0_creates_jitter = ast_channel_tech(c0)->properties & AST_CHAN_TP_CREATESJITTER; |
| 178 |
int c0_jb_enabled = ast_test_flag(conf0, AST_JB_ENABLED); |
| 179 |
int c0_force_jb = ast_test_flag(conf0, AST_JB_FORCED); |
| 180 |
int c0_jb_timebase_initialized = ast_test_flag(jb0, JB_TIMEBASE_INITIALIZED); |
| 181 |
int c0_jb_created = ast_test_flag(jb0, JB_CREATED); |
| 182 |
int c1_wants_jitter = ast_channel_tech(c1)->properties & AST_CHAN_TP_WANTSJITTER; |
| 183 |
int c1_creates_jitter = ast_channel_tech(c1)->properties & AST_CHAN_TP_CREATESJITTER; |
| 184 |
int c1_jb_enabled = ast_test_flag(conf1, AST_JB_ENABLED); |
| 185 |
int c1_force_jb = ast_test_flag(conf1, AST_JB_FORCED); |
| 186 |
int c1_jb_timebase_initialized = ast_test_flag(jb1, JB_TIMEBASE_INITIALIZED); |
| 187 |
int c1_jb_created = ast_test_flag(jb1, JB_CREATED); |
| 188 |
int inuse = 0; |
| 189 |
|
| 190 |
|
| 191 |
if (((!c0_wants_jitter && c1_creates_jitter) || (c0_force_jb && c1_creates_jitter)) && c0_jb_enabled) { |
| 192 |
ast_set_flag(jb0, JB_USE); |
| 193 |
if (!c0_jb_timebase_initialized) { |
| 194 |
if (c1_jb_timebase_initialized) { |
| 195 |
memcpy(&jb0->timebase, &jb1->timebase, sizeof(struct timeval)); |
| 196 |
} else { |
| 197 |
gettimeofday(&jb0->timebase, NULL); |
| 198 |
} |
| 199 |
ast_set_flag(jb0, JB_TIMEBASE_INITIALIZED); |
| 200 |
} |
| 201 |
|
| 202 |
if (!c0_jb_created) { |
| 203 |
jb_choose_impl(c0); |
| 204 |
} |
| 205 |
|
| 206 |
inuse = 1; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if (((!c1_wants_jitter && c0_creates_jitter) || (c1_force_jb && c0_creates_jitter)) && c1_jb_enabled) { |
| 211 |
ast_set_flag(jb1, JB_USE); |
| 212 |
if (!c1_jb_timebase_initialized) { |
| 213 |
if (c0_jb_timebase_initialized) { |
| 214 |
memcpy(&jb1->timebase, &jb0->timebase, sizeof(struct timeval)); |
| 215 |
} else { |
| 216 |
gettimeofday(&jb1->timebase, NULL); |
| 217 |
} |
| 218 |
ast_set_flag(jb1, JB_TIMEBASE_INITIALIZED); |
| 219 |
} |
| 220 |
|
| 221 |
if (!c1_jb_created) { |
| 222 |
jb_choose_impl(c1); |
| 223 |
} |
| 224 |
|
| 225 |
inuse = 1; |
| 226 |
} |
| 227 |
|
| 228 |
return inuse; |
| 229 |
} |
| 230 |
|
| 231 |
int ast_jb_get_when_to_wakeup(struct ast_channel *c0, struct ast_channel *c1, int time_left) |
| 232 |
{ |
| 233 |
struct ast_jb *jb0 = ast_channel_jb(c0); |
| 234 |
struct ast_jb *jb1 = ast_channel_jb(c1); |
| 235 |
int c0_use_jb = ast_test_flag(jb0, JB_USE); |
| 236 |
int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED); |
| 237 |
int c1_use_jb = ast_test_flag(jb1, JB_USE); |
| 238 |
int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED); |
| 239 |
int wait, wait0, wait1; |
| 240 |
struct timeval tv_now; |
| 241 |
|
| 242 |
if (time_left == 0) { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
if (time_left < 0) { |
| 249 |
time_left = INT_MAX; |
| 250 |
} |
| 251 |
|
| 252 |
gettimeofday(&tv_now, NULL); |
| 253 |
|
| 254 |
wait0 = (c0_use_jb && c0_jb_is_created) ? jb0->next - get_now(jb0, &tv_now) : time_left; |
| 255 |
wait1 = (c1_use_jb && c1_jb_is_created) ? jb1->next - get_now(jb1, &tv_now) : time_left; |
| 256 |
|
| 257 |
wait = wait0 < wait1 ? wait0 : wait1; |
| 258 |
wait = wait < time_left ? wait : time_left; |
| 259 |
|
| 260 |
if (wait == INT_MAX) { |
| 261 |
wait = -1; |
| 262 |
} else if (wait < 1) { |
| 263 |
|
| 264 |
wait = 1; |
| 265 |
} |
| 266 |
|
| 267 |
return wait; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
int ast_jb_put(struct ast_channel *chan, struct ast_frame *f) |
| 272 |
{ |
| 273 |
struct ast_jb *jb = ast_channel_jb(chan); |
| 274 |
const struct ast_jb_impl *jbimpl = jb->impl; |
| 275 |
void *jbobj = jb->jbobj; |
| 276 |
struct ast_frame *frr; |
| 277 |
long now = 0; |
| 278 |
|
| 279 |
if (!ast_test_flag(jb, JB_USE)) |
| 280 |
return -1; |
| 281 |
|
| 282 |
if (f->frametype != AST_FRAME_VOICE) { |
| 283 |
if (f->frametype == AST_FRAME_DTMF && ast_test_flag(jb, JB_CREATED)) { |
| 284 |
jb_framelog("JB_PUT {now=%ld}: Received DTMF frame. Force resynching jb...\n", now); |
| 285 |
jbimpl->force_resync(jbobj); |
| 286 |
} |
| 287 |
|
| 288 |
return -1; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
if (!ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO) || f->len < 2 || f->ts < 0) { |
| 293 |
ast_log(LOG_WARNING, "%s received frame with invalid timing info: " |
| 294 |
"has_timing_info=%u, len=%ld, ts=%ld, src=%s\n", |
| 295 |
ast_channel_name(chan), ast_test_flag(f, AST_FRFLAG_HAS_TIMING_INFO), f->len, f->ts, f->src); |
| 296 |
return -1; |
| 297 |
} |
| 298 |
|
| 299 |
frr = ast_frdup(f); |
| 300 |
|
| 301 |
if (!frr) { |
| 302 |
ast_log(LOG_ERROR, "Failed to isolate frame for the jitterbuffer on channel '%s'\n", ast_channel_name(chan)); |
| 303 |
return -1; |
| 304 |
} |
| 305 |
|
| 306 |
if (!ast_test_flag(jb, JB_CREATED)) { |
| 307 |
if (create_jb(chan, frr)) { |
| 308 |
ast_frfree(frr); |
| 309 |
|
| 310 |
ast_clear_flag(jb, JB_USE); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
ast_set_flag(jb, JB_CREATED); |
| 315 |
return 0; |
| 316 |
} else { |
| 317 |
now = get_now(jb, NULL); |
| 318 |
if (jbimpl->put(jbobj, frr, now) != AST_JB_IMPL_OK) { |
| 319 |
jb_framelog("JB_PUT {now=%ld}: Dropped frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len); |
| 320 |
ast_frfree(frr); |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
jb->next = jbimpl->next(jbobj); |
| 328 |
|
| 329 |
jb_framelog("JB_PUT {now=%ld}: Queued frame with ts=%ld and len=%ld\n", now, frr->ts, frr->len); |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
void ast_jb_get_and_deliver(struct ast_channel *c0, struct ast_channel *c1) |
| 337 |
{ |
| 338 |
struct ast_jb *jb0 = ast_channel_jb(c0); |
| 339 |
struct ast_jb *jb1 = ast_channel_jb(c1); |
| 340 |
int c0_use_jb = ast_test_flag(jb0, JB_USE); |
| 341 |
int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED); |
| 342 |
int c1_use_jb = ast_test_flag(jb1, JB_USE); |
| 343 |
int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED); |
| 344 |
|
| 345 |
if (c0_use_jb && c0_jb_is_created) |
| 346 |
jb_get_and_deliver(c0); |
| 347 |
|
| 348 |
if (c1_use_jb && c1_jb_is_created) |
| 349 |
jb_get_and_deliver(c1); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
static void jb_get_and_deliver(struct ast_channel *chan) |
| 354 |
{ |
| 355 |
struct ast_jb *jb = ast_channel_jb(chan); |
| 356 |
const struct ast_jb_impl *jbimpl = jb->impl; |
| 357 |
void *jbobj = jb->jbobj; |
| 358 |
struct ast_frame *f, finterp = { .frametype = AST_FRAME_VOICE, }; |
| 359 |
long now; |
| 360 |
int interpolation_len, res; |
| 361 |
|
| 362 |
now = get_now(jb, NULL); |
| 363 |
jb->next = jbimpl->next(jbobj); |
| 364 |
if (now < jb->next) { |
| 365 |
jb_framelog("\tJB_GET {now=%ld}: now < next=%ld\n", now, jb->next); |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
while (now >= jb->next) { |
| 370 |
interpolation_len = ast_format_get_default_ms(jb->last_format); |
| 371 |
|
| 372 |
res = jbimpl->get(jbobj, &f, now, interpolation_len); |
| 373 |
|
| 374 |
switch (res) { |
| 375 |
case AST_JB_IMPL_OK: |
| 376 |
|
| 377 |
ast_write(chan, f); |
| 378 |
case AST_JB_IMPL_DROP: |
| 379 |
jb_framelog("\tJB_GET {now=%ld}: %s frame with ts=%ld and len=%ld\n", |
| 380 |
now, jb_get_actions[res], f->ts, f->len); |
| 381 |
ao2_replace(jb->last_format, f->subclass.format); |
| 382 |
ast_frfree(f); |
| 383 |
break; |
| 384 |
case AST_JB_IMPL_INTERP: |
| 385 |
|
| 386 |
f = &finterp; |
| 387 |
f->subclass.format = jb->last_format; |
| 388 |
f->samples = interpolation_len * 8; |
| 389 |
f->src = "JB interpolation"; |
| 390 |
f->delivery = ast_tvadd(jb->timebase, ast_samp2tv(jb->next, 1000)); |
| 391 |
f->offset = AST_FRIENDLY_OFFSET; |
| 392 |
|
| 393 |
ast_write(chan, f); |
| 394 |
jb_framelog("\tJB_GET {now=%ld}: Interpolated frame with len=%d\n", now, interpolation_len); |
| 395 |
break; |
| 396 |
case AST_JB_IMPL_NOFRAME: |
| 397 |
ast_log(LOG_WARNING, |
| 398 |
"AST_JB_IMPL_NOFRAME is returned from the %s jb when now=%ld >= next=%ld, jbnext=%ld!\n", |
| 399 |
jbimpl->name, now, jb->next, jbimpl->next(jbobj)); |
| 400 |
jb_framelog("\tJB_GET {now=%ld}: No frame for now!?\n", now); |
| 401 |
return; |
| 402 |
default: |
| 403 |
ast_log(LOG_ERROR, "This should never happen!\n"); |
| 404 |
ast_assert("JB type unknown" == NULL); |
| 405 |
break; |
| 406 |
} |
| 407 |
|
| 408 |
jb->next = jbimpl->next(jbobj); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
static int create_jb(struct ast_channel *chan, struct ast_frame *frr) |
| 414 |
{ |
| 415 |
struct ast_jb *jb = ast_channel_jb(chan); |
| 416 |
struct ast_jb_conf *jbconf = &jb->conf; |
| 417 |
const struct ast_jb_impl *jbimpl = jb->impl; |
| 418 |
void *jbobj; |
| 419 |
long now; |
| 420 |
char logfile_pathname[20 + AST_JB_IMPL_NAME_SIZE + 2*AST_CHANNEL_NAME + 1]; |
| 421 |
char name1[AST_CHANNEL_NAME], name2[AST_CHANNEL_NAME], *tmp; |
| 422 |
int res; |
| 423 |
|
| 424 |
jbobj = jb->jbobj = jbimpl->create(jbconf); |
| 425 |
if (!jbobj) { |
| 426 |
ast_log(LOG_WARNING, "Failed to create jitterbuffer on channel '%s'\n", ast_channel_name(chan)); |
| 427 |
return -1; |
| 428 |
} |
| 429 |
|
| 430 |
now = get_now(jb, NULL); |
| 431 |
res = jbimpl->put_first(jbobj, frr, now); |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
if (res != AST_JB_IMPL_OK) { |
| 436 |
ast_log(LOG_WARNING, "Failed to put first frame in the jitterbuffer on channel '%s'\n", ast_channel_name(chan)); |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
jb->next = jbimpl->next(jbobj); |
| 445 |
|
| 446 |
|
| 447 |
jb->last_format = ao2_bump(frr->subclass.format); |
| 448 |
|
| 449 |
|
| 450 |
if (ast_test_flag(jbconf, AST_JB_LOG)) { |
| 451 |
struct ast_channel *bridged = ast_channel_bridge_peer(chan); |
| 452 |
char safe_logfile[30] = "/tmp/logfile-XXXXXX"; |
| 453 |
int safe_fd; |
| 454 |
|
| 455 |
snprintf(name2, sizeof(name2), "%s", ast_channel_name(chan)); |
| 456 |
while ((tmp = strchr(name2, '/'))) { |
| 457 |
*tmp = '#'; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
ast_assert(bridged != NULL); |
| 462 |
|
| 463 |
snprintf(name1, sizeof(name1), "%s", ast_channel_name(bridged)); |
| 464 |
while ((tmp = strchr(name1, '/'))) { |
| 465 |
*tmp = '#'; |
| 466 |
} |
| 467 |
|
| 468 |
snprintf(logfile_pathname, sizeof(logfile_pathname), |
| 469 |
"/tmp/ast_%s_jb_%s--%s.log", jbimpl->name, name1, name2); |
| 470 |
unlink(logfile_pathname); |
| 471 |
safe_fd = mkstemp(safe_logfile); |
| 472 |
if (safe_fd < 0 || link(safe_logfile, logfile_pathname) || unlink(safe_logfile) || !(jb->logfile = fdopen(safe_fd, "w+b"))) { |
| 473 |
ast_log(LOG_ERROR, "Failed to create frame log file with pathname '%s': %s\n", logfile_pathname, strerror(errno)); |
| 474 |
jb->logfile = NULL; |
| 475 |
if (safe_fd > -1) { |
| 476 |
close(safe_fd); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
if (res == AST_JB_IMPL_OK) { |
| 481 |
jb_framelog("JB_PUT_FIRST {now=%ld}: Queued frame with ts=%ld and len=%ld\n", |
| 482 |
now, frr->ts, frr->len); |
| 483 |
} else { |
| 484 |
jb_framelog("JB_PUT_FIRST {now=%ld}: Dropped frame with ts=%ld and len=%ld\n", |
| 485 |
now, frr->ts, frr->len); |
| 486 |
} |
| 487 |
|
| 488 |
ast_channel_cleanup(bridged); |
| 489 |
} |
| 490 |
|
| 491 |
ast_verb(3, "%s jitterbuffer created on channel %s\n", jbimpl->name, ast_channel_name(chan)); |
| 492 |
|
| 493 |
|
| 494 |
if (res != AST_JB_IMPL_OK) { |
| 495 |
ast_frfree(frr); |
| 496 |
} |
| 497 |
|
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
void ast_jb_destroy(struct ast_channel *chan) |
| 503 |
{ |
| 504 |
struct ast_jb *jb = ast_channel_jb(chan); |
| 505 |
const struct ast_jb_impl *jbimpl = jb->impl; |
| 506 |
void *jbobj = jb->jbobj; |
| 507 |
struct ast_frame *f; |
| 508 |
|
| 509 |
if (jb->logfile) { |
| 510 |
fclose(jb->logfile); |
| 511 |
jb->logfile = NULL; |
| 512 |
} |
| 513 |
|
| 514 |
ao2_cleanup(jb->last_format); |
| 515 |
|
| 516 |
if (ast_test_flag(jb, JB_CREATED)) { |
| 517 |
|
| 518 |
while (jbimpl->remove(jbobj, &f) == AST_JB_IMPL_OK) { |
| 519 |
ast_frfree(f); |
| 520 |
} |
| 521 |
|
| 522 |
jbimpl->destroy(jbobj); |
| 523 |
jb->jbobj = NULL; |
| 524 |
|
| 525 |
ast_clear_flag(jb, JB_CREATED); |
| 526 |
|
| 527 |
ast_verb(3, "%s jitterbuffer destroyed on channel %s\n", jbimpl->name, ast_channel_name(chan)); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
static long get_now(struct ast_jb *jb, struct timeval *when) |
| 533 |
{ |
| 534 |
struct timeval now; |
| 535 |
|
| 536 |
if (!when) { |
| 537 |
when = &now; |
| 538 |
gettimeofday(when, NULL); |
| 539 |
} |
| 540 |
|
| 541 |
return ast_tvdiff_ms(*when, jb->timebase); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
int ast_jb_read_conf(struct ast_jb_conf *conf, const char *varname, const char *value) |
| 546 |
{ |
| 547 |
int prefixlen = sizeof(AST_JB_CONF_PREFIX) - 1; |
| 548 |
const char *name; |
| 549 |
int tmp; |
| 550 |
|
| 551 |
if (strncasecmp(AST_JB_CONF_PREFIX, varname, prefixlen)) { |
| 552 |
return -1; |
| 553 |
} |
| 554 |
|
| 555 |
name = varname + prefixlen; |
| 556 |
|
| 557 |
if (!strcasecmp(name, AST_JB_CONF_ENABLE)) { |
| 558 |
ast_set2_flag(conf, ast_true(value), AST_JB_ENABLED); |
| 559 |
} else if (!strcasecmp(name, AST_JB_CONF_FORCE)) { |
| 560 |
ast_set2_flag(conf, ast_true(value), AST_JB_FORCED); |
| 561 |
} else if (!strcasecmp(name, AST_JB_CONF_MAX_SIZE)) { |
| 562 |
if ((tmp = atoi(value)) > 0) |
| 563 |
conf->max_size = tmp; |
| 564 |
} else if (!strcasecmp(name, AST_JB_CONF_RESYNCH_THRESHOLD)) { |
| 565 |
if ((tmp = atoi(value)) > 0) |
| 566 |
conf->resync_threshold = tmp; |
| 567 |
} else if (!strcasecmp(name, AST_JB_CONF_IMPL)) { |
| 568 |
if (!ast_strlen_zero(value)) |
| 569 |
snprintf(conf->impl, sizeof(conf->impl), "%s", value); |
| 570 |
} else if (!strcasecmp(name, AST_JB_CONF_TARGET_EXTRA)) { |
| 571 |
if (sscanf(value, "%30d", &tmp) == 1) { |
| 572 |
conf->target_extra = tmp; |
| 573 |
} |
| 574 |
} else if (!strcasecmp(name, AST_JB_CONF_LOG)) { |
| 575 |
ast_set2_flag(conf, ast_true(value), AST_JB_LOG); |
| 576 |
} else if (!strcasecmp(name, AST_JB_CONF_SYNC_VIDEO)) { |
| 577 |
ast_set2_flag(conf, ast_true(value), AST_JB_SYNC_VIDEO); |
| 578 |
} else { |
| 579 |
return -1; |
| 580 |
} |
| 581 |
|
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
void ast_jb_enable_for_channel(struct ast_channel *chan) |
| 586 |
{ |
| 587 |
struct ast_jb_conf conf = ast_channel_jb(chan)->conf; |
| 588 |
if (ast_test_flag(&conf, AST_JB_ENABLED)) { |
| 589 |
ast_jb_create_framehook(chan, &conf, 1); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
void ast_jb_configure(struct ast_channel *chan, const struct ast_jb_conf *conf) |
| 594 |
{ |
| 595 |
memcpy(&ast_channel_jb(chan)->conf, conf, sizeof(*conf)); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
void ast_jb_get_config(const struct ast_channel *chan, struct ast_jb_conf *conf) |
| 600 |
{ |
| 601 |
memcpy(conf, &ast_channel_jb((struct ast_channel *) chan)->conf, sizeof(*conf)); |
| 602 |
} |
| 603 |
|
| 604 |
void ast_jb_empty_and_reset(struct ast_channel *c0, struct ast_channel *c1) |
| 605 |
{ |
| 606 |
struct ast_jb *jb0 = ast_channel_jb(c0); |
| 607 |
struct ast_jb *jb1 = ast_channel_jb(c1); |
| 608 |
int c0_use_jb = ast_test_flag(jb0, JB_USE); |
| 609 |
int c0_jb_is_created = ast_test_flag(jb0, JB_CREATED); |
| 610 |
int c1_use_jb = ast_test_flag(jb1, JB_USE); |
| 611 |
int c1_jb_is_created = ast_test_flag(jb1, JB_CREATED); |
| 612 |
|
| 613 |
if (c0_use_jb && c0_jb_is_created && jb0->impl->empty_and_reset) { |
| 614 |
jb0->impl->empty_and_reset(jb0->jbobj); |
| 615 |
} |
| 616 |
|
| 617 |
if (c1_use_jb && c1_jb_is_created && jb1->impl->empty_and_reset) { |
| 618 |
jb1->impl->empty_and_reset(jb1->jbobj); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
static void * jb_create_fixed(struct ast_jb_conf *general_config) |
| 626 |
{ |
| 627 |
struct fixed_jb_conf conf; |
| 628 |
|
| 629 |
conf.jbsize = general_config->max_size; |
| 630 |
conf.resync_threshold = general_config->resync_threshold; |
| 631 |
|
| 632 |
return fixed_jb_new(&conf); |
| 633 |
} |
| 634 |
|
| 635 |
static void jb_destroy_fixed(void *jb) |
| 636 |
{ |
| 637 |
struct fixed_jb *fixedjb = (struct fixed_jb *) jb; |
| 638 |
|
| 639 |
|
| 640 |
jb_empty_and_reset_fixed(jb); |
| 641 |
|
| 642 |
|
| 643 |
fixed_jb_destroy(fixedjb); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
static int jb_put_first_fixed(void *jb, struct ast_frame *fin, long now) |
| 648 |
{ |
| 649 |
struct fixed_jb *fixedjb = (struct fixed_jb *) jb; |
| 650 |
int res; |
| 651 |
|
| 652 |
res = fixed_jb_put_first(fixedjb, fin, fin->len, fin->ts, now); |
| 653 |
|
| 654 |
return fixed_to_abstract_code[res]; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
static int jb_put_fixed(void *jb, struct ast_frame *fin, long now) |
| 659 |
{ |
| 660 |
struct fixed_jb *fixedjb = (struct fixed_jb *) jb; |
| 661 |
int res; |
| 662 |
|
| 663 |
res = fixed_jb_put(fixedjb, fin, fin->len, fin->ts, now); |
| 664 |
|
| 665 |
return fixed_to_abstract_code[res]; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
static int jb_get_fixed(void *jb, struct ast_frame **fout, long now, long interpl) |
| 670 |
{ |
| 671 |
struct fixed_jb *fixedjb = (struct fixed_jb *) jb; |
| 672 |
struct fixed_jb_frame frame = { .data = &ast_null_frame }; |
| 673 |
int res; |
| 674 |
|
| 675 |
res = fixed_jb_get(fixedjb, &frame, now, interpl); |
| 676 |
*fout = frame.data; |
| 677 |
|
| 678 |
return fixed_to_abstract_code[res]; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
static long jb_next_fixed(void *jb) |
| 683 |
{ |
| 684 |
struct fixed_jb *fixedjb = (struct fixed_jb *) jb; |
| 685 |
|
| 686 |
return fixed_jb_next(fixedjb); |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
static int jb_remove_fixed(void *jb, struct ast_frame **fout) |
| 691 |
{ |
| 692 |
struct fixed_jb *fixedjb = (struct fixed_jb *) jb; |
| 693 |
struct fixed_jb_frame frame; |
| 694 |
int res; |
| 695 |
|
| 696 |
res = fixed_jb_remove(fixedjb, &frame); |
| 697 |
*fout = frame.data; |
| 698 |
|
| 699 |
return fixed_to_abstract_code[res]; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
static void jb_force_resynch_fixed(void *jb) |
| 704 |
{ |
| 705 |
struct fixed_jb *fixedjb = (struct fixed_jb *) jb; |
| 706 |
|
| 707 |
fixed_jb_set_force_resynch(fixedjb); |
| 708 |
} |
| 709 |
|
| 710 |
static void jb_empty_and_reset_fixed(void *jb) |
| 711 |
{ |
| 712 |
struct fixed_jb *fixedjb = jb; |
| 713 |
struct fixed_jb_frame f; |
| 714 |
|
| 715 |
while (fixed_jb_remove(fixedjb, &f) == FIXED_JB_OK) { |
| 716 |
ast_frfree(f.data); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
static int jb_is_late_fixed(void *jb, long ts) |
| 721 |
{ |
| 722 |
return fixed_jb_is_late(jb, ts); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
static void *jb_create_adaptive(struct ast_jb_conf *general_config) |
| 728 |
{ |
| 729 |
jb_conf jbconf; |
| 730 |
jitterbuf *adaptivejb; |
| 731 |
|
| 732 |
adaptivejb = jb_new(); |
| 733 |
if (adaptivejb) { |
| 734 |
jbconf.max_jitterbuf = general_config->max_size; |
| 735 |
jbconf.resync_threshold = general_config->resync_threshold; |
| 736 |
jbconf.max_contig_interp = 10; |
| 737 |
jbconf.target_extra = general_config->target_extra; |
| 738 |
jb_setconf(adaptivejb, &jbconf); |
| 739 |
} |
| 740 |
|
| 741 |
return adaptivejb; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
static void jb_destroy_adaptive(void *jb) |
| 746 |
{ |
| 747 |
jitterbuf *adaptivejb = (jitterbuf *) jb; |
| 748 |
|
| 749 |
jb_destroy(adaptivejb); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
static int jb_put_first_adaptive(void *jb, struct ast_frame *fin, long now) |
| 754 |
{ |
| 755 |
return jb_put_adaptive(jb, fin, now); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
static int jb_put_adaptive(void *jb, struct ast_frame *fin, long now) |
| 760 |
{ |
| 761 |
jitterbuf *adaptivejb = (jitterbuf *) jb; |
| 762 |
int res; |
| 763 |
|
| 764 |
res = jb_put(adaptivejb, fin, JB_TYPE_VOICE, fin->len, fin->ts, now); |
| 765 |
|
| 766 |
return adaptive_to_abstract_code[res]; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
static int jb_get_adaptive(void *jb, struct ast_frame **fout, long now, long interpl) |
| 771 |
{ |
| 772 |
jitterbuf *adaptivejb = (jitterbuf *) jb; |
| 773 |
jb_frame frame = { .data = &ast_null_frame }; |
| 774 |
int res; |
| 775 |
|
| 776 |
res = jb_get(adaptivejb, &frame, now, interpl); |
| 777 |
*fout = frame.data; |
| 778 |
|
| 779 |
return adaptive_to_abstract_code[res]; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
static long jb_next_adaptive(void *jb) |
| 784 |
{ |
| 785 |
jitterbuf *adaptivejb = (jitterbuf *) jb; |
| 786 |
|
| 787 |
return jb_next(adaptivejb); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
static int jb_remove_adaptive(void *jb, struct ast_frame **fout) |
| 792 |
{ |
| 793 |
jitterbuf *adaptivejb = (jitterbuf *) jb; |
| 794 |
jb_frame frame; |
| 795 |
int res; |
| 796 |
|
| 797 |
res = jb_getall(adaptivejb, &frame); |
| 798 |
*fout = frame.data; |
| 799 |
|
| 800 |
return adaptive_to_abstract_code[res]; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
static void jb_force_resynch_adaptive(void *jb) |
| 805 |
{ |
| 806 |
} |
| 807 |
|
| 808 |
static void jb_empty_and_reset_adaptive(void *jb) |
| 809 |
{ |
| 810 |
jitterbuf *adaptivejb = jb; |
| 811 |
jb_frame f; |
| 812 |
|
| 813 |
while (jb_getall(adaptivejb, &f) == JB_OK) { |
| 814 |
ast_frfree(f.data); |
| 815 |
} |
| 816 |
|
| 817 |
jb_reset(adaptivejb); |
| 818 |
} |
| 819 |
|
| 820 |
const struct ast_jb_impl *ast_jb_get_impl(enum ast_jb_type type) |
| 821 |
{ |
| 822 |
int i; |
| 823 |
for (i = 0; i < ARRAY_LEN(avail_impl); i++) { |
| 824 |
if (avail_impl[i].type == type) { |
| 825 |
return &avail_impl[i]; |
| 826 |
} |
| 827 |
} |
| 828 |
return NULL; |
| 829 |
} |
| 830 |
|
| 831 |
static int jb_is_late_adaptive(void *jb, long ts) |
| 832 |
{ |
| 833 |
return jb_is_late(jb, ts); |
| 834 |
} |
| 835 |
|
| 836 |
#define DEFAULT_TIMER_INTERVAL 20 |
| 837 |
#define DEFAULT_SIZE 200 |
| 838 |
#define DEFAULT_TARGET_EXTRA 40 |
| 839 |
#define DEFAULT_RESYNC 1000 |
| 840 |
#define DEFAULT_TYPE AST_JB_FIXED |
| 841 |
|
| 842 |
struct jb_stream_sync { |
| 843 |
unsigned int timestamp; |
| 844 |
struct timeval ntp; |
| 845 |
}; |
| 846 |
|
| 847 |
struct jb_framedata { |
| 848 |
const struct ast_jb_impl *jb_impl; |
| 849 |
struct ast_jb_conf jb_conf; |
| 850 |
struct timeval start_tv; |
| 851 |
struct ast_format *last_format; |
| 852 |
struct ast_timer *timer; |
| 853 |
int timer_interval; |
| 854 |
int timer_fd; |
| 855 |
int first; |
| 856 |
int audio_stream_id; |
| 857 |
struct jb_stream_sync audio_stream_sync; |
| 858 |
int video_stream_id; |
| 859 |
struct jb_stream_sync video_stream_sync; |
| 860 |
AST_LIST_HEAD_NOLOCK(, ast_frame) early_frames; |
| 861 |
unsigned int early_frame_count; |
| 862 |
struct timeval last_audio_ntp_timestamp; |
| 863 |
int audio_flowing; |
| 864 |
void *jb_obj; |
| 865 |
}; |
| 866 |
|
| 867 |
static void jb_framedata_destroy(struct jb_framedata *framedata) |
| 868 |
{ |
| 869 |
struct ast_frame *frame; |
| 870 |
|
| 871 |
if (framedata->timer) { |
| 872 |
ast_timer_close(framedata->timer); |
| 873 |
framedata->timer = NULL; |
| 874 |
} |
| 875 |
if (framedata->jb_impl && framedata->jb_obj) { |
| 876 |
struct ast_frame *f; |
| 877 |
while (framedata->jb_impl->remove(framedata->jb_obj, &f) == AST_JB_IMPL_OK) { |
| 878 |
ast_frfree(f); |
| 879 |
} |
| 880 |
framedata->jb_impl->destroy(framedata->jb_obj); |
| 881 |
framedata->jb_obj = NULL; |
| 882 |
} |
| 883 |
ao2_cleanup(framedata->last_format); |
| 884 |
while ((frame = AST_LIST_REMOVE_HEAD(&framedata->early_frames, frame_list))) { |
| 885 |
ast_frfree(frame); |
| 886 |
} |
| 887 |
ast_free(framedata); |
| 888 |
} |
| 889 |
|
| 890 |
void ast_jb_conf_default(struct ast_jb_conf *conf) |
| 891 |
{ |
| 892 |
ast_clear_flag(conf, AST_FLAGS_ALL); |
| 893 |
conf->max_size = DEFAULT_SIZE; |
| 894 |
conf->resync_threshold = DEFAULT_RESYNC; |
| 895 |
ast_copy_string(conf->impl, "fixed", sizeof(conf->impl)); |
| 896 |
conf->target_extra = DEFAULT_TARGET_EXTRA; |
| 897 |
} |
| 898 |
|
| 899 |
static void datastore_destroy_cb(void *data) { |
| 900 |
ast_free(data); |
| 901 |
ast_debug(1, "JITTERBUFFER datastore destroyed\n"); |
| 902 |
} |
| 903 |
|
| 904 |
static const struct ast_datastore_info jb_datastore = { |
| 905 |
.type = "jitterbuffer", |
| 906 |
.destroy = datastore_destroy_cb |
| 907 |
}; |
| 908 |
|
| 909 |
static void hook_destroy_cb(void *framedata) |
| 910 |
{ |
| 911 |
ast_debug(1, "JITTERBUFFER hook destroyed\n"); |
| 912 |
jb_framedata_destroy((struct jb_framedata *) framedata); |
| 913 |
} |
| 914 |
|
| 915 |
static struct timeval jitterbuffer_frame_get_ntp_timestamp(const struct jb_stream_sync *stream_sync, const struct ast_frame *frame) |
| 916 |
{ |
| 917 |
int timestamp_diff; |
| 918 |
unsigned int rate; |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
if (ast_tvzero(stream_sync->ntp)) { |
| 925 |
return ast_tv(0, 0); |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
if (frame->frametype == AST_FRAME_VOICE) { |
| 933 |
rate = ast_rtp_get_rate(frame->subclass.format); |
| 934 |
timestamp_diff = (frame->ts * (rate / 1000)) - stream_sync->timestamp; |
| 935 |
} else { |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
rate = 90000; |
| 940 |
timestamp_diff = frame->ts - stream_sync->timestamp; |
| 941 |
} |
| 942 |
|
| 943 |
if (timestamp_diff < 0) { |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
return ast_tvsub(stream_sync->ntp, ast_samp2tv(abs(timestamp_diff), rate)); |
| 948 |
} else { |
| 949 |
return ast_tvadd(stream_sync->ntp, ast_samp2tv(timestamp_diff, rate)); |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
static struct ast_frame *hook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data) |
| 954 |
{ |
| 955 |
struct jb_framedata *framedata = data; |
| 956 |
struct timeval now_tv; |
| 957 |
int64_t relative_frame_start; |
| 958 |
int putframe = 0; |
| 959 |
|
| 960 |
switch (event) { |
| 961 |
case AST_FRAMEHOOK_EVENT_READ: |
| 962 |
break; |
| 963 |
case AST_FRAMEHOOK_EVENT_ATTACHED: |
| 964 |
case AST_FRAMEHOOK_EVENT_DETACHED: |
| 965 |
case AST_FRAMEHOOK_EVENT_WRITE: |
| 966 |
return frame; |
| 967 |
} |
| 968 |
|
| 969 |
if (ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) { |
| 970 |
if (ast_timer_ack(framedata->timer, 1) < 0) { |
| 971 |
ast_log(LOG_ERROR, "Failed to acknowledge timer in jitter buffer\n"); |
| 972 |
return frame; |
| 973 |
} |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
if (!frame || (ast_test_flag(frame, AST_FRFLAG_REQUEUED) && |
| 991 |
framedata->jb_impl->is_late(framedata->jb_obj, frame->ts))) { |
| 992 |
return frame; |
| 993 |
} |
| 994 |
|
| 995 |
if (ast_test_flag(&framedata->jb_conf, AST_JB_SYNC_VIDEO)) { |
| 996 |
if (frame->frametype == AST_FRAME_VOICE) { |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
framedata->audio_stream_id = frame->stream_num; |
| 1001 |
} else if (frame->frametype == AST_FRAME_RTCP && frame->subclass.integer == AST_RTP_RTCP_SR) { |
| 1002 |
struct ast_rtp_rtcp_report *rtcp_report = frame->data.ptr; |
| 1003 |
struct jb_stream_sync *stream_sync = NULL; |
| 1004 |
|
| 1005 |
|
| 1006 |
if (framedata->audio_stream_id == frame->stream_num) { |
| 1007 |
stream_sync = &framedata->audio_stream_sync; |
| 1008 |
} else if (framedata->video_stream_id == frame->stream_num) { |
| 1009 |
stream_sync = &framedata->video_stream_sync; |
| 1010 |
} |
| 1011 |
|
| 1012 |
if (stream_sync) { |
| 1013 |
|
| 1014 |
stream_sync->timestamp = rtcp_report->sender_information.rtp_timestamp; |
| 1015 |
stream_sync->ntp = rtcp_report->sender_information.ntp_timestamp; |
| 1016 |
} |
| 1017 |
} else if (frame->frametype == AST_FRAME_VIDEO) { |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
struct ast_frame *jbframe; |
| 1022 |
|
| 1023 |
framedata->video_stream_id = frame->stream_num; |
| 1024 |
|
| 1025 |
|
| 1026 |
if (!ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) { |
| 1027 |
return frame; |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
if (framedata->audio_flowing) { |
| 1034 |
struct timeval video_timestamp; |
| 1035 |
|
| 1036 |
video_timestamp = jitterbuffer_frame_get_ntp_timestamp(&framedata->video_stream_sync, frame); |
| 1037 |
if (ast_tvdiff_ms(framedata->last_audio_ntp_timestamp, video_timestamp) >= 0) { |
| 1038 |
return frame; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
if (framedata->early_frame_count == MAXIMUM_EARLY_FRAME_COUNT) { |
| 1047 |
jbframe = AST_LIST_REMOVE_HEAD(&framedata->early_frames, frame_list); |
| 1048 |
framedata->early_frame_count--; |
| 1049 |
ast_frfree(jbframe); |
| 1050 |
} |
| 1051 |
|
| 1052 |
jbframe = ast_frisolate(frame); |
| 1053 |
if (!jbframe) { |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
return frame; |
| 1058 |
} |
| 1059 |
|
| 1060 |
AST_LIST_INSERT_TAIL(&framedata->early_frames, jbframe, frame_list); |
| 1061 |
framedata->early_frame_count++; |
| 1062 |
return &ast_null_frame; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
now_tv = ast_tvnow(); |
| 1067 |
relative_frame_start = ast_tvdiff_ms(now_tv, framedata->start_tv); |
| 1068 |
if (relative_frame_start < 0) { |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
return frame; |
| 1077 |
} |
| 1078 |
|
| 1079 |
if (frame->frametype == AST_FRAME_VOICE) { |
| 1080 |
int res; |
| 1081 |
struct ast_frame *jbframe; |
| 1082 |
|
| 1083 |
if (!ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO) || frame->len < 2 || frame->ts < 0) { |
| 1084 |
|
| 1085 |
return frame; |
| 1086 |
} |
| 1087 |
|
| 1088 |
jbframe = ast_frisolate(frame); |
| 1089 |
ao2_replace(framedata->last_format, frame->subclass.format); |
| 1090 |
|
| 1091 |
if (frame->len && (frame->len != framedata->timer_interval)) { |
| 1092 |
framedata->timer_interval = frame->len; |
| 1093 |
ast_timer_set_rate(framedata->timer, 1000 / framedata->timer_interval); |
| 1094 |
} |
| 1095 |
if (!framedata->first) { |
| 1096 |
framedata->first = 1; |
| 1097 |
res = framedata->jb_impl->put_first(framedata->jb_obj, jbframe, relative_frame_start); |
| 1098 |
} else { |
| 1099 |
res = framedata->jb_impl->put(framedata->jb_obj, jbframe, relative_frame_start); |
| 1100 |
} |
| 1101 |
|
| 1102 |
if (res == AST_JB_IMPL_OK) { |
| 1103 |
if (jbframe != frame) { |
| 1104 |
ast_frfree(frame); |
| 1105 |
} |
| 1106 |
frame = &ast_null_frame; |
| 1107 |
} else if (jbframe != frame) { |
| 1108 |
ast_frfree(jbframe); |
| 1109 |
} |
| 1110 |
putframe = 1; |
| 1111 |
} |
| 1112 |
|
| 1113 |
if (frame->frametype == AST_FRAME_NULL) { |
| 1114 |
int res; |
| 1115 |
long next = framedata->jb_impl->next(framedata->jb_obj); |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
if (relative_frame_start < next) { |
| 1127 |
long int diff = next - relative_frame_start; |
| 1128 |
if (!putframe) { |
| 1129 |
return frame; |
| 1130 |
} else if (diff >= framedata->timer_interval) { |
| 1131 |
return frame; |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
ast_frfree(frame); |
| 1136 |
frame = &ast_null_frame; |
| 1137 |
res = framedata->jb_impl->get(framedata->jb_obj, &frame, relative_frame_start, framedata->timer_interval); |
| 1138 |
switch (res) { |
| 1139 |
case AST_JB_IMPL_OK: |
| 1140 |
|
| 1141 |
break; |
| 1142 |
case AST_JB_IMPL_DROP: |
| 1143 |
ast_frfree(frame); |
| 1144 |
frame = &ast_null_frame; |
| 1145 |
break; |
| 1146 |
case AST_JB_IMPL_INTERP: |
| 1147 |
if (framedata->last_format) { |
| 1148 |
struct ast_frame tmp = { 0, }; |
| 1149 |
|
| 1150 |
tmp.frametype = AST_FRAME_VOICE; |
| 1151 |
tmp.subclass.format = framedata->last_format; |
| 1152 |
|
| 1153 |
tmp.samples = ast_format_get_sample_rate(framedata->last_format) / (1000 / framedata->timer_interval); |
| 1154 |
tmp.delivery = ast_tvadd(framedata->start_tv, ast_samp2tv(next, 1000)); |
| 1155 |
tmp.offset = AST_FRIENDLY_OFFSET; |
| 1156 |
tmp.src = "func_jitterbuffer interpolation"; |
| 1157 |
ast_frfree(frame); |
| 1158 |
frame = ast_frdup(&tmp); |
| 1159 |
break; |
| 1160 |
} |
| 1161 |
|
| 1162 |
case AST_JB_IMPL_NOFRAME: |
| 1163 |
ast_frfree(frame); |
| 1164 |
frame = &ast_null_frame; |
| 1165 |
break; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
if (frame->frametype == AST_FRAME_CONTROL) { |
| 1170 |
struct ast_frame *early_frame; |
| 1171 |
|
| 1172 |
switch(frame->subclass.integer) { |
| 1173 |
case AST_CONTROL_HOLD: |
| 1174 |
case AST_CONTROL_UNHOLD: |
| 1175 |
case AST_CONTROL_T38_PARAMETERS: |
| 1176 |
case AST_CONTROL_SRCUPDATE: |
| 1177 |
case AST_CONTROL_SRCCHANGE: |
| 1178 |
framedata->jb_impl->force_resync(framedata->jb_obj); |
| 1179 |
|
| 1180 |
while ((early_frame = AST_LIST_REMOVE_HEAD(&framedata->early_frames, frame_list))) { |
| 1181 |
ast_frfree(early_frame); |
| 1182 |
} |
| 1183 |
framedata->audio_flowing = 0; |
| 1184 |
framedata->early_frame_count = 0; |
| 1185 |
break; |
| 1186 |
default: |
| 1187 |
break; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
if (ast_test_flag(&framedata->jb_conf, AST_JB_SYNC_VIDEO) && frame->frametype == AST_FRAME_VOICE) { |
| 1193 |
AST_LIST_HEAD_NOLOCK(, ast_frame) additional_frames; |
| 1194 |
struct ast_frame *early_frame; |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
framedata->last_audio_ntp_timestamp = jitterbuffer_frame_get_ntp_timestamp(&framedata->audio_stream_sync, frame); |
| 1200 |
framedata->audio_flowing = 1; |
| 1201 |
|
| 1202 |
AST_LIST_HEAD_INIT_NOLOCK(&additional_frames); |
| 1203 |
|
| 1204 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&framedata->early_frames, early_frame, frame_list) { |
| 1205 |
struct timeval early_timestamp = jitterbuffer_frame_get_ntp_timestamp(&framedata->video_stream_sync, early_frame); |
| 1206 |
int diff = ast_tvdiff_ms(framedata->last_audio_ntp_timestamp, early_timestamp); |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
if (diff >= 0) { |
| 1212 |
AST_LIST_REMOVE_CURRENT(frame_list); |
| 1213 |
framedata->early_frame_count--; |
| 1214 |
AST_LIST_INSERT_TAIL(&additional_frames, early_frame, frame_list); |
| 1215 |
} |
| 1216 |
} |
| 1217 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1218 |
|
| 1219 |
|
| 1220 |
AST_LIST_NEXT(frame, frame_list) = AST_LIST_FIRST(&additional_frames); |
| 1221 |
} |
| 1222 |
|
| 1223 |
return frame; |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
static int jb_framedata_init(struct jb_framedata *framedata, struct ast_jb_conf *jb_conf) |
| 1228 |
{ |
| 1229 |
int jb_impl_type = DEFAULT_TYPE; |
| 1230 |
|
| 1231 |
framedata->timer_fd = -1; |
| 1232 |
memcpy(&framedata->jb_conf, jb_conf, sizeof(*jb_conf)); |
| 1233 |
|
| 1234 |
|
| 1235 |
if (!ast_strlen_zero(jb_conf->impl)) { |
| 1236 |
if (!strcasecmp(jb_conf->impl, "fixed")) { |
| 1237 |
jb_impl_type = AST_JB_FIXED; |
| 1238 |
} else if (!strcasecmp(jb_conf->impl, "adaptive")) { |
| 1239 |
jb_impl_type = AST_JB_ADAPTIVE; |
| 1240 |
} else { |
| 1241 |
ast_log(LOG_WARNING, "Unknown Jitterbuffer type %s. Failed to create jitterbuffer.\n", jb_conf->impl); |
| 1242 |
return -1; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
if (!(framedata->jb_impl = ast_jb_get_impl(jb_impl_type))) { |
| 1247 |
return -1; |
| 1248 |
} |
| 1249 |
|
| 1250 |
if (!(framedata->timer = ast_timer_open())) { |
| 1251 |
return -1; |
| 1252 |
} |
| 1253 |
|
| 1254 |
framedata->audio_stream_id = -1; |
| 1255 |
framedata->video_stream_id = -1; |
| 1256 |
AST_LIST_HEAD_INIT_NOLOCK(&framedata->early_frames); |
| 1257 |
framedata->timer_fd = ast_timer_fd(framedata->timer); |
| 1258 |
framedata->timer_interval = DEFAULT_TIMER_INTERVAL; |
| 1259 |
ast_timer_set_rate(framedata->timer, 1000 / framedata->timer_interval); |
| 1260 |
framedata->start_tv = ast_tvnow(); |
| 1261 |
|
| 1262 |
framedata->jb_obj = framedata->jb_impl->create(&framedata->jb_conf); |
| 1263 |
return 0; |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
void ast_jb_create_framehook(struct ast_channel *chan, struct ast_jb_conf *jb_conf, int prefer_existing) |
| 1268 |
{ |
| 1269 |
struct jb_framedata *framedata; |
| 1270 |
struct ast_datastore *datastore = NULL; |
| 1271 |
struct ast_framehook_interface interface = { |
| 1272 |
.version = AST_FRAMEHOOK_INTERFACE_VERSION, |
| 1273 |
.event_cb = hook_event_cb, |
| 1274 |
.destroy_cb = hook_destroy_cb, |
| 1275 |
}; |
| 1276 |
int i = 0; |
| 1277 |
|
| 1278 |
|
| 1279 |
if (!strcasecmp(jb_conf->impl, "disabled")) { |
| 1280 |
int *id; |
| 1281 |
ast_channel_lock(chan); |
| 1282 |
if ((datastore = ast_channel_datastore_find(chan, &jb_datastore, NULL))) { |
| 1283 |
id = datastore->data; |
| 1284 |
ast_framehook_detach(chan, *id); |
| 1285 |
ast_channel_datastore_remove(chan, datastore); |
| 1286 |
ast_datastore_free(datastore); |
| 1287 |
ast_channel_set_fd(chan, AST_JITTERBUFFER_FD, -1); |
| 1288 |
} |
| 1289 |
ast_channel_unlock(chan); |
| 1290 |
return; |
| 1291 |
} |
| 1292 |
|
| 1293 |
if (!(framedata = ast_calloc(1, sizeof(*framedata)))) { |
| 1294 |
return; |
| 1295 |
} |
| 1296 |
|
| 1297 |
if (jb_framedata_init(framedata, jb_conf)) { |
| 1298 |
jb_framedata_destroy(framedata); |
| 1299 |
return; |
| 1300 |
} |
| 1301 |
|
| 1302 |
interface.data = framedata; |
| 1303 |
|
| 1304 |
ast_channel_lock(chan); |
| 1305 |
i = ast_framehook_attach(chan, &interface); |
| 1306 |
if (i >= 0) { |
| 1307 |
int *id; |
| 1308 |
if ((datastore = ast_channel_datastore_find(chan, &jb_datastore, NULL))) { |
| 1309 |
|
| 1310 |
if (prefer_existing) { |
| 1311 |
|
| 1312 |
ast_framehook_detach(chan, i); |
| 1313 |
ast_channel_unlock(chan); |
| 1314 |
return; |
| 1315 |
} |
| 1316 |
|
| 1317 |
id = datastore->data; |
| 1318 |
ast_framehook_detach(chan, *id); |
| 1319 |
ast_channel_datastore_remove(chan, datastore); |
| 1320 |
ast_datastore_free(datastore); |
| 1321 |
} |
| 1322 |
|
| 1323 |
if (!(datastore = ast_datastore_alloc(&jb_datastore, NULL))) { |
| 1324 |
ast_framehook_detach(chan, i); |
| 1325 |
ast_channel_unlock(chan); |
| 1326 |
return; |
| 1327 |
} |
| 1328 |
|
| 1329 |
if (!(id = ast_calloc(1, sizeof(int)))) { |
| 1330 |
ast_datastore_free(datastore); |
| 1331 |
ast_framehook_detach(chan, i); |
| 1332 |
ast_channel_unlock(chan); |
| 1333 |
return; |
| 1334 |
} |
| 1335 |
|
| 1336 |
*id = i; |
| 1337 |
datastore->data = id; |
| 1338 |
ast_channel_datastore_add(chan, datastore); |
| 1339 |
|
| 1340 |
ast_channel_set_fd(chan, AST_JITTERBUFFER_FD, framedata->timer_fd); |
| 1341 |
} else { |
| 1342 |
jb_framedata_destroy(framedata); |
| 1343 |
framedata = NULL; |
| 1344 |
} |
| 1345 |
ast_channel_unlock(chan); |
| 1346 |
} |