Benny Prijono | 2058f47 | 2009-02-22 17:15:34 +0000 | [diff] [blame] | 1 | /* $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 __PJMEDIA_AUDIODEV_CONFIG_H__ |
| 21 | #define __PJMEDIA_AUDIODEV_CONFIG_H__ |
| 22 | |
| 23 | /** |
| 24 | * @file audiodev.h |
| 25 | * @brief Audio device API. |
| 26 | */ |
Benny Prijono | 64f9138 | 2009-03-05 18:02:28 +0000 | [diff] [blame] | 27 | #include <pjmedia/types.h> |
Benny Prijono | 2058f47 | 2009-02-22 17:15:34 +0000 | [diff] [blame] | 28 | #include <pj/pool.h> |
| 29 | |
| 30 | |
| 31 | PJ_BEGIN_DECL |
| 32 | |
| 33 | /** |
| 34 | * @defgroup audio_device_api Audio Device API |
| 35 | * @brief PJMEDIA audio device abstraction API. |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * @defgroup s1_audio_device_config Compile time configurations |
| 40 | * @ingroup audio_device_api |
| 41 | * @brief Compile time configurations |
| 42 | * @{ |
| 43 | */ |
| 44 | |
| 45 | /** |
| 46 | * This setting controls whether PortAudio support should be included. |
| 47 | */ |
| 48 | #ifndef PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO |
Benny Prijono | 64f9138 | 2009-03-05 18:02:28 +0000 | [diff] [blame] | 49 | # define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 1 |
Benny Prijono | 2058f47 | 2009-02-22 17:15:34 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * This setting controls whether WMME support should be included. |
| 55 | */ |
| 56 | #ifndef PJMEDIA_AUDIO_DEV_HAS_WMME |
| 57 | # define PJMEDIA_AUDIO_DEV_HAS_WMME 1 |
| 58 | #endif |
| 59 | |
| 60 | |
| 61 | /** |
Nanang Izzuddin | a940b36 | 2009-02-23 13:53:30 +0000 | [diff] [blame] | 62 | * This setting controls whether Symbian APS support should be included. |
| 63 | */ |
| 64 | #ifndef PJMEDIA_AUDIO_DEV_HAS_SYMB_APS |
| 65 | # define PJMEDIA_AUDIO_DEV_HAS_SYMB_APS 0 |
| 66 | #endif |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * This setting controls whether Symbian audio (using built-in multimedia |
| 71 | * framework) support should be included. |
| 72 | */ |
| 73 | #ifndef PJMEDIA_AUDIO_DEV_HAS_SYMB_MDA |
Benny Prijono | 64f9138 | 2009-03-05 18:02:28 +0000 | [diff] [blame] | 74 | # define PJMEDIA_AUDIO_DEV_HAS_SYMB_MDA PJ_SYMBIAN |
Nanang Izzuddin | a940b36 | 2009-02-23 13:53:30 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Benny Prijono | 64f9138 | 2009-03-05 18:02:28 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * This setting controls whether the Audio Device API should support |
| 80 | * device implementation that is based on the old sound device API |
| 81 | * (sound.h). |
| 82 | * |
| 83 | * Enable this API if: |
| 84 | * - you have implemented your own sound device using the old sound |
| 85 | * device API (sound.h), and |
| 86 | * - you wish to be able to use your sound device implementation |
| 87 | * using the new Audio Device API. |
| 88 | * |
| 89 | * Please see http://trac.pjsip.org/repos/wiki/Audio_Dev_API for more |
| 90 | * info. |
| 91 | */ |
| 92 | #ifndef PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE |
| 93 | # define PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE 0 |
| 94 | #endif |
| 95 | |
| 96 | |
Nanang Izzuddin | a940b36 | 2009-02-23 13:53:30 +0000 | [diff] [blame] | 97 | /** |
Benny Prijono | 2058f47 | 2009-02-22 17:15:34 +0000 | [diff] [blame] | 98 | * @} |
| 99 | */ |
| 100 | |
| 101 | PJ_END_DECL |
| 102 | |
| 103 | |
| 104 | #endif /* __PJMEDIA_AUDIODEV_CONFIG_H__ */ |
| 105 | |
| 106 | /* |
| 107 | --------------------- DOCUMENTATION FOLLOWS --------------------------- |
| 108 | */ |
| 109 | |
| 110 | /** |
| 111 | * @addtogroup audio_device_api Audio Device API |
| 112 | * @{ |
| 113 | |
| 114 | PJMEDIA Audio Device API is a cross-platform audio API appropriate for use with |
| 115 | VoIP applications and many other types of audio streaming applications. |
| 116 | |
| 117 | The API abstracts many different audio API's on various platforms, such as: |
| 118 | - PortAudio back-end for Win32, Windows Mobile, Linux, Unix, dan MacOS X. |
| 119 | - native WMME audio for Win32 and Windows Mobile devices |
| 120 | - native Symbian audio streaming/multimedia framework (MMF) implementation |
| 121 | - native Nokia Audio Proxy Server (APS) implementation |
| 122 | - null-audio implementation |
| 123 | - and more to be implemented in the future |
| 124 | |
| 125 | The Audio Device API/library is an evolution from PJMEDIA @ref PJMED_SND and |
| 126 | contains many enhancements: |
| 127 | |
| 128 | - Forward compatibility: |
| 129 | \n |
| 130 | The new API has been designed to be extensible, it will support new API's as |
| 131 | well as new features that may be introduced in the future without breaking |
| 132 | compatibility with applications that use this API as well as compatibility |
| 133 | with existing device implementations. |
| 134 | |
| 135 | - Device capabilities: |
| 136 | \n |
| 137 | At the heart of the API is device capabilities management, where all possible |
| 138 | audio capabilities of audio devices should be able to be handled in a generic |
| 139 | manner. With this framework, new capabilities that may be discovered in the |
| 140 | future can be handled in manner without breaking existing applications. |
| 141 | |
| 142 | - Built-in features: |
| 143 | \n |
Benny Prijono | eef2a87 | 2009-02-23 09:55:52 +0000 | [diff] [blame] | 144 | The device capabilities framework enables applications to use and control |
| 145 | audio features built-in in the device, such as: |
Benny Prijono | 2058f47 | 2009-02-22 17:15:34 +0000 | [diff] [blame] | 146 | - echo cancellation, |
| 147 | - built-in codecs, |
Benny Prijono | eef2a87 | 2009-02-23 09:55:52 +0000 | [diff] [blame] | 148 | - audio routing (e.g. to earpiece or loudspeaker), |
| 149 | - volume control, |
| 150 | - etc. |
Benny Prijono | 2058f47 | 2009-02-22 17:15:34 +0000 | [diff] [blame] | 151 | |
| 152 | - Codec support: |
| 153 | \n |
| 154 | Some audio devices such as Nokia/Symbian Audio Proxy Server (APS) and Nokia |
| 155 | VoIP Audio Services (VAS) support built-in hardware audio codecs (e.g. G.729, |
| 156 | iLBC, and AMR), and application can use the sound device in encoded mode to |
| 157 | make use of these hardware codecs. |
| 158 | |
| 159 | - Multiple backends: |
| 160 | \n |
| 161 | The new API supports multiple audio backends (called factories or drivers in |
| 162 | the code) to be active simultaneously, and audio backends may be added or |
| 163 | removed during run-time. |
Benny Prijono | eef2a87 | 2009-02-23 09:55:52 +0000 | [diff] [blame] | 164 | |
| 165 | |
| 166 | @section using Overview on using the API |
| 167 | |
| 168 | @subsection getting_started Getting started |
| 169 | |
| 170 | -# <b>Configure the application's project settings</b>.\n |
| 171 | Add the following |
| 172 | include: |
| 173 | \code |
| 174 | #include <pjmedia_audiodev.h>\endcode\n |
| 175 | And add <b>pjmedia-audiodev</b> library to your application link |
| 176 | specifications.\n |
| 177 | -# <b>Compile time settings</b>.\n |
| 178 | Use the compile time settings to enable or |
| 179 | disable specific audio drivers. For more information, please see |
| 180 | \ref s1_audio_device_config. |
| 181 | -# <b>API initialization and cleaning up</b>.\n |
| 182 | Before anything else, application must initialize the API by calling: |
| 183 | \code |
| 184 | pjmedia_aud_subsys_init(pf);\endcode\n |
| 185 | And add this in the application cleanup sequence |
| 186 | \code |
| 187 | pjmedia_aud_subsys_shutdown();\endcode |
| 188 | |
| 189 | @subsection devices Working with devices |
| 190 | |
| 191 | -# The following code prints the list of audio devices detected |
| 192 | in the system. |
| 193 | \code |
| 194 | int dev_count; |
| 195 | pjmedia_aud_dev_index dev_idx; |
| 196 | pj_status_t status; |
| 197 | |
| 198 | dev_count = pjmedia_aud_dev_count(); |
| 199 | printf("Got %d audio devices\n", dev_count); |
| 200 | |
| 201 | for (dev_idx=0; dev_idx<dev_count; ++i) { |
| 202 | pjmedia_aud_dev_info info; |
| 203 | |
| 204 | status = pjmedia_aud_dev_get_info(dev_idx, &info); |
| 205 | printf("%d. %s (in=%d, out=%d)\n", |
| 206 | dev_idx, info.name, |
| 207 | info.input_count, info.output_count); |
| 208 | } |
| 209 | \endcode\n |
| 210 | -# Info: The #PJMEDIA_AUD_DEFAULT_CAPTURE_DEV and #PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV |
| 211 | constants are used to denote default capture and playback devices |
| 212 | respectively. |
| 213 | -# Info: You may save the device and driver's name in your application |
| 214 | setting, for example to specify the prefered devices to be |
| 215 | used by your application. You can then retrieve the device index |
| 216 | for the device by calling: |
| 217 | \code |
| 218 | const char *drv_name = "WMME"; |
| 219 | const char *dev_name = "Wave mapper"; |
| 220 | pjmedia_aud_dev_index dev_idx; |
| 221 | |
| 222 | status = pjmedia_aud_dev_lookup(drv_name, dev_name, &dev_idx); |
| 223 | if (status==PJ_SUCCESS) |
| 224 | printf("Device index is %d\n", dev_idx); |
| 225 | \endcode |
| 226 | |
| 227 | @subsection caps Device capabilities |
| 228 | |
| 229 | Capabilities are encoded as #pjmedia_aud_dev_cap enumeration. Please see |
| 230 | #pjmedia_aud_dev_cap enumeration for more information. |
| 231 | |
| 232 | -# The following snippet prints the capabilities supported by the device: |
| 233 | \code |
| 234 | pjmedia_aud_dev_info info; |
| 235 | pj_status_t status; |
| 236 | |
| 237 | status = pjmedia_aud_dev_get_info(PJMEDIA_AUD_DEFAULT_CAPTURE_DEV, &info); |
| 238 | if (status == PJ_SUCCESS) { |
| 239 | unsigned i; |
| 240 | // Enumerate capability bits |
| 241 | printf("Device capabilities: "); |
| 242 | for (i=0; i<32; ++i) { |
| 243 | if (info.caps & (1 << i)) |
| 244 | printf("%s ", pjmedia_aud_dev_cap_name(1 << i, NULL)); |
| 245 | } |
| 246 | } |
| 247 | \endcode\n |
| 248 | -# Info: You can set the device settings when opening audio stream by setting |
| 249 | the flags and the appropriate setting in #pjmedia_aud_param when calling |
| 250 | #pjmedia_aud_stream_create()\n |
| 251 | -# Info: Once the audio stream is running, you can retrieve or change the stream |
| 252 | setting by specifying the capability in #pjmedia_aud_stream_get_cap() |
| 253 | and #pjmedia_aud_stream_set_cap() respectively. |
| 254 | |
| 255 | |
| 256 | @subsection creating_stream Creating audio streams |
| 257 | |
| 258 | The audio stream enables audio streaming to capture device, playback device, |
| 259 | or both. |
| 260 | |
| 261 | -# It is recommended to initialize the #pjmedia_aud_param with its default |
| 262 | values before using it: |
| 263 | \code |
| 264 | pjmedia_aud_param param; |
| 265 | pjmedia_aud_dev_index dev_idx; |
| 266 | pj_status_t status; |
| 267 | |
| 268 | dev_idx = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV; |
| 269 | status = pjmedia_aud_dev_default_param(dev_idx, ¶m); |
| 270 | \endcode\n |
| 271 | -# Configure the mandatory parameters: |
| 272 | \code |
| 273 | param.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK; |
| 274 | param.rec_id = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV; |
| 275 | param.play_id = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV; |
| 276 | param.clock_rate = 8000; |
| 277 | param.channel_count = 1; |
| 278 | param.samples_per_frame = 160; |
| 279 | param.bits_per_sample = 16; |
| 280 | \endcode\n |
| 281 | -# If you want the audio stream to use the device's built-in codec, specify |
| 282 | the codec in the #pjmedia_aud_param. You must make sure that the codec |
| 283 | is supported by the device, by looking at its supported format list in |
| 284 | the #pjmedia_aud_dev_info.\n |
| 285 | The snippet below sets the audio stream to use G.711 ULAW encoding: |
| 286 | \code |
| 287 | unsigned i; |
| 288 | |
| 289 | // Make sure Ulaw is supported |
| 290 | if ((info.caps & PJMEDIA_AUD_DEV_CAP_EXT_FORMAT) == 0) |
| 291 | error("Device does not support extended formats"); |
| 292 | for (i = 0; i < info.ext_fmt_cnt; ++i) { |
| 293 | if (info.ext_fmt[i].id == PJMEDIA_FORMAT_ULAW) |
| 294 | break; |
| 295 | } |
| 296 | if (i == info.ext_fmt_cnt) |
| 297 | error("Device does not support Ulaw format"); |
| 298 | |
| 299 | // Set Ulaw format |
| 300 | param.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT; |
| 301 | param.ext_fmt.id = PJMEDIA_FORMAT_ULAW; |
| 302 | param.ext_fmt.bitrate = 64000; |
| 303 | param.ext_fmt.vad = PJ_FALSE; |
| 304 | \endcode\n |
| 305 | -# Note that if non-PCM format is configured on the audio stream, the |
| 306 | capture and/or playback functions (#pjmedia_aud_rec_cb and |
| 307 | #pjmedia_aud_play_cb respectively) will report the audio frame as |
| 308 | #pjmedia_frame_ext structure instead of the #pjmedia_frame. |
| 309 | -# Optionally configure other device's capabilities. The following snippet |
| 310 | shows how to enable echo cancellation on the device (note that this |
| 311 | snippet may not be necessary since the setting may have been enabled |
| 312 | when calling #pjmedia_aud_dev_default_param() above): |
| 313 | \code |
| 314 | if (info.caps & PJMEDIA_AUD_DEV_CAP_EC) { |
| 315 | param.flags |= PJMEDIA_AUD_DEV_CAP_EC; |
| 316 | param.ec_enabled = PJ_TRUE; |
| 317 | } |
| 318 | \endcode |
| 319 | -# Open the audio stream, specifying the capture and/or playback callback |
| 320 | functions: |
| 321 | \code |
| 322 | pjmedia_aud_stream *stream; |
| 323 | |
| 324 | status = pjmedia_aud_stream_create(¶m, &rec_cb, &play_cb, |
| 325 | user_data, &stream); |
| 326 | \endcode |
| 327 | |
| 328 | @subsection working_with_stream Working with audio streams |
| 329 | |
| 330 | -# To start the audio stream: |
| 331 | \code |
| 332 | status = pjmedia_aud_stream_start(stream); |
| 333 | \endcode\n |
| 334 | To stop the stream: |
| 335 | \code |
| 336 | status = pjmedia_aud_stream_stop(stream); |
| 337 | \endcode\n |
| 338 | And to destroy the stream: |
| 339 | \code |
| 340 | status = pjmedia_aud_stream_destroy(stream); |
| 341 | \endcode\n |
| 342 | -# Info: The following shows how to retrieve the capability value of the |
| 343 | stream (in this case, the current output volume setting). |
| 344 | \code |
| 345 | // Volume setting is an unsigned integer showing the level in percent. |
| 346 | unsigned vol; |
| 347 | status = pjmedia_aud_stream_get_cap(stream, |
| 348 | PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, |
| 349 | &vol); |
| 350 | \endcode |
| 351 | -# Info: And following shows how to modify the capability value of the |
| 352 | stream (in this case, the current output volume setting). |
| 353 | \code |
| 354 | // Volume setting is an unsigned integer showing the level in percent. |
| 355 | unsigned vol = 50; |
| 356 | status = pjmedia_aud_stream_set_cap(stream, |
| 357 | PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING, |
| 358 | &vol); |
| 359 | \endcode |
| 360 | |
| 361 | |
| 362 | */ |
| 363 | |
Benny Prijono | 2058f47 | 2009-02-22 17:15:34 +0000 | [diff] [blame] | 364 | |
| 365 | /** |
| 366 | * @} |
| 367 | */ |
| 368 | |