Fri Jul 24 00:40:44 2009

Asterisk developer's documentation


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 'file' Colp <jcolp@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 187600 $")
00029 
00030 #include <signal.h>
00031 
00032 #include "asterisk/channel.h"
00033 #include "asterisk/utils.h"
00034 #include "asterisk/lock.h"
00035 #include "asterisk/linkedlists.h"
00036 #include "asterisk/audiohook.h"
00037 #include "asterisk/slinfactory.h"
00038 #include "asterisk/frame.h"
00039 #include "asterisk/translate.h"
00040 
00041 struct ast_audiohook_translate {
00042    struct ast_trans_pvt *trans_pvt;
00043    int format;
00044 };
00045 
00046 struct ast_audiohook_list {
00047    struct ast_audiohook_translate in_translate[2];
00048    struct ast_audiohook_translate out_translate[2];
00049    AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
00050    AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
00051    AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
00052 };
00053 
00054 /*! \brief Initialize an audiohook structure
00055  * \param audiohook Audiohook structure
00056  * \param type
00057  * \param source
00058  * \return Returns 0 on success, -1 on failure
00059  */
00060 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
00061 {
00062    /* Need to keep the type and source */
00063    audiohook->type = type;
00064    audiohook->source = source;
00065 
00066    /* Initialize lock that protects our audiohook */
00067    ast_mutex_init(&audiohook->lock);
00068    ast_cond_init(&audiohook->trigger, NULL);
00069 
00070    /* Setup the factories that are needed for this audiohook type */
00071    switch (type) {
00072    case AST_AUDIOHOOK_TYPE_SPY:
00073       ast_slinfactory_init(&audiohook->read_factory);
00074    case AST_AUDIOHOOK_TYPE_WHISPER:
00075       ast_slinfactory_init(&audiohook->write_factory);
00076       break;
00077    default:
00078       break;
00079    }
00080 
00081    /* Since we are just starting out... this audiohook is new */
00082    audiohook->status = AST_AUDIOHOOK_STATUS_NEW;
00083 
00084    return 0;
00085 }
00086 
00087 /*! \brief Destroys an audiohook structure
00088  * \param audiohook Audiohook structure
00089  * \return Returns 0 on success, -1 on failure
00090  */
00091 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
00092 {
00093    /* Drop the factories used by this audiohook type */
00094    switch (audiohook->type) {
00095    case AST_AUDIOHOOK_TYPE_SPY:
00096       ast_slinfactory_destroy(&audiohook->read_factory);
00097    case AST_AUDIOHOOK_TYPE_WHISPER:
00098       ast_slinfactory_destroy(&audiohook->write_factory);
00099       break;
00100    default:
00101       break;
00102    }
00103 
00104    /* Destroy translation path if present */
00105    if (audiohook->trans_pvt)
00106       ast_translator_free_path(audiohook->trans_pvt);
00107 
00108    /* Lock and trigger be gone! */
00109    ast_cond_destroy(&audiohook->trigger);
00110    ast_mutex_destroy(&audiohook->lock);
00111 
00112    return 0;
00113 }
00114 
00115 /*! \brief Writes a frame into the audiohook structure
00116  * \param audiohook Audiohook structure
00117  * \param direction Direction the audio frame came from
00118  * \param frame Frame to write in
00119  * \return Returns 0 on success, -1 on failure
00120  */
00121 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
00122 {
00123    struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00124    struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory);
00125    struct timeval *rwtime = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *rwtime;
00126    int our_factory_ms;
00127    int other_factory_samples;
00128    int other_factory_ms;
00129 
00130    /* Update last feeding time to be current */
00131    *rwtime = ast_tvnow();
00132 
00133    our_factory_ms = ast_tvdiff_ms(*rwtime, previous_time) + (ast_slinfactory_available(factory) / 8);
00134    other_factory_samples = ast_slinfactory_available(other_factory);
00135    other_factory_ms = other_factory_samples / 8;
00136 
00137    if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && other_factory_samples && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) {
00138       if (option_debug)
00139          ast_log(LOG_DEBUG, "Flushing audiohook %p so it remains in sync\n", audiohook);
00140       ast_slinfactory_flush(factory);
00141       ast_slinfactory_flush(other_factory);
00142    }
00143 
00144    /* Write frame out to respective factory */
00145    ast_slinfactory_feed(factory, frame);
00146 
00147    /* If we need to notify the respective handler of this audiohook, do so */
00148    if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
00149       ast_cond_signal(&audiohook->trigger);
00150    } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
00151       ast_cond_signal(&audiohook->trigger);
00152    } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
00153       ast_cond_signal(&audiohook->trigger);
00154    }
00155 
00156    return 0;
00157 }
00158 
00159 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
00160 {
00161    struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00162    int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
00163    short buf[samples];
00164    struct ast_frame frame = {
00165       .frametype = AST_FRAME_VOICE,
00166       .subclass = AST_FORMAT_SLINEAR,
00167       .data.ptr = buf,
00168       .datalen = sizeof(buf),
00169       .samples = samples,
00170    };
00171 
00172    /* Ensure the factory is able to give us the samples we want */
00173    if (samples > ast_slinfactory_available(factory))
00174       return NULL;
00175    
00176    /* Read data in from factory */
00177    if (!ast_slinfactory_read(factory, buf, samples))
00178       return NULL;
00179 
00180    /* If a volume adjustment needs to be applied apply it */
00181    if (vol)
00182       ast_frame_adjust_volume(&frame, vol);
00183 
00184    return ast_frdup(&frame);
00185 }
00186 
00187 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
00188 {
00189    int i = 0, usable_read, usable_write;
00190    short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
00191    struct ast_frame frame = {
00192       .frametype = AST_FRAME_VOICE,
00193       .subclass = AST_FORMAT_SLINEAR,
00194       .data.ptr = NULL,
00195       .datalen = sizeof(buf1),
00196       .samples = samples,
00197    };
00198 
00199    /* Make sure both factories have the required samples */
00200    usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
00201    usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
00202 
00203    if (!usable_read && !usable_write) {
00204       /* If both factories are unusable bail out */
00205       ast_debug(1, "Read factory %p and write factory %p both fail to provide %zd samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
00206       return NULL;
00207    }
00208 
00209    /* If we want to provide only a read factory make sure we aren't waiting for other audio */
00210    if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
00211       ast_debug(3, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
00212       return NULL;
00213    }
00214 
00215    /* If we want to provide only a write factory make sure we aren't waiting for other audio */
00216    if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*2)) {
00217       ast_debug(3, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
00218       return NULL;
00219    }
00220 
00221    /* Start with the read factory... if there are enough samples, read them in */
00222    if (usable_read) {
00223       if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
00224          read_buf = buf1;
00225          /* Adjust read volume if need be */
00226          if (audiohook->options.read_volume) {
00227             int count = 0;
00228             short adjust_value = abs(audiohook->options.read_volume);
00229             for (count = 0; count < samples; count++) {
00230                if (audiohook->options.read_volume > 0)
00231                   ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
00232                else if (audiohook->options.read_volume < 0)
00233                   ast_slinear_saturated_divide(&buf1[count], &adjust_value);
00234             }
00235          }
00236       }
00237    } else if (option_debug)
00238       ast_log(LOG_DEBUG, "Failed to get %d samples from read factory %p\n", (int)samples, &audiohook->read_factory);
00239 
00240    /* Move on to the write factory... if there are enough samples, read them in */
00241    if (usable_write) {
00242       if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
00243          write_buf = buf2;
00244          /* Adjust write volume if need be */
00245          if (audiohook->options.write_volume) {
00246             int count = 0;
00247             short adjust_value = abs(audiohook->options.write_volume);
00248             for (count = 0; count < samples; count++) {
00249                if (audiohook->options.write_volume > 0)
00250                   ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
00251                else if (audiohook->options.write_volume < 0)
00252                   ast_slinear_saturated_divide(&buf2[count], &adjust_value);
00253             }
00254          }
00255       }
00256    } else if (option_debug)
00257       ast_log(LOG_DEBUG, "Failed to get %d samples from write factory %p\n", (int)samples, &audiohook->write_factory);
00258 
00259    /* Basically we figure out which buffer to use... and if mixing can be done here */
00260    if (!read_buf && !write_buf)
00261       return NULL;
00262    else if (read_buf && write_buf) {
00263       for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
00264          ast_slinear_saturated_add(data1, data2);
00265       final_buf = buf1;
00266    } else if (read_buf)
00267       final_buf = buf1;
00268    else if (write_buf)
00269       final_buf = buf2;
00270 
00271    /* Make the final buffer part of the frame, so it gets duplicated fine */
00272    frame.data.ptr = final_buf;
00273 
00274    /* Yahoo, a combined copy of the audio! */
00275    return ast_frdup(&frame);
00276 }
00277 
00278 /*! \brief Reads a frame in from the audiohook structure
00279  * \param audiohook Audiohook structure
00280  * \param samples Number of samples wanted
00281  * \param direction Direction the audio frame came from
00282  * \param format Format of frame remote side wants back
00283  * \return Returns frame on success, NULL on failure
00284  */
00285 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
00286 {
00287    struct ast_frame *read_frame = NULL, *final_frame = NULL;
00288 
00289    if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
00290       return NULL;
00291 
00292    /* If they don't want signed linear back out, we'll have to send it through the translation path */
00293    if (format != AST_FORMAT_SLINEAR) {
00294       /* Rebuild translation path if different format then previously */
00295       if (audiohook->format != format) {
00296          if (audiohook->trans_pvt) {
00297             ast_translator_free_path(audiohook->trans_pvt);
00298             audiohook->trans_pvt = NULL;
00299          }
00300          /* 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 */
00301          if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
00302             ast_frfree(read_frame);
00303             return NULL;
00304          }
00305       }
00306       /* Convert to requested format, and allow the read in frame to be freed */
00307       final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
00308    } else {
00309       final_frame = read_frame;
00310    }
00311 
00312    return final_frame;
00313 }
00314 
00315 /*! \brief Attach audiohook to channel
00316  * \param chan Channel
00317  * \param audiohook Audiohook structure
00318  * \return Returns 0 on success, -1 on failure
00319  */
00320 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
00321 {
00322    ast_channel_lock(chan);
00323 
00324    if (!chan->audiohooks) {
00325       /* Whoops... allocate a new structure */
00326       if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
00327          ast_channel_unlock(chan);
00328          return -1;
00329       }
00330       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
00331       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
00332       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
00333    }
00334 
00335    /* Drop into respective list */
00336    if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
00337       AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
00338    else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
00339       AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
00340    else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
00341       AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
00342 
00343    /* Change status over to running since it is now attached */
00344    audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
00345 
00346    ast_channel_unlock(chan);
00347 
00348    return 0;
00349 }
00350 
00351 /*! \brief Detach audiohook from channel
00352  * \param audiohook Audiohook structure
00353  * \return Returns 0 on success, -1 on failure
00354  */
00355 int ast_audiohook_detach(struct ast_audiohook *audiohook)
00356 {
00357    if (audiohook->status == AST_AUDIOHOOK_STATUS_NEW || audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
00358       return 0;
00359 
00360    audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
00361 
00362    while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
00363       ast_audiohook_trigger_wait(audiohook);
00364 
00365    return 0;
00366 }
00367 
00368 /*! \brief Detach audiohooks from list and destroy said list
00369  * \param audiohook_list List of audiohooks
00370  * \return Returns 0 on success, -1 on failure
00371  */
00372 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
00373 {
00374    int i = 0;
00375    struct ast_audiohook *audiohook = NULL;
00376 
00377    /* Drop any spies */
00378    while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->spy_list, list))) {
00379       ast_audiohook_lock(audiohook);
00380       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00381       ast_cond_signal(&audiohook->trigger);
00382       ast_audiohook_unlock(audiohook);
00383    }
00384 
00385    /* Drop any whispering sources */
00386    while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->whisper_list, list))) {
00387       ast_audiohook_lock(audiohook);
00388       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00389       ast_cond_signal(&audiohook->trigger);
00390       ast_audiohook_unlock(audiohook);
00391    }
00392 
00393    /* Drop any manipulaters */
00394    while ((audiohook = AST_LIST_REMOVE_HEAD(&audiohook_list->manipulate_list, list))) {
00395       ast_audiohook_lock(audiohook);
00396       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00397       ast_audiohook_unlock(audiohook);
00398       audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00399    }
00400 
00401    /* Drop translation paths if present */
00402    for (i = 0; i < 2; i++) {
00403       if (audiohook_list->in_translate[i].trans_pvt)
00404          ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
00405       if (audiohook_list->out_translate[i].trans_pvt)
00406          ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
00407    }
00408    
00409    /* Free ourselves */
00410    ast_free(audiohook_list);
00411 
00412    return 0;
00413 }
00414 
00415 /*! \brief find an audiohook based on its source
00416  * \param audiohook_list The list of audiohooks to search in
00417  * \param source The source of the audiohook we wish to find
00418  * \return Return the corresponding audiohook or NULL if it cannot be found.
00419  */
00420 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
00421 {
00422    struct ast_audiohook *audiohook = NULL;
00423 
00424    AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
00425       if (!strcasecmp(audiohook->source, source))
00426          return audiohook;
00427    }
00428 
00429    AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
00430       if (!strcasecmp(audiohook->source, source))
00431          return audiohook;
00432    }
00433 
00434    AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
00435       if (!strcasecmp(audiohook->source, source))
00436          return audiohook;
00437    }
00438 
00439    return NULL;
00440 }
00441 
00442 void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
00443 {
00444    struct ast_audiohook *audiohook;
00445 
00446    if (!old_chan->audiohooks || !(audiohook = find_audiohook_by_source(old_chan->audiohooks, source))) {
00447       return;
00448    }
00449 
00450    /* By locking both channels and the audiohook, we can assure that
00451     * another thread will not have a chance to read the audiohook's status
00452     * as done, even though ast_audiohook_remove signals the trigger
00453     * condition
00454     */
00455    ast_audiohook_lock(audiohook);
00456    ast_audiohook_remove(old_chan, audiohook);
00457    ast_audiohook_attach(new_chan, audiohook);
00458    ast_audiohook_unlock(audiohook);
00459 }
00460 
00461 /*! \brief Detach specified source audiohook from channel
00462  * \param chan Channel to detach from
00463  * \param source Name of source to detach
00464  * \return Returns 0 on success, -1 on failure
00465  */
00466 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
00467 {
00468    struct ast_audiohook *audiohook = NULL;
00469 
00470    ast_channel_lock(chan);
00471 
00472    /* Ensure the channel has audiohooks on it */
00473    if (!chan->audiohooks) {
00474       ast_channel_unlock(chan);
00475       return -1;
00476    }
00477 
00478    audiohook = find_audiohook_by_source(chan->audiohooks, source);
00479 
00480    ast_channel_unlock(chan);
00481 
00482    if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
00483       audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
00484 
00485    return (audiohook ? 0 : -1);
00486 }
00487 
00488 /*!
00489  * \brief Remove an audiohook from a specified channel
00490  *
00491  * \param chan Channel to remove from
00492  * \param audiohook Audiohook to remove
00493  *
00494  * \return Returns 0 on success, -1 on failure
00495  *
00496  * \note The channel does not need to be locked before calling this function
00497  */
00498 int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
00499 {
00500    ast_channel_lock(chan);
00501 
00502    if (!chan->audiohooks) {
00503       ast_channel_unlock(chan);
00504       return -1;
00505    }
00506 
00507    if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
00508       AST_LIST_REMOVE(&chan->audiohooks->spy_list, audiohook, list);
00509    else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
00510       AST_LIST_REMOVE(&chan->audiohooks->whisper_list, audiohook, list);
00511    else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
00512       AST_LIST_REMOVE(&chan->audiohooks->manipulate_list, audiohook, list);
00513 
00514    ast_audiohook_lock(audiohook);
00515    audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00516    ast_cond_signal(&audiohook->trigger);
00517    ast_audiohook_unlock(audiohook);
00518 
00519    ast_channel_unlock(chan);
00520 
00521    return 0;
00522 }
00523 
00524 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
00525  * \param chan Channel that the list is coming off of
00526  * \param audiohook_list List of audiohooks
00527  * \param direction Direction frame is coming in from
00528  * \param frame The frame itself
00529  * \return Return frame on success, NULL on failure
00530  */
00531 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)
00532 {
00533    struct ast_audiohook *audiohook = NULL;
00534 
00535    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00536       ast_audiohook_lock(audiohook);
00537       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00538          AST_LIST_REMOVE_CURRENT(list);
00539          audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00540          ast_audiohook_unlock(audiohook);
00541          audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00542          continue;
00543       }
00544       if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
00545          audiohook->manipulate_callback(audiohook, chan, frame, direction);
00546       ast_audiohook_unlock(audiohook);
00547    }
00548    AST_LIST_TRAVERSE_SAFE_END;
00549 
00550    return frame;
00551 }
00552 
00553 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
00554  * \param chan Channel that the list is coming off of
00555  * \param audiohook_list List of audiohooks
00556  * \param direction Direction frame is coming in from
00557  * \param frame The frame itself
00558  * \return Return frame on success, NULL on failure
00559  */
00560 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)
00561 {
00562    struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
00563    struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
00564    struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
00565    struct ast_audiohook *audiohook = NULL;
00566    int samples = frame->samples;
00567    
00568    /* If the frame coming in is not signed linear we have to send it through the in_translate path */
00569    if (frame->subclass != AST_FORMAT_SLINEAR) {
00570       if (in_translate->format != frame->subclass) {
00571          if (in_translate->trans_pvt)
00572             ast_translator_free_path(in_translate->trans_pvt);
00573          if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
00574             return frame;
00575          in_translate->format = frame->subclass;
00576       }
00577       if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
00578          return frame;
00579       samples = middle_frame->samples;
00580    }
00581 
00582    /* Queue up signed linear frame to each spy */
00583    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
00584       ast_audiohook_lock(audiohook);
00585       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00586          AST_LIST_REMOVE_CURRENT(list);
00587          audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00588          ast_cond_signal(&audiohook->trigger);
00589          ast_audiohook_unlock(audiohook);
00590          continue;
00591       }
00592       ast_audiohook_write_frame(audiohook, direction, middle_frame);
00593       ast_audiohook_unlock(audiohook);
00594    }
00595    AST_LIST_TRAVERSE_SAFE_END;
00596 
00597    /* If this frame is being written out to the channel then we need to use whisper sources */
00598    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
00599       int i = 0;
00600       short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
00601       memset(&combine_buf, 0, sizeof(combine_buf));
00602       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
00603          ast_audiohook_lock(audiohook);
00604          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00605             AST_LIST_REMOVE_CURRENT(list);
00606             audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00607             ast_cond_signal(&audiohook->trigger);
00608             ast_audiohook_unlock(audiohook);
00609             continue;
00610          }
00611          if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
00612             /* Take audio from this whisper source and combine it into our main buffer */
00613             for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
00614                ast_slinear_saturated_add(data1, data2);
00615          }
00616          ast_audiohook_unlock(audiohook);
00617       }
00618       AST_LIST_TRAVERSE_SAFE_END;
00619       /* We take all of the combined whisper sources and combine them into the audio being written out */
00620       for (i = 0, data1 = middle_frame->data.ptr, data2 = combine_buf; i < samples; i++, data1++, data2++)
00621          ast_slinear_saturated_add(data1, data2);
00622       end_frame = middle_frame;
00623    }
00624 
00625    /* Pass off frame to manipulate audiohooks */
00626    if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
00627       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00628          ast_audiohook_lock(audiohook);
00629          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00630             AST_LIST_REMOVE_CURRENT(list);
00631             audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00632             ast_audiohook_unlock(audiohook);
00633             /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
00634             audiohook->manipulate_callback(audiohook, chan, NULL, direction);
00635             continue;
00636          }
00637          /* Feed in frame to manipulation */
00638          audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
00639          ast_audiohook_unlock(audiohook);
00640       }
00641       AST_LIST_TRAVERSE_SAFE_END;
00642       end_frame = middle_frame;
00643    }
00644 
00645    /* Now we figure out what to do with our end frame (whether to transcode or not) */
00646    if (middle_frame == end_frame) {
00647       /* Middle frame was modified and became the end frame... let's see if we need to transcode */
00648       if (end_frame->subclass != start_frame->subclass) {
00649          if (out_translate->format != start_frame->subclass) {
00650             if (out_translate->trans_pvt)
00651                ast_translator_free_path(out_translate->trans_pvt);
00652             if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
00653                /* We can't transcode this... drop our middle frame and return the original */
00654                ast_frfree(middle_frame);
00655                return start_frame;
00656             }
00657             out_translate->format = start_frame->subclass;
00658          }
00659          /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
00660          if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
00661             /* Failed to transcode the frame... drop it and return the original */
00662             ast_frfree(middle_frame);
00663             return start_frame;
00664          }
00665          /* Here's the scoop... middle frame is no longer of use to us */
00666          ast_frfree(middle_frame);
00667       }
00668    } else {
00669       /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
00670       ast_frfree(middle_frame);
00671    }
00672 
00673    return end_frame;
00674 }
00675 
00676 /*! \brief Pass a frame off to be handled by the audiohook core
00677  * \param chan Channel that the list is coming off of
00678  * \param audiohook_list List of audiohooks
00679  * \param direction Direction frame is coming in from
00680  * \param frame The frame itself
00681  * \return Return frame on success, NULL on failure
00682  */
00683 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)
00684 {
00685    /* Pass off frame to it's respective list write function */
00686    if (frame->frametype == AST_FRAME_VOICE)
00687       return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
00688    else if (frame->frametype == AST_FRAME_DTMF)
00689       return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
00690    else
00691       return frame;
00692 }
00693          
00694 
00695 /*! \brief Wait for audiohook trigger to be triggered
00696  * \param audiohook Audiohook to wait on
00697  */
00698 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
00699 {
00700    struct timeval wait;
00701    struct timespec ts;
00702 
00703    wait = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
00704    ts.tv_sec = wait.tv_sec;
00705    ts.tv_nsec = wait.tv_usec * 1000;
00706    
00707    ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
00708    
00709    return;
00710 }
00711 
00712 /* Count number of channel audiohooks by type, regardless of type */
00713 int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
00714 {
00715    int count = 0;
00716    struct ast_audiohook *ah = NULL;
00717 
00718    if (!chan->audiohooks)
00719       return -1;
00720 
00721    switch (type) {
00722       case AST_AUDIOHOOK_TYPE_SPY:
00723          AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->spy_list, ah, list) {
00724             if (!strcmp(ah->source, source)) {
00725                count++;
00726             }
00727          }
00728          AST_LIST_TRAVERSE_SAFE_END;
00729          break;
00730       case AST_AUDIOHOOK_TYPE_WHISPER:
00731          AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->whisper_list, ah, list) {
00732             if (!strcmp(ah->source, source)) {
00733                count++;
00734             }
00735          }
00736          AST_LIST_TRAVERSE_SAFE_END;
00737          break;
00738       case AST_AUDIOHOOK_TYPE_MANIPULATE:
00739          AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->manipulate_list, ah, list) {
00740             if (!strcmp(ah->source, source)) {
00741                count++;
00742             }
00743          }
00744          AST_LIST_TRAVERSE_SAFE_END;
00745          break;
00746       default:
00747          ast_log(LOG_DEBUG, "Invalid audiohook type supplied, (%d)\n", type);
00748          return -1;
00749    }
00750 
00751    return count;
00752 }
00753 
00754 /* Count number of channel audiohooks by type that are running */
00755 int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
00756 {
00757    int count = 0;
00758    struct ast_audiohook *ah = NULL;
00759    if (!chan->audiohooks)
00760       return -1;
00761 
00762    switch (type) {
00763       case AST_AUDIOHOOK_TYPE_SPY:
00764          AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->spy_list, ah, list) {
00765             if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
00766                count++;
00767          }
00768          AST_LIST_TRAVERSE_SAFE_END;
00769          break;
00770       case AST_AUDIOHOOK_TYPE_WHISPER:
00771          AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->whisper_list, ah, list) {
00772             if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
00773                count++;
00774          }
00775          AST_LIST_TRAVERSE_SAFE_END;
00776          break;
00777       case AST_AUDIOHOOK_TYPE_MANIPULATE:
00778          AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->audiohooks->manipulate_list, ah, list) {
00779             if ((!strcmp(ah->source, source)) && (ah->status == AST_AUDIOHOOK_STATUS_RUNNING))
00780                count++;
00781          }
00782          AST_LIST_TRAVERSE_SAFE_END;
00783          break;
00784       default:
00785          ast_log(LOG_DEBUG, "Invalid audiohook type supplied, (%d)\n", type);
00786          return -1;
00787    }
00788    return count;
00789 }
00790 
00791 /*! \brief Audiohook volume adjustment structure */
00792 struct audiohook_volume {
00793    struct ast_audiohook audiohook; /*!< Audiohook attached to the channel */
00794    int read_adjustment;            /*!< Value to adjust frames read from the channel by */
00795    int write_adjustment;           /*!< Value to adjust frames written to the channel by */
00796 };
00797 
00798 /*! \brief Callback used to destroy the audiohook volume datastore
00799  * \param data Volume information structure
00800  * \return Returns nothing
00801  */
00802 static void audiohook_volume_destroy(void *data)
00803 {
00804    struct audiohook_volume *audiohook_volume = data;
00805 
00806    /* Destroy the audiohook as it is no longer in use */
00807    ast_audiohook_destroy(&audiohook_volume->audiohook);
00808 
00809    /* Finally free ourselves, we are of no more use */
00810    ast_free(audiohook_volume);
00811 
00812    return;
00813 }
00814 
00815 /*! \brief Datastore used to store audiohook volume information */
00816 static const struct ast_datastore_info audiohook_volume_datastore = {
00817    .type = "Volume",
00818    .destroy = audiohook_volume_destroy,
00819 };
00820 
00821 /*! \brief Helper function which actually gets called by audiohooks to perform the adjustment
00822  * \param audiohook Audiohook attached to the channel
00823  * \param chan Channel we are attached to
00824  * \param frame Frame of audio we want to manipulate
00825  * \param direction Direction the audio came in from
00826  * \return Returns 0 on success, -1 on failure
00827  */
00828 static int audiohook_volume_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
00829 {
00830    struct ast_datastore *datastore = NULL;
00831    struct audiohook_volume *audiohook_volume = NULL;
00832    int *gain = NULL;
00833 
00834    /* If the audiohook is shutting down don't even bother */
00835    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
00836       return 0;
00837    }
00838 
00839    /* Try to find the datastore containg adjustment information, if we can't just bail out */
00840    if (!(datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
00841       return 0;
00842    }
00843 
00844    audiohook_volume = datastore->data;
00845 
00846    /* Based on direction grab the appropriate adjustment value */
00847    if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
00848       gain = &audiohook_volume->read_adjustment;
00849    } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
00850       gain = &audiohook_volume->write_adjustment;
00851    }
00852 
00853    /* If an adjustment value is present modify the frame */
00854    if (gain && *gain) {
00855       ast_frame_adjust_volume(frame, *gain);
00856    }
00857 
00858    return 0;
00859 }
00860 
00861 /*! \brief Helper function which finds and optionally creates an audiohook_volume_datastore datastore on a channel
00862  * \param chan Channel to look on
00863  * \param create Whether to create the datastore if not found
00864  * \return Returns audiohook_volume structure on success, NULL on failure
00865  */
00866 static struct audiohook_volume *audiohook_volume_get(struct ast_channel *chan, int create)
00867 {
00868    struct ast_datastore *datastore = NULL;
00869    struct audiohook_volume *audiohook_volume = NULL;
00870 
00871    /* If we are able to find the datastore return the contents (which is actually an audiohook_volume structure) */
00872    if ((datastore = ast_channel_datastore_find(chan, &audiohook_volume_datastore, NULL))) {
00873       return datastore->data;
00874    }
00875 
00876    /* 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 */
00877    if (!create || !(datastore = ast_datastore_alloc(&audiohook_volume_datastore, NULL))) {
00878       return NULL;
00879    }
00880 
00881    /* Create a new audiohook_volume structure to contain our adjustments and audiohook */
00882    if (!(audiohook_volume = ast_calloc(1, sizeof(*audiohook_volume)))) {
00883       ast_datastore_free(datastore);
00884       return NULL;
00885    }
00886 
00887    /* Setup our audiohook structure so we can manipulate the audio */
00888    ast_audiohook_init(&audiohook_volume->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
00889    audiohook_volume->audiohook.manipulate_callback = audiohook_volume_callback;
00890 
00891    /* Attach the audiohook_volume blob to the datastore and attach to the channel */
00892    datastore->data = audiohook_volume;
00893    ast_channel_datastore_add(chan, datastore);
00894 
00895    /* All is well... put the audiohook into motion */
00896    ast_audiohook_attach(chan, &audiohook_volume->audiohook);
00897 
00898    return audiohook_volume;
00899 }
00900 
00901 /*! \brief Adjust the volume on frames read from or written to a channel
00902  * \param chan Channel to muck with
00903  * \param direction Direction to set on
00904  * \param volume Value to adjust the volume by
00905  * \return Returns 0 on success, -1 on failure
00906  */
00907 int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
00908 {
00909    struct audiohook_volume *audiohook_volume = NULL;
00910 
00911    /* Attempt to find the audiohook volume information, but only create it if we are not setting the adjustment value to zero */
00912    if (!(audiohook_volume = audiohook_volume_get(chan, (volume ? 1 : 0)))) {
00913       return -1;
00914    }
00915 
00916    /* Now based on the direction set the proper value */
00917    if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
00918       audiohook_volume->read_adjustment = volume;
00919    }
00920    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
00921       audiohook_volume->write_adjustment = volume;
00922    }
00923 
00924    return 0;
00925 }
00926 
00927 /*! \brief Retrieve the volume adjustment value on frames read from or written to a channel
00928  * \param chan Channel to retrieve volume adjustment from
00929  * \param direction Direction to retrieve
00930  * \return Returns adjustment value
00931  */
00932 int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
00933 {
00934    struct audiohook_volume *audiohook_volume = NULL;
00935    int adjustment = 0;
00936 
00937    /* Attempt to find the audiohook volume information, but do not create it as we only want to look at the values */
00938    if (!(audiohook_volume = audiohook_volume_get(chan, 0))) {
00939       return 0;
00940    }
00941 
00942    /* Grab the adjustment value based on direction given */
00943    if (direction == AST_AUDIOHOOK_DIRECTION_READ) {
00944       adjustment = audiohook_volume->read_adjustment;
00945    } else if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
00946       adjustment = audiohook_volume->write_adjustment;
00947    }
00948 
00949    return adjustment;
00950 }
00951 
00952 /*! \brief Adjust the volume on frames read from or written to a channel
00953  * \param chan Channel to muck with
00954  * \param direction Direction to increase
00955  * \param volume Value to adjust the adjustment by
00956  * \return Returns 0 on success, -1 on failure
00957  */
00958 int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
00959 {
00960    struct audiohook_volume *audiohook_volume = NULL;
00961 
00962    /* Attempt to find the audiohook volume information, and create an audiohook if none exists */
00963    if (!(audiohook_volume = audiohook_volume_get(chan, 1))) {
00964       return -1;
00965    }
00966 
00967    /* Based on the direction change the specific adjustment value */
00968    if (direction == AST_AUDIOHOOK_DIRECTION_READ || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
00969       audiohook_volume->read_adjustment += volume;
00970    }
00971    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE || direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
00972       audiohook_volume->write_adjustment += volume;
00973    }
00974 
00975    return 0;
00976 }

Generated on Fri Jul 24 00:40:44 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7