config.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_AUDIODEV_CONFIG_H__
  20. #define __PJMEDIA_AUDIODEV_CONFIG_H__
  21. /**
  22. * @file config.h
  23. * @brief Audio config.
  24. */
  25. #include <pjmedia/types.h>
  26. #include <pj/pool.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup audio_device_api Audio Device API
  30. * @brief PJMEDIA audio device abstraction API.
  31. */
  32. /**
  33. * @defgroup s1_audio_device_config Compile time configurations
  34. * @ingroup audio_device_api
  35. * @brief Compile time configurations
  36. * @{
  37. */
  38. /**
  39. * This setting controls the buffer length of audio device name.
  40. *
  41. * Default: 128 for Windows platforms, 64 for others
  42. */
  43. #ifndef PJMEDIA_AUD_DEV_INFO_NAME_LEN
  44. # if (defined(PJ_WIN32) && PJ_WIN32!=0) || \
  45. (defined(PJ_WIN64) && PJ_WIN64!=0)
  46. # define PJMEDIA_AUD_DEV_INFO_NAME_LEN 128
  47. # else
  48. # define PJMEDIA_AUD_DEV_INFO_NAME_LEN 64
  49. # endif
  50. #endif
  51. /**
  52. * This setting controls whether PortAudio support should be included.
  53. *
  54. * By default it is disabled.
  55. */
  56. #ifndef PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO
  57. # define PJMEDIA_AUDIO_DEV_HAS_PORTAUDIO 0
  58. #endif
  59. /**
  60. * This setting controls whether Android OpenSL audio support should be
  61. * included.
  62. */
  63. #ifndef PJMEDIA_AUDIO_DEV_HAS_OPENSL
  64. # define PJMEDIA_AUDIO_DEV_HAS_OPENSL 0
  65. #endif
  66. /**
  67. * This setting controls whether Android JNI audio support should be
  68. * included.
  69. */
  70. #ifndef PJMEDIA_AUDIO_DEV_HAS_ANDROID_JNI
  71. # define PJMEDIA_AUDIO_DEV_HAS_ANDROID_JNI PJ_ANDROID
  72. #endif
  73. /**
  74. * This setting controls whether Android Oboe audio support should be
  75. * included.
  76. */
  77. #ifndef PJMEDIA_AUDIO_DEV_HAS_OBOE
  78. # define PJMEDIA_AUDIO_DEV_HAS_OBOE 0
  79. #endif
  80. /**
  81. * This setting controls whether BlackBerry 10 (BB10) audio support
  82. * should be included.
  83. */
  84. #ifndef PJMEDIA_AUDIO_DEV_HAS_BB10
  85. # define PJMEDIA_AUDIO_DEV_HAS_BB10 0
  86. #endif
  87. /**
  88. * This setting controls whether native ALSA support should be included.
  89. */
  90. #ifndef PJMEDIA_AUDIO_DEV_HAS_ALSA
  91. # define PJMEDIA_AUDIO_DEV_HAS_ALSA 0
  92. #endif
  93. /**
  94. * This setting controls whether null audio support should be included.
  95. */
  96. #ifndef PJMEDIA_AUDIO_DEV_HAS_NULL_AUDIO
  97. # define PJMEDIA_AUDIO_DEV_HAS_NULL_AUDIO 0
  98. #endif
  99. /**
  100. * This setting controls whether coreaudio support should be included.
  101. */
  102. #ifndef PJMEDIA_AUDIO_DEV_HAS_COREAUDIO
  103. # define PJMEDIA_AUDIO_DEV_HAS_COREAUDIO 0
  104. #endif
  105. /**
  106. * This setting controls whether WMME support should be included.
  107. */
  108. #ifndef PJMEDIA_AUDIO_DEV_HAS_WMME
  109. # if (defined(PJ_WIN32_UWP) && PJ_WIN32_UWP!=0) || \
  110. (defined(PJ_WIN32_WINPHONE8) && PJ_WIN32_WINPHONE8!=0)
  111. # define PJMEDIA_AUDIO_DEV_HAS_WMME 0
  112. # else
  113. # define PJMEDIA_AUDIO_DEV_HAS_WMME 1
  114. # endif
  115. #endif
  116. /**
  117. * This setting controls whether Windows Audio Session API (WASAPI)
  118. * support should be included.
  119. */
  120. #ifndef PJMEDIA_AUDIO_DEV_HAS_WASAPI
  121. # if (defined(PJ_WIN32_UWP) && PJ_WIN32_UWP!=0) || \
  122. (defined(PJ_WIN32_WINPHONE8) && PJ_WIN32_WINPHONE8!=0)
  123. # define PJMEDIA_AUDIO_DEV_HAS_WASAPI 1
  124. # else
  125. # define PJMEDIA_AUDIO_DEV_HAS_WASAPI 0
  126. # endif
  127. #endif
  128. /**
  129. * This setting controls whether BDIMAD support should be included.
  130. */
  131. #ifndef PJMEDIA_AUDIO_DEV_HAS_BDIMAD
  132. # define PJMEDIA_AUDIO_DEV_HAS_BDIMAD 0
  133. #endif
  134. /**
  135. * This setting controls whether Symbian APS support should be included.
  136. */
  137. #ifndef PJMEDIA_AUDIO_DEV_HAS_SYMB_APS
  138. # define PJMEDIA_AUDIO_DEV_HAS_SYMB_APS 0
  139. #endif
  140. /**
  141. * This setting controls whether Symbian APS should perform codec
  142. * detection in its factory initalization. Note that codec detection
  143. * may take few seconds and detecting more codecs will take more time.
  144. * Possible values are:
  145. * - 0: no codec detection, all APS codec (AMR-NB, G.711, G.729, and
  146. * iLBC) will be assumed as supported.
  147. * - 1: minimal codec detection, i.e: only detect for AMR-NB and G.711,
  148. * (G.729 and iLBC are considered to be supported/unsupported when
  149. * G.711 is supported/unsupported).
  150. * - 2: full codec detection, i.e: detect AMR-NB, G.711, G.729, and iLBC.
  151. *
  152. * Default: 1 (minimal codec detection)
  153. */
  154. #ifndef PJMEDIA_AUDIO_DEV_SYMB_APS_DETECTS_CODEC
  155. # define PJMEDIA_AUDIO_DEV_SYMB_APS_DETECTS_CODEC 1
  156. #endif
  157. /**
  158. * This setting controls whether Symbian VAS support should be included.
  159. */
  160. #ifndef PJMEDIA_AUDIO_DEV_HAS_SYMB_VAS
  161. # define PJMEDIA_AUDIO_DEV_HAS_SYMB_VAS 0
  162. #endif
  163. /**
  164. * This setting controls Symbian VAS version to be used. Currently, valid
  165. * values are only 1 (for VAS 1.0) and 2 (for VAS 2.0).
  166. *
  167. * Default: 1 (VAS version 1.0)
  168. */
  169. #ifndef PJMEDIA_AUDIO_DEV_SYMB_VAS_VERSION
  170. # define PJMEDIA_AUDIO_DEV_SYMB_VAS_VERSION 1
  171. #endif
  172. /**
  173. * This setting controls whether Symbian audio (using built-in multimedia
  174. * framework) support should be included.
  175. */
  176. #ifndef PJMEDIA_AUDIO_DEV_HAS_SYMB_MDA
  177. # define PJMEDIA_AUDIO_DEV_HAS_SYMB_MDA PJ_SYMBIAN
  178. #endif
  179. /**
  180. * This setting controls whether the Symbian audio with built-in multimedia
  181. * framework backend should be started synchronously. Note that synchronous
  182. * start will block the application/UI, e.g: about 40ms for each direction
  183. * on N95. While asynchronous start may cause invalid value (always zero)
  184. * returned in input/output volume query, if the query is performed when
  185. * the internal start procedure is not completely finished.
  186. *
  187. * Default: 1 (yes)
  188. */
  189. #ifndef PJMEDIA_AUDIO_DEV_MDA_USE_SYNC_START
  190. # define PJMEDIA_AUDIO_DEV_MDA_USE_SYNC_START 1
  191. #endif
  192. /**
  193. * This setting controls whether the Audio Device API should support
  194. * device implementation that is based on the old sound device API
  195. * (sound.h).
  196. *
  197. * Enable this API if:
  198. * - you have implemented your own sound device using the old sound
  199. * device API (sound.h), and
  200. * - you wish to be able to use your sound device implementation
  201. * using the new Audio Device API.
  202. *
  203. * Please see http://trac.pjsip.org/repos/wiki/Audio_Dev_API for more
  204. * info.
  205. */
  206. #ifndef PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE
  207. # define PJMEDIA_AUDIO_DEV_HAS_LEGACY_DEVICE 0
  208. #endif
  209. /**
  210. * @}
  211. */
  212. PJ_END_DECL
  213. #endif /* __PJMEDIA_AUDIODEV_CONFIG_H__ */
  214. /*
  215. --------------------- DOCUMENTATION FOLLOWS ---------------------------
  216. */
  217. /**
  218. * @addtogroup audio_device_api Audio Device API
  219. * @{
  220. PJMEDIA Audio Device API is a cross-platform audio API appropriate for use with
  221. VoIP applications and many other types of audio streaming applications.
  222. The API abstracts many different audio API's on various platforms, such as:
  223. - WMME audio for Windows and Windows Mobile devices
  224. - Windows Audio Session API (WASAPI)
  225. - CoreAudio for Mac and iPhone
  226. - ALSA for Linux
  227. - Android OpenSL
  228. - Android JNI
  229. - Android Oboe
  230. - PortAudio for Windows, Windows Mobile, Linux, Unix, dan MacOS X.
  231. - BDIMAP
  232. - Blackberry BB10
  233. - Symbian audio streaming/multimedia framework (MMF) implementation
  234. - Nokia Audio Proxy Server (APS) implementation
  235. - null-audio implementation
  236. - and more to be implemented in the future
  237. The Audio Device API/library is an evolution from PJMEDIA @ref PJMED_SND and
  238. contains many enhancements:
  239. - Forward compatibility:
  240. \n
  241. The new API has been designed to be extensible, it will support new API's as
  242. well as new features that may be introduced in the future without breaking
  243. compatibility with applications that use this API as well as compatibility
  244. with existing device implementations.
  245. - Device capabilities:
  246. \n
  247. At the heart of the API is device capabilities management, where all possible
  248. audio capabilities of audio devices should be able to be handled in a generic
  249. manner. With this framework, new capabilities that may be discovered in the
  250. future can be handled in manner without breaking existing applications.
  251. - Built-in features:
  252. \n
  253. The device capabilities framework enables applications to use and control
  254. audio features built-in in the device, such as:
  255. - echo cancellation,
  256. - built-in codecs,
  257. - audio routing (e.g. to earpiece or loudspeaker),
  258. - volume control,
  259. - etc.
  260. - Codec support:
  261. \n
  262. Some audio devices such as Nokia/Symbian Audio Proxy Server (APS) and Nokia
  263. VoIP Audio Services (VAS) support built-in hardware audio codecs (e.g. G.729,
  264. iLBC, and AMR), and application can use the sound device in encoded mode to
  265. make use of these hardware codecs.
  266. - Multiple backends:
  267. \n
  268. The new API supports multiple audio backends (called factories or drivers in
  269. the code) to be active simultaneously, and audio backends may be added or
  270. removed during run-time.
  271. @section using Overview on using the API
  272. @subsection getting_started Getting started
  273. -# <b>Configure the application's project settings</b>.\n
  274. Add the following
  275. include:
  276. \code
  277. #include <pjmedia_audiodev.h>\endcode\n
  278. And add <b>pjmedia-audiodev</b> library to your application link
  279. specifications.\n
  280. -# <b>Compile time settings</b>.\n
  281. Use the compile time settings to enable or
  282. disable specific audio drivers. For more information, please see
  283. \ref s1_audio_device_config.
  284. -# <b>API initialization and cleaning up</b>.\n
  285. Before anything else, application must initialize the API by calling:
  286. \code
  287. pjmedia_aud_subsys_init(pf);\endcode\n
  288. And add this in the application cleanup sequence
  289. \code
  290. pjmedia_aud_subsys_shutdown();\endcode
  291. @subsection devices Working with devices
  292. -# The following code prints the list of audio devices detected
  293. in the system.
  294. \code
  295. int dev_count;
  296. pjmedia_aud_dev_index dev_idx;
  297. pj_status_t status;
  298. dev_count = pjmedia_aud_dev_count();
  299. printf("Got %d audio devices\n", dev_count);
  300. for (dev_idx=0; dev_idx<dev_count; ++i) {
  301. pjmedia_aud_dev_info info;
  302. status = pjmedia_aud_dev_get_info(dev_idx, &info);
  303. printf("%d. %s (in=%d, out=%d)\n",
  304. dev_idx, info.name,
  305. info.input_count, info.output_count);
  306. }
  307. \endcode\n
  308. -# Info: The #PJMEDIA_AUD_DEFAULT_CAPTURE_DEV and #PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV
  309. constants are used to denote default capture and playback devices
  310. respectively.
  311. -# Info: You may save the device and driver's name in your application
  312. setting, for example to specify the prefered devices to be
  313. used by your application. You can then retrieve the device index
  314. for the device by calling:
  315. \code
  316. const char *drv_name = "WMME";
  317. const char *dev_name = "Wave mapper";
  318. pjmedia_aud_dev_index dev_idx;
  319. status = pjmedia_aud_dev_lookup(drv_name, dev_name, &dev_idx);
  320. if (status==PJ_SUCCESS)
  321. printf("Device index is %d\n", dev_idx);
  322. \endcode
  323. @subsection caps Device capabilities
  324. Capabilities are encoded as #pjmedia_aud_dev_cap enumeration. Please see
  325. #pjmedia_aud_dev_cap enumeration for more information.
  326. -# The following snippet prints the capabilities supported by the device:
  327. \code
  328. pjmedia_aud_dev_info info;
  329. pj_status_t status;
  330. status = pjmedia_aud_dev_get_info(PJMEDIA_AUD_DEFAULT_CAPTURE_DEV, &info);
  331. if (status == PJ_SUCCESS) {
  332. unsigned i;
  333. // Enumerate capability bits
  334. printf("Device capabilities: ");
  335. for (i=0; i<32; ++i) {
  336. if (info.caps & (1 << i))
  337. printf("%s ", pjmedia_aud_dev_cap_name(1 << i, NULL));
  338. }
  339. }
  340. \endcode\n
  341. -# Info: You can set the device settings when opening audio stream by setting
  342. the flags and the appropriate setting in #pjmedia_aud_param when calling
  343. #pjmedia_aud_stream_create()\n
  344. -# Info: Once the audio stream is running, you can retrieve or change the stream
  345. setting by specifying the capability in #pjmedia_aud_stream_get_cap()
  346. and #pjmedia_aud_stream_set_cap() respectively.
  347. @subsection creating_stream Creating audio streams
  348. The audio stream enables audio streaming to capture device, playback device,
  349. or both.
  350. -# It is recommended to initialize the #pjmedia_aud_param with its default
  351. values before using it:
  352. \code
  353. pjmedia_aud_param param;
  354. pjmedia_aud_dev_index dev_idx;
  355. pj_status_t status;
  356. dev_idx = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
  357. status = pjmedia_aud_dev_default_param(dev_idx, &param);
  358. \endcode\n
  359. -# Configure the mandatory parameters:
  360. \code
  361. param.dir = PJMEDIA_DIR_CAPTURE_PLAYBACK;
  362. param.rec_id = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
  363. param.play_id = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
  364. param.clock_rate = 8000;
  365. param.channel_count = 1;
  366. param.samples_per_frame = 160;
  367. param.bits_per_sample = 16;
  368. \endcode\n
  369. -# If you want the audio stream to use the device's built-in codec, specify
  370. the codec in the #pjmedia_aud_param. You must make sure that the codec
  371. is supported by the device, by looking at its supported format list in
  372. the #pjmedia_aud_dev_info.\n
  373. The snippet below sets the audio stream to use G.711 ULAW encoding:
  374. \code
  375. unsigned i;
  376. // Make sure Ulaw is supported
  377. if ((info.caps & PJMEDIA_AUD_DEV_CAP_EXT_FORMAT) == 0)
  378. error("Device does not support extended formats");
  379. for (i = 0; i < info.ext_fmt_cnt; ++i) {
  380. if (info.ext_fmt[i].id == PJMEDIA_FORMAT_ULAW)
  381. break;
  382. }
  383. if (i == info.ext_fmt_cnt)
  384. error("Device does not support Ulaw format");
  385. // Set Ulaw format
  386. param.flags |= PJMEDIA_AUD_DEV_CAP_EXT_FORMAT;
  387. param.ext_fmt.id = PJMEDIA_FORMAT_ULAW;
  388. param.ext_fmt.bitrate = 64000;
  389. param.ext_fmt.vad = PJ_FALSE;
  390. \endcode\n
  391. -# Note that if non-PCM format is configured on the audio stream, the
  392. capture and/or playback functions (#pjmedia_aud_rec_cb and
  393. #pjmedia_aud_play_cb respectively) will report the audio frame as
  394. #pjmedia_frame_ext structure instead of the #pjmedia_frame.
  395. -# Optionally configure other device's capabilities. The following snippet
  396. shows how to enable echo cancellation on the device (note that this
  397. snippet may not be necessary since the setting may have been enabled
  398. when calling #pjmedia_aud_dev_default_param() above):
  399. \code
  400. if (info.caps & PJMEDIA_AUD_DEV_CAP_EC) {
  401. param.flags |= PJMEDIA_AUD_DEV_CAP_EC;
  402. param.ec_enabled = PJ_TRUE;
  403. }
  404. \endcode
  405. -# Open the audio stream, specifying the capture and/or playback callback
  406. functions:
  407. \code
  408. pjmedia_aud_stream *stream;
  409. status = pjmedia_aud_stream_create(&param, &rec_cb, &play_cb,
  410. user_data, &stream);
  411. \endcode
  412. @subsection working_with_stream Working with audio streams
  413. -# To start the audio stream:
  414. \code
  415. status = pjmedia_aud_stream_start(stream);
  416. \endcode\n
  417. To stop the stream:
  418. \code
  419. status = pjmedia_aud_stream_stop(stream);
  420. \endcode\n
  421. And to destroy the stream:
  422. \code
  423. status = pjmedia_aud_stream_destroy(stream);
  424. \endcode\n
  425. -# Info: The following shows how to retrieve the capability value of the
  426. stream (in this case, the current output volume setting).
  427. \code
  428. // Volume setting is an unsigned integer showing the level in percent.
  429. unsigned vol;
  430. status = pjmedia_aud_stream_get_cap(stream,
  431. PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
  432. &vol);
  433. \endcode
  434. -# Info: And following shows how to modify the capability value of the
  435. stream (in this case, the current output volume setting).
  436. \code
  437. // Volume setting is an unsigned integer showing the level in percent.
  438. unsigned vol = 50;
  439. status = pjmedia_aud_stream_set_cap(stream,
  440. PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
  441. &vol);
  442. \endcode
  443. */
  444. /**
  445. * @}
  446. */