Asterisk - The Open Source Telephony Project  18.5.0
audiohook.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, Digium, Inc.
5  *
6  * Joshua Colp <[email protected]>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  * \brief Audiohooks Architecture
21  */
22 
23 #ifndef _ASTERISK_AUDIOHOOK_H
24 #define _ASTERISK_AUDIOHOOK_H
25 
26 #if defined(__cplusplus) || defined(c_plusplus)
27 extern "C" {
28 #endif
29 
30 /* these two are used in struct ast_audiohook */
31 #include "asterisk/lock.h"
32 #include "asterisk/linkedlists.h"
33 #include "asterisk/slinfactory.h"
34 
36  AST_AUDIOHOOK_TYPE_SPY = 0, /*!< Audiohook wants to receive audio */
37  AST_AUDIOHOOK_TYPE_WHISPER, /*!< Audiohook wants to provide audio to be mixed with existing audio */
38  AST_AUDIOHOOK_TYPE_MANIPULATE, /*!< Audiohook wants to manipulate the audio */
39 };
40 
42  AST_AUDIOHOOK_STATUS_NEW = 0, /*!< Audiohook was just created, not in use yet */
43  AST_AUDIOHOOK_STATUS_RUNNING, /*!< Audiohook is running on a channel */
44  AST_AUDIOHOOK_STATUS_SHUTDOWN, /*!< Audiohook is being shutdown */
45  AST_AUDIOHOOK_STATUS_DONE, /*!< Audiohook has shutdown and is not running on a channel any longer */
46 };
47 
49  AST_AUDIOHOOK_DIRECTION_READ = 0, /*!< Reading audio in */
50  AST_AUDIOHOOK_DIRECTION_WRITE, /*!< Writing audio out */
51  AST_AUDIOHOOK_DIRECTION_BOTH, /*!< Both reading audio in and writing audio out */
52 };
53 
55  AST_AUDIOHOOK_TRIGGER_MODE = (3 << 0), /*!< When audiohook should be triggered to do something */
56  AST_AUDIOHOOK_TRIGGER_READ = (1 << 0), /*!< Audiohook wants to be triggered when reading audio in */
57  AST_AUDIOHOOK_TRIGGER_WRITE = (2 << 0), /*!< Audiohook wants to be triggered when writing audio out */
58  AST_AUDIOHOOK_WANTS_DTMF = (1 << 2), /*!< Audiohook also wants to receive DTMF frames */
59  AST_AUDIOHOOK_TRIGGER_SYNC = (1 << 3), /*!< Audiohook wants to be triggered when both sides have combined audio available */
60  /*! Audiohooks with this flag set will not allow for a large amount of samples to build up on its
61  * slinfactories. We will flush the factories if they contain too many samples.
62  */
64  AST_AUDIOHOOK_MUTE_READ = (1 << 5), /*!< audiohook should be mute frames read */
65  AST_AUDIOHOOK_MUTE_WRITE = (1 << 6), /*!< audiohook should be mute frames written */
66  AST_AUDIOHOOK_COMPATIBLE = (1 << 7), /*!< is the audiohook native slin compatible */
67 
68  AST_AUDIOHOOK_SUBSTITUTE_SILENCE = (1 << 8), /*!< Substitute silence for missing audio */
69 };
70 
72  /*! Audiohook manipulate callback is capable of handling slinear at any sample rate.
73  * Without enabling this flag on initialization the manipulation callback is guaranteed
74  * 8khz audio only. */
76 };
77 
78 struct ast_audiohook;
79 
80 /*! \brief Callback function for manipulate audiohook type
81  * \param audiohook Audiohook structure
82  * \param chan Channel
83  * \param frame Frame of audio to manipulate
84  * \param direction Direction frame came from
85  * \return Returns 0 on success, -1 on failure.
86  * \note An audiohook does not have any reference to a private data structure for manipulate
87  * types. It is up to the manipulate callback to store this data via it's own method.
88  * An example would be datastores.
89  * \note The input frame should never be freed or corrupted during a manipulate callback.
90  * If the callback has the potential to corrupt the frame's data during manipulation,
91  * local data should be used for the manipulation and only copied to the frame on
92  * success.
93  * \note A failure return value indicates that the frame was not manipulated and that
94  * is being returned in its original state.
95  */
96 typedef int (*ast_audiohook_manipulate_callback)(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction);
97 
99  int read_volume; /*!< Volume adjustment on frames read from the channel the hook is on */
100  int write_volume; /*!< Volume adjustment on frames written to the channel the hook is on */
101 };
102 
104  ast_mutex_t lock; /*!< Lock that protects the audiohook structure */
105  ast_cond_t trigger; /*!< Trigger condition (if enabled) */
106  enum ast_audiohook_type type; /*!< Type of audiohook */
107  enum ast_audiohook_status status; /*!< Status of the audiohook */
108  enum ast_audiohook_init_flags init_flags; /*!< Init flags */
109  const char *source; /*!< Who this audiohook ultimately belongs to */
110  unsigned int flags; /*!< Flags on the audiohook */
111  struct ast_slinfactory read_factory; /*!< Factory where frames read from the channel, or read from the whisper source will go through */
112  struct ast_slinfactory write_factory; /*!< Factory where frames written to the channel will go through */
113  struct timeval read_time; /*!< Last time read factory was fed */
114  struct timeval write_time; /*!< Last time write factory was fed */
115  struct ast_format *format; /*!< Format translation path is setup as */
116  struct ast_trans_pvt *trans_pvt; /*!< Translation path for reading frames */
118  struct ast_audiohook_options options; /*!< Applicable options */
119  unsigned int hook_internal_samp_rate; /*!< internal read/write sample rate on the audiohook.*/
120  AST_LIST_ENTRY(ast_audiohook) list; /*!< Linked list information */
121 };
122 
123 struct ast_audiohook_list;
124 
125 /*! \brief Initialize an audiohook structure
126  * \param audiohook Audiohook structure
127  * \param type Type of audiohook to initialize this as
128  * \param source Who is initializing this audiohook
129  * \param init flags
130  * \return Returns 0 on success, -1 on failure
131  */
132 int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags);
133 
134 /*! \brief Destroys an audiohook structure
135  * \param audiohook Audiohook structure
136  * \return Returns 0 on success, -1 on failure
137  */
138 int ast_audiohook_destroy(struct ast_audiohook *audiohook);
139 
140 /*! \brief Writes a frame into the audiohook structure
141  * \param audiohook Audiohook structure
142  * \param direction Direction the audio frame came from
143  * \param frame Frame to write in
144  * \return Returns 0 on success, -1 on failure
145  */
146 int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame);
147 
148 /*! \brief Reads a frame in from the audiohook structure
149  * \param audiohook Audiohook structure
150  * \param samples Number of samples wanted
151  * \param direction Direction the audio frame came from
152  * \param format Format of frame remote side wants back
153  * \return Returns frame on success, NULL on failure
154  */
156 
157 /*! \brief Reads a frame in from the audiohook structure in mixed audio mode and copies read and write frame data to provided arguments.
158  * \param audiohook Audiohook structure
159  * \param samples Number of samples wanted
160  * \param ast_format Format of frame remote side wants back
161  * \param read_frame if available, we'll copy the read buffer to this.
162  * \param write_frame if available, we'll copy the write buffer to this.
163  * \param direction
164  * \return Returns frame on success, NULL on failure
165  */
166 struct ast_frame *ast_audiohook_read_frame_all(struct ast_audiohook *audiohook, size_t samples, struct ast_format *format, struct ast_frame **read_frame, struct ast_frame **write_frame);
167 
168 /*! \brief Attach audiohook to channel
169  * \param chan Channel
170  * \param audiohook Audiohook structure
171  * \return Returns 0 on success, -1 on failure
172  */
173 int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook);
174 
175 /*! \brief Detach audiohook from channel
176  * \param audiohook Audiohook structure
177  * \return Returns 0 on success, -1 on failure
178  */
179 int ast_audiohook_detach(struct ast_audiohook *audiohook);
180 
181 /*!
182  * \brief Detach audiohooks from list and destroy said list
183  * \param audiohook_list List of audiohooks (NULL tolerant)
184  * \return Nothing
185  */
186 void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list);
187 
188 /*! \brief Move an audiohook from one channel to a new one
189  *
190  * \todo Currently only the first audiohook of a specific source found will be moved.
191  * We should add the capability to move multiple audiohooks from a single source as well.
192  *
193  * \note It is required that both old_chan and new_chan are locked prior to calling
194  * this function. Besides needing to protect the data within the channels, not locking
195  * these channels can lead to a potential deadlock
196  *
197  * \param old_chan The source of the audiohook to move
198  * \param new_chan The destination to which we want the audiohook to move
199  * \param source The source of the audiohook we want to move
200  */
201 void ast_audiohook_move_by_source(struct ast_channel *old_chan, struct ast_channel *new_chan, const char *source);
202 
203 /*! \brief Move all audiohooks from one channel to another
204  *
205  * \note It is required that both old_chan and new_chan are locked prior to calling
206  * this function. Besides needing to protect the data within the channels, not locking
207  * these channels can lead to a potential deadlock.
208  *
209  * \param old_chan The source of the audiohooks being moved
210  * \param new_chan The destination channel for the audiohooks to be moved to
211  */
212 void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan);
213 
214 /*!
215  * \brief Detach specified source audiohook from channel
216  *
217  * \param chan Channel to detach from
218  * \param source Name of source to detach
219  *
220  * \return Returns 0 on success, -1 on failure
221  *
222  * \note The channel does not need to be locked before calling this function.
223  */
224 int ast_audiohook_detach_source(struct ast_channel *chan, const char *source);
225 
226 /*!
227  * \brief Remove an audiohook from a specified channel
228  *
229  * \param chan Channel to remove from
230  * \param audiohook Audiohook to remove
231  *
232  * \return Returns 0 on success, -1 on failure
233  *
234  * \note The channel does not need to be locked before calling this function
235  */
236 int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook);
237 
238 /*!
239  * \brief Determine if a audiohook_list is empty or not.
240  *
241  * \param audiohook Audiohook to check. (NULL also means empty)
242  *
243  * retval 0 false, 1 true
244  */
245 int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list);
246 
247 /*! \brief Pass a frame off to be handled by the audiohook core
248  * \param chan Channel that the list is coming off of
249  * \param audiohook_list List of audiohooks
250  * \param direction Direction frame is coming in from
251  * \param frame The frame itself
252  * \return Return frame on success, NULL on failure
253  */
254 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);
255 
256 /*! \brief Update audiohook's status
257  * \param audiohook Audiohook structure
258  * \param audiohook status enum
259  *
260  * \note once status is updated to DONE, this function can not be used to set the
261  * status back to any other setting. Setting DONE effectively locks the status as such.
262  */
264 
265 /*! \brief Wait for audiohook trigger to be triggered
266  * \param audiohook Audiohook to wait on
267  */
268 void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook);
269 
270 /*!
271  \brief Find out how many audiohooks from a certain source exist on a given channel, regardless of status.
272  \param chan The channel on which to find the spies
273  \param source The audiohook's source
274  \param type The type of audiohook
275  \return Return the number of audiohooks which are from the source specified
276 
277  Note: Function performs nlocking.
278 */
279 int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type);
280 
281 /*!
282  \brief Find out how many spies of a certain type exist on a given channel, and are in state running.
283  \param chan The channel on which to find the spies
284  \param source The source of the audiohook
285  \param type The type of spy to look for
286  \return Return the number of running audiohooks which are from the source specified
287 
288  Note: Function performs no locking.
289 */
290 int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type);
291 
292 /*! \brief Lock an audiohook
293  * \param ah Audiohook structure
294  */
295 #define ast_audiohook_lock(ah) ast_mutex_lock(&(ah)->lock)
296 
297 /*! \brief Unlock an audiohook
298  * \param ah Audiohook structure
299  */
300 #define ast_audiohook_unlock(ah) ast_mutex_unlock(&(ah)->lock)
301 
302 /*!
303  * \brief Adjust the volume on frames read from or written to a channel
304  * \param chan Channel to muck with
305  * \param direction Direction to set on
306  * \param volume Value to adjust the volume by
307  * \return Returns 0 on success, -1 on failure
308  * \since 1.6.1
309  */
311 
312 /*!
313  * \brief Retrieve the volume adjustment value on frames read from or written to a channel
314  * \param chan Channel to retrieve volume adjustment from
315  * \param direction Direction to retrieve
316  * \return Returns adjustment value
317  * \since 1.6.1
318  */
320 
321 /*!
322  * \brief Adjust the volume on frames read from or written to a channel
323  * \param chan Channel to muck with
324  * \param direction Direction to increase
325  * \param volume Value to adjust the adjustment by
326  * \return Returns 0 on success, -1 on failure
327  * \since 1.6.1
328  */
330 
331 /*! \brief Mute frames read from or written to a channel
332  * \param chan Channel to muck with
333  * \param source Type of audiohook
334  * \param flag which direction to set / clear
335  * \param clear set or clear muted frames on direction based on flag parameter
336  * \retval 0 success
337  * \retval -1 failure
338  */
339 int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear);
340 
341 #if defined(__cplusplus) || defined(c_plusplus)
342 }
343 #endif
344 
345 #endif /* _ASTERISK_AUDIOHOOK_H */
int(* ast_audiohook_manipulate_callback)(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
Callback function for manipulate audiohook type.
Definition: audiohook.h:96
int ast_audiohook_volume_set(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
Adjust the volume on frames read from or written to a channel.
Definition: audiohook.c:1346
static const char type[]
Definition: chan_ooh323.c:109
Main Channel structure associated with a channel.
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.
Definition: audiohook.c:684
ast_audiohook_flags
Definition: audiohook.h:54
Asterisk locking-related definitions:
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.
Definition: audiohook.c:170
int ast_audiohook_volume_get(struct ast_channel *chan, enum ast_audiohook_direction direction)
Retrieve the volume adjustment value on frames read from or written to a channel. ...
Definition: audiohook.c:1371
void ast_audiohook_move_all(struct ast_channel *old_chan, struct ast_channel *new_chan)
Move all audiohooks from one channel to another.
Definition: audiohook.c:700
ast_audiohook_init_flags
Definition: audiohook.h:71
void ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list)
Detach audiohooks from list and destroy said list.
Definition: audiohook.c:594
int ast_audiohook_write_list_empty(struct ast_audiohook_list *audiohook_list)
Determine if a audiohook_list is empty or not.
Definition: audiohook.c:1112
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Definition: audiohook.c:764
void ast_audiohook_update_status(struct ast_audiohook *audiohook, enum ast_audiohook_status status)
Update audiohook&#39;s status.
Definition: audiohook.c:565
int ast_audiohook_set_mute(struct ast_channel *chan, const char *source, enum ast_audiohook_flags flag, int clear)
Mute frames read from or written to a channel.
Definition: audiohook.c:1424
Definition of a media format.
Definition: format.c:43
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
Definition: audiohook.c:501
ast_mutex_t lock
Definition: audiohook.h:104
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
Definition: audiohook.c:133
struct ast_format * format
Definition: audiohook.h:115
pthread_cond_t ast_cond_t
Definition: lock.h:176
ast_audiohook_manipulate_callback manipulate_callback
Definition: audiohook.h:117
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
Definition: audiohook.c:108
int ast_channel_audiohook_count_by_source(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
Find out how many audiohooks from a certain source exist on a given channel, regardless of status...
Definition: audiohook.c:1157
ast_audiohook_type
Definition: audiohook.h:35
A set of macros to manage forward-linked lists.
struct ast_trans_pvt * trans_pvt
Definition: audiohook.h:116
int ast_audiohook_detach_source(struct ast_channel *chan, const char *source)
Detach specified source audiohook from channel.
Definition: audiohook.c:731
unsigned int hook_internal_samp_rate
Definition: audiohook.h:119
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
Definition: audiohook.c:579
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.
Definition: audiohook.c:1127
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition: translate.h:213
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
long int flag
Definition: f2c.h:83
const char * source
Definition: audiohook.h:109
A machine to gather up arbitrary frames and convert them to raw slinear on demand.
static struct ast_frame * read_frame(struct ast_filestream *s, int *whennext)
Definition: file.c:874
int ast_audiohook_volume_adjust(struct ast_channel *chan, enum ast_audiohook_direction direction, int volume)
Adjust the volume on frames read from or written to a channel.
Definition: audiohook.c:1397
ast_cond_t trigger
Definition: audiohook.h:105
ast_audiohook_direction
Definition: audiohook.h:48
struct ast_frame * ast_audiohook_read_frame_all(struct ast_audiohook *audiohook, size_t samples, struct ast_format *format, struct ast_frame **read_frame, struct ast_frame **write_frame)
Reads a frame in from the audiohook structure in mixed audio mode and copies read and write frame dat...
Definition: audiohook.c:463
unsigned int flags
Definition: audiohook.h:110
Data structure associated with a single frame of data.
static struct test_options options
ast_audiohook_status
Definition: audiohook.h:41
direction
static snd_pcm_format_t format
Definition: chan_alsa.c:102
void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook)
Wait for audiohook trigger to be triggered.
Definition: audiohook.c:1142
struct ast_frame * ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, struct ast_format *format)
Reads a frame in from the audiohook structure.
Definition: audiohook.c:449
int ast_channel_audiohook_count_by_source_running(struct ast_channel *chan, const char *source, enum ast_audiohook_type type)
Find out how many spies of a certain type exist on a given channel, and are in state running...
Definition: audiohook.c:1197
Structure for mutex and tracking information.
Definition: lock.h:135
jack_status_t status
Definition: app_jack.c:146