| 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 <ctype.h> |
| 33 |
#include <errno.h> |
| 34 |
|
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/audiohook.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
#include "asterisk/utils.h" |
| 39 |
#include "asterisk/pbx.h" |
| 40 |
#include "asterisk/module.h" |
| 41 |
#include "asterisk/lock.h" |
| 42 |
#include "asterisk/options.h" |
| 43 |
#include "asterisk/autochan.h" |
| 44 |
#include "asterisk/format_cache.h" |
| 45 |
#include "asterisk/cli.h" |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
static const char app_broadcast[] = "Broadcast"; |
| 134 |
|
| 135 |
enum { |
| 136 |
OPTION_READONLY = (1 << 0), |
| 137 |
OPTION_BARGE = (1 << 1), |
| 138 |
OPTION_LONG_QUEUE = (1 << 2), |
| 139 |
OPTION_WHISPER = (1 << 3), |
| 140 |
OPTION_SPY = (1 << 4), |
| 141 |
OPTION_REVERSE_FEED = (1 << 5), |
| 142 |
OPTION_ANSWER_WARN = (1 << 6), |
| 143 |
}; |
| 144 |
|
| 145 |
AST_APP_OPTIONS(spy_opts, { |
| 146 |
AST_APP_OPTION('b', OPTION_BARGE), |
| 147 |
AST_APP_OPTION('l', OPTION_LONG_QUEUE), |
| 148 |
AST_APP_OPTION('o', OPTION_READONLY), |
| 149 |
AST_APP_OPTION('r', OPTION_REVERSE_FEED), |
| 150 |
AST_APP_OPTION('s', OPTION_SPY), |
| 151 |
AST_APP_OPTION('w', OPTION_WHISPER), |
| 152 |
}); |
| 153 |
|
| 154 |
struct multi_autochan { |
| 155 |
char *name; |
| 156 |
struct ast_autochan *autochan; |
| 157 |
struct ast_autochan *bridge_autochan; |
| 158 |
struct ast_audiohook whisper_audiohook; |
| 159 |
struct ast_audiohook bridge_whisper_audiohook; |
| 160 |
struct ast_audiohook spy_audiohook; |
| 161 |
unsigned int connected:1; |
| 162 |
unsigned int bridge_connected:1; |
| 163 |
unsigned int spying:1; |
| 164 |
AST_LIST_ENTRY(multi_autochan) entry; |
| 165 |
}; |
| 166 |
|
| 167 |
AST_RWLIST_HEAD(multi_autochan_list, multi_autochan); |
| 168 |
|
| 169 |
struct multi_spy { |
| 170 |
struct multi_autochan_list *chanlist; |
| 171 |
unsigned int readonly:1; |
| 172 |
}; |
| 173 |
|
| 174 |
static void *spy_alloc(struct ast_channel *chan, void *data) |
| 175 |
{ |
| 176 |
return data; |
| 177 |
} |
| 178 |
|
| 179 |
static void spy_release(struct ast_channel *chan, void *data) |
| 180 |
{ |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
static int spy_generate(struct ast_channel *chan, void *data, int len, int samples) |
| 185 |
{ |
| 186 |
struct multi_spy *multispy = data; |
| 187 |
struct multi_autochan_list *chanlist = multispy->chanlist; |
| 188 |
struct multi_autochan *mac; |
| 189 |
struct ast_frame *f; |
| 190 |
short *data1, *data2; |
| 191 |
int res, i; |
| 192 |
|
| 193 |
|
| 194 |
static const int num_samples = 160; |
| 195 |
short combine_buf[num_samples]; |
| 196 |
struct ast_frame wf = { |
| 197 |
.frametype = AST_FRAME_VOICE, |
| 198 |
.offset = 0, |
| 199 |
.subclass.format = ast_format_slin, |
| 200 |
.datalen = num_samples * 2, |
| 201 |
.samples = num_samples, |
| 202 |
.src = __FUNCTION__, |
| 203 |
}; |
| 204 |
|
| 205 |
memset(&combine_buf, 0, sizeof(combine_buf)); |
| 206 |
wf.data.ptr = combine_buf; |
| 207 |
|
| 208 |
AST_RWLIST_WRLOCK(chanlist); |
| 209 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(chanlist, mac, entry) { |
| 210 |
ast_audiohook_lock(&mac->spy_audiohook); |
| 211 |
if (mac->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 212 |
ast_audiohook_unlock(&mac->spy_audiohook); |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
if (multispy->readonly) { |
| 217 |
f = ast_audiohook_read_frame(&mac->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_READ, ast_format_slin); |
| 218 |
} else { |
| 219 |
f = ast_audiohook_read_frame(&mac->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, ast_format_slin); |
| 220 |
} |
| 221 |
ast_audiohook_unlock(&mac->spy_audiohook); |
| 222 |
|
| 223 |
if (!f) { |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
for (i = 0, data1 = combine_buf, data2 = f->data.ptr; i < num_samples; i++, data1++, data2++) { |
| 229 |
ast_slinear_saturated_add(data1, data2); |
| 230 |
} |
| 231 |
ast_frfree(f); |
| 232 |
} |
| 233 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 234 |
AST_RWLIST_UNLOCK(chanlist); |
| 235 |
|
| 236 |
res = ast_write(chan, &wf); |
| 237 |
ast_frfree(&wf); |
| 238 |
|
| 239 |
return res; |
| 240 |
} |
| 241 |
|
| 242 |
static struct ast_generator spygen = { |
| 243 |
.alloc = spy_alloc, |
| 244 |
.release = spy_release, |
| 245 |
.generate = spy_generate, |
| 246 |
}; |
| 247 |
|
| 248 |
static int start_spying(struct ast_autochan *autochan, const char *spychan_name, struct ast_audiohook *audiohook, struct ast_flags *flags) |
| 249 |
{ |
| 250 |
int res; |
| 251 |
|
| 252 |
ast_autochan_channel_lock(autochan); |
| 253 |
ast_debug(1, "Attaching spy channel %s to %s\n", spychan_name, ast_channel_name(autochan->chan)); |
| 254 |
|
| 255 |
if (ast_test_flag(flags, OPTION_READONLY)) { |
| 256 |
ast_audiohook_set_frame_feed_direction(audiohook, AST_AUDIOHOOK_DIRECTION_READ); |
| 257 |
ast_set_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE); |
| 258 |
} else { |
| 259 |
ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC); |
| 260 |
} |
| 261 |
if (ast_test_flag(flags, OPTION_LONG_QUEUE)) { |
| 262 |
ast_debug(2, "Using a long queue to store audio frames in spy audiohook\n"); |
| 263 |
} else { |
| 264 |
ast_set_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE); |
| 265 |
} |
| 266 |
res = ast_audiohook_attach(autochan->chan, audiohook); |
| 267 |
ast_autochan_channel_unlock(autochan); |
| 268 |
return res; |
| 269 |
} |
| 270 |
|
| 271 |
static int start_whispering(struct ast_autochan *autochan, const char *spychan_name, struct ast_audiohook *audiohook, struct ast_flags *flags) |
| 272 |
{ |
| 273 |
int res; |
| 274 |
|
| 275 |
ast_autochan_channel_lock(autochan); |
| 276 |
ast_verb(3, "Attaching spy channel %s to %s\n", |
| 277 |
spychan_name, ast_channel_name(autochan->chan)); |
| 278 |
|
| 279 |
ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC); |
| 280 |
if (ast_test_flag(flags, OPTION_LONG_QUEUE)) { |
| 281 |
ast_debug(9, "Using a long queue to store audio frames in whisper audiohook\n"); |
| 282 |
} else { |
| 283 |
ast_set_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE); |
| 284 |
} |
| 285 |
res = ast_audiohook_attach(autochan->chan, audiohook); |
| 286 |
ast_autochan_channel_unlock(autochan); |
| 287 |
return res; |
| 288 |
} |
| 289 |
|
| 290 |
static int attach_barge(struct ast_autochan *spyee_autochan, struct ast_autochan **spyee_bridge_autochan, |
| 291 |
struct ast_audiohook *bridge_whisper_audiohook, const char *spyer_name, const char *name, struct ast_flags *flags) |
| 292 |
{ |
| 293 |
int retval = 0; |
| 294 |
struct ast_autochan *internal_bridge_autochan; |
| 295 |
struct ast_channel *spyee_chan; |
| 296 |
RAII_VAR(struct ast_channel *, bridged, NULL, ast_channel_cleanup); |
| 297 |
|
| 298 |
ast_autochan_channel_lock(spyee_autochan); |
| 299 |
spyee_chan = ast_channel_ref(spyee_autochan->chan); |
| 300 |
ast_autochan_channel_unlock(spyee_autochan); |
| 301 |
|
| 302 |
|
| 303 |
bridged = ast_channel_bridge_peer(spyee_chan); |
| 304 |
ast_channel_unref(spyee_chan); |
| 305 |
if (!bridged) { |
| 306 |
ast_debug(9, "Channel %s is not yet bridged, unable to setup barge\n", ast_channel_name(spyee_chan)); |
| 307 |
|
| 308 |
if (ast_test_flag(flags, OPTION_ANSWER_WARN) && ast_channel_is_bridged(spyee_chan)) { |
| 309 |
ast_clear_flag(flags, OPTION_ANSWER_WARN); |
| 310 |
ast_log(LOG_WARNING, "Barge failed: channel is bridged, but not to a 2-party bridge. Use the 'r' option.\n"); |
| 311 |
} |
| 312 |
return -1; |
| 313 |
} |
| 314 |
|
| 315 |
ast_audiohook_init(bridge_whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "Broadcast", 0); |
| 316 |
internal_bridge_autochan = ast_autochan_setup(bridged); |
| 317 |
if (!internal_bridge_autochan) { |
| 318 |
return -1; |
| 319 |
} |
| 320 |
|
| 321 |
if (start_whispering(internal_bridge_autochan, spyer_name, bridge_whisper_audiohook, flags)) { |
| 322 |
ast_log(LOG_WARNING, "Unable to attach barge audiohook on spyee '%s'. Barge mode disabled.\n", name); |
| 323 |
retval = -1; |
| 324 |
} |
| 325 |
|
| 326 |
*spyee_bridge_autochan = internal_bridge_autochan; |
| 327 |
return retval; |
| 328 |
} |
| 329 |
|
| 330 |
static void multi_autochan_free(struct multi_autochan *mac) |
| 331 |
{ |
| 332 |
if (mac->connected) { |
| 333 |
if (mac->whisper_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 334 |
ast_debug(2, "Whisper audiohook no longer running\n"); |
| 335 |
} |
| 336 |
ast_audiohook_lock(&mac->whisper_audiohook); |
| 337 |
ast_audiohook_detach(&mac->whisper_audiohook); |
| 338 |
ast_audiohook_unlock(&mac->whisper_audiohook); |
| 339 |
ast_audiohook_destroy(&mac->whisper_audiohook); |
| 340 |
} |
| 341 |
if (mac->bridge_connected) { |
| 342 |
if (mac->bridge_whisper_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 343 |
ast_debug(2, "Whisper (bridged) audiohook no longer running\n"); |
| 344 |
} |
| 345 |
ast_audiohook_lock(&mac->bridge_whisper_audiohook); |
| 346 |
ast_audiohook_detach(&mac->bridge_whisper_audiohook); |
| 347 |
ast_audiohook_unlock(&mac->bridge_whisper_audiohook); |
| 348 |
ast_audiohook_destroy(&mac->bridge_whisper_audiohook); |
| 349 |
} |
| 350 |
if (mac->spying) { |
| 351 |
if (mac->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 352 |
ast_debug(2, "Spy audiohook no longer running\n"); |
| 353 |
} |
| 354 |
ast_audiohook_lock(&mac->spy_audiohook); |
| 355 |
ast_audiohook_detach(&mac->spy_audiohook); |
| 356 |
ast_audiohook_unlock(&mac->spy_audiohook); |
| 357 |
ast_audiohook_destroy(&mac->spy_audiohook); |
| 358 |
} |
| 359 |
if (mac->name) { |
| 360 |
int total = mac->connected + mac->bridge_connected + mac->spying; |
| 361 |
ast_debug(1, "Removing channel %s from target list (%d hook%s)\n", mac->name, total, ESS(total)); |
| 362 |
ast_free(mac->name); |
| 363 |
} |
| 364 |
if (mac->autochan) { |
| 365 |
ast_autochan_destroy(mac->autochan); |
| 366 |
} |
| 367 |
if (mac->bridge_autochan) { |
| 368 |
ast_autochan_destroy(mac->bridge_autochan); |
| 369 |
} |
| 370 |
ast_free(mac); |
| 371 |
} |
| 372 |
|
| 373 |
static int do_broadcast(struct ast_channel *chan, struct ast_flags *flags, const char *channels) |
| 374 |
{ |
| 375 |
int res = 0; |
| 376 |
struct ast_frame *f; |
| 377 |
struct ast_silence_generator *silgen = NULL; |
| 378 |
struct multi_spy multispy; |
| 379 |
struct multi_autochan_list chanlist; |
| 380 |
struct multi_autochan *mac; |
| 381 |
int numchans = 0; |
| 382 |
int readonly = ast_test_flag(flags, OPTION_READONLY) ? 1 : 0; |
| 383 |
char *next, *chansdup = ast_strdupa(channels); |
| 384 |
|
| 385 |
AST_RWLIST_HEAD_INIT(&chanlist); |
| 386 |
ast_channel_set_flag(chan, AST_FLAG_SPYING); |
| 387 |
|
| 388 |
ast_set_flag(flags, OPTION_ANSWER_WARN); |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
while ((next = strsep(&chansdup, ","))) { |
| 394 |
struct ast_channel *ochan; |
| 395 |
if (ast_strlen_zero(next)) { |
| 396 |
continue; |
| 397 |
} |
| 398 |
if (!strcmp(next, ast_channel_name(chan))) { |
| 399 |
ast_log(LOG_WARNING, "Refusing to broadcast to ourself: %s\n", next); |
| 400 |
continue; |
| 401 |
} |
| 402 |
ochan = ast_channel_get_by_name(next); |
| 403 |
if (!ochan) { |
| 404 |
ast_log(LOG_WARNING, "No such channel: %s\n", next); |
| 405 |
continue; |
| 406 |
} |
| 407 |
|
| 408 |
if (!(mac = ast_calloc(1, sizeof(*mac)))) { |
| 409 |
ast_log(LOG_WARNING, "Multi autochan allocation failure\n"); |
| 410 |
continue; |
| 411 |
} |
| 412 |
mac->name = ast_strdup(next); |
| 413 |
mac->autochan = ast_autochan_setup(ochan); |
| 414 |
if (!mac->name || !mac->autochan) { |
| 415 |
multi_autochan_free(mac); |
| 416 |
continue; |
| 417 |
} |
| 418 |
if (ast_test_flag(flags, OPTION_WHISPER)) { |
| 419 |
mac->connected = 1; |
| 420 |
ast_audiohook_init(&mac->whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "Broadcast", 0); |
| 421 |
|
| 422 |
if (start_whispering(mac->autochan, next, &mac->whisper_audiohook, flags)) { |
| 423 |
ast_log(LOG_WARNING, "Unable to attach whisper audiohook to %s\n", next); |
| 424 |
multi_autochan_free(mac); |
| 425 |
continue; |
| 426 |
} |
| 427 |
} |
| 428 |
if (ast_test_flag(flags, OPTION_SPY)) { |
| 429 |
mac->spying = 1; |
| 430 |
ast_audiohook_init(&mac->spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "Broadcast", 0); |
| 431 |
if (start_spying(mac->autochan, next, &mac->spy_audiohook, flags)) { |
| 432 |
ast_log(LOG_WARNING, "Unable to attach spy audiohook to %s\n", next); |
| 433 |
multi_autochan_free(mac); |
| 434 |
continue; |
| 435 |
} |
| 436 |
} |
| 437 |
AST_RWLIST_INSERT_TAIL(&chanlist, mac, entry); |
| 438 |
numchans++; |
| 439 |
ochan = ast_channel_unref(ochan); |
| 440 |
} |
| 441 |
|
| 442 |
ast_verb(4, "Broadcasting to %d channel%s on %s\n", numchans, ESS(numchans), ast_channel_name(chan)); |
| 443 |
ast_debug(1, "Broadcasting: (TX->1) whisper=%d, (TX->2) barge=%d, (RX<-%d) spy=%d (%s)\n", |
| 444 |
ast_test_flag(flags, OPTION_WHISPER) ? 1 : 0, |
| 445 |
ast_test_flag(flags, OPTION_BARGE) ? 1 : 0, |
| 446 |
readonly ? 1 : 2, |
| 447 |
ast_test_flag(flags, OPTION_SPY) ? 1 : 0, |
| 448 |
readonly ? "single" : "both"); |
| 449 |
|
| 450 |
if (ast_test_flag(flags, OPTION_SPY)) { |
| 451 |
multispy.chanlist = &chanlist; |
| 452 |
multispy.readonly = readonly; |
| 453 |
ast_activate_generator(chan, &spygen, &multispy); |
| 454 |
} else { |
| 455 |
|
| 456 |
silgen = ast_channel_start_silence_generator(chan); |
| 457 |
} |
| 458 |
|
| 459 |
while (numchans && ast_waitfor(chan, -1) > 0) { |
| 460 |
int fres = 0; |
| 461 |
f = ast_read(chan); |
| 462 |
if (!f) { |
| 463 |
ast_debug(1, "Channel %s must have hung up\n", ast_channel_name(chan)); |
| 464 |
res = -1; |
| 465 |
break; |
| 466 |
} |
| 467 |
if (f->frametype != AST_FRAME_VOICE) { |
| 468 |
ast_frfree(f); |
| 469 |
continue; |
| 470 |
} |
| 471 |
|
| 472 |
AST_RWLIST_WRLOCK(&chanlist); |
| 473 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&chanlist, mac, entry) { |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
if ((ast_test_flag(flags, OPTION_WHISPER) && mac->whisper_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) |
| 479 |
|| (ast_test_flag(flags, OPTION_SPY) && mac->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) |
| 480 |
|| (mac->bridge_connected && ast_test_flag(flags, OPTION_BARGE) && mac->bridge_whisper_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING)) { |
| 481 |
|
| 482 |
AST_RWLIST_REMOVE_CURRENT(entry); |
| 483 |
ast_debug(2, "Looks like %s has hung up\n", mac->name); |
| 484 |
multi_autochan_free(mac); |
| 485 |
numchans--; |
| 486 |
ast_debug(2, "%d channel%s remaining in broadcast on %s\n", numchans, ESS(numchans), ast_channel_name(chan)); |
| 487 |
continue; |
| 488 |
} |
| 489 |
|
| 490 |
if (ast_test_flag(flags, OPTION_WHISPER)) { |
| 491 |
ast_audiohook_lock(&mac->whisper_audiohook); |
| 492 |
fres |= ast_audiohook_write_frame(&mac->whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); |
| 493 |
ast_audiohook_unlock(&mac->whisper_audiohook); |
| 494 |
} |
| 495 |
|
| 496 |
if (ast_test_flag(flags, OPTION_BARGE)) { |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
if (!ast_test_flag(flags, OPTION_REVERSE_FEED) && !mac->bridge_connected && !attach_barge(mac->autochan, &mac->bridge_autochan, |
| 505 |
&mac->bridge_whisper_audiohook, ast_channel_name(chan), mac->name, flags)) { |
| 506 |
ast_debug(2, "Attached barge channel for %s\n", mac->name); |
| 507 |
mac->bridge_connected = 1; |
| 508 |
} |
| 509 |
|
| 510 |
if (mac->bridge_connected) { |
| 511 |
ast_audiohook_lock(&mac->bridge_whisper_audiohook); |
| 512 |
fres |= ast_audiohook_write_frame(&mac->bridge_whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); |
| 513 |
ast_audiohook_unlock(&mac->bridge_whisper_audiohook); |
| 514 |
} else if (ast_test_flag(flags, OPTION_REVERSE_FEED)) { |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
ast_audiohook_lock(&mac->whisper_audiohook); |
| 546 |
fres |= ast_audiohook_write_frame(&mac->whisper_audiohook, AST_AUDIOHOOK_DIRECTION_READ, f); |
| 547 |
ast_audiohook_unlock(&mac->whisper_audiohook); |
| 548 |
} |
| 549 |
} |
| 550 |
if (fres) { |
| 551 |
ast_log(LOG_WARNING, "Failed to write to audiohook for %s\n", mac->name); |
| 552 |
fres = 0; |
| 553 |
} |
| 554 |
} |
| 555 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 556 |
AST_RWLIST_UNLOCK(&chanlist); |
| 557 |
ast_frfree(f); |
| 558 |
} |
| 559 |
|
| 560 |
if (!numchans) { |
| 561 |
ast_debug(1, "Exiting due to all target channels having left the broadcast\n"); |
| 562 |
} |
| 563 |
|
| 564 |
if (ast_test_flag(flags, OPTION_SPY)) { |
| 565 |
ast_deactivate_generator(chan); |
| 566 |
} else { |
| 567 |
ast_channel_stop_silence_generator(chan, silgen); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&chanlist, mac, entry) { |
| 572 |
AST_RWLIST_REMOVE_CURRENT(entry); |
| 573 |
multi_autochan_free(mac); |
| 574 |
} |
| 575 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 576 |
|
| 577 |
ast_channel_clear_flag(chan, AST_FLAG_SPYING); |
| 578 |
return res; |
| 579 |
} |
| 580 |
|
| 581 |
static int broadcast_exec(struct ast_channel *chan, const char *data) |
| 582 |
{ |
| 583 |
struct ast_flags flags; |
| 584 |
struct ast_format *write_format; |
| 585 |
int res = -1; |
| 586 |
AST_DECLARE_APP_ARGS(args, |
| 587 |
AST_APP_ARG(options); |
| 588 |
AST_APP_ARG(channels); |
| 589 |
); |
| 590 |
char *parse = NULL; |
| 591 |
|
| 592 |
if (ast_strlen_zero(data)) { |
| 593 |
ast_log(LOG_WARNING, "Broadcast requires at least one channel\n"); |
| 594 |
return -1; |
| 595 |
} |
| 596 |
|
| 597 |
parse = ast_strdupa(data); |
| 598 |
AST_STANDARD_APP_ARGS(args, parse); |
| 599 |
|
| 600 |
if (ast_strlen_zero(args.channels)) { |
| 601 |
ast_log(LOG_WARNING, "Must specify at least one channel for broadcast\n"); |
| 602 |
return -1; |
| 603 |
} |
| 604 |
if (args.options) { |
| 605 |
ast_app_parse_options(spy_opts, &flags, NULL, args.options); |
| 606 |
} else { |
| 607 |
ast_clear_flag(&flags, AST_FLAGS_ALL); |
| 608 |
} |
| 609 |
|
| 610 |
if (!ast_test_flag(&flags, OPTION_BARGE) && !ast_test_flag(&flags, OPTION_SPY) && !ast_test_flag(&flags, OPTION_WHISPER)) { |
| 611 |
ast_log(LOG_WARNING, "At least one of the b, s, or w option must be specified (provided options have no effect)\n"); |
| 612 |
return -1; |
| 613 |
} |
| 614 |
|
| 615 |
write_format = ao2_bump(ast_channel_writeformat(chan)); |
| 616 |
if (ast_set_write_format(chan, ast_format_slin) < 0) { |
| 617 |
ast_log(LOG_ERROR, "Failed to set write format to slin.\n"); |
| 618 |
goto cleanup; |
| 619 |
} |
| 620 |
|
| 621 |
res = do_broadcast(chan, &flags, args.channels); |
| 622 |
|
| 623 |
|
| 624 |
if (ast_set_write_format(chan, write_format)) { |
| 625 |
ast_log(LOG_ERROR, "Failed to restore write format for channel %s\n", ast_channel_name(chan)); |
| 626 |
} |
| 627 |
|
| 628 |
cleanup: |
| 629 |
ao2_ref(write_format, -1); |
| 630 |
return res; |
| 631 |
} |
| 632 |
|
| 633 |
static int unload_module(void) |
| 634 |
{ |
| 635 |
return ast_unregister_application(app_broadcast); |
| 636 |
} |
| 637 |
|
| 638 |
static int load_module(void) |
| 639 |
{ |
| 640 |
return ast_register_application_xml(app_broadcast, broadcast_exec); |
| 641 |
} |
| 642 |
|
| 643 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Channel Audio Broadcasting"); |