audiohook.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2007, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Audiohooks Architecture
00022  *
00023  * \author Joshua Colp <jcolp@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 432812 $")
00033 
00034 #include <signal.h>
00035 
00036 #include "asterisk/channel.h"
00037 #include "asterisk/utils.h"
00038 #include "asterisk/lock.h"
00039 #include "asterisk/linkedlists.h"
00040 #include "asterisk/audiohook.h"
00041 #include "asterisk/slinfactory.h"
00042 #include "asterisk/frame.h"
00043 #include "asterisk/translate.h"
00044 #include "asterisk/format_cache.h"
00045 
00046 #define AST_AUDIOHOOK_SYNC_TOLERANCE 100 /*!< Tolerance in milliseconds for audiohooks synchronization */
00047 #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. */
00048 
00049 struct ast_audiohook_translate {
00050    struct ast_trans_pvt *trans_pvt;
00051    struct ast_format *format;
00052 };
00053 
00054 struct ast_audiohook_list {
00055    /* If all the audiohooks in this list are capable
00056     * of processing slinear at any sample rate, this
00057     * variable will be set and the sample rate will
00058     * be preserved during ast_audiohook_write_list()*/
00059    int native_slin_compatible;
00060    int list_internal_samp_rate;/*!< Internal sample rate used when writing to the audiohook list */
00061 
00062    struct ast_audiohook_translate in_translate[2];
00063    struct ast_audiohook_translate out_translate[2];
00064    AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
00065    AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
00066    AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
00067 };
00068 
00069 static int audiohook_set_internal_rate(struct ast_audiohook *audiohook, int rate, int reset)
00070 {
00071    struct ast_format *slin;
00072 
00073    if (audiohook->hook_internal_samp_rate == rate) {
00074       return 0;
00075    }
00076 
00077    audiohook->hook_internal_samp_rate = rate;
00078 
00079    slin = ast_format_cache_get_slin_by_rate(rate);
00080 
00081    /* Setup the factories that are needed for this audiohook type */
00082    switch (audiohook->type) {
00083    case AST_AUDIOHOOK_TYPE_SPY:
00084    case AST_AUDIOHOOK_TYPE_WHISPER:
00085       if (reset) {
00086          ast_slinfactory_destroy(&audiohook->read_factory);
00087          ast_slinfactory_destroy(&audiohook->write_factory);
00088       }
00089       ast_slinfactory_init_with_format(&audiohook->read_factory, slin);
00090       ast_slinfactory_init_with_format(&audiohook->write_factory, slin);
00091       break;
00092    default:
00093       break;
00094    }
00095 
00096    return 0;
00097 }
00098 
00099 /*! \brief Initialize an audiohook structure
00100  *
00101  * \param audiohook Audiohook structure
00102  * \param type
00103  * \param source, init_flags
00104  *
00105  * \return Returns 0 on success, -1 on failure
00106  */
00107 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags init_flags)
00108 {
00109    /* Need to keep the type and source */
00110    audiohook->type = type;
00111    audiohook->source = source;
00112 
00113    /* Initialize lock that protects our audiohook */
00114    ast_mutex_init(&audiohook->lock);
00115    ast_cond_init(&audiohook->trigger, NULL);
00116 
00117    audiohook->init_flags = init_flags;
00118 
00119    /* initialize internal rate at 8khz, this will adjust if necessary */
00120    audiohook_set_internal_rate(audiohook, 8000, 0);
00121 
00122    /* Since we are just starting out... this audiohook is new */
00123    ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_NEW);
00124 
00125    return 0;
00126 }
00127 
00128 /*! \brief Destroys an audiohook structure
00129  * \param audiohook Audiohook structure
00130  * \return Returns 0 on success, -1 on failure
00131  */
00132 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
00133 {
00134    /* Drop the factories used by this audiohook type */
00135    switch (audiohook->type) {
00136    case AST_AUDIOHOOK_TYPE_SPY:
00137    case AST_AUDIOHOOK_TYPE_WHISPER:
00138       ast_slinfactory_destroy(&audiohook->read_factory);
00139       ast_slinfactory_destroy(&audiohook->write_factory);
00140       break;
00141    default:
00142       break;
00143    }
00144 
00145    /* Destroy translation path if present */
00146    if (audiohook->trans_pvt)
00147       ast_translator_free_path(audiohook->trans_pvt);
00148 
00149    ao2_cleanup(audiohook->format);
00150 
00151    /* Lock and trigger be gone! */
00152    ast_cond_destroy(&audiohook->trigger);
00153    ast_mutex_destroy(&audiohook->lock);
00154 
00155    return 0;
00156 }
00157 
00158 /*! \brief Writes a frame into the audiohook structure
00159  * \param audiohook Audiohook structure
00160  * \param direction Direction the audio frame came from
00161  * \param frame Frame to write in
00162  * \return Returns 0 on success, -1 on failure
00163  */
00164 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
00165 {
00166    struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00167    struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory);
00168    struct timeval *rwtime = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *rwtime;
00169    int our_factory_samples;
00170    int our_factory_ms;
00171    int other_factory_samples;
00172    int other_factory_ms;
00173    int muteme = 0;
00174 
00175    /* Update last feeding time to be current */
00176    *rwtime = ast_tvnow();
00177 
00178    our_factory_samples = ast_slinfactory_available(factory);
00179    our_factory_ms = ast_tvdiff_ms(*rwtime, previous_time) + (our_factory_samples / (audiohook->hook_internal_samp_rate / 1000));
00180    other_factory_samples = ast_slinfactory_available(other_factory);
00181    other_factory_ms = other_factory_samples / (audiohook->hook_internal_samp_rate / 1000);
00182 
00183    if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && other_factory_samples && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) {
00184       ast_debug(1, "Flushing audiohook %p so it remains in sync\n", audiohook);
00185       ast_slinfactory_flush(factory);
00186       ast_slinfactory_flush(other_factory);
00187    }
00188 
00189    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))) {
00190       ast_debug(1, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook);
00191       ast_slinfactory_flush(factory);
00192       ast_slinfactory_flush(other_factory);
00193    }
00194 
00195    /* swap frame data for zeros if mute is required */
00196    if ((ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) ||
00197       (ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) ||
00198       (ast_test_flag(audiohook, AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE) == (AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE))) {
00199          muteme = 1;
00200    }
00201 
00202    if (muteme && frame->datalen > 0) {
00203       ast_frame_clear(frame);
00204    }
00205 
00206    /* Write frame out to respective factory */
00207    ast_slinfactory_feed(factory, frame);
00208 
00209    /* If we need to notify the respective handler of this audiohook, do so */
00210    if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
00211       ast_cond_signal(&audiohook->trigger);
00212    } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
00213       ast_cond_signal(&audiohook->trigger);
00214    } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
00215       ast_cond_signal(&audiohook->trigger);
00216    }
00217 
00218    return 0;
00219 }
00220 
00221 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
00222 {
00223    struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00224    int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
00225    short buf[samples];
00226    struct ast_frame frame = {
00227       .frametype = AST_FRAME_VOICE,
00228       .subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate),
00229       .data.ptr = buf,
00230       .datalen = sizeof(buf),
00231       .samples = samples,
00232    };
00233 
00234    /* Ensure the factory is able to give us the samples we want */
00235    if (samples > ast_slinfactory_available(factory)) {
00236       return NULL;
00237    }
00238 
00239    /* Read data in from factory */
00240    if (!ast_slinfactory_read(factory, buf, samples)) {
00241       return NULL;
00242    }
00243 
00244    /* If a volume adjustment needs to be applied apply it */
00245    if (vol) {
00246       ast_frame_adjust_volume(&frame, vol);
00247    }
00248 
00249    return ast_frdup(&frame);
00250 }
00251 
00252 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)
00253 {
00254    int i = 0, usable_read, usable_write;
00255    short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
00256    struct ast_frame frame = {
00257       .frametype = AST_FRAME_VOICE,
00258       .data.ptr = NULL,
00259       .datalen = sizeof(buf1),
00260       .samples = samples,
00261    };
00262 
00263    /* Make sure both factories have the required samples */
00264    usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
00265    usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
00266 
00267    if (!usable_read && !usable_write) {
00268       /* If both factories are unusable bail out */
00269       ast_debug(1, "Read factory %p and write factory %p both fail to provide %zu samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
00270       return NULL;
00271    }
00272 
00273    /* If we want to provide only a read factory make sure we aren't waiting for other audio */
00274    if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
00275       ast_debug(3, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
00276       return NULL;
00277    }
00278 
00279    /* If we want to provide only a write factory make sure we aren't waiting for other audio */
00280    if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*2)) {
00281       ast_debug(3, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
00282       return NULL;
00283    }
00284 
00285    /* Start with the read factory... if there are enough samples, read them in */
00286    if (usable_read) {
00287       if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
00288          read_buf = buf1;
00289          /* Adjust read volume if need be */
00290          if (audiohook->options.read_volume) {
00291             int count = 0;
00292             short adjust_value = abs(audiohook->options.read_volume);
00293             for (count = 0; count < samples; count++) {
00294                if (audiohook->options.read_volume > 0) {
00295                   ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
00296                } else if (audiohook->options.read_volume < 0) {
00297                   ast_slinear_saturated_divide(&buf1[count], &adjust_value);
00298                }
00299             }
00300          }
00301       }
00302    } else {
00303       ast_debug(1, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory);
00304    }
00305 
00306    /* Move on to the write factory... if there are enough samples, read them in */
00307    if (usable_write) {
00308       if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
00309          write_buf = buf2;
00310          /* Adjust write volume if need be */
00311          if (audiohook->options.write_volume) {
00312             int count = 0;
00313             short adjust_value = abs(audiohook->options.write_volume);
00314             for (count = 0; count < samples; count++) {
00315                if (audiohook->options.write_volume > 0) {
00316                   ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
00317                } else if (audiohook->options.write_volume < 0) {
00318                   ast_slinear_saturated_divide(&buf2[count], &adjust_value);
00319                }
00320             }
00321          }
00322       }
00323    } else {
00324       ast_debug(1, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory);
00325    }
00326 
00327    /* Basically we figure out which buffer to use... and if mixing can be done here */
00328    if (read_buf && read_reference) {
00329       frame.data.ptr = buf1;
00330       *read_reference = ast_frdup(&frame);
00331    }
00332    if (write_buf && write_reference) {
00333       frame.data.ptr = buf2;
00334       *write_reference = ast_frdup(&frame);
00335    }
00336 
00337    if (read_buf && write_buf) {
00338       for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++) {
00339          ast_slinear_saturated_add(data1, data2);
00340       }
00341       final_buf = buf1;
00342    } else if (read_buf) {
00343       final_buf = buf1;
00344    } else if (write_buf) {
00345       final_buf = buf2;
00346    } else {
00347       return NULL;
00348    }
00349 
00350    /* Make the final buffer part of the frame, so it gets duplicated fine */
00351    frame.data.ptr = final_buf;
00352 
00353    frame.subclass.format = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate);
00354 
00355    /* Yahoo, a combined copy of the audio! */
00356    return ast_frdup(&frame);
00357 }
00358 
00359 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)
00360 {
00361    struct ast_frame *read_frame = NULL, *final_frame = NULL;
00362    struct ast_format *slin;
00363 
00364    audiohook_set_internal_rate(audiohook, ast_format_get_sample_rate(format), 1);
00365 
00366    if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ?
00367       audiohook_read_frame_both(audiohook, samples, read_reference, write_reference) :
00368       audiohook_read_frame_single(audiohook, samples, direction)))) {
00369       return NULL;
00370    }
00371 
00372    slin = ast_format_cache_get_slin_by_rate(audiohook->hook_internal_samp_rate);
00373 
00374    /* If they don't want signed linear back out, we'll have to send it through the translation path */
00375    if (ast_format_cmp(format, slin) != AST_FORMAT_CMP_EQUAL) {
00376       /* Rebuild translation path if different format then previously */
00377       if (ast_format_cmp(format, audiohook->format) == AST_FORMAT_CMP_NOT_EQUAL) {
00378          if (audiohook->trans_pvt) {
00379             ast_translator_free_path(audiohook->trans_pvt);
00380             audiohook->trans_pvt = NULL;
00381          }
00382 
00383          /* 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 */
00384          if (!(audiohook->trans_pvt = ast_translator_build_path(format, slin))) {
00385             ast_frfree(read_frame);
00386             return NULL;
00387          }
00388          ao2_replace(audiohook->format, format);
00389       }
00390       /* Convert to requested format, and allow the read in frame to be freed */
00391       final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
00392    } else {
00393       final_frame = read_frame;
00394    }
00395 
00396    return final_frame;
00397 }
00398 
00399 /*! \brief Reads a frame in from the audiohook structure
00400  * \param audiohook Audiohook structure
00401  * \param samples Number of samples wanted in requested output format
00402  * \param direction Direction the audio frame came from
00403  * \param format Format of frame remote side wants back
00404  * \return Returns frame on success, NULL on failure
00405  */
00406 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format)
00407 {
00408    return audiohook_read_frame_helper(audiohook, samples, direction, format, NULL, NULL);
00409 }
00410 
00411 /*! \brief Reads a frame in from the audiohook structure
00412  * \param audiohook Audiohook structure
00413  * \param samples Number of samples wanted
00414  * \param direction Direction the audio frame came from
00415  * \param format Format of frame remote side wants back
00416  * \param read_frame frame pointer for copying read frame data
00417  * \param write_frame frame pointer for copying write frame data
00418  * \return Returns frame on success, NULL on failure
00419  */
00420 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)
00421 {
00422    return audiohook_read_frame_helper(audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, format, read_frame, write_frame);
00423 }
00424 
00425 static void audiohook_list_set_samplerate_compatibility(struct ast_audiohook_list *audiohook_list)
00426 {
00427    struct ast_audiohook *ah = NULL;
00428    audiohook_list->native_slin_compatible = 1;
00429    AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, ah, list) {
00430       if (!(ah->init_flags & AST_AUDIOHOOK_MANIPULATE_ALL_RATES)) {
00431          audiohook_list->native_slin_compatible = 0;
00432          return;
00433       }
00434    }
00435 }
00436 
00437 /*! \brief Attach audiohook to channel
00438  * \param chan Channel
00439  * \param audiohook Audiohook structure
00440  * \return Returns 0 on success, -1 on failure
00441  */
00442 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
00443 {
00444    ast_channel_lock(chan);
00445 
00446    if (!ast_channel_audiohooks(chan)) {
00447       struct ast_audiohook_list *ahlist;
00448       /* Whoops... allocate a new structure */
00449       if (!(ahlist = ast_calloc(1, sizeof(*ahlist)))) {
00450          ast_channel_unlock(chan);
00451          return -1;
00452       }
00453       ast_channel_audiohooks_set(chan, ahlist);
00454       AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->spy_list);
00455       AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->whisper_list);
00456       AST_LIST_HEAD_INIT_NOLOCK(&ast_channel_audiohooks(chan)->manipulate_list);
00457       /* This sample rate will adjust as necessary when writing to the list. */
00458       ast_channel_audiohooks(chan)->list_internal_samp_rate = 8000;
00459    }
00460 
00461    /* Drop into respective list */
00462    if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
00463       AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
00464    } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
00465       AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
00466    } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
00467       AST_LIST_INSERT_TAIL(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
00468    }
00469 
00470 
00471    audiohook_set_internal_rate(audiohook, ast_channel_audiohooks(chan)->list_internal_samp_rate, 1);
00472    audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
00473 
00474    /* Change status over to running since it is now attached */
00475    ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_RUNNING);
00476 
00477    if (ast_channel_is_bridged(chan)) {
00478       ast_channel_set_unbridged_nolock(chan, 1);
00479    }
00480 
00481    ast_channel_unlock(chan);
00482 
00483    return 0;
00484 }
00485 
00486 /*! \brief Update audiohook's status
00487  * \param audiohook Audiohook structure
00488  * \param status Audiohook status enum
00489  *
00490  * \note once status is updated to DONE, this function can not be used to set the
00491  * status back to any other setting.  Setting DONE effectively locks the status as such.
00492  */
00493 
00494 void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
00495 {
00496    ast_audiohook_lock(audiohook);
00497    if (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
00498       audiohook->status = status;
00499       ast_cond_signal(&audiohook->trigger);
00500    }
00501    ast_audiohook_unlock(audiohook);
00502 }
00503 
00504 /*! \brief Detach audiohook from channel
00505  * \param audiohook Audiohook structure
00506  * \return Returns 0 on success, -1 on failure
00507  */
00508 int ast_audiohook_detach(struct ast_audiohook *audiohook)
00509 {
00510    if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
00511       return 0;
00512    }
00513 
00514    ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
00515 
00516    while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
00517       ast_audiohook_trigger_wait(audiohook);
00518    }
00519 
00520    return 0;
00521 }
00522 
00523 void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
00524 {
00525    int i;
00526    struct ast_audiohook *audiohook;
00527 
00528    if (!audiohook_list) {
00529       return;
00530    }
00531 
00532    /* Drop any spies */
00533    while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
00534       ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00535    }
00536 
00537    /* Drop any whispering sources */
00538    while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
00539       ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00540    }
00541 
00542    /* Drop any manipulaters */
00543    while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
00544       ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00545       audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00546    }
00547 
00548    /* Drop translation paths if present */
00549    for (i = 0; i < 2; i++) {
00550       if (audiohook_list->in_translate[i].trans_pvt) {
00551          ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
00552          ao2_cleanup(audiohook_list->in_translate[i].format);
00553       }
00554       if (audiohook_list->out_translate[i].trans_pvt) {
00555          ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
00556          ao2_cleanup(audiohook_list->in_translate[i].format);
00557       }
00558    }
00559 
00560    /* Free ourselves */
00561    ast_free(audiohook_list);
00562 }
00563 
00564 /*! \brief find an audiohook based on its source
00565  * \param audiohook_list The list of audiohooks to search in
00566  * \param source The source of the audiohook we wish to find
00567  * \return Return the corresponding audiohook or NULL if it cannot be found.
00568  */
00569 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
00570 {
00571    struct ast_audiohook *audiohook = NULL;
00572 
00573    AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
00574       if (!strcasecmp(audiohook->source, source)) {
00575          return audiohook;
00576       }
00577    }
00578 
00579    AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
00580       if (!strcasecmp(audiohook->source, source)) {
00581          return audiohook;
00582       }
00583    }
00584 
00585    AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
00586       if (!strcasecmp(audiohook->source, source)) {
00587          return audiohook;
00588       }
00589    }
00590 
00591    return NULL;
00592 }
00593 
00594 static void audiohook_move(struct ast_channel *old_chan, struct ast_channel *new_chan, struct ast_audiohook *audiohook)
00595 {
00596    enum ast_audiohook_status oldstatus;
00597 
00598    /* By locking both channels and the audiohook, we can assure that
00599     * another thread will not have a chance to read the audiohook's status
00600     * as done, even though ast_audiohook_remove signals the trigger
00601     * condition.
00602     */
00603    ast_audiohook_lock(audiohook);
00604    oldstatus = audiohook->status;
00605 
00606    ast_audiohook_remove(old_chan, audiohook);
00607    ast_audiohook_attach(new_chan, audiohook);
00608 
00609    audiohook->status = oldstatus;
00610    ast_audiohook_unlock(audiohook);
00611 }
00612 
00613 void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
00614 {
00615    struct ast_audiohook *audiohook;
00616 
00617    if (!ast_channel_audiohooks(old_chan)) {
00618       return;
00619    }
00620 
00621    audiohook = find_audiohook_by_source(ast_channel_audiohooks(old_chan), source);
00622    if (!audiohook) {
00623       return;
00624    }
00625 
00626    audiohook_move(old_chan, new_chan, audiohook);
00627 }
00628 
00629 void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
00630 {
00631    struct ast_audiohook *audiohook;
00632    struct ast_audiohook_list *audiohook_list;
00633 
00634    audiohook_list = ast_channel_audiohooks(old_chan);
00635    if (!audiohook_list) {
00636       return;
00637    }
00638 
00639    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
00640       audiohook_move(old_chan, new_chan, audiohook);
00641    }
00642    AST_LIST_TRAVERSE_SAFE_END;
00643 
00644    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
00645       audiohook_move(old_chan, new_chan, audiohook);
00646    }
00647    AST_LIST_TRAVERSE_SAFE_END;
00648 
00649    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00650       audiohook_move(old_chan, new_chan, audiohook);
00651    }
00652    AST_LIST_TRAVERSE_SAFE_END;
00653 }
00654 
00655 /*! \brief Detach specified source audiohook from channel
00656  * \param chan Channel to detach from
00657  * \param source Name of source to detach
00658  * \return Returns 0 on success, -1 on failure
00659  */
00660 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
00661 {
00662    struct ast_audiohook *audiohook = NULL;
00663 
00664    ast_channel_lock(chan);
00665 
00666    /* Ensure the channel has audiohooks on it */
00667    if (!ast_channel_audiohooks(chan)) {
00668       ast_channel_unlock(chan);
00669       return -1;
00670    }
00671 
00672    audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
00673 
00674    ast_channel_unlock(chan);
00675 
00676    if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
00677       ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
00678    }
00679 
00680    return (audiohook ? 0 : -1);
00681 }
00682 
00683 /*!
00684  * \brief Remove an audiohook from a specified channel
00685  *
00686  * \param chan Channel to remove from
00687  * \param audiohook Audiohook to remove
00688  *
00689  * \return Returns 0 on success, -1 on failure
00690  *
00691  * \note The channel does not need to be locked before calling this function
00692  */
00693 int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
00694 {
00695    ast_channel_lock(chan);
00696 
00697    if (!ast_channel_audiohooks(chan)) {
00698       ast_channel_unlock(chan);
00699       return -1;
00700    }
00701 
00702    if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY) {
00703       AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->spy_list, audiohook, list);
00704    } else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER) {
00705       AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->whisper_list, audiohook, list);
00706    } else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE) {
00707       AST_LIST_REMOVE(&ast_channel_audiohooks(chan)->manipulate_list, audiohook, list);
00708    }
00709 
00710    audiohook_list_set_samplerate_compatibility(ast_channel_audiohooks(chan));
00711    ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00712 
00713    if (ast_channel_is_bridged(chan)) {
00714       ast_channel_set_unbridged_nolock(chan, 1);
00715    }
00716 
00717    ast_channel_unlock(chan);
00718 
00719    return 0;
00720 }
00721 
00722 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
00723  * \param chan Channel that the list is coming off of
00724  * \param audiohook_list List of audiohooks
00725  * \param direction Direction frame is coming in from
00726  * \param frame The frame itself
00727  * \return Return frame on success, NULL on failure
00728  */
00729 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)
00730 {
00731    struct ast_audiohook *audiohook = NULL;
00732    int removed = 0;
00733 
00734    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00735       ast_audiohook_lock(audiohook);
00736       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00737          AST_LIST_REMOVE_CURRENT(list);
00738          removed = 1;
00739          ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00740          ast_audiohook_unlock(audiohook);
00741          audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00742          if (ast_channel_is_bridged(chan)) {
00743             ast_channel_set_unbridged_nolock(chan, 1);
00744          }
00745          continue;
00746       }
00747       if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF)) {
00748          audiohook->manipulate_callback(audiohook, chan, frame, direction);
00749       }
00750       ast_audiohook_unlock(audiohook);
00751    }
00752    AST_LIST_TRAVERSE_SAFE_END;
00753 
00754    /* if an audiohook got removed, reset samplerate compatibility */
00755    if (removed) {
00756       audiohook_list_set_samplerate_compatibility(audiohook_list);
00757    }
00758    return frame;
00759 }
00760 
00761 static struct ast_frame *audiohook_list_translate_to_slin(struct ast_audiohook_list *audiohook_list,
00762    enum ast_audiohook_direction direction, struct ast_frame *frame)
00763 {
00764    struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ?
00765       &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
00766    struct ast_frame *new_frame = frame;
00767    struct ast_format *slin;
00768 
00769    /* If we are capable of maintaining doing samplerates other that 8khz, update
00770     * the internal audiohook_list's rate and higher samplerate audio arrives. By
00771     * updating the list's rate, all the audiohooks in the list will be updated as well
00772     * as the are written and read from. */
00773    if (audiohook_list->native_slin_compatible) {
00774       audiohook_list->list_internal_samp_rate =
00775          MAX(ast_format_get_sample_rate(frame->subclass.format), audiohook_list->list_internal_samp_rate);
00776    }
00777 
00778    slin = ast_format_cache_get_slin_by_rate(audiohook_list->list_internal_samp_rate);
00779    if (ast_format_cmp(frame->subclass.format, slin) == AST_FORMAT_CMP_EQUAL) {
00780       return new_frame;
00781    }
00782 
00783    if (ast_format_cmp(frame->subclass.format, in_translate->format) == AST_FORMAT_CMP_NOT_EQUAL) {
00784       if (in_translate->trans_pvt) {
00785          ast_translator_free_path(in_translate->trans_pvt);
00786       }
00787       if (!(in_translate->trans_pvt = ast_translator_build_path(slin, frame->subclass.format))) {
00788          return NULL;
00789       }
00790       ao2_replace(in_translate->format, frame->subclass.format);
00791    }
00792 
00793    if (!(new_frame = ast_translate(in_translate->trans_pvt, frame, 0))) {
00794       return NULL;
00795    }
00796 
00797    return new_frame;
00798 }
00799 
00800 static struct ast_frame *audiohook_list_translate_to_native(struct ast_audiohook_list *audiohook_list,
00801    enum ast_audiohook_direction direction, struct ast_frame *slin_frame, struct ast_format *outformat)
00802 {
00803    struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
00804    struct ast_frame *outframe = NULL;
00805    if (ast_format_cmp(slin_frame->subclass.format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
00806       /* rebuild translators if necessary */
00807       if (ast_format_cmp(out_translate->format, outformat) == AST_FORMAT_CMP_NOT_EQUAL) {
00808          if (out_translate->trans_pvt) {
00809             ast_translator_free_path(out_translate->trans_pvt);
00810          }
00811          if (!(out_translate->trans_pvt = ast_translator_build_path(outformat, slin_frame->subclass.format))) {
00812             return NULL;
00813          }
00814          ao2_replace(out_translate->format, outformat);
00815       }
00816       /* translate back to the format the frame came in as. */
00817       if (!(outframe = ast_translate(out_translate->trans_pvt, slin_frame, 0))) {
00818          return NULL;
00819       }
00820    }
00821    return outframe;
00822 }
00823 
00824 /*!
00825  * \brief Pass an AUDIO frame off to be handled by the audiohook core
00826  *
00827  * \details
00828  * This function has 3 ast_frames and 3 parts to handle each.  At the beginning of this
00829  * function all 3 frames, start_frame, middle_frame, and end_frame point to the initial
00830  * input frame.
00831  *
00832  * Part_1: Translate the start_frame into SLINEAR audio if it is not already in that
00833  *         format.  The result of this part is middle_frame is guaranteed to be in
00834  *         SLINEAR format for Part_2.
00835  * Part_2: Send middle_frame off to spies and manipulators.  At this point middle_frame is
00836  *         either a new frame as result of the translation, or points directly to the start_frame
00837  *         because no translation to SLINEAR audio was required.
00838  * Part_3: Translate end_frame's audio back into the format of start frame if necessary.  This
00839  *         is only necessary if manipulation of middle_frame occurred.
00840  *
00841  * \param chan Channel that the list is coming off of
00842  * \param audiohook_list List of audiohooks
00843  * \param direction Direction frame is coming in from
00844  * \param frame The frame itself
00845  * \return Return frame on success, NULL on failure
00846  */
00847 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)
00848 {
00849    struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
00850    struct ast_audiohook *audiohook = NULL;
00851    int samples;
00852    int middle_frame_manipulated = 0;
00853    int removed = 0;
00854 
00855    /* ---Part_1. translate start_frame to SLINEAR if necessary. */
00856    if (!(middle_frame = audiohook_list_translate_to_slin(audiohook_list, direction, start_frame))) {
00857       return frame;
00858    }
00859    samples = middle_frame->samples;
00860 
00861    /* ---Part_2: Send middle_frame to spy and manipulator lists.  middle_frame is guaranteed to be SLINEAR here.*/
00862    /* Queue up signed linear frame to each spy */
00863    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
00864       ast_audiohook_lock(audiohook);
00865       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00866          AST_LIST_REMOVE_CURRENT(list);
00867          removed = 1;
00868          ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00869          ast_audiohook_unlock(audiohook);
00870          if (ast_channel_is_bridged(chan)) {
00871             ast_channel_set_unbridged_nolock(chan, 1);
00872          }
00873          continue;
00874       }
00875       audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
00876       ast_audiohook_write_frame(audiohook, direction, middle_frame);
00877       ast_audiohook_unlock(audiohook);
00878    }
00879    AST_LIST_TRAVERSE_SAFE_END;
00880 
00881    /* If this frame is being written out to the channel then we need to use whisper sources */
00882    if (!AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
00883       int i = 0;
00884       short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
00885       memset(&combine_buf, 0, sizeof(combine_buf));
00886       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
00887          struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00888          ast_audiohook_lock(audiohook);
00889          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00890             AST_LIST_REMOVE_CURRENT(list);
00891             removed = 1;
00892             ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00893             ast_audiohook_unlock(audiohook);
00894             if (ast_channel_is_bridged(chan)) {
00895                ast_channel_set_unbridged_nolock(chan, 1);
00896             }
00897             continue;
00898          }
00899          audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
00900          if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) {
00901             /* Take audio from this whisper source and combine it into our main buffer */
00902             for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++) {
00903                ast_slinear_saturated_add(data1, data2);
00904             }
00905          }
00906          ast_audiohook_unlock(audiohook);
00907       }
00908       AST_LIST_TRAVERSE_SAFE_END;
00909       /* We take all of the combined whisper sources and combine them into the audio being written out */
00910       for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++) {
00911          ast_slinear_saturated_add(data1, data2);
00912       }
00913       middle_frame_manipulated = 1;
00914    }
00915 
00916    /* Pass off frame to manipulate audiohooks */
00917    if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
00918       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00919          ast_audiohook_lock(audiohook);
00920          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00921             AST_LIST_REMOVE_CURRENT(list);
00922             removed = 1;
00923             ast_audiohook_update_status(audiohook, AST_AUDIOHOOK_STATUS_DONE);
00924             ast_audiohook_unlock(audiohook);
00925             /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
00926             audiohook->manipulate_callback(audiohook, chan, NULL, direction);
00927             if (ast_channel_is_bridged(chan)) {
00928                ast_channel_set_unbridged_nolock(chan, 1);
00929             }
00930             continue;
00931          }
00932          audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
00933          /* Feed in frame to manipulation. */
00934          if (!audiohook->manipulate_callback(audiohook, chan, middle_frame, direction)) {
00935             /* If the manipulation fails then the frame will be returned in its original state.
00936              * Since there are potentially more manipulator callbacks in the list, no action should
00937              * be taken here to exit early. */
00938              middle_frame_manipulated = 1;
00939          }
00940          ast_audiohook_unlock(audiohook);
00941       }
00942       AST_LIST_TRAVERSE_SAFE_END;
00943    }
00944 
00945    /* ---Part_3: Decide what to do with the end_frame (whether to transcode or not) */
00946    if (middle_frame_manipulated) {
00947       if (!(end_frame = audiohook_list_translate_to_native(audiohook_list, direction, middle_frame, start_frame->subclass.format))) {
00948          /* translation failed, so just pass back the input frame */
00949          end_frame = start_frame;
00950       }
00951    } else {
00952       end_frame = start_frame;
00953    }
00954    /* clean up our middle_frame if required */
00955    if (middle_frame != end_frame) {
00956       ast_frfree(middle_frame);
00957       middle_frame = NULL;
00958    }
00959 
00960    /* Before returning, if an audiohook got removed, reset samplerate compatibility */
00961    if (removed) {
00962       audiohook_list_set_samplerate_compatibility(audiohook_list);
00963    }
00964 
00965    return end_frame;
00966 }
00967 
00968 int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
00969 {
00970    return !audiohook_list
00971       || (AST_LIST_EMPTY(&audiohook_list->spy_list)
00972          && AST_LIST_EMPTY(&audiohook_list->whisper_list)
00973          && AST_LIST_EMPTY(&audiohook_list->manipulate_list));
00974 }
00975 
00976 /*! \brief Pass a frame off to be handled by the audiohook core
00977  * \param chan Channel that the list is coming off of
00978  * \param audiohook_list List of audiohooks
00979  * \param direction Direction frame is coming in from
00980  * \param frame The frame itself
00981  * \return Return frame on success, NULL on failure
00982  */
00983 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)
00984 {
00985    /* Pass off frame to it's respective list write function */
00986    if (frame->frametype == AST_FRAME_VOICE) {
00987       return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
00988    } else if (frame->frametype == AST_FRAME_DTMF) {
00989       return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
00990    } else {
00991       return frame;
00992    }
00993 }
00994 
00995 /*! \brief Wait for audiohook trigger to be triggered
00996  * \param audiohook Audiohook to wait on
00997  */
00998 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
00999 {
01000    struct timeval wait;
01001    struct timespec ts;
01002 
01003    wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
01004    ts.tv_sec = wait.tv_sec;
01005    ts.tv_nsec = wait.tv_usec * 1000;
01006 
01007    ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
01008 
01009    return;
01010 }
01011 
01012 /* Count number of channel audiohooks by type, regardless of type */
01013 int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
01014 {
01015    int count = 0;
01016    struct ast_audiohook *ah = NULL;
01017 
01018    if (!ast_channel_audiohooks(chan)) {
01019       return -1;
01020    }
01021 
01022    switch (type) {
01023       case AST_AUDIOHOOK_TYPE_SPY:
01024          AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
01025             if (!strcmp(ah->source, source)) {
01026                count++;
01027             }
01028          }
01029          break;
01030       case AST_AUDIOHOOK_TYPE_WHISPER:
01031          AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
01032             if (!strcmp(ah->source, source)) {
01033                count++;
01034             }
01035          }
01036          break;
01037       case AST_AUDIOHOOK_TYPE_MANIPULATE:
01038          AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
01039             if (!strcmp(ah->source, source)) {
01040                count++;
01041             }
01042          }
01043          break;
01044       default:
01045          ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
01046          return -1;
01047    }
01048 
01049    return count;
01050 }
01051 
01052 /* Count number of channel audiohooks by type that are running */
01053 int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
01054 {
01055    int count = 0;
01056    struct ast_audiohook *ah = NULL;
01057    if (!ast_channel_audiohooks(chan))
01058       return -1;
01059 
01060    switch (type) {
01061       case AST_AUDIOHOOK_TYPE_SPY:
01062          AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->spy_list, ah, list) {
01063             if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
01064                count++;
01065          }
01066          break;
01067       case AST_AUDIOHOOK_TYPE_WHISPER:
01068          AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->whisper_list, ah, list) {
01069             if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
01070                count++;
01071          }
01072          break;
01073       case AST_AUDIOHOOK_TYPE_MANIPULATE:
01074          AST_LIST_TRAVERSE(&ast_channel_audiohooks(chan)->manipulate_list, ah, list) {
01075             if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
01076                count++;
01077          }
01078          break;
01079       default:
01080          ast_debug(1, "Invalid audiohook type supplied, (%u)\n", type);
01081          return -1;
01082    }
01083    return count;
01084 }
01085 
01086 /*! \brief Audiohook volume adjustment structure */
01087 struct audiohook_volume {
01088    struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
01089    int read_adjustment;            /*!< Value to adjust frames read from the channel by */
01090    int write_adjustment;           /*!< Value to adjust frames written to the channel by */
01091 };
01092 
01093 /*! \brief Callback used to destroy the audiohook volume datastore
01094  * \param data Volume information structure
01095  * \return Returns nothing
01096  */
01097 static void audiohook_volume_destroy(void *data)
01098 {
01099    struct audiohook_volume *audiohook_volume = data;
01100 
01101    /* Destroy the audiohook as it is no longer in use */
01102    ast_audiohook_destroy(&audiohook_volume->audiohook);
01103 
01104    /* Finally free ourselves, we are of no more use */
01105    ast_free(audiohook_volume);
01106 
01107    return;
01108 }
01109 
01110 /*! \brief Datastore used to store audiohook volume information */
01111 static const struct ast_datastore_info audiohook_volume_datastore = {
01112    .type = "Volume",
01113    .destroy = audiohook_volume_destroy,
01114 };
01115 
01116 /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
01117  * \param audiohook Audiohook attached to the channel
01118  * \param chan Channel we are attached to
01119  * \param frame Frame of audio we want to manipulate
01120  * \param direction Direction the audio came in from
01121  * \return Returns 0 on success, -1 on failure
01122  */
01123 static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
01124 {
01125    struct ast_datastore *datastore = NULL;
01126    struct audiohook_volume *audiohook_volume = NULL;
01127    int *gain = NULL;
01128 
01129    /* If the audiohook is shutting down don't even bother */
01130    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
01131       return 0;
01132    }
01133 
01134    /* Try to find the datastore containg adjustment information, if we can't just bail out */
01135    if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
01136       return 0;
01137    }
01138 
01139    audiohook_volume = datastore->data;
01140 
01141    /* Based on direction grab the appropriate adjustment value */
01142    if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
01143       gain = &audiohook_volume->read_adjustment;
01144    } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
01145       gain = &audiohook_volume->write_adjustment;
01146    }
01147 
01148    /* If an adjustment value is present modify the frame */
01149    if (gain && *gain) {
01150       ast_frame_adjust_volume(frame, *gain);
01151    }
01152 
01153    return 0;
01154 }
01155 
01156 /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
01157  * \param chan Channel to look on
01158  * \param create Whether to create the datastore if not found
01159  * \return Returns audiohook_volume structure on success, NULL on failure
01160  */
01161 static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
01162 {
01163    struct ast_datastore *datastore = NULL;
01164    struct audiohook_volume *audiohook_volume = NULL;
01165 
01166    /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
01167    if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
01168       return datastore->data;
01169    }
01170 
01171    /* 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 */
01172    if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
01173       return NULL;
01174    }
01175 
01176    /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
01177    if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
01178       ast_datastore_free(datastore);
01179       return NULL;
01180    }
01181 
01182    /* Setup our audiohook structure so we can manipulate the audio */
01183    ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
01184    audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
01185 
01186    /* Attach the audiohook_volume blob to the datastore and attach to the channel */
01187    datastore->data = audiohook_volume;
01188    ast_channel_datastore_add(chan, datastore);
01189 
01190    /* All is well... put the audiohook into motion */
01191    ast_audiohook_attach(chan, &audiohook_volume->audiohook);
01192 
01193    return audiohook_volume;
01194 }
01195 
01196 /*! \brief Adjust the volume on frames read from or written to a channel
01197  * \param chan Channel to muck with
01198  * \param direction Direction to set on
01199  * \param volume Value to adjust the volume by
01200  * \return Returns 0 on success, -1 on failure
01201  */
01202 int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
01203 {
01204    struct audiohook_volume *audiohook_volume = NULL;
01205 
01206    /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
01207    if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
01208       return -1;
01209    }
01210 
01211    /* Now based on the direction set the proper value */
01212    if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
01213       audiohook_volume->read_adjustment = volume;
01214    }
01215    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
01216       audiohook_volume->write_adjustment = volume;
01217    }
01218 
01219    return 0;
01220 }
01221 
01222 /*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
01223  * \param chan Channel to retrieve volume adjustment from
01224  * \param direction Direction to retrieve
01225  * \return Returns adjustment value
01226  */
01227 int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
01228 {
01229    struct audiohook_volume *audiohook_volume = NULL;
01230    int adjustment = 0;
01231 
01232    /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
01233    if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
01234       return 0;
01235    }
01236 
01237    /* Grab the adjustment value based on direction given */
01238    if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
01239       adjustment = audiohook_volume->read_adjustment;
01240    } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
01241       adjustment = audiohook_volume->write_adjustment;
01242    }
01243 
01244    return adjustment;
01245 }
01246 
01247 /*! \brief Adjust the volume on frames read from or written to a channel
01248  * \param chan Channel to muck with
01249  * \param direction Direction to increase
01250  * \param volume Value to adjust the adjustment by
01251  * \return Returns 0 on success, -1 on failure
01252  */
01253 int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
01254 {
01255    struct audiohook_volume *audiohook_volume = NULL;
01256 
01257    /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
01258    if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
01259       return -1;
01260    }
01261 
01262    /* Based on the direction change the specific adjustment value */
01263    if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
01264       audiohook_volume->read_adjustment += volume;
01265    }
01266    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
01267       audiohook_volume->write_adjustment += volume;
01268    }
01269 
01270    return 0;
01271 }
01272 
01273 /*! \brief Mute frames read from or written to a channel
01274  * \param chan Channel to muck with
01275  * \param source Type of audiohook
01276  * \param flag which flag to set / clear
01277  * \param clear set or clear
01278  * \return Returns 0 on success, -1 on failure
01279  */
01280 int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
01281 {
01282    struct ast_audiohook *audiohook = NULL;
01283 
01284    ast_channel_lock(chan);
01285 
01286    /* Ensure the channel has audiohooks on it */
01287    if (!ast_channel_audiohooks(chan)) {
01288       ast_channel_unlock(chan);
01289       return -1;
01290    }
01291 
01292    audiohook = find_audiohook_by_source(ast_channel_audiohooks(chan), source);
01293 
01294    if (audiohook) {
01295       if (clear) {
01296          ast_clear_flag(audiohook, flag);
01297       } else {
01298          ast_set_flag(audiohook, flag);
01299       }
01300    }
01301 
01302    ast_channel_unlock(chan);
01303 
01304    return (audiohook ? 0 : -1);
01305 }

Generated on Thu Apr 16 06:27:15 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6