improve linked-list macros in two ways:
[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         while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
350                 ast_audiohook_lock(audiohook);
351                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
352                 ast_cond_signal(&audiohook->trigger);
353                 ast_audiohook_unlock(audiohook);
354         }
355
356         /* Drop any whispering sources */
357         while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
358                 ast_audiohook_lock(audiohook);
359                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
360                 ast_cond_signal(&audiohook->trigger);
361                 ast_audiohook_unlock(audiohook);
362         }
363
364         /* Drop any manipulaters */
365         while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
366                 ast_audiohook_lock(audiohook);
367                 ast_mutex_lock(&audiohook->lock);
368                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
369                 ast_audiohook_unlock(audiohook);
370                 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
371         }
372
373         /* Drop translation paths if present */
374         for (i = 0; i < 2; i++) {
375                 if (audiohook_list->in_translate[i].trans_pvt)
376                         ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
377                 if (audiohook_list->out_translate[i].trans_pvt)
378                         ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
379         }
380         
381         /* Free ourselves */
382         ast_free(audiohook_list);
383
384         return 0;
385 }
386
387 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
388 {
389         struct ast_audiohook *audiohook = NULL;
390
391         AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
392                 if (!strcasecmp(audiohook->source, source))
393                         return audiohook;
394         }
395
396         AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
397                 if (!strcasecmp(audiohook->source, source))
398                         return audiohook;
399         }
400
401         AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
402                 if (!strcasecmp(audiohook->source, source))
403                         return audiohook;
404         }
405
406         return NULL;
407 }
408
409 /*! \brief Detach specified source audiohook from channel
410  * \param chan Channel to detach from
411  * \param source Name of source to detach
412  * \return Returns 0 on success, -1 on failure
413  */
414 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
415 {
416         struct ast_audiohook *audiohook = NULL;
417
418         ast_channel_lock(chan);
419
420         /* Ensure the channel has audiohooks on it */
421         if (!chan->audiohooks) {
422                 ast_channel_unlock(chan);
423                 return -1;
424         }
425
426         audiohook = find_audiohook_by_source(chan->audiohooks, source);
427
428         ast_channel_unlock(chan);
429
430         if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
431                 audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
432
433         return (audiohook ? 0 : -1);
434 }
435
436 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
437  * \param chan Channel that the list is coming off of
438  * \param audiohook_list List of audiohooks
439  * \param direction Direction frame is coming in from
440  * \param frame The frame itself
441  * \return Return frame on success, NULL on failure
442  */
443 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)
444 {
445         struct ast_audiohook *audiohook = NULL;
446
447         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
448                 ast_audiohook_lock(audiohook);
449                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
450                         AST_LIST_REMOVE_CURRENT(list);
451                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
452                         ast_audiohook_unlock(audiohook);
453                         audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
454                         continue;
455                 }
456                 if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
457                         audiohook->manipulate_callback(audiohook, chan, frame, direction);
458                 ast_audiohook_unlock(audiohook);
459         }
460         AST_LIST_TRAVERSE_SAFE_END;
461
462         return frame;
463 }
464
465 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
466  * \param chan Channel that the list is coming off of
467  * \param audiohook_list List of audiohooks
468  * \param direction Direction frame is coming in from
469  * \param frame The frame itself
470  * \return Return frame on success, NULL on failure
471  */
472 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)
473 {
474         struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
475         struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
476         struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
477         struct ast_audiohook *audiohook = NULL;
478         int samples = frame->samples;
479         
480         /* If the frame coming in is not signed linear we have to send it through the in_translate path */
481         if (frame->subclass != AST_FORMAT_SLINEAR) {
482                 if (in_translate->format != frame->subclass) {
483                         if (in_translate->trans_pvt)
484                                 ast_translator_free_path(in_translate->trans_pvt);
485                         if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
486                                 return frame;
487                         in_translate->format = frame->subclass;
488                 }
489                 if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
490                         return frame;
491         }
492
493         /* Queue up signed linear frame to each spy */
494         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
495                 ast_audiohook_lock(audiohook);
496                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
497                         AST_LIST_REMOVE_CURRENT(list);
498                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
499                         ast_cond_signal(&audiohook->trigger);
500                         ast_audiohook_unlock(audiohook);
501                         continue;
502                 }
503                 ast_audiohook_write_frame(audiohook, direction, middle_frame);
504                 ast_audiohook_unlock(audiohook);
505         }
506         AST_LIST_TRAVERSE_SAFE_END
507
508         /* If this frame is being written out to the channel then we need to use whisper sources */
509         if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
510                 int i = 0;
511                 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
512                 memset(&combine_buf, 0, sizeof(combine_buf));
513                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
514                         ast_audiohook_lock(audiohook);
515                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
516                                 AST_LIST_REMOVE_CURRENT(list);
517                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
518                                 ast_cond_signal(&audiohook->trigger);
519                                 ast_audiohook_unlock(audiohook);
520                                 continue;
521                         }
522                         if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
523                                 /* Take audio from this whisper source and combine it into our main buffer */
524                                 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
525                                         ast_slinear_saturated_add(data1, data2);
526                         }
527                         ast_audiohook_unlock(audiohook);
528                 }
529                 AST_LIST_TRAVERSE_SAFE_END
530                 /* We take all of the combined whisper sources and combine them into the audio being written out */
531                 for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
532                         ast_slinear_saturated_add(data1, data2);
533                 end_frame = middle_frame;
534         }
535
536         /* Pass off frame to manipulate audiohooks */
537         if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
538                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
539                         ast_audiohook_lock(audiohook);
540                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
541                                 AST_LIST_REMOVE_CURRENT(list);
542                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
543                                 ast_audiohook_unlock(audiohook);
544                                 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
545                                 audiohook->manipulate_callback(audiohook, chan, NULL, direction);
546                                 continue;
547                         }
548                         /* Feed in frame to manipulation */
549                         audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
550                         ast_audiohook_unlock(audiohook);
551                 }
552                 AST_LIST_TRAVERSE_SAFE_END
553                 end_frame = middle_frame;
554         }
555
556         /* Now we figure out what to do with our end frame (whether to transcode or not) */
557         if (middle_frame == end_frame) {
558                 /* Middle frame was modified and became the end frame... let's see if we need to transcode */
559                 if (end_frame->subclass != start_frame->subclass) {
560                         if (out_translate->format != start_frame->subclass) {
561                                 if (out_translate->trans_pvt)
562                                         ast_translator_free_path(out_translate->trans_pvt);
563                                 if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
564                                         /* We can't transcode this... drop our middle frame and return the original */
565                                         ast_frfree(middle_frame);
566                                         return start_frame;
567                                 }
568                                 out_translate->format = start_frame->subclass;
569                         }
570                         /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
571                         if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
572                                 /* Failed to transcode the frame... drop it and return the original */
573                                 ast_frfree(middle_frame);
574                                 return start_frame;
575                         }
576                         /* Here's the scoop... middle frame is no longer of use to us */
577                         ast_frfree(middle_frame);
578                 }
579         } else {
580                 /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
581                 ast_frfree(middle_frame);
582         }
583
584         return end_frame;
585 }
586
587 /*! \brief Pass a frame off to be handled by the audiohook core
588  * \param chan Channel that the list is coming off of
589  * \param audiohook_list List of audiohooks
590  * \param direction Direction frame is coming in from
591  * \param frame The frame itself
592  * \return Return frame on success, NULL on failure
593  */
594 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)
595 {
596         /* Pass off frame to it's respective list write function */
597         if (frame->frametype == AST_FRAME_VOICE)
598                 return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
599         else if (frame->frametype == AST_FRAME_DTMF)
600                 return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
601         else
602                 return frame;
603 }
604                         
605
606 /*! \brief Wait for audiohook trigger to be triggered
607  * \param audiohook Audiohook to wait on
608  */
609 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
610 {
611         struct timeval tv;
612         struct timespec ts;
613
614         tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
615         ts.tv_sec = tv.tv_sec;
616         ts.tv_nsec = tv.tv_usec * 1000;
617         
618         ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
619         
620         return;
621 }