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

Generated on Mon Nov 24 15:34:08 2008 for Asterisk - the Open Source PBX by  doxygen 1.4.7