Merge audiohooks branch into trunk. This is a new API for developers to listen and...
[asterisk/asterisk.git] / main / audiohook.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Audiohooks Architecture
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <signal.h>
34 #include <errno.h>
35 #include <unistd.h>
36
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"
47
48 struct ast_audiohook_translate {
49         struct ast_trans_pvt *trans_pvt;
50         int format;
51 };
52
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;
59 };
60
61 /*! \brief Initialize an audiohook structure
62  * \param audiohook Audiohook structure
63  * \return Returns 0 on success, -1 on failure
64  */
65 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
66 {
67         /* Need to keep the type and source */
68         audiohook->type = type;
69         audiohook->source = source;
70
71         /* Initialize lock that protects our audiohook */
72         ast_mutex_init(&audiohook->lock);
73         ast_cond_init(&audiohook->trigger, NULL);
74
75         /* Setup the factories that are needed for this audiohook type */
76         switch (type) {
77         case AST_AUDIOHOOK_TYPE_SPY:
78                 ast_slinfactory_init(&audiohook->read_factory);
79         case AST_AUDIOHOOK_TYPE_WHISPER:
80                 ast_slinfactory_init(&audiohook->write_factory);
81                 break;
82         default:
83                 break;
84         }
85
86         /* Since we are just starting out... this audiohook is new */
87         audiohook->status = AST_AUDIOHOOK_STATUS_NEW;
88
89         return 0;
90 }
91
92 /*! \brief Destroys an audiohook structure
93  * \param audiohook Audiohook structure
94  * \return Returns 0 on success, -1 on failure
95  */
96 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
97 {
98         /* Drop the factories used by this audiohook type */
99         switch (audiohook->type) {
100         case AST_AUDIOHOOK_TYPE_SPY:
101                 ast_slinfactory_destroy(&audiohook->read_factory);
102         case AST_AUDIOHOOK_TYPE_WHISPER:
103                 ast_slinfactory_destroy(&audiohook->write_factory);
104                 break;
105         default:
106                 break;
107         }
108
109         /* Destroy translation path if present */
110         if (audiohook->trans_pvt)
111                 ast_translator_free_path(audiohook->trans_pvt);
112
113         /* Lock and trigger be gone! */
114         ast_cond_destroy(&audiohook->trigger);
115         ast_mutex_destroy(&audiohook->lock);
116
117         return 0;
118 }
119
120 /*! \brief Writes a frame into the audiohook structure
121  * \param audiohook Audiohook structure
122  * \param direction Direction the audio frame came from
123  * \param frame Frame to write in
124  * \return Returns 0 on success, -1 on failure
125  */
126 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
127 {
128         struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
129
130         /* Write frame out to respective factory */
131         ast_slinfactory_feed(factory, frame);
132
133         /* If we need to notify the respective handler of this audiohook, do so */
134         switch (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE)) {
135         case AST_AUDIOHOOK_TRIGGER_READ:
136                 if (direction == AST_AUDIOHOOK_DIRECTION_READ)
137                         ast_cond_signal(&audiohook->trigger);
138                 break;
139         case AST_AUDIOHOOK_TRIGGER_WRITE:
140                 if (direction == AST_AUDIOHOOK_DIRECTION_WRITE)
141                         ast_cond_signal(&audiohook->trigger);
142                 break;
143         default:
144                 break;
145         }
146
147         return 0;
148 }
149
150 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
151 {
152         struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
153         int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
154         short buf[samples];
155         struct ast_frame frame = {
156                 .frametype = AST_FRAME_VOICE,
157                 .subclass = AST_FORMAT_SLINEAR,
158                 .data = buf,
159                 .datalen = sizeof(buf),
160                 .samples = samples,
161         };
162
163         /* Ensure the factory is able to give us the samples we want */
164         if (samples > ast_slinfactory_available(factory))
165                 return NULL;
166         
167         /* Read data in from factory */
168         if (!ast_slinfactory_read(factory, buf, samples))
169                 return NULL;
170
171         /* If a volume adjustment needs to be applied apply it */
172         if (vol)
173                 ast_frame_adjust_volume(&frame, vol);
174
175         return ast_frdup(&frame);
176 }
177
178 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
179 {
180         int i = 0;
181         short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
182         struct ast_frame frame = {
183                 .frametype = AST_FRAME_VOICE,
184                 .subclass = AST_FORMAT_SLINEAR,
185                 .data = NULL,
186                 .datalen = sizeof(buf1),
187                 .samples = samples,
188         };
189
190         /* Start with the read factory... if there are enough samples, read them in */
191         if (ast_slinfactory_available(&audiohook->read_factory) >= samples) {
192                 if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples))
193                         read_buf = buf1;
194                 /* Adjust read volume if need be */
195                 if (audiohook->options.read_volume) {
196                         int count = 0;
197                         short adjust_value = abs(audiohook->options.read_volume);
198                         for (count = 0; count < samples; count++) {
199                                 if (audiohook->options.read_volume > 0)
200                                         ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
201                                 else if (audiohook->options.read_volume < 0)
202                                         ast_slinear_saturated_divide(&buf1[count], &adjust_value);
203                         }
204                 }
205         } else if (option_debug)
206                 ast_log(LOG_DEBUG, "Failed to get %zd samples from read factory %p\n", samples, &audiohook->read_factory);
207
208         /* Move on to the write factory... if there are enough samples, read them in */
209         if (ast_slinfactory_available(&audiohook->write_factory) >= samples) {
210                 if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples))
211                         write_buf = buf2;
212                 /* Adjust write volume if need be */
213                 if (audiohook->options.write_volume) {
214                         int count = 0;
215                         short adjust_value = abs(audiohook->options.write_volume);
216                         for (count = 0; count < samples; count++) {
217                                 if (audiohook->options.write_volume > 0)
218                                         ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
219                                 else if (audiohook->options.write_volume < 0)
220                                         ast_slinear_saturated_divide(&buf2[count], &adjust_value);
221                         }
222                 }
223         } else if (option_debug)
224                 ast_log(LOG_DEBUG, "Failed to get %zd samples from write factory %p\n", samples, &audiohook->write_factory);
225
226         /* Basically we figure out which buffer to use... and if mixing can be done here */
227         if (!read_buf && !write_buf)
228                 return NULL;
229         else if (read_buf && write_buf) {
230                 for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
231                         ast_slinear_saturated_add(data1, data2);
232                 final_buf = buf1;
233         } else if (read_buf)
234                 final_buf = buf1;
235         else if (write_buf)
236                 final_buf = buf2;
237
238         /* Make the final buffer part of the frame, so it gets duplicated fine */
239         frame.data = final_buf;
240
241         /* Yahoo, a combined copy of the audio! */
242         return ast_frdup(&frame);
243 }
244
245 /*! \brief Reads a frame in from the audiohook structure
246  * \param audiohook Audiohook structure
247  * \param samples Number of samples wanted
248  * \param direction Direction the audio frame came from
249  * \param format Format of frame remote side wants back
250  * \return Returns frame on success, NULL on failure
251  */
252 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
253 {
254         struct ast_frame *read_frame = NULL, *final_frame = NULL;
255
256         if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
257                 return NULL;
258
259         /* If they don't want signed linear back out, we'll have to send it through the translation path */
260         if (format != AST_FORMAT_SLINEAR) {
261                 /* Rebuild translation path if different format then previously */
262                 if (audiohook->format != format) {
263                         if (audiohook->trans_pvt) {
264                                 ast_translator_free_path(audiohook->trans_pvt);
265                                 audiohook->trans_pvt = NULL;
266                         }
267                         /* 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 */
268                         if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
269                                 ast_frfree(read_frame);
270                                 return NULL;
271                         }
272                 }
273                 /* Convert to requested format, and allow the read in frame to be freed */
274                 final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
275         } else {
276                 final_frame = read_frame;
277         }
278
279         return final_frame;
280 }
281
282 /*! \brief Attach audiohook to channel
283  * \param chan Channel
284  * \param audiohook Audiohook structure
285  * \return Returns 0 on success, -1 on failure
286  */
287 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
288 {
289         ast_channel_lock(chan);
290
291         if (!chan->audiohooks) {
292                 /* Whoops... allocate a new structure */
293                 if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
294                         ast_channel_unlock(chan);
295                         return -1;
296                 }
297                 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
298                 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
299                 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
300         }
301
302         /* Drop into respective list */
303         if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
304                 AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
305         else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
306                 AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
307         else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
308                 AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
309
310         /* Change status over to running since it is now attached */
311         audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
312
313         ast_channel_unlock(chan);
314
315         return 0;
316 }
317
318 /*! \brief Detach audiohook from channel
319  * \param audiohook Audiohook structure
320  * \return Returns 0 on success, -1 on failure
321  */
322 int ast_audiohook_detach(struct ast_audiohook *audiohook)
323 {
324         if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
325                 return 0;
326
327         audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
328
329         while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
330                 ast_audiohook_trigger_wait(audiohook);
331
332         return 0;
333 }
334
335 /*! \brief Detach audiohooks from list and destroy said list
336  * \param audiohook_list List of audiohooks
337  * \return Returns 0 on success, -1 on failure
338  */
339 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
340 {
341         int i = 0;
342         struct ast_audiohook *audiohook = NULL;
343
344         /* Drop any spies */
345         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
346                 ast_audiohook_lock(audiohook);
347                 AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
348                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
349                 ast_cond_signal(&audiohook->trigger);
350                 ast_audiohook_unlock(audiohook);
351         }
352         AST_LIST_TRAVERSE_SAFE_END
353
354         /* Drop any whispering sources */
355         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
356                 ast_audiohook_lock(audiohook);
357                 AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
358                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
359                 ast_cond_signal(&audiohook->trigger);
360                 ast_audiohook_unlock(audiohook);
361         }
362         AST_LIST_TRAVERSE_SAFE_END
363
364         /* Drop any manipulaters */
365         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
366                 ast_audiohook_lock(audiohook);
367                 ast_mutex_lock(&audiohook->lock);
368                 AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
369                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
370                 ast_audiohook_unlock(audiohook);
371                 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
372         }
373         AST_LIST_TRAVERSE_SAFE_END
374
375         /* Drop translation paths if present */
376         for (i = 0; i < 2; i++) {
377                 if (audiohook_list->in_translate[i].trans_pvt)
378                         ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
379                 if (audiohook_list->out_translate[i].trans_pvt)
380                         ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
381         }
382         
383         /* Free ourselves */
384         ast_free(audiohook_list);
385
386         return 0;
387 }
388
389 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
390 {
391         struct ast_audiohook *audiohook = NULL;
392
393         AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
394                 if (!strcasecmp(audiohook->source, source))
395                         return audiohook;
396         }
397
398         AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
399                 if (!strcasecmp(audiohook->source, source))
400                         return audiohook;
401         }
402
403         AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
404                 if (!strcasecmp(audiohook->source, source))
405                         return audiohook;
406         }
407
408         return NULL;
409 }
410
411 /*! \brief Detach specified source audiohook from channel
412  * \param chan Channel to detach from
413  * \param source Name of source to detach
414  * \return Returns 0 on success, -1 on failure
415  */
416 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
417 {
418         struct ast_audiohook *audiohook = NULL;
419
420         ast_channel_lock(chan);
421
422         /* Ensure the channel has audiohooks on it */
423         if (!chan->audiohooks) {
424                 ast_channel_unlock(chan);
425                 return -1;
426         }
427
428         audiohook = find_audiohook_by_source(chan->audiohooks, source);
429
430         ast_channel_unlock(chan);
431
432         if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
433                 audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
434
435         return (audiohook ? 0 : -1);
436 }
437
438 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
439  * \param chan Channel that the list is coming off of
440  * \param audiohook_list List of audiohooks
441  * \param direction Direction frame is coming in from
442  * \param frame The frame itself
443  * \return Return frame on success, NULL on failure
444  */
445 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)
446 {
447         struct ast_audiohook *audiohook = NULL;
448
449         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
450                 ast_audiohook_lock(audiohook);
451                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
452                         AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
453                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
454                         ast_audiohook_unlock(audiohook);
455                         audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
456                         continue;
457                 }
458                 if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
459                         audiohook->manipulate_callback(audiohook, chan, frame, direction);
460                 ast_audiohook_unlock(audiohook);
461         }
462         AST_LIST_TRAVERSE_SAFE_END
463
464         return frame;
465 }
466
467 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
468  * \param chan Channel that the list is coming off of
469  * \param audiohook_list List of audiohooks
470  * \param direction Direction frame is coming in from
471  * \param frame The frame itself
472  * \return Return frame on success, NULL on failure
473  */
474 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)
475 {
476         struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
477         struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
478         struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
479         struct ast_audiohook *audiohook = NULL;
480         int samples = frame->samples;
481         
482         /* If the frame coming in is not signed linear we have to send it through the in_translate path */
483         if (frame->subclass != AST_FORMAT_SLINEAR) {
484                 if (in_translate->format != frame->subclass) {
485                         if (in_translate->trans_pvt)
486                                 ast_translator_free_path(in_translate->trans_pvt);
487                         if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
488                                 return frame;
489                         in_translate->format = frame->subclass;
490                 }
491                 if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
492                         return frame;
493         }
494
495         /* Queue up signed linear frame to each spy */
496         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
497                 ast_audiohook_lock(audiohook);
498                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
499                         AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
500                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
501                         ast_cond_signal(&audiohook->trigger);
502                         ast_audiohook_unlock(audiohook);
503                         continue;
504                 }
505                 ast_audiohook_write_frame(audiohook, direction, middle_frame);
506                 ast_audiohook_unlock(audiohook);
507         }
508         AST_LIST_TRAVERSE_SAFE_END
509
510         /* If this frame is being written out to the channel then we need to use whisper sources */
511         if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
512                 int i = 0;
513                 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
514                 memset(&combine_buf, 0, sizeof(combine_buf));
515                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
516                         ast_audiohook_lock(audiohook);
517                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
518                                 AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
519                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
520                                 ast_cond_signal(&audiohook->trigger);
521                                 ast_audiohook_unlock(audiohook);
522                                 continue;
523                         }
524                         if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
525                                 /* Take audio from this whisper source and combine it into our main buffer */
526                                 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
527                                         ast_slinear_saturated_add(data1, data2);
528                         }
529                         ast_audiohook_unlock(audiohook);
530                 }
531                 AST_LIST_TRAVERSE_SAFE_END
532                 /* We take all of the combined whisper sources and combine them into the audio being written out */
533                 for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
534                         ast_slinear_saturated_add(data1, data2);
535                 end_frame = middle_frame;
536         }
537
538         /* Pass off frame to manipulate audiohooks */
539         if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
540                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
541                         ast_audiohook_lock(audiohook);
542                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
543                                 AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
544                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
545                                 ast_audiohook_unlock(audiohook);
546                                 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
547                                 audiohook->manipulate_callback(audiohook, chan, NULL, direction);
548                                 continue;
549                         }
550                         /* Feed in frame to manipulation */
551                         audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
552                         ast_audiohook_unlock(audiohook);
553                 }
554                 AST_LIST_TRAVERSE_SAFE_END
555                 end_frame = middle_frame;
556         }
557
558         /* Now we figure out what to do with our end frame (whether to transcode or not) */
559         if (middle_frame == end_frame) {
560                 /* Middle frame was modified and became the end frame... let's see if we need to transcode */
561                 if (end_frame->subclass != start_frame->subclass) {
562                         if (out_translate->format != start_frame->subclass) {
563                                 if (out_translate->trans_pvt)
564                                         ast_translator_free_path(out_translate->trans_pvt);
565                                 if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
566                                         /* We can't transcode this... drop our middle frame and return the original */
567                                         ast_frfree(middle_frame);
568                                         return start_frame;
569                                 }
570                                 out_translate->format = start_frame->subclass;
571                         }
572                         /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
573                         if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
574                                 /* Failed to transcode the frame... drop it and return the original */
575                                 ast_frfree(middle_frame);
576                                 return start_frame;
577                         }
578                         /* Here's the scoop... middle frame is no longer of use to us */
579                         ast_frfree(middle_frame);
580                 }
581                 /* Yay let's rid ourselves of the start frame */
582                 ast_frfree(start_frame);
583         } else {
584                 /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
585                 ast_frfree(middle_frame);
586         }
587
588         return end_frame;
589 }
590
591 /*! \brief Pass a frame off to be handled by the audiohook core
592  * \param chan Channel that the list is coming off of
593  * \param audiohook_list List of audiohooks
594  * \param direction Direction frame is coming in from
595  * \param frame The frame itself
596  * \return Return frame on success, NULL on failure
597  */
598 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)
599 {
600         /* Pass off frame to it's respective list write function */
601         if (frame->frametype == AST_FRAME_VOICE)
602                 return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
603         else if (frame->frametype == AST_FRAME_DTMF)
604                 return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
605         else
606                 return frame;
607 }
608                         
609
610 /*! \brief Wait for audiohook trigger to be triggered
611  * \param audiohook Audiohook to wait on
612  */
613 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
614 {
615         struct timeval tv;
616         struct timespec ts;
617
618         tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
619         ts.tv_sec = tv.tv_sec;
620         ts.tv_nsec = tv.tv_usec * 1000;
621         
622         ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
623         
624         return;
625 }