Use the same delimited character as the FILTER function in FIELDQTY and CUT.
[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  * \param type
64  * \param source
65  * \return Returns 0 on success, -1 on failure
66  */
67 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
68 {
69         /* Need to keep the type and source */
70         audiohook->type = type;
71         audiohook->source = source;
72
73         /* Initialize lock that protects our audiohook */
74         ast_mutex_init(&audiohook->lock);
75         ast_cond_init(&audiohook->trigger, NULL);
76
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;
86         }
87
88         /* Since we are just starting out... this audiohook is new */
89         audiohook->status = AST_AUDIOHOOK_STATUS_NEW;
90
91         return 0;
92 }
93
94 /*! \brief Destroys an audiohook structure
95  * \param audiohook Audiohook structure
96  * \return Returns 0 on success, -1 on failure
97  */
98 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
99 {
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;
109         }
110
111         /* Destroy translation path if present */
112         if (audiohook->trans_pvt)
113                 ast_translator_free_path(audiohook->trans_pvt);
114
115         /* Lock and trigger be gone! */
116         ast_cond_destroy(&audiohook->trigger);
117         ast_mutex_destroy(&audiohook->lock);
118
119         return 0;
120 }
121
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
127  */
128 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
129 {
130         struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
131
132         /* Write frame out to respective factory */
133         ast_slinfactory_feed(factory, frame);
134
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;
147         }
148
149         return 0;
150 }
151
152 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
153 {
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,
163         };
164
165         /* Ensure the factory is able to give us the samples we want */
166         if (samples > ast_slinfactory_available(factory))
167                 return NULL;
168         
169         /* Read data in from factory */
170         if (!ast_slinfactory_read(factory, buf, samples))
171                 return NULL;
172
173         /* If a volume adjustment needs to be applied apply it */
174         if (vol)
175                 ast_frame_adjust_volume(&frame, vol);
176
177         return ast_frdup(&frame);
178 }
179
180 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
181 {
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,
190         };
191
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);
205                                 }
206                         }
207                 }
208         } else if (option_debug)
209                 ast_log(LOG_DEBUG, "Failed to get %zd samples from read factory %p\n", samples, &audiohook->read_factory);
210
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);
224                                 }
225                         }
226                 }
227         } else if (option_debug)
228                 ast_log(LOG_DEBUG, "Failed to get %zd samples from write factory %p\n", samples, &audiohook->write_factory);
229
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;
241
242         /* Make the final buffer part of the frame, so it gets duplicated fine */
243         frame.data = final_buf;
244
245         /* Yahoo, a combined copy of the audio! */
246         return ast_frdup(&frame);
247 }
248
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
255  */
256 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
257 {
258         struct ast_frame *read_frame = NULL, *final_frame = NULL;
259
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;
262
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;
270                         }
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;
275                         }
276                 }
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;
281         }
282
283         return final_frame;
284 }
285
286 /*! \brief Attach audiohook to channel
287  * \param chan Channel
288  * \param audiohook Audiohook structure
289  * \return Returns 0 on success, -1 on failure
290  */
291 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
292 {
293         ast_channel_lock(chan);
294
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;
300                 }
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);
304         }
305
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);
313
314         /* Change status over to running since it is now attached */
315         audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
316
317         ast_channel_unlock(chan);
318
319         return 0;
320 }
321
322 /*! \brief Detach audiohook from channel
323  * \param audiohook Audiohook structure
324  * \return Returns 0 on success, -1 on failure
325  */
326 int ast_audiohook_detach(struct ast_audiohook *audiohook)
327 {
328         if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
329                 return 0;
330
331         audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
332
333         while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
334                 ast_audiohook_trigger_wait(audiohook);
335
336         return 0;
337 }
338
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
342  */
343 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
344 {
345         int i = 0;
346         struct ast_audiohook *audiohook = NULL;
347
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);
355         }
356         AST_LIST_TRAVERSE_SAFE_END
357
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);
365         }
366         AST_LIST_TRAVERSE_SAFE_END
367
368         /* Drop any manipulaters */
369         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
370                 ast_audiohook_lock(audiohook);
371                 ast_mutex_lock(&audiohook->lock);
372                 AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
373                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
374                 ast_audiohook_unlock(audiohook);
375                 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
376         }
377         AST_LIST_TRAVERSE_SAFE_END
378
379         /* Drop translation paths if present */
380         for (i = 0; i < 2; i++) {
381                 if (audiohook_list->in_translate[i].trans_pvt)
382                         ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
383                 if (audiohook_list->out_translate[i].trans_pvt)
384                         ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
385         }
386         
387         /* Free ourselves */
388         ast_free(audiohook_list);
389
390         return 0;
391 }
392
393 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
394 {
395         struct ast_audiohook *audiohook = NULL;
396
397         AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
398                 if (!strcasecmp(audiohook->source, source))
399                         return audiohook;
400         }
401
402         AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
403                 if (!strcasecmp(audiohook->source, source))
404                         return audiohook;
405         }
406
407         AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
408                 if (!strcasecmp(audiohook->source, source))
409                         return audiohook;
410         }
411
412         return NULL;
413 }
414
415 /*! \brief Detach specified source audiohook from channel
416  * \param chan Channel to detach from
417  * \param source Name of source to detach
418  * \return Returns 0 on success, -1 on failure
419  */
420 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
421 {
422         struct ast_audiohook *audiohook = NULL;
423
424         ast_channel_lock(chan);
425
426         /* Ensure the channel has audiohooks on it */
427         if (!chan->audiohooks) {
428                 ast_channel_unlock(chan);
429                 return -1;
430         }
431
432         audiohook = find_audiohook_by_source(chan->audiohooks, source);
433
434         ast_channel_unlock(chan);
435
436         if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
437                 audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
438
439         return (audiohook ? 0 : -1);
440 }
441
442 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
443  * \param chan Channel that the list is coming off of
444  * \param audiohook_list List of audiohooks
445  * \param direction Direction frame is coming in from
446  * \param frame The frame itself
447  * \return Return frame on success, NULL on failure
448  */
449 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 {
451         struct ast_audiohook *audiohook = NULL;
452
453         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
454                 ast_audiohook_lock(audiohook);
455                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
456                         AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
457                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
458                         ast_audiohook_unlock(audiohook);
459                         audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
460                         continue;
461                 }
462                 if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
463                         audiohook->manipulate_callback(audiohook, chan, frame, direction);
464                 ast_audiohook_unlock(audiohook);
465         }
466         AST_LIST_TRAVERSE_SAFE_END
467
468         return frame;
469 }
470
471 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
472  * \param chan Channel that the list is coming off of
473  * \param audiohook_list List of audiohooks
474  * \param direction Direction frame is coming in from
475  * \param frame The frame itself
476  * \return Return frame on success, NULL on failure
477  */
478 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 {
480         struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
481         struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
482         struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
483         struct ast_audiohook *audiohook = NULL;
484         int samples = frame->samples;
485         
486         /* If the frame coming in is not signed linear we have to send it through the in_translate path */
487         if (frame->subclass != AST_FORMAT_SLINEAR) {
488                 if (in_translate->format != frame->subclass) {
489                         if (in_translate->trans_pvt)
490                                 ast_translator_free_path(in_translate->trans_pvt);
491                         if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
492                                 return frame;
493                         in_translate->format = frame->subclass;
494                 }
495                 if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
496                         return frame;
497         }
498
499         /* Queue up signed linear frame to each spy */
500         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
501                 ast_audiohook_lock(audiohook);
502                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
503                         AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
504                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
505                         ast_cond_signal(&audiohook->trigger);
506                         ast_audiohook_unlock(audiohook);
507                         continue;
508                 }
509                 ast_audiohook_write_frame(audiohook, direction, middle_frame);
510                 ast_audiohook_unlock(audiohook);
511         }
512         AST_LIST_TRAVERSE_SAFE_END
513
514         /* If this frame is being written out to the channel then we need to use whisper sources */
515         if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
516                 int i = 0;
517                 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
518                 memset(&combine_buf, 0, sizeof(combine_buf));
519                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
520                         ast_audiohook_lock(audiohook);
521                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
522                                 AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
523                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
524                                 ast_cond_signal(&audiohook->trigger);
525                                 ast_audiohook_unlock(audiohook);
526                                 continue;
527                         }
528                         if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
529                                 /* Take audio from this whisper source and combine it into our main buffer */
530                                 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
531                                         ast_slinear_saturated_add(data1, data2);
532                         }
533                         ast_audiohook_unlock(audiohook);
534                 }
535                 AST_LIST_TRAVERSE_SAFE_END
536                 /* We take all of the combined whisper sources and combine them into the audio being written out */
537                 for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
538                         ast_slinear_saturated_add(data1, data2);
539                 end_frame = middle_frame;
540         }
541
542         /* Pass off frame to manipulate audiohooks */
543         if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
544                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
545                         ast_audiohook_lock(audiohook);
546                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
547                                 AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
548                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
549                                 ast_audiohook_unlock(audiohook);
550                                 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
551                                 audiohook->manipulate_callback(audiohook, chan, NULL, direction);
552                                 continue;
553                         }
554                         /* Feed in frame to manipulation */
555                         audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
556                         ast_audiohook_unlock(audiohook);
557                 }
558                 AST_LIST_TRAVERSE_SAFE_END
559                 end_frame = middle_frame;
560         }
561
562         /* Now we figure out what to do with our end frame (whether to transcode or not) */
563         if (middle_frame == end_frame) {
564                 /* Middle frame was modified and became the end frame... let's see if we need to transcode */
565                 if (end_frame->subclass != start_frame->subclass) {
566                         if (out_translate->format != start_frame->subclass) {
567                                 if (out_translate->trans_pvt)
568                                         ast_translator_free_path(out_translate->trans_pvt);
569                                 if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
570                                         /* We can't transcode this... drop our middle frame and return the original */
571                                         ast_frfree(middle_frame);
572                                         return start_frame;
573                                 }
574                                 out_translate->format = start_frame->subclass;
575                         }
576                         /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
577                         if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
578                                 /* Failed to transcode the frame... drop it and return the original */
579                                 ast_frfree(middle_frame);
580                                 return start_frame;
581                         }
582                         /* Here's the scoop... middle frame is no longer of use to us */
583                         ast_frfree(middle_frame);
584                 }
585         } else {
586                 /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
587                 ast_frfree(middle_frame);
588         }
589
590         return end_frame;
591 }
592
593 /*! \brief Pass a frame off to be handled by the audiohook core
594  * \param chan Channel that the list is coming off of
595  * \param audiohook_list List of audiohooks
596  * \param direction Direction frame is coming in from
597  * \param frame The frame itself
598  * \return Return frame on success, NULL on failure
599  */
600 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 {
602         /* Pass off frame to it's respective list write function */
603         if (frame->frametype == AST_FRAME_VOICE)
604                 return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
605         else if (frame->frametype == AST_FRAME_DTMF)
606                 return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
607         else
608                 return frame;
609 }
610                         
611
612 /*! \brief Wait for audiohook trigger to be triggered
613  * \param audiohook Audiohook to wait on
614  */
615 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
616 {
617         struct timeval tv;
618         struct timespec ts;
619
620         tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
621         ts.tv_sec = tv.tv_sec;
622         ts.tv_nsec = tv.tv_usec * 1000;
623         
624         ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
625         
626         return;
627 }