2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2007, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Audiohooks Architecture
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/channel.h"
37 #include "asterisk/utils.h"
38 #include "asterisk/lock.h"
39 #include "asterisk/linkedlists.h"
40 #include "asterisk/audiohook.h"
41 #include "asterisk/slinfactory.h"
42 #include "asterisk/frame.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/format_cache.h"
46 #define AST_AUDIOHOOK_SYNC_TOLERANCE 100 /*!< Tolerance in milliseconds for audiohooks synchronization */
47 #define AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE 100 /*!< When small queue is enabled, this is the maximum amount of audio that can remain queued at a time. */
49 struct ast_audiohook_translate {
50 struct ast_trans_pvt *trans_pvt;
51 struct ast_format *format;
54 struct ast_audiohook_list {
55 /* If all the audiohooks in this list are capable
56 * of processing slinear at any sample rate, this
57 * variable will be set and the sample rate will
58 * be preserved during ast_audiohook_write_list()*/
59 int native_slin_compatible;
60 int list_internal_samp_rate;/*!< Internal sample rate used when writing to the audiohook list */
62 struct ast_audiohook_translate in_translate[2];
63 struct ast_audiohook_translate out_translate[2];
64 AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
65 AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
66 AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
69 static int audiohook_set_internal_rate(struct ast_audiohook *audiohook, int rate, int reset)
71 struct ast_format *slin;
73 if (audiohook->hook_internal_samp_rate == rate) {
77 audiohook->hook_internal_samp_rate = rate;
79 slin = ast_format_cache_get_slin_by_rate(rate);
81 /* Setup the factories that are needed for this audiohook type */
82 switch (audiohook->type) {
83 case AST_AUDIOHOOK_TYPE_SPY:
84 case AST_AUDIOHOOK_TYPE_WHISPER:
86 ast_slinfactory_destroy(&audiohook->read_factory);
87 ast_slinfactory_destroy(&audiohook->write_factory);
89 ast_slinfactory_init_with_format(&audiohook->read_factory, slin);
90 ast_slinfactory_init_with_format(&audiohook->write_factory, slin);
99 /*! \brief Initialize an audiohook structure
101 * \param audiohook Audiohook structure
103 * \param source, init_flags
105 * \return Returns 0 on success, -1 on failure
107 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags init_flags)
109 /* Need to keep the type and source */
110 audiohook->type = type;
111 audiohook->source = source;
113 /* Initialize lock that protects our audiohook */
114 ast_mutex_init(&audiohook->lock);
115 ast_cond_init(&audiohook->trigger, NULL);
117 audiohook->init_flags = init_flags;
119 /* initialize internal rate at 8khz, this will adjust if necessary */
120 audiohook_set_internal_rate(audiohook, 8000, 0);
122 /* Since we are just starting out... this audiohook is new */
123 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_NEW);
128 /*! \brief Destroys an audiohook structure
129 * \param audiohook Audiohook structure
130 * \return Returns 0 on success, -1 on failure
132 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
134 /* Drop the factories used by this audiohook type */
135 switch (audiohook->type) {
136 case AST_AUDIOHOOK_TYPE_SPY:
137 case AST_AUDIOHOOK_TYPE_WHISPER:
138 ast_slinfactory_destroy(&audiohook->read_factory);
139 ast_slinfactory_destroy(&audiohook->write_factory);
145 /* Destroy translation path if present */
146 if (audiohook->trans_pvt)
147 ast_translator_free_path(audiohook->trans_pvt);
149 ao2_cleanup(audiohook->format);
151 /* Lock and trigger be gone! */
152 ast_cond_destroy(&audiohook->trigger);
153 ast_mutex_destroy(&audiohook->lock);
158 /*! \brief Writes a frame into the audiohook structure
159 * \param audiohook Audiohook structure
160 * \param direction Direction the audio frame came from
161 * \param frame Frame to write in
162 * \return Returns 0 on success, -1 on failure
164 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
166 struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
167 struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory);
168 struct timeval *rwtime = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *rwtime;
169 int our_factory_samples;
171 int other_factory_samples;
172 int other_factory_ms;
175 /* Update last feeding time to be current */
176 *rwtime = ast_tvnow();
178 our_factory_samples = ast_slinfactory_available(factory);
179 our_factory_ms = ast_tvdiff_ms(*rwtime, previous_time) + (our_factory_samples / (audiohook->hook_internal_samp_rate / 1000));
180 other_factory_samples = ast_slinfactory_available(other_factory);
181 other_factory_ms = other_factory_samples / (audiohook->hook_internal_samp_rate / 1000);
183 if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && other_factory_samples && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) {
184 ast_debug(1, "Flushing audiohook %p so it remains in sync\n", audiohook);
185 ast_slinfactory_flush(factory);
186 ast_slinfactory_flush(other_factory);
189 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))) {
190 ast_debug(1, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
191 ast_slinfactory_flush(factory);
192 ast_slinfactory_flush(other_factory);
195 /* swap frame data for zeros if mute is required */
196 if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) ||
197 (ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) ||
198 (ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE) == (AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE))) {
202 if (muteme && frame->datalen > 0) {
203 ast_frame_clear(frame);
206 /* Write frame out to respective factory */
207 ast_slinfactory_feed(factory, frame);
209 /* If we need to notify the respective handler of this audiohook, do so */
210 if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
211 ast_cond_signal(&audiohook->trigger);
212 } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
213 ast_cond_signal(&audiohook->trigger);
214 } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
215 ast_cond_signal(&audiohook->trigger);
221 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
223 struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
224 int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
226 struct ast_frame frame = {
227 .frametype = AST_FRAME_VOICE,
228 .subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate),
230 .datalen = sizeof(buf),
234 /* Ensure the factory is able to give us the samples we want */
235 if (samples > ast_slinfactory_available(factory)) {
239 /* Read data in from factory */
240 if (!ast_slinfactory_read(factory, buf, samples)) {
244 /* If a volume adjustment needs to be applied apply it */
246 ast_frame_adjust_volume(&frame, vol);
249 return ast_frdup(&frame);
252 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)
254 int i = 0, usable_read, usable_write;
255 short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
256 struct ast_frame frame = {
257 .frametype = AST_FRAME_VOICE,
259 .datalen = sizeof(buf1),
263 /* Make sure both factories have the required samples */
264 usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
265 usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
267 if (!usable_read && !usable_write) {
268 /* If both factories are unusable bail out */
269 ast_debug(1, "Read factory %p and write factory %p both fail to provide %zu samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
273 /* If we want to provide only a read factory make sure we aren't waiting for other audio */
274 if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
275 ast_debug(3, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
279 /* If we want to provide only a write factory make sure we aren't waiting for other audio */
280 if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*2)) {
281 ast_debug(3, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
285 /* Start with the read factory... if there are enough samples, read them in */
287 if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
289 /* Adjust read volume if need be */
290 if (audiohook->options.read_volume) {
292 short adjust_value = abs(audiohook->options.read_volume);
293 for (count = 0; count < samples; count++) {
294 if (audiohook->options.read_volume > 0) {
295 ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
296 } else if (audiohook->options.read_volume < 0) {
297 ast_slinear_saturated_divide(&buf1[count], &adjust_value);
303 ast_debug(1, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory);
306 /* Move on to the write factory... if there are enough samples, read them in */
308 if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
310 /* Adjust write volume if need be */
311 if (audiohook->options.write_volume) {
313 short adjust_value = abs(audiohook->options.write_volume);
314 for (count = 0; count < samples; count++) {
315 if (audiohook->options.write_volume > 0) {
316 ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
317 } else if (audiohook->options.write_volume < 0) {
318 ast_slinear_saturated_divide(&buf2[count], &adjust_value);
324 ast_debug(1, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory);
327 /* Basically we figure out which buffer to use... and if mixing can be done here */
328 if (read_buf && read_reference) {
329 frame.data.ptr = buf1;
330 *read_reference = ast_frdup(&frame);
332 if (write_buf && write_reference) {
333 frame.data.ptr = buf2;
334 *write_reference = ast_frdup(&frame);
337 if (read_buf && write_buf) {
338 for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++) {
339 ast_slinear_saturated_add(data1, data2);
342 } else if (read_buf) {
344 } else if (write_buf) {
350 /* Make the final buffer part of the frame, so it gets duplicated fine */
351 frame.data.ptr = final_buf;
353 frame.subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate);
355 /* Yahoo, a combined copy of the audio! */
356 return ast_frdup(&frame);
359 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)
361 struct ast_frame *read_frame = NULL, *final_frame = NULL;
362 struct ast_format *slin;
363 int samples_converted;
365 /* the number of samples requested is based on the format they are requesting. Inorder
366 * to process this correctly samples must be converted to our internal sample rate */
367 if (audiohook->hook_internal_samp_rate == ast_format_get_sample_rate(format)) {
368 samples_converted = samples;
369 } else if (audiohook->hook_internal_samp_rate > ast_format_get_sample_rate(format)) {
370 samples_converted = samples * (audiohook->hook_internal_samp_rate / (float) ast_format_get_sample_rate(format));
372 samples_converted = samples * (ast_format_get_sample_rate(format) / (float) audiohook->hook_internal_samp_rate);
375 if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ?
376 audiohook_read_frame_both(audiohook, samples_converted, read_reference, write_reference) :
377 audiohook_read_frame_single(audiohook, samples_converted, direction)))) {
381 slin = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate);
383 /* If they don't want signed linear back out, we'll have to send it through the translation path */
384 if (ast_format_cmp(format, slin) != AST_FORMAT_CMP_EQUAL) {
385 /* Rebuild translation path if different format then previously */
386 if (ast_format_cmp(format, audiohook->format) == AST_FORMAT_CMP_NOT_EQUAL) {
387 if (audiohook->trans_pvt) {
388 ast_translator_free_path(audiohook->trans_pvt);
389 audiohook->trans_pvt = NULL;
392 /* Setup new translation path for this format... if we fail we can't very well return signed linear so free the frame and return nothing */
393 if (!(audiohook->trans_pvt = ast_translator_build_path(format, slin))) {
394 ast_frfree(read_frame);
397 ao2_replace(audiohook->format, format);
399 /* Convert to requested format, and allow the read in frame to be freed */
400 final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
402 final_frame = read_frame;
408 /*! \brief Reads a frame in from the audiohook structure
409 * \param audiohook Audiohook structure
410 * \param samples Number of samples wanted in requested output format
411 * \param direction Direction the audio frame came from
412 * \param format Format of frame remote side wants back
413 * \return Returns frame on success, NULL on failure
415 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format)
417 return audiohook_read_frame_helper(audiohook, samples, direction, format, NULL, NULL);
420 /*! \brief Reads a frame in from the audiohook structure
421 * \param audiohook Audiohook structure
422 * \param samples Number of samples wanted
423 * \param direction Direction the audio frame came from
424 * \param format Format of frame remote side wants back
425 * \param read_frame frame pointer for copying read frame data
426 * \param write_frame frame pointer for copying write frame data
427 * \return Returns frame on success, NULL on failure
429 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)
431 return audiohook_read_frame_helper(audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, format, read_frame, write_frame);
434 static void audiohook_list_set_samplerate_compatibility(struct ast_audiohook_list *audiohook_list)
436 struct ast_audiohook *ah = NULL;
437 audiohook_list->native_slin_compatible = 1;
438 AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, ah, list) {
439 if (!(ah->init_flags & AST_AUDIOHOOK_MANIPULATE_ALL_RATES)) {
440 audiohook_list->native_slin_compatible = 0;
446 /*! \brief Attach audiohook to channel
447 * \param chan Channel
448 * \param audiohook Audiohook structure
449 * \return Returns 0 on success, -1 on failure
451 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
453 ast_channel_lock(chan);
455 if (!ast_channel_audiohooks(chan)) {
456 struct ast_audiohook_list *ahlist;
457 /* Whoops... allocate a new structure */
458 if (!(ahlist = ast_calloc(1, sizeof(*ahlist)))) {
459 ast_channel_unlock(chan);
462 ast_channel_audiohooks_set(chan, ahlist);
463 AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->spy_list);
464 AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->whisper_list);
465 AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->manipulate_list);
466 /* This sample rate will adjust as necessary when writing to the list. */
467 ast_channel_audiohooks(chan)->list_internal_samp_rate = 8000;
470 /* Drop into respective list */
471 if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
472 AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
473 } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
474 AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
475 } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
476 AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
480 audiohook_set_internal_rate(audiohook, ast_channel_audiohooks(chan)->list_internal_samp_rate, 1);
481 audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
483 /* Change status over to running since it is now attached */
484 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_RUNNING);
486 if (ast_channel_is_bridged(chan)) {
487 ast_channel_set_unbridged_nolock(chan, 1);
490 ast_channel_unlock(chan);
495 /*! \brief Update audiohook's status
496 * \param audiohook Audiohook structure
497 * \param status Audiohook status enum
499 * \note once status is updated to DONE, this function can not be used to set the
500 * status back to any other setting. Setting DONE effectively locks the status as such.
503 void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
505 ast_audiohook_lock(audiohook);
506 if (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
507 audiohook->status = status;
508 ast_cond_signal(&audiohook->trigger);
510 ast_audiohook_unlock(audiohook);
513 /*! \brief Detach audiohook from channel
514 * \param audiohook Audiohook structure
515 * \return Returns 0 on success, -1 on failure
517 int ast_audiohook_detach(struct ast_audiohook *audiohook)
519 if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
523 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
525 while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
526 ast_audiohook_trigger_wait(audiohook);
532 void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
535 struct ast_audiohook *audiohook;
537 if (!audiohook_list) {
542 while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
543 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
546 /* Drop any whispering sources */
547 while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
548 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
551 /* Drop any manipulaters */
552 while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
553 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
554 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
557 /* Drop translation paths if present */
558 for (i = 0; i < 2; i++) {
559 if (audiohook_list->in_translate[i].trans_pvt) {
560 ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
562 if (audiohook_list->out_translate[i].trans_pvt) {
563 ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
568 ast_free(audiohook_list);
571 /*! \brief find an audiohook based on its source
572 * \param audiohook_list The list of audiohooks to search in
573 * \param source The source of the audiohook we wish to find
574 * \return Return the corresponding audiohook or NULL if it cannot be found.
576 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
578 struct ast_audiohook *audiohook = NULL;
580 AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
581 if (!strcasecmp(audiohook->source, source)) {
586 AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
587 if (!strcasecmp(audiohook->source, source)) {
592 AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
593 if (!strcasecmp(audiohook->source, source)) {
601 static void audiohook_move(struct ast_channel *old_chan, struct ast_channel *new_chan, struct ast_audiohook *audiohook)
603 enum ast_audiohook_status oldstatus;
605 /* By locking both channels and the audiohook, we can assure that
606 * another thread will not have a chance to read the audiohook's status
607 * as done, even though ast_audiohook_remove signals the trigger
610 ast_audiohook_lock(audiohook);
611 oldstatus = audiohook->status;
613 ast_audiohook_remove(old_chan, audiohook);
614 ast_audiohook_attach(new_chan, audiohook);
616 audiohook->status = oldstatus;
617 ast_audiohook_unlock(audiohook);
620 void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
622 struct ast_audiohook *audiohook;
624 if (!ast_channel_audiohooks(old_chan)) {
628 audiohook = find_audiohook_by_source(ast_channel_audiohooks(old_chan), source);
633 audiohook_move(old_chan, new_chan, audiohook);
636 void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
638 struct ast_audiohook *audiohook;
639 struct ast_audiohook_list *audiohook_list;
641 audiohook_list = ast_channel_audiohooks(old_chan);
642 if (!audiohook_list) {
646 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
647 audiohook_move(old_chan, new_chan, audiohook);
649 AST_LIST_TRAVERSE_SAFE_END;
651 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
652 audiohook_move(old_chan, new_chan, audiohook);
654 AST_LIST_TRAVERSE_SAFE_END;
656 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
657 audiohook_move(old_chan, new_chan, audiohook);
659 AST_LIST_TRAVERSE_SAFE_END;
662 /*! \brief Detach specified source audiohook from channel
663 * \param chan Channel to detach from
664 * \param source Name of source to detach
665 * \return Returns 0 on success, -1 on failure
667 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
669 struct ast_audiohook *audiohook = NULL;
671 ast_channel_lock(chan);
673 /* Ensure the channel has audiohooks on it */
674 if (!ast_channel_audiohooks(chan)) {
675 ast_channel_unlock(chan);
679 audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
681 ast_channel_unlock(chan);
683 if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
684 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
687 return (audiohook ? 0 : -1);
691 * \brief Remove an audiohook from a specified channel
693 * \param chan Channel to remove from
694 * \param audiohook Audiohook to remove
696 * \return Returns 0 on success, -1 on failure
698 * \note The channel does not need to be locked before calling this function
700 int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
702 ast_channel_lock(chan);
704 if (!ast_channel_audiohooks(chan)) {
705 ast_channel_unlock(chan);
709 if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
710 AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
711 } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
712 AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
713 } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
714 AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
717 audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
718 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
720 if (ast_channel_is_bridged(chan)) {
721 ast_channel_set_unbridged_nolock(chan, 1);
724 ast_channel_unlock(chan);
729 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
730 * \param chan Channel that the list is coming off of
731 * \param audiohook_list List of audiohooks
732 * \param direction Direction frame is coming in from
733 * \param frame The frame itself
734 * \return Return frame on success, NULL on failure
736 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)
738 struct ast_audiohook *audiohook = NULL;
741 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
742 ast_audiohook_lock(audiohook);
743 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
744 AST_LIST_REMOVE_CURRENT(list);
746 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
747 ast_audiohook_unlock(audiohook);
748 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
749 if (ast_channel_is_bridged(chan)) {
750 ast_channel_set_unbridged_nolock(chan, 1);
754 if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF)) {
755 audiohook->manipulate_callback(audiohook, chan, frame, direction);
757 ast_audiohook_unlock(audiohook);
759 AST_LIST_TRAVERSE_SAFE_END;
761 /* if an audiohook got removed, reset samplerate compatibility */
763 audiohook_list_set_samplerate_compatibility(audiohook_list);
768 static struct ast_frame *audiohook_list_translate_to_slin(struct ast_audiohook_list *audiohook_list,
769 enum ast_audiohook_direction direction, struct ast_frame *frame)
771 struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ?
772 &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
773 struct ast_frame *new_frame = frame;
774 struct ast_format *slin;
776 /* If we are capable of maintaining doing samplerates other that 8khz, update
777 * the internal audiohook_list's rate and higher samplerate audio arrives. By
778 * updating the list's rate, all the audiohooks in the list will be updated as well
779 * as the are written and read from. */
780 if (audiohook_list->native_slin_compatible) {
781 audiohook_list->list_internal_samp_rate =
782 MAX(ast_format_get_sample_rate(frame->subclass.format), audiohook_list->list_internal_samp_rate);
785 slin = ast_format_cache_get_slin_by_rate(audiohook_list->list_internal_samp_rate);
786 if (ast_format_cmp(frame->subclass.format, slin) == AST_FORMAT_CMP_EQUAL) {
790 if (ast_format_cmp(frame->subclass.format, in_translate->format) == AST_FORMAT_CMP_NOT_EQUAL) {
791 if (in_translate->trans_pvt) {
792 ast_translator_free_path(in_translate->trans_pvt);
794 if (!(in_translate->trans_pvt = ast_translator_build_path(slin, frame->subclass.format))) {
797 ao2_replace(in_translate->format, frame->subclass.format);
800 if (!(new_frame = ast_translate(in_translate->trans_pvt, frame, 0))) {
807 static struct ast_frame *audiohook_list_translate_to_native(struct ast_audiohook_list *audiohook_list,
808 enum ast_audiohook_direction direction, struct ast_frame *slin_frame, struct ast_format *outformat)
810 struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
811 struct ast_frame *outframe = NULL;
812 if (ast_format_cmp(slin_frame->subclass.format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
813 /* rebuild translators if necessary */
814 if (ast_format_cmp(out_translate->format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
815 if (out_translate->trans_pvt) {
816 ast_translator_free_path(out_translate->trans_pvt);
818 if (!(out_translate->trans_pvt = ast_translator_build_path(outformat, slin_frame->subclass.format))) {
821 ao2_replace(out_translate->format, outformat);
823 /* translate back to the format the frame came in as. */
824 if (!(outframe = ast_translate(out_translate->trans_pvt, slin_frame, 0))) {
832 * \brief Pass an AUDIO frame off to be handled by the audiohook core
835 * This function has 3 ast_frames and 3 parts to handle each. At the beginning of this
836 * function all 3 frames, start_frame, middle_frame, and end_frame point to the initial
839 * Part_1: Translate the start_frame into SLINEAR audio if it is not already in that
840 * format. The result of this part is middle_frame is guaranteed to be in
841 * SLINEAR format for Part_2.
842 * Part_2: Send middle_frame off to spies and manipulators. At this point middle_frame is
843 * either a new frame as result of the translation, or points directly to the start_frame
844 * because no translation to SLINEAR audio was required.
845 * Part_3: Translate end_frame's audio back into the format of start frame if necessary. This
846 * is only necessary if manipulation of middle_frame occurred.
848 * \param chan Channel that the list is coming off of
849 * \param audiohook_list List of audiohooks
850 * \param direction Direction frame is coming in from
851 * \param frame The frame itself
852 * \return Return frame on success, NULL on failure
854 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)
856 struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
857 struct ast_audiohook *audiohook = NULL;
859 int middle_frame_manipulated = 0;
862 /* ---Part_1. translate start_frame to SLINEAR if necessary. */
863 if (!(middle_frame = audiohook_list_translate_to_slin(audiohook_list, direction, start_frame))) {
866 samples = middle_frame->samples;
868 /* ---Part_2: Send middle_frame to spy and manipulator lists. middle_frame is guaranteed to be SLINEAR here.*/
869 /* Queue up signed linear frame to each spy */
870 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
871 ast_audiohook_lock(audiohook);
872 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
873 AST_LIST_REMOVE_CURRENT(list);
875 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
876 ast_audiohook_unlock(audiohook);
877 if (ast_channel_is_bridged(chan)) {
878 ast_channel_set_unbridged_nolock(chan, 1);
882 audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
883 ast_audiohook_write_frame(audiohook, direction, middle_frame);
884 ast_audiohook_unlock(audiohook);
886 AST_LIST_TRAVERSE_SAFE_END;
888 /* If this frame is being written out to the channel then we need to use whisper sources */
889 if (!AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
891 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
892 memset(&combine_buf, 0, sizeof(combine_buf));
893 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
894 struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
895 ast_audiohook_lock(audiohook);
896 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
897 AST_LIST_REMOVE_CURRENT(list);
899 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
900 ast_audiohook_unlock(audiohook);
901 if (ast_channel_is_bridged(chan)) {
902 ast_channel_set_unbridged_nolock(chan, 1);
906 audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
907 if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) {
908 /* Take audio from this whisper source and combine it into our main buffer */
909 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++) {
910 ast_slinear_saturated_add(data1, data2);
913 ast_audiohook_unlock(audiohook);
915 AST_LIST_TRAVERSE_SAFE_END;
916 /* We take all of the combined whisper sources and combine them into the audio being written out */
917 for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) {
918 ast_slinear_saturated_add(data1, data2);
920 middle_frame_manipulated = 1;
923 /* Pass off frame to manipulate audiohooks */
924 if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
925 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
926 ast_audiohook_lock(audiohook);
927 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
928 AST_LIST_REMOVE_CURRENT(list);
930 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
931 ast_audiohook_unlock(audiohook);
932 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
933 audiohook->manipulate_callback(audiohook, chan, NULL, direction);
934 if (ast_channel_is_bridged(chan)) {
935 ast_channel_set_unbridged_nolock(chan, 1);
939 audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
940 /* Feed in frame to manipulation. */
941 if (!audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) {
942 /* If the manipulation fails then the frame will be returned in its original state.
943 * Since there are potentially more manipulator callbacks in the list, no action should
944 * be taken here to exit early. */
945 middle_frame_manipulated = 1;
947 ast_audiohook_unlock(audiohook);
949 AST_LIST_TRAVERSE_SAFE_END;
952 /* ---Part_3: Decide what to do with the end_frame (whether to transcode or not) */
953 if (middle_frame_manipulated) {
954 if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, start_frame->subclass.format))) {
955 /* translation failed, so just pass back the input frame */
956 end_frame = start_frame;
959 end_frame = start_frame;
961 /* clean up our middle_frame if required */
962 if (middle_frame != end_frame) {
963 ast_frfree(middle_frame);
967 /* Before returning, if an audiohook got removed, reset samplerate compatibility */
969 audiohook_list_set_samplerate_compatibility(audiohook_list);
975 int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
977 return !audiohook_list
978 || (AST_LIST_EMPTY(&audiohook_list->spy_list)
979 && AST_LIST_EMPTY(&audiohook_list->whisper_list)
980 && AST_LIST_EMPTY(&audiohook_list->manipulate_list));
983 /*! \brief Pass a frame off to be handled by the audiohook core
984 * \param chan Channel that the list is coming off of
985 * \param audiohook_list List of audiohooks
986 * \param direction Direction frame is coming in from
987 * \param frame The frame itself
988 * \return Return frame on success, NULL on failure
990 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)
992 /* Pass off frame to it's respective list write function */
993 if (frame->frametype == AST_FRAME_VOICE) {
994 return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
995 } else if (frame->frametype == AST_FRAME_DTMF) {
996 return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
1002 /*! \brief Wait for audiohook trigger to be triggered
1003 * \param audiohook Audiohook to wait on
1005 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
1007 struct timeval wait;
1010 wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
1011 ts.tv_sec = wait.tv_sec;
1012 ts.tv_nsec = wait.tv_usec * 1000;
1014 ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
1019 /* Count number of channel audiohooks by type, regardless of type */
1020 int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
1023 struct ast_audiohook *ah = NULL;
1025 if (!ast_channel_audiohooks(chan)) {
1030 case AST_AUDIOHOOK_TYPE_SPY:
1031 AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
1032 if (!strcmp(ah->source, source)) {
1037 case AST_AUDIOHOOK_TYPE_WHISPER:
1038 AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
1039 if (!strcmp(ah->source, source)) {
1044 case AST_AUDIOHOOK_TYPE_MANIPULATE:
1045 AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
1046 if (!strcmp(ah->source, source)) {
1052 ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
1059 /* Count number of channel audiohooks by type that are running */
1060 int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
1063 struct ast_audiohook *ah = NULL;
1064 if (!ast_channel_audiohooks(chan))
1068 case AST_AUDIOHOOK_TYPE_SPY:
1069 AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
1070 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
1074 case AST_AUDIOHOOK_TYPE_WHISPER:
1075 AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
1076 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
1080 case AST_AUDIOHOOK_TYPE_MANIPULATE:
1081 AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
1082 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
1087 ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
1093 /*! \brief Audiohook volume adjustment structure */
1094 struct audiohook_volume {
1095 struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
1096 int read_adjustment; /*!< Value to adjust frames read from the channel by */
1097 int write_adjustment; /*!< Value to adjust frames written to the channel by */
1100 /*! \brief Callback used to destroy the audiohook volume datastore
1101 * \param data Volume information structure
1102 * \return Returns nothing
1104 static void audiohook_volume_destroy(void *data)
1106 struct audiohook_volume *audiohook_volume = data;
1108 /* Destroy the audiohook as it is no longer in use */
1109 ast_audiohook_destroy(&audiohook_volume->audiohook);
1111 /* Finally free ourselves, we are of no more use */
1112 ast_free(audiohook_volume);
1117 /*! \brief Datastore used to store audiohook volume information */
1118 static const struct ast_datastore_info audiohook_volume_datastore = {
1120 .destroy = audiohook_volume_destroy,
1123 /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
1124 * \param audiohook Audiohook attached to the channel
1125 * \param chan Channel we are attached to
1126 * \param frame Frame of audio we want to manipulate
1127 * \param direction Direction the audio came in from
1128 * \return Returns 0 on success, -1 on failure
1130 static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
1132 struct ast_datastore *datastore = NULL;
1133 struct audiohook_volume *audiohook_volume = NULL;
1136 /* If the audiohook is shutting down don't even bother */
1137 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
1141 /* Try to find the datastore containg adjustment information, if we can't just bail out */
1142 if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
1146 audiohook_volume = datastore->data;
1148 /* Based on direction grab the appropriate adjustment value */
1149 if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
1150 gain = &audiohook_volume->read_adjustment;
1151 } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
1152 gain = &audiohook_volume->write_adjustment;
1155 /* If an adjustment value is present modify the frame */
1156 if (gain && *gain) {
1157 ast_frame_adjust_volume(frame, *gain);
1163 /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
1164 * \param chan Channel to look on
1165 * \param create Whether to create the datastore if not found
1166 * \return Returns audiohook_volume structure on success, NULL on failure
1168 static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
1170 struct ast_datastore *datastore = NULL;
1171 struct audiohook_volume *audiohook_volume = NULL;
1173 /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
1174 if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
1175 return datastore->data;
1178 /* If we are not allowed to create a datastore or if we fail to create a datastore, bail out now as we have nothing for them */
1179 if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
1183 /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
1184 if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
1185 ast_datastore_free(datastore);
1189 /* Setup our audiohook structure so we can manipulate the audio */
1190 ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
1191 audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
1193 /* Attach the audiohook_volume blob to the datastore and attach to the channel */
1194 datastore->data = audiohook_volume;
1195 ast_channel_datastore_add(chan, datastore);
1197 /* All is well... put the audiohook into motion */
1198 ast_audiohook_attach(chan, &audiohook_volume->audiohook);
1200 return audiohook_volume;
1203 /*! \brief Adjust the volume on frames read from or written to a channel
1204 * \param chan Channel to muck with
1205 * \param direction Direction to set on
1206 * \param volume Value to adjust the volume by
1207 * \return Returns 0 on success, -1 on failure
1209 int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
1211 struct audiohook_volume *audiohook_volume = NULL;
1213 /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
1214 if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
1218 /* Now based on the direction set the proper value */
1219 if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
1220 audiohook_volume->read_adjustment = volume;
1222 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
1223 audiohook_volume->write_adjustment = volume;
1229 /*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
1230 * \param chan Channel to retrieve volume adjustment from
1231 * \param direction Direction to retrieve
1232 * \return Returns adjustment value
1234 int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
1236 struct audiohook_volume *audiohook_volume = NULL;
1239 /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
1240 if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
1244 /* Grab the adjustment value based on direction given */
1245 if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
1246 adjustment = audiohook_volume->read_adjustment;
1247 } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
1248 adjustment = audiohook_volume->write_adjustment;
1254 /*! \brief Adjust the volume on frames read from or written to a channel
1255 * \param chan Channel to muck with
1256 * \param direction Direction to increase
1257 * \param volume Value to adjust the adjustment by
1258 * \return Returns 0 on success, -1 on failure
1260 int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
1262 struct audiohook_volume *audiohook_volume = NULL;
1264 /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
1265 if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
1269 /* Based on the direction change the specific adjustment value */
1270 if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
1271 audiohook_volume->read_adjustment += volume;
1273 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
1274 audiohook_volume->write_adjustment += volume;
1280 /*! \brief Mute frames read from or written to a channel
1281 * \param chan Channel to muck with
1282 * \param source Type of audiohook
1283 * \param flag which flag to set / clear
1284 * \param clear set or clear
1285 * \return Returns 0 on success, -1 on failure
1287 int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
1289 struct ast_audiohook *audiohook = NULL;
1291 ast_channel_lock(chan);
1293 /* Ensure the channel has audiohooks on it */
1294 if (!ast_channel_audiohooks(chan)) {
1295 ast_channel_unlock(chan);
1299 audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
1303 ast_clear_flag(audiohook, flag);
1305 ast_set_flag(audiohook, flag);
1309 ast_channel_unlock(chan);
1311 return (audiohook ? 0 : -1);