Mon Mar 31 07:37:57 2008

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 Colp <jcolp@digium.com>
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029 
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <signal.h>
00034 #include <errno.h>
00035 #include <unistd.h>
00036 
00037 #include "asterisk/logger.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/options.h"
00040 #include "asterisk/utils.h"
00041 #include "asterisk/lock.h"
00042 #include "asterisk/linkedlists.h"
00043 #include "asterisk/audiohook.h"
00044 #include "asterisk/slinfactory.h"
00045 #include "asterisk/frame.h"
00046 #include "asterisk/translate.h"
00047 
00048 struct ast_audiohook_translate {
00049    struct ast_trans_pvt *trans_pvt;
00050    int format;
00051 };
00052 
00053 struct ast_audiohook_list {
00054    struct ast_audiohook_translate in_translate[2];
00055    struct ast_audiohook_translate out_translate[2];
00056    AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
00057    AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
00058    AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
00059 };
00060 
00061 /*! \brief Initialize an audiohook structure
00062  * \param audiohook Audiohook structure
00063  * \param type
00064  * \param source
00065  * \return Returns 0 on success, -1 on failure
00066  */
00067 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
00068 {
00069    /* Need to keep the type and source */
00070    audiohook->type = type;
00071    audiohook->source = source;
00072 
00073    /* Initialize lock that protects our audiohook */
00074    ast_mutex_init(&audiohook->lock);
00075    ast_cond_init(&audiohook->trigger, NULL);
00076 
00077    /* Setup the factories that are needed for this audiohook type */
00078    switch (type) {
00079    case AST_AUDIOHOOK_TYPE_SPY:
00080       ast_slinfactory_init(&audiohook->read_factory);
00081    case AST_AUDIOHOOK_TYPE_WHISPER:
00082       ast_slinfactory_init(&audiohook->write_factory);
00083       break;
00084    default:
00085       break;
00086    }
00087 
00088    /* Since we are just starting out... this audiohook is new */
00089    audiohook->status = AST_AUDIOHOOK_STATUS_NEW;
00090 
00091    return 0;
00092 }
00093 
00094 /*! \brief Destroys an audiohook structure
00095  * \param audiohook Audiohook structure
00096  * \return Returns 0 on success, -1 on failure
00097  */
00098 int ast_audiohook_destroy(struct ast_audiohook *audiohook)
00099 {
00100    /* Drop the factories used by this audiohook type */
00101    switch (audiohook->type) {
00102    case AST_AUDIOHOOK_TYPE_SPY:
00103       ast_slinfactory_destroy(&audiohook->read_factory);
00104    case AST_AUDIOHOOK_TYPE_WHISPER:
00105       ast_slinfactory_destroy(&audiohook->write_factory);
00106       break;
00107    default:
00108       break;
00109    }
00110 
00111    /* Destroy translation path if present */
00112    if (audiohook->trans_pvt)
00113       ast_translator_free_path(audiohook->trans_pvt);
00114 
00115    /* Lock and trigger be gone! */
00116    ast_cond_destroy(&audiohook->trigger);
00117    ast_mutex_destroy(&audiohook->lock);
00118 
00119    return 0;
00120 }
00121 
00122 /*! \brief Writes a frame into the audiohook structure
00123  * \param audiohook Audiohook structure
00124  * \param direction Direction the audio frame came from
00125  * \param frame Frame to write in
00126  * \return Returns 0 on success, -1 on failure
00127  */
00128 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
00129 {
00130    struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00131    struct timeval *time = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time);
00132 
00133    /* Write frame out to respective factory */
00134    ast_slinfactory_feed(factory, frame);
00135 
00136    /* Update last fed time for the above factory */
00137    *time = ast_tvnow();
00138 
00139    /* If we need to notify the respective handler of this audiohook, do so */
00140    if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
00141       ast_cond_signal(&audiohook->trigger);
00142    } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
00143       ast_cond_signal(&audiohook->trigger);
00144    } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
00145       ast_cond_signal(&audiohook->trigger);
00146    }
00147 
00148    return 0;
00149 }
00150 
00151 static struct ast_frame *audiohook_read_frame_single(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
00152 {
00153    struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00154    int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
00155    short buf[samples];
00156    struct ast_frame frame = {
00157       .frametype = AST_FRAME_VOICE,
00158       .subclass = AST_FORMAT_SLINEAR,
00159       .data = buf,
00160       .datalen = sizeof(buf),
00161       .samples = samples,
00162    };
00163 
00164    /* Ensure the factory is able to give us the samples we want */
00165    if (samples > ast_slinfactory_available(factory))
00166       return NULL;
00167    
00168    /* Read data in from factory */
00169    if (!ast_slinfactory_read(factory, buf, samples))
00170       return NULL;
00171 
00172    /* If a volume adjustment needs to be applied apply it */
00173    if (vol)
00174       ast_frame_adjust_volume(&frame, vol);
00175 
00176    return ast_frdup(&frame);
00177 }
00178 
00179 static struct ast_frame *audiohook_read_frame_both(struct ast_audiohook *audiohook, size_t samples)
00180 {
00181    int i = 0, usable_read, usable_write;
00182    short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
00183    struct ast_frame frame = {
00184       .frametype = AST_FRAME_VOICE,
00185       .subclass = AST_FORMAT_SLINEAR,
00186       .data = NULL,
00187       .datalen = sizeof(buf1),
00188       .samples = samples,
00189    };
00190 
00191    /* Make sure both factories have the required samples */
00192    usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
00193    usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
00194 
00195    if (!usable_read && !usable_write) {
00196       /* If both factories are unusable bail out */
00197       if (option_debug)
00198          ast_log(LOG_DEBUG, "Read factory %p and write factory %p both fail to provide %zd samples\n", &audiohook->read_factory, &audiohook->write_factory, samples);
00199       return NULL;
00200    }
00201 
00202    /* If we want to provide only a read factory make sure we aren't waiting for other audio */
00203    if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
00204       if (option_debug)
00205          ast_log(LOG_DEBUG, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
00206       return NULL;
00207    }
00208 
00209    /* If we want to provide only a write factory make sure we aren't waiting for other audio */
00210    if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
00211       if (option_debug)
00212          ast_log(LOG_DEBUG, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
00213       return NULL;
00214    }
00215 
00216    /* Start with the read factory... if there are enough samples, read them in */
00217    if (usable_read && ast_slinfactory_available(&audiohook->read_factory) >= samples) {
00218       if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
00219          read_buf = buf1;
00220          /* Adjust read volume if need be */
00221          if (audiohook->options.read_volume) {
00222             int count = 0;
00223             short adjust_value = abs(audiohook->options.read_volume);
00224             for (count = 0; count < samples; count++) {
00225                if (audiohook->options.read_volume > 0)
00226                   ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
00227                else if (audiohook->options.read_volume < 0)
00228                   ast_slinear_saturated_divide(&buf1[count], &adjust_value);
00229             }
00230          }
00231       }
00232    } else if (option_debug)
00233       ast_log(LOG_DEBUG, "Failed to get %zd samples from read factory %p\n", samples, &audiohook->read_factory);
00234 
00235    /* Move on to the write factory... if there are enough samples, read them in */
00236    if (usable_write && ast_slinfactory_available(&audiohook->write_factory) >= samples) {
00237       if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
00238          write_buf = buf2;
00239          /* Adjust write volume if need be */
00240          if (audiohook->options.write_volume) {
00241             int count = 0;
00242             short adjust_value = abs(audiohook->options.write_volume);
00243             for (count = 0; count < samples; count++) {
00244                if (audiohook->options.write_volume > 0)
00245                   ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
00246                else if (audiohook->options.write_volume < 0)
00247                   ast_slinear_saturated_divide(&buf2[count], &adjust_value);
00248             }
00249          }
00250       }
00251    } else if (option_debug)
00252       ast_log(LOG_DEBUG, "Failed to get %zd samples from write factory %p\n", samples, &audiohook->write_factory);
00253 
00254    /* Basically we figure out which buffer to use... and if mixing can be done here */
00255    if (!read_buf && !write_buf)
00256       return NULL;
00257    else if (read_buf && write_buf) {
00258       for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
00259          ast_slinear_saturated_add(data1, data2);
00260       final_buf = buf1;
00261    } else if (read_buf)
00262       final_buf = buf1;
00263    else if (write_buf)
00264       final_buf = buf2;
00265 
00266    /* Make the final buffer part of the frame, so it gets duplicated fine */
00267    frame.data = final_buf;
00268 
00269    /* Yahoo, a combined copy of the audio! */
00270    return ast_frdup(&frame);
00271 }
00272 
00273 /*! \brief Reads a frame in from the audiohook structure
00274  * \param audiohook Audiohook structure
00275  * \param samples Number of samples wanted
00276  * \param direction Direction the audio frame came from
00277  * \param format Format of frame remote side wants back
00278  * \return Returns frame on success, NULL on failure
00279  */
00280 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
00281 {
00282    struct ast_frame *read_frame = NULL, *final_frame = NULL;
00283 
00284    if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
00285       return NULL;
00286 
00287    /* If they don't want signed linear back out, we'll have to send it through the translation path */
00288    if (format != AST_FORMAT_SLINEAR) {
00289       /* Rebuild translation path if different format then previously */
00290       if (audiohook->format != format) {
00291          if (audiohook->trans_pvt) {
00292             ast_translator_free_path(audiohook->trans_pvt);
00293             audiohook->trans_pvt = NULL;
00294          }
00295          /* 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 */
00296          if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
00297             ast_frfree(read_frame);
00298             return NULL;
00299          }
00300       }
00301       /* Convert to requested format, and allow the read in frame to be freed */
00302       final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
00303    } else {
00304       final_frame = read_frame;
00305    }
00306 
00307    return final_frame;
00308 }
00309 
00310 /*! \brief Attach audiohook to channel
00311  * \param chan Channel
00312  * \param audiohook Audiohook structure
00313  * \return Returns 0 on success, -1 on failure
00314  */
00315 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
00316 {
00317    ast_channel_lock(chan);
00318 
00319    if (!chan->audiohooks) {
00320       /* Whoops... allocate a new structure */
00321       if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
00322          ast_channel_unlock(chan);
00323          return -1;
00324       }
00325       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
00326       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
00327       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
00328    }
00329 
00330    /* Drop into respective list */
00331    if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
00332       AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
00333    else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
00334       AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
00335    else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
00336       AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
00337 
00338    /* Change status over to running since it is now attached */
00339    audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
00340 
00341    ast_channel_unlock(chan);
00342 
00343    return 0;
00344 }
00345 
00346 /*! \brief Detach audiohook from channel
00347  * \param audiohook Audiohook structure
00348  * \return Returns 0 on success, -1 on failure
00349  */
00350 int ast_audiohook_detach(struct ast_audiohook *audiohook)
00351 {
00352    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
00353       return 0;
00354 
00355    audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
00356 
00357    while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
00358       ast_audiohook_trigger_wait(audiohook);
00359 
00360    return 0;
00361 }
00362 
00363 /*! \brief Detach audiohooks from list and destroy said list
00364  * \param audiohook_list List of audiohooks
00365  * \return Returns 0 on success, -1 on failure
00366  */
00367 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
00368 {
00369    int i = 0;
00370    struct ast_audiohook *audiohook = NULL;
00371 
00372    /* Drop any spies */
00373    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
00374       ast_audiohook_lock(audiohook);
00375       AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
00376       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00377       ast_cond_signal(&audiohook->trigger);
00378       ast_audiohook_unlock(audiohook);
00379    }
00380    AST_LIST_TRAVERSE_SAFE_END
00381 
00382    /* Drop any whispering sources */
00383    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
00384       ast_audiohook_lock(audiohook);
00385       AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
00386       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00387       ast_cond_signal(&audiohook->trigger);
00388       ast_audiohook_unlock(audiohook);
00389    }
00390    AST_LIST_TRAVERSE_SAFE_END
00391 
00392    /* Drop any manipulaters */
00393    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00394       ast_audiohook_lock(audiohook);
00395       AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
00396       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00397       ast_audiohook_unlock(audiohook);
00398       audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00399    }
00400    AST_LIST_TRAVERSE_SAFE_END
00401 
00402    /* Drop translation paths if present */
00403    for (i = 0; i < 2; i++) {
00404       if (audiohook_list->in_translate[i].trans_pvt)
00405          ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
00406       if (audiohook_list->out_translate[i].trans_pvt)
00407          ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
00408    }
00409    
00410    /* Free ourselves */
00411    ast_free(audiohook_list);
00412 
00413    return 0;
00414 }
00415 
00416 static struct ast_audiohook *find_audiohook_by_source(struct ast_audiohook_list *audiohook_list, const char *source)
00417 {
00418    struct ast_audiohook *audiohook = NULL;
00419 
00420    AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
00421       if (!strcasecmp(audiohook->source, source))
00422          return audiohook;
00423    }
00424 
00425    AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
00426       if (!strcasecmp(audiohook->source, source))
00427          return audiohook;
00428    }
00429 
00430    AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
00431       if (!strcasecmp(audiohook->source, source))
00432          return audiohook;
00433    }
00434 
00435    return NULL;
00436 }
00437 
00438 /*! \brief Detach specified source audiohook from channel
00439  * \param chan Channel to detach from
00440  * \param source Name of source to detach
00441  * \return Returns 0 on success, -1 on failure
00442  */
00443 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
00444 {
00445    struct ast_audiohook *audiohook = NULL;
00446 
00447    ast_channel_lock(chan);
00448 
00449    /* Ensure the channel has audiohooks on it */
00450    if (!chan->audiohooks) {
00451       ast_channel_unlock(chan);
00452       return -1;
00453    }
00454 
00455    audiohook = find_audiohook_by_source(chan->audiohooks, source);
00456 
00457    ast_channel_unlock(chan);
00458 
00459    if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
00460       audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
00461 
00462    return (audiohook ? 0 : -1);
00463 }
00464 
00465 /*! \brief Pass a DTMF frame off to be handled by the audiohook core
00466  * \param chan Channel that the list is coming off of
00467  * \param audiohook_list List of audiohooks
00468  * \param direction Direction frame is coming in from
00469  * \param frame The frame itself
00470  * \return Return frame on success, NULL on failure
00471  */
00472 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)
00473 {
00474    struct ast_audiohook *audiohook = NULL;
00475 
00476    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00477       ast_audiohook_lock(audiohook);
00478       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00479          AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
00480          audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00481          ast_audiohook_unlock(audiohook);
00482          audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00483          continue;
00484       }
00485       if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
00486          audiohook->manipulate_callback(audiohook, chan, frame, direction);
00487       ast_audiohook_unlock(audiohook);
00488    }
00489    AST_LIST_TRAVERSE_SAFE_END
00490 
00491    return frame;
00492 }
00493 
00494 /*! \brief Pass an AUDIO frame off to be handled by the audiohook core
00495  * \param chan Channel that the list is coming off of
00496  * \param audiohook_list List of audiohooks
00497  * \param direction Direction frame is coming in from
00498  * \param frame The frame itself
00499  * \return Return frame on success, NULL on failure
00500  */
00501 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)
00502 {
00503    struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
00504    struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
00505    struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
00506    struct ast_audiohook *audiohook = NULL;
00507    int samples = frame->samples;
00508    
00509    /* If the frame coming in is not signed linear we have to send it through the in_translate path */
00510    if (frame->subclass != AST_FORMAT_SLINEAR) {
00511       if (in_translate->format != frame->subclass) {
00512          if (in_translate->trans_pvt)
00513             ast_translator_free_path(in_translate->trans_pvt);
00514          if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
00515             return frame;
00516          in_translate->format = frame->subclass;
00517       }
00518       if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
00519          return frame;
00520    }
00521 
00522    /* Queue up signed linear frame to each spy */
00523    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
00524       ast_audiohook_lock(audiohook);
00525       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00526          AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
00527          audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00528          ast_cond_signal(&audiohook->trigger);
00529          ast_audiohook_unlock(audiohook);
00530          continue;
00531       }
00532       ast_audiohook_write_frame(audiohook, direction, middle_frame);
00533       ast_audiohook_unlock(audiohook);
00534    }
00535    AST_LIST_TRAVERSE_SAFE_END
00536 
00537    /* If this frame is being written out to the channel then we need to use whisper sources */
00538    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
00539       int i = 0;
00540       short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
00541       memset(&combine_buf, 0, sizeof(combine_buf));
00542       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
00543          ast_audiohook_lock(audiohook);
00544          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00545             AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
00546             audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00547             ast_cond_signal(&audiohook->trigger);
00548             ast_audiohook_unlock(audiohook);
00549             continue;
00550          }
00551          if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
00552             /* Take audio from this whisper source and combine it into our main buffer */
00553             for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
00554                ast_slinear_saturated_add(data1, data2);
00555          }
00556          ast_audiohook_unlock(audiohook);
00557       }
00558       AST_LIST_TRAVERSE_SAFE_END
00559       /* We take all of the combined whisper sources and combine them into the audio being written out */
00560       for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
00561          ast_slinear_saturated_add(data1, data2);
00562       end_frame = middle_frame;
00563    }
00564 
00565    /* Pass off frame to manipulate audiohooks */
00566    if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
00567       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00568          ast_audiohook_lock(audiohook);
00569          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00570             AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
00571             audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00572             ast_audiohook_unlock(audiohook);
00573             /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
00574             audiohook->manipulate_callback(audiohook, chan, NULL, direction);
00575             continue;
00576          }
00577          /* Feed in frame to manipulation */
00578          audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
00579          ast_audiohook_unlock(audiohook);
00580       }
00581       AST_LIST_TRAVERSE_SAFE_END
00582       end_frame = middle_frame;
00583    }
00584 
00585    /* Now we figure out what to do with our end frame (whether to transcode or not) */
00586    if (middle_frame == end_frame) {
00587       /* Middle frame was modified and became the end frame... let's see if we need to transcode */
00588       if (end_frame->subclass != start_frame->subclass) {
00589          if (out_translate->format != start_frame->subclass) {
00590             if (out_translate->trans_pvt)
00591                ast_translator_free_path(out_translate->trans_pvt);
00592             if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
00593                /* We can't transcode this... drop our middle frame and return the original */
00594                ast_frfree(middle_frame);
00595                return start_frame;
00596             }
00597             out_translate->format = start_frame->subclass;
00598          }
00599          /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
00600          if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
00601             /* Failed to transcode the frame... drop it and return the original */
00602             ast_frfree(middle_frame);
00603             return start_frame;
00604          }
00605          /* Here's the scoop... middle frame is no longer of use to us */
00606          ast_frfree(middle_frame);
00607       }
00608    } else {
00609       /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
00610       ast_frfree(middle_frame);
00611    }
00612 
00613    return end_frame;
00614 }
00615 
00616 /*! \brief Pass a frame off to be handled by the audiohook core
00617  * \param chan Channel that the list is coming off of
00618  * \param audiohook_list List of audiohooks
00619  * \param direction Direction frame is coming in from
00620  * \param frame The frame itself
00621  * \return Return frame on success, NULL on failure
00622  */
00623 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)
00624 {
00625    /* Pass off frame to it's respective list write function */
00626    if (frame->frametype == AST_FRAME_VOICE)
00627       return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
00628    else if (frame->frametype == AST_FRAME_DTMF)
00629       return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
00630    else
00631       return frame;
00632 }
00633          
00634 
00635 /*! \brief Wait for audiohook trigger to be triggered
00636  * \param audiohook Audiohook to wait on
00637  */
00638 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
00639 {
00640    struct timeval tv;
00641    struct timespec ts;
00642 
00643    tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
00644    ts.tv_sec = tv.tv_sec;
00645    ts.tv_nsec = tv.tv_usec * 1000;
00646    
00647    ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
00648    
00649    return;
00650 }

Generated on Mon Mar 31 07:37:57 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1