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