Sat Aug 6 00:39:22 2011

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

Generated on Sat Aug 6 00:39:22 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7