| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include "asterisk.h" |
| 42 |
|
| 43 |
#include "asterisk/paths.h" |
| 44 |
#include "asterisk/stringfields.h" |
| 45 |
#include "asterisk/file.h" |
| 46 |
#include "asterisk/audiohook.h" |
| 47 |
#include "asterisk/pbx.h" |
| 48 |
#include "asterisk/module.h" |
| 49 |
#include "asterisk/cli.h" |
| 50 |
#include "asterisk/app.h" |
| 51 |
#include "asterisk/channel.h" |
| 52 |
#include "asterisk/autochan.h" |
| 53 |
#include "asterisk/manager.h" |
| 54 |
#include "asterisk/stasis.h" |
| 55 |
#include "asterisk/stasis_channels.h" |
| 56 |
#include "asterisk/callerid.h" |
| 57 |
#include "asterisk/mod_format.h" |
| 58 |
#include "asterisk/linkedlists.h" |
| 59 |
#include "asterisk/test.h" |
| 60 |
#include "asterisk/mixmonitor.h" |
| 61 |
#include "asterisk/format_cache.h" |
| 62 |
#include "asterisk/beep.h" |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 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 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
#define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0 |
| 402 |
|
| 403 |
#define MIN_SKIP_SECONDS 1 |
| 404 |
|
| 405 |
static const char * const app = "MixMonitor"; |
| 406 |
|
| 407 |
static const char * const stop_app = "StopMixMonitor"; |
| 408 |
|
| 409 |
static const char * const mixmonitor_spy_type = "MixMonitor"; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
struct vm_recipient { |
| 416 |
char mailbox[AST_MAX_CONTEXT]; |
| 417 |
char context[AST_MAX_EXTENSION]; |
| 418 |
char folder[80]; |
| 419 |
AST_LIST_ENTRY(vm_recipient) list; |
| 420 |
}; |
| 421 |
|
| 422 |
struct mixmonitor { |
| 423 |
struct ast_audiohook audiohook; |
| 424 |
char *filename; |
| 425 |
char *filename_read; |
| 426 |
char *filename_write; |
| 427 |
char *post_process; |
| 428 |
char *name; |
| 429 |
ast_callid callid; |
| 430 |
unsigned int flags; |
| 431 |
struct ast_autochan *autochan; |
| 432 |
struct mixmonitor_ds *mixmonitor_ds; |
| 433 |
|
| 434 |
|
| 435 |
AST_DECLARE_STRING_FIELDS( |
| 436 |
AST_STRING_FIELD(call_context); |
| 437 |
AST_STRING_FIELD(call_extension); |
| 438 |
AST_STRING_FIELD(call_callerchan); |
| 439 |
AST_STRING_FIELD(call_callerid); |
| 440 |
); |
| 441 |
int call_priority; |
| 442 |
|
| 443 |
|
| 444 |
double skip_seconds; |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
AST_LIST_HEAD_NOLOCK(, vm_recipient) recipient_list; |
| 449 |
}; |
| 450 |
|
| 451 |
enum mixmonitor_flags { |
| 452 |
MUXFLAG_APPEND = (1 << 1), |
| 453 |
MUXFLAG_BRIDGED = (1 << 2), |
| 454 |
MUXFLAG_VOLUME = (1 << 3), |
| 455 |
MUXFLAG_READVOLUME = (1 << 4), |
| 456 |
MUXFLAG_WRITEVOLUME = (1 << 5), |
| 457 |
MUXFLAG_READ = (1 << 6), |
| 458 |
MUXFLAG_WRITE = (1 << 7), |
| 459 |
MUXFLAG_COMBINED = (1 << 8), |
| 460 |
MUXFLAG_UID = (1 << 9), |
| 461 |
MUXFLAG_VMRECIPIENTS = (1 << 10), |
| 462 |
MUXFLAG_BEEP = (1 << 11), |
| 463 |
MUXFLAG_BEEP_START = (1 << 12), |
| 464 |
MUXFLAG_BEEP_STOP = (1 << 13), |
| 465 |
MUXFLAG_DEPRECATED_RWSYNC = (1 << 14), |
| 466 |
MUXFLAG_NO_RWSYNC = (1 << 15), |
| 467 |
MUXFLAG_AUTO_DELETE = (1 << 16), |
| 468 |
MUXFLAG_REAL_CALLERID = (1 << 17), |
| 469 |
MUXFLAG_INTERLEAVED = (1 << 18), |
| 470 |
MUXFLAG_SKIP = (1 << 19), |
| 471 |
}; |
| 472 |
|
| 473 |
enum mixmonitor_args { |
| 474 |
OPT_ARG_READVOLUME = 0, |
| 475 |
OPT_ARG_WRITEVOLUME, |
| 476 |
OPT_ARG_VOLUME, |
| 477 |
OPT_ARG_WRITENAME, |
| 478 |
OPT_ARG_READNAME, |
| 479 |
OPT_ARG_UID, |
| 480 |
OPT_ARG_VMRECIPIENTS, |
| 481 |
OPT_ARG_BEEP_INTERVAL, |
| 482 |
OPT_ARG_DEPRECATED_RWSYNC, |
| 483 |
OPT_ARG_NO_RWSYNC, |
| 484 |
OPT_ARG_SKIP, |
| 485 |
OPT_ARG_ARRAY_SIZE, |
| 486 |
}; |
| 487 |
|
| 488 |
AST_APP_OPTIONS(mixmonitor_opts, { |
| 489 |
AST_APP_OPTION('a', MUXFLAG_APPEND), |
| 490 |
AST_APP_OPTION('b', MUXFLAG_BRIDGED), |
| 491 |
AST_APP_OPTION_ARG('B', MUXFLAG_BEEP, OPT_ARG_BEEP_INTERVAL), |
| 492 |
AST_APP_OPTION('c', MUXFLAG_REAL_CALLERID), |
| 493 |
AST_APP_OPTION('d', MUXFLAG_AUTO_DELETE), |
| 494 |
AST_APP_OPTION('p', MUXFLAG_BEEP_START), |
| 495 |
AST_APP_OPTION('P', MUXFLAG_BEEP_STOP), |
| 496 |
AST_APP_OPTION_ARG('v', MUXFLAG_READVOLUME, OPT_ARG_READVOLUME), |
| 497 |
AST_APP_OPTION_ARG('V', MUXFLAG_WRITEVOLUME, OPT_ARG_WRITEVOLUME), |
| 498 |
AST_APP_OPTION_ARG('W', MUXFLAG_VOLUME, OPT_ARG_VOLUME), |
| 499 |
AST_APP_OPTION_ARG('r', MUXFLAG_READ, OPT_ARG_READNAME), |
| 500 |
AST_APP_OPTION_ARG('t', MUXFLAG_WRITE, OPT_ARG_WRITENAME), |
| 501 |
AST_APP_OPTION('D', MUXFLAG_INTERLEAVED), |
| 502 |
AST_APP_OPTION_ARG('i', MUXFLAG_UID, OPT_ARG_UID), |
| 503 |
AST_APP_OPTION_ARG('m', MUXFLAG_VMRECIPIENTS, OPT_ARG_VMRECIPIENTS), |
| 504 |
AST_APP_OPTION_ARG('S', MUXFLAG_DEPRECATED_RWSYNC, OPT_ARG_DEPRECATED_RWSYNC), |
| 505 |
AST_APP_OPTION_ARG('n', MUXFLAG_NO_RWSYNC, OPT_ARG_NO_RWSYNC), |
| 506 |
AST_APP_OPTION_ARG('s', MUXFLAG_SKIP, OPT_ARG_SKIP), |
| 507 |
}); |
| 508 |
|
| 509 |
struct mixmonitor_ds { |
| 510 |
unsigned int destruction_ok; |
| 511 |
ast_cond_t destruction_condition; |
| 512 |
ast_mutex_t lock; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
int fs_quit; |
| 517 |
|
| 518 |
struct ast_filestream *fs; |
| 519 |
struct ast_filestream *fs_read; |
| 520 |
struct ast_filestream *fs_write; |
| 521 |
|
| 522 |
struct ast_audiohook *audiohook; |
| 523 |
|
| 524 |
unsigned int samp_rate; |
| 525 |
char *filename; |
| 526 |
char *beep_id; |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
static void mixmonitor_ds_close_fs(struct mixmonitor_ds *mixmonitor_ds) |
| 534 |
{ |
| 535 |
unsigned char quitting = 0; |
| 536 |
|
| 537 |
if (mixmonitor_ds->fs) { |
| 538 |
quitting = 1; |
| 539 |
ast_closestream(mixmonitor_ds->fs); |
| 540 |
mixmonitor_ds->fs = NULL; |
| 541 |
ast_verb(2, "MixMonitor close filestream (mixed)\n"); |
| 542 |
} |
| 543 |
|
| 544 |
if (mixmonitor_ds->fs_read) { |
| 545 |
quitting = 1; |
| 546 |
ast_closestream(mixmonitor_ds->fs_read); |
| 547 |
mixmonitor_ds->fs_read = NULL; |
| 548 |
ast_verb(2, "MixMonitor close filestream (read)\n"); |
| 549 |
} |
| 550 |
|
| 551 |
if (mixmonitor_ds->fs_write) { |
| 552 |
quitting = 1; |
| 553 |
ast_closestream(mixmonitor_ds->fs_write); |
| 554 |
mixmonitor_ds->fs_write = NULL; |
| 555 |
ast_verb(2, "MixMonitor close filestream (write)\n"); |
| 556 |
} |
| 557 |
|
| 558 |
if (quitting) { |
| 559 |
mixmonitor_ds->fs_quit = 1; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
static void mixmonitor_ds_destroy(void *data) |
| 564 |
{ |
| 565 |
struct mixmonitor_ds *mixmonitor_ds = data; |
| 566 |
|
| 567 |
ast_mutex_lock(&mixmonitor_ds->lock); |
| 568 |
mixmonitor_ds->audiohook = NULL; |
| 569 |
mixmonitor_ds->destruction_ok = 1; |
| 570 |
ast_free(mixmonitor_ds->filename); |
| 571 |
ast_free(mixmonitor_ds->beep_id); |
| 572 |
ast_cond_signal(&mixmonitor_ds->destruction_condition); |
| 573 |
ast_mutex_unlock(&mixmonitor_ds->lock); |
| 574 |
} |
| 575 |
|
| 576 |
static const struct ast_datastore_info mixmonitor_ds_info = { |
| 577 |
.type = "mixmonitor", |
| 578 |
.destroy = mixmonitor_ds_destroy, |
| 579 |
}; |
| 580 |
|
| 581 |
static void destroy_monitor_audiohook(struct mixmonitor *mixmonitor) |
| 582 |
{ |
| 583 |
if (mixmonitor->mixmonitor_ds) { |
| 584 |
ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); |
| 585 |
mixmonitor->mixmonitor_ds->audiohook = NULL; |
| 586 |
ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); |
| 587 |
} |
| 588 |
|
| 589 |
ast_audiohook_lock(&mixmonitor->audiohook); |
| 590 |
ast_audiohook_detach(&mixmonitor->audiohook); |
| 591 |
ast_audiohook_unlock(&mixmonitor->audiohook); |
| 592 |
ast_audiohook_destroy(&mixmonitor->audiohook); |
| 593 |
} |
| 594 |
|
| 595 |
static int startmon(struct ast_channel *chan, struct ast_audiohook *audiohook) |
| 596 |
{ |
| 597 |
if (!chan) { |
| 598 |
return -1; |
| 599 |
} |
| 600 |
|
| 601 |
return ast_audiohook_attach(chan, audiohook); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
static void add_vm_recipients_from_string(struct mixmonitor *mixmonitor, const char *vm_recipients) |
| 611 |
{ |
| 612 |
|
| 613 |
char *cur_mailbox = ast_strdupa(vm_recipients); |
| 614 |
char *cur_context; |
| 615 |
char *cur_folder; |
| 616 |
char *next; |
| 617 |
int elements_processed = 0; |
| 618 |
|
| 619 |
while (!ast_strlen_zero(cur_mailbox)) { |
| 620 |
ast_debug(3, "attempting to add next element %d from %s\n", elements_processed, cur_mailbox); |
| 621 |
if ((next = strchr(cur_mailbox, ',')) || (next = strchr(cur_mailbox, '&'))) { |
| 622 |
*(next++) = '\0'; |
| 623 |
} |
| 624 |
|
| 625 |
if ((cur_folder = strchr(cur_mailbox, '/'))) { |
| 626 |
*(cur_folder++) = '\0'; |
| 627 |
} else { |
| 628 |
cur_folder = "INBOX"; |
| 629 |
} |
| 630 |
|
| 631 |
if ((cur_context = strchr(cur_mailbox, '@'))) { |
| 632 |
*(cur_context++) = '\0'; |
| 633 |
} else { |
| 634 |
cur_context = "default"; |
| 635 |
} |
| 636 |
|
| 637 |
if (!ast_strlen_zero(cur_mailbox) && !ast_strlen_zero(cur_context)) { |
| 638 |
struct vm_recipient *recipient; |
| 639 |
if (!(recipient = ast_malloc(sizeof(*recipient)))) { |
| 640 |
ast_log(LOG_ERROR, "Failed to allocate recipient. Aborting function.\n"); |
| 641 |
return; |
| 642 |
} |
| 643 |
ast_copy_string(recipient->context, cur_context, sizeof(recipient->context)); |
| 644 |
ast_copy_string(recipient->mailbox, cur_mailbox, sizeof(recipient->mailbox)); |
| 645 |
ast_copy_string(recipient->folder, cur_folder, sizeof(recipient->folder)); |
| 646 |
|
| 647 |
|
| 648 |
ast_verb(4, "Adding %s@%s to recipient list\n", recipient->mailbox, recipient->context); |
| 649 |
AST_LIST_INSERT_HEAD(&mixmonitor->recipient_list, recipient, list); |
| 650 |
} else { |
| 651 |
ast_log(LOG_ERROR, "Failed to properly parse extension and/or context from element %d of recipient string: %s\n", elements_processed, vm_recipients); |
| 652 |
} |
| 653 |
|
| 654 |
cur_mailbox = next; |
| 655 |
elements_processed++; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
static void clear_mixmonitor_recipient_list(struct mixmonitor *mixmonitor) |
| 660 |
{ |
| 661 |
struct vm_recipient *current; |
| 662 |
while ((current = AST_LIST_REMOVE_HEAD(&mixmonitor->recipient_list, list))) { |
| 663 |
|
| 664 |
ast_free(current); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
#define SAMPLES_PER_FRAME 160 |
| 669 |
|
| 670 |
static void mixmonitor_free(struct mixmonitor *mixmonitor) |
| 671 |
{ |
| 672 |
if (mixmonitor) { |
| 673 |
if (mixmonitor->mixmonitor_ds) { |
| 674 |
ast_mutex_destroy(&mixmonitor->mixmonitor_ds->lock); |
| 675 |
ast_cond_destroy(&mixmonitor->mixmonitor_ds->destruction_condition); |
| 676 |
ast_free(mixmonitor->mixmonitor_ds); |
| 677 |
} |
| 678 |
|
| 679 |
ast_free(mixmonitor->name); |
| 680 |
ast_free(mixmonitor->post_process); |
| 681 |
ast_free(mixmonitor->filename); |
| 682 |
ast_free(mixmonitor->filename_write); |
| 683 |
ast_free(mixmonitor->filename_read); |
| 684 |
|
| 685 |
|
| 686 |
clear_mixmonitor_recipient_list(mixmonitor); |
| 687 |
|
| 688 |
|
| 689 |
ast_string_field_free_memory(mixmonitor); |
| 690 |
|
| 691 |
ast_free(mixmonitor); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
static void copy_to_voicemail(struct mixmonitor *mixmonitor, const char *ext, const char *filename) |
| 703 |
{ |
| 704 |
struct vm_recipient *recipient = NULL; |
| 705 |
struct ast_vm_recording_data recording_data; |
| 706 |
if (ast_string_field_init(&recording_data, 512)) { |
| 707 |
ast_log(LOG_ERROR, "Failed to string_field_init, skipping copy_to_voicemail\n"); |
| 708 |
return; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
ast_string_field_set(&recording_data, recording_file, filename); |
| 713 |
ast_string_field_set(&recording_data, recording_ext, ext); |
| 714 |
ast_string_field_set(&recording_data, call_context, mixmonitor->call_context); |
| 715 |
ast_string_field_set(&recording_data, call_extension, mixmonitor->call_extension); |
| 716 |
ast_string_field_set(&recording_data, call_callerchan, mixmonitor->call_callerchan); |
| 717 |
ast_string_field_set(&recording_data, call_callerid, mixmonitor->call_callerid); |
| 718 |
|
| 719 |
recording_data.call_priority = mixmonitor->call_priority; |
| 720 |
|
| 721 |
AST_LIST_TRAVERSE(&mixmonitor->recipient_list, recipient, list) { |
| 722 |
|
| 723 |
ast_string_field_set(&recording_data, context, recipient->context); |
| 724 |
ast_string_field_set(&recording_data, mailbox, recipient->mailbox); |
| 725 |
ast_string_field_set(&recording_data, folder, recipient->folder); |
| 726 |
|
| 727 |
ast_verb(4, "MixMonitor attempting to send voicemail copy to %s@%s\n", recording_data.mailbox, |
| 728 |
recording_data.context); |
| 729 |
ast_app_copy_recording_to_vm(&recording_data); |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
ast_string_field_free_memory(&recording_data); |
| 734 |
} |
| 735 |
|
| 736 |
static void mixmonitor_save_prep(struct mixmonitor *mixmonitor, char *filename, struct ast_filestream **fs, unsigned int *oflags, int *errflag, char **ext) |
| 737 |
{ |
| 738 |
|
| 739 |
char *last_slash = NULL; |
| 740 |
if (!ast_strlen_zero(filename)) { |
| 741 |
if (!*fs && !*errflag && !mixmonitor->mixmonitor_ds->fs_quit) { |
| 742 |
*oflags = O_CREAT | O_WRONLY; |
| 743 |
*oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC; |
| 744 |
|
| 745 |
last_slash = strrchr(filename, '/'); |
| 746 |
|
| 747 |
if ((*ext = strrchr(filename, '.')) && (*ext > last_slash)) { |
| 748 |
**ext = '\0'; |
| 749 |
*ext = *ext + 1; |
| 750 |
} else { |
| 751 |
*ext = "raw"; |
| 752 |
} |
| 753 |
|
| 754 |
if (!(*fs = ast_writefile(filename, *ext, NULL, *oflags, 0, 0666))) { |
| 755 |
ast_log(LOG_ERROR, "Cannot open %s.%s\n", filename, *ext); |
| 756 |
*errflag = 1; |
| 757 |
} else { |
| 758 |
struct ast_filestream *tmp = *fs; |
| 759 |
mixmonitor->mixmonitor_ds->samp_rate = MAX(mixmonitor->mixmonitor_ds->samp_rate, ast_format_get_sample_rate(tmp->fmt->format)); |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
static int mixmonitor_autochan_is_bridged(struct ast_autochan *autochan) |
| 766 |
{ |
| 767 |
int is_bridged; |
| 768 |
|
| 769 |
ast_autochan_channel_lock(autochan); |
| 770 |
is_bridged = ast_channel_is_bridged(autochan->chan); |
| 771 |
ast_autochan_channel_unlock(autochan); |
| 772 |
return is_bridged; |
| 773 |
} |
| 774 |
|
| 775 |
static void *mixmonitor_thread(void *obj) |
| 776 |
{ |
| 777 |
struct mixmonitor *mixmonitor = obj; |
| 778 |
char *fs_ext = ""; |
| 779 |
char *fs_read_ext = ""; |
| 780 |
char *fs_write_ext = ""; |
| 781 |
|
| 782 |
struct ast_filestream **fs = NULL; |
| 783 |
struct ast_filestream **fs_read = NULL; |
| 784 |
struct ast_filestream **fs_write = NULL; |
| 785 |
|
| 786 |
unsigned int oflags; |
| 787 |
int errflag = 0; |
| 788 |
struct ast_format *format_slin; |
| 789 |
|
| 790 |
struct timeval skip_start = ast_tvnow(); |
| 791 |
|
| 792 |
|
| 793 |
if (mixmonitor->callid) { |
| 794 |
ast_callid_threadassoc_add(mixmonitor->callid); |
| 795 |
} |
| 796 |
|
| 797 |
ast_verb(2, "Begin MixMonitor Recording %s\n", mixmonitor->name); |
| 798 |
|
| 799 |
fs = &mixmonitor->mixmonitor_ds->fs; |
| 800 |
fs_read = &mixmonitor->mixmonitor_ds->fs_read; |
| 801 |
fs_write = &mixmonitor->mixmonitor_ds->fs_write; |
| 802 |
|
| 803 |
ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); |
| 804 |
mixmonitor_save_prep(mixmonitor, mixmonitor->filename, fs, &oflags, &errflag, &fs_ext); |
| 805 |
mixmonitor_save_prep(mixmonitor, mixmonitor->filename_read, fs_read, &oflags, &errflag, &fs_read_ext); |
| 806 |
mixmonitor_save_prep(mixmonitor, mixmonitor->filename_write, fs_write, &oflags, &errflag, &fs_write_ext); |
| 807 |
|
| 808 |
format_slin = ast_format_cache_get_slin_by_rate(mixmonitor->mixmonitor_ds->samp_rate); |
| 809 |
|
| 810 |
ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); |
| 811 |
|
| 812 |
if (mixmonitor->skip_seconds > 0.0) { |
| 813 |
ast_debug(3, "%s skipping initial %.3f seconds\n", |
| 814 |
mixmonitor->name, mixmonitor->skip_seconds); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
ast_audiohook_lock(&mixmonitor->audiohook); |
| 819 |
while (mixmonitor->audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING && !mixmonitor->mixmonitor_ds->fs_quit) { |
| 820 |
struct ast_frame *fr = NULL; |
| 821 |
struct ast_frame *fr_read = NULL; |
| 822 |
struct ast_frame *fr_write = NULL; |
| 823 |
|
| 824 |
if (!(fr = ast_audiohook_read_frame_all(&mixmonitor->audiohook, SAMPLES_PER_FRAME, format_slin, |
| 825 |
&fr_read, &fr_write))) { |
| 826 |
ast_audiohook_trigger_wait(&mixmonitor->audiohook); |
| 827 |
|
| 828 |
if (mixmonitor->audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 829 |
break; |
| 830 |
} |
| 831 |
continue; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
ast_audiohook_unlock(&mixmonitor->audiohook); |
| 837 |
|
| 838 |
if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) |
| 839 |
|| mixmonitor_autochan_is_bridged(mixmonitor->autochan)) { |
| 840 |
ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); |
| 841 |
|
| 842 |
|
| 843 |
if (mixmonitor->skip_seconds > 0.0) { |
| 844 |
struct timeval now = ast_tvnow(); |
| 845 |
double elapsed = ast_tvdiff_ms(now, skip_start) / 1000.0; |
| 846 |
|
| 847 |
if (elapsed < mixmonitor->skip_seconds) { |
| 848 |
ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); |
| 849 |
|
| 850 |
goto frame_cleanup; |
| 851 |
} else { |
| 852 |
ast_debug(3, "%s skip period %.3f seconds elapsed; starting to write audio\n", |
| 853 |
mixmonitor->name, mixmonitor->skip_seconds); |
| 854 |
mixmonitor->skip_seconds = 0.0; |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
if ((*fs_read) && (fr_read)) { |
| 860 |
struct ast_frame *cur; |
| 861 |
|
| 862 |
for (cur = fr_read; cur; cur = AST_LIST_NEXT(cur, frame_list)) { |
| 863 |
ast_writestream(*fs_read, cur); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
if ((*fs_write) && (fr_write)) { |
| 868 |
struct ast_frame *cur; |
| 869 |
|
| 870 |
for (cur = fr_write; cur; cur = AST_LIST_NEXT(cur, frame_list)) { |
| 871 |
ast_writestream(*fs_write, cur); |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
if (ast_test_flag(mixmonitor, MUXFLAG_INTERLEAVED)) { |
| 876 |
|
| 877 |
int i; |
| 878 |
short read_buf[SAMPLES_PER_FRAME]; |
| 879 |
short write_buf[SAMPLES_PER_FRAME]; |
| 880 |
short stereo_buf[SAMPLES_PER_FRAME * 2]; |
| 881 |
struct ast_frame stereo_frame = { |
| 882 |
.frametype = AST_FRAME_VOICE, |
| 883 |
.datalen = sizeof(stereo_buf), |
| 884 |
.samples = SAMPLES_PER_FRAME, |
| 885 |
}; |
| 886 |
|
| 887 |
if (fr) { |
| 888 |
ast_frame_free(fr, 0); |
| 889 |
fr = NULL; |
| 890 |
} |
| 891 |
|
| 892 |
if (fr_read) { |
| 893 |
memcpy(read_buf, fr_read->data.ptr, sizeof(read_buf)); |
| 894 |
} else { |
| 895 |
memset(read_buf, 0, sizeof(read_buf)); |
| 896 |
} |
| 897 |
|
| 898 |
if (fr_write) { |
| 899 |
memcpy(write_buf, fr_write->data.ptr, sizeof(write_buf)); |
| 900 |
} else { |
| 901 |
memset(write_buf, 0, sizeof(write_buf)); |
| 902 |
} |
| 903 |
|
| 904 |
for (i = 0; i < SAMPLES_PER_FRAME; i++) { |
| 905 |
stereo_buf[i * 2] = read_buf[i]; |
| 906 |
stereo_buf[i * 2 + 1] = write_buf[i]; |
| 907 |
} |
| 908 |
|
| 909 |
stereo_frame.data.ptr = stereo_buf; |
| 910 |
stereo_frame.subclass.format = ast_format_cache_get_slin_by_rate(SAMPLES_PER_FRAME); |
| 911 |
|
| 912 |
fr = ast_frdup(&stereo_frame); |
| 913 |
} |
| 914 |
|
| 915 |
if ((*fs) && (fr)) { |
| 916 |
struct ast_frame *cur; |
| 917 |
|
| 918 |
for (cur = fr; cur; cur = AST_LIST_NEXT(cur, frame_list)) { |
| 919 |
ast_writestream(*fs, cur); |
| 920 |
} |
| 921 |
} |
| 922 |
ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); |
| 923 |
} |
| 924 |
|
| 925 |
frame_cleanup: |
| 926 |
|
| 927 |
if (fr) { |
| 928 |
ast_frame_free(fr, 0); |
| 929 |
} |
| 930 |
if (fr_read) { |
| 931 |
ast_frame_free(fr_read, 0); |
| 932 |
} |
| 933 |
if (fr_write) { |
| 934 |
ast_frame_free(fr_write, 0); |
| 935 |
} |
| 936 |
|
| 937 |
fr = NULL; |
| 938 |
fr_write = NULL; |
| 939 |
fr_read = NULL; |
| 940 |
|
| 941 |
ast_audiohook_lock(&mixmonitor->audiohook); |
| 942 |
} |
| 943 |
|
| 944 |
ast_audiohook_unlock(&mixmonitor->audiohook); |
| 945 |
|
| 946 |
if (ast_test_flag(mixmonitor, MUXFLAG_BEEP_STOP)) { |
| 947 |
ast_autochan_channel_lock(mixmonitor->autochan); |
| 948 |
ast_stream_and_wait(mixmonitor->autochan->chan, "beep", ""); |
| 949 |
ast_autochan_channel_unlock(mixmonitor->autochan); |
| 950 |
} |
| 951 |
|
| 952 |
ast_autochan_destroy(mixmonitor->autochan); |
| 953 |
|
| 954 |
|
| 955 |
ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock); |
| 956 |
mixmonitor_ds_close_fs(mixmonitor->mixmonitor_ds); |
| 957 |
if (!mixmonitor->mixmonitor_ds->destruction_ok) { |
| 958 |
ast_cond_wait(&mixmonitor->mixmonitor_ds->destruction_condition, &mixmonitor->mixmonitor_ds->lock); |
| 959 |
} |
| 960 |
ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock); |
| 961 |
|
| 962 |
|
| 963 |
destroy_monitor_audiohook(mixmonitor); |
| 964 |
|
| 965 |
if (mixmonitor->post_process) { |
| 966 |
ast_verb(2, "Executing [%s]\n", mixmonitor->post_process); |
| 967 |
ast_safe_system(mixmonitor->post_process); |
| 968 |
} |
| 969 |
|
| 970 |
ast_verb(2, "End MixMonitor Recording %s\n", mixmonitor->name); |
| 971 |
ast_test_suite_event_notify("MIXMONITOR_END", "File: %s\r\n", mixmonitor->filename); |
| 972 |
|
| 973 |
if (!AST_LIST_EMPTY(&mixmonitor->recipient_list)) { |
| 974 |
if (ast_strlen_zero(fs_ext)) { |
| 975 |
ast_log(LOG_ERROR, "No file extension set for Mixmonitor %s. Skipping copy to voicemail.\n", |
| 976 |
mixmonitor -> name); |
| 977 |
} else { |
| 978 |
ast_verb(3, "Copying recordings for Mixmonitor %s to voicemail recipients\n", mixmonitor->name); |
| 979 |
copy_to_voicemail(mixmonitor, fs_ext, mixmonitor->filename); |
| 980 |
} |
| 981 |
if (!ast_strlen_zero(fs_read_ext)) { |
| 982 |
ast_verb(3, "Copying read recording for Mixmonitor %s to voicemail recipients\n", mixmonitor->name); |
| 983 |
copy_to_voicemail(mixmonitor, fs_read_ext, mixmonitor->filename_read); |
| 984 |
} |
| 985 |
if (!ast_strlen_zero(fs_write_ext)) { |
| 986 |
ast_verb(3, "Copying write recording for Mixmonitor %s to voicemail recipients\n", mixmonitor->name); |
| 987 |
copy_to_voicemail(mixmonitor, fs_write_ext, mixmonitor->filename_write); |
| 988 |
} |
| 989 |
} else { |
| 990 |
ast_debug(3, "No recipients to forward monitor to, moving on.\n"); |
| 991 |
} |
| 992 |
|
| 993 |
if (ast_test_flag(mixmonitor, MUXFLAG_AUTO_DELETE)) { |
| 994 |
ast_debug(3, "Deleting our copies of recording files\n"); |
| 995 |
if (!ast_strlen_zero(fs_ext)) { |
| 996 |
ast_filedelete(mixmonitor->filename, fs_ext); |
| 997 |
} |
| 998 |
if (!ast_strlen_zero(fs_read_ext)) { |
| 999 |
ast_filedelete(mixmonitor->filename_read, fs_ext); |
| 1000 |
} |
| 1001 |
if (!ast_strlen_zero(fs_write_ext)) { |
| 1002 |
ast_filedelete(mixmonitor->filename_write, fs_ext); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
mixmonitor_free(mixmonitor); |
| 1007 |
|
| 1008 |
ast_module_unref(ast_module_info->self); |
| 1009 |
return NULL; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static int setup_mixmonitor_ds(struct mixmonitor *mixmonitor, struct ast_channel *chan, char **datastore_id, const char *beep_id) |
| 1013 |
{ |
| 1014 |
struct ast_datastore *datastore = NULL; |
| 1015 |
struct mixmonitor_ds *mixmonitor_ds; |
| 1016 |
|
| 1017 |
if (!(mixmonitor_ds = ast_calloc(1, sizeof(*mixmonitor_ds)))) { |
| 1018 |
return -1; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if (ast_asprintf(datastore_id, "%p", mixmonitor_ds) == -1) { |
| 1022 |
ast_log(LOG_ERROR, "Failed to allocate memory for MixMonitor ID.\n"); |
| 1023 |
ast_free(mixmonitor_ds); |
| 1024 |
return -1; |
| 1025 |
} |
| 1026 |
|
| 1027 |
ast_mutex_init(&mixmonitor_ds->lock); |
| 1028 |
ast_cond_init(&mixmonitor_ds->destruction_condition, NULL); |
| 1029 |
|
| 1030 |
if (!(datastore = ast_datastore_alloc(&mixmonitor_ds_info, *datastore_id))) { |
| 1031 |
ast_mutex_destroy(&mixmonitor_ds->lock); |
| 1032 |
ast_cond_destroy(&mixmonitor_ds->destruction_condition); |
| 1033 |
ast_free(mixmonitor_ds); |
| 1034 |
return -1; |
| 1035 |
} |
| 1036 |
|
| 1037 |
if (ast_test_flag(mixmonitor, MUXFLAG_BEEP_START)) { |
| 1038 |
ast_autochan_channel_lock(mixmonitor->autochan); |
| 1039 |
ast_stream_and_wait(mixmonitor->autochan->chan, "beep", ""); |
| 1040 |
ast_autochan_channel_unlock(mixmonitor->autochan); |
| 1041 |
} |
| 1042 |
|
| 1043 |
mixmonitor_ds->samp_rate = 8000; |
| 1044 |
mixmonitor_ds->audiohook = &mixmonitor->audiohook; |
| 1045 |
mixmonitor_ds->filename = ast_strdup(mixmonitor->filename); |
| 1046 |
if (!ast_strlen_zero(beep_id)) { |
| 1047 |
mixmonitor_ds->beep_id = ast_strdup(beep_id); |
| 1048 |
} |
| 1049 |
datastore->data = mixmonitor_ds; |
| 1050 |
|
| 1051 |
ast_channel_lock(chan); |
| 1052 |
ast_channel_datastore_add(chan, datastore); |
| 1053 |
ast_channel_unlock(chan); |
| 1054 |
|
| 1055 |
mixmonitor->mixmonitor_ds = mixmonitor_ds; |
| 1056 |
return 0; |
| 1057 |
} |
| 1058 |
|
| 1059 |
static void mixmonitor_ds_remove_and_free(struct ast_channel *chan, const char *datastore_id) |
| 1060 |
{ |
| 1061 |
struct ast_datastore *datastore; |
| 1062 |
|
| 1063 |
ast_channel_lock(chan); |
| 1064 |
|
| 1065 |
datastore = ast_channel_datastore_find(chan, &mixmonitor_ds_info, datastore_id); |
| 1066 |
|
| 1067 |
if (datastore) { |
| 1068 |
ast_channel_datastore_remove(chan, datastore); |
| 1069 |
ast_datastore_free(datastore); |
| 1070 |
} |
| 1071 |
|
| 1072 |
ast_channel_unlock(chan); |
| 1073 |
} |
| 1074 |
|
| 1075 |
static int launch_monitor_thread(struct ast_channel *chan, const char *filename, |
| 1076 |
unsigned int flags, int readvol, int writevol, |
| 1077 |
const char *post_process, const char *filename_write, |
| 1078 |
char *filename_read, const char *uid_channel_var, |
| 1079 |
const char *recipients, const char *beep_id, double skip_seconds) |
| 1080 |
{ |
| 1081 |
pthread_t thread; |
| 1082 |
struct mixmonitor *mixmonitor; |
| 1083 |
char postprocess2[1024] = ""; |
| 1084 |
char *datastore_id = NULL; |
| 1085 |
|
| 1086 |
postprocess2[0] = 0; |
| 1087 |
|
| 1088 |
if (!ast_strlen_zero(post_process)) { |
| 1089 |
char *p1, *p2; |
| 1090 |
|
| 1091 |
p1 = ast_strdupa(post_process); |
| 1092 |
for (p2 = p1; *p2; p2++) { |
| 1093 |
if (*p2 == '^' && *(p2+1) == '{') { |
| 1094 |
*p2 = '$'; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
ast_channel_lock(chan); |
| 1098 |
pbx_substitute_variables_helper(chan, p1, postprocess2, sizeof(postprocess2) - 1); |
| 1099 |
ast_channel_unlock(chan); |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
if (!(mixmonitor = ast_calloc(1, sizeof(*mixmonitor)))) { |
| 1104 |
return -1; |
| 1105 |
} |
| 1106 |
|
| 1107 |
|
| 1108 |
if (ast_string_field_init(mixmonitor, 512)) { |
| 1109 |
mixmonitor_free(mixmonitor); |
| 1110 |
return -1; |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
if (ast_audiohook_init(&mixmonitor->audiohook, AST_AUDIOHOOK_TYPE_SPY, mixmonitor_spy_type, 0)) { |
| 1115 |
mixmonitor_free(mixmonitor); |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
mixmonitor->flags = flags; |
| 1121 |
mixmonitor->skip_seconds = skip_seconds; |
| 1122 |
if (!(mixmonitor->autochan = ast_autochan_setup(chan))) { |
| 1123 |
mixmonitor_free(mixmonitor); |
| 1124 |
return -1; |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (!ast_strlen_zero(filename)) { |
| 1128 |
mixmonitor->filename = ast_strdup(filename); |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (!ast_strlen_zero(filename_write)) { |
| 1132 |
mixmonitor->filename_write = ast_strdup(filename_write); |
| 1133 |
} |
| 1134 |
|
| 1135 |
if (!ast_strlen_zero(filename_read)) { |
| 1136 |
mixmonitor->filename_read = ast_strdup(filename_read); |
| 1137 |
} |
| 1138 |
|
| 1139 |
if (setup_mixmonitor_ds(mixmonitor, chan, &datastore_id, beep_id)) { |
| 1140 |
ast_autochan_destroy(mixmonitor->autochan); |
| 1141 |
mixmonitor_free(mixmonitor); |
| 1142 |
ast_free(datastore_id); |
| 1143 |
return -1; |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (!ast_strlen_zero(uid_channel_var)) { |
| 1147 |
if (datastore_id) { |
| 1148 |
pbx_builtin_setvar_helper(chan, uid_channel_var, datastore_id); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
mixmonitor->name = ast_strdup(ast_channel_name(chan)); |
| 1153 |
|
| 1154 |
if (!ast_strlen_zero(postprocess2)) { |
| 1155 |
mixmonitor->post_process = ast_strdup(postprocess2); |
| 1156 |
} |
| 1157 |
|
| 1158 |
if (!ast_strlen_zero(recipients)) { |
| 1159 |
char callerid[256]; |
| 1160 |
|
| 1161 |
ast_channel_lock(chan); |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
if (ast_test_flag(mixmonitor, MUXFLAG_REAL_CALLERID)) { |
| 1171 |
struct ast_party_caller *caller; |
| 1172 |
caller = ast_channel_caller(chan); |
| 1173 |
ast_debug(3, "Caller ID = %d - %s : %d - %s\n", caller->id.name.valid, |
| 1174 |
caller->id.name.str, caller->id.number.valid, |
| 1175 |
caller->id.number.str); |
| 1176 |
ast_callerid_merge(callerid, sizeof(callerid), |
| 1177 |
S_COR(caller->id.name.valid, caller->id.name.str, NULL), |
| 1178 |
S_COR(caller->id.number.valid, caller->id.number.str, NULL), |
| 1179 |
"Unknown"); |
| 1180 |
} else { |
| 1181 |
struct ast_party_connected_line *connected; |
| 1182 |
connected = ast_channel_connected(chan); |
| 1183 |
ast_debug(3, "Connected Line CID = %d - %s : %d - %s\n", connected->id.name.valid, |
| 1184 |
connected->id.name.str, connected->id.number.valid, |
| 1185 |
connected->id.number.str); |
| 1186 |
ast_callerid_merge(callerid, sizeof(callerid), |
| 1187 |
S_COR(connected->id.name.valid, connected->id.name.str, NULL), |
| 1188 |
S_COR(connected->id.number.valid, connected->id.number.str, NULL), |
| 1189 |
"Unknown"); |
| 1190 |
} |
| 1191 |
|
| 1192 |
ast_string_field_set(mixmonitor, call_context, ast_channel_context(chan)); |
| 1193 |
ast_string_field_set(mixmonitor, call_extension, ast_channel_exten(chan)); |
| 1194 |
ast_string_field_set(mixmonitor, call_callerchan, ast_channel_name(chan)); |
| 1195 |
ast_string_field_set(mixmonitor, call_callerid, callerid); |
| 1196 |
mixmonitor->call_priority = ast_channel_priority(chan); |
| 1197 |
|
| 1198 |
ast_channel_unlock(chan); |
| 1199 |
|
| 1200 |
add_vm_recipients_from_string(mixmonitor, recipients); |
| 1201 |
} |
| 1202 |
|
| 1203 |
ast_set_flag(&mixmonitor->audiohook, AST_AUDIOHOOK_TRIGGER_SYNC); |
| 1204 |
if (!ast_test_flag(mixmonitor, MUXFLAG_NO_RWSYNC)) { |
| 1205 |
ast_set_flag(&mixmonitor->audiohook, AST_AUDIOHOOK_SUBSTITUTE_SILENCE); |
| 1206 |
} |
| 1207 |
|
| 1208 |
if (readvol) |
| 1209 |
mixmonitor->audiohook.options.read_volume = readvol; |
| 1210 |
if (writevol) |
| 1211 |
mixmonitor->audiohook.options.write_volume = writevol; |
| 1212 |
|
| 1213 |
if (startmon(chan, &mixmonitor->audiohook)) { |
| 1214 |
ast_log(LOG_WARNING, "Unable to add '%s' spy to channel '%s'\n", |
| 1215 |
mixmonitor_spy_type, ast_channel_name(chan)); |
| 1216 |
mixmonitor_ds_remove_and_free(chan, datastore_id); |
| 1217 |
ast_free(datastore_id); |
| 1218 |
ast_autochan_destroy(mixmonitor->autochan); |
| 1219 |
ast_audiohook_destroy(&mixmonitor->audiohook); |
| 1220 |
mixmonitor_free(mixmonitor); |
| 1221 |
return -1; |
| 1222 |
} |
| 1223 |
|
| 1224 |
ast_free(datastore_id); |
| 1225 |
|
| 1226 |
|
| 1227 |
mixmonitor->callid = ast_read_threadstorage_callid(); |
| 1228 |
|
| 1229 |
return ast_pthread_create_detached_background(&thread, NULL, mixmonitor_thread, mixmonitor); |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
static char *filename_parse(char *filename, char *buffer, size_t len) |
| 1235 |
{ |
| 1236 |
char *slash; |
| 1237 |
char *ext; |
| 1238 |
|
| 1239 |
ast_assert(len > 0); |
| 1240 |
|
| 1241 |
if (ast_strlen_zero(filename)) { |
| 1242 |
ast_log(LOG_WARNING, "No file name was provided for a file save option.\n"); |
| 1243 |
buffer[0] = 0; |
| 1244 |
return buffer; |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
if (*filename != '/') { |
| 1249 |
char *build = ast_alloca(strlen(ast_config_AST_MONITOR_DIR) + strlen(filename) + 3); |
| 1250 |
sprintf(build, "%s/%s", ast_config_AST_MONITOR_DIR, filename); |
| 1251 |
filename = build; |
| 1252 |
} |
| 1253 |
|
| 1254 |
ast_copy_string(buffer, filename, len); |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
ext = strrchr(buffer, '.'); |
| 1260 |
if (ext && !strcmp(ext, ".wav49")) { |
| 1261 |
|
| 1262 |
|
| 1263 |
memcpy(ext, ".WAV", sizeof(".WAV")); |
| 1264 |
} |
| 1265 |
|
| 1266 |
if ((slash = strrchr(filename, '/'))) { |
| 1267 |
*slash = '\0'; |
| 1268 |
} |
| 1269 |
ast_mkdir(filename, 0777); |
| 1270 |
|
| 1271 |
return buffer; |
| 1272 |
} |
| 1273 |
|
| 1274 |
static int mixmonitor_exec(struct ast_channel *chan, const char *data) |
| 1275 |
{ |
| 1276 |
int x, readvol = 0, writevol = 0; |
| 1277 |
double skip_seconds = 0.0; |
| 1278 |
char *filename_read = NULL; |
| 1279 |
char *filename_write = NULL; |
| 1280 |
char filename_buffer[1024] = ""; |
| 1281 |
char *uid_channel_var = NULL; |
| 1282 |
char beep_id[64] = ""; |
| 1283 |
|
| 1284 |
struct ast_flags flags = { 0 }; |
| 1285 |
char *recipients = NULL; |
| 1286 |
char *parse; |
| 1287 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 1288 |
AST_DECLARE_APP_ARGS(args, |
| 1289 |
AST_APP_ARG(filename); |
| 1290 |
AST_APP_ARG(options); |
| 1291 |
AST_APP_ARG(post_process); |
| 1292 |
); |
| 1293 |
|
| 1294 |
if (ast_strlen_zero(data)) { |
| 1295 |
ast_log(LOG_WARNING, "MixMonitor requires an argument (filename or ,t(filename) and/or r(filename)\n"); |
| 1296 |
return -1; |
| 1297 |
} |
| 1298 |
|
| 1299 |
parse = ast_strdupa(data); |
| 1300 |
|
| 1301 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1302 |
|
| 1303 |
if (args.options) { |
| 1304 |
char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, }; |
| 1305 |
|
| 1306 |
ast_app_parse_options(mixmonitor_opts, &flags, opts, args.options); |
| 1307 |
|
| 1308 |
if (ast_test_flag(&flags, MUXFLAG_DEPRECATED_RWSYNC)) { |
| 1309 |
ast_log(LOG_NOTICE, "The synchronization behavior enabled by the 'S' option is now the default" |
| 1310 |
" and does not need to be specified.\n"); |
| 1311 |
} |
| 1312 |
|
| 1313 |
if (ast_test_flag(&flags, MUXFLAG_READVOLUME)) { |
| 1314 |
if (ast_strlen_zero(opts[OPT_ARG_READVOLUME])) { |
| 1315 |
ast_log(LOG_WARNING, "No volume level was provided for the heard volume ('v') option.\n"); |
| 1316 |
} else if ((sscanf(opts[OPT_ARG_READVOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) { |
| 1317 |
ast_log(LOG_NOTICE, "Heard volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_READVOLUME]); |
| 1318 |
} else { |
| 1319 |
readvol = get_volfactor(x); |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
if (ast_test_flag(&flags, MUXFLAG_WRITEVOLUME)) { |
| 1324 |
if (ast_strlen_zero(opts[OPT_ARG_WRITEVOLUME])) { |
| 1325 |
ast_log(LOG_WARNING, "No volume level was provided for the spoken volume ('V') option.\n"); |
| 1326 |
} else if ((sscanf(opts[OPT_ARG_WRITEVOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) { |
| 1327 |
ast_log(LOG_NOTICE, "Spoken volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_WRITEVOLUME]); |
| 1328 |
} else { |
| 1329 |
writevol = get_volfactor(x); |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
if (ast_test_flag(&flags, MUXFLAG_VOLUME)) { |
| 1334 |
if (ast_strlen_zero(opts[OPT_ARG_VOLUME])) { |
| 1335 |
ast_log(LOG_WARNING, "No volume level was provided for the combined volume ('W') option.\n"); |
| 1336 |
} else if ((sscanf(opts[OPT_ARG_VOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) { |
| 1337 |
ast_log(LOG_NOTICE, "Combined volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_VOLUME]); |
| 1338 |
} else { |
| 1339 |
readvol = writevol = get_volfactor(x); |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
if (ast_test_flag(&flags, MUXFLAG_VMRECIPIENTS)) { |
| 1344 |
if (ast_strlen_zero(opts[OPT_ARG_VMRECIPIENTS])) { |
| 1345 |
ast_log(LOG_WARNING, "No voicemail recipients were specified for the vm copy ('m') option.\n"); |
| 1346 |
} else { |
| 1347 |
recipients = ast_strdupa(opts[OPT_ARG_VMRECIPIENTS]); |
| 1348 |
} |
| 1349 |
} |
| 1350 |
|
| 1351 |
if (ast_test_flag(&flags, MUXFLAG_WRITE)) { |
| 1352 |
filename_write = ast_strdupa(filename_parse(opts[OPT_ARG_WRITENAME], filename_buffer, sizeof(filename_buffer))); |
| 1353 |
} |
| 1354 |
|
| 1355 |
if (ast_test_flag(&flags, MUXFLAG_READ)) { |
| 1356 |
filename_read = ast_strdupa(filename_parse(opts[OPT_ARG_READNAME], filename_buffer, sizeof(filename_buffer))); |
| 1357 |
} |
| 1358 |
|
| 1359 |
if (ast_test_flag(&flags, MUXFLAG_UID)) { |
| 1360 |
uid_channel_var = opts[OPT_ARG_UID]; |
| 1361 |
} |
| 1362 |
|
| 1363 |
if (ast_test_flag(&flags, MUXFLAG_BEEP)) { |
| 1364 |
const char *interval_str = S_OR(opts[OPT_ARG_BEEP_INTERVAL], "15"); |
| 1365 |
unsigned int interval = 15; |
| 1366 |
|
| 1367 |
if (sscanf(interval_str, "%30u", &interval) != 1) { |
| 1368 |
ast_log(LOG_WARNING, "Invalid interval '%s' for periodic beep. Using default of %u\n", |
| 1369 |
interval_str, interval); |
| 1370 |
} |
| 1371 |
|
| 1372 |
if (ast_beep_start(chan, interval, beep_id, sizeof(beep_id))) { |
| 1373 |
ast_log(LOG_WARNING, "Unable to enable periodic beep, please ensure func_periodic_hook is loaded.\n"); |
| 1374 |
return -1; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (ast_test_flag(&flags, MUXFLAG_SKIP)) { |
| 1379 |
if (ast_strlen_zero(opts[OPT_ARG_SKIP])) { |
| 1380 |
ast_log(LOG_WARNING, "No skip value provided for the 's' (skip) option; skipping will be ignored as no default exists.\n"); |
| 1381 |
} else { |
| 1382 |
char *endptr = NULL; |
| 1383 |
double val = strtod(opts[OPT_ARG_SKIP], &endptr); |
| 1384 |
if (endptr == opts[OPT_ARG_SKIP] || *endptr != '\0') { |
| 1385 |
ast_log(LOG_WARNING, "Skip value '%s' is not a valid number; ignoring skip.\n", opts[OPT_ARG_SKIP]); |
| 1386 |
} else if (val < (double) MIN_SKIP_SECONDS) { |
| 1387 |
ast_log(LOG_WARNING, "Skip value %.3f is below minimum %d; ignoring skip.\n", val, MIN_SKIP_SECONDS); |
| 1388 |
} else { |
| 1389 |
skip_seconds = val; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
|
| 1396 |
if (!ast_test_flag(&flags, MUXFLAG_WRITE) && !ast_test_flag(&flags, MUXFLAG_READ) && ast_strlen_zero(args.filename)) { |
| 1397 |
ast_log(LOG_WARNING, "MixMonitor requires an argument (filename)\n"); |
| 1398 |
return -1; |
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
if (!(ast_strlen_zero(args.filename))) { |
| 1403 |
args.filename = ast_strdupa(filename_parse(args.filename, filename_buffer, sizeof(filename_buffer))); |
| 1404 |
} |
| 1405 |
|
| 1406 |
pbx_builtin_setvar_helper(chan, "MIXMONITOR_FILENAME", args.filename); |
| 1407 |
|
| 1408 |
|
| 1409 |
ast_module_ref(ast_module_info->self); |
| 1410 |
if (launch_monitor_thread(chan, |
| 1411 |
args.filename, |
| 1412 |
flags.flags, |
| 1413 |
readvol, |
| 1414 |
writevol, |
| 1415 |
args.post_process, |
| 1416 |
filename_write, |
| 1417 |
filename_read, |
| 1418 |
uid_channel_var, |
| 1419 |
recipients, |
| 1420 |
beep_id, |
| 1421 |
skip_seconds)) { |
| 1422 |
ast_module_unref(ast_module_info->self); |
| 1423 |
} |
| 1424 |
|
| 1425 |
message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), |
| 1426 |
ast_channel_mixmonitor_start_type(), NULL); |
| 1427 |
if (message) { |
| 1428 |
stasis_publish(ast_channel_topic(chan), message); |
| 1429 |
} |
| 1430 |
|
| 1431 |
return 0; |
| 1432 |
} |
| 1433 |
|
| 1434 |
static int stop_mixmonitor_full(struct ast_channel *chan, const char *data) |
| 1435 |
{ |
| 1436 |
struct ast_datastore *datastore = NULL; |
| 1437 |
char *parse = ""; |
| 1438 |
struct mixmonitor_ds *mixmonitor_ds; |
| 1439 |
const char *beep_id = NULL; |
| 1440 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 1441 |
|
| 1442 |
AST_DECLARE_APP_ARGS(args, |
| 1443 |
AST_APP_ARG(mixmonid); |
| 1444 |
); |
| 1445 |
|
| 1446 |
if (!ast_strlen_zero(data)) { |
| 1447 |
parse = ast_strdupa(data); |
| 1448 |
} |
| 1449 |
|
| 1450 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1451 |
|
| 1452 |
ast_channel_lock(chan); |
| 1453 |
|
| 1454 |
datastore = ast_channel_datastore_find(chan, &mixmonitor_ds_info, |
| 1455 |
S_OR(args.mixmonid, NULL)); |
| 1456 |
if (!datastore) { |
| 1457 |
ast_channel_unlock(chan); |
| 1458 |
return -1; |
| 1459 |
} |
| 1460 |
mixmonitor_ds = datastore->data; |
| 1461 |
|
| 1462 |
ast_mutex_lock(&mixmonitor_ds->lock); |
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
mixmonitor_ds_close_fs(mixmonitor_ds); |
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
if (mixmonitor_ds->audiohook) { |
| 1472 |
if (mixmonitor_ds->audiohook->status != AST_AUDIOHOOK_STATUS_DONE) { |
| 1473 |
ast_audiohook_update_status(mixmonitor_ds->audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN); |
| 1474 |
} |
| 1475 |
ast_audiohook_lock(mixmonitor_ds->audiohook); |
| 1476 |
ast_cond_signal(&mixmonitor_ds->audiohook->trigger); |
| 1477 |
ast_audiohook_unlock(mixmonitor_ds->audiohook); |
| 1478 |
mixmonitor_ds->audiohook = NULL; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (!ast_strlen_zero(mixmonitor_ds->beep_id)) { |
| 1482 |
beep_id = ast_strdupa(mixmonitor_ds->beep_id); |
| 1483 |
} |
| 1484 |
|
| 1485 |
ast_mutex_unlock(&mixmonitor_ds->lock); |
| 1486 |
|
| 1487 |
|
| 1488 |
if (!ast_channel_datastore_remove(chan, datastore)) { |
| 1489 |
ast_datastore_free(datastore); |
| 1490 |
} |
| 1491 |
|
| 1492 |
ast_channel_unlock(chan); |
| 1493 |
|
| 1494 |
if (!ast_strlen_zero(beep_id)) { |
| 1495 |
ast_beep_stop(chan, beep_id); |
| 1496 |
} |
| 1497 |
|
| 1498 |
message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), |
| 1499 |
ast_channel_mixmonitor_stop_type(), |
| 1500 |
NULL); |
| 1501 |
if (message) { |
| 1502 |
stasis_publish(ast_channel_topic(chan), message); |
| 1503 |
} |
| 1504 |
|
| 1505 |
return 0; |
| 1506 |
} |
| 1507 |
|
| 1508 |
static int stop_mixmonitor_exec(struct ast_channel *chan, const char *data) |
| 1509 |
{ |
| 1510 |
stop_mixmonitor_full(chan, data); |
| 1511 |
return 0; |
| 1512 |
} |
| 1513 |
|
| 1514 |
static char *handle_cli_mixmonitor(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1515 |
{ |
| 1516 |
struct ast_channel *chan; |
| 1517 |
struct ast_datastore *datastore = NULL; |
| 1518 |
struct mixmonitor_ds *mixmonitor_ds = NULL; |
| 1519 |
|
| 1520 |
switch (cmd) { |
| 1521 |
case CLI_INIT: |
| 1522 |
e->command = "mixmonitor {start|stop|list}"; |
| 1523 |
e->usage = |
| 1524 |
"Usage: mixmonitor start <chan_name> [args]\n" |
| 1525 |
" The optional arguments are passed to the MixMonitor application.\n" |
| 1526 |
" mixmonitor stop <chan_name> [args]\n" |
| 1527 |
" The optional arguments are passed to the StopMixMonitor application.\n" |
| 1528 |
" mixmonitor list <chan_name>\n"; |
| 1529 |
return NULL; |
| 1530 |
case CLI_GENERATE: |
| 1531 |
return ast_complete_channels(a->line, a->word, a->pos, a->n, 2); |
| 1532 |
} |
| 1533 |
|
| 1534 |
if (a->argc < 3) { |
| 1535 |
return CLI_SHOWUSAGE; |
| 1536 |
} |
| 1537 |
|
| 1538 |
if (!(chan = ast_channel_get_by_name_prefix(a->argv[2], strlen(a->argv[2])))) { |
| 1539 |
ast_cli(a->fd, "No channel matching '%s' found.\n", a->argv[2]); |
| 1540 |
|
| 1541 |
return CLI_SUCCESS; |
| 1542 |
} |
| 1543 |
|
| 1544 |
if (!strcasecmp(a->argv[1], "start")) { |
| 1545 |
mixmonitor_exec(chan, (a->argc >= 4) ? a->argv[3] : ""); |
| 1546 |
} else if (!strcasecmp(a->argv[1], "stop")){ |
| 1547 |
stop_mixmonitor_exec(chan, (a->argc >= 4) ? a->argv[3] : ""); |
| 1548 |
} else if (!strcasecmp(a->argv[1], "list")) { |
| 1549 |
ast_cli(a->fd, "MixMonitor ID\tFile\tReceive File\tTransmit File\n"); |
| 1550 |
ast_cli(a->fd, "=========================================================================\n"); |
| 1551 |
ast_channel_lock(chan); |
| 1552 |
AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) { |
| 1553 |
if (datastore->info == &mixmonitor_ds_info) { |
| 1554 |
char *filename = ""; |
| 1555 |
char *filename_read = ""; |
| 1556 |
char *filename_write = ""; |
| 1557 |
|
| 1558 |
mixmonitor_ds = datastore->data; |
| 1559 |
if (mixmonitor_ds->fs) { |
| 1560 |
filename = mixmonitor_ds->fs->filename; |
| 1561 |
} |
| 1562 |
if (mixmonitor_ds->fs_read) { |
| 1563 |
filename_read = mixmonitor_ds->fs_read->filename; |
| 1564 |
} |
| 1565 |
if (mixmonitor_ds->fs_write) { |
| 1566 |
filename_write = mixmonitor_ds->fs_write->filename; |
| 1567 |
} |
| 1568 |
ast_cli(a->fd, "%p\t%s\t%s\t%s\n", mixmonitor_ds, filename, filename_read, filename_write); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
ast_channel_unlock(chan); |
| 1572 |
} else { |
| 1573 |
chan = ast_channel_unref(chan); |
| 1574 |
return CLI_SHOWUSAGE; |
| 1575 |
} |
| 1576 |
|
| 1577 |
chan = ast_channel_unref(chan); |
| 1578 |
|
| 1579 |
return CLI_SUCCESS; |
| 1580 |
} |
| 1581 |
|
| 1582 |
|
| 1583 |
static int mute_mixmonitor_instance(struct ast_channel *chan, const char *data, |
| 1584 |
enum ast_audiohook_flags flag, int clearmute) |
| 1585 |
{ |
| 1586 |
struct ast_datastore *datastore = NULL; |
| 1587 |
char *parse = ""; |
| 1588 |
struct mixmonitor_ds *mixmonitor_ds; |
| 1589 |
|
| 1590 |
AST_DECLARE_APP_ARGS(args, |
| 1591 |
AST_APP_ARG(mixmonid); |
| 1592 |
); |
| 1593 |
|
| 1594 |
if (!ast_strlen_zero(data)) { |
| 1595 |
parse = ast_strdupa(data); |
| 1596 |
} |
| 1597 |
|
| 1598 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1599 |
|
| 1600 |
ast_channel_lock(chan); |
| 1601 |
|
| 1602 |
datastore = ast_channel_datastore_find(chan, &mixmonitor_ds_info, |
| 1603 |
S_OR(args.mixmonid, NULL)); |
| 1604 |
if (!datastore) { |
| 1605 |
ast_channel_unlock(chan); |
| 1606 |
return -1; |
| 1607 |
} |
| 1608 |
mixmonitor_ds = datastore->data; |
| 1609 |
|
| 1610 |
ast_mutex_lock(&mixmonitor_ds->lock); |
| 1611 |
|
| 1612 |
if (mixmonitor_ds->audiohook) { |
| 1613 |
if (clearmute) { |
| 1614 |
ast_clear_flag(mixmonitor_ds->audiohook, flag); |
| 1615 |
} else { |
| 1616 |
ast_set_flag(mixmonitor_ds->audiohook, flag); |
| 1617 |
} |
| 1618 |
} |
| 1619 |
|
| 1620 |
ast_mutex_unlock(&mixmonitor_ds->lock); |
| 1621 |
ast_channel_unlock(chan); |
| 1622 |
|
| 1623 |
return 0; |
| 1624 |
} |
| 1625 |
|
| 1626 |
|
| 1627 |
static int manager_mute_mixmonitor(struct mansession *s, const struct message *m) |
| 1628 |
{ |
| 1629 |
struct ast_channel *c; |
| 1630 |
const char *name = astman_get_header(m, "Channel"); |
| 1631 |
const char *id = astman_get_header(m, "ActionID"); |
| 1632 |
const char *state = astman_get_header(m, "State"); |
| 1633 |
const char *direction = astman_get_header(m,"Direction"); |
| 1634 |
const char *mixmonitor_id = astman_get_header(m, "MixMonitorID"); |
| 1635 |
int clearmute = 1, mutedcount = 0; |
| 1636 |
enum ast_audiohook_flags flag; |
| 1637 |
RAII_VAR(struct stasis_message *, stasis_message, NULL, ao2_cleanup); |
| 1638 |
RAII_VAR(struct ast_json *, stasis_message_blob, NULL, ast_json_unref); |
| 1639 |
|
| 1640 |
if (ast_strlen_zero(direction)) { |
| 1641 |
astman_send_error(s, m, "No direction specified. Must be read, write or both"); |
| 1642 |
return AMI_SUCCESS; |
| 1643 |
} |
| 1644 |
|
| 1645 |
if (!strcasecmp(direction, "read")) { |
| 1646 |
flag = AST_AUDIOHOOK_MUTE_READ; |
| 1647 |
} else if (!strcasecmp(direction, "write")) { |
| 1648 |
flag = AST_AUDIOHOOK_MUTE_WRITE; |
| 1649 |
} else if (!strcasecmp(direction, "both")) { |
| 1650 |
flag = AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE; |
| 1651 |
} else { |
| 1652 |
astman_send_error(s, m, "Invalid direction specified. Must be read, write or both"); |
| 1653 |
return AMI_SUCCESS; |
| 1654 |
} |
| 1655 |
|
| 1656 |
if (ast_strlen_zero(name)) { |
| 1657 |
astman_send_error(s, m, "No channel specified"); |
| 1658 |
return AMI_SUCCESS; |
| 1659 |
} |
| 1660 |
|
| 1661 |
if (ast_strlen_zero(state)) { |
| 1662 |
astman_send_error(s, m, "No state specified"); |
| 1663 |
return AMI_SUCCESS; |
| 1664 |
} |
| 1665 |
|
| 1666 |
clearmute = ast_false(state); |
| 1667 |
|
| 1668 |
c = ast_channel_get_by_name(name); |
| 1669 |
if (!c) { |
| 1670 |
astman_send_error(s, m, "No such channel"); |
| 1671 |
return AMI_SUCCESS; |
| 1672 |
} |
| 1673 |
|
| 1674 |
if (ast_strlen_zero(mixmonitor_id)) { |
| 1675 |
mutedcount = ast_audiohook_set_mute_all(c, mixmonitor_spy_type, flag, clearmute); |
| 1676 |
if (mutedcount < 0) { |
| 1677 |
ast_channel_unref(c); |
| 1678 |
astman_send_error(s, m, "Cannot set mute flag"); |
| 1679 |
return AMI_SUCCESS; |
| 1680 |
} |
| 1681 |
} else { |
| 1682 |
if (mute_mixmonitor_instance(c, mixmonitor_id, flag, clearmute)) { |
| 1683 |
ast_channel_unref(c); |
| 1684 |
astman_send_error(s, m, "Cannot set mute flag"); |
| 1685 |
return AMI_SUCCESS; |
| 1686 |
} |
| 1687 |
mutedcount = 1; |
| 1688 |
} |
| 1689 |
|
| 1690 |
|
| 1691 |
stasis_message_blob = ast_json_pack("{s: s, s: b, s: s, s: i}", |
| 1692 |
"direction", direction, |
| 1693 |
"state", ast_true(state), |
| 1694 |
"mixmonitorid", mixmonitor_id, |
| 1695 |
"count", mutedcount); |
| 1696 |
|
| 1697 |
stasis_message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(c), |
| 1698 |
ast_channel_mixmonitor_mute_type(), stasis_message_blob); |
| 1699 |
|
| 1700 |
if (stasis_message) { |
| 1701 |
stasis_publish(ast_channel_topic(c), stasis_message); |
| 1702 |
} |
| 1703 |
|
| 1704 |
astman_append(s, "Response: Success\r\n"); |
| 1705 |
|
| 1706 |
if (!ast_strlen_zero(id)) { |
| 1707 |
astman_append(s, "ActionID: %s\r\n", id); |
| 1708 |
} |
| 1709 |
|
| 1710 |
astman_append(s, "\r\n"); |
| 1711 |
|
| 1712 |
ast_channel_unref(c); |
| 1713 |
|
| 1714 |
return AMI_SUCCESS; |
| 1715 |
} |
| 1716 |
|
| 1717 |
static int start_mixmonitor_callback(struct ast_channel *chan, const char *filename, const char *options) |
| 1718 |
{ |
| 1719 |
char args[PATH_MAX]; |
| 1720 |
|
| 1721 |
if (ast_strlen_zero(options)) { |
| 1722 |
snprintf(args, sizeof(args), "%s", filename); |
| 1723 |
} else { |
| 1724 |
snprintf(args, sizeof(args), "%s,%s", filename, options); |
| 1725 |
} |
| 1726 |
|
| 1727 |
return mixmonitor_exec(chan, args); |
| 1728 |
} |
| 1729 |
|
| 1730 |
static int stop_mixmonitor_callback(struct ast_channel *chan, const char *mixmonitor_id) |
| 1731 |
{ |
| 1732 |
return stop_mixmonitor_full(chan, mixmonitor_id); |
| 1733 |
} |
| 1734 |
|
| 1735 |
static int manager_mixmonitor(struct mansession *s, const struct message *m) |
| 1736 |
{ |
| 1737 |
struct ast_channel *c; |
| 1738 |
const char *name = astman_get_header(m, "Channel"); |
| 1739 |
const char *id = astman_get_header(m, "ActionID"); |
| 1740 |
const char *file = astman_get_header(m, "File"); |
| 1741 |
const char *options = astman_get_header(m, "Options"); |
| 1742 |
const char *command = astman_get_header(m, "Command"); |
| 1743 |
char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, }; |
| 1744 |
struct ast_flags flags = { 0 }; |
| 1745 |
char *uid_channel_var = NULL; |
| 1746 |
const char *mixmonitor_id = NULL; |
| 1747 |
int res; |
| 1748 |
char args[PATH_MAX]; |
| 1749 |
|
| 1750 |
if (ast_strlen_zero(name)) { |
| 1751 |
astman_send_error(s, m, "No channel specified"); |
| 1752 |
return AMI_SUCCESS; |
| 1753 |
} |
| 1754 |
|
| 1755 |
c = ast_channel_get_by_name(name); |
| 1756 |
if (!c) { |
| 1757 |
astman_send_error(s, m, "No such channel"); |
| 1758 |
return AMI_SUCCESS; |
| 1759 |
} |
| 1760 |
|
| 1761 |
if (!ast_strlen_zero(options)) { |
| 1762 |
ast_app_parse_options(mixmonitor_opts, &flags, opts, ast_strdupa(options)); |
| 1763 |
} |
| 1764 |
|
| 1765 |
snprintf(args, sizeof(args), "%s,%s,%s", file, options, command); |
| 1766 |
|
| 1767 |
res = mixmonitor_exec(c, args); |
| 1768 |
|
| 1769 |
if (ast_test_flag(&flags, MUXFLAG_UID)) { |
| 1770 |
uid_channel_var = opts[OPT_ARG_UID]; |
| 1771 |
ast_channel_lock(c); |
| 1772 |
mixmonitor_id = pbx_builtin_getvar_helper(c, uid_channel_var); |
| 1773 |
mixmonitor_id = ast_strdupa(S_OR(mixmonitor_id, "")); |
| 1774 |
ast_channel_unlock(c); |
| 1775 |
} |
| 1776 |
|
| 1777 |
if (res) { |
| 1778 |
ast_channel_unref(c); |
| 1779 |
astman_send_error(s, m, "Could not start monitoring channel"); |
| 1780 |
return AMI_SUCCESS; |
| 1781 |
} |
| 1782 |
|
| 1783 |
astman_append(s, "Response: Success\r\n"); |
| 1784 |
|
| 1785 |
if (!ast_strlen_zero(id)) { |
| 1786 |
astman_append(s, "ActionID: %s\r\n", id); |
| 1787 |
} |
| 1788 |
|
| 1789 |
if (!ast_strlen_zero(mixmonitor_id)) { |
| 1790 |
astman_append(s, "MixMonitorID: %s\r\n", mixmonitor_id); |
| 1791 |
} |
| 1792 |
|
| 1793 |
astman_append(s, "\r\n"); |
| 1794 |
|
| 1795 |
ast_channel_unref(c); |
| 1796 |
|
| 1797 |
return AMI_SUCCESS; |
| 1798 |
} |
| 1799 |
|
| 1800 |
static int manager_stop_mixmonitor(struct mansession *s, const struct message *m) |
| 1801 |
{ |
| 1802 |
struct ast_channel *c; |
| 1803 |
const char *name = astman_get_header(m, "Channel"); |
| 1804 |
const char *id = astman_get_header(m, "ActionID"); |
| 1805 |
const char *mixmonitor_id = astman_get_header(m, "MixMonitorID"); |
| 1806 |
int res; |
| 1807 |
|
| 1808 |
if (ast_strlen_zero(name)) { |
| 1809 |
astman_send_error(s, m, "No channel specified"); |
| 1810 |
return AMI_SUCCESS; |
| 1811 |
} |
| 1812 |
|
| 1813 |
c = ast_channel_get_by_name(name); |
| 1814 |
if (!c) { |
| 1815 |
astman_send_error(s, m, "No such channel"); |
| 1816 |
return AMI_SUCCESS; |
| 1817 |
} |
| 1818 |
|
| 1819 |
res = stop_mixmonitor_full(c, mixmonitor_id); |
| 1820 |
if (res) { |
| 1821 |
ast_channel_unref(c); |
| 1822 |
astman_send_error(s, m, "Could not stop monitoring channel"); |
| 1823 |
return AMI_SUCCESS; |
| 1824 |
} |
| 1825 |
|
| 1826 |
astman_append(s, "Response: Success\r\n"); |
| 1827 |
|
| 1828 |
if (!ast_strlen_zero(id)) { |
| 1829 |
astman_append(s, "ActionID: %s\r\n", id); |
| 1830 |
} |
| 1831 |
|
| 1832 |
astman_append(s, "\r\n"); |
| 1833 |
|
| 1834 |
ast_channel_unref(c); |
| 1835 |
|
| 1836 |
return AMI_SUCCESS; |
| 1837 |
} |
| 1838 |
|
| 1839 |
static int func_mixmonitor_read(struct ast_channel *chan, const char *cmd, char *data, |
| 1840 |
char *buf, size_t len) |
| 1841 |
{ |
| 1842 |
struct ast_datastore *datastore; |
| 1843 |
struct mixmonitor_ds *ds_data; |
| 1844 |
AST_DECLARE_APP_ARGS(args, |
| 1845 |
AST_APP_ARG(id); |
| 1846 |
AST_APP_ARG(key); |
| 1847 |
); |
| 1848 |
|
| 1849 |
AST_STANDARD_APP_ARGS(args, data); |
| 1850 |
|
| 1851 |
if (ast_strlen_zero(args.id) || ast_strlen_zero(args.key)) { |
| 1852 |
ast_log(LOG_WARNING, "Not enough arguments provided to %s. " |
| 1853 |
"An ID and key must be provided\n", cmd); |
| 1854 |
return -1; |
| 1855 |
} |
| 1856 |
|
| 1857 |
ast_channel_lock(chan); |
| 1858 |
datastore = ast_channel_datastore_find(chan, &mixmonitor_ds_info, args.id); |
| 1859 |
ast_channel_unlock(chan); |
| 1860 |
|
| 1861 |
if (!datastore) { |
| 1862 |
ast_log(LOG_WARNING, "Could not find MixMonitor with ID %s\n", args.id); |
| 1863 |
return -1; |
| 1864 |
} |
| 1865 |
|
| 1866 |
ds_data = datastore->data; |
| 1867 |
|
| 1868 |
if (!strcasecmp(args.key, "filename")) { |
| 1869 |
ast_copy_string(buf, ds_data->filename, len); |
| 1870 |
} else { |
| 1871 |
ast_log(LOG_WARNING, "Unrecognized %s option %s\n", cmd, args.key); |
| 1872 |
return -1; |
| 1873 |
} |
| 1874 |
return 0; |
| 1875 |
} |
| 1876 |
|
| 1877 |
static struct ast_custom_function mixmonitor_function = { |
| 1878 |
.name = "MIXMONITOR", |
| 1879 |
.read = func_mixmonitor_read, |
| 1880 |
}; |
| 1881 |
|
| 1882 |
static struct ast_cli_entry cli_mixmonitor[] = { |
| 1883 |
AST_CLI_DEFINE(handle_cli_mixmonitor, "Execute a MixMonitor command") |
| 1884 |
}; |
| 1885 |
|
| 1886 |
static int set_mixmonitor_methods(void) |
| 1887 |
{ |
| 1888 |
struct ast_mixmonitor_methods mixmonitor_methods = { |
| 1889 |
.start = start_mixmonitor_callback, |
| 1890 |
.stop = stop_mixmonitor_callback, |
| 1891 |
}; |
| 1892 |
|
| 1893 |
return ast_set_mixmonitor_methods(&mixmonitor_methods); |
| 1894 |
} |
| 1895 |
|
| 1896 |
static int clear_mixmonitor_methods(void) |
| 1897 |
{ |
| 1898 |
return ast_clear_mixmonitor_methods(); |
| 1899 |
} |
| 1900 |
|
| 1901 |
static int unload_module(void) |
| 1902 |
{ |
| 1903 |
int res; |
| 1904 |
|
| 1905 |
ast_cli_unregister_multiple(cli_mixmonitor, ARRAY_LEN(cli_mixmonitor)); |
| 1906 |
res = ast_unregister_application(stop_app); |
| 1907 |
res |= ast_unregister_application(app); |
| 1908 |
res |= ast_manager_unregister("MixMonitorMute"); |
| 1909 |
res |= ast_manager_unregister("MixMonitor"); |
| 1910 |
res |= ast_manager_unregister("StopMixMonitor"); |
| 1911 |
res |= ast_custom_function_unregister(&mixmonitor_function); |
| 1912 |
res |= clear_mixmonitor_methods(); |
| 1913 |
|
| 1914 |
return res; |
| 1915 |
} |
| 1916 |
|
| 1917 |
static int load_module(void) |
| 1918 |
{ |
| 1919 |
int res; |
| 1920 |
|
| 1921 |
ast_cli_register_multiple(cli_mixmonitor, ARRAY_LEN(cli_mixmonitor)); |
| 1922 |
res = ast_register_application_xml(app, mixmonitor_exec); |
| 1923 |
res |= ast_register_application_xml(stop_app, stop_mixmonitor_exec); |
| 1924 |
res |= ast_manager_register_xml("MixMonitorMute", EVENT_FLAG_SYSTEM | EVENT_FLAG_CALL, manager_mute_mixmonitor); |
| 1925 |
res |= ast_manager_register_xml("MixMonitor", EVENT_FLAG_SYSTEM, manager_mixmonitor); |
| 1926 |
res |= ast_manager_register_xml("StopMixMonitor", EVENT_FLAG_SYSTEM | EVENT_FLAG_CALL, manager_stop_mixmonitor); |
| 1927 |
res |= ast_custom_function_register(&mixmonitor_function); |
| 1928 |
res |= set_mixmonitor_methods(); |
| 1929 |
|
| 1930 |
return res; |
| 1931 |
} |
| 1932 |
|
| 1933 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Mixed Audio Monitoring Application", |
| 1934 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1935 |
.load = load_module, |
| 1936 |
.unload = unload_module, |
| 1937 |
.optional_modules = "func_periodic_hook", |
| 1938 |
); |