Asterisk - The Open Source Telephony Project  GIT-master-a24979a
audiohook.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \brief Audiohooks Architecture
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include <signal.h>
33 
34 #include "asterisk/channel.h"
35 #include "asterisk/utils.h"
36 #include "asterisk/lock.h"
37 #include "asterisk/linkedlists.h"
38 #include "asterisk/audiohook.h"
39 #include "asterisk/slinfactory.h"
40 #include "asterisk/frame.h"
41 #include "asterisk/translate.h"
42 #include "asterisk/format_cache.h"
43 
44 #define AST_AUDIOHOOK_SYNC_TOLERANCE 100 /*!< Tolerance in milliseconds for audiohooks synchronization */
45 #define AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE 100 /*!< When small queue is enabled, this is the maximum amount of audio that can remain queued at a time. */
46 #define AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE 500 /*!< Otheriwise we still don't want the queue to grow indefinitely */
47 
48 #define DEFAULT_INTERNAL_SAMPLE_RATE 8000
49 
52  struct ast_format *format;
53 };
54 
56  /* If all the audiohooks in this list are capable
57  * of processing slinear at any sample rate, this
58  * variable will be set and the sample rate will
59  * be preserved during ast_audiohook_write_list()*/
61  int list_internal_samp_rate;/*!< Internal sample rate used when writing to the audiohook list */
62 
68 };
69 
70 static int audiohook_set_internal_rate(struct ast_audiohook *audiohook, int rate, int reset)
71 {
72  struct ast_format *slin;
73 
74  if (audiohook->hook_internal_samp_rate == rate) {
75  return 0;
76  }
77 
78  audiohook->hook_internal_samp_rate = rate;
79 
81 
82  /* Setup the factories that are needed for this audiohook type */
83  switch (audiohook->type) {
86  if (reset) {
89  }
92  break;
93  default:
94  break;
95  }
96 
97  return 0;
98 }
99 
100 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags init_flags)
101 {
102  /* Need to keep the type and source */
103  audiohook->type = type;
104  audiohook->source = source;
105 
106  /* Initialize lock that protects our audiohook */
107  ast_mutex_init(&audiohook->lock);
108  ast_cond_init(&audiohook->trigger, NULL);
109 
110  audiohook->init_flags = init_flags;
111 
112  /* initialize internal rate at 8khz, this will adjust if necessary */
114 
115  /* Since we are just starting out... this audiohook is new */
117 
118  return 0;
119 }
120 
121 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
122 {
123  /* Drop the factories used by this audiohook type */
124  switch (audiohook->type) {
129  break;
130  default:
131  break;
132  }
133 
134  /* Destroy translation path if present */
135  if (audiohook->trans_pvt)
137 
138  ao2_cleanup(audiohook->format);
139 
140  /* Lock and trigger be gone! */
141  ast_cond_destroy(&audiohook->trigger);
142  ast_mutex_destroy(&audiohook->lock);
143 
144  return 0;
145 }
146 
147 #define SHOULD_MUTE(hook, dir) \
148  ((ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ) && (dir == AST_AUDIOHOOK_DIRECTION_READ)) || \
149  (ast_test_flag(hook, AST_AUDIOHOOK_MUTE_WRITE) && (dir == AST_AUDIOHOOK_DIRECTION_WRITE)) || \
150  (ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE) == (AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE)))
151 
153 {
154  struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
155  struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory);
156  struct timeval *rwtime = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *rwtime;
157  int our_factory_samples;
158  int our_factory_ms;
159  int other_factory_samples;
160  int other_factory_ms;
161 
162  /* Update last feeding time to be current */
163  *rwtime = ast_tvnow();
164 
165  our_factory_samples = ast_slinfactory_available(factory);
166  our_factory_ms = ast_tvdiff_ms(*rwtime, previous_time) + (our_factory_samples / (audiohook->hook_internal_samp_rate / 1000));
167  other_factory_samples = ast_slinfactory_available(other_factory);
168  other_factory_ms = other_factory_samples / (audiohook->hook_internal_samp_rate / 1000);
169 
170  if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) {
171  ast_debug(4, "Flushing audiohook %p so it remains in sync\n", audiohook);
172  ast_slinfactory_flush(factory);
173  ast_slinfactory_flush(other_factory);
174  }
175 
176  if (ast_test_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE) && ((our_factory_ms > AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE) || (other_factory_ms > AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE))) {
177  ast_debug(4, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
178  ast_slinfactory_flush(factory);
179  ast_slinfactory_flush(other_factory);
180  } else if ((our_factory_ms > AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE) || (other_factory_ms > AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE)) {
181  ast_debug(4, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
182  ast_slinfactory_flush(factory);
183  ast_slinfactory_flush(other_factory);
184  }
185 
186  /* Write frame out to respective factory */
187  ast_slinfactory_feed(factory, frame);
188 
189  /* If we need to notify the respective handler of this audiohook, do so */
191  ast_cond_signal(&audiohook->trigger);
193  ast_cond_signal(&audiohook->trigger);
194  } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
195  ast_cond_signal(&audiohook->trigger);
196  }
197 
198  return 0;
199 }
200 
202 {
203  struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
204  int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
205  short buf[samples];
206  struct ast_frame frame = {
208  .subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate),
209  .data.ptr = buf,
210  .datalen = sizeof(buf),
211  .samples = samples,
212  };
213 
214  /* Ensure the factory is able to give us the samples we want */
215  if (samples > ast_slinfactory_available(factory)) {
216  return NULL;
217  }
218 
219  /* Read data in from factory */
220  if (!ast_slinfactory_read(factory, buf, samples)) {
221  return NULL;
222  }
223 
224  if (SHOULD_MUTE(audiohook, direction)) {
225  /* Swap frame data for zeros if mute is required */
226  ast_frame_clear(&frame);
227  } else if (vol) {
228  /* If a volume adjustment needs to be applied apply it */
229  ast_frame_adjust_volume(&frame, vol);
230  }
231 
232  return ast_frdup(&frame);
233 }
234 
235 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples, struct ast_frame **read_reference, struct ast_frame **write_reference)
236 {
237  int count;
238  int usable_read;
239  int usable_write;
240  short adjust_value;
241  short buf1[samples];
242  short buf2[samples];
243  short *read_buf = NULL;
244  short *write_buf = NULL;
245  struct ast_frame frame = {
247  .datalen = sizeof(buf1),
248  .samples = samples,
249  };
250 
251  /* Make sure both factories have the required samples */
252  usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
253  usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
254 
255  if (!usable_read && !usable_write) {
256  /* If both factories are unusable bail out */
257  ast_debug(3, "Read factory %p and write factory %p both fail to provide %zu samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
258  return NULL;
259  }
260 
261  /* If we want to provide only a read factory make sure we aren't waiting for other audio */
262  if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
263  ast_debug(3, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
264  return NULL;
265  }
266 
267  /* If we want to provide only a write factory make sure we aren't waiting for other audio */
268  if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*2)) {
269  ast_debug(3, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
270  return NULL;
271  }
272 
273  /* Start with the read factory... if there are enough samples, read them in */
274  if (usable_read) {
275  if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
276  read_buf = buf1;
277 
278  if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ))) {
279  /* Clear the frame data if we are muting */
280  memset(buf1, 0, sizeof(buf1));
281  } else if (audiohook->options.read_volume) {
282  /* Adjust read volume if need be */
283  adjust_value = abs(audiohook->options.read_volume);
284  for (count = 0; count < samples; count++) {
285  if (audiohook->options.read_volume > 0) {
286  ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
287  } else if (audiohook->options.read_volume < 0) {
288  ast_slinear_saturated_divide(&buf1[count], &adjust_value);
289  }
290  }
291  }
292  }
293  } else {
294  ast_debug(1, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory);
295  }
296 
297  /* Move on to the write factory... if there are enough samples, read them in */
298  if (usable_write) {
299  if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
300  write_buf = buf2;
301 
302  if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE))) {
303  /* Clear the frame data if we are muting */
304  memset(buf2, 0, sizeof(buf2));
305  } else if (audiohook->options.write_volume) {
306  /* Adjust write volume if need be */
307  adjust_value = abs(audiohook->options.write_volume);
308  for (count = 0; count < samples; count++) {
309  if (audiohook->options.write_volume > 0) {
310  ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
311  } else if (audiohook->options.write_volume < 0) {
312  ast_slinear_saturated_divide(&buf2[count], &adjust_value);
313  }
314  }
315  }
316  }
317  } else {
318  ast_debug(3, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory);
319  }
320 
322 
323  /* Should we substitute silence if one side lacks audio? */
325  if (read_reference && !read_buf && write_buf) {
326  read_buf = buf1;
327  memset(buf1, 0, sizeof(buf1));
328  } else if (write_reference && read_buf && !write_buf) {
329  write_buf = buf2;
330  memset(buf2, 0, sizeof(buf2));
331  }
332  }
333 
334  /* Basically we figure out which buffer to use... and if mixing can be done here */
335  if (read_buf && read_reference) {
336  frame.data.ptr = read_buf;
337  *read_reference = ast_frdup(&frame);
338  }
339  if (write_buf && write_reference) {
340  frame.data.ptr = write_buf;
341  *write_reference = ast_frdup(&frame);
342  }
343 
344  /* Make the correct buffer part of the built frame, so it gets duplicated. */
345  if (read_buf) {
346  frame.data.ptr = read_buf;
347  if (write_buf) {
348  for (count = 0; count < samples; count++) {
349  ast_slinear_saturated_add(read_buf++, write_buf++);
350  }
351  }
352  } else if (write_buf) {
353  frame.data.ptr = write_buf;
354  } else {
355  return NULL;
356  }
357 
358  /* Yahoo, a combined copy of the audio! */
359  return ast_frdup(&frame);
360 }
361 
362 static struct ast_frame *audiohook_read_frame_helper(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format, struct ast_frame **read_reference, struct ast_frame **write_reference)
363 {
364  struct ast_frame *read_frame = NULL, *final_frame = NULL;
365  struct ast_format *slin;
366 
367  /*
368  * Update the rate if compatibility mode is turned off or if it is
369  * turned on and the format rate is higher than the current rate.
370  *
371  * This makes it so any unnecessary rate switching/resetting does
372  * not take place and also any associated audiohook_list's internal
373  * sample rate maintains the highest sample rate between hooks.
374  */
375  if (!ast_test_flag(audiohook, AST_AUDIOHOOK_COMPATIBLE) ||
379  }
380 
381  /* If the sample rate of the requested format differs from that of the underlying audiohook
382  * sample rate determine how many samples we actually need to get from the audiohook. This
383  * needs to occur as the signed linear factory stores them at the rate of the audiohook.
384  * We do this by determining the duration of audio they've requested and then determining
385  * how many samples that would be in the audiohook format.
386  */
388  samples = (audiohook->hook_internal_samp_rate / 1000) * (samples / (ast_format_get_sample_rate(format) / 1000));
389  }
390 
392  audiohook_read_frame_both(audiohook, samples, read_reference, write_reference) :
393  audiohook_read_frame_single(audiohook, samples, direction)))) {
394  return NULL;
395  }
396 
398 
399  /* If they don't want signed linear back out, we'll have to send it through the translation path */
401  /* Rebuild translation path if different format then previously */
403  if (audiohook->trans_pvt) {
405  audiohook->trans_pvt = NULL;
406  }
407 
408  /* 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 */
409  if (!(audiohook->trans_pvt = ast_translator_build_path(format, slin))) {
411  return NULL;
412  }
413  ao2_replace(audiohook->format, format);
414  }
415  /* Convert to requested format, and allow the read in frame to be freed */
416  final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
417  } else {
418  final_frame = read_frame;
419  }
420 
421  return final_frame;
422 }
423 
425 {
427 }
428 
429 struct ast_frame *ast_audiohook_read_frame_all(struct ast_audiohook *audiohook, size_t samples, struct ast_format *format, struct ast_frame **read_frame, struct ast_frame **write_frame)
430 {
432 }
433 
435 {
436  struct ast_audiohook *ah = NULL;
437 
438  /*
439  * Anytime the samplerate compatibility is set (attach/remove an audiohook) the
440  * list's internal sample rate needs to be reset so that the next time processing
441  * through write_list, if needed, it will get updated to the correct rate.
442  *
443  * A list's internal rate always chooses the higher between its own rate and a
444  * given rate. If the current rate is being driven by an audiohook that wanted a
445  * higher rate then when this audiohook is removed the list's rate would remain
446  * at that level when it should be lower, and with no way to lower it since any
447  * rate compared against it would be lower.
448  *
449  * By setting it back to the lowest rate it can recalulate the new highest rate.
450  */
452 
453  audiohook_list->native_slin_compatible = 1;
454  AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, ah, list) {
456  audiohook_list->native_slin_compatible = 0;
457  return;
458  }
459  }
460 }
461 
462 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
463 {
464  ast_channel_lock(chan);
465 
466  /* Don't allow an audiohook to be attached to a channel that is already hung up.
467  * The hang up process is what actually notifies the audiohook that it should
468  * stop.
469  */
471  ast_channel_unlock(chan);
472  return -1;
473  }
474 
475  if (!ast_channel_audiohooks(chan)) {
476  struct ast_audiohook_list *ahlist;
477  /* Whoops... allocate a new structure */
478  if (!(ahlist = ast_calloc(1, sizeof(*ahlist)))) {
479  ast_channel_unlock(chan);
480  return -1;
481  }
482  ast_channel_audiohooks_set(chan, ahlist);
486  /* This sample rate will adjust as necessary when writing to the list. */
488  }
489 
490  /* Drop into respective list */
491  if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
492  AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
493  } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
494  AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
495  } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
497  }
498 
499  /*
500  * Initialize the audiohook's rate to the default. If it needs to be,
501  * it will get updated later.
502  */
505 
506  /* Change status over to running since it is now attached */
508 
509  if (ast_channel_is_bridged(chan)) {
511  }
512 
513  ast_channel_unlock(chan);
514 
515  return 0;
516 }
517 
519 {
520  ast_audiohook_lock(audiohook);
521  if (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
522  audiohook->status = status;
523  ast_cond_signal(&audiohook->trigger);
524  }
525  ast_audiohook_unlock(audiohook);
526 }
527 
528 int ast_audiohook_detach(struct ast_audiohook *audiohook)
529 {
530  if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
531  return 0;
532  }
533 
535 
536  while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
537  ast_audiohook_trigger_wait(audiohook);
538  }
539 
540  return 0;
541 }
542 
543 void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
544 {
545  int i;
546  struct ast_audiohook *audiohook;
547 
548  if (!audiohook_list) {
549  return;
550  }
551 
552  /* Drop any spies */
553  while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
555  }
556 
557  /* Drop any whispering sources */
558  while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
560  }
561 
562  /* Drop any manipulators */
563  while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
565  audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
566  }
567 
568  /* Drop translation paths if present */
569  for (i = 0; i < 2; i++) {
570  if (audiohook_list->in_translate[i].trans_pvt) {
571  ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
572  ao2_cleanup(audiohook_list->in_translate[i].format);
573  }
574  if (audiohook_list->out_translate[i].trans_pvt) {
576  ao2_cleanup(audiohook_list->in_translate[i].format);
577  }
578  }
579 
580  /* Free ourselves */
581  ast_free(audiohook_list);
582 }
583 
584 /*! \brief find an audiohook based on its source
585  * \param audiohook_list The list of audiohooks to search in
586  * \param source The source of the audiohook we wish to find
587  * \return corresponding audiohook
588  * \retval NULL if it cannot be found
589  */
590 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
591 {
592  struct ast_audiohook *audiohook = NULL;
593 
594  AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
595  if (!strcasecmp(audiohook->source, source)) {
596  return audiohook;
597  }
598  }
599 
600  AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
601  if (!strcasecmp(audiohook->source, source)) {
602  return audiohook;
603  }
604  }
605 
606  AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
607  if (!strcasecmp(audiohook->source, source)) {
608  return audiohook;
609  }
610  }
611 
612  return NULL;
613 }
614 
615 static void audiohook_move(struct ast_channel *old_chan, struct ast_channel *new_chan, struct ast_audiohook *audiohook)
616 {
617  enum ast_audiohook_status oldstatus;
618 
619  /* By locking both channels and the audiohook, we can assure that
620  * another thread will not have a chance to read the audiohook's status
621  * as done, even though ast_audiohook_remove signals the trigger
622  * condition.
623  */
624  ast_audiohook_lock(audiohook);
625  oldstatus = audiohook->status;
626 
627  ast_audiohook_remove(old_chan, audiohook);
628  ast_audiohook_attach(new_chan, audiohook);
629 
630  audiohook->status = oldstatus;
631  ast_audiohook_unlock(audiohook);
632 }
633 
634 void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
635 {
636  struct ast_audiohook *audiohook;
637 
638  if (!ast_channel_audiohooks(old_chan)) {
639  return;
640  }
641 
643  if (!audiohook) {
644  return;
645  }
646 
647  audiohook_move(old_chan, new_chan, audiohook);
648 }
649 
650 void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
651 {
652  struct ast_audiohook *audiohook;
653  struct ast_audiohook_list *audiohook_list;
654 
655  audiohook_list = ast_channel_audiohooks(old_chan);
656  if (!audiohook_list) {
657  return;
658  }
659 
660  AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
661  audiohook_move(old_chan, new_chan, audiohook);
662  }
664 
665  AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
666  audiohook_move(old_chan, new_chan, audiohook);
667  }
669 
670  AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
671  audiohook_move(old_chan, new_chan, audiohook);
672  }
674 }
675 
676 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
677 {
678  struct ast_audiohook *audiohook = NULL;
679 
680  ast_channel_lock(chan);
681 
682  /* Ensure the channel has audiohooks on it */
683  if (!ast_channel_audiohooks(chan)) {
684  ast_channel_unlock(chan);
685  return -1;
686  }
687 
689 
690  ast_channel_unlock(chan);
691 
692  if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
694  }
695 
696  return (audiohook ? 0 : -1);
697 }
698 
699 int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
700 {
701  ast_channel_lock(chan);
702 
703  if (!ast_channel_audiohooks(chan)) {
704  ast_channel_unlock(chan);
705  return -1;
706  }
707 
708  if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
709  AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
710  } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
711  AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
712  } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
713  AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
714  }
715 
718 
719  if (ast_channel_is_bridged(chan)) {
721  }
722 
723  ast_channel_unlock(chan);
724 
725  return 0;
726 }
727 
728 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
729  * \param chan Channel that the list is coming off of
730  * \param audiohook_list List of audiohooks
731  * \param direction Direction frame is coming in from
732  * \param frame The frame itself
733  * \return frame on success
734  * \retval NULL on failure
735  */
736 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)
737 {
738  struct ast_audiohook *audiohook = NULL;
739  int removed = 0;
740 
741  AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
742  ast_audiohook_lock(audiohook);
743  if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
745  removed = 1;
747  ast_audiohook_unlock(audiohook);
748  audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
749  if (ast_channel_is_bridged(chan)) {
751  }
752  continue;
753  }
754  if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF)) {
755  audiohook->manipulate_callback(audiohook, chan, frame, direction);
756  }
757  ast_audiohook_unlock(audiohook);
758  }
760 
761  /* if an audiohook got removed, reset samplerate compatibility */
762  if (removed) {
764  }
765  return frame;
766 }
767 
768 static struct ast_frame *audiohook_list_translate_to_slin(struct ast_audiohook_list *audiohook_list,
769  enum ast_audiohook_direction direction, struct ast_frame *frame)
770 {
772  &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
773  struct ast_frame *new_frame = frame;
774  struct ast_format *slin;
775 
776  /*
777  * If we are capable of sample rates other that 8khz, update the internal
778  * audiohook_list's rate and higher sample rate audio arrives. If native
779  * slin compatibility is turned on all audiohooks in the list will be
780  * updated as well during read/write processing.
781  */
782  audiohook_list->list_internal_samp_rate =
784 
786  if (ast_format_cmp(frame->subclass.format, slin) == AST_FORMAT_CMP_EQUAL) {
787  return new_frame;
788  }
789 
790  if (!in_translate->format ||
791  ast_format_cmp(frame->subclass.format, in_translate->format) != AST_FORMAT_CMP_EQUAL) {
792  struct ast_trans_pvt *new_trans;
793 
794  new_trans = ast_translator_build_path(slin, frame->subclass.format);
795  if (!new_trans) {
796  return NULL;
797  }
798 
799  if (in_translate->trans_pvt) {
800  ast_translator_free_path(in_translate->trans_pvt);
801  }
802  in_translate->trans_pvt = new_trans;
803 
804  ao2_replace(in_translate->format, frame->subclass.format);
805  }
806 
807  if (!(new_frame = ast_translate(in_translate->trans_pvt, frame, 0))) {
808  return NULL;
809  }
810 
811  return new_frame;
812 }
813 
815  enum ast_audiohook_direction direction, struct ast_frame *slin_frame, struct ast_format *outformat)
816 {
817  struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
818  struct ast_frame *outframe = NULL;
819  if (ast_format_cmp(slin_frame->subclass.format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
820  /* rebuild translators if necessary */
821  if (ast_format_cmp(out_translate->format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
822  if (out_translate->trans_pvt) {
823  ast_translator_free_path(out_translate->trans_pvt);
824  }
825  if (!(out_translate->trans_pvt = ast_translator_build_path(outformat, slin_frame->subclass.format))) {
826  return NULL;
827  }
828  ao2_replace(out_translate->format, outformat);
829  }
830  /* translate back to the format the frame came in as. */
831  if (!(outframe = ast_translate(out_translate->trans_pvt, slin_frame, 0))) {
832  return NULL;
833  }
834  }
835  return outframe;
836 }
837 
838 /*!
839  *\brief Set the audiohook's internal sample rate to the audiohook_list's rate,
840  * but only when native slin compatibility is turned on.
841  *
842  * \param audiohook_list audiohook_list data object
843  * \param audiohook the audiohook to update
844  * \param rate the current max internal sample rate
845  */
846 static void audiohook_list_set_hook_rate(struct ast_audiohook_list *audiohook_list,
847  struct ast_audiohook *audiohook, int *rate)
848 {
849  /* The rate should always be the max between itself and the hook */
850  if (audiohook->hook_internal_samp_rate > *rate) {
851  *rate = audiohook->hook_internal_samp_rate;
852  }
853 
854  /*
855  * If native slin compatibility is turned on then update the audiohook
856  * with the audiohook_list's current rate. Note, the audiohook's rate is
857  * set to the audiohook_list's rate and not the given rate. If there is
858  * a change in rate the hook's rate is changed on its next check.
859  */
860  if (audiohook_list->native_slin_compatible) {
862  audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
863  } else {
865  }
866 }
867 
868 /*!
869  * \brief Pass an AUDIO frame off to be handled by the audiohook core
870  *
871  * \details
872  * This function has 3 ast_frames and 3 parts to handle each. At the beginning of this
873  * function all 3 frames, start_frame, middle_frame, and end_frame point to the initial
874  * input frame.
875  *
876  * Part_1: Translate the start_frame into SLINEAR audio if it is not already in that
877  * format. The result of this part is middle_frame is guaranteed to be in
878  * SLINEAR format for Part_2.
879  * Part_2: Send middle_frame off to spies and manipulators. At this point middle_frame is
880  * either a new frame as result of the translation, or points directly to the start_frame
881  * because no translation to SLINEAR audio was required.
882  * Part_3: Translate end_frame's audio back into the format of start frame if necessary. This
883  * is only necessary if manipulation of middle_frame occurred.
884  *
885  * \param chan Channel that the list is coming off of
886  * \param audiohook_list List of audiohooks
887  * \param direction Direction frame is coming in from
888  * \param frame The frame itself
889  * \return frame on success
890  * \retval NULL on failure
891  */
892 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)
893 {
894  struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
895  struct ast_audiohook *audiohook = NULL;
896  int samples;
897  int middle_frame_manipulated = 0;
898  int removed = 0;
899  int internal_sample_rate;
900 
901  /* ---Part_1. translate start_frame to SLINEAR if necessary. */
902  if (!(middle_frame = audiohook_list_translate_to_slin(audiohook_list, direction, start_frame))) {
903  return frame;
904  }
905 
906  /* If the translation resulted in an interpolated frame then immediately return as audiohooks
907  * rely on actual media being present to do things.
908  */
909  if (!middle_frame->data.ptr) {
910  if (middle_frame != start_frame) {
911  ast_frfree(middle_frame);
912  }
913  return start_frame;
914  }
915 
916  samples = middle_frame->samples;
917 
918  /*
919  * While processing each audiohook check to see if the internal sample rate needs
920  * to be adjusted (it should be the highest rate specified between formats and
921  * hooks). The given audiohook_list's internal sample rate is then set to the
922  * updated value before returning.
923  *
924  * If slin compatibility mode is turned on then an audiohook's internal sample
925  * rate is set to its audiohook_list's rate. If an audiohook_list's rate is
926  * adjusted during this pass then the change is picked up by the audiohooks
927  * on the next pass.
928  */
929  internal_sample_rate = audiohook_list->list_internal_samp_rate;
930 
931  /* ---Part_2: Send middle_frame to spy and manipulator lists. middle_frame is guaranteed to be SLINEAR here.*/
932  /* Queue up signed linear frame to each spy */
933  AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
934  ast_audiohook_lock(audiohook);
935  if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
937  removed = 1;
939  ast_audiohook_unlock(audiohook);
940  if (ast_channel_is_bridged(chan)) {
942  }
943  continue;
944  }
945  audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
946  ast_audiohook_write_frame(audiohook, direction, middle_frame);
947  ast_audiohook_unlock(audiohook);
948  }
950 
951  /* If this frame is being written out to the channel then we need to use whisper sources */
952  if (!AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
953  int i = 0;
954  short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
955  memset(&combine_buf, 0, sizeof(combine_buf));
956  AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
957  struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
958  ast_audiohook_lock(audiohook);
959  if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
961  removed = 1;
963  ast_audiohook_unlock(audiohook);
964  if (ast_channel_is_bridged(chan)) {
966  }
967  continue;
968  }
969  audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
970  if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) {
971  /* Take audio from this whisper source and combine it into our main buffer */
972  for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++) {
973  ast_slinear_saturated_add(data1, data2);
974  }
975  }
976  ast_audiohook_unlock(audiohook);
977  }
979  /* We take all of the combined whisper sources and combine them into the audio being written out */
980  for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) {
981  ast_slinear_saturated_add(data1, data2);
982  }
983  middle_frame_manipulated = 1;
984  }
985 
986  /* Pass off frame to manipulate audiohooks */
987  if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
988  AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
989  ast_audiohook_lock(audiohook);
990  if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
992  removed = 1;
994  ast_audiohook_unlock(audiohook);
995  /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
996  audiohook->manipulate_callback(audiohook, chan, NULL, direction);
997  if (ast_channel_is_bridged(chan)) {
999  }
1000  continue;
1001  }
1002  audiohook_list_set_hook_rate(audiohook_list, audiohook, &internal_sample_rate);
1003  /*
1004  * Feed in frame to manipulation.
1005  */
1006  if (!audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) {
1007  /*
1008  * XXX FAILURES ARE IGNORED XXX
1009  * If the manipulation fails then the frame will be returned in its original state.
1010  * Since there are potentially more manipulator callbacks in the list, no action should
1011  * be taken here to exit early.
1012  */
1013  middle_frame_manipulated = 1;
1014  }
1015  ast_audiohook_unlock(audiohook);
1016  }
1018  }
1019 
1020  /* ---Part_3: Decide what to do with the end_frame (whether to transcode or not) */
1021  if (middle_frame_manipulated) {
1022  if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, start_frame->subclass.format))) {
1023  /* translation failed, so just pass back the input frame */
1024  end_frame = start_frame;
1025  }
1026  } else {
1027  end_frame = start_frame;
1028  }
1029  /* clean up our middle_frame if required */
1030  if (middle_frame != end_frame) {
1031  ast_frfree(middle_frame);
1032  middle_frame = NULL;
1033  }
1034 
1035  /* Before returning, if an audiohook got removed, reset samplerate compatibility */
1036  if (removed) {
1038  } else {
1039  /*
1040  * Set the audiohook_list's rate to the updated rate. Note that if a hook
1041  * was removed then the list's internal rate is reset to the default.
1042  */
1043  audiohook_list->list_internal_samp_rate = internal_sample_rate;
1044  }
1045 
1046  return end_frame;
1047 }
1048 
1050 {
1051  return !audiohook_list
1052  || (AST_LIST_EMPTY(&audiohook_list->spy_list)
1053  && AST_LIST_EMPTY(&audiohook_list->whisper_list)
1054  && AST_LIST_EMPTY(&audiohook_list->manipulate_list));
1055 }
1056 
1057 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)
1058 {
1059  /* Pass off frame to it's respective list write function */
1060  if (frame->frametype == AST_FRAME_VOICE) {
1061  return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
1062  } else if (frame->frametype == AST_FRAME_DTMF) {
1063  return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
1064  } else {
1065  return frame;
1066  }
1067 }
1068 
1069 /*! \brief Wait for audiohook trigger to be triggered
1070  * \param audiohook Audiohook to wait on
1071  */
1073 {
1074  struct timeval wait;
1075  struct timespec ts;
1076 
1077  wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
1078  ts.tv_sec = wait.tv_sec;
1079  ts.tv_nsec = wait.tv_usec * 1000;
1080 
1081  ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
1082 
1083  return;
1084 }
1085 
1086 /* Count number of channel audiohooks by type, regardless of type */
1088 {
1089  int count = 0;
1090  struct ast_audiohook *ah = NULL;
1091 
1092  if (!ast_channel_audiohooks(chan)) {
1093  return -1;
1094  }
1095 
1096  switch (type) {
1098  AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
1099  if (!strcmp(ah->source, source)) {
1100  count++;
1101  }
1102  }
1103  break;
1105  AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
1106  if (!strcmp(ah->source, source)) {
1107  count++;
1108  }
1109  }
1110  break;
1112  AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
1113  if (!strcmp(ah->source, source)) {
1114  count++;
1115  }
1116  }
1117  break;
1118  default:
1119  ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
1120  return -1;
1121  }
1122 
1123  return count;
1124 }
1125 
1126 /* Count number of channel audiohooks by type that are running */
1128 {
1129  int count = 0;
1130  struct ast_audiohook *ah = NULL;
1131  if (!ast_channel_audiohooks(chan))
1132  return -1;
1133 
1134  switch (type) {
1136  AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
1137  if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
1138  count++;
1139  }
1140  break;
1142  AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
1143  if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
1144  count++;
1145  }
1146  break;
1148  AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
1149  if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
1150  count++;
1151  }
1152  break;
1153  default:
1154  ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
1155  return -1;
1156  }
1157  return count;
1158 }
1159 
1160 /*! \brief Audiohook volume adjustment structure */
1162  struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
1163  int read_adjustment; /*!< Value to adjust frames read from the channel by */
1164  int write_adjustment; /*!< Value to adjust frames written to the channel by */
1165 };
1166 
1167 /*! \brief Callback used to destroy the audiohook volume datastore
1168  * \param data Volume information structure
1169  */
1170 static void audiohook_volume_destroy(void *data)
1171 {
1172  struct audiohook_volume *audiohook_volume = data;
1173 
1174  /* Destroy the audiohook as it is no longer in use */
1176 
1177  /* Finally free ourselves, we are of no more use */
1179 
1180  return;
1181 }
1182 
1183 /*! \brief Datastore used to store audiohook volume information */
1184 static const struct ast_datastore_info audiohook_volume_datastore = {
1185  .type = "Volume",
1186  .destroy = audiohook_volume_destroy,
1187 };
1188 
1189 /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
1190  * \param audiohook Audiohook attached to the channel
1191  * \param chan Channel we are attached to
1192  * \param frame Frame of audio we want to manipulate
1193  * \param direction Direction the audio came in from
1194  * \retval 0 on success
1195  * \retval -1 on failure
1196  */
1197 static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
1198 {
1199  struct ast_datastore *datastore = NULL;
1201  int *gain = NULL;
1202 
1203  /* If the audiohook is shutting down don't even bother */
1205  return 0;
1206  }
1207 
1208  /* Try to find the datastore containg adjustment information, if we can't just bail out */
1209  if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
1210  return 0;
1211  }
1212 
1213  audiohook_volume = datastore->data;
1214 
1215  /* Based on direction grab the appropriate adjustment value */
1218  } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
1220  }
1221 
1222  /* If an adjustment value is present modify the frame */
1223  if (gain && *gain) {
1224  ast_frame_adjust_volume(frame, *gain);
1225  }
1226 
1227  return 0;
1228 }
1229 
1230 /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
1231  * \param chan Channel to look on
1232  * \param create Whether to create the datastore if not found
1233  * \return audiohook_volume structure on success
1234  * \retval NULL on failure
1235  */
1236 static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
1237 {
1238  struct ast_datastore *datastore = NULL;
1240 
1241  /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
1242  if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
1243  return datastore->data;
1244  }
1245 
1246  /* 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 */
1247  if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
1248  return NULL;
1249  }
1250 
1251  /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
1252  if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
1253  ast_datastore_free(datastore);
1254  return NULL;
1255  }
1256 
1257  /* Setup our audiohook structure so we can manipulate the audio */
1260 
1261  /* Attach the audiohook_volume blob to the datastore and attach to the channel */
1262  datastore->data = audiohook_volume;
1263  ast_channel_datastore_add(chan, datastore);
1264 
1265  /* All is well... put the audiohook into motion */
1267 
1268  return audiohook_volume;
1269 }
1270 
1272 {
1274 
1275  /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
1276  if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
1277  return -1;
1278  }
1279 
1280  /* Now based on the direction set the proper value */
1283  }
1286  }
1287 
1288  return 0;
1289 }
1290 
1292 {
1294  int adjustment = 0;
1295 
1296  /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
1297  if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
1298  return 0;
1299  }
1300 
1301  /* Grab the adjustment value based on direction given */
1303  adjustment = audiohook_volume->read_adjustment;
1304  } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
1305  adjustment = audiohook_volume->write_adjustment;
1306  }
1307 
1308  return adjustment;
1309 }
1310 
1312 {
1314 
1315  /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
1316  if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
1317  return -1;
1318  }
1319 
1320  /* Based on the direction change the specific adjustment value */
1323  }
1326  }
1327 
1328  return 0;
1329 }
1330 
1331 int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
1332 {
1333  struct ast_audiohook *audiohook = NULL;
1334 
1335  ast_channel_lock(chan);
1336 
1337  /* Ensure the channel has audiohooks on it */
1338  if (!ast_channel_audiohooks(chan)) {
1339  ast_channel_unlock(chan);
1340  return -1;
1341  }
1342 
1344 
1345  if (audiohook) {
1346  if (clear) {
1347  ast_clear_flag(audiohook, flag);
1348  } else {
1349  ast_set_flag(audiohook, flag);
1350  }
1351  }
1352 
1353  ast_channel_unlock(chan);
1354 
1355  return (audiohook ? 0 : -1);
1356 }
jack_status_t status
Definition: app_jack.c:146
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
Definition: astobj2.h:501
static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
Helper function which actually gets called by audiohooks to perform the adjustment.
Definition: audiohook.c:1197
struct ast_frame * ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format)
Reads a frame in from the audiohook structure.
Definition: audiohook.c:424
static void audiohook_list_set_samplerate_compatibility(struct ast_audiohook_list *audiohook_list)
Definition: audiohook.c:434
static const struct ast_datastore_info audiohook_volume_datastore
Datastore used to store audiohook volume information.
Definition: audiohook.c:1184
static struct ast_audiohook * find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
find an audiohook based on its source
Definition: audiohook.c:590
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)
Pass a DTMF frame off to be handled by the audiohook core.
Definition: audiohook.c:736
int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
Mute frames read from or written to a channel.
Definition: audiohook.c:1331
int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
Writes a frame into the audiohook structure.
Definition: audiohook.c:152
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Definition: audiohook.c:699
int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
Detach specified source audiohook from channel.
Definition: audiohook.c:676
#define AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE
Definition: audiohook.c:46
struct ast_frame * ast_audiohook_read_frame_all(struct ast_audiohook *audiohook, size_t samples, struct ast_format *format, struct ast_frame **read_frame, struct ast_frame **write_frame)
Reads a frame in from the audiohook structure in mixed audio mode and copies read and write frame dat...
Definition: audiohook.c:429
int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
Retrieve the volume adjustment value on frames read from or written to a channel.
Definition: audiohook.c:1291
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags init_flags)
Initialize an audiohook structure.
Definition: audiohook.c:100
int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
Determine if a audiohook_list is empty or not.
Definition: audiohook.c:1049
void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
Wait for audiohook trigger to be triggered.
Definition: audiohook.c:1072
#define AST_AUDIOHOOK_SYNC_TOLERANCE
Definition: audiohook.c:44
void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
Detach audiohooks from list and destroy said list.
Definition: audiohook.c:543
void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
Update audiohook's status.
Definition: audiohook.c:518
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)
Pass a frame off to be handled by the audiohook core.
Definition: audiohook.c:1057
static void audiohook_move(struct ast_channel *old_chan, struct ast_channel *new_chan, struct ast_audiohook *audiohook)
Definition: audiohook.c:615
static struct ast_frame * audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples, struct ast_frame **read_reference, struct ast_frame **write_reference)
Definition: audiohook.c:235
static struct ast_frame * audiohook_list_translate_to_native(struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *slin_frame, struct ast_format *outformat)
Definition: audiohook.c:814
int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
Find out how many audiohooks from a certain source exist on a given channel, regardless of status.
Definition: audiohook.c:1087
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition: audiohook.c:528
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:462
int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
Find out how many spies of a certain type exist on a given channel, and are in state running.
Definition: audiohook.c:1127
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:121
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)
Pass an AUDIO frame off to be handled by the audiohook core.
Definition: audiohook.c:892
static struct ast_frame * audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
Definition: audiohook.c:201
static void audiohook_list_set_hook_rate(struct ast_audiohook_list *audiohook_list, struct ast_audiohook *audiohook, int *rate)
Set the audiohook's internal sample rate to the audiohook_list's rate, but only when native slin comp...
Definition: audiohook.c:846
static int audiohook_set_internal_rate(struct ast_audiohook *audiohook, int rate, int reset)
Definition: audiohook.c:70
int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
Adjust the volume on frames read from or written to a channel.
Definition: audiohook.c:1311
int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
Adjust the volume on frames read from or written to a channel.
Definition: audiohook.c:1271
void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
Move an audiohook from one channel to a new one.
Definition: audiohook.c:634
#define AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE
Definition: audiohook.c:45
void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
Move all audiohooks from one channel to another.
Definition: audiohook.c:650
#define DEFAULT_INTERNAL_SAMPLE_RATE
Definition: audiohook.c:48
static void audiohook_volume_destroy(void *data)
Callback used to destroy the audiohook volume datastore.
Definition: audiohook.c:1170
static struct ast_frame * audiohook_read_frame_helper(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format, struct ast_frame **read_reference, struct ast_frame **write_reference)
Definition: audiohook.c:362
static struct ast_frame * audiohook_list_translate_to_slin(struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
Definition: audiohook.c:768
#define SHOULD_MUTE(hook, dir)
Definition: audiohook.c:147
static struct audiohook_volume * audiohook_volume_get(struct ast_channel *chan, int create)
Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a chann...
Definition: audiohook.c:1236
Audiohooks Architecture.
ast_audiohook_init_flags
Definition: audiohook.h:71
@ AST_AUDIOHOOK_MANIPULATE_ALL_RATES
Definition: audiohook.h:75
ast_audiohook_direction
Definition: audiohook.h:48
@ AST_AUDIOHOOK_DIRECTION_READ
Definition: audiohook.h:49
@ AST_AUDIOHOOK_DIRECTION_WRITE
Definition: audiohook.h:50
@ AST_AUDIOHOOK_DIRECTION_BOTH
Definition: audiohook.h:51
#define ast_audiohook_lock(ah)
Lock an audiohook.
Definition: audiohook.h:304
ast_audiohook_flags
Definition: audiohook.h:54
@ AST_AUDIOHOOK_COMPATIBLE
Definition: audiohook.h:66
@ AST_AUDIOHOOK_WANTS_DTMF
Definition: audiohook.h:58
@ AST_AUDIOHOOK_TRIGGER_MODE
Definition: audiohook.h:55
@ AST_AUDIOHOOK_MUTE_READ
Definition: audiohook.h:64
@ AST_AUDIOHOOK_MUTE_WRITE
Definition: audiohook.h:65
@ AST_AUDIOHOOK_SUBSTITUTE_SILENCE
Definition: audiohook.h:68
@ AST_AUDIOHOOK_SMALL_QUEUE
Definition: audiohook.h:63
@ AST_AUDIOHOOK_TRIGGER_READ
Definition: audiohook.h:56
@ AST_AUDIOHOOK_TRIGGER_WRITE
Definition: audiohook.h:57
@ AST_AUDIOHOOK_TRIGGER_SYNC
Definition: audiohook.h:59
#define ast_audiohook_unlock(ah)
Unlock an audiohook.
Definition: audiohook.h:309
ast_audiohook_type
Definition: audiohook.h:35
@ AST_AUDIOHOOK_TYPE_MANIPULATE
Definition: audiohook.h:38
@ AST_AUDIOHOOK_TYPE_SPY
Definition: audiohook.h:36
@ AST_AUDIOHOOK_TYPE_WHISPER
Definition: audiohook.h:37
ast_audiohook_status
Definition: audiohook.h:41
@ AST_AUDIOHOOK_STATUS_DONE
Definition: audiohook.h:45
@ AST_AUDIOHOOK_STATUS_NEW
Definition: audiohook.h:42
@ AST_AUDIOHOOK_STATUS_RUNNING
Definition: audiohook.h:43
@ AST_AUDIOHOOK_STATUS_SHUTDOWN
Definition: audiohook.h:44
static snd_pcm_format_t format
Definition: chan_alsa.c:106
static const char type[]
Definition: chan_ooh323.c:109
General Asterisk PBX channel definitions.
void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
Variant of ast_channel_set_unbridged. Use this if the channel is already locked prior to calling.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2384
#define ast_channel_lock(chan)
Definition: channel.h:2922
struct ast_audiohook_list * ast_channel_audiohooks(const struct ast_channel *chan)
@ AST_FLAG_ZOMBIE
Definition: channel.h:987
void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
int ast_channel_is_bridged(const struct ast_channel *chan)
Determine if a channel is in a bridge.
Definition: channel.c:10731
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2398
#define ast_channel_unlock(chan)
Definition: channel.h:2923
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
void write_buf(int file, char *buffer, int num)
Definition: eagi_proxy.c:312
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define abs(x)
Definition: f2c.h:195
long int flag
Definition: f2c.h:83
static struct ast_frame * read_frame(struct ast_filestream *s, int *whennext)
Definition: file.c:909
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
@ AST_FORMAT_CMP_EQUAL
Definition: format.h:36
@ AST_FORMAT_CMP_NOT_EQUAL
Definition: format.h:38
Media Format Cache API.
struct ast_format * ast_format_cache_get_slin_by_rate(unsigned int rate)
Retrieve the best signed linear format given a sample rate.
Definition: format_cache.c:512
direction
static struct ast_threadstorage buf2
static struct ast_threadstorage buf1
Asterisk internal frame definitions.
int ast_frame_clear(struct ast_frame *frame)
Clear all audio samples from an ast_frame. The frame must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR.
Definition: main/frame.c:859
#define AST_FRAME_DTMF
#define ast_frdup(fr)
Copies a frame.
int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
Adjusts the volume of the audio samples contained in a frame.
Definition: main/frame.c:787
#define ast_frfree(fr)
@ AST_FRAME_VOICE
#define ast_debug(level,...)
Log a DEBUG message.
A set of macros to manage forward-linked lists.
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
Definition: linkedlists.h:450
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:856
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
Asterisk locking-related definitions:
#define ast_cond_destroy(cond)
Definition: lock.h:200
#define ast_cond_init(cond, attr)
Definition: lock.h:199
#define ast_cond_timedwait(cond, mutex, time)
Definition: lock.h:204
#define ast_mutex_init(pmutex)
Definition: lock.h:184
#define ast_mutex_destroy(a)
Definition: lock.h:186
#define ast_cond_signal(cond)
Definition: lock.h:201
AST_LIST_HEAD_NOLOCK(contactliststruct, contact)
#define NULL
Definition: resample.c:96
A machine to gather up arbitrary frames and convert them to raw slinear on demand.
int ast_slinfactory_init_with_format(struct ast_slinfactory *sf, struct ast_format *slin_out)
Initialize a slinfactory.
Definition: slinfactory.c:46
unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
Retrieve number of samples currently in a slinfactory.
Definition: slinfactory.c:199
int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
Read samples from a slinfactory.
Definition: slinfactory.c:145
void ast_slinfactory_flush(struct ast_slinfactory *sf)
Flush the contents of a slinfactory.
Definition: slinfactory.c:204
int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
Feed audio into a slinfactory.
Definition: slinfactory.c:77
void ast_slinfactory_destroy(struct ast_slinfactory *sf)
Destroy the contents of a slinfactory.
Definition: slinfactory.c:58
struct ast_audiohook_translate out_translate[2]
Definition: audiohook.c:64
int list_internal_samp_rate
Definition: audiohook.c:61
struct ast_audiohook_list::@332 spy_list
int native_slin_compatible
Definition: audiohook.c:60
struct ast_audiohook_list::@334 manipulate_list
struct ast_audiohook_list::@333 whisper_list
struct ast_audiohook_translate in_translate[2]
Definition: audiohook.c:63
struct ast_trans_pvt * trans_pvt
Definition: audiohook.c:51
struct ast_format * format
Definition: audiohook.c:52
ast_cond_t trigger
Definition: audiohook.h:106
struct timeval write_time
Definition: audiohook.h:115
enum ast_audiohook_type type
Definition: audiohook.h:107
struct timeval read_time
Definition: audiohook.h:114
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:118
unsigned int hook_internal_samp_rate
Definition: audiohook.h:120
struct ast_slinfactory read_factory
Definition: audiohook.h:112
struct ast_trans_pvt * trans_pvt
Definition: audiohook.h:117
struct ast_audiohook_options options
Definition: audiohook.h:119
enum ast_audiohook_init_flags init_flags
Definition: audiohook.h:109
enum ast_audiohook_status status
Definition: audiohook.h:108
struct ast_audiohook::@215 list
struct ast_format * format
Definition: audiohook.h:116
ast_mutex_t lock
Definition: audiohook.h:105
struct ast_slinfactory write_factory
Definition: audiohook.h:113
const char * source
Definition: audiohook.h:110
Main Channel structure associated with a channel.
Structure for a data store type.
Definition: datastore.h:31
const char * type
Definition: datastore.h:32
Structure for a data store object.
Definition: datastore.h:64
void * data
Definition: datastore.h:66
Definition of a media format.
Definition: format.c:43
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@254 data
enum ast_frame_type frametype
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:213
Audiohook volume adjustment structure.
Definition: audiohook.c:1161
struct ast_audiohook audiohook
Definition: audiohook.c:1162
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:245
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:105
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:157
Support for translation of data formats. translate.c.
struct ast_trans_pvt * ast_translator_build_path(struct ast_format *dest, struct ast_format *source)
Builds a translator path Build a path (possibly NULL) from source to dest.
Definition: translate.c:485
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:475
struct ast_frame * ast_translate(struct ast_trans_pvt *tr, struct ast_frame *f, int consume)
translates one or more frames Apply an input frame into the translator and receive zero or one output...
Definition: translate.c:565
Utility functions.
#define ast_test_flag(p, flag)
Definition: utils.h:63
static force_inline void ast_slinear_saturated_multiply(short *input, short *value)
Definition: utils.h:471
#define ast_clear_flag(p, flag)
Definition: utils.h:77
static force_inline void ast_slinear_saturated_add(short *input, short *value)
Definition: utils.h:445
#define ast_set_flag(p, flag)
Definition: utils.h:70
static force_inline void ast_slinear_saturated_divide(short *input, short *value)
Definition: utils.h:497
#define MAX(a, b)
Definition: utils.h:228