44 #define AST_AUDIOHOOK_SYNC_TOLERANCE 100
45 #define AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE 100
46 #define AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE 500
48 #define DEFAULT_INTERNAL_SAMPLE_RATE 8000
60 int native_slin_compatible;
70 static
int audiohook_set_internal_rate(struct
ast_audiohook *audiohook,
int rate,
int reset)
74 if (audiohook->hook_internal_samp_rate == rate) {
78 audiohook->hook_internal_samp_rate = rate;
83 switch (audiohook->type) {
103 audiohook->
type = type;
104 audiohook->
source = source;
107 ast_mutex_init(&audiohook->
lock);
108 ast_cond_init(&audiohook->
trigger, NULL);
116 audiohook_set_internal_rate(audiohook, DEFAULT_INTERNAL_SAMPLE_RATE, 0);
127 switch (audiohook->
type) {
141 ao2_cleanup(audiohook->
format);
144 ast_cond_destroy(&audiohook->
trigger);
145 ast_mutex_destroy(&audiohook->
lock);
154 ast_debug(3,
"Can not set direction on attached Audiohook %p\n", audiohook);
162 #define SHOULD_MUTE(hook, dir) \
163 ((ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ) && (dir == AST_AUDIOHOOK_DIRECTION_READ)) || \
164 (ast_test_flag(hook, AST_AUDIOHOOK_MUTE_WRITE) && (dir == AST_AUDIOHOOK_DIRECTION_WRITE)) || \
165 (ast_test_flag(hook, AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE) == (AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE)))
172 int our_factory_samples;
174 int other_factory_samples;
175 int other_factory_ms;
193 ast_debug(4,
"Flushing audiohook %p so it remains in sync\n", audiohook);
199 ast_debug(4,
"Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
203 ast_debug(4,
"Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
213 ast_cond_signal(&audiohook->
trigger);
215 ast_cond_signal(&audiohook->
trigger);
217 ast_cond_signal(&audiohook->
trigger);
232 .datalen =
sizeof(buf),
246 if (SHOULD_MUTE(audiohook, direction)) {
265 short *read_buf = NULL;
266 short *write_buf = NULL;
269 .datalen =
sizeof(buf1),
277 if (!usable_read && !usable_write) {
285 ast_debug(3,
"Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->
write_factory);
291 ast_debug(3,
"Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->
read_factory);
302 memset(buf1, 0,
sizeof(buf1));
306 for (count = 0; count <
samples; count++) {
308 ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
310 ast_slinear_saturated_divide(&buf1[count], &adjust_value);
316 ast_debug(1,
"Failed to get %d samples from read factory %p\n", (
int)samples, &audiohook->
read_factory);
326 memset(buf2, 0,
sizeof(buf2));
330 for (count = 0; count <
samples; count++) {
332 ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
334 ast_slinear_saturated_divide(&buf2[count], &adjust_value);
340 ast_debug(3,
"Failed to get %d samples from write factory %p\n", (
int)samples, &audiohook->
write_factory);
347 if (read_reference && !read_buf && write_buf) {
349 memset(buf1, 0,
sizeof(buf1));
350 }
else if (write_reference && read_buf && !write_buf) {
352 memset(buf2, 0,
sizeof(buf2));
357 if (read_buf && read_reference) {
358 frame.
data.ptr = read_buf;
361 if (write_buf && write_reference) {
362 frame.
data.ptr = write_buf;
368 frame.
data.ptr = read_buf;
370 for (count = 0; count <
samples; count++) {
371 ast_slinear_saturated_add(read_buf++, write_buf++);
374 }
else if (write_buf) {
375 frame.
data.ptr = write_buf;
386 struct ast_frame *read_frame = NULL, *final_frame = NULL;
414 audiohook_read_frame_both(audiohook, samples, read_reference, write_reference) :
415 audiohook_read_frame_single(audiohook, samples, direction)))) {
432 ast_frfree(read_frame);
440 final_frame = read_frame;
448 return audiohook_read_frame_helper(audiohook, samples, direction, format, NULL, NULL);
456 static void audiohook_list_set_samplerate_compatibility(
struct ast_audiohook_list *audiohook_list)
475 audiohook_list->native_slin_compatible = 1;
478 audiohook_list->native_slin_compatible = 0;
486 ast_channel_lock(chan);
493 ast_channel_unlock(chan);
497 if (!ast_channel_audiohooks(chan)) {
500 if (!(ahlist =
ast_calloc(1,
sizeof(*ahlist)))) {
501 ast_channel_unlock(chan);
504 ast_channel_audiohooks_set(chan, ahlist);
525 audiohook_set_internal_rate(audiohook, DEFAULT_INTERNAL_SAMPLE_RATE, 1);
526 audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
535 ast_channel_unlock(chan);
544 audiohook->
status = status;
545 ast_cond_signal(&audiohook->
trigger);
570 if (!audiohook_list) {
591 for (i = 0; i < 2; i++) {
592 if (audiohook_list->in_translate[i].trans_pvt) {
594 ao2_cleanup(audiohook_list->in_translate[i].format);
596 if (audiohook_list->out_translate[i].trans_pvt) {
598 ao2_cleanup(audiohook_list->in_translate[i].format);
603 ast_free(audiohook_list);
617 if (!strcasecmp(audiohook->
source, source)) {
623 if (!strcasecmp(audiohook->
source, source)) {
629 if (!strcasecmp(audiohook->
source, source)) {
647 oldstatus = audiohook->
status;
652 audiohook->
status = oldstatus;
660 if (!ast_channel_audiohooks(old_chan)) {
669 audiohook_move(old_chan, new_chan, audiohook);
677 audiohook_list = ast_channel_audiohooks(old_chan);
678 if (!audiohook_list) {
683 audiohook_move(old_chan, new_chan, audiohook);
688 audiohook_move(old_chan, new_chan, audiohook);
693 audiohook_move(old_chan, new_chan, audiohook);
702 ast_channel_lock(chan);
705 if (!ast_channel_audiohooks(chan)) {
706 ast_channel_unlock(chan);
712 ast_channel_unlock(chan);
718 return (audiohook ? 0 : -1);
723 ast_channel_lock(chan);
725 if (!ast_channel_audiohooks(chan)) {
726 ast_channel_unlock(chan);
738 audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
745 ast_channel_unlock(chan);
785 audiohook_list_set_samplerate_compatibility(audiohook_list);
794 &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
812 if (!in_translate->format ||
821 if (in_translate->trans_pvt) {
824 in_translate->trans_pvt = new_trans;
829 if (!(new_frame =
ast_translate(in_translate->trans_pvt, frame, 0))) {
844 if (out_translate->trans_pvt) {
853 if (!(outframe =
ast_translate(out_translate->trans_pvt, slin_frame, 0))) {
882 if (audiohook_list->native_slin_compatible) {
916 struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
919 int middle_frame_manipulated = 0;
921 int internal_sample_rate;
924 if (!(middle_frame = audiohook_list_translate_to_slin(audiohook_list, direction, start_frame))) {
931 if (!middle_frame->data.ptr) {
932 if (middle_frame != start_frame) {
933 ast_frfree(middle_frame);
938 samples = middle_frame->
samples;
976 short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
977 memset(&combine_buf, 0,
sizeof(combine_buf));
994 for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++) {
995 ast_slinear_saturated_add(data1, data2);
1002 for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) {
1003 ast_slinear_saturated_add(data1, data2);
1005 middle_frame_manipulated = 1;
1035 middle_frame_manipulated = 1;
1043 if (middle_frame_manipulated) {
1044 if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, start_frame->
subclass.
format))) {
1046 end_frame = start_frame;
1049 end_frame = start_frame;
1052 if (middle_frame != end_frame) {
1053 ast_frfree(middle_frame);
1054 middle_frame = NULL;
1059 audiohook_list_set_samplerate_compatibility(audiohook_list);
1073 return !audiohook_list
1084 }
else if (frame->
frametype == AST_FRAME_DTMF) {
1096 struct timeval wait;
1100 ts.tv_sec = wait.tv_sec;
1101 ts.tv_nsec = wait.tv_usec * 1000;
1103 ast_cond_timedwait(&audiohook->
trigger, &audiohook->
lock, &ts);
1114 if (!ast_channel_audiohooks(chan)) {
1121 if (!strcmp(ah->
source, source)) {
1128 if (!strcmp(ah->
source, source)) {
1135 if (!strcmp(ah->
source, source)) {
1141 ast_debug(1,
"Invalid audiohook type supplied, (%u)\n", type);
1153 if (!ast_channel_audiohooks(chan))
1176 ast_debug(1,
"Invalid audiohook type supplied, (%u)\n", type);
1200 ast_free(audiohook_volume);
1235 audiohook_volume = datastore->
data;
1245 if (gain && *gain) {
1265 return datastore->
data;
1269 if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
1274 if (!(audiohook_volume =
ast_calloc(1,
sizeof(*audiohook_volume)))) {
1284 datastore->
data = audiohook_volume;
1290 return audiohook_volume;
1357 ast_channel_lock(chan);
1360 if (!ast_channel_audiohooks(chan)) {
1361 ast_channel_unlock(chan);
1369 ast_clear_flag(audiohook, flag);
1371 ast_set_flag(audiohook, flag);
1375 ast_channel_unlock(chan);
1377 return (audiohook ? 0 : -1);
1385 ast_channel_lock(chan);
1387 if (!ast_channel_audiohooks(chan)) {
1388 ast_channel_unlock(chan);
1393 if (!strcasecmp(audiohook->
source, source)) {
1396 ast_clear_flag(audiohook, flag);
1398 ast_set_flag(audiohook, flag);
1404 ast_channel_name(chan), source, count);
1406 ast_channel_unlock(chan);
struct ast_audiohook audiohook
Audiohook volume adjustment structure.
int ast_audiohook_set_mute_all(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clearmute)
Mute frames read from or written for all audiohooks on a channel.
Main Channel structure associated with a channel.
#define ast_frdup(fr)
Copies a frame.
struct ast_slinfactory write_factory
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
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.
static void audiohook_volume_destroy(void *data)
Callback used to destroy the audiohook volume datastore.
enum ast_audiohook_type type
void ast_slinfactory_flush(struct ast_slinfactory *sf)
Flush the contents of a slinfactory.
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.
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.
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
int list_internal_samp_rate
Support for translation of data formats. translate.c.
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. ...
void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
Update audiohook's status.
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.
int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
Feed audio into a slinfactory.
Structure for a data store type.
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...
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
#define AST_LIST_EMPTY(head)
Checks whether the specified list contains any entries.
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...
static const struct ast_datastore_info audiohook_volume_datastore
Datastore used to store audiohook volume information.
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Structure for a data store object.
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.
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
struct ast_format * format
unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
Retrieve number of samples currently in a slinfactory.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
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.
struct ast_frame_subclass subclass
void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
Detach audiohooks from list and destroy said list.
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...
#define AST_AUDIOHOOK_LONG_QUEUE_TOLERANCE
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
#define AST_AUDIOHOOK_SYNC_TOLERANCE
ast_audiohook_manipulate_callback manipulate_callback
#define ast_audiohook_unlock(ah)
Unlock an audiohook.
static struct ast_audiohook * find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
find an audiohook based on its source
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.
General Asterisk PBX channel definitions.
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
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.
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.
enum ast_audiohook_init_flags init_flags
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.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Asterisk internal frame definitions.
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 ...
struct ast_trans_pvt * trans_pvt
#define ast_debug(level,...)
Log a DEBUG message.
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...
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
enum ast_audiohook_direction direction
void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
Wait for audiohook trigger to be triggered.
void ast_slinfactory_destroy(struct ast_slinfactory *sf)
Destroy the contents of a slinfactory.
unsigned int hook_internal_samp_rate
#define ast_test_suite_event_notify(s, f,...)
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
int ast_slinfactory_init_with_format(struct ast_slinfactory *sf, struct ast_format *slin_out)
Initialize a slinfactory.
int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
Determine if a audiohook_list is empty or not.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
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...
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
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...
#define AST_AUDIOHOOK_SMALL_QUEUE_TOLERANCE
int ast_channel_is_bridged(const struct ast_channel *chan)
Determine if a channel is in a bridge.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
A machine to gather up arbitrary frames and convert them to raw slinear on demand.
union ast_frame::@224 data
#define ast_calloc(num, len)
A wrapper for calloc()
int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
Detach specified source audiohook from channel.
struct ast_slinfactory read_factory
struct ast_audiohook::@185 list
struct ast_audiohook_options options
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
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.
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
struct timeval write_time
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...
Data structure associated with a single frame of data.
int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
Read samples from a slinfactory.
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.
enum ast_audiohook_status status
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
enum ast_frame_type frametype
int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
Adjusts the volume of the audio samples contained in a frame.
void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
Move all audiohooks from one channel to another.
struct ast_format * format
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.
#define ast_audiohook_lock(ah)
Lock an audiohook.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
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.
int ast_audiohook_set_frame_feed_direction(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction)
Sets direction on audiohook.