2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2007, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 * \brief Audiohooks Architecture
23 #ifndef _ASTERISK_AUDIOHOOK_H
24 #define _ASTERISK_AUDIOHOOK_H
26 #if defined(__cplusplus) || defined(c_plusplus)
30 #include "asterisk/slinfactory.h"
32 enum ast_audiohook_type {
33 AST_AUDIOHOOK_TYPE_SPY = 0, /*!< Audiohook wants to receive audio */
34 AST_AUDIOHOOK_TYPE_WHISPER, /*!< Audiohook wants to provide audio to be mixed with existing audio */
35 AST_AUDIOHOOK_TYPE_MANIPULATE, /*!< Audiohook wants to manipulate the audio */
38 enum ast_audiohook_status {
39 AST_AUDIOHOOK_STATUS_NEW = 0, /*!< Audiohook was just created, not in use yet */
40 AST_AUDIOHOOK_STATUS_RUNNING, /*!< Audiohook is running on a channel */
41 AST_AUDIOHOOK_STATUS_SHUTDOWN, /*!< Audiohook is being shutdown */
42 AST_AUDIOHOOK_STATUS_DONE, /*!< Audiohook has shutdown and is not running on a channel any longer */
45 enum ast_audiohook_direction {
46 AST_AUDIOHOOK_DIRECTION_READ = 0, /*!< Reading audio in */
47 AST_AUDIOHOOK_DIRECTION_WRITE, /*!< Writing audio out */
48 AST_AUDIOHOOK_DIRECTION_BOTH, /*!< Both reading audio in and writing audio out */
51 enum ast_audiohook_flags {
52 AST_AUDIOHOOK_TRIGGER_MODE = (3 << 0), /*!< When audiohook should be triggered to do something */
53 AST_AUDIOHOOK_TRIGGER_READ = (1 << 0), /*!< Audiohook wants to be triggered when reading audio in */
54 AST_AUDIOHOOK_TRIGGER_WRITE = (2 << 0), /*!< Audiohook wants to be triggered when writing audio out */
55 AST_AUDIOHOOK_WANTS_DTMF = (1 << 1), /*!< Audiohook also wants to receive DTMF frames */
60 /*! \brief Callback function for manipulate audiohook type
61 * \param audiohook Audiohook structure
63 * \param frame Frame of audio to manipulate
64 * \param direction Direction frame came from
65 * \return Returns 0 on success, -1 on failure
66 * \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
67 * via it's own method. An example would be datastores.
69 typedef int (*ast_audiohook_manipulate_callback)(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction);
71 struct ast_audiohook_options {
72 int read_volume; /*!< Volume adjustment on frames read from the channel the hook is on */
73 int write_volume; /*!< Volume adjustment on frames written to the channel the hook is on */
76 struct ast_audiohook {
77 ast_mutex_t lock; /*!< Lock that protects the audiohook structure */
78 ast_cond_t trigger; /*!< Trigger condition (if enabled) */
79 enum ast_audiohook_type type; /*!< Type of audiohook */
80 enum ast_audiohook_status status; /*!< Status of the audiohook */
81 const char *source; /*!< Who this audiohook ultimately belongs to */
82 unsigned int flags; /*!< Flags on the audiohook */
83 struct ast_slinfactory read_factory; /*!< Factory where frames read from the channel, or read from the whisper source will go through */
84 struct ast_slinfactory write_factory; /*!< Factory where frames written to the channel will go through */
85 int format; /*!< Format translation path is setup as */
86 struct ast_trans_pvt *trans_pvt; /*!< Translation path for reading frames */
87 ast_audiohook_manipulate_callback manipulate_callback; /*!< Manipulation callback */
88 struct ast_audiohook_options options; /*!< Applicable options */
89 AST_LIST_ENTRY(ast_audiohook) list; /*!< Linked list information */
92 struct ast_audiohook_list;
94 /*! \brief Initialize an audiohook structure
95 * \param audiohook Audiohook structure
96 * \param type Type of audiohook to initialize this as
97 * \param source Who is initializing this audiohook
98 * \return Returns 0 on success, -1 on failure
100 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source);
102 /*! \brief Destroys an audiohook structure
103 * \param audiohook Audiohook structure
104 * \return Returns 0 on success, -1 on failure
106 int ast_audiohook_destroy(struct ast_audiohook *audiohook);
108 /*! \brief Writes a frame into the audiohook structure
109 * \param audiohook Audiohook structure
110 * \param direction Direction the audio frame came from
111 * \param frame Frame to write in
112 * \return Returns 0 on success, -1 on failure
114 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame);
116 /*! \brief Reads a frame in from the audiohook structure
117 * \param audiohook Audiohook structure
118 * \param samples Number of samples wanted
119 * \param direction Direction the audio frame came from
120 * \param format Format of frame remote side wants back
121 * \return Returns frame on success, NULL on failure
123 struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format);
125 /*! \brief Attach audiohook to channel
126 * \param chan Channel
127 * \param audiohook Audiohook structure
128 * \return Returns 0 on success, -1 on failure
130 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook);
132 /*! \brief Detach audiohook from channel
133 * \param audiohook Audiohook structure
134 * \return Returns 0 on success, -1 on failure
136 int ast_audiohook_detach(struct ast_audiohook *audiohook);
138 /*! \brief Detach audiohooks from list and destroy said list
139 * \param audiohook_list List of audiohooks
140 * \return Returns 0 on success, -1 on failure
142 int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list);
144 /*! \brief Detach specified source audiohook from channel
145 * \param chan Channel to detach from
146 * \param source Name of source to detach
147 * \return Returns 0 on success, -1 on failure
149 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source);
151 /*! \brief Pass a frame off to be handled by the audiohook core
152 * \param chan Channel that the list is coming off of
153 * \param audiohook_list List of audiohooks
154 * \param direction Direction frame is coming in from
155 * \param frame The frame itself
156 * \return Return frame on success, NULL on failure
158 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);
160 /*! \brief Wait for audiohook trigger to be triggered
161 * \param audiohook Audiohook to wait on
163 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook);
165 /*! \brief Lock an audiohook
166 * \param audiohook Audiohook structure
168 #define ast_audiohook_lock(ah) ast_mutex_lock(&(ah)->lock)
170 /*! \brief Unlock an audiohook
171 * \param audiohook Audiohook structure
173 #define ast_audiohook_unlock(ah) ast_mutex_unlock(&(ah)->lock)
175 #if defined(__cplusplus) || defined(c_plusplus)
179 #endif /* _ASTERISK_AUDIOHOOK_H */