2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2007, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
21 * \brief Audiohooks Architecture
23 * \author Joshua Colp <jcolp@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/options.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/linkedlists.h"
43 #include "asterisk/audiohook.h"
44 #include "asterisk/slinfactory.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/translate.h"
48 struct ast_audiohook_translate
{
49 struct ast_trans_pvt
*trans_pvt
;
53 struct ast_audiohook_list
{
54 struct ast_audiohook_translate in_translate
[2];
55 struct ast_audiohook_translate out_translate
[2];
56 AST_LIST_HEAD_NOLOCK(, ast_audiohook
) spy_list
;
57 AST_LIST_HEAD_NOLOCK(, ast_audiohook
) whisper_list
;
58 AST_LIST_HEAD_NOLOCK(, ast_audiohook
) manipulate_list
;
61 /*! \brief Initialize an audiohook structure
62 * \param audiohook Audiohook structure
65 * \return Returns 0 on success, -1 on failure
67 int ast_audiohook_init(struct ast_audiohook
*audiohook
, enum ast_audiohook_type type
, const char *source
)
69 /* Need to keep the type and source */
70 audiohook
->type
= type
;
71 audiohook
->source
= source
;
73 /* Initialize lock that protects our audiohook */
74 ast_mutex_init(&audiohook
->lock
);
75 ast_cond_init(&audiohook
->trigger
, NULL
);
77 /* Setup the factories that are needed for this audiohook type */
79 case AST_AUDIOHOOK_TYPE_SPY
:
80 ast_slinfactory_init(&audiohook
->read_factory
);
81 case AST_AUDIOHOOK_TYPE_WHISPER
:
82 ast_slinfactory_init(&audiohook
->write_factory
);
88 /* Since we are just starting out... this audiohook is new */
89 audiohook
->status
= AST_AUDIOHOOK_STATUS_NEW
;
94 /*! \brief Destroys an audiohook structure
95 * \param audiohook Audiohook structure
96 * \return Returns 0 on success, -1 on failure
98 int ast_audiohook_destroy(struct ast_audiohook
*audiohook
)
100 /* Drop the factories used by this audiohook type */
101 switch (audiohook
->type
) {
102 case AST_AUDIOHOOK_TYPE_SPY
:
103 ast_slinfactory_destroy(&audiohook
->read_factory
);
104 case AST_AUDIOHOOK_TYPE_WHISPER
:
105 ast_slinfactory_destroy(&audiohook
->write_factory
);
111 /* Destroy translation path if present */
112 if (audiohook
->trans_pvt
)
113 ast_translator_free_path(audiohook
->trans_pvt
);
115 /* Lock and trigger be gone! */
116 ast_cond_destroy(&audiohook
->trigger
);
117 ast_mutex_destroy(&audiohook
->lock
);
122 /*! \brief Writes a frame into the audiohook structure
123 * \param audiohook Audiohook structure
124 * \param direction Direction the audio frame came from
125 * \param frame Frame to write in
126 * \return Returns 0 on success, -1 on failure
128 int ast_audiohook_write_frame(struct ast_audiohook
*audiohook
, enum ast_audiohook_direction direction
, struct ast_frame
*frame
)
130 struct ast_slinfactory
*factory
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
? &audiohook
->read_factory
: &audiohook
->write_factory
);
131 struct ast_slinfactory
*other_factory
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
? &audiohook
->write_factory
: &audiohook
->read_factory
);
132 struct timeval
*time
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
? &audiohook
->read_time
: &audiohook
->write_time
), previous_time
= *time
;
134 /* Update last feeding time to be current */
137 /* 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 */
138 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))) {
140 ast_log(LOG_DEBUG
, "Flushing audiohook %p so it remains in sync\n", audiohook
);
141 ast_slinfactory_flush(factory
);
142 ast_slinfactory_flush(other_factory
);
145 /* Write frame out to respective factory */
146 ast_slinfactory_feed(factory
, frame
);
148 /* If we need to notify the respective handler of this audiohook, do so */
149 if ((ast_test_flag(audiohook
, AST_AUDIOHOOK_TRIGGER_MODE
) == AST_AUDIOHOOK_TRIGGER_READ
) && (direction
== AST_AUDIOHOOK_DIRECTION_READ
)) {
150 ast_cond_signal(&audiohook
->trigger
);
151 } else if ((ast_test_flag(audiohook
, AST_AUDIOHOOK_TRIGGER_MODE
) == AST_AUDIOHOOK_TRIGGER_WRITE
) && (direction
== AST_AUDIOHOOK_DIRECTION_WRITE
)) {
152 ast_cond_signal(&audiohook
->trigger
);
153 } else if (ast_test_flag(audiohook
, AST_AUDIOHOOK_TRIGGER_SYNC
)) {
154 ast_cond_signal(&audiohook
->trigger
);
160 static struct ast_frame
*audiohook_read_frame_single(struct ast_audiohook
*audiohook
, size_t samples
, enum ast_audiohook_direction direction
)
162 struct ast_slinfactory
*factory
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
? &audiohook
->read_factory
: &audiohook
->write_factory
);
163 int vol
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
? audiohook
->options
.read_volume
: audiohook
->options
.write_volume
);
165 struct ast_frame frame
= {
166 .frametype
= AST_FRAME_VOICE
,
167 .subclass
= AST_FORMAT_SLINEAR
,
169 .datalen
= sizeof(buf
),
173 /* Ensure the factory is able to give us the samples we want */
174 if (samples
> ast_slinfactory_available(factory
))
177 /* Read data in from factory */
178 if (!ast_slinfactory_read(factory
, buf
, samples
))
181 /* If a volume adjustment needs to be applied apply it */
183 ast_frame_adjust_volume(&frame
, vol
);
185 return ast_frdup(&frame
);
188 static struct ast_frame
*audiohook_read_frame_both(struct ast_audiohook
*audiohook
, size_t samples
)
190 int i
= 0, usable_read
, usable_write
;
191 short buf1
[samples
], buf2
[samples
], *read_buf
= NULL
, *write_buf
= NULL
, *final_buf
= NULL
, *data1
= NULL
, *data2
= NULL
;
192 struct ast_frame frame
= {
193 .frametype
= AST_FRAME_VOICE
,
194 .subclass
= AST_FORMAT_SLINEAR
,
196 .datalen
= sizeof(buf1
),
200 /* Make sure both factories have the required samples */
201 usable_read
= (ast_slinfactory_available(&audiohook
->read_factory
) >= samples
? 1 : 0);
202 usable_write
= (ast_slinfactory_available(&audiohook
->write_factory
) >= samples
? 1 : 0);
204 if (!usable_read
&& !usable_write
) {
205 /* If both factories are unusable bail out */
207 ast_log(LOG_DEBUG
, "Read factory %p and write factory %p both fail to provide %zd samples\n", &audiohook
->read_factory
, &audiohook
->write_factory
, samples
);
211 /* If we want to provide only a read factory make sure we aren't waiting for other audio */
212 if (usable_read
&& !usable_write
&& (ast_tvdiff_ms(ast_tvnow(), audiohook
->write_time
) < (samples
/8)*2)) {
213 if (option_debug
> 2)
214 ast_log(LOG_DEBUG
, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook
->write_factory
);
218 /* If we want to provide only a write factory make sure we aren't waiting for other audio */
219 if (usable_write
&& !usable_read
&& (ast_tvdiff_ms(ast_tvnow(), audiohook
->read_time
) < (samples
/8)*2)) {
220 if (option_debug
> 2)
221 ast_log(LOG_DEBUG
, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook
->read_factory
);
225 /* Start with the read factory... if there are enough samples, read them in */
227 if (ast_slinfactory_read(&audiohook
->read_factory
, buf1
, samples
)) {
229 /* Adjust read volume if need be */
230 if (audiohook
->options
.read_volume
) {
232 short adjust_value
= abs(audiohook
->options
.read_volume
);
233 for (count
= 0; count
< samples
; count
++) {
234 if (audiohook
->options
.read_volume
> 0)
235 ast_slinear_saturated_multiply(&buf1
[count
], &adjust_value
);
236 else if (audiohook
->options
.read_volume
< 0)
237 ast_slinear_saturated_divide(&buf1
[count
], &adjust_value
);
241 } else if (option_debug
)
242 ast_log(LOG_DEBUG
, "Failed to get %zd samples from read factory %p\n", samples
, &audiohook
->read_factory
);
244 /* Move on to the write factory... if there are enough samples, read them in */
246 if (ast_slinfactory_read(&audiohook
->write_factory
, buf2
, samples
)) {
248 /* Adjust write volume if need be */
249 if (audiohook
->options
.write_volume
) {
251 short adjust_value
= abs(audiohook
->options
.write_volume
);
252 for (count
= 0; count
< samples
; count
++) {
253 if (audiohook
->options
.write_volume
> 0)
254 ast_slinear_saturated_multiply(&buf2
[count
], &adjust_value
);
255 else if (audiohook
->options
.write_volume
< 0)
256 ast_slinear_saturated_divide(&buf2
[count
], &adjust_value
);
260 } else if (option_debug
)
261 ast_log(LOG_DEBUG
, "Failed to get %zd samples from write factory %p\n", samples
, &audiohook
->write_factory
);
263 /* Basically we figure out which buffer to use... and if mixing can be done here */
264 if (!read_buf
&& !write_buf
)
266 else if (read_buf
&& write_buf
) {
267 for (i
= 0, data1
= read_buf
, data2
= write_buf
; i
< samples
; i
++, data1
++, data2
++)
268 ast_slinear_saturated_add(data1
, data2
);
275 /* Make the final buffer part of the frame, so it gets duplicated fine */
276 frame
.data
= final_buf
;
278 /* Yahoo, a combined copy of the audio! */
279 return ast_frdup(&frame
);
282 /*! \brief Reads a frame in from the audiohook structure
283 * \param audiohook Audiohook structure
284 * \param samples Number of samples wanted
285 * \param direction Direction the audio frame came from
286 * \param format Format of frame remote side wants back
287 * \return Returns frame on success, NULL on failure
289 struct ast_frame
*ast_audiohook_read_frame(struct ast_audiohook
*audiohook
, size_t samples
, enum ast_audiohook_direction direction
, int format
)
291 struct ast_frame
*read_frame
= NULL
, *final_frame
= NULL
;
293 if (!(read_frame
= (direction
== AST_AUDIOHOOK_DIRECTION_BOTH
? audiohook_read_frame_both(audiohook
, samples
) : audiohook_read_frame_single(audiohook
, samples
, direction
))))
296 /* If they don't want signed linear back out, we'll have to send it through the translation path */
297 if (format
!= AST_FORMAT_SLINEAR
) {
298 /* Rebuild translation path if different format then previously */
299 if (audiohook
->format
!= format
) {
300 if (audiohook
->trans_pvt
) {
301 ast_translator_free_path(audiohook
->trans_pvt
);
302 audiohook
->trans_pvt
= NULL
;
304 /* 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 */
305 if (!(audiohook
->trans_pvt
= ast_translator_build_path(format
, AST_FORMAT_SLINEAR
))) {
306 ast_frfree(read_frame
);
310 /* Convert to requested format, and allow the read in frame to be freed */
311 final_frame
= ast_translate(audiohook
->trans_pvt
, read_frame
, 1);
313 final_frame
= read_frame
;
319 /*! \brief Attach audiohook to channel
320 * \param chan Channel
321 * \param audiohook Audiohook structure
322 * \return Returns 0 on success, -1 on failure
324 int ast_audiohook_attach(struct ast_channel
*chan
, struct ast_audiohook
*audiohook
)
326 ast_channel_lock(chan
);
328 if (!chan
->audiohooks
) {
329 /* Whoops... allocate a new structure */
330 if (!(chan
->audiohooks
= ast_calloc(1, sizeof(*chan
->audiohooks
)))) {
331 ast_channel_unlock(chan
);
334 AST_LIST_HEAD_INIT_NOLOCK(&chan
->audiohooks
->spy_list
);
335 AST_LIST_HEAD_INIT_NOLOCK(&chan
->audiohooks
->whisper_list
);
336 AST_LIST_HEAD_INIT_NOLOCK(&chan
->audiohooks
->manipulate_list
);
339 /* Drop into respective list */
340 if (audiohook
->type
== AST_AUDIOHOOK_TYPE_SPY
)
341 AST_LIST_INSERT_TAIL(&chan
->audiohooks
->spy_list
, audiohook
, list
);
342 else if (audiohook
->type
== AST_AUDIOHOOK_TYPE_WHISPER
)
343 AST_LIST_INSERT_TAIL(&chan
->audiohooks
->whisper_list
, audiohook
, list
);
344 else if (audiohook
->type
== AST_AUDIOHOOK_TYPE_MANIPULATE
)
345 AST_LIST_INSERT_TAIL(&chan
->audiohooks
->manipulate_list
, audiohook
, list
);
347 /* Change status over to running since it is now attached */
348 audiohook
->status
= AST_AUDIOHOOK_STATUS_RUNNING
;
350 ast_channel_unlock(chan
);
355 /*! \brief Detach audiohook from channel
356 * \param audiohook Audiohook structure
357 * \return Returns 0 on success, -1 on failure
359 int ast_audiohook_detach(struct ast_audiohook
*audiohook
)
361 if (audiohook
->status
== AST_AUDIOHOOK_STATUS_DONE
)
364 audiohook
->status
= AST_AUDIOHOOK_STATUS_SHUTDOWN
;
366 while (audiohook
->status
!= AST_AUDIOHOOK_STATUS_DONE
)
367 ast_audiohook_trigger_wait(audiohook
);
372 /*! \brief Detach audiohooks from list and destroy said list
373 * \param audiohook_list List of audiohooks
374 * \return Returns 0 on success, -1 on failure
376 int ast_audiohook_detach_list(struct ast_audiohook_list
*audiohook_list
)
379 struct ast_audiohook
*audiohook
= NULL
;
382 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list
->spy_list
, audiohook
, list
) {
383 ast_audiohook_lock(audiohook
);
384 AST_LIST_REMOVE_CURRENT(&audiohook_list
->spy_list
, list
);
385 audiohook
->status
= AST_AUDIOHOOK_STATUS_DONE
;
386 ast_cond_signal(&audiohook
->trigger
);
387 ast_audiohook_unlock(audiohook
);
389 AST_LIST_TRAVERSE_SAFE_END
391 /* Drop any whispering sources */
392 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list
->whisper_list
, audiohook
, list
) {
393 ast_audiohook_lock(audiohook
);
394 AST_LIST_REMOVE_CURRENT(&audiohook_list
->whisper_list
, list
);
395 audiohook
->status
= AST_AUDIOHOOK_STATUS_DONE
;
396 ast_cond_signal(&audiohook
->trigger
);
397 ast_audiohook_unlock(audiohook
);
399 AST_LIST_TRAVERSE_SAFE_END
401 /* Drop any manipulaters */
402 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list
->manipulate_list
, audiohook
, list
) {
403 ast_audiohook_lock(audiohook
);
404 AST_LIST_REMOVE_CURRENT(&audiohook_list
->manipulate_list
, list
);
405 audiohook
->status
= AST_AUDIOHOOK_STATUS_DONE
;
406 ast_audiohook_unlock(audiohook
);
407 audiohook
->manipulate_callback(audiohook
, NULL
, NULL
, 0);
409 AST_LIST_TRAVERSE_SAFE_END
411 /* Drop translation paths if present */
412 for (i
= 0; i
< 2; i
++) {
413 if (audiohook_list
->in_translate
[i
].trans_pvt
)
414 ast_translator_free_path(audiohook_list
->in_translate
[i
].trans_pvt
);
415 if (audiohook_list
->out_translate
[i
].trans_pvt
)
416 ast_translator_free_path(audiohook_list
->out_translate
[i
].trans_pvt
);
420 ast_free(audiohook_list
);
425 static struct ast_audiohook
*find_audiohook_by_source(struct ast_audiohook_list
*audiohook_list
, const char *source
)
427 struct ast_audiohook
*audiohook
= NULL
;
429 AST_LIST_TRAVERSE(&audiohook_list
->spy_list
, audiohook
, list
) {
430 if (!strcasecmp(audiohook
->source
, source
))
434 AST_LIST_TRAVERSE(&audiohook_list
->whisper_list
, audiohook
, list
) {
435 if (!strcasecmp(audiohook
->source
, source
))
439 AST_LIST_TRAVERSE(&audiohook_list
->manipulate_list
, audiohook
, list
) {
440 if (!strcasecmp(audiohook
->source
, source
))
447 /*! \brief Detach specified source audiohook from channel
448 * \param chan Channel to detach from
449 * \param source Name of source to detach
450 * \return Returns 0 on success, -1 on failure
452 int ast_audiohook_detach_source(struct ast_channel
*chan
, const char *source
)
454 struct ast_audiohook
*audiohook
= NULL
;
456 ast_channel_lock(chan
);
458 /* Ensure the channel has audiohooks on it */
459 if (!chan
->audiohooks
) {
460 ast_channel_unlock(chan
);
464 audiohook
= find_audiohook_by_source(chan
->audiohooks
, source
);
466 ast_channel_unlock(chan
);
468 if (audiohook
&& audiohook
->status
!= AST_AUDIOHOOK_STATUS_DONE
)
469 audiohook
->status
= AST_AUDIOHOOK_STATUS_SHUTDOWN
;
471 return (audiohook
? 0 : -1);
474 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
475 * \param chan Channel that the list is coming off of
476 * \param audiohook_list List of audiohooks
477 * \param direction Direction frame is coming in from
478 * \param frame The frame itself
479 * \return Return frame on success, NULL on failure
481 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
)
483 struct ast_audiohook
*audiohook
= NULL
;
485 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list
->manipulate_list
, audiohook
, list
) {
486 ast_audiohook_lock(audiohook
);
487 if (audiohook
->status
!= AST_AUDIOHOOK_STATUS_RUNNING
) {
488 AST_LIST_REMOVE_CURRENT(&audiohook_list
->manipulate_list
, list
);
489 audiohook
->status
= AST_AUDIOHOOK_STATUS_DONE
;
490 ast_audiohook_unlock(audiohook
);
491 audiohook
->manipulate_callback(audiohook
, NULL
, NULL
, 0);
494 if (ast_test_flag(audiohook
, AST_AUDIOHOOK_WANTS_DTMF
))
495 audiohook
->manipulate_callback(audiohook
, chan
, frame
, direction
);
496 ast_audiohook_unlock(audiohook
);
498 AST_LIST_TRAVERSE_SAFE_END
503 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
504 * \param chan Channel that the list is coming off of
505 * \param audiohook_list List of audiohooks
506 * \param direction Direction frame is coming in from
507 * \param frame The frame itself
508 * \return Return frame on success, NULL on failure
510 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
)
512 struct ast_audiohook_translate
*in_translate
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
? &audiohook_list
->in_translate
[0] : &audiohook_list
->in_translate
[1]);
513 struct ast_audiohook_translate
*out_translate
= (direction
== AST_AUDIOHOOK_DIRECTION_READ
? &audiohook_list
->out_translate
[0] : &audiohook_list
->out_translate
[1]);
514 struct ast_frame
*start_frame
= frame
, *middle_frame
= frame
, *end_frame
= frame
;
515 struct ast_audiohook
*audiohook
= NULL
;
516 int samples
= frame
->samples
;
518 /* If the frame coming in is not signed linear we have to send it through the in_translate path */
519 if (frame
->subclass
!= AST_FORMAT_SLINEAR
) {
520 if (in_translate
->format
!= frame
->subclass
) {
521 if (in_translate
->trans_pvt
)
522 ast_translator_free_path(in_translate
->trans_pvt
);
523 if (!(in_translate
->trans_pvt
= ast_translator_build_path(AST_FORMAT_SLINEAR
, frame
->subclass
)))
525 in_translate
->format
= frame
->subclass
;
527 if (!(middle_frame
= ast_translate(in_translate
->trans_pvt
, frame
, 0)))
531 /* Queue up signed linear frame to each spy */
532 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list
->spy_list
, audiohook
, list
) {
533 ast_audiohook_lock(audiohook
);
534 if (audiohook
->status
!= AST_AUDIOHOOK_STATUS_RUNNING
) {
535 AST_LIST_REMOVE_CURRENT(&audiohook_list
->spy_list
, list
);
536 audiohook
->status
= AST_AUDIOHOOK_STATUS_DONE
;
537 ast_cond_signal(&audiohook
->trigger
);
538 ast_audiohook_unlock(audiohook
);
541 ast_audiohook_write_frame(audiohook
, direction
, middle_frame
);
542 ast_audiohook_unlock(audiohook
);
544 AST_LIST_TRAVERSE_SAFE_END
546 /* If this frame is being written out to the channel then we need to use whisper sources */
547 if (direction
== AST_AUDIOHOOK_DIRECTION_WRITE
&& !AST_LIST_EMPTY(&audiohook_list
->whisper_list
)) {
549 short read_buf
[samples
], combine_buf
[samples
], *data1
= NULL
, *data2
= NULL
;
550 memset(&combine_buf
, 0, sizeof(combine_buf
));
551 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list
->whisper_list
, audiohook
, list
) {
552 ast_audiohook_lock(audiohook
);
553 if (audiohook
->status
!= AST_AUDIOHOOK_STATUS_RUNNING
) {
554 AST_LIST_REMOVE_CURRENT(&audiohook_list
->whisper_list
, list
);
555 audiohook
->status
= AST_AUDIOHOOK_STATUS_DONE
;
556 ast_cond_signal(&audiohook
->trigger
);
557 ast_audiohook_unlock(audiohook
);
560 if (ast_slinfactory_available(&audiohook
->write_factory
) >= samples
&& ast_slinfactory_read(&audiohook
->write_factory
, read_buf
, samples
)) {
561 /* Take audio from this whisper source and combine it into our main buffer */
562 for (i
= 0, data1
= combine_buf
, data2
= read_buf
; i
< samples
; i
++, data1
++, data2
++)
563 ast_slinear_saturated_add(data1
, data2
);
565 ast_audiohook_unlock(audiohook
);
567 AST_LIST_TRAVERSE_SAFE_END
568 /* We take all of the combined whisper sources and combine them into the audio being written out */
569 for (i
= 0, data1
= middle_frame
->data
, data2
= combine_buf
; i
< samples
; i
++, data1
++, data2
++)
570 ast_slinear_saturated_add(data1
, data2
);
571 end_frame
= middle_frame
;
574 /* Pass off frame to manipulate audiohooks */
575 if (!AST_LIST_EMPTY(&audiohook_list
->manipulate_list
)) {
576 AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list
->manipulate_list
, audiohook
, list
) {
577 ast_audiohook_lock(audiohook
);
578 if (audiohook
->status
!= AST_AUDIOHOOK_STATUS_RUNNING
) {
579 AST_LIST_REMOVE_CURRENT(&audiohook_list
->manipulate_list
, list
);
580 audiohook
->status
= AST_AUDIOHOOK_STATUS_DONE
;
581 ast_audiohook_unlock(audiohook
);
582 /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
583 audiohook
->manipulate_callback(audiohook
, chan
, NULL
, direction
);
586 /* Feed in frame to manipulation */
587 audiohook
->manipulate_callback(audiohook
, chan
, middle_frame
, direction
);
588 ast_audiohook_unlock(audiohook
);
590 AST_LIST_TRAVERSE_SAFE_END
591 end_frame
= middle_frame
;
594 /* Now we figure out what to do with our end frame (whether to transcode or not) */
595 if (middle_frame
== end_frame
) {
596 /* Middle frame was modified and became the end frame... let's see if we need to transcode */
597 if (end_frame
->subclass
!= start_frame
->subclass
) {
598 if (out_translate
->format
!= start_frame
->subclass
) {
599 if (out_translate
->trans_pvt
)
600 ast_translator_free_path(out_translate
->trans_pvt
);
601 if (!(out_translate
->trans_pvt
= ast_translator_build_path(start_frame
->subclass
, AST_FORMAT_SLINEAR
))) {
602 /* We can't transcode this... drop our middle frame and return the original */
603 ast_frfree(middle_frame
);
606 out_translate
->format
= start_frame
->subclass
;
608 /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
609 if (!(end_frame
= ast_translate(out_translate
->trans_pvt
, middle_frame
, 0))) {
610 /* Failed to transcode the frame... drop it and return the original */
611 ast_frfree(middle_frame
);
614 /* Here's the scoop... middle frame is no longer of use to us */
615 ast_frfree(middle_frame
);
618 /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
619 ast_frfree(middle_frame
);
625 /*! \brief Pass a frame off to be handled by the audiohook core
626 * \param chan Channel that the list is coming off of
627 * \param audiohook_list List of audiohooks
628 * \param direction Direction frame is coming in from
629 * \param frame The frame itself
630 * \return Return frame on success, NULL on failure
632 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
)
634 /* Pass off frame to it's respective list write function */
635 if (frame
->frametype
== AST_FRAME_VOICE
)
636 return audio_audiohook_write_list(chan
, audiohook_list
, direction
, frame
);
637 else if (frame
->frametype
== AST_FRAME_DTMF
)
638 return dtmf_audiohook_write_list(chan
, audiohook_list
, direction
, frame
);
644 /*! \brief Wait for audiohook trigger to be triggered
645 * \param audiohook Audiohook to wait on
647 void ast_audiohook_trigger_wait(struct ast_audiohook
*audiohook
)
652 tv
= ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
653 ts
.tv_sec
= tv
.tv_sec
;
654 ts
.tv_nsec
= tv
.tv_usec
* 1000;
656 ast_cond_timedwait(&audiohook
->trigger
, &audiohook
->lock
, &ts
);