blob: 6362bf14e50f62b57e697486a9d3e453efb55450 [file] [log] [blame]
Benny Prijono2cd64f82009-02-17 19:57:48 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#ifndef __AUDIODEV_H__
21#define __AUDIODEV_H__
22
23/**
24 * @file audiodev.h
25 * @brief Audio device API.
26 */
27#include <pjmedia/types.h>
28#include <pj/pool.h>
29
30
31PJ_BEGIN_DECL
32
33/**
34 * @defgroup PJMED_AUD_DEV Audio device API
35 * @ingroup PJMED_AUD_PORT
36 * @brief API to work with audio devices and to implement new audio device backends.
37 * @{
38 *
39 * The audio device API contains two parts:
40 * - the base API, for both applications and audio device implementors, and
41 * - the API for audio device implementors, for extending the audio device
42 * framework with new audio device drivers.
43 *
44 * @}
45 */
46
47/**
48 * @defgroup PJMED_AUD_DEV_API Base Audio Device API
49 * @ingroup PJMED_AUD_DEV
50 * @brief The base API, for both applications and audio device implementors
51 * @{
52 */
53
54/** Device identifier */
55typedef pj_int32_t pjmedia_aud_dev_id;
56
57/** Constant to denote default ID */
58#define PJMEDIA_AUD_DEV_DEFAULT_ID (-1)
59
60
61/**
62 * This enumeration identifies various audio device capabilities. These audio
63 * capabilities indicates what features are supported by the underlying
64 * audio device implementation.
65 *
66 * Applications get these capabilities in the #pjmedia_aud_dev_info structure.
67 *
68 * Application can also set the specific features/capabilities when opening
69 * the audio stream by setting the \a flags member of #pjmedia_aud_dev_param
70 * structure.
71 *
72 * Once audio stream is running, application can also retrieve or set some
73 * specific audio capability, by using #pjmedia_aud_stream_get_cap() and
74 * #pjmedia_aud_stream_set_cap() and specifying the desired capability. The
75 * value of the capability is specified as pointer, and application needs to
76 * supply the pointer with the correct value, according to the documentation
77 * of each of the capability.
78 */
79typedef enum pjmedia_aud_dev_cap
80{
81 /**
82 * Support for audio formats other than PCM. The value of this capability
83 * is represented by #pjmedia_format structure.
84 */
85 PJMEDIA_AUD_DEV_CAP_EXT_FORMAT = 1,
86
87 /**
88 * Support for audio input latency control or query. The value of this
89 * capability is an unsigned integer containing milliseconds value of
90 * the latency.
91 */
92 PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY = 2,
93
94 /**
95 * Support for audio output latency control or query. The value of this
96 * capability is an unsigned integer containing milliseconds value of
97 * the latency.
98 */
99 PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY = 4,
100
101 /**
102 * Support for setting the audio device volume level. The value of this
103 * capability is an unsigned integer representing the audio volume in
104 * percent.
105 */
106 PJMEDIA_AUD_DEV_CAP_SET_VOLUME = 8,
107
108 /**
109 * Support for audio volume level query. The value of this capability
110 * is unsigned integer representing the audio volume in percent.
111 */
112 PJMEDIA_AUD_DEV_CAP_GET_VOLUME = 16,
113
114 /**
115 * Support for audio routing (e.g. loudspeaker vs earpiece). The value
116 * of this capability is an integer containing #pjmedia_aud_dev_route
117 * enumeration.
118 */
119 PJMEDIA_AUD_DEV_CAP_ROUTE = 32,
120
121 /**
122 * The audio device has echo cancellation feature. The value of this
123 * capability is an integer containing boolean PJ_TRUE or PJ_FALSE.
124 */
125 PJMEDIA_AUD_DEV_CAP_EC = 64,
126
127 /**
128 * The audio device supports setting echo cancellation fail length. The
129 * value of this capability is an unsigned integer representing the
130 * echo tail in milliseconds.
131 */
132 PJMEDIA_AUD_DEV_CAP_EC_TAIL = 128,
133
134 /**
135 * The audio device has voice activity detection feature. The value
136 * of this capability is an integer containing boolean PJ_TRUE or
137 * PJ_FALSE.
138 */
139 PJMEDIA_AUD_DEV_CAP_VAD = 256,
140
141 /**
142 * The audio device has comfort noise generation feature. The value
143 * of this capability is an integer containing boolean PJ_TRUE or
144 * PJ_FALSE.
145 */
146 PJMEDIA_AUD_DEV_CAP_CNG = 512,
147
148 /**
149 * The audio device has packet loss concealment feature. The value
150 * of this capability is an integer containing boolean PJ_TRUE or
151 * PJ_FALSE.
152 */
153 PJMEDIA_AUD_DEV_CAP_PLC = 1024
154
155} pjmedia_aud_dev_cap;
156
157
158/**
159 * This enumeration describes audio routing setting.
160 */
161typedef enum pjmedia_aud_dev_route
162{
163 /** Default route. */
164 PJMEDIA_AUD_DEV_ROUTE_DEFAULT = 0,
165
166 /** Route to loudspeaker */
167 PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER = 1,
168
169 /** Route to earpiece */
170 PJMEDIA_AUD_DEV_ROUTE_EARPIECE = 2
171
172} pjmedia_aud_dev_route;
173
174
175/**
176 * Device information structure returned by #pjmedia_aud_get_dev_info.
177 */
178typedef struct pjmedia_aud_dev_info
179{
180 /**
181 * The device name
182 */
183 char name[64];
184
185 /**
186 * Maximum number of input channels supported by this device. If the
187 * value is zero, the device does not support input operation (i.e.
188 * it is a playback only device).
189 */
190 unsigned input_count;
191
192 /**
193 * Maximum number of output channels supported by this device. If the
194 * value is zero, the device does not support output operation (i.e.
195 * it is an input only device).
196 */
197 unsigned output_count;
198
199 /**
200 * Default sampling rate.
201 */
202 unsigned default_samples_per_sec;
203
204 /**
205 * The underlying driver name
206 */
207 char driver[64];
208
209 /**
210 * Device capabilities, as bitmask combination of #pjmedia_aud_dev_cap.
211 */
212 unsigned caps;
213
214 /**
215 * Supported audio device routes, as bitmask combination of
216 * #pjmedia_aud_dev_route. The value may be zero if the device
217 * does not support audio routing.
218 */
219 unsigned routes;
220
221 /**
222 * Number of audio formats supported by this device. The value may be
223 * zero if the device does not support non-PCM format.
224 */
225 unsigned ext_fmt_cnt;
226
227 /**
228 * Array of supported extended audio formats
229 */
230 pjmedia_format ext_fmt[8];
231
232
233} pjmedia_aud_dev_info;
234
235
236/**
237 * This callback is called by player stream when it needs additional data
238 * to be played by the device. Application must fill in the whole of output
239 * buffer with audio samples.
240 *
241 * @param user_data User data associated with the stream.
242 * @param ts Timestamp, in samples.
243 * @param output Buffer to be filled out by application.
244 * @param size The size requested in bytes, which will be equal to
245 * the size of one whole packet.
246 *
247 * @return Non-zero to stop the stream.
248 */
249typedef pj_status_t (*pjmedia_aud_play_cb)(/* in */ void *user_data,
250 /* in */ const pj_timestamp *ts,
251 /* out */ void *output,
252 /* out */ unsigned size);
253
254/**
255 * This callback is called by recorder stream when it has captured the whole
256 * packet worth of audio samples.
257 *
258 * @param user_data User data associated with the stream.
259 * @param ts Timestamp, in samples.
260 * @param output Buffer containing the captured audio samples.
261 * @param size The size of the data in the buffer, in bytes.
262 *
263 * @return Non-zero to stop the stream.
264 */
265typedef pj_status_t (*pjmedia_aud_rec_cb)(/* in */ void *user_data,
266 /* in */ const pj_timestamp *ts,
267 /* in */ void *input,
268 /* in*/ unsigned size);
269
270/**
271 * This structure specifies the parameters to open the audio device stream.
272 */
273typedef struct pjmedia_aud_dev_param
274{
275 /**
276 * The audio direction. This setting is mandatory.
277 */
278 pjmedia_dir dir;
279
280 /**
281 * The audio recorder device ID. This setting is mandatory if the audio
282 * direction includes input/capture direction.
283 */
284 pjmedia_aud_dev_id rec_id;
285
286 /**
287 * The audio playback device ID. This setting is mandatory if the audio
288 * direction includes output/playback direction.
289 */
290 pjmedia_aud_dev_id play_id;
291
292 /**
293 * Clock rate/sampling rate. This setting is mandatory.
294 */
295 unsigned clock_rate;
296
297 /**
298 * Number of channels. This setting is mandatory.
299 */
300 unsigned channel_count;
301
302 /**
303 * Number of samples per frame. This setting is mandatory.
304 */
305 unsigned samples_per_frame;
306
307 /**
308 * Number of bits per sample. This setting is mandatory.
309 */
310 unsigned bits_per_sample;
311
312 /**
313 * This flags specifies which of the optional settings are valid in this
314 * structure. The flags is bitmask combination of pjmedia_aud_dev_cap.
315 */
316 unsigned flags;
317
318 /**
319 * Set the audio format. This setting is optional, and will only be used
320 * if PJMEDIA_AUD_DEV_CAP_EXT_FORMAT is set in the flags.
321 */
322 pjmedia_format ext_fmt;
323
324 /**
325 * Input latency, in milliseconds. This setting is optional, and will
326 * only be used if PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY is set in the flags.
327 */
328 unsigned input_latency_ms;
329
330 /**
331 * Input latency, in milliseconds. This setting is optional, and will
332 * only be used if PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY is set in the flags.
333 */
334 unsigned output_latency_ms;
335
336 /**
337 * Set the audio route. This setting is optional, and will only be used
338 * if PJMEDIA_AUD_DEV_CAP_ROUTE is set in the flags.
339 */
340 pjmedia_aud_dev_route route;
341
342 /**
343 * Enable/disable echo canceller, if the device supports it. This setting
344 * is optional, and will only be used if PJMEDIA_AUD_DEV_CAP_EC is set in
345 * the flags.
346 */
347 pj_bool_t ec_enabled;
348
349 /**
350 * Set echo canceller tail length in milliseconds, if the device supports
351 * it. This setting is optional, and will only be used if
352 * PJMEDIA_AUD_DEV_CAP_EC_TAIL is set in the flags.
353 */
354 unsigned ec_tail_ms;
355
356 /**
357 * Enable/disable PLC. This setting is optional, and will only be used
358 * if PJMEDIA_AUD_DEV_CAP_PLC is set in the flags.
359 */
360 pj_bool_t plc_enabled;
361
362 /**
363 * Enable/disable CNG. This setting is optional, and will only be used
364 * if PJMEDIA_AUD_DEV_CAP_CNG is set in the flags.
365 */
366 pj_bool_t cng_enabled;
367
368} pjmedia_aud_dev_param;
369
370
371/** Forward declaration for pjmedia_aud_stream */
372typedef struct pjmedia_aud_stream pjmedia_aud_stream;
373
374/** Forward declaration for audio device factory */
375typedef struct pjmedia_aud_dev_factory pjmedia_aud_dev_factory;
376
377
378/**
379 * Initialize the audio subsystem. This will register all supported audio
380 * device factories to the audio subsystem. This function may be called
381 * more than once, but each call to this function must have the
382 * corresponding #pjmedia_aud_subsys_shutdown() call.
383 *
384 * @param pf The pool factory.
385 *
386 * @return PJ_SUCCESS on successful operation or the appropriate
387 * error code.
388 */
389PJ_DECL(pj_status_t) pjmedia_aud_subsys_init(pj_pool_factory *pf);
390
391
392/**
393 * Shutdown the audio subsystem. This will destroy all audio device factories
394 * registered in the audio subsystem. Note that currently opened audio streams
395 * may or may not be closed, depending on the implementation of the audio
396 * device factories.
397 *
398 * @return PJ_SUCCESS on successful operation or the appropriate
399 * error code.
400 */
401PJ_DECL(pj_status_t) pjmedia_aud_subsys_shutdown(void);
402
403
404/**
405 * Get the number of sound devices installed in the system.
406 *
407 * @return The number of sound devices installed in the system.
408 */
409PJ_DECL(unsigned) pjmedia_aud_dev_count(void);
410
411
412/**
413 * Enumerate device ID's.
414 *
415 * @param max_count Maximum number of device id's to retrieve.
416 * @param ids Array to receive the device id's.
417 *
418 * @return The actual number of device id's filled in.
419 */
420PJ_DECL(unsigned) pjmedia_aud_dev_enum(unsigned max_count,
421 pjmedia_aud_dev_id ids[]);
422
423/**
424 * Get device information.
425 *
426 * @param id The audio device ID.
427 * @param info The device information which will be filled in by this
428 * function once it returns successfully.
429 *
430 * @return PJ_SUCCESS on successful operation or the appropriate
431 * error code.
432 */
433PJ_DECL(pj_status_t) pjmedia_aud_dev_get_info(pjmedia_aud_dev_id id,
434 pjmedia_aud_dev_info *info);
435
436
437/**
438 * Initialize the audio device parameters with default values for the
439 * specified device.
440 *
441 * @param id The audio device ID.
442 * @param param The audio device parameters which will be initialized
443 * by this function once it returns successfully.
444 *
445 * @return PJ_SUCCESS on successful operation or the appropriate
446 * error code.
447 */
448PJ_DECL(pj_status_t) pjmedia_aud_dev_default_param(pjmedia_aud_dev_id id,
449 pjmedia_aud_dev_param *param);
450
451
452/**
453 * Open audio stream object using the specified parameters.
454 *
455 * @param param Sound device parameters to be used for the stream.
456 * @param rec_cb Callback to be called on every input frame captured.
457 * @param play_cb Callback to be called everytime the sound device needs
458 * audio frames to be played back.
459 * @param user_data Arbitrary user data, which will be given back in the
460 * callbacks.
461 * @param p_aud_strm Pointer to receive the audio stream.
462 *
463 * @return PJ_SUCCESS on successful operation or the appropriate
464 * error code.
465 */
466PJ_DECL(pj_status_t) pjmedia_aud_stream_create(const pjmedia_aud_dev_param *param,
467 pjmedia_aud_rec_cb rec_cb,
468 pjmedia_aud_play_cb play_cb,
469 void *user_data,
470 pjmedia_aud_stream **p_aud_strm);
471
472/**
473 * Get the running parameters for the specified audio stream.
474 *
475 * @param strm The audio stream.
476 * @param param Audio stream parameters to be filled in by this
477 * function once it returns successfully.
478 *
479 * @return PJ_SUCCESS on successful operation or the appropriate
480 * error code.
481 */
482PJ_DECL(pj_status_t) pjmedia_aud_stream_get_param(pjmedia_aud_stream *strm,
483 pjmedia_aud_dev_param *param);
484
485/**
486 * Get the value of a specific capability of the audio stream.
487 *
488 * @param strm The audio stream.
489 * @param cap The audio capability which value is to be retrieved.
490 * @param value Pointer to value to be filled in by this function
491 * once it returns successfully.
492 *
493 * @return PJ_SUCCESS on successful operation or the appropriate
494 * error code.
495 */
496PJ_DECL(pj_status_t) pjmedia_aud_stream_get_cap(pjmedia_aud_stream *strm,
497 pjmedia_aud_dev_cap cap,
498 void *value);
499
500/**
501 * Set the value of a specific capability of the audio stream.
502 *
503 * @param strm The audio stream.
504 * @param cap The audio capability which value is to be set.
505 * @param value Pointer to value.
506 *
507 * @return PJ_SUCCESS on successful operation or the appropriate
508 * error code.
509 */
510PJ_DECL(pj_status_t) pjmedia_aud_stream_set_cap(pjmedia_aud_stream *strm,
511 pjmedia_aud_dev_cap cap,
512 const void *value);
513
514/**
515 * Start the stream.
516 *
517 * @param strm The audio stream.
518 *
519 * @return PJ_SUCCESS on successful operation or the appropriate
520 * error code.
521 */
522PJ_DECL(pj_status_t) pjmedia_aud_stream_start(pjmedia_aud_stream *strm);
523
524/**
525 * Stop the stream.
526 *
527 * @param strm The audio stream.
528 *
529 * @return PJ_SUCCESS on successful operation or the appropriate
530 * error code.
531 */
532PJ_DECL(pj_status_t) pjmedia_aud_stream_stop(pjmedia_aud_stream *strm);
533
534/**
535 * Destroy the stream.
536 *
537 * @param strm The audio stream.
538 *
539 * @return PJ_SUCCESS on successful operation or the appropriate
540 * error code.
541 */
542PJ_DECL(pj_status_t) pjmedia_aud_stream_destroy(pjmedia_aud_stream *strm);
543
544
545/* Invalid audio device */
546#define PJMEDIA_EAUD_INVDEV -1
547
548/**
549 * @)
550 */
551
552PJ_END_DECL
553
554
555#endif /* __AUDIODEV_H__ */
556