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