Merged revisions 113296 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 ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory);
125         struct timeval *time = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *time;
126
127         /* Update last feeding time to be current */
128         *time = ast_tvnow();
129
130         /* If we are using a sync trigger and this factory suddenly got audio fed in after a lapse, then flush both factories to ensure they remain in sync */
131         if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && ast_slinfactory_available(other_factory) && (ast_tvdiff_ms(*time, previous_time) > (ast_slinfactory_available(other_factory) / 8))) {
132                 if (option_debug)
133                         ast_log(LOG_DEBUG, "Flushing audiohook %p so it remains in sync\n", audiohook);
134                 ast_slinfactory_flush(factory);
135                 ast_slinfactory_flush(other_factory);
136         }
137
138         /* Write frame out to respective factory */
139         ast_slinfactory_feed(factory, frame);
140
141         /* If we need to notify the respective handler of this audiohook, do so */
142         if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
143                 ast_cond_signal(&audiohook->trigger);
144         } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
145                 ast_cond_signal(&audiohook->trigger);
146         } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
147                 ast_cond_signal(&audiohook->trigger);
148         }
149
150         return 0;
151 }
152
153 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
154 {
155         struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
156         int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
157         short buf[samples];
158         struct ast_frame frame = {
159                 .frametype = AST_FRAME_VOICE,
160                 .subclass = AST_FORMAT_SLINEAR,
161                 .data = buf,
162                 .datalen = sizeof(buf),
163                 .samples = samples,
164         };
165
166         /* Ensure the factory is able to give us the samples we want */
167         if (samples > ast_slinfactory_available(factory))
168                 return NULL;
169         
170         /* Read data in from factory */
171         if (!ast_slinfactory_read(factory, buf, samples))
172                 return NULL;
173
174         /* If a volume adjustment needs to be applied apply it */
175         if (vol)
176                 ast_frame_adjust_volume(&frame, vol);
177
178         return ast_frdup(&frame);
179 }
180
181 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
182 {
183         int i = 0, usable_read, usable_write;
184         short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
185         struct ast_frame frame = {
186                 .frametype = AST_FRAME_VOICE,
187                 .subclass = AST_FORMAT_SLINEAR,
188                 .data = NULL,
189                 .datalen = sizeof(buf1),
190                 .samples = samples,
191         };
192
193         /* Make sure both factories have the required samples */
194         usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
195         usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
196
197         if (!usable_read && !usable_write) {
198                 /* If both factories are unusable bail out */
199                 ast_debug(1, "Read factory %p and write factory %p both fail to provide %zd samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
200                 return NULL;
201         }
202
203         /* If we want to provide only a read factory make sure we aren't waiting for other audio */
204         if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
205                 ast_debug(1, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
206                 return NULL;
207         }
208
209         /* If we want to provide only a write factory make sure we aren't waiting for other audio */
210         if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
211                 ast_debug(1, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
212                 return NULL;
213         }
214
215         /* Start with the read factory... if there are enough samples, read them in */
216         if (usable_read && ast_slinfactory_available(&audiohook->read_factory) >= samples) {
217                 if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
218                         read_buf = buf1;
219                         /* Adjust read volume if need be */
220                         if (audiohook->options.read_volume) {
221                                 int count = 0;
222                                 short adjust_value = abs(audiohook->options.read_volume);
223                                 for (count = 0; count < samples; count++) {
224                                         if (audiohook->options.read_volume > 0)
225                                                 ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
226                                         else if (audiohook->options.read_volume < 0)
227                                                 ast_slinear_saturated_divide(&buf1[count], &adjust_value);
228                                 }
229                         }
230                 }
231         } else if (option_debug)
232                 ast_log(LOG_DEBUG, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory);
233
234         /* Move on to the write factory... if there are enough samples, read them in */
235         if (usable_write && ast_slinfactory_available(&audiohook->write_factory) >= samples) {
236                 if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
237                         write_buf = buf2;
238                         /* Adjust write volume if need be */
239                         if (audiohook->options.write_volume) {
240                                 int count = 0;
241                                 short adjust_value = abs(audiohook->options.write_volume);
242                                 for (count = 0; count < samples; count++) {
243                                         if (audiohook->options.write_volume > 0)
244                                                 ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
245                                         else if (audiohook->options.write_volume < 0)
246                                                 ast_slinear_saturated_divide(&buf2[count], &adjust_value);
247                                 }
248                         }
249                 }
250         } else if (option_debug)
251                 ast_log(LOG_DEBUG, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory);
252
253         /* Basically we figure out which buffer to use... and if mixing can be done here */
254         if (!read_buf && !write_buf)
255                 return NULL;
256         else if (read_buf && write_buf) {
257                 for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
258                         ast_slinear_saturated_add(data1, data2);
259                 final_buf = buf1;
260         } else if (read_buf)
261                 final_buf = buf1;
262         else if (write_buf)
263                 final_buf = buf2;
264
265         /* Make the final buffer part of the frame, so it gets duplicated fine */
266         frame.data = final_buf;
267
268         /* Yahoo, a combined copy of the audio! */
269         return ast_frdup(&frame);
270 }
271
272 /*! \brief Reads a frame in from the audiohook structure
273  * \param audiohook Audiohook structure
274  * \param samples Number of samples wanted
275  * \param direction Direction the audio frame came from
276  * \param format Format of frame remote side wants back
277  * \return Returns frame on success, NULL on failure
278  */
279 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
280 {
281         struct ast_frame *read_frame = NULL, *final_frame = NULL;
282
283         if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
284                 return NULL;
285
286         /* If they don't want signed linear back out, we'll have to send it through the translation path */
287         if (format != AST_FORMAT_SLINEAR) {
288                 /* Rebuild translation path if different format then previously */
289                 if (audiohook->format != format) {
290                         if (audiohook->trans_pvt) {
291                                 ast_translator_free_path(audiohook->trans_pvt);
292                                 audiohook->trans_pvt = NULL;
293                         }
294                         /* 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 */
295                         if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
296                                 ast_frfree(read_frame);
297                                 return NULL;
298                         }
299                 }
300                 /* Convert to requested format, and allow the read in frame to be freed */
301                 final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
302         } else {
303                 final_frame = read_frame;
304         }
305
306         return final_frame;
307 }
308
309 /*! \brief Attach audiohook to channel
310  * \param chan Channel
311  * \param audiohook Audiohook structure
312  * \return Returns 0 on success, -1 on failure
313  */
314 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
315 {
316         ast_channel_lock(chan);
317
318         if (!chan->audiohooks) {
319                 /* Whoops... allocate a new structure */
320                 if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
321                         ast_channel_unlock(chan);
322                         return -1;
323                 }
324                 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
325                 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
326                 AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
327         }
328
329         /* Drop into respective list */
330         if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
331                 AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
332         else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
333                 AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
334         else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
335                 AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
336
337         /* Change status over to running since it is now attached */
338         audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
339
340         ast_channel_unlock(chan);
341
342         return 0;
343 }
344
345 /*! \brief Detach audiohook from channel
346  * \param audiohook Audiohook structure
347  * \return Returns 0 on success, -1 on failure
348  */
349 int ast_audiohook_detach(struct ast_audiohook *audiohook)
350 {
351         if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
352                 return 0;
353
354         audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
355
356         while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
357                 ast_audiohook_trigger_wait(audiohook);
358
359         return 0;
360 }
361
362 /*! \brief Detach audiohooks from list and destroy said list
363  * \param audiohook_list List of audiohooks
364  * \return Returns 0 on success, -1 on failure
365  */
366 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
367 {
368         int i = 0;
369         struct ast_audiohook *audiohook = NULL;
370
371         /* Drop any spies */
372         while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
373                 ast_audiohook_lock(audiohook);
374                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
375                 ast_cond_signal(&audiohook->trigger);
376                 ast_audiohook_unlock(audiohook);
377         }
378
379         /* Drop any whispering sources */
380         while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
381                 ast_audiohook_lock(audiohook);
382                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
383                 ast_cond_signal(&audiohook->trigger);
384                 ast_audiohook_unlock(audiohook);
385         }
386
387         /* Drop any manipulaters */
388         while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
389                 ast_audiohook_lock(audiohook);
390                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
391                 ast_audiohook_unlock(audiohook);
392                 audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
393         }
394
395         /* Drop translation paths if present */
396         for (i = 0; i < 2; i++) {
397                 if (audiohook_list->in_translate[i].trans_pvt)
398                         ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
399                 if (audiohook_list->out_translate[i].trans_pvt)
400                         ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
401         }
402         
403         /* Free ourselves */
404         ast_free(audiohook_list);
405
406         return 0;
407 }
408
409 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
410 {
411         struct ast_audiohook *audiohook = NULL;
412
413         AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
414                 if (!strcasecmp(audiohook->source, source))
415                         return audiohook;
416         }
417
418         AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
419                 if (!strcasecmp(audiohook->source, source))
420                         return audiohook;
421         }
422
423         AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
424                 if (!strcasecmp(audiohook->source, source))
425                         return audiohook;
426         }
427
428         return NULL;
429 }
430
431 /*! \brief Detach specified source audiohook from channel
432  * \param chan Channel to detach from
433  * \param source Name of source to detach
434  * \return Returns 0 on success, -1 on failure
435  */
436 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
437 {
438         struct ast_audiohook *audiohook = NULL;
439
440         ast_channel_lock(chan);
441
442         /* Ensure the channel has audiohooks on it */
443         if (!chan->audiohooks) {
444                 ast_channel_unlock(chan);
445                 return -1;
446         }
447
448         audiohook = find_audiohook_by_source(chan->audiohooks, source);
449
450         ast_channel_unlock(chan);
451
452         if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
453                 audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
454
455         return (audiohook ? 0 : -1);
456 }
457
458 /*! \brief Pass a DTMF 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 *dtmf_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 *audiohook = NULL;
468
469         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
470                 ast_audiohook_lock(audiohook);
471                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
472                         AST_LIST_REMOVE_CURRENT(list);
473                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
474                         ast_audiohook_unlock(audiohook);
475                         audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
476                         continue;
477                 }
478                 if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
479                         audiohook->manipulate_callback(audiohook, chan, frame, direction);
480                 ast_audiohook_unlock(audiohook);
481         }
482         AST_LIST_TRAVERSE_SAFE_END;
483
484         return frame;
485 }
486
487 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
488  * \param chan Channel that the list is coming off of
489  * \param audiohook_list List of audiohooks
490  * \param direction Direction frame is coming in from
491  * \param frame The frame itself
492  * \return Return frame on success, NULL on failure
493  */
494 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)
495 {
496         struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
497         struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
498         struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
499         struct ast_audiohook *audiohook = NULL;
500         int samples = frame->samples;
501         
502         /* If the frame coming in is not signed linear we have to send it through the in_translate path */
503         if (frame->subclass != AST_FORMAT_SLINEAR) {
504                 if (in_translate->format != frame->subclass) {
505                         if (in_translate->trans_pvt)
506                                 ast_translator_free_path(in_translate->trans_pvt);
507                         if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
508                                 return frame;
509                         in_translate->format = frame->subclass;
510                 }
511                 if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
512                         return frame;
513         }
514
515         /* Queue up signed linear frame to each spy */
516         AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
517                 ast_audiohook_lock(audiohook);
518                 if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
519                         AST_LIST_REMOVE_CURRENT(list);
520                         audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
521                         ast_cond_signal(&audiohook->trigger);
522                         ast_audiohook_unlock(audiohook);
523                         continue;
524                 }
525                 ast_audiohook_write_frame(audiohook, direction, middle_frame);
526                 ast_audiohook_unlock(audiohook);
527         }
528         AST_LIST_TRAVERSE_SAFE_END
529
530         /* If this frame is being written out to the channel then we need to use whisper sources */
531         if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
532                 int i = 0;
533                 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
534                 memset(&combine_buf, 0, sizeof(combine_buf));
535                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
536                         ast_audiohook_lock(audiohook);
537                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
538                                 AST_LIST_REMOVE_CURRENT(list);
539                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
540                                 ast_cond_signal(&audiohook->trigger);
541                                 ast_audiohook_unlock(audiohook);
542                                 continue;
543                         }
544                         if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
545                                 /* Take audio from this whisper source and combine it into our main buffer */
546                                 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
547                                         ast_slinear_saturated_add(data1, data2);
548                         }
549                         ast_audiohook_unlock(audiohook);
550                 }
551                 AST_LIST_TRAVERSE_SAFE_END
552                 /* We take all of the combined whisper sources and combine them into the audio being written out */
553                 for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
554                         ast_slinear_saturated_add(data1, data2);
555                 end_frame = middle_frame;
556         }
557
558         /* Pass off frame to manipulate audiohooks */
559         if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
560                 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
561                         ast_audiohook_lock(audiohook);
562                         if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
563                                 AST_LIST_REMOVE_CURRENT(list);
564                                 audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
565                                 ast_audiohook_unlock(audiohook);
566                                 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
567                                 audiohook->manipulate_callback(audiohook, chan, NULL, direction);
568                                 continue;
569                         }
570                         /* Feed in frame to manipulation */
571                         audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
572                         ast_audiohook_unlock(audiohook);
573                 }
574                 AST_LIST_TRAVERSE_SAFE_END
575                 end_frame = middle_frame;
576         }
577
578         /* Now we figure out what to do with our end frame (whether to transcode or not) */
579         if (middle_frame == end_frame) {
580                 /* Middle frame was modified and became the end frame... let's see if we need to transcode */
581                 if (end_frame->subclass != start_frame->subclass) {
582                         if (out_translate->format != start_frame->subclass) {
583                                 if (out_translate->trans_pvt)
584                                         ast_translator_free_path(out_translate->trans_pvt);
585                                 if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
586                                         /* We can't transcode this... drop our middle frame and return the original */
587                                         ast_frfree(middle_frame);
588                                         return start_frame;
589                                 }
590                                 out_translate->format = start_frame->subclass;
591                         }
592                         /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
593                         if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
594                                 /* Failed to transcode the frame... drop it and return the original */
595                                 ast_frfree(middle_frame);
596                                 return start_frame;
597                         }
598                         /* Here's the scoop... middle frame is no longer of use to us */
599                         ast_frfree(middle_frame);
600                 }
601         } else {
602                 /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
603                 ast_frfree(middle_frame);
604         }
605
606         return end_frame;
607 }
608
609 /*! \brief Pass a frame off to be handled by the audiohook core
610  * \param chan Channel that the list is coming off of
611  * \param audiohook_list List of audiohooks
612  * \param direction Direction frame is coming in from
613  * \param frame The frame itself
614  * \return Return frame on success, NULL on failure
615  */
616 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)
617 {
618         /* Pass off frame to it's respective list write function */
619         if (frame->frametype == AST_FRAME_VOICE)
620                 return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
621         else if (frame->frametype == AST_FRAME_DTMF)
622                 return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
623         else
624                 return frame;
625 }
626                         
627
628 /*! \brief Wait for audiohook trigger to be triggered
629  * \param audiohook Audiohook to wait on
630  */
631 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
632 {
633         struct timeval tv;
634         struct timespec ts;
635
636         tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
637         ts.tv_sec = tv.tv_sec;
638         ts.tv_nsec = tv.tv_usec * 1000;
639         
640         ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
641         
642         return;
643 }
644
645 /* Count number of channel audiohooks by type, regardless of type */
646 int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
647 {
648         int count = 0;
649         struct ast_audiohook *ah = NULL;
650
651         if (!chan->audiohooks)
652                 return -1;
653
654         switch (type) {
655                 case AST_AUDIOHOOK_TYPE_SPY:
656                         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->spy_list, ah, list) {
657                                 if (!strcmp(ah->source, source)) {
658                                         count++;
659                                 }
660                         }
661                         AST_LIST_TRAVERSE_SAFE_END;
662                         break;
663                 case AST_AUDIOHOOK_TYPE_WHISPER:
664                         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->whisper_list, ah, list) {
665                                 if (!strcmp(ah->source, source)) {
666                                         count++;
667                                 }
668                         }
669                         AST_LIST_TRAVERSE_SAFE_END;
670                         break;
671                 case AST_AUDIOHOOK_TYPE_MANIPULATE:
672                         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->manipulate_list, ah, list) {
673                                 if (!strcmp(ah->source, source)) {
674                                         count++;
675                                 }
676                         }
677                         AST_LIST_TRAVERSE_SAFE_END;
678                         break;
679                 default:
680                         ast_log(LOG_DEBUG, "Invalid audiohook type supplied, (%d)\n", type);
681                         return -1;
682         }
683
684         return count;
685 }
686
687 /* Count number of channel audiohooks by type that are running */
688 int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
689 {
690         int count = 0;
691         struct ast_audiohook *ah = NULL;
692         if (!chan->audiohooks)
693                 return -1;
694
695         switch (type) {
696                 case AST_AUDIOHOOK_TYPE_SPY:
697                         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->spy_list, ah, list) {
698                                 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
699                                         count++;
700                         }
701                         AST_LIST_TRAVERSE_SAFE_END;
702                         break;
703                 case AST_AUDIOHOOK_TYPE_WHISPER:
704                         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->whisper_list, ah, list) {
705                                 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
706                                         count++;
707                         }
708                         AST_LIST_TRAVERSE_SAFE_END;
709                         break;
710                 case AST_AUDIOHOOK_TYPE_MANIPULATE:
711                         AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->manipulate_list, ah, list) {
712                                 if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
713                                         count++;
714                         }
715                         AST_LIST_TRAVERSE_SAFE_END;
716                         break;
717                 default:
718                         ast_log(LOG_DEBUG, "Invalid audiohook type supplied, (%d)\n", type);
719                         return -1;
720         }
721         return count;
722 }
723
724 /*! \brief Audiohook volume adjustment structure */
725 struct audiohook_volume {
726         struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
727         int read_adjustment;            /*!< Value to adjust frames read from the channel by */
728         int write_adjustment;           /*!< Value to adjust frames written to the channel by */
729 };
730
731 /*! \brief Callback used to destroy the audiohook volume datastore
732  * \param data Volume information structure
733  * \return Returns nothing
734  */
735 static void audiohook_volume_destroy(void *data)
736 {
737         struct audiohook_volume *audiohook_volume = data;
738
739         /* Destroy the audiohook as it is no longer in use */
740         ast_audiohook_destroy(&audiohook_volume->audiohook);
741
742         /* Finally free ourselves, we are of no more use */
743         ast_free(audiohook_volume);
744
745         return;
746 }
747
748 /*! \brief Datastore used to store audiohook volume information */
749 static const struct ast_datastore_info audiohook_volume_datastore = {
750         .type = "Volume",
751         .destroy = audiohook_volume_destroy,
752 };
753
754 /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
755  * \param audiohook Audiohook attached to the channel
756  * \param chan Channel we are attached to
757  * \param frame Frame of audio we want to manipulate
758  * \param direction Direction the audio came in from
759  * \return Returns 0 on success, -1 on failure
760  */
761 static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
762 {
763         struct ast_datastore *datastore = NULL;
764         struct audiohook_volume *audiohook_volume = NULL;
765         int *gain = NULL;
766
767         /* If the audiohook is shutting down don't even bother */
768         if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
769                 return 0;
770         }
771
772         /* Try to find the datastore containg adjustment information, if we can't just bail out */
773         if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
774                 return 0;
775         }
776
777         audiohook_volume = datastore->data;
778
779         /* Based on direction grab the appropriate adjustment value */
780         if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
781                 gain = &audiohook_volume->read_adjustment;
782         } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
783                 gain = &audiohook_volume->write_adjustment;
784         }
785
786         /* If an adjustment value is present modify the frame */
787         if (gain && *gain) {
788                 ast_frame_adjust_volume(frame, *gain);
789         }
790
791         return 0;
792 }
793
794 /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
795  * \param chan Channel to look on
796  * \param create Whether to create the datastore if not found
797  * \return Returns audiohook_volume structure on success, NULL on failure
798  */
799 static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
800 {
801         struct ast_datastore *datastore = NULL;
802         struct audiohook_volume *audiohook_volume = NULL;
803
804         /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
805         if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
806                 return datastore->data;
807         }
808
809         /* If we are not allowed to create a datastore or if we fail to create a datastore, bail out now as we have nothing for them */
810         if (!create || !(datastore = ast_channel_datastore_alloc(&audiohook_volume_datastore, NULL))) {
811                 return NULL;
812         }
813
814         /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
815         if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
816                 ast_channel_datastore_free(datastore);
817                 return NULL;
818         }
819
820         /* Setup our audiohook structure so we can manipulate the audio */
821         ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
822         audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
823
824         /* Attach the audiohook_volume blob to the datastore and attach to the channel */
825         datastore->data = audiohook_volume;
826         ast_channel_datastore_add(chan, datastore);
827
828         /* All is well... put the audiohook into motion */
829         ast_audiohook_attach(chan, &audiohook_volume->audiohook);
830
831         return audiohook_volume;
832 }
833
834 /*! \brief Adjust the volume on frames read from or written to a channel
835  * \param chan Channel to muck with
836  * \param direction Direction to set on
837  * \param volume Value to adjust the volume by
838  * \return Returns 0 on success, -1 on failure
839  */
840 int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
841 {
842         struct audiohook_volume *audiohook_volume = NULL;
843
844         /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
845         if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
846                 return -1;
847         }
848
849         /* Now based on the direction set the proper value */
850         if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
851                 audiohook_volume->read_adjustment = volume;
852         } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
853                 audiohook_volume->write_adjustment = volume;
854         }
855
856         return 0;
857 }
858
859 /*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
860  * \param chan Channel to retrieve volume adjustment from
861  * \param direction Direction to retrieve
862  * \return Returns adjustment value
863  */
864 int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
865 {
866         struct audiohook_volume *audiohook_volume = NULL;
867         int adjustment = 0;
868
869         /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
870         if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
871                 return 0;
872         }
873
874         /* Grab the adjustment value based on direction given */
875         if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
876                 adjustment = audiohook_volume->read_adjustment;
877         } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
878                 adjustment = audiohook_volume->write_adjustment;
879         }
880
881         return adjustment;
882 }
883
884 /*! \brief Adjust the volume on frames read from or written to a channel
885  * \param chan Channel to muck with
886  * \param direction Direction to increase
887  * \param volume Value to adjust the adjustment by
888  * \return Returns 0 on success, -1 on failure
889  */
890 int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
891 {
892         struct audiohook_volume *audiohook_volume = NULL;
893
894         /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
895         if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
896                 return -1;
897         }
898
899         /* Based on the direction change the specific adjustment value */
900         if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
901                 audiohook_volume->read_adjustment += volume;
902         } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
903                 audiohook_volume->write_adjustment += volume;
904         }
905
906         return 0;
907 }