when a PRI call must be moved to a different B channel at the request of the other...
[asterisk-bristuff.git] / main / audiohook.c
blob806ae0beafab7f33e07f56f54ada7e8210811cad
1 /*
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.
19 /*! \file
21 * \brief Audiohooks Architecture
23 * \author Joshua Colp <jcolp@digium.com>
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <unistd.h>
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/options.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/linkedlists.h"
43 #include "asterisk/audiohook.h"
44 #include "asterisk/slinfactory.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/translate.h"
48 struct ast_audiohook_translate {
49 struct ast_trans_pvt *trans_pvt;
50 int format;
53 struct ast_audiohook_list {
54 struct ast_audiohook_translate in_translate[2];
55 struct ast_audiohook_translate out_translate[2];
56 AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
57 AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
58 AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
61 /*! \brief Initialize an audiohook structure
62 * \param audiohook Audiohook structure
63 * \param type
64 * \param source
65 * \return Returns 0 on success, -1 on failure
67 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
69 /* Need to keep the type and source */
70 audiohook->type = type;
71 audiohook->source = source;
73 /* Initialize lock that protects our audiohook */
74 ast_mutex_init(&audiohook->lock);
75 ast_cond_init(&audiohook->trigger, NULL);
77 /* Setup the factories that are needed for this audiohook type */
78 switch (type) {
79 case AST_AUDIOHOOK_TYPE_SPY:
80 ast_slinfactory_init(&audiohook->read_factory);
81 case AST_AUDIOHOOK_TYPE_WHISPER:
82 ast_slinfactory_init(&audiohook->write_factory);
83 break;
84 default:
85 break;
88 /* Since we are just starting out... this audiohook is new */
89 audiohook->status = AST_AUDIOHOOK_STATUS_NEW;
91 return 0;
94 /*! \brief Destroys an audiohook structure
95 * \param audiohook Audiohook structure
96 * \return Returns 0 on success, -1 on failure
98 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
100 /* Drop the factories used by this audiohook type */
101 switch (audiohook->type) {
102 case AST_AUDIOHOOK_TYPE_SPY:
103 ast_slinfactory_destroy(&audiohook->read_factory);
104 case AST_AUDIOHOOK_TYPE_WHISPER:
105 ast_slinfactory_destroy(&audiohook->write_factory);
106 break;
107 default:
108 break;
111 /* Destroy translation path if present */
112 if (audiohook->trans_pvt)
113 ast_translator_free_path(audiohook->trans_pvt);
115 /* Lock and trigger be gone! */
116 ast_cond_destroy(&audiohook->trigger);
117 ast_mutex_destroy(&audiohook->lock);
119 return 0;
122 /*! \brief Writes a frame into the audiohook structure
123 * \param audiohook Audiohook structure
124 * \param direction Direction the audio frame came from
125 * \param frame Frame to write in
126 * \return Returns 0 on success, -1 on failure
128 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
130 struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
132 /* Write frame out to respective factory */
133 ast_slinfactory_feed(factory, frame);
135 /* If we need to notify the respective handler of this audiohook, do so */
136 switch (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE)) {
137 case AST_AUDIOHOOK_TRIGGER_READ:
138 if (direction == AST_AUDIOHOOK_DIRECTION_READ)
139 ast_cond_signal(&audiohook->trigger);
140 break;
141 case AST_AUDIOHOOK_TRIGGER_WRITE:
142 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE)
143 ast_cond_signal(&audiohook->trigger);
144 break;
145 default:
146 break;
149 return 0;
152 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
154 struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
155 int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
156 short buf[samples];
157 struct ast_frame frame = {
158 .frametype = AST_FRAME_VOICE,
159 .subclass = AST_FORMAT_SLINEAR,
160 .data = buf,
161 .datalen = sizeof(buf),
162 .samples = samples,
165 /* Ensure the factory is able to give us the samples we want */
166 if (samples > ast_slinfactory_available(factory))
167 return NULL;
169 /* Read data in from factory */
170 if (!ast_slinfactory_read(factory, buf, samples))
171 return NULL;
173 /* If a volume adjustment needs to be applied apply it */
174 if (vol)
175 ast_frame_adjust_volume(&frame, vol);
177 return ast_frdup(&frame);
180 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
182 int i = 0;
183 short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
184 struct ast_frame frame = {
185 .frametype = AST_FRAME_VOICE,
186 .subclass = AST_FORMAT_SLINEAR,
187 .data = NULL,
188 .datalen = sizeof(buf1),
189 .samples = samples,
192 /* Start with the read factory... if there are enough samples, read them in */
193 if (ast_slinfactory_available(&audiohook->read_factory) >= samples) {
194 if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
195 read_buf = buf1;
196 /* Adjust read volume if need be */
197 if (audiohook->options.read_volume) {
198 int count = 0;
199 short adjust_value = abs(audiohook->options.read_volume);
200 for (count = 0; count < samples; count++) {
201 if (audiohook->options.read_volume > 0)
202 ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
203 else if (audiohook->options.read_volume < 0)
204 ast_slinear_saturated_divide(&buf1[count], &adjust_value);
208 } else if (option_debug)
209 ast_log(LOG_DEBUG, "Failed to get %zd samples from read factory %p\n", samples, &audiohook->read_factory);
211 /* Move on to the write factory... if there are enough samples, read them in */
212 if (ast_slinfactory_available(&audiohook->write_factory) >= samples) {
213 if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
214 write_buf = buf2;
215 /* Adjust write volume if need be */
216 if (audiohook->options.write_volume) {
217 int count = 0;
218 short adjust_value = abs(audiohook->options.write_volume);
219 for (count = 0; count < samples; count++) {
220 if (audiohook->options.write_volume > 0)
221 ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
222 else if (audiohook->options.write_volume < 0)
223 ast_slinear_saturated_divide(&buf2[count], &adjust_value);
227 } else if (option_debug)
228 ast_log(LOG_DEBUG, "Failed to get %zd samples from write factory %p\n", samples, &audiohook->write_factory);
230 /* Basically we figure out which buffer to use... and if mixing can be done here */
231 if (!read_buf && !write_buf)
232 return NULL;
233 else if (read_buf && write_buf) {
234 for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
235 ast_slinear_saturated_add(data1, data2);
236 final_buf = buf1;
237 } else if (read_buf)
238 final_buf = buf1;
239 else if (write_buf)
240 final_buf = buf2;
242 /* Make the final buffer part of the frame, so it gets duplicated fine */
243 frame.data = final_buf;
245 /* Yahoo, a combined copy of the audio! */
246 return ast_frdup(&frame);
249 /*! \brief Reads a frame in from the audiohook structure
250 * \param audiohook Audiohook structure
251 * \param samples Number of samples wanted
252 * \param direction Direction the audio frame came from
253 * \param format Format of frame remote side wants back
254 * \return Returns frame on success, NULL on failure
256 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
258 struct ast_frame *read_frame = NULL, *final_frame = NULL;
260 if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
261 return NULL;
263 /* If they don't want signed linear back out, we'll have to send it through the translation path */
264 if (format != AST_FORMAT_SLINEAR) {
265 /* Rebuild translation path if different format then previously */
266 if (audiohook->format != format) {
267 if (audiohook->trans_pvt) {
268 ast_translator_free_path(audiohook->trans_pvt);
269 audiohook->trans_pvt = NULL;
271 /* 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 */
272 if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
273 ast_frfree(read_frame);
274 return NULL;
277 /* Convert to requested format, and allow the read in frame to be freed */
278 final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
279 } else {
280 final_frame = read_frame;
283 return final_frame;
286 /*! \brief Attach audiohook to channel
287 * \param chan Channel
288 * \param audiohook Audiohook structure
289 * \return Returns 0 on success, -1 on failure
291 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
293 ast_channel_lock(chan);
295 if (!chan->audiohooks) {
296 /* Whoops... allocate a new structure */
297 if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
298 ast_channel_unlock(chan);
299 return -1;
301 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
302 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
303 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
306 /* Drop into respective list */
307 if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
308 AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
309 else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
310 AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
311 else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
312 AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
314 /* Change status over to running since it is now attached */
315 audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
317 ast_channel_unlock(chan);
319 return 0;
322 /*! \brief Detach audiohook from channel
323 * \param audiohook Audiohook structure
324 * \return Returns 0 on success, -1 on failure
326 int ast_audiohook_detach(struct ast_audiohook *audiohook)
328 if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
329 return 0;
331 audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
333 while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
334 ast_audiohook_trigger_wait(audiohook);
336 return 0;
339 /*! \brief Detach audiohooks from list and destroy said list
340 * \param audiohook_list List of audiohooks
341 * \return Returns 0 on success, -1 on failure
343 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
345 int i = 0;
346 struct ast_audiohook *audiohook = NULL;
348 /* Drop any spies */
349 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
350 ast_audiohook_lock(audiohook);
351 AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
352 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
353 ast_cond_signal(&audiohook->trigger);
354 ast_audiohook_unlock(audiohook);
356 AST_LIST_TRAVERSE_SAFE_END
358 /* Drop any whispering sources */
359 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
360 ast_audiohook_lock(audiohook);
361 AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
362 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
363 ast_cond_signal(&audiohook->trigger);
364 ast_audiohook_unlock(audiohook);
366 AST_LIST_TRAVERSE_SAFE_END
368 /* Drop any manipulaters */
369 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
370 ast_audiohook_lock(audiohook);
371 AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
372 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
373 ast_audiohook_unlock(audiohook);
374 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
376 AST_LIST_TRAVERSE_SAFE_END
378 /* Drop translation paths if present */
379 for (i = 0; i < 2; i++) {
380 if (audiohook_list->in_translate[i].trans_pvt)
381 ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
382 if (audiohook_list->out_translate[i].trans_pvt)
383 ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
386 /* Free ourselves */
387 ast_free(audiohook_list);
389 return 0;
392 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
394 struct ast_audiohook *audiohook = NULL;
396 AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
397 if (!strcasecmp(audiohook->source, source))
398 return audiohook;
401 AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
402 if (!strcasecmp(audiohook->source, source))
403 return audiohook;
406 AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
407 if (!strcasecmp(audiohook->source, source))
408 return audiohook;
411 return NULL;
414 /*! \brief Detach specified source audiohook from channel
415 * \param chan Channel to detach from
416 * \param source Name of source to detach
417 * \return Returns 0 on success, -1 on failure
419 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
421 struct ast_audiohook *audiohook = NULL;
423 ast_channel_lock(chan);
425 /* Ensure the channel has audiohooks on it */
426 if (!chan->audiohooks) {
427 ast_channel_unlock(chan);
428 return -1;
431 audiohook = find_audiohook_by_source(chan->audiohooks, source);
433 ast_channel_unlock(chan);
435 if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
436 audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
438 return (audiohook ? 0 : -1);
441 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
442 * \param chan Channel that the list is coming off of
443 * \param audiohook_list List of audiohooks
444 * \param direction Direction frame is coming in from
445 * \param frame The frame itself
446 * \return Return frame on success, NULL on failure
448 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)
450 struct ast_audiohook *audiohook = NULL;
452 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
453 ast_audiohook_lock(audiohook);
454 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
455 AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
456 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
457 ast_audiohook_unlock(audiohook);
458 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
459 continue;
461 if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
462 audiohook->manipulate_callback(audiohook, chan, frame, direction);
463 ast_audiohook_unlock(audiohook);
465 AST_LIST_TRAVERSE_SAFE_END
467 return frame;
470 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
471 * \param chan Channel that the list is coming off of
472 * \param audiohook_list List of audiohooks
473 * \param direction Direction frame is coming in from
474 * \param frame The frame itself
475 * \return Return frame on success, NULL on failure
477 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)
479 struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
480 struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
481 struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
482 struct ast_audiohook *audiohook = NULL;
483 int samples = frame->samples;
485 /* If the frame coming in is not signed linear we have to send it through the in_translate path */
486 if (frame->subclass != AST_FORMAT_SLINEAR) {
487 if (in_translate->format != frame->subclass) {
488 if (in_translate->trans_pvt)
489 ast_translator_free_path(in_translate->trans_pvt);
490 if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
491 return frame;
492 in_translate->format = frame->subclass;
494 if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
495 return frame;
498 /* Queue up signed linear frame to each spy */
499 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
500 ast_audiohook_lock(audiohook);
501 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
502 AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
503 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
504 ast_cond_signal(&audiohook->trigger);
505 ast_audiohook_unlock(audiohook);
506 continue;
508 ast_audiohook_write_frame(audiohook, direction, middle_frame);
509 ast_audiohook_unlock(audiohook);
511 AST_LIST_TRAVERSE_SAFE_END
513 /* If this frame is being written out to the channel then we need to use whisper sources */
514 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
515 int i = 0;
516 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
517 memset(&combine_buf, 0, sizeof(combine_buf));
518 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
519 ast_audiohook_lock(audiohook);
520 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
521 AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
522 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
523 ast_cond_signal(&audiohook->trigger);
524 ast_audiohook_unlock(audiohook);
525 continue;
527 if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
528 /* Take audio from this whisper source and combine it into our main buffer */
529 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
530 ast_slinear_saturated_add(data1, data2);
532 ast_audiohook_unlock(audiohook);
534 AST_LIST_TRAVERSE_SAFE_END
535 /* We take all of the combined whisper sources and combine them into the audio being written out */
536 for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
537 ast_slinear_saturated_add(data1, data2);
538 end_frame = middle_frame;
541 /* Pass off frame to manipulate audiohooks */
542 if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
543 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
544 ast_audiohook_lock(audiohook);
545 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
546 AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
547 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
548 ast_audiohook_unlock(audiohook);
549 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
550 audiohook->manipulate_callback(audiohook, chan, NULL, direction);
551 continue;
553 /* Feed in frame to manipulation */
554 audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
555 ast_audiohook_unlock(audiohook);
557 AST_LIST_TRAVERSE_SAFE_END
558 end_frame = middle_frame;
561 /* Now we figure out what to do with our end frame (whether to transcode or not) */
562 if (middle_frame == end_frame) {
563 /* Middle frame was modified and became the end frame... let's see if we need to transcode */
564 if (end_frame->subclass != start_frame->subclass) {
565 if (out_translate->format != start_frame->subclass) {
566 if (out_translate->trans_pvt)
567 ast_translator_free_path(out_translate->trans_pvt);
568 if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
569 /* We can't transcode this... drop our middle frame and return the original */
570 ast_frfree(middle_frame);
571 return start_frame;
573 out_translate->format = start_frame->subclass;
575 /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
576 if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
577 /* Failed to transcode the frame... drop it and return the original */
578 ast_frfree(middle_frame);
579 return start_frame;
581 /* Here's the scoop... middle frame is no longer of use to us */
582 ast_frfree(middle_frame);
584 } else {
585 /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
586 ast_frfree(middle_frame);
589 return end_frame;
592 /*! \brief Pass a frame off to be handled by the audiohook core
593 * \param chan Channel that the list is coming off of
594 * \param audiohook_list List of audiohooks
595 * \param direction Direction frame is coming in from
596 * \param frame The frame itself
597 * \return Return frame on success, NULL on failure
599 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)
601 /* Pass off frame to it's respective list write function */
602 if (frame->frametype == AST_FRAME_VOICE)
603 return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
604 else if (frame->frametype == AST_FRAME_DTMF)
605 return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
606 else
607 return frame;
611 /*! \brief Wait for audiohook trigger to be triggered
612 * \param audiohook Audiohook to wait on
614 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
616 struct timeval tv;
617 struct timespec ts;
619 tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
620 ts.tv_sec = tv.tv_sec;
621 ts.tv_nsec = tv.tv_usec * 1000;
623 ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
625 return;