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