Thu Jul 9 13:40:23 2009

Asterisk developer's documentation


audiohook.h

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  * \brief Audiohooks Architecture
00021  */
00022 
00023 #ifndef _ASTERISK_AUDIOHOOK_H
00024 #define _ASTERISK_AUDIOHOOK_H
00025 
00026 #if defined(__cplusplus) || defined(c_plusplus)
00027 extern "C" {
00028 #endif
00029 
00030 /* these two are used in struct ast_audiohook */
00031 #include "asterisk/lock.h"
00032 #include "asterisk/linkedlists.h"
00033 
00034 #include "asterisk/slinfactory.h"
00035 
00036 enum ast_audiohook_type {
00037    AST_AUDIOHOOK_TYPE_SPY = 0,    /*!< Audiohook wants to receive audio */
00038    AST_AUDIOHOOK_TYPE_WHISPER,    /*!< Audiohook wants to provide audio to be mixed with existing audio */
00039    AST_AUDIOHOOK_TYPE_MANIPULATE, /*!< Audiohook wants to manipulate the audio */
00040 };
00041 
00042 enum ast_audiohook_status {
00043    AST_AUDIOHOOK_STATUS_NEW = 0,  /*!< Audiohook was just created, not in use yet */
00044    AST_AUDIOHOOK_STATUS_RUNNING,  /*!< Audiohook is running on a channel */
00045    AST_AUDIOHOOK_STATUS_SHUTDOWN, /*!< Audiohook is being shutdown */
00046    AST_AUDIOHOOK_STATUS_DONE,     /*!< Audiohook has shutdown and is not running on a channel any longer */
00047 };
00048 
00049 enum ast_audiohook_direction {
00050    AST_AUDIOHOOK_DIRECTION_READ = 0, /*!< Reading audio in */
00051    AST_AUDIOHOOK_DIRECTION_WRITE,    /*!< Writing audio out */
00052    AST_AUDIOHOOK_DIRECTION_BOTH,     /*!< Both reading audio in and writing audio out */
00053 };
00054 
00055 enum ast_audiohook_flags {
00056    AST_AUDIOHOOK_TRIGGER_MODE = (3 << 0),  /*!< When audiohook should be triggered to do something */
00057    AST_AUDIOHOOK_TRIGGER_READ = (1 << 0),  /*!< Audiohook wants to be triggered when reading audio in */
00058    AST_AUDIOHOOK_TRIGGER_WRITE = (2 << 0), /*!< Audiohook wants to be triggered when writing audio out */
00059    AST_AUDIOHOOK_WANTS_DTMF = (1 << 1),    /*!< Audiohook also wants to receive DTMF frames */
00060    AST_AUDIOHOOK_TRIGGER_SYNC = (1 << 2),  /*!< Audiohook wants to be triggered when both sides have combined audio available */
00061    /*! Audiohooks with this flag set will not allow for a large amount of samples to build up on its
00062     * slinfactories. We will flush the factories if they contain too many samples.
00063     */
00064    AST_AUDIOHOOK_SMALL_QUEUE = (1 << 3),
00065 };
00066 
00067 #define AST_AUDIOHOOK_SYNC_TOLERANCE 100 /*< Tolerance in milliseconds for audiohooks synchronization */
00068 
00069 struct ast_audiohook;
00070 
00071 /*! \brief Callback function for manipulate audiohook type
00072  * \param audiohook Audiohook structure
00073  * \param chan Channel
00074  * \param frame Frame of audio to manipulate
00075  * \param direction Direction frame came from
00076  * \return Returns 0 on success, -1 on failure
00077  * \note An audiohook does not have any reference to a private data structure for manipulate types. It is up to the manipulate callback to store this data
00078  *       via it's own method. An example would be datastores.
00079  */
00080 typedef int (*ast_audiohook_manipulate_callback)(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction);
00081 
00082 struct ast_audiohook_options {
00083    int read_volume;  /*!< Volume adjustment on frames read from the channel the hook is on */
00084    int write_volume; /*!< Volume adjustment on frames written to the channel the hook is on */
00085 };
00086 
00087 struct ast_audiohook {
00088    ast_mutex_t lock;                                      /*!< Lock that protects the audiohook structure */
00089    ast_cond_t trigger;                                    /*!< Trigger condition (if enabled) */
00090    enum ast_audiohook_type type;                          /*!< Type of audiohook */
00091    enum ast_audiohook_status status;                      /*!< Status of the audiohook */
00092    const char *source;                                    /*!< Who this audiohook ultimately belongs to */
00093    unsigned int flags;                                    /*!< Flags on the audiohook */
00094    struct ast_slinfactory read_factory;                   /*!< Factory where frames read from the channel, or read from the whisper source will go through */
00095    struct ast_slinfactory write_factory;                  /*!< Factory where frames written to the channel will go through */
00096    struct timeval read_time;                              /*!< Last time read factory was fed */
00097    struct timeval write_time;                             /*!< Last time write factory was fed */
00098    int format;                                            /*!< Format translation path is setup as */
00099    struct ast_trans_pvt *trans_pvt;                       /*!< Translation path for reading frames */
00100    ast_audiohook_manipulate_callback manipulate_callback; /*!< Manipulation callback */
00101    struct ast_audiohook_options options;                  /*!< Applicable options */
00102    AST_LIST_ENTRY(ast_audiohook) list;                    /*!< Linked list information */
00103 };
00104 
00105 struct ast_audiohook_list;
00106 
00107 /*! \brief Initialize an audiohook structure
00108  * \param audiohook Audiohook structure
00109  * \param type Type of audiohook to initialize this as
00110  * \param source Who is initializing this audiohook
00111  * \return Returns 0 on success, -1 on failure
00112  */
00113 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source);
00114 
00115 /*! \brief Destroys an audiohook structure
00116  * \param audiohook Audiohook structure
00117  * \return Returns 0 on success, -1 on failure
00118  */
00119 int ast_audiohook_destroy(struct ast_audiohook *audiohook);
00120 
00121 /*! \brief Writes a frame into the audiohook structure
00122  * \param audiohook Audiohook structure
00123  * \param direction Direction the audio frame came from
00124  * \param frame Frame to write in
00125  * \return Returns 0 on success, -1 on failure
00126  */
00127 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame);
00128 
00129 /*! \brief Reads a frame in from the audiohook structure
00130  * \param audiohook Audiohook structure
00131  * \param samples Number of samples wanted
00132  * \param direction Direction the audio frame came from
00133  * \param format Format of frame remote side wants back
00134  * \return Returns frame on success, NULL on failure
00135  */
00136 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format);
00137 
00138 /*! \brief Attach audiohook to channel
00139  * \param chan Channel
00140  * \param audiohook Audiohook structure
00141  * \return Returns 0 on success, -1 on failure
00142  */
00143 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook);
00144 
00145 /*! \brief Detach audiohook from channel
00146  * \param audiohook Audiohook structure
00147  * \return Returns 0 on success, -1 on failure
00148  */
00149 int ast_audiohook_detach(struct ast_audiohook *audiohook);
00150 
00151 /*! \brief Detach audiohooks from list and destroy said list
00152  * \param audiohook_list List of audiohooks
00153  * \return Returns 0 on success, -1 on failure
00154  */
00155 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list);
00156 
00157 /*! \brief Move an audiohook from one channel to a new one
00158  *
00159  * \todo Currently only the first audiohook of a specific source found will be moved.
00160  * We should add the capability to move multiple audiohooks from a single source as well.
00161  *
00162  * \note It is required that both old_chan and new_chan are locked prior to calling
00163  * this function. Besides needing to protect the data within the channels, not locking
00164  * these channels can lead to a potential deadlock
00165  *
00166  * \param old_chan The source of the audiohook to move
00167  * \param new_chan The destination to which we want the audiohook to move
00168  * \param source The source of the audiohook we want to move
00169  */
00170 void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source);
00171 
00172 /*! 
00173  * \brief Detach specified source audiohook from channel
00174  *
00175  * \param chan Channel to detach from
00176  * \param source Name of source to detach
00177  *
00178  * \return Returns 0 on success, -1 on failure
00179  *
00180  * \note The channel does not need to be locked before calling this function.
00181  */
00182 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source);
00183 
00184 /*!
00185  * \brief Remove an audiohook from a specified channel
00186  *
00187  * \param chan Channel to remove from
00188  * \param audiohook Audiohook to remove
00189  *
00190  * \return Returns 0 on success, -1 on failure
00191  *
00192  * \note The channel does not need to be locked before calling this function
00193  */
00194 int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook);
00195 
00196 /*! \brief Pass a frame off to be handled by the audiohook core
00197  * \param chan Channel that the list is coming off of
00198  * \param audiohook_list List of audiohooks
00199  * \param direction Direction frame is coming in from
00200  * \param frame The frame itself
00201  * \return Return frame on success, NULL on failure
00202  */
00203 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);
00204 
00205 /*! \brief Wait for audiohook trigger to be triggered
00206  * \param audiohook Audiohook to wait on
00207  */
00208 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook);
00209 
00210 /*!
00211   \brief Find out how many audiohooks from  a certain source exist on a given channel, regardless of status.
00212   \param chan The channel on which to find the spies 
00213   \param source The audiohook's source
00214   \param type The type of audiohook 
00215   \return Return the number of audiohooks which are from the source specified
00216 
00217   Note: Function performs nlocking.
00218 */
00219 int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type);
00220 
00221 /*!
00222   \brief Find out how many spies of a certain type exist on a given channel, and are in state running.
00223   \param chan The channel on which to find the spies
00224   \param source The source of the audiohook
00225   \param type The type of spy to look for
00226   \return Return the number of running audiohooks which are from the source specified
00227 
00228   Note: Function performs no locking.
00229 */
00230 int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type);
00231 
00232 /*! \brief Lock an audiohook
00233  * \param ah Audiohook structure
00234  */
00235 #define ast_audiohook_lock(ah) ast_mutex_lock(&(ah)->lock)
00236 
00237 /*! \brief Unlock an audiohook
00238  * \param ah Audiohook structure
00239  */
00240 #define ast_audiohook_unlock(ah) ast_mutex_unlock(&(ah)->lock)
00241 
00242 #if defined(__cplusplus) || defined(c_plusplus)
00243 }
00244 #endif
00245 
00246 #endif /* _ASTERISK_AUDIOHOOK_H */

Generated on Thu Jul 9 13:40:23 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7