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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/channel.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/lock.h"
35 #include "asterisk/linkedlists.h"
36 #include "asterisk/audiohook.h"
37 #include "asterisk/slinfactory.h"
38 #include "asterisk/frame.h"
39 #include "asterisk/translate.h"
41 struct ast_audiohook_translate {
42 struct ast_trans_pvt *trans_pvt;
46 struct ast_audiohook_list {
47 struct ast_audiohook_translate in_translate[2];
48 struct ast_audiohook_translate out_translate[2];
49 AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
50 AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
51 AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
54 /*! \brief Initialize an audiohook structure
55 * \param audiohook Audiohook structure
58 * \return Returns 0 on success, -1 on failure
60 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
62 /* Need to keep the type and source */
63 audiohook->type = type;
64 audiohook->source = source;
66 /* Initialize lock that protects our audiohook */
67 ast_mutex_init(&audiohook->lock);
68 ast_cond_init(&audiohook->trigger, NULL);
70 /* Setup the factories that are needed for this audiohook type */
72 case AST_AUDIOHOOK_TYPE_SPY:
73 ast_slinfactory_init(&audiohook->read_factory);
74 case AST_AUDIOHOOK_TYPE_WHISPER:
75 ast_slinfactory_init(&audiohook->write_factory);
81 /* Since we are just starting out... this audiohook is new */
82 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_NEW);
87 /*! \brief Destroys an audiohook structure
88 * \param audiohook Audiohook structure
89 * \return Returns 0 on success, -1 on failure
91 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
93 /* Drop the factories used by this audiohook type */
94 switch (audiohook->type) {
95 case AST_AUDIOHOOK_TYPE_SPY:
96 ast_slinfactory_destroy(&audiohook->read_factory);
97 case AST_AUDIOHOOK_TYPE_WHISPER:
98 ast_slinfactory_destroy(&audiohook->write_factory);
104 /* Destroy translation path if present */
105 if (audiohook->trans_pvt)
106 ast_translator_free_path(audiohook->trans_pvt);
108 /* Lock and trigger be gone! */
109 ast_cond_destroy(&audiohook->trigger);
110 ast_mutex_destroy(&audiohook->lock);
115 /*! \brief Writes a frame into the audiohook structure
116 * \param audiohook Audiohook structure
117 * \param direction Direction the audio frame came from
118 * \param frame Frame to write in
119 * \return Returns 0 on success, -1 on failure
121 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
123 struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
124 struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory);
125 struct timeval *rwtime = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *rwtime;
126 int our_factory_samples;
128 int other_factory_samples;
129 int other_factory_ms;
132 /* Update last feeding time to be current */
133 *rwtime = ast_tvnow();
135 our_factory_samples = ast_slinfactory_available(factory);
136 our_factory_ms = ast_tvdiff_ms(*rwtime, previous_time) + (our_factory_samples / 8);
137 other_factory_samples = ast_slinfactory_available(other_factory);
138 other_factory_ms = other_factory_samples / 8;
140 if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && other_factory_samples && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) {
142 ast_log(LOG_DEBUG, "Flushing audiohook %p so it remains in sync\n", audiohook);
143 ast_slinfactory_flush(factory);
144 ast_slinfactory_flush(other_factory);
147 if (ast_test_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE) && (our_factory_samples > 640 || other_factory_samples > 640)) {
149 ast_log(LOG_DEBUG, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
151 ast_slinfactory_flush(factory);
152 ast_slinfactory_flush(other_factory);
155 /* swap frame data for zeros if mute is required */
156 if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) ||
157 (ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) ||
158 (ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE) == (AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE))) {
162 if (muteme && frame->datalen > 0) {
163 ast_frame_clear(frame);
166 /* Write frame out to respective factory */
167 ast_slinfactory_feed(factory, frame);
169 /* If we need to notify the respective handler of this audiohook, do so */
170 if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
171 ast_cond_signal(&audiohook->trigger);
172 } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
173 ast_cond_signal(&audiohook->trigger);
174 } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
175 ast_cond_signal(&audiohook->trigger);
181 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
183 struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
184 int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
186 struct ast_frame frame = {
187 .frametype = AST_FRAME_VOICE,
188 .subclass.codec = AST_FORMAT_SLINEAR,
190 .datalen = sizeof(buf),
194 /* Ensure the factory is able to give us the samples we want */
195 if (samples > ast_slinfactory_available(factory))
198 /* Read data in from factory */
199 if (!ast_slinfactory_read(factory, buf, samples))
202 /* If a volume adjustment needs to be applied apply it */
204 ast_frame_adjust_volume(&frame, vol);
206 return ast_frdup(&frame);
209 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
211 int i = 0, usable_read, usable_write;
212 short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
213 struct ast_frame frame = {
214 .frametype = AST_FRAME_VOICE,
215 .subclass.codec = AST_FORMAT_SLINEAR,
217 .datalen = sizeof(buf1),
221 /* Make sure both factories have the required samples */
222 usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
223 usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
225 if (!usable_read && !usable_write) {
226 /* If both factories are unusable bail out */
227 ast_debug(1, "Read factory %p and write factory %p both fail to provide %zd samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
231 /* If we want to provide only a read factory make sure we aren't waiting for other audio */
232 if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
233 ast_debug(3, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
237 /* If we want to provide only a write factory make sure we aren't waiting for other audio */
238 if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*2)) {
239 ast_debug(3, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
243 /* Start with the read factory... if there are enough samples, read them in */
245 if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
247 /* Adjust read volume if need be */
248 if (audiohook->options.read_volume) {
250 short adjust_value = abs(audiohook->options.read_volume);
251 for (count = 0; count < samples; count++) {
252 if (audiohook->options.read_volume > 0)
253 ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
254 else if (audiohook->options.read_volume < 0)
255 ast_slinear_saturated_divide(&buf1[count], &adjust_value);
259 } else if (option_debug)
260 ast_log(LOG_DEBUG, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory);
262 /* Move on to the write factory... if there are enough samples, read them in */
264 if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
266 /* Adjust write volume if need be */
267 if (audiohook->options.write_volume) {
269 short adjust_value = abs(audiohook->options.write_volume);
270 for (count = 0; count < samples; count++) {
271 if (audiohook->options.write_volume > 0)
272 ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
273 else if (audiohook->options.write_volume < 0)
274 ast_slinear_saturated_divide(&buf2[count], &adjust_value);
278 } else if (option_debug)
279 ast_log(LOG_DEBUG, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory);
281 /* Basically we figure out which buffer to use... and if mixing can be done here */
282 if (!read_buf && !write_buf)
284 else if (read_buf && write_buf) {
285 for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
286 ast_slinear_saturated_add(data1, data2);
293 /* Make the final buffer part of the frame, so it gets duplicated fine */
294 frame.data.ptr = final_buf;
296 /* Yahoo, a combined copy of the audio! */
297 return ast_frdup(&frame);
300 /*! \brief Reads a frame in from the audiohook structure
301 * \param audiohook Audiohook structure
302 * \param samples Number of samples wanted
303 * \param direction Direction the audio frame came from
304 * \param format Format of frame remote side wants back
305 * \return Returns frame on success, NULL on failure
307 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, format_t format)
309 struct ast_frame *read_frame = NULL, *final_frame = NULL;
311 if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
314 /* If they don't want signed linear back out, we'll have to send it through the translation path */
315 if (format != AST_FORMAT_SLINEAR) {
316 /* Rebuild translation path if different format then previously */
317 if (audiohook->format != format) {
318 if (audiohook->trans_pvt) {
319 ast_translator_free_path(audiohook->trans_pvt);
320 audiohook->trans_pvt = NULL;
322 /* 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 */
323 if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
324 ast_frfree(read_frame);
328 /* Convert to requested format, and allow the read in frame to be freed */
329 final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
331 final_frame = read_frame;
337 /*! \brief Attach audiohook to channel
338 * \param chan Channel
339 * \param audiohook Audiohook structure
340 * \return Returns 0 on success, -1 on failure
342 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
344 ast_channel_lock(chan);
346 if (!chan->audiohooks) {
347 /* Whoops... allocate a new structure */
348 if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
349 ast_channel_unlock(chan);
352 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
353 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
354 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
357 /* Drop into respective list */
358 if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
359 AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
360 else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
361 AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
362 else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
363 AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
365 /* Change status over to running since it is now attached */
366 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_RUNNING);
368 ast_channel_unlock(chan);
373 /*! \brief Update audiohook's status
374 * \param audiohook Audiohook structure
375 * \param status Audiohook status enum
377 * \note once status is updated to DONE, this function can not be used to set the
378 * status back to any other setting. Setting DONE effectively locks the status as such.
381 void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
383 ast_audiohook_lock(audiohook);
384 if (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
385 audiohook->status = status;
386 ast_cond_signal(&audiohook->trigger);
388 ast_audiohook_unlock(audiohook);
391 /*! \brief Detach audiohook from channel
392 * \param audiohook Audiohook structure
393 * \return Returns 0 on success, -1 on failure
395 int ast_audiohook_detach(struct ast_audiohook *audiohook)
397 if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
400 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
402 while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
403 ast_audiohook_trigger_wait(audiohook);
408 /*! \brief Detach audiohooks from list and destroy said list
409 * \param audiohook_list List of audiohooks
410 * \return Returns 0 on success, -1 on failure
412 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
415 struct ast_audiohook *audiohook = NULL;
418 while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
419 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
422 /* Drop any whispering sources */
423 while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
424 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
427 /* Drop any manipulaters */
428 while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
429 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
430 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
433 /* Drop translation paths if present */
434 for (i = 0; i < 2; i++) {
435 if (audiohook_list->in_translate[i].trans_pvt)
436 ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
437 if (audiohook_list->out_translate[i].trans_pvt)
438 ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
442 ast_free(audiohook_list);
447 /*! \brief find an audiohook based on its source
448 * \param audiohook_list The list of audiohooks to search in
449 * \param source The source of the audiohook we wish to find
450 * \return Return the corresponding audiohook or NULL if it cannot be found.
452 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
454 struct ast_audiohook *audiohook = NULL;
456 AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
457 if (!strcasecmp(audiohook->source, source))
461 AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
462 if (!strcasecmp(audiohook->source, source))
466 AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
467 if (!strcasecmp(audiohook->source, source))
474 void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
476 struct ast_audiohook *audiohook;
477 enum ast_audiohook_status oldstatus;
479 if (!old_chan->audiohooks || !(audiohook = find_audiohook_by_source(old_chan->audiohooks, source))) {
483 /* By locking both channels and the audiohook, we can assure that
484 * another thread will not have a chance to read the audiohook's status
485 * as done, even though ast_audiohook_remove signals the trigger
488 ast_audiohook_lock(audiohook);
489 oldstatus = audiohook->status;
491 ast_audiohook_remove(old_chan, audiohook);
492 ast_audiohook_attach(new_chan, audiohook);
494 audiohook->status = oldstatus;
495 ast_audiohook_unlock(audiohook);
498 /*! \brief Detach specified source audiohook from channel
499 * \param chan Channel to detach from
500 * \param source Name of source to detach
501 * \return Returns 0 on success, -1 on failure
503 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
505 struct ast_audiohook *audiohook = NULL;
507 ast_channel_lock(chan);
509 /* Ensure the channel has audiohooks on it */
510 if (!chan->audiohooks) {
511 ast_channel_unlock(chan);
515 audiohook = find_audiohook_by_source(chan->audiohooks, source);
517 ast_channel_unlock(chan);
519 if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
520 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
522 return (audiohook ? 0 : -1);
526 * \brief Remove an audiohook from a specified channel
528 * \param chan Channel to remove from
529 * \param audiohook Audiohook to remove
531 * \return Returns 0 on success, -1 on failure
533 * \note The channel does not need to be locked before calling this function
535 int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
537 ast_channel_lock(chan);
539 if (!chan->audiohooks) {
540 ast_channel_unlock(chan);
544 if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
545 AST_LIST_REMOVE(&chan->audiohooks->spy_list, audiohook, list);
546 else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
547 AST_LIST_REMOVE(&chan->audiohooks->whisper_list, audiohook, list);
548 else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
549 AST_LIST_REMOVE(&chan->audiohooks->manipulate_list, audiohook, list);
551 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
553 ast_channel_unlock(chan);
558 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
559 * \param chan Channel that the list is coming off of
560 * \param audiohook_list List of audiohooks
561 * \param direction Direction frame is coming in from
562 * \param frame The frame itself
563 * \return Return frame on success, NULL on failure
565 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)
567 struct ast_audiohook *audiohook = NULL;
569 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
570 ast_audiohook_lock(audiohook);
571 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
572 AST_LIST_REMOVE_CURRENT(list);
573 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
574 ast_audiohook_unlock(audiohook);
575 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
578 if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
579 audiohook->manipulate_callback(audiohook, chan, frame, direction);
580 ast_audiohook_unlock(audiohook);
582 AST_LIST_TRAVERSE_SAFE_END;
588 * \brief Pass an AUDIO frame off to be handled by the audiohook core
591 * This function has 3 ast_frames and 3 parts to handle each. At the beginning of this
592 * function all 3 frames, start_frame, middle_frame, and end_frame point to the initial
595 * Part_1: Translate the start_frame into SLINEAR audio if it is not already in that
596 * format. The result of this part is middle_frame is guaranteed to be in
597 * SLINEAR format for Part_2.
598 * Part_2: Send middle_frame off to spies and manipulators. At this point middle_frame is
599 * either a new frame as result of the translation, or points directly to the start_frame
600 * because no translation to SLINEAR audio was required. The result of this part
601 * is end_frame will be updated to point to middle_frame if any audiohook manipulation
603 * Part_3: Translate end_frame's audio back into the format of start frame if necessary.
604 * At this point if middle_frame != end_frame, we are guaranteed that no manipulation
605 * took place and middle_frame can be freed as it was translated... If middle_frame was
606 * not translated and still pointed to start_frame, it would be equal to end_frame as well
607 * regardless if manipulation took place which would not result in this free. The result
608 * of this part is end_frame is guaranteed to be the format of start_frame for the return.
610 * \param chan Channel that the list is coming off of
611 * \param audiohook_list List of audiohooks
612 * \param direction Direction frame is coming in from
613 * \param frame The frame itself
614 * \return Return frame on success, NULL on failure
616 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)
618 struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
619 struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
620 struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
621 struct ast_audiohook *audiohook = NULL;
622 int samples = frame->samples;
624 /* ---Part_1. translate start_frame to SLINEAR if necessary. */
625 /* If the frame coming in is not signed linear we have to send it through the in_translate path */
626 if (frame->subclass.codec != AST_FORMAT_SLINEAR) {
627 if (in_translate->format != frame->subclass.codec) {
628 if (in_translate->trans_pvt)
629 ast_translator_free_path(in_translate->trans_pvt);
630 if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass.codec)))
632 in_translate->format = frame->subclass.codec;
634 if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
636 samples = middle_frame->samples;
639 /* ---Part_2: Send middle_frame to spy and manipulator lists. middle_frame is guaranteed to be SLINEAR here.*/
640 /* Queue up signed linear frame to each spy */
641 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
642 ast_audiohook_lock(audiohook);
643 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
644 AST_LIST_REMOVE_CURRENT(list);
645 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
646 ast_audiohook_unlock(audiohook);
649 ast_audiohook_write_frame(audiohook, direction, middle_frame);
650 ast_audiohook_unlock(audiohook);
652 AST_LIST_TRAVERSE_SAFE_END;
654 /* If this frame is being written out to the channel then we need to use whisper sources */
655 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
657 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
658 memset(&combine_buf, 0, sizeof(combine_buf));
659 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
660 ast_audiohook_lock(audiohook);
661 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
662 AST_LIST_REMOVE_CURRENT(list);
663 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
664 ast_audiohook_unlock(audiohook);
667 if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
668 /* Take audio from this whisper source and combine it into our main buffer */
669 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
670 ast_slinear_saturated_add(data1, data2);
672 ast_audiohook_unlock(audiohook);
674 AST_LIST_TRAVERSE_SAFE_END;
675 /* We take all of the combined whisper sources and combine them into the audio being written out */
676 for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++)
677 ast_slinear_saturated_add(data1, data2);
678 end_frame = middle_frame;
681 /* Pass off frame to manipulate audiohooks */
682 if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
683 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
684 ast_audiohook_lock(audiohook);
685 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
686 AST_LIST_REMOVE_CURRENT(list);
687 ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
688 ast_audiohook_unlock(audiohook);
689 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
690 audiohook->manipulate_callback(audiohook, chan, NULL, direction);
693 /* Feed in frame to manipulation. */
694 if (audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) {
695 /* XXX IGNORE FAILURE */
697 /* If the manipulation fails then the frame will be returned in its original state.
698 * Since there are potentially more manipulator callbacks in the list, no action should
699 * be taken here to exit early. */
701 ast_audiohook_unlock(audiohook);
703 AST_LIST_TRAVERSE_SAFE_END;
704 end_frame = middle_frame;
707 /* ---Part_3: Decide what to do with the end_frame (whether to transcode or not) */
708 if (middle_frame == end_frame) {
709 /* Middle frame was modified and became the end frame... let's see if we need to transcode */
710 if (end_frame->subclass.codec != start_frame->subclass.codec) {
711 if (out_translate->format != start_frame->subclass.codec) {
712 if (out_translate->trans_pvt)
713 ast_translator_free_path(out_translate->trans_pvt);
714 if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass.codec, AST_FORMAT_SLINEAR))) {
715 /* We can't transcode this... drop our middle frame and return the original */
716 ast_frfree(middle_frame);
719 out_translate->format = start_frame->subclass.codec;
721 /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
722 if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
723 /* Failed to transcode the frame... drop it and return the original */
724 ast_frfree(middle_frame);
727 /* Here's the scoop... middle frame is no longer of use to us */
728 ast_frfree(middle_frame);
731 /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
732 ast_frfree(middle_frame);
738 int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
740 if (AST_LIST_EMPTY(&audiohook_list->spy_list) &&
741 AST_LIST_EMPTY(&audiohook_list->whisper_list) &&
742 AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
749 /*! \brief Pass a frame off to be handled by the audiohook core
750 * \param chan Channel that the list is coming off of
751 * \param audiohook_list List of audiohooks
752 * \param direction Direction frame is coming in from
753 * \param frame The frame itself
754 * \return Return frame on success, NULL on failure
756 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)
758 /* Pass off frame to it's respective list write function */
759 if (frame->frametype == AST_FRAME_VOICE)
760 return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
761 else if (frame->frametype == AST_FRAME_DTMF)
762 return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
767 /*! \brief Wait for audiohook trigger to be triggered
768 * \param audiohook Audiohook to wait on
770 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
775 wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
776 ts.tv_sec = wait.tv_sec;
777 ts.tv_nsec = wait.tv_usec * 1000;
779 ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
784 /* Count number of channel audiohooks by type, regardless of type */
785 int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
788 struct ast_audiohook *ah = NULL;
790 if (!chan->audiohooks)
794 case AST_AUDIOHOOK_TYPE_SPY:
795 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->spy_list, ah, list) {
796 if (!strcmp(ah->source, source)) {
800 AST_LIST_TRAVERSE_SAFE_END;
802 case AST_AUDIOHOOK_TYPE_WHISPER:
803 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->whisper_list, ah, list) {
804 if (!strcmp(ah->source, source)) {
808 AST_LIST_TRAVERSE_SAFE_END;
810 case AST_AUDIOHOOK_TYPE_MANIPULATE:
811 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->manipulate_list, ah, list) {
812 if (!strcmp(ah->source, source)) {
816 AST_LIST_TRAVERSE_SAFE_END;
819 ast_log(LOG_DEBUG, "Invalid audiohook type supplied, (%d)\n", type);
826 /* Count number of channel audiohooks by type that are running */
827 int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
830 struct ast_audiohook *ah = NULL;
831 if (!chan->audiohooks)
835 case AST_AUDIOHOOK_TYPE_SPY:
836 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->spy_list, ah, list) {
837 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
840 AST_LIST_TRAVERSE_SAFE_END;
842 case AST_AUDIOHOOK_TYPE_WHISPER:
843 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->whisper_list, ah, list) {
844 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
847 AST_LIST_TRAVERSE_SAFE_END;
849 case AST_AUDIOHOOK_TYPE_MANIPULATE:
850 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->manipulate_list, ah, list) {
851 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
854 AST_LIST_TRAVERSE_SAFE_END;
857 ast_log(LOG_DEBUG, "Invalid audiohook type supplied, (%d)\n", type);
863 /*! \brief Audiohook volume adjustment structure */
864 struct audiohook_volume {
865 struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
866 int read_adjustment; /*!< Value to adjust frames read from the channel by */
867 int write_adjustment; /*!< Value to adjust frames written to the channel by */
870 /*! \brief Callback used to destroy the audiohook volume datastore
871 * \param data Volume information structure
872 * \return Returns nothing
874 static void audiohook_volume_destroy(void *data)
876 struct audiohook_volume *audiohook_volume = data;
878 /* Destroy the audiohook as it is no longer in use */
879 ast_audiohook_destroy(&audiohook_volume->audiohook);
881 /* Finally free ourselves, we are of no more use */
882 ast_free(audiohook_volume);
887 /*! \brief Datastore used to store audiohook volume information */
888 static const struct ast_datastore_info audiohook_volume_datastore = {
890 .destroy = audiohook_volume_destroy,
893 /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
894 * \param audiohook Audiohook attached to the channel
895 * \param chan Channel we are attached to
896 * \param frame Frame of audio we want to manipulate
897 * \param direction Direction the audio came in from
898 * \return Returns 0 on success, -1 on failure
900 static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
902 struct ast_datastore *datastore = NULL;
903 struct audiohook_volume *audiohook_volume = NULL;
906 /* If the audiohook is shutting down don't even bother */
907 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
911 /* Try to find the datastore containg adjustment information, if we can't just bail out */
912 if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
916 audiohook_volume = datastore->data;
918 /* Based on direction grab the appropriate adjustment value */
919 if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
920 gain = &audiohook_volume->read_adjustment;
921 } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
922 gain = &audiohook_volume->write_adjustment;
925 /* If an adjustment value is present modify the frame */
927 ast_frame_adjust_volume(frame, *gain);
933 /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
934 * \param chan Channel to look on
935 * \param create Whether to create the datastore if not found
936 * \return Returns audiohook_volume structure on success, NULL on failure
938 static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
940 struct ast_datastore *datastore = NULL;
941 struct audiohook_volume *audiohook_volume = NULL;
943 /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
944 if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
945 return datastore->data;
948 /* 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 */
949 if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
953 /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
954 if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
955 ast_datastore_free(datastore);
959 /* Setup our audiohook structure so we can manipulate the audio */
960 ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
961 audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
963 /* Attach the audiohook_volume blob to the datastore and attach to the channel */
964 datastore->data = audiohook_volume;
965 ast_channel_datastore_add(chan, datastore);
967 /* All is well... put the audiohook into motion */
968 ast_audiohook_attach(chan, &audiohook_volume->audiohook);
970 return audiohook_volume;
973 /*! \brief Adjust the volume on frames read from or written to a channel
974 * \param chan Channel to muck with
975 * \param direction Direction to set on
976 * \param volume Value to adjust the volume by
977 * \return Returns 0 on success, -1 on failure
979 int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
981 struct audiohook_volume *audiohook_volume = NULL;
983 /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
984 if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
988 /* Now based on the direction set the proper value */
989 if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
990 audiohook_volume->read_adjustment = volume;
992 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
993 audiohook_volume->write_adjustment = volume;
999 /*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
1000 * \param chan Channel to retrieve volume adjustment from
1001 * \param direction Direction to retrieve
1002 * \return Returns adjustment value
1004 int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
1006 struct audiohook_volume *audiohook_volume = NULL;
1009 /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
1010 if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
1014 /* Grab the adjustment value based on direction given */
1015 if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
1016 adjustment = audiohook_volume->read_adjustment;
1017 } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
1018 adjustment = audiohook_volume->write_adjustment;
1024 /*! \brief Adjust the volume on frames read from or written to a channel
1025 * \param chan Channel to muck with
1026 * \param direction Direction to increase
1027 * \param volume Value to adjust the adjustment by
1028 * \return Returns 0 on success, -1 on failure
1030 int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
1032 struct audiohook_volume *audiohook_volume = NULL;
1034 /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
1035 if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
1039 /* Based on the direction change the specific adjustment value */
1040 if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
1041 audiohook_volume->read_adjustment += volume;
1043 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
1044 audiohook_volume->write_adjustment += volume;
1050 /*! \brief Mute frames read from or written to a channel
1051 * \param chan Channel to muck with
1052 * \param source Type of audiohook
1053 * \param flag which flag to set / clear
1054 * \param clear set or clear
1055 * \return Returns 0 on success, -1 on failure
1057 int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
1059 struct ast_audiohook *audiohook = NULL;
1061 ast_channel_lock(chan);
1063 /* Ensure the channel has audiohooks on it */
1064 if (!chan->audiohooks) {
1065 ast_channel_unlock(chan);
1069 audiohook = find_audiohook_by_source(chan->audiohooks, source);
1073 ast_clear_flag(audiohook, flag);
1075 ast_set_flag(audiohook, flag);
1079 ast_channel_unlock(chan);
1081 return (audiohook ? 0 : -1);