| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include <signal.h> |
| 33 |
|
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/utils.h" |
| 36 |
#include "asterisk/lock.h" |
| 37 |
#include "asterisk/audiohook.h" |
| 38 |
#include "asterisk/slinfactory.h" |
| 39 |
#include "asterisk/frame.h" |
| 40 |
#include "asterisk/translate.h" |
| 41 |
#include "asterisk/format_cache.h" |
| 42 |
#include "asterisk/test.h" |
| 43 |
|
| 44 |
#define AST_AUDIOHOOK_SYNC_TOLERANCE 100 |
| 45 |
#define AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE 100 |
| 46 |
#define AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE 500 |
| 47 |
|
| 48 |
#define DEFAULT_INTERNAL_SAMPLE_RATE 8000 |
| 49 |
|
| 50 |
struct ast_audiohook_translate { |
| 51 |
struct ast_trans_pvt *trans_pvt; |
| 52 |
struct ast_format *format; |
| 53 |
}; |
| 54 |
|
| 55 |
struct ast_audiohook_list { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int native_slin_compatible; |
| 61 |
int list_internal_samp_rate; |
| 62 |
|
| 63 |
struct ast_audiohook_translate in_translate[2]; |
| 64 |
struct ast_audiohook_translate out_translate[2]; |
| 65 |
AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list; |
| 66 |
AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list; |
| 67 |
AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list; |
| 68 |
}; |
| 69 |
|
| 70 |
static int audiohook_set_internal_rate(struct ast_audiohook *audiohook, int rate, int reset) |
| 71 |
{ |
| 72 |
struct ast_format *slin; |
| 73 |
|
| 74 |
if (audiohook->hook_internal_samp_rate == rate) { |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
audiohook->hook_internal_samp_rate = rate; |
| 79 |
|
| 80 |
slin = ast_format_cache_get_slin_by_rate(rate); |
| 81 |
|
| 82 |
|
| 83 |
switch (audiohook->type) { |
| 84 |
case AST_AUDIOHOOK_TYPE_SPY: |
| 85 |
case AST_AUDIOHOOK_TYPE_WHISPER: |
| 86 |
if (reset) { |
| 87 |
ast_slinfactory_destroy(&audiohook->read_factory); |
| 88 |
ast_slinfactory_destroy(&audiohook->write_factory); |
| 89 |
} |
| 90 |
ast_slinfactory_init_with_format(&audiohook->read_factory, slin); |
| 91 |
ast_slinfactory_init_with_format(&audiohook->write_factory, slin); |
| 92 |
break; |
| 93 |
default: |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags init_flags) |
| 101 |
{ |
| 102 |
|
| 103 |
audiohook->type = type; |
| 104 |
audiohook->source = source; |
| 105 |
|
| 106 |
|
| 107 |
ast_mutex_init(&audiohook->lock); |
| 108 |
ast_cond_init(&audiohook->trigger, NULL); |
| 109 |
|
| 110 |
audiohook->init_flags = init_flags; |
| 111 |
|
| 112 |
|
| 113 |
audiohook->direction = AST_AUDIOHOOK_DIRECTION_BOTH; |
| 114 |
|
| 115 |
|
| 116 |
audiohook_set_internal_rate(audiohook, DEFAULT_INTERNAL_SAMPLE_RATE, 0); |
| 117 |
|
| 118 |
|
| 119 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_NEW); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
int ast_audiohook_destroy(struct ast_audiohook *audiohook) |
| 125 |
{ |
| 126 |
|
| 127 |
switch (audiohook->type) { |
| 128 |
case AST_AUDIOHOOK_TYPE_SPY: |
| 129 |
case AST_AUDIOHOOK_TYPE_WHISPER: |
| 130 |
ast_slinfactory_destroy(&audiohook->read_factory); |
| 131 |
ast_slinfactory_destroy(&audiohook->write_factory); |
| 132 |
break; |
| 133 |
default: |
| 134 |
break; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
if (audiohook->trans_pvt) |
| 139 |
ast_translator_free_path(audiohook->trans_pvt); |
| 140 |
|
| 141 |
ao2_cleanup(audiohook->format); |
| 142 |
|
| 143 |
|
| 144 |
ast_cond_destroy(&audiohook->trigger); |
| 145 |
ast_mutex_destroy(&audiohook->lock); |
| 146 |
|
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 150 |
int ast_audiohook_set_frame_feed_direction(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction) |
| 151 |
{ |
| 152 |
|
| 153 |
if (audiohook->status != AST_AUDIOHOOK_STATUS_NEW) { |
| 154 |
ast_debug(3, "Can not set direction on attached Audiohook %p\n", audiohook); |
| 155 |
return -1; |
| 156 |
} |
| 157 |
|
| 158 |
audiohook->direction = direction; |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
#define SHOULD_MUTE(hook, dir) \ |
| 163 |
((ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ) && (dir == AST_AUDIOHOOK_DIRECTION_READ)) || \ |
| 164 |
(ast_test_flag(hook, AST_AUDIOHOOK_MUTE_WRITE) && (dir == AST_AUDIOHOOK_DIRECTION_WRITE)) || \ |
| 165 |
(ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE) == (AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE))) |
| 166 |
|
| 167 |
int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame) |
| 168 |
{ |
| 169 |
struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory); |
| 170 |
struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory); |
| 171 |
struct timeval *rwtime = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *rwtime; |
| 172 |
int our_factory_samples; |
| 173 |
int our_factory_ms; |
| 174 |
int other_factory_samples; |
| 175 |
int other_factory_ms; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if (audiohook->direction != AST_AUDIOHOOK_DIRECTION_BOTH && audiohook->direction != direction) { |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
*rwtime = ast_tvnow(); |
| 186 |
|
| 187 |
our_factory_samples = ast_slinfactory_available(factory); |
| 188 |
our_factory_ms = ast_tvdiff_ms(*rwtime, previous_time) + (our_factory_samples / (audiohook->hook_internal_samp_rate / 1000)); |
| 189 |
other_factory_samples = ast_slinfactory_available(other_factory); |
| 190 |
other_factory_ms = other_factory_samples / (audiohook->hook_internal_samp_rate / 1000); |
| 191 |
|
| 192 |
if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) { |
| 193 |
ast_debug(4, "Flushing audiohook %p so it remains in sync\n", audiohook); |
| 194 |
ast_slinfactory_flush(factory); |
| 195 |
ast_slinfactory_flush(other_factory); |
| 196 |
} |
| 197 |
|
| 198 |
if (ast_test_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE) && ((our_factory_ms > AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE) || (other_factory_ms > AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE))) { |
| 199 |
ast_debug(4, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook); |
| 200 |
ast_slinfactory_flush(factory); |
| 201 |
ast_slinfactory_flush(other_factory); |
| 202 |
} else if ((our_factory_ms > AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE) || (other_factory_ms > AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE)) { |
| 203 |
ast_debug(4, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook); |
| 204 |
ast_slinfactory_flush(factory); |
| 205 |
ast_slinfactory_flush(other_factory); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
ast_slinfactory_feed(factory, frame); |
| 210 |
|
| 211 |
|
| 212 |
if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) { |
| 213 |
ast_cond_signal(&audiohook->trigger); |
| 214 |
} else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) { |
| 215 |
ast_cond_signal(&audiohook->trigger); |
| 216 |
} else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) { |
| 217 |
ast_cond_signal(&audiohook->trigger); |
| 218 |
} |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction) |
| 224 |
{ |
| 225 |
struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory); |
| 226 |
int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume); |
| 227 |
short buf[samples]; |
| 228 |
struct ast_frame frame = { |
| 229 |
.frametype = AST_FRAME_VOICE, |
| 230 |
.subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate), |
| 231 |
.data.ptr = buf, |
| 232 |
.datalen = sizeof(buf), |
| 233 |
.samples = samples, |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
if (samples > ast_slinfactory_available(factory)) { |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
if (!ast_slinfactory_read(factory, buf, samples)) { |
| 243 |
return NULL; |
| 244 |
} |
| 245 |
|
| 246 |
if (SHOULD_MUTE(audiohook, direction)) { |
| 247 |
|
| 248 |
ast_frame_clear(&frame); |
| 249 |
} else if (vol) { |
| 250 |
|
| 251 |
ast_frame_adjust_volume(&frame, vol); |
| 252 |
} |
| 253 |
|
| 254 |
return ast_frdup(&frame); |
| 255 |
} |
| 256 |
|
| 257 |
static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples, struct ast_frame **read_reference, struct ast_frame **write_reference) |
| 258 |
{ |
| 259 |
int count; |
| 260 |
int usable_read; |
| 261 |
int usable_write; |
| 262 |
short adjust_value; |
| 263 |
short buf1[samples]; |
| 264 |
short buf2[samples]; |
| 265 |
short *read_buf = NULL; |
| 266 |
short *write_buf = NULL; |
| 267 |
struct ast_frame frame = { |
| 268 |
.frametype = AST_FRAME_VOICE, |
| 269 |
.datalen = sizeof(buf1), |
| 270 |
.samples = samples, |
| 271 |
}; |
| 272 |
|
| 273 |
|
| 274 |
usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0); |
| 275 |
usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0); |
| 276 |
|
| 277 |
if (!usable_read && !usable_write) { |
| 278 |
|
| 279 |
ast_debug(3, "Read factory %p and write factory %p both fail to provide %zu samples\n", &audiohook->read_factory, &audiohook->write_factory, samples); |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*4) && (ast_slinfactory_available(&audiohook->read_factory) < 4 * samples)) { |
| 322 |
ast_debug(3, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory); |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*4) && (ast_slinfactory_available(&audiohook->write_factory) < 4 * samples)) { |
| 328 |
ast_debug(3, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory); |
| 329 |
return NULL; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
if (usable_read) { |
| 334 |
if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) { |
| 335 |
read_buf = buf1; |
| 336 |
|
| 337 |
if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ))) { |
| 338 |
|
| 339 |
memset(buf1, 0, sizeof(buf1)); |
| 340 |
} else if (audiohook->options.read_volume) { |
| 341 |
|
| 342 |
adjust_value = abs(audiohook->options.read_volume); |
| 343 |
for (count = 0; count < samples; count++) { |
| 344 |
if (audiohook->options.read_volume > 0) { |
| 345 |
ast_slinear_saturated_multiply(&buf1[count], &adjust_value); |
| 346 |
} else if (audiohook->options.read_volume < 0) { |
| 347 |
ast_slinear_saturated_divide(&buf1[count], &adjust_value); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} else { |
| 353 |
ast_debug(1, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
if (usable_write) { |
| 358 |
if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) { |
| 359 |
write_buf = buf2; |
| 360 |
|
| 361 |
if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE))) { |
| 362 |
|
| 363 |
memset(buf2, 0, sizeof(buf2)); |
| 364 |
} else if (audiohook->options.write_volume) { |
| 365 |
|
| 366 |
adjust_value = abs(audiohook->options.write_volume); |
| 367 |
for (count = 0; count < samples; count++) { |
| 368 |
if (audiohook->options.write_volume > 0) { |
| 369 |
ast_slinear_saturated_multiply(&buf2[count], &adjust_value); |
| 370 |
} else if (audiohook->options.write_volume < 0) { |
| 371 |
ast_slinear_saturated_divide(&buf2[count], &adjust_value); |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
} else { |
| 377 |
ast_debug(3, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory); |
| 378 |
} |
| 379 |
|
| 380 |
frame.subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate); |
| 381 |
|
| 382 |
|
| 383 |
if ((ast_test_flag(audiohook, AST_AUDIOHOOK_SUBSTITUTE_SILENCE))) { |
| 384 |
if (read_reference && !read_buf && write_buf) { |
| 385 |
read_buf = buf1; |
| 386 |
memset(buf1, 0, sizeof(buf1)); |
| 387 |
} else if (write_reference && read_buf && !write_buf) { |
| 388 |
write_buf = buf2; |
| 389 |
memset(buf2, 0, sizeof(buf2)); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
if (read_buf && read_reference) { |
| 395 |
frame.data.ptr = read_buf; |
| 396 |
*read_reference = ast_frdup(&frame); |
| 397 |
} |
| 398 |
if (write_buf && write_reference) { |
| 399 |
frame.data.ptr = write_buf; |
| 400 |
*write_reference = ast_frdup(&frame); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
if (read_buf) { |
| 405 |
frame.data.ptr = read_buf; |
| 406 |
if (write_buf) { |
| 407 |
for (count = 0; count < samples; count++) { |
| 408 |
ast_slinear_saturated_add(read_buf++, write_buf++); |
| 409 |
} |
| 410 |
} |
| 411 |
} else if (write_buf) { |
| 412 |
frame.data.ptr = write_buf; |
| 413 |
} else { |
| 414 |
return NULL; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
return ast_frdup(&frame); |
| 419 |
} |
| 420 |
|
| 421 |
static struct ast_frame *audiohook_read_frame_helper(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format, struct ast_frame **read_reference, struct ast_frame **write_reference) |
| 422 |
{ |
| 423 |
struct ast_frame *read_frame = NULL, *final_frame = NULL; |
| 424 |
struct ast_format *slin; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if (!ast_test_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE) || |
| 435 |
(ast_test_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE) && |
| 436 |
ast_format_get_sample_rate(format) > audiohook->hook_internal_samp_rate)) { |
| 437 |
audiohook_set_internal_rate(audiohook, ast_format_get_sample_rate(format), 1); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
if (ast_format_get_sample_rate(format) != audiohook->hook_internal_samp_rate) { |
| 447 |
samples = (audiohook->hook_internal_samp_rate / 1000) * (samples / (ast_format_get_sample_rate(format) / 1000)); |
| 448 |
} |
| 449 |
|
| 450 |
if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? |
| 451 |
audiohook_read_frame_both(audiohook, samples, read_reference, write_reference) : |
| 452 |
audiohook_read_frame_single(audiohook, samples, direction)))) { |
| 453 |
return NULL; |
| 454 |
} |
| 455 |
|
| 456 |
slin = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate); |
| 457 |
|
| 458 |
|
| 459 |
if (ast_format_cmp(format, slin) != AST_FORMAT_CMP_EQUAL) { |
| 460 |
|
| 461 |
if (ast_format_cmp(format, audiohook->format) == AST_FORMAT_CMP_NOT_EQUAL) { |
| 462 |
if (audiohook->trans_pvt) { |
| 463 |
ast_translator_free_path(audiohook->trans_pvt); |
| 464 |
audiohook->trans_pvt = NULL; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
if (!(audiohook->trans_pvt = ast_translator_build_path(format, slin))) { |
| 469 |
ast_frfree(read_frame); |
| 470 |
return NULL; |
| 471 |
} |
| 472 |
ao2_replace(audiohook->format, format); |
| 473 |
} |
| 474 |
|
| 475 |
final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1); |
| 476 |
} else { |
| 477 |
final_frame = read_frame; |
| 478 |
} |
| 479 |
|
| 480 |
return final_frame; |
| 481 |
} |
| 482 |
|
| 483 |
struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format) |
| 484 |
{ |
| 485 |
return audiohook_read_frame_helper(audiohook, samples, direction, format, NULL, NULL); |
| 486 |
} |
| 487 |
|
| 488 |
struct ast_frame *ast_audiohook_read_frame_all(struct ast_audiohook *audiohook, size_t samples, struct ast_format *format, struct ast_frame **read_frame, struct ast_frame **write_frame) |
| 489 |
{ |
| 490 |
return audiohook_read_frame_helper(audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, format, read_frame, write_frame); |
| 491 |
} |
| 492 |
|
| 493 |
static void audiohook_list_set_samplerate_compatibility(struct ast_audiohook_list *audiohook_list) |
| 494 |
{ |
| 495 |
struct ast_audiohook *ah = NULL; |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
audiohook_list->list_internal_samp_rate = DEFAULT_INTERNAL_SAMPLE_RATE; |
| 511 |
|
| 512 |
audiohook_list->native_slin_compatible = 1; |
| 513 |
AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, ah, list) { |
| 514 |
if (!(ah->init_flags & AST_AUDIOHOOK_MANIPULATE_ALL_RATES)) { |
| 515 |
audiohook_list->native_slin_compatible = 0; |
| 516 |
return; |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook) |
| 522 |
{ |
| 523 |
ast_channel_lock(chan); |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) { |
| 530 |
ast_channel_unlock(chan); |
| 531 |
return -1; |
| 532 |
} |
| 533 |
|
| 534 |
if (!ast_channel_audiohooks(chan)) { |
| 535 |
struct ast_audiohook_list *ahlist; |
| 536 |
|
| 537 |
if (!(ahlist = ast_calloc(1, sizeof(*ahlist)))) { |
| 538 |
ast_channel_unlock(chan); |
| 539 |
return -1; |
| 540 |
} |
| 541 |
ast_channel_audiohooks_set(chan, ahlist); |
| 542 |
AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->spy_list); |
| 543 |
AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->whisper_list); |
| 544 |
AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->manipulate_list); |
| 545 |
|
| 546 |
ast_channel_audiohooks(chan)->list_internal_samp_rate = DEFAULT_INTERNAL_SAMPLE_RATE; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) { |
| 551 |
AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->spy_list, audiohook, list); |
| 552 |
} else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) { |
| 553 |
AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list); |
| 554 |
} else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) { |
| 555 |
AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
audiohook_set_internal_rate(audiohook, DEFAULT_INTERNAL_SAMPLE_RATE, 1); |
| 563 |
audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan)); |
| 564 |
|
| 565 |
|
| 566 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_RUNNING); |
| 567 |
|
| 568 |
if (ast_channel_is_bridged(chan)) { |
| 569 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 570 |
} |
| 571 |
|
| 572 |
ast_channel_unlock(chan); |
| 573 |
|
| 574 |
return 0; |
| 575 |
} |
| 576 |
|
| 577 |
void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status) |
| 578 |
{ |
| 579 |
ast_audiohook_lock(audiohook); |
| 580 |
if (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) { |
| 581 |
audiohook->status = status; |
| 582 |
ast_cond_signal(&audiohook->trigger); |
| 583 |
} |
| 584 |
ast_audiohook_unlock(audiohook); |
| 585 |
} |
| 586 |
|
| 587 |
int ast_audiohook_detach(struct ast_audiohook *audiohook) |
| 588 |
{ |
| 589 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE) { |
| 590 |
return 0; |
| 591 |
} |
| 592 |
|
| 593 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN); |
| 594 |
|
| 595 |
while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) { |
| 596 |
ast_audiohook_trigger_wait(audiohook); |
| 597 |
} |
| 598 |
|
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list) |
| 603 |
{ |
| 604 |
int i; |
| 605 |
struct ast_audiohook *audiohook; |
| 606 |
|
| 607 |
if (!audiohook_list) { |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) { |
| 613 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) { |
| 618 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) { |
| 623 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 624 |
audiohook->manipulate_callback(audiohook, NULL, NULL, 0); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
for (i = 0; i < 2; i++) { |
| 629 |
if (audiohook_list->in_translate[i].trans_pvt) { |
| 630 |
ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt); |
| 631 |
ao2_cleanup(audiohook_list->in_translate[i].format); |
| 632 |
} |
| 633 |
if (audiohook_list->out_translate[i].trans_pvt) { |
| 634 |
ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt); |
| 635 |
ao2_cleanup(audiohook_list->out_translate[i].format); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
ast_free(audiohook_list); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source) |
| 650 |
{ |
| 651 |
struct ast_audiohook *audiohook = NULL; |
| 652 |
|
| 653 |
AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) { |
| 654 |
if (!strcasecmp(audiohook->source, source)) { |
| 655 |
return audiohook; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) { |
| 660 |
if (!strcasecmp(audiohook->source, source)) { |
| 661 |
return audiohook; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) { |
| 666 |
if (!strcasecmp(audiohook->source, source)) { |
| 667 |
return audiohook; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
return NULL; |
| 672 |
} |
| 673 |
|
| 674 |
static void audiohook_move(struct ast_channel *old_chan, struct ast_channel *new_chan, struct ast_audiohook *audiohook) |
| 675 |
{ |
| 676 |
enum ast_audiohook_status oldstatus; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
ast_audiohook_lock(audiohook); |
| 684 |
oldstatus = audiohook->status; |
| 685 |
|
| 686 |
ast_audiohook_remove(old_chan, audiohook); |
| 687 |
ast_audiohook_attach(new_chan, audiohook); |
| 688 |
|
| 689 |
audiohook->status = oldstatus; |
| 690 |
ast_audiohook_unlock(audiohook); |
| 691 |
} |
| 692 |
|
| 693 |
void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source) |
| 694 |
{ |
| 695 |
struct ast_audiohook *audiohook; |
| 696 |
|
| 697 |
if (!ast_channel_audiohooks(old_chan)) { |
| 698 |
return; |
| 699 |
} |
| 700 |
|
| 701 |
audiohook = find_audiohook_by_source(ast_channel_audiohooks(old_chan), source); |
| 702 |
if (!audiohook) { |
| 703 |
return; |
| 704 |
} |
| 705 |
|
| 706 |
audiohook_move(old_chan, new_chan, audiohook); |
| 707 |
} |
| 708 |
|
| 709 |
void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan) |
| 710 |
{ |
| 711 |
struct ast_audiohook *audiohook; |
| 712 |
struct ast_audiohook_list *audiohook_list; |
| 713 |
|
| 714 |
audiohook_list = ast_channel_audiohooks(old_chan); |
| 715 |
if (!audiohook_list) { |
| 716 |
return; |
| 717 |
} |
| 718 |
|
| 719 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) { |
| 720 |
audiohook_move(old_chan, new_chan, audiohook); |
| 721 |
} |
| 722 |
AST_LIST_TRAVERSE_SAFE_END; |
| 723 |
|
| 724 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) { |
| 725 |
audiohook_move(old_chan, new_chan, audiohook); |
| 726 |
} |
| 727 |
AST_LIST_TRAVERSE_SAFE_END; |
| 728 |
|
| 729 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) { |
| 730 |
audiohook_move(old_chan, new_chan, audiohook); |
| 731 |
} |
| 732 |
AST_LIST_TRAVERSE_SAFE_END; |
| 733 |
} |
| 734 |
|
| 735 |
int ast_audiohook_detach_source(struct ast_channel *chan, const char *source) |
| 736 |
{ |
| 737 |
struct ast_audiohook *audiohook = NULL; |
| 738 |
|
| 739 |
ast_channel_lock(chan); |
| 740 |
|
| 741 |
|
| 742 |
if (!ast_channel_audiohooks(chan)) { |
| 743 |
ast_channel_unlock(chan); |
| 744 |
return -1; |
| 745 |
} |
| 746 |
|
| 747 |
audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source); |
| 748 |
|
| 749 |
ast_channel_unlock(chan); |
| 750 |
|
| 751 |
if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE) { |
| 752 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN); |
| 753 |
} |
| 754 |
|
| 755 |
return (audiohook ? 0 : -1); |
| 756 |
} |
| 757 |
|
| 758 |
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook) |
| 759 |
{ |
| 760 |
ast_channel_lock(chan); |
| 761 |
|
| 762 |
if (!ast_channel_audiohooks(chan)) { |
| 763 |
ast_channel_unlock(chan); |
| 764 |
return -1; |
| 765 |
} |
| 766 |
|
| 767 |
if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) { |
| 768 |
AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->spy_list, audiohook, list); |
| 769 |
} else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) { |
| 770 |
AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list); |
| 771 |
} else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) { |
| 772 |
AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list); |
| 773 |
} |
| 774 |
|
| 775 |
audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan)); |
| 776 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 777 |
|
| 778 |
if (ast_channel_is_bridged(chan)) { |
| 779 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 780 |
} |
| 781 |
|
| 782 |
ast_channel_unlock(chan); |
| 783 |
|
| 784 |
return 0; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
static struct ast_frame *dtmf_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame) |
| 796 |
{ |
| 797 |
struct ast_audiohook *audiohook = NULL; |
| 798 |
int removed = 0; |
| 799 |
|
| 800 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) { |
| 801 |
ast_audiohook_lock(audiohook); |
| 802 |
if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 803 |
AST_LIST_REMOVE_CURRENT(list); |
| 804 |
removed = 1; |
| 805 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 806 |
ast_audiohook_unlock(audiohook); |
| 807 |
audiohook->manipulate_callback(audiohook, NULL, NULL, 0); |
| 808 |
if (ast_channel_is_bridged(chan)) { |
| 809 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 810 |
} |
| 811 |
continue; |
| 812 |
} |
| 813 |
if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF)) { |
| 814 |
audiohook->manipulate_callback(audiohook, chan, frame, direction); |
| 815 |
} |
| 816 |
ast_audiohook_unlock(audiohook); |
| 817 |
} |
| 818 |
AST_LIST_TRAVERSE_SAFE_END; |
| 819 |
|
| 820 |
|
| 821 |
if (removed) { |
| 822 |
audiohook_list_set_samplerate_compatibility(audiohook_list); |
| 823 |
} |
| 824 |
return frame; |
| 825 |
} |
| 826 |
|
| 827 |
static struct ast_frame *audiohook_list_translate_to_slin(struct ast_audiohook_list *audiohook_list, |
| 828 |
enum ast_audiohook_direction direction, struct ast_frame *frame) |
| 829 |
{ |
| 830 |
struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? |
| 831 |
&audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]); |
| 832 |
struct ast_frame *new_frame = frame; |
| 833 |
struct ast_format *slin; |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
audiohook_list->list_internal_samp_rate = |
| 842 |
MAX(ast_format_get_sample_rate(frame->subclass.format), audiohook_list->list_internal_samp_rate); |
| 843 |
|
| 844 |
slin = ast_format_cache_get_slin_by_rate(audiohook_list->list_internal_samp_rate); |
| 845 |
if (ast_format_cmp(frame->subclass.format, slin) == AST_FORMAT_CMP_EQUAL) { |
| 846 |
return new_frame; |
| 847 |
} |
| 848 |
|
| 849 |
if (!in_translate->format || |
| 850 |
ast_format_cmp(frame->subclass.format, in_translate->format) != AST_FORMAT_CMP_EQUAL) { |
| 851 |
struct ast_trans_pvt *new_trans; |
| 852 |
|
| 853 |
new_trans = ast_translator_build_path(slin, frame->subclass.format); |
| 854 |
if (!new_trans) { |
| 855 |
return NULL; |
| 856 |
} |
| 857 |
|
| 858 |
if (in_translate->trans_pvt) { |
| 859 |
ast_translator_free_path(in_translate->trans_pvt); |
| 860 |
} |
| 861 |
in_translate->trans_pvt = new_trans; |
| 862 |
|
| 863 |
ao2_replace(in_translate->format, frame->subclass.format); |
| 864 |
} |
| 865 |
|
| 866 |
if (!(new_frame = ast_translate(in_translate->trans_pvt, frame, 0))) { |
| 867 |
return NULL; |
| 868 |
} |
| 869 |
|
| 870 |
return new_frame; |
| 871 |
} |
| 872 |
|
| 873 |
static struct ast_frame *audiohook_list_translate_to_native(struct ast_audiohook_list *audiohook_list, |
| 874 |
enum ast_audiohook_direction direction, struct ast_frame *slin_frame, struct ast_format *outformat) |
| 875 |
{ |
| 876 |
struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]); |
| 877 |
struct ast_frame *outframe = NULL; |
| 878 |
if (ast_format_cmp(slin_frame->subclass.format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) { |
| 879 |
|
| 880 |
if (ast_format_cmp(out_translate->format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) { |
| 881 |
if (out_translate->trans_pvt) { |
| 882 |
ast_translator_free_path(out_translate->trans_pvt); |
| 883 |
} |
| 884 |
if (!(out_translate->trans_pvt = ast_translator_build_path(outformat, slin_frame->subclass.format))) { |
| 885 |
return NULL; |
| 886 |
} |
| 887 |
ao2_replace(out_translate->format, outformat); |
| 888 |
} |
| 889 |
|
| 890 |
if (!(outframe = ast_translate(out_translate->trans_pvt, slin_frame, 0))) { |
| 891 |
return NULL; |
| 892 |
} |
| 893 |
} |
| 894 |
return outframe; |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
static void audiohook_list_set_hook_rate(struct ast_audiohook_list *audiohook_list, |
| 906 |
struct ast_audiohook *audiohook, int *rate) |
| 907 |
{ |
| 908 |
|
| 909 |
if (audiohook->hook_internal_samp_rate > *rate) { |
| 910 |
*rate = audiohook->hook_internal_samp_rate; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
if (audiohook_list->native_slin_compatible) { |
| 920 |
ast_set_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE); |
| 921 |
audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1); |
| 922 |
} else { |
| 923 |
ast_clear_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE); |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
static struct ast_frame *audio_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame) |
| 952 |
{ |
| 953 |
struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame; |
| 954 |
struct ast_audiohook *audiohook = NULL; |
| 955 |
int samples; |
| 956 |
int middle_frame_manipulated = 0; |
| 957 |
int removed = 0; |
| 958 |
int internal_sample_rate; |
| 959 |
|
| 960 |
|
| 961 |
if (!(middle_frame = audiohook_list_translate_to_slin(audiohook_list, direction, start_frame))) { |
| 962 |
return frame; |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
if (!middle_frame->data.ptr) { |
| 969 |
if (middle_frame != start_frame) { |
| 970 |
ast_frfree(middle_frame); |
| 971 |
} |
| 972 |
return start_frame; |
| 973 |
} |
| 974 |
|
| 975 |
samples = middle_frame->samples; |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
internal_sample_rate = audiohook_list->list_internal_samp_rate; |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) { |
| 993 |
ast_audiohook_lock(audiohook); |
| 994 |
if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 995 |
AST_LIST_REMOVE_CURRENT(list); |
| 996 |
removed = 1; |
| 997 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 998 |
ast_audiohook_unlock(audiohook); |
| 999 |
if (ast_channel_is_bridged(chan)) { |
| 1000 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 1001 |
} |
| 1002 |
continue; |
| 1003 |
} |
| 1004 |
audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate); |
| 1005 |
ast_audiohook_write_frame(audiohook, direction, middle_frame); |
| 1006 |
ast_audiohook_unlock(audiohook); |
| 1007 |
} |
| 1008 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1009 |
|
| 1010 |
|
| 1011 |
if (!AST_LIST_EMPTY(&audiohook_list->whisper_list)) { |
| 1012 |
int i = 0; |
| 1013 |
short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL; |
| 1014 |
memset(&combine_buf, 0, sizeof(combine_buf)); |
| 1015 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) { |
| 1016 |
struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory); |
| 1017 |
ast_audiohook_lock(audiohook); |
| 1018 |
if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 1019 |
AST_LIST_REMOVE_CURRENT(list); |
| 1020 |
removed = 1; |
| 1021 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 1022 |
ast_audiohook_unlock(audiohook); |
| 1023 |
if (ast_channel_is_bridged(chan)) { |
| 1024 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 1025 |
} |
| 1026 |
continue; |
| 1027 |
} |
| 1028 |
audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate); |
| 1029 |
if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) { |
| 1030 |
|
| 1031 |
for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++) { |
| 1032 |
ast_slinear_saturated_add(data1, data2); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
ast_audiohook_unlock(audiohook); |
| 1036 |
} |
| 1037 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1038 |
|
| 1039 |
for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) { |
| 1040 |
ast_slinear_saturated_add(data1, data2); |
| 1041 |
} |
| 1042 |
middle_frame_manipulated = 1; |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) { |
| 1047 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) { |
| 1048 |
ast_audiohook_lock(audiohook); |
| 1049 |
if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 1050 |
AST_LIST_REMOVE_CURRENT(list); |
| 1051 |
removed = 1; |
| 1052 |
ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE); |
| 1053 |
ast_audiohook_unlock(audiohook); |
| 1054 |
|
| 1055 |
audiohook->manipulate_callback(audiohook, chan, NULL, direction); |
| 1056 |
if (ast_channel_is_bridged(chan)) { |
| 1057 |
ast_channel_set_unbridged_nolock(chan, 1); |
| 1058 |
} |
| 1059 |
continue; |
| 1060 |
} |
| 1061 |
audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate); |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
if (!audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) { |
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
middle_frame_manipulated = 1; |
| 1073 |
} |
| 1074 |
ast_audiohook_unlock(audiohook); |
| 1075 |
} |
| 1076 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
if (middle_frame_manipulated) { |
| 1081 |
if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, start_frame->subclass.format))) { |
| 1082 |
|
| 1083 |
end_frame = start_frame; |
| 1084 |
} |
| 1085 |
} else { |
| 1086 |
end_frame = start_frame; |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (middle_frame != end_frame) { |
| 1090 |
ast_frfree(middle_frame); |
| 1091 |
middle_frame = NULL; |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
if (removed) { |
| 1096 |
audiohook_list_set_samplerate_compatibility(audiohook_list); |
| 1097 |
} else { |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
audiohook_list->list_internal_samp_rate = internal_sample_rate; |
| 1103 |
} |
| 1104 |
|
| 1105 |
return end_frame; |
| 1106 |
} |
| 1107 |
|
| 1108 |
int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list) |
| 1109 |
{ |
| 1110 |
return !audiohook_list |
| 1111 |
|| (AST_LIST_EMPTY(&audiohook_list->spy_list) |
| 1112 |
&& AST_LIST_EMPTY(&audiohook_list->whisper_list) |
| 1113 |
&& AST_LIST_EMPTY(&audiohook_list->manipulate_list)); |
| 1114 |
} |
| 1115 |
|
| 1116 |
struct ast_frame *ast_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame) |
| 1117 |
{ |
| 1118 |
|
| 1119 |
if (frame->frametype == AST_FRAME_VOICE) { |
| 1120 |
return audio_audiohook_write_list(chan, audiohook_list, direction, frame); |
| 1121 |
} else if (frame->frametype == AST_FRAME_DTMF) { |
| 1122 |
return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame); |
| 1123 |
} else { |
| 1124 |
return frame; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook) |
| 1132 |
{ |
| 1133 |
struct timeval wait; |
| 1134 |
struct timespec ts; |
| 1135 |
|
| 1136 |
wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000)); |
| 1137 |
ts.tv_sec = wait.tv_sec; |
| 1138 |
ts.tv_nsec = wait.tv_usec * 1000; |
| 1139 |
|
| 1140 |
ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts); |
| 1141 |
|
| 1142 |
return; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type) |
| 1147 |
{ |
| 1148 |
int count = 0; |
| 1149 |
struct ast_audiohook *ah = NULL; |
| 1150 |
|
| 1151 |
if (!ast_channel_audiohooks(chan)) { |
| 1152 |
return -1; |
| 1153 |
} |
| 1154 |
|
| 1155 |
switch (type) { |
| 1156 |
case AST_AUDIOHOOK_TYPE_SPY: |
| 1157 |
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) { |
| 1158 |
if (!strcmp(ah->source, source)) { |
| 1159 |
count++; |
| 1160 |
} |
| 1161 |
} |
| 1162 |
break; |
| 1163 |
case AST_AUDIOHOOK_TYPE_WHISPER: |
| 1164 |
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) { |
| 1165 |
if (!strcmp(ah->source, source)) { |
| 1166 |
count++; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
break; |
| 1170 |
case AST_AUDIOHOOK_TYPE_MANIPULATE: |
| 1171 |
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) { |
| 1172 |
if (!strcmp(ah->source, source)) { |
| 1173 |
count++; |
| 1174 |
} |
| 1175 |
} |
| 1176 |
break; |
| 1177 |
default: |
| 1178 |
ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type); |
| 1179 |
return -1; |
| 1180 |
} |
| 1181 |
|
| 1182 |
return count; |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type) |
| 1187 |
{ |
| 1188 |
int count = 0; |
| 1189 |
struct ast_audiohook *ah = NULL; |
| 1190 |
if (!ast_channel_audiohooks(chan)) |
| 1191 |
return -1; |
| 1192 |
|
| 1193 |
switch (type) { |
| 1194 |
case AST_AUDIOHOOK_TYPE_SPY: |
| 1195 |
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) { |
| 1196 |
if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING)) |
| 1197 |
count++; |
| 1198 |
} |
| 1199 |
break; |
| 1200 |
case AST_AUDIOHOOK_TYPE_WHISPER: |
| 1201 |
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) { |
| 1202 |
if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING)) |
| 1203 |
count++; |
| 1204 |
} |
| 1205 |
break; |
| 1206 |
case AST_AUDIOHOOK_TYPE_MANIPULATE: |
| 1207 |
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) { |
| 1208 |
if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING)) |
| 1209 |
count++; |
| 1210 |
} |
| 1211 |
break; |
| 1212 |
default: |
| 1213 |
ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type); |
| 1214 |
return -1; |
| 1215 |
} |
| 1216 |
return count; |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
struct audiohook_volume { |
| 1221 |
struct ast_audiohook audiohook; |
| 1222 |
float read_adjustment; |
| 1223 |
float write_adjustment; |
| 1224 |
}; |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
static void audiohook_volume_destroy(void *data) |
| 1230 |
{ |
| 1231 |
struct audiohook_volume *audiohook_volume = data; |
| 1232 |
|
| 1233 |
|
| 1234 |
ast_audiohook_destroy(&audiohook_volume->audiohook); |
| 1235 |
|
| 1236 |
|
| 1237 |
ast_free(audiohook_volume); |
| 1238 |
|
| 1239 |
return; |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
static const struct ast_datastore_info audiohook_volume_datastore = { |
| 1244 |
.type = "Volume", |
| 1245 |
.destroy = audiohook_volume_destroy, |
| 1246 |
}; |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction) |
| 1257 |
{ |
| 1258 |
struct ast_datastore *datastore = NULL; |
| 1259 |
struct audiohook_volume *audiohook_volume = NULL; |
| 1260 |
float *gain = NULL; |
| 1261 |
|
| 1262 |
|
| 1263 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) { |
| 1264 |
return 0; |
| 1265 |
} |
| 1266 |
|
| 1267 |
|
| 1268 |
if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) { |
| 1269 |
return 0; |
| 1270 |
} |
| 1271 |
|
| 1272 |
audiohook_volume = datastore->data; |
| 1273 |
|
| 1274 |
|
| 1275 |
if (direction == AST_AUDIOHOOK_DIRECTION_READ) { |
| 1276 |
gain = &audiohook_volume->read_adjustment; |
| 1277 |
} else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) { |
| 1278 |
gain = &audiohook_volume->write_adjustment; |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
if (gain && *gain) { |
| 1283 |
ast_frame_adjust_volume_float(frame, *gain); |
| 1284 |
} |
| 1285 |
|
| 1286 |
return 0; |
| 1287 |
} |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create) |
| 1296 |
{ |
| 1297 |
struct ast_datastore *datastore = NULL; |
| 1298 |
struct audiohook_volume *audiohook_volume = NULL; |
| 1299 |
|
| 1300 |
|
| 1301 |
if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) { |
| 1302 |
return datastore->data; |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) { |
| 1307 |
return NULL; |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) { |
| 1312 |
ast_datastore_free(datastore); |
| 1313 |
return NULL; |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 1318 |
audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback; |
| 1319 |
|
| 1320 |
|
| 1321 |
datastore->data = audiohook_volume; |
| 1322 |
ast_channel_datastore_add(chan, datastore); |
| 1323 |
|
| 1324 |
|
| 1325 |
ast_audiohook_attach(chan, &audiohook_volume->audiohook); |
| 1326 |
|
| 1327 |
return audiohook_volume; |
| 1328 |
} |
| 1329 |
|
| 1330 |
int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume) |
| 1331 |
{ |
| 1332 |
return ast_audiohook_volume_adjust_float(chan, direction, (float) volume); |
| 1333 |
} |
| 1334 |
|
| 1335 |
int ast_audiohook_volume_set_float(struct ast_channel *chan, enum ast_audiohook_direction direction, float volume) |
| 1336 |
{ |
| 1337 |
struct audiohook_volume *audiohook_volume = NULL; |
| 1338 |
|
| 1339 |
|
| 1340 |
if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) { |
| 1341 |
return -1; |
| 1342 |
} |
| 1343 |
|
| 1344 |
|
| 1345 |
if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) { |
| 1346 |
audiohook_volume->read_adjustment = volume; |
| 1347 |
} |
| 1348 |
if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) { |
| 1349 |
audiohook_volume->write_adjustment = volume; |
| 1350 |
} |
| 1351 |
|
| 1352 |
return 0; |
| 1353 |
} |
| 1354 |
|
| 1355 |
int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction) |
| 1356 |
{ |
| 1357 |
return (int) ast_audiohook_volume_get_float(chan, direction); |
| 1358 |
} |
| 1359 |
|
| 1360 |
float ast_audiohook_volume_get_float(struct ast_channel *chan, enum ast_audiohook_direction direction) |
| 1361 |
{ |
| 1362 |
struct audiohook_volume *audiohook_volume = NULL; |
| 1363 |
float adjustment = 0; |
| 1364 |
|
| 1365 |
|
| 1366 |
if (!(audiohook_volume = audiohook_volume_get(chan, 0))) { |
| 1367 |
return 0; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
if (direction == AST_AUDIOHOOK_DIRECTION_READ) { |
| 1372 |
adjustment = audiohook_volume->read_adjustment; |
| 1373 |
} else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) { |
| 1374 |
adjustment = audiohook_volume->write_adjustment; |
| 1375 |
} |
| 1376 |
|
| 1377 |
return adjustment; |
| 1378 |
} |
| 1379 |
|
| 1380 |
int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume) |
| 1381 |
{ |
| 1382 |
return ast_audiohook_volume_adjust_float(chan, direction, (float) volume); |
| 1383 |
} |
| 1384 |
|
| 1385 |
int ast_audiohook_volume_adjust_float(struct ast_channel *chan, enum ast_audiohook_direction direction, float volume) |
| 1386 |
{ |
| 1387 |
struct audiohook_volume *audiohook_volume = NULL; |
| 1388 |
|
| 1389 |
|
| 1390 |
if (!(audiohook_volume = audiohook_volume_get(chan, 1))) { |
| 1391 |
return -1; |
| 1392 |
} |
| 1393 |
|
| 1394 |
|
| 1395 |
if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) { |
| 1396 |
audiohook_volume->read_adjustment += volume; |
| 1397 |
} |
| 1398 |
if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) { |
| 1399 |
audiohook_volume->write_adjustment += volume; |
| 1400 |
} |
| 1401 |
|
| 1402 |
return 0; |
| 1403 |
} |
| 1404 |
|
| 1405 |
int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear) |
| 1406 |
{ |
| 1407 |
struct ast_audiohook *audiohook = NULL; |
| 1408 |
|
| 1409 |
ast_channel_lock(chan); |
| 1410 |
|
| 1411 |
|
| 1412 |
if (!ast_channel_audiohooks(chan)) { |
| 1413 |
ast_channel_unlock(chan); |
| 1414 |
return -1; |
| 1415 |
} |
| 1416 |
|
| 1417 |
audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source); |
| 1418 |
|
| 1419 |
if (audiohook) { |
| 1420 |
if (clear) { |
| 1421 |
ast_clear_flag(audiohook, flag); |
| 1422 |
} else { |
| 1423 |
ast_set_flag(audiohook, flag); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
|
| 1427 |
ast_channel_unlock(chan); |
| 1428 |
|
| 1429 |
return (audiohook ? 0 : -1); |
| 1430 |
} |
| 1431 |
|
| 1432 |
int ast_audiohook_set_mute_all(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clearmute) |
| 1433 |
{ |
| 1434 |
struct ast_audiohook *audiohook = NULL; |
| 1435 |
int count = 0; |
| 1436 |
|
| 1437 |
ast_channel_lock(chan); |
| 1438 |
|
| 1439 |
if (!ast_channel_audiohooks(chan)) { |
| 1440 |
ast_channel_unlock(chan); |
| 1441 |
return -1; |
| 1442 |
} |
| 1443 |
|
| 1444 |
AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, audiohook, list) { |
| 1445 |
if (!strcasecmp(audiohook->source, source)) { |
| 1446 |
count++; |
| 1447 |
if (clearmute) { |
| 1448 |
ast_clear_flag(audiohook, flag); |
| 1449 |
} else { |
| 1450 |
ast_set_flag(audiohook, flag); |
| 1451 |
} |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
ast_test_suite_event_notify("AUDIOHOOK_GROUP_MUTE_TOGGLE", "Channel: %s\r\nSource: %s\r\nCount: %d\r\n", |
| 1456 |
ast_channel_name(chan), source, count); |
| 1457 |
|
| 1458 |
ast_channel_unlock(chan); |
| 1459 |
|
| 1460 |
return count; |
| 1461 |
} |