Wed Mar 4 19:58:23 2009

Asterisk developer's documentation


audiohook.c File Reference

Audiohooks Architecture. More...

#include "asterisk.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/audiohook.h"
#include "asterisk/slinfactory.h"
#include "asterisk/frame.h"
#include "asterisk/translate.h"

Go to the source code of this file.

Data Structures

struct  ast_audiohook_list
struct  ast_audiohook_translate

Functions

int ast_audiohook_attach (struct ast_channel *chan, struct ast_audiohook *audiohook)
 Attach audiohook to channel.
int ast_audiohook_destroy (struct ast_audiohook *audiohook)
 Destroys an audiohook structure.
int ast_audiohook_detach (struct ast_audiohook *audiohook)
 Detach audiohook from channel.
int ast_audiohook_detach_list (struct ast_audiohook_list *audiohook_list)
 Detach audiohooks from list and destroy said list.
int ast_audiohook_detach_source (struct ast_channel *chan, const char *source)
 Detach specified source audiohook from channel.
int ast_audiohook_init (struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
 Initialize an audiohook structure.
void ast_audiohook_move_by_source (struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source)
 Move an audiohook from one channel to a new one.
ast_frameast_audiohook_read_frame (struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format)
 Reads a frame in from the audiohook structure.
int ast_audiohook_remove (struct ast_channel *chan, struct ast_audiohook *audiohook)
 Remove an audiohook from a specified channel.
void ast_audiohook_trigger_wait (struct ast_audiohook *audiohook)
 Wait for audiohook trigger to be triggered.
int ast_audiohook_write_frame (struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
 Writes a frame into the audiohook structure.
ast_frameast_audiohook_write_list (struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
 Pass a frame off to be handled by the audiohook core.
static struct ast_frameaudio_audiohook_write_list (struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
 Pass an AUDIO frame off to be handled by the audiohook core.
static struct ast_frameaudiohook_read_frame_both (struct ast_audiohook *audiohook, size_t samples)
static struct ast_frameaudiohook_read_frame_single (struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction)
static struct ast_framedtmf_audiohook_write_list (struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame)
 Pass a DTMF frame off to be handled by the audiohook core.
static struct ast_audiohookfind_audiohook_by_source (struct ast_audiohook_list *audiohook_list, const char *source)


Detailed Description

Audiohooks Architecture.

Author:
Joshua Colp <jcolp@digium.com>

Definition in file audiohook.c.


Function Documentation

int ast_audiohook_attach ( struct ast_channel chan,
struct ast_audiohook audiohook 
)

Attach audiohook to channel.

Parameters:
chan Channel
audiohook Audiohook structure
Returns:
Returns 0 on success, -1 on failure

Definition at line 331 of file audiohook.c.

References AST_AUDIOHOOK_STATUS_RUNNING, AST_AUDIOHOOK_TYPE_MANIPULATE, AST_AUDIOHOOK_TYPE_SPY, AST_AUDIOHOOK_TYPE_WHISPER, ast_calloc, ast_channel_lock, ast_channel_unlock, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_INSERT_TAIL, ast_channel::audiohooks, ast_audiohook_list::manipulate_list, ast_audiohook_list::spy_list, ast_audiohook::status, ast_audiohook::type, and ast_audiohook_list::whisper_list.

Referenced by ast_audiohook_move_by_source(), start_spying(), and startmon().

00332 {
00333    ast_channel_lock(chan);
00334 
00335    if (!chan->audiohooks) {
00336       /* Whoops... allocate a new structure */
00337       if (!(chan->audiohooks = ast_calloc(1, sizeof(*chan->audiohooks)))) {
00338          ast_channel_unlock(chan);
00339          return -1;
00340       }
00341       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->spy_list);
00342       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->whisper_list);
00343       AST_LIST_HEAD_INIT_NOLOCK(&chan->audiohooks->manipulate_list);
00344    }
00345 
00346    /* Drop into respective list */
00347    if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
00348       AST_LIST_INSERT_TAIL(&chan->audiohooks->spy_list, audiohook, list);
00349    else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
00350       AST_LIST_INSERT_TAIL(&chan->audiohooks->whisper_list, audiohook, list);
00351    else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
00352       AST_LIST_INSERT_TAIL(&chan->audiohooks->manipulate_list, audiohook, list);
00353 
00354    /* Change status over to running since it is now attached */
00355    audiohook->status = AST_AUDIOHOOK_STATUS_RUNNING;
00356 
00357    ast_channel_unlock(chan);
00358 
00359    return 0;
00360 }

int ast_audiohook_destroy ( struct ast_audiohook audiohook  ) 

Destroys an audiohook structure.

Parameters:
audiohook Audiohook structure
Returns:
Returns 0 on success, -1 on failure

Definition at line 98 of file audiohook.c.

References AST_AUDIOHOOK_TYPE_SPY, AST_AUDIOHOOK_TYPE_WHISPER, ast_cond_destroy(), ast_mutex_destroy(), ast_slinfactory_destroy(), ast_translator_free_path(), ast_audiohook::lock, ast_audiohook::read_factory, ast_audiohook::trans_pvt, ast_audiohook::trigger, ast_audiohook::type, and ast_audiohook::write_factory.

Referenced by channel_spy(), launch_monitor_thread(), and mixmonitor_thread().

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 }

int ast_audiohook_detach ( struct ast_audiohook audiohook  ) 

Detach audiohook from channel.

Parameters:
audiohook Audiohook structure
Returns:
Returns 0 on success, -1 on failure

Definition at line 366 of file audiohook.c.

References AST_AUDIOHOOK_STATUS_DONE, AST_AUDIOHOOK_STATUS_SHUTDOWN, ast_audiohook_trigger_wait(), and ast_audiohook::status.

Referenced by channel_spy(), and mixmonitor_thread().

00367 {
00368    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
00369       return 0;
00370 
00371    audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
00372 
00373    while (audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
00374       ast_audiohook_trigger_wait(audiohook);
00375 
00376    return 0;
00377 }

int ast_audiohook_detach_list ( struct ast_audiohook_list audiohook_list  ) 

Detach audiohooks from list and destroy said list.

Parameters:
audiohook_list List of audiohooks
Returns:
Returns 0 on success, -1 on failure

Definition at line 383 of file audiohook.c.

References ast_audiohook_lock, AST_AUDIOHOOK_STATUS_DONE, ast_audiohook_unlock, ast_cond_signal(), ast_free, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_translator_free_path(), ast_audiohook_list::in_translate, ast_audiohook::manipulate_callback, ast_audiohook_list::manipulate_list, ast_audiohook_list::out_translate, ast_audiohook_list::spy_list, ast_audiohook::status, ast_audiohook_translate::trans_pvt, ast_audiohook::trigger, and ast_audiohook_list::whisper_list.

Referenced by ast_hangup().

00384 {
00385    int i = 0;
00386    struct ast_audiohook *audiohook = NULL;
00387 
00388    /* Drop any spies */
00389    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
00390       ast_audiohook_lock(audiohook);
00391       AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
00392       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00393       ast_cond_signal(&audiohook->trigger);
00394       ast_audiohook_unlock(audiohook);
00395    }
00396    AST_LIST_TRAVERSE_SAFE_END
00397 
00398    /* Drop any whispering sources */
00399    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
00400       ast_audiohook_lock(audiohook);
00401       AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
00402       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00403       ast_cond_signal(&audiohook->trigger);
00404       ast_audiohook_unlock(audiohook);
00405    }
00406    AST_LIST_TRAVERSE_SAFE_END
00407 
00408    /* Drop any manipulaters */
00409    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00410       ast_audiohook_lock(audiohook);
00411       AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
00412       audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00413       ast_audiohook_unlock(audiohook);
00414       audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00415    }
00416    AST_LIST_TRAVERSE_SAFE_END
00417 
00418    /* Drop translation paths if present */
00419    for (i = 0; i < 2; i++) {
00420       if (audiohook_list->in_translate[i].trans_pvt)
00421          ast_translator_free_path(audiohook_list->in_translate[i].trans_pvt);
00422       if (audiohook_list->out_translate[i].trans_pvt)
00423          ast_translator_free_path(audiohook_list->out_translate[i].trans_pvt);
00424    }
00425    
00426    /* Free ourselves */
00427    ast_free(audiohook_list);
00428 
00429    return 0;
00430 }

int ast_audiohook_detach_source ( struct ast_channel chan,
const char *  source 
)

Detach specified source audiohook from channel.

Parameters:
chan Channel to detach from
source Name of source to detach
Returns:
Returns 0 on success, -1 on failure

Definition at line 478 of file audiohook.c.

References AST_AUDIOHOOK_STATUS_DONE, AST_AUDIOHOOK_STATUS_SHUTDOWN, ast_channel_lock, ast_channel_unlock, ast_channel::audiohooks, find_audiohook_by_source(), and ast_audiohook::status.

Referenced by mixmonitor_cli(), and stop_mixmonitor_exec().

00479 {
00480    struct ast_audiohook *audiohook = NULL;
00481 
00482    ast_channel_lock(chan);
00483 
00484    /* Ensure the channel has audiohooks on it */
00485    if (!chan->audiohooks) {
00486       ast_channel_unlock(chan);
00487       return -1;
00488    }
00489 
00490    audiohook = find_audiohook_by_source(chan->audiohooks, source);
00491 
00492    ast_channel_unlock(chan);
00493 
00494    if (audiohook && audiohook->status != AST_AUDIOHOOK_STATUS_DONE)
00495       audiohook->status = AST_AUDIOHOOK_STATUS_SHUTDOWN;
00496 
00497    return (audiohook ? 0 : -1);
00498 }

int ast_audiohook_init ( struct ast_audiohook audiohook,
enum ast_audiohook_type  type,
const char *  source 
)

Initialize an audiohook structure.

Parameters:
audiohook Audiohook structure
type 
source 
Returns:
Returns 0 on success, -1 on failure

Definition at line 67 of file audiohook.c.

References AST_AUDIOHOOK_STATUS_NEW, AST_AUDIOHOOK_TYPE_SPY, AST_AUDIOHOOK_TYPE_WHISPER, ast_cond_init(), ast_mutex_init(), and ast_slinfactory_init().

Referenced by channel_spy(), and launch_monitor_thread().

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 }

void ast_audiohook_move_by_source ( struct ast_channel old_chan,
struct ast_channel new_chan,
const char *  source 
)

Move an audiohook from one channel to a new one.

Todo:
Currently only the first audiohook of a specific source found will be moved. We should add the capability to move multiple audiohooks from a single source as well.
Note:
It is required that both old_chan and new_chan are locked prior to calling this function. Besides needing to protect the data within the channels, not locking these channels can lead to a potential deadlock
Parameters:
old_chan The source of the audiohook to move
new_chan The destination to which we want the audiohook to move
source The source of the audiohook we want to move

Definition at line 454 of file audiohook.c.

References ast_audiohook_attach(), ast_audiohook_lock, ast_audiohook_remove(), ast_audiohook_unlock, ast_channel::audiohooks, and find_audiohook_by_source().

Referenced by audiohook_inheritance_fixup().

00455 {
00456    struct ast_audiohook *audiohook = find_audiohook_by_source(old_chan->audiohooks, source);
00457 
00458    if (!audiohook) {
00459       return;
00460    }
00461    
00462    /* By locking both channels and the audiohook, we can assure that
00463     * another thread will not have a chance to read the audiohook's status
00464     * as done, even though ast_audiohook_remove signals the trigger
00465     * condition
00466     */
00467    ast_audiohook_lock(audiohook);
00468    ast_audiohook_remove(old_chan, audiohook);
00469    ast_audiohook_attach(new_chan, audiohook);
00470    ast_audiohook_unlock(audiohook);
00471 }

struct ast_frame* ast_audiohook_read_frame ( struct ast_audiohook audiohook,
size_t  samples,
enum ast_audiohook_direction  direction,
int  format 
)

Reads a frame in from the audiohook structure.

Parameters:
audiohook Audiohook structure
samples Number of samples wanted
direction Direction the audio frame came from
format Format of frame remote side wants back
Returns:
Returns frame on success, NULL on failure

Definition at line 296 of file audiohook.c.

References AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR, ast_frfree, ast_translate(), ast_translator_build_path(), ast_translator_free_path(), audiohook_read_frame_both(), and audiohook_read_frame_single().

Referenced by mixmonitor_thread(), and spy_generate().

00297 {
00298    struct ast_frame *read_frame = NULL, *final_frame = NULL;
00299 
00300    if (!(read_frame = (direction == AST_AUDIOHOOK_DIRECTION_BOTH ? audiohook_read_frame_both(audiohook, samples) : audiohook_read_frame_single(audiohook, samples, direction))))
00301       return NULL;
00302 
00303    /* If they don't want signed linear back out, we'll have to send it through the translation path */
00304    if (format != AST_FORMAT_SLINEAR) {
00305       /* Rebuild translation path if different format then previously */
00306       if (audiohook->format != format) {
00307          if (audiohook->trans_pvt) {
00308             ast_translator_free_path(audiohook->trans_pvt);
00309             audiohook->trans_pvt = NULL;
00310          }
00311          /* 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 */
00312          if (!(audiohook->trans_pvt = ast_translator_build_path(format, AST_FORMAT_SLINEAR))) {
00313             ast_frfree(read_frame);
00314             return NULL;
00315          }
00316       }
00317       /* Convert to requested format, and allow the read in frame to be freed */
00318       final_frame = ast_translate(audiohook->trans_pvt, read_frame, 1);
00319    } else {
00320       final_frame = read_frame;
00321    }
00322 
00323    return final_frame;
00324 }

int ast_audiohook_remove ( struct ast_channel chan,
struct ast_audiohook audiohook 
)

Remove an audiohook from a specified channel.

Parameters:
chan Channel to remove from
audiohook Audiohook to remove
Returns:
Returns 0 on success, -1 on failure
Note:
The channel does not need to be locked before calling this function

Definition at line 510 of file audiohook.c.

References ast_audiohook_lock, AST_AUDIOHOOK_STATUS_DONE, AST_AUDIOHOOK_TYPE_MANIPULATE, AST_AUDIOHOOK_TYPE_SPY, AST_AUDIOHOOK_TYPE_WHISPER, ast_audiohook_unlock, ast_channel_lock, ast_channel_unlock, ast_cond_signal(), AST_LIST_REMOVE, ast_channel::audiohooks, ast_audiohook_list::manipulate_list, ast_audiohook_list::spy_list, ast_audiohook::status, ast_audiohook::trigger, ast_audiohook::type, and ast_audiohook_list::whisper_list.

Referenced by ast_audiohook_move_by_source().

00511 {
00512    ast_channel_lock(chan);
00513 
00514    if (!chan->audiohooks) {
00515       ast_channel_unlock(chan);
00516       return -1;
00517    }
00518 
00519    if (audiohook->type == AST_AUDIOHOOK_TYPE_SPY)
00520       AST_LIST_REMOVE(&chan->audiohooks->spy_list, audiohook, list);
00521    else if (audiohook->type == AST_AUDIOHOOK_TYPE_WHISPER)
00522       AST_LIST_REMOVE(&chan->audiohooks->whisper_list, audiohook, list);
00523    else if (audiohook->type == AST_AUDIOHOOK_TYPE_MANIPULATE)
00524       AST_LIST_REMOVE(&chan->audiohooks->manipulate_list, audiohook, list);
00525 
00526    ast_audiohook_lock(audiohook);
00527    audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00528    ast_cond_signal(&audiohook->trigger);
00529    ast_audiohook_unlock(audiohook);
00530 
00531    ast_channel_unlock(chan);
00532 
00533    return 0;
00534 }

void ast_audiohook_trigger_wait ( struct ast_audiohook audiohook  ) 

Wait for audiohook trigger to be triggered.

Parameters:
audiohook Audiohook to wait on

Definition at line 709 of file audiohook.c.

References ast_cond_timedwait(), ast_samp2tv(), ast_tvadd(), ast_tvnow(), ast_audiohook::lock, and ast_audiohook::trigger.

Referenced by ast_audiohook_detach(), and mixmonitor_thread().

00710 {
00711    struct timeval tv;
00712    struct timespec ts;
00713 
00714    tv = ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
00715    ts.tv_sec = tv.tv_sec;
00716    ts.tv_nsec = tv.tv_usec * 1000;
00717    
00718    ast_cond_timedwait(&audiohook->trigger, &audiohook->lock, &ts);
00719    
00720    return;
00721 }

int ast_audiohook_write_frame ( struct ast_audiohook audiohook,
enum ast_audiohook_direction  direction,
struct ast_frame frame 
)

Writes a frame into the audiohook structure.

Parameters:
audiohook Audiohook structure
direction Direction the audio frame came from
frame Frame to write in
Returns:
Returns 0 on success, -1 on failure

Definition at line 128 of file audiohook.c.

References AST_AUDIOHOOK_DIRECTION_READ, AST_AUDIOHOOK_DIRECTION_WRITE, AST_AUDIOHOOK_SYNC_TOLERANCE, AST_AUDIOHOOK_TRIGGER_MODE, AST_AUDIOHOOK_TRIGGER_READ, AST_AUDIOHOOK_TRIGGER_SYNC, AST_AUDIOHOOK_TRIGGER_WRITE, ast_cond_signal(), ast_log(), ast_slinfactory_available(), ast_slinfactory_feed(), ast_slinfactory_flush(), ast_test_flag, ast_tvdiff_ms(), ast_tvnow(), LOG_DEBUG, option_debug, ast_audiohook::read_factory, ast_audiohook::read_time, ast_audiohook::trigger, ast_audiohook::write_factory, and ast_audiohook::write_time.

Referenced by audio_audiohook_write_list(), and channel_spy().

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    int our_factory_ms;
00134    int other_factory_samples;
00135    int other_factory_ms;
00136 
00137    /* Update last feeding time to be current */
00138    *time = ast_tvnow();
00139 
00140    our_factory_ms = ast_tvdiff_ms(*time, previous_time) + (ast_slinfactory_available(factory) / 8);
00141    other_factory_samples = ast_slinfactory_available(other_factory);
00142    other_factory_ms = other_factory_samples / 8;
00143 
00144    /* 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 */
00145    if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC) && other_factory_samples && (our_factory_ms - other_factory_ms > AST_AUDIOHOOK_SYNC_TOLERANCE)) {
00146       if (option_debug)
00147          ast_log(LOG_DEBUG, "Flushing audiohook %p so it remains in sync\n", audiohook);
00148       ast_slinfactory_flush(factory);
00149       ast_slinfactory_flush(other_factory);
00150    }
00151 
00152    /* Write frame out to respective factory */
00153    ast_slinfactory_feed(factory, frame);
00154 
00155    /* If we need to notify the respective handler of this audiohook, do so */
00156    if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_READ) && (direction == AST_AUDIOHOOK_DIRECTION_READ)) {
00157       ast_cond_signal(&audiohook->trigger);
00158    } else if ((ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE) == AST_AUDIOHOOK_TRIGGER_WRITE) && (direction == AST_AUDIOHOOK_DIRECTION_WRITE)) {
00159       ast_cond_signal(&audiohook->trigger);
00160    } else if (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC)) {
00161       ast_cond_signal(&audiohook->trigger);
00162    }
00163 
00164    return 0;
00165 }

struct ast_frame* ast_audiohook_write_list ( struct ast_channel chan,
struct ast_audiohook_list audiohook_list,
enum ast_audiohook_direction  direction,
struct ast_frame frame 
)

Pass a frame off to be handled by the audiohook core.

Parameters:
chan Channel that the list is coming off of
audiohook_list List of audiohooks
direction Direction frame is coming in from
frame The frame itself
Returns:
Return frame on success, NULL on failure

Definition at line 694 of file audiohook.c.

References AST_FRAME_DTMF, AST_FRAME_VOICE, audio_audiohook_write_list(), dtmf_audiohook_write_list(), and ast_frame::frametype.

Referenced by __ast_read(), and ast_write().

00695 {
00696    /* Pass off frame to it's respective list write function */
00697    if (frame->frametype == AST_FRAME_VOICE)
00698       return audio_audiohook_write_list(chan, audiohook_list, direction, frame);
00699    else if (frame->frametype == AST_FRAME_DTMF)
00700       return dtmf_audiohook_write_list(chan, audiohook_list, direction, frame);
00701    else
00702       return frame;
00703 }

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 
) [static]

Pass an AUDIO frame off to be handled by the audiohook core.

Parameters:
chan Channel that the list is coming off of
audiohook_list List of audiohooks
direction Direction frame is coming in from
frame The frame itself
Returns:
Return frame on success, NULL on failure

Definition at line 572 of file audiohook.c.

References AST_AUDIOHOOK_DIRECTION_READ, AST_AUDIOHOOK_DIRECTION_WRITE, ast_audiohook_lock, AST_AUDIOHOOK_STATUS_DONE, AST_AUDIOHOOK_STATUS_RUNNING, ast_audiohook_unlock, ast_audiohook_write_frame(), ast_cond_signal(), AST_FORMAT_SLINEAR, ast_frfree, AST_LIST_EMPTY, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_slinear_saturated_add(), ast_slinfactory_available(), ast_slinfactory_read(), ast_translate(), ast_translator_build_path(), ast_translator_free_path(), ast_audiohook_translate::format, ast_audiohook_list::in_translate, ast_audiohook::manipulate_callback, ast_audiohook_list::manipulate_list, ast_audiohook_list::out_translate, ast_frame::samples, ast_audiohook_list::spy_list, ast_audiohook::status, ast_frame::subclass, ast_audiohook_translate::trans_pvt, ast_audiohook::trigger, ast_audiohook_list::whisper_list, and ast_audiohook::write_factory.

Referenced by ast_audiohook_write_list().

00573 {
00574    struct ast_audiohook_translate *in_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->in_translate[0] : &audiohook_list->in_translate[1]);
00575    struct ast_audiohook_translate *out_translate = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook_list->out_translate[0] : &audiohook_list->out_translate[1]);
00576    struct ast_frame *start_frame = frame, *middle_frame = frame, *end_frame = frame;
00577    struct ast_audiohook *audiohook = NULL;
00578    int samples = frame->samples;
00579    
00580    /* If the frame coming in is not signed linear we have to send it through the in_translate path */
00581    if (frame->subclass != AST_FORMAT_SLINEAR) {
00582       if (in_translate->format != frame->subclass) {
00583          if (in_translate->trans_pvt)
00584             ast_translator_free_path(in_translate->trans_pvt);
00585          if (!(in_translate->trans_pvt = ast_translator_build_path(AST_FORMAT_SLINEAR, frame->subclass)))
00586             return frame;
00587          in_translate->format = frame->subclass;
00588       }
00589       if (!(middle_frame = ast_translate(in_translate->trans_pvt, frame, 0)))
00590          return frame;
00591    }
00592 
00593    /* Queue up signed linear frame to each spy */
00594    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->spy_list, audiohook, list) {
00595       ast_audiohook_lock(audiohook);
00596       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00597          AST_LIST_REMOVE_CURRENT(&audiohook_list->spy_list, list);
00598          audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00599          ast_cond_signal(&audiohook->trigger);
00600          ast_audiohook_unlock(audiohook);
00601          continue;
00602       }
00603       ast_audiohook_write_frame(audiohook, direction, middle_frame);
00604       ast_audiohook_unlock(audiohook);
00605    }
00606    AST_LIST_TRAVERSE_SAFE_END
00607 
00608    /* If this frame is being written out to the channel then we need to use whisper sources */
00609    if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
00610       int i = 0;
00611       short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
00612       memset(&combine_buf, 0, sizeof(combine_buf));
00613       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
00614          ast_audiohook_lock(audiohook);
00615          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00616             AST_LIST_REMOVE_CURRENT(&audiohook_list->whisper_list, list);
00617             audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00618             ast_cond_signal(&audiohook->trigger);
00619             ast_audiohook_unlock(audiohook);
00620             continue;
00621          }
00622          if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
00623             /* Take audio from this whisper source and combine it into our main buffer */
00624             for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
00625                ast_slinear_saturated_add(data1, data2);
00626          }
00627          ast_audiohook_unlock(audiohook);
00628       }
00629       AST_LIST_TRAVERSE_SAFE_END
00630       /* We take all of the combined whisper sources and combine them into the audio being written out */
00631       for (i = 0, data1 = middle_frame->data, data2 = combine_buf; i < samples; i++, data1++, data2++)
00632          ast_slinear_saturated_add(data1, data2);
00633       end_frame = middle_frame;
00634    }
00635 
00636    /* Pass off frame to manipulate audiohooks */
00637    if (!AST_LIST_EMPTY(&audiohook_list->manipulate_list)) {
00638       AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00639          ast_audiohook_lock(audiohook);
00640          if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00641             AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
00642             audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00643             ast_audiohook_unlock(audiohook);
00644             /* We basically drop all of our links to the manipulate audiohook and prod it to do it's own destructive things */
00645             audiohook->manipulate_callback(audiohook, chan, NULL, direction);
00646             continue;
00647          }
00648          /* Feed in frame to manipulation */
00649          audiohook->manipulate_callback(audiohook, chan, middle_frame, direction);
00650          ast_audiohook_unlock(audiohook);
00651       }
00652       AST_LIST_TRAVERSE_SAFE_END
00653       end_frame = middle_frame;
00654    }
00655 
00656    /* Now we figure out what to do with our end frame (whether to transcode or not) */
00657    if (middle_frame == end_frame) {
00658       /* Middle frame was modified and became the end frame... let's see if we need to transcode */
00659       if (end_frame->subclass != start_frame->subclass) {
00660          if (out_translate->format != start_frame->subclass) {
00661             if (out_translate->trans_pvt)
00662                ast_translator_free_path(out_translate->trans_pvt);
00663             if (!(out_translate->trans_pvt = ast_translator_build_path(start_frame->subclass, AST_FORMAT_SLINEAR))) {
00664                /* We can't transcode this... drop our middle frame and return the original */
00665                ast_frfree(middle_frame);
00666                return start_frame;
00667             }
00668             out_translate->format = start_frame->subclass;
00669          }
00670          /* Transcode from our middle (signed linear) frame to new format of the frame that came in */
00671          if (!(end_frame = ast_translate(out_translate->trans_pvt, middle_frame, 0))) {
00672             /* Failed to transcode the frame... drop it and return the original */
00673             ast_frfree(middle_frame);
00674             return start_frame;
00675          }
00676          /* Here's the scoop... middle frame is no longer of use to us */
00677          ast_frfree(middle_frame);
00678       }
00679    } else {
00680       /* No frame was modified, we can just drop our middle frame and pass the frame we got in out */
00681       ast_frfree(middle_frame);
00682    }
00683 
00684    return end_frame;
00685 }

static struct ast_frame* audiohook_read_frame_both ( struct ast_audiohook audiohook,
size_t  samples 
) [static]

Definition at line 195 of file audiohook.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_log(), ast_slinear_saturated_divide(), ast_slinear_saturated_multiply(), ast_slinfactory_available(), ast_slinfactory_read(), ast_tvdiff_ms(), ast_tvnow(), ast_frame::frametype, LOG_DEBUG, option_debug, ast_audiohook::options, ast_audiohook::read_factory, ast_audiohook::read_time, ast_audiohook_options::read_volume, ast_audiohook::write_factory, ast_audiohook::write_time, and ast_audiohook_options::write_volume.

Referenced by ast_audiohook_read_frame().

00196 {
00197    int i = 0, usable_read, usable_write;
00198    short buf1[samples], buf2[samples], *read_buf = NULL, *write_buf = NULL, *final_buf = NULL, *data1 = NULL, *data2 = NULL;
00199    struct ast_frame frame = {
00200       .frametype = AST_FRAME_VOICE,
00201       .subclass = AST_FORMAT_SLINEAR,
00202       .data = NULL,
00203       .datalen = sizeof(buf1),
00204       .samples = samples,
00205    };
00206 
00207    /* Make sure both factories have the required samples */
00208    usable_read = (ast_slinfactory_available(&audiohook->read_factory) >= samples ? 1 : 0);
00209    usable_write = (ast_slinfactory_available(&audiohook->write_factory) >= samples ? 1 : 0);
00210 
00211    if (!usable_read && !usable_write) {
00212       /* If both factories are unusable bail out */
00213       if (option_debug)
00214          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);
00215       return NULL;
00216    }
00217 
00218    /* If we want to provide only a read factory make sure we aren't waiting for other audio */
00219    if (usable_read && !usable_write && (ast_tvdiff_ms(ast_tvnow(), audiohook->write_time) < (samples/8)*2)) {
00220       if (option_debug > 2)
00221          ast_log(LOG_DEBUG, "Write factory %p was pretty quick last time, waiting for them.\n", &audiohook->write_factory);
00222       return NULL;
00223    }
00224 
00225    /* If we want to provide only a write factory make sure we aren't waiting for other audio */
00226    if (usable_write && !usable_read && (ast_tvdiff_ms(ast_tvnow(), audiohook->read_time) < (samples/8)*2)) {
00227       if (option_debug > 2)
00228          ast_log(LOG_DEBUG, "Read factory %p was pretty quick last time, waiting for them.\n", &audiohook->read_factory);
00229       return NULL;
00230    }
00231 
00232    /* Start with the read factory... if there are enough samples, read them in */
00233    if (usable_read) {
00234       if (ast_slinfactory_read(&audiohook->read_factory, buf1, samples)) {
00235          read_buf = buf1;
00236          /* Adjust read volume if need be */
00237          if (audiohook->options.read_volume) {
00238             int count = 0;
00239             short adjust_value = abs(audiohook->options.read_volume);
00240             for (count = 0; count < samples; count++) {
00241                if (audiohook->options.read_volume > 0)
00242                   ast_slinear_saturated_multiply(&buf1[count], &adjust_value);
00243                else if (audiohook->options.read_volume < 0)
00244                   ast_slinear_saturated_divide(&buf1[count], &adjust_value);
00245             }
00246          }
00247       }
00248    } else if (option_debug)
00249       ast_log(LOG_DEBUG, "Failed to get %zd samples from read factory %p\n", samples, &audiohook->read_factory);
00250 
00251    /* Move on to the write factory... if there are enough samples, read them in */
00252    if (usable_write) {
00253       if (ast_slinfactory_read(&audiohook->write_factory, buf2, samples)) {
00254          write_buf = buf2;
00255          /* Adjust write volume if need be */
00256          if (audiohook->options.write_volume) {
00257             int count = 0;
00258             short adjust_value = abs(audiohook->options.write_volume);
00259             for (count = 0; count < samples; count++) {
00260                if (audiohook->options.write_volume > 0)
00261                   ast_slinear_saturated_multiply(&buf2[count], &adjust_value);
00262                else if (audiohook->options.write_volume < 0)
00263                   ast_slinear_saturated_divide(&buf2[count], &adjust_value);
00264             }
00265          }
00266       }
00267    } else if (option_debug)
00268       ast_log(LOG_DEBUG, "Failed to get %zd samples from write factory %p\n", samples, &audiohook->write_factory);
00269 
00270    /* Basically we figure out which buffer to use... and if mixing can be done here */
00271    if (!read_buf && !write_buf)
00272       return NULL;
00273    else if (read_buf && write_buf) {
00274       for (i = 0, data1 = read_buf, data2 = write_buf; i < samples; i++, data1++, data2++)
00275          ast_slinear_saturated_add(data1, data2);
00276       final_buf = buf1;
00277    } else if (read_buf)
00278       final_buf = buf1;
00279    else if (write_buf)
00280       final_buf = buf2;
00281 
00282    /* Make the final buffer part of the frame, so it gets duplicated fine */
00283    frame.data = final_buf;
00284 
00285    /* Yahoo, a combined copy of the audio! */
00286    return ast_frdup(&frame);
00287 }

static struct ast_frame* audiohook_read_frame_single ( struct ast_audiohook audiohook,
size_t  samples,
enum ast_audiohook_direction  direction 
) [static]

Definition at line 167 of file audiohook.c.

References AST_AUDIOHOOK_DIRECTION_READ, AST_FORMAT_SLINEAR, ast_frame_adjust_volume(), AST_FRAME_VOICE, ast_frdup(), ast_slinfactory_available(), ast_slinfactory_read(), ast_frame::frametype, ast_audiohook::options, ast_audiohook::read_factory, ast_audiohook_options::read_volume, ast_audiohook::write_factory, and ast_audiohook_options::write_volume.

Referenced by ast_audiohook_read_frame().

00168 {
00169    struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
00170    int vol = (direction == AST_AUDIOHOOK_DIRECTION_READ ? audiohook->options.read_volume : audiohook->options.write_volume);
00171    short buf[samples];
00172    struct ast_frame frame = {
00173       .frametype = AST_FRAME_VOICE,
00174       .subclass = AST_FORMAT_SLINEAR,
00175       .data = buf,
00176       .datalen = sizeof(buf),
00177       .samples = samples,
00178    };
00179 
00180    /* Ensure the factory is able to give us the samples we want */
00181    if (samples > ast_slinfactory_available(factory))
00182       return NULL;
00183    
00184    /* Read data in from factory */
00185    if (!ast_slinfactory_read(factory, buf, samples))
00186       return NULL;
00187 
00188    /* If a volume adjustment needs to be applied apply it */
00189    if (vol)
00190       ast_frame_adjust_volume(&frame, vol);
00191 
00192    return ast_frdup(&frame);
00193 }

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 
) [static]

Pass a DTMF frame off to be handled by the audiohook core.

Parameters:
chan Channel that the list is coming off of
audiohook_list List of audiohooks
direction Direction frame is coming in from
frame The frame itself
Returns:
Return frame on success, NULL on failure

Definition at line 543 of file audiohook.c.

References ast_audiohook_lock, AST_AUDIOHOOK_STATUS_DONE, AST_AUDIOHOOK_STATUS_RUNNING, ast_audiohook_unlock, AST_AUDIOHOOK_WANTS_DTMF, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_test_flag, ast_audiohook::manipulate_callback, ast_audiohook_list::manipulate_list, and ast_audiohook::status.

Referenced by ast_audiohook_write_list().

00544 {
00545    struct ast_audiohook *audiohook = NULL;
00546 
00547    AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->manipulate_list, audiohook, list) {
00548       ast_audiohook_lock(audiohook);
00549       if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
00550          AST_LIST_REMOVE_CURRENT(&audiohook_list->manipulate_list, list);
00551          audiohook->status = AST_AUDIOHOOK_STATUS_DONE;
00552          ast_audiohook_unlock(audiohook);
00553          audiohook->manipulate_callback(audiohook, NULL, NULL, 0);
00554          continue;
00555       }
00556       if (ast_test_flag(audiohook, AST_AUDIOHOOK_WANTS_DTMF))
00557          audiohook->manipulate_callback(audiohook, chan, frame, direction);
00558       ast_audiohook_unlock(audiohook);
00559    }
00560    AST_LIST_TRAVERSE_SAFE_END
00561 
00562    return frame;
00563 }

static struct ast_audiohook* find_audiohook_by_source ( struct ast_audiohook_list audiohook_list,
const char *  source 
) [static]

Definition at line 432 of file audiohook.c.

References AST_LIST_TRAVERSE, ast_audiohook_list::manipulate_list, ast_audiohook::source, ast_audiohook_list::spy_list, and ast_audiohook_list::whisper_list.

Referenced by ast_audiohook_detach_source(), and ast_audiohook_move_by_source().

00433 {
00434    struct ast_audiohook *audiohook = NULL;
00435 
00436    AST_LIST_TRAVERSE(&audiohook_list->spy_list, audiohook, list) {
00437       if (!strcasecmp(audiohook->source, source))
00438          return audiohook;
00439    }
00440 
00441    AST_LIST_TRAVERSE(&audiohook_list->whisper_list, audiohook, list) {
00442       if (!strcasecmp(audiohook->source, source))
00443          return audiohook;
00444    }
00445 
00446    AST_LIST_TRAVERSE(&audiohook_list->manipulate_list, audiohook, list) {
00447       if (!strcasecmp(audiohook->source, source))
00448          return audiohook;
00449    }
00450 
00451    return NULL;
00452 }


Generated on Wed Mar 4 19:58:24 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7