/PlaceHolderTTSEngine/native/project/jni/media/AudioSystem.h

http://eyes-free.googlecode.com/ · C Header · 482 lines · 317 code · 70 blank · 95 comment · 0 complexity · dc18c8d00f53c1e9f432c1a8022193df MD5 · raw file

  1. /*
  2. * Copied from frameworks/base/media/AudioSystems.h in the Android sources.
  3. * Modified by commenting out parts that we don't need
  4. * (especially dependencies on other header files).
  5. */
  6. /*
  7. * Copyright (C) 2008 The Android Open Source Project
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #ifndef ANDROID_AUDIOSYSTEM_H_
  22. #define ANDROID_AUDIOSYSTEM_H_
  23. #if 0
  24. #include <utils/RefBase.h>
  25. #include <utils/threads.h>
  26. #include <media/IAudioFlinger.h>
  27. #endif
  28. namespace android {
  29. class AudioSystem
  30. {
  31. public:
  32. enum stream_type {
  33. DEFAULT =-1,
  34. VOICE_CALL = 0,
  35. SYSTEM = 1,
  36. RING = 2,
  37. MUSIC = 3,
  38. ALARM = 4,
  39. NOTIFICATION = 5,
  40. BLUETOOTH_SCO = 6,
  41. ENFORCED_AUDIBLE = 7, // Sounds that cannot be muted by user and must be routed to speaker
  42. DTMF = 8,
  43. TTS = 9,
  44. NUM_STREAM_TYPES
  45. };
  46. // Audio sub formats (see AudioSystem::audio_format).
  47. enum pcm_sub_format {
  48. PCM_SUB_16_BIT = 0x1, // must be 1 for backward compatibility
  49. PCM_SUB_8_BIT = 0x2, // must be 2 for backward compatibility
  50. };
  51. // MP3 sub format field definition : can use 11 LSBs in the same way as MP3 frame header to specify
  52. // bit rate, stereo mode, version...
  53. enum mp3_sub_format {
  54. //TODO
  55. };
  56. // AMR NB/WB sub format field definition: specify frame block interleaving, bandwidth efficient or octet aligned,
  57. // encoding mode for recording...
  58. enum amr_sub_format {
  59. //TODO
  60. };
  61. // AAC sub format field definition: specify profile or bitrate for recording...
  62. enum aac_sub_format {
  63. //TODO
  64. };
  65. // VORBIS sub format field definition: specify quality for recording...
  66. enum vorbis_sub_format {
  67. //TODO
  68. };
  69. // Audio format consists in a main format field (upper 8 bits) and a sub format field (lower 24 bits).
  70. // The main format indicates the main codec type. The sub format field indicates options and parameters
  71. // for each format. The sub format is mainly used for record to indicate for instance the requested bitrate
  72. // or profile. It can also be used for certain formats to give informations not present in the encoded
  73. // audio stream (e.g. octet alignement for AMR).
  74. enum audio_format {
  75. INVALID_FORMAT = -1,
  76. FORMAT_DEFAULT = 0,
  77. PCM = 0x00000000, // must be 0 for backward compatibility
  78. MP3 = 0x01000000,
  79. AMR_NB = 0x02000000,
  80. AMR_WB = 0x03000000,
  81. AAC = 0x04000000,
  82. HE_AAC_V1 = 0x05000000,
  83. HE_AAC_V2 = 0x06000000,
  84. VORBIS = 0x07000000,
  85. MAIN_FORMAT_MASK = 0xFF000000,
  86. SUB_FORMAT_MASK = 0x00FFFFFF,
  87. // Aliases
  88. PCM_16_BIT = (PCM|PCM_SUB_16_BIT),
  89. PCM_8_BIT = (PCM|PCM_SUB_8_BIT)
  90. };
  91. // Channel mask definitions must be kept in sync with JAVA values in /media/java/android/media/AudioFormat.java
  92. enum audio_channels {
  93. // output channels
  94. CHANNEL_OUT_FRONT_LEFT = 0x4,
  95. CHANNEL_OUT_FRONT_RIGHT = 0x8,
  96. CHANNEL_OUT_FRONT_CENTER = 0x10,
  97. CHANNEL_OUT_LOW_FREQUENCY = 0x20,
  98. CHANNEL_OUT_BACK_LEFT = 0x40,
  99. CHANNEL_OUT_BACK_RIGHT = 0x80,
  100. CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100,
  101. CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200,
  102. CHANNEL_OUT_BACK_CENTER = 0x400,
  103. CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT,
  104. CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT),
  105. CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
  106. CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT),
  107. CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
  108. CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER),
  109. CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
  110. CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT),
  111. CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
  112. CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
  113. CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER),
  114. CHANNEL_OUT_ALL = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
  115. CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
  116. CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER | CHANNEL_OUT_BACK_CENTER),
  117. // input channels
  118. CHANNEL_IN_LEFT = 0x4,
  119. CHANNEL_IN_RIGHT = 0x8,
  120. CHANNEL_IN_FRONT = 0x10,
  121. CHANNEL_IN_BACK = 0x20,
  122. CHANNEL_IN_LEFT_PROCESSED = 0x40,
  123. CHANNEL_IN_RIGHT_PROCESSED = 0x80,
  124. CHANNEL_IN_FRONT_PROCESSED = 0x100,
  125. CHANNEL_IN_BACK_PROCESSED = 0x200,
  126. CHANNEL_IN_PRESSURE = 0x400,
  127. CHANNEL_IN_X_AXIS = 0x800,
  128. CHANNEL_IN_Y_AXIS = 0x1000,
  129. CHANNEL_IN_Z_AXIS = 0x2000,
  130. CHANNEL_IN_VOICE_UPLINK = 0x4000,
  131. CHANNEL_IN_VOICE_DNLINK = 0x8000,
  132. CHANNEL_IN_MONO = CHANNEL_IN_FRONT,
  133. CHANNEL_IN_STEREO = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT),
  134. CHANNEL_IN_ALL = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT | CHANNEL_IN_FRONT | CHANNEL_IN_BACK|
  135. CHANNEL_IN_LEFT_PROCESSED | CHANNEL_IN_RIGHT_PROCESSED | CHANNEL_IN_FRONT_PROCESSED | CHANNEL_IN_BACK_PROCESSED|
  136. CHANNEL_IN_PRESSURE | CHANNEL_IN_X_AXIS | CHANNEL_IN_Y_AXIS | CHANNEL_IN_Z_AXIS |
  137. CHANNEL_IN_VOICE_UPLINK | CHANNEL_IN_VOICE_DNLINK)
  138. };
  139. enum audio_mode {
  140. MODE_INVALID = -2,
  141. MODE_CURRENT = -1,
  142. MODE_NORMAL = 0,
  143. MODE_RINGTONE,
  144. MODE_IN_CALL,
  145. NUM_MODES // not a valid entry, denotes end-of-list
  146. };
  147. enum audio_in_acoustics {
  148. AGC_ENABLE = 0x0001,
  149. AGC_DISABLE = 0,
  150. NS_ENABLE = 0x0002,
  151. NS_DISABLE = 0,
  152. TX_IIR_ENABLE = 0x0004,
  153. TX_DISABLE = 0
  154. };
  155. #if 0
  156. /* These are static methods to control the system-wide AudioFlinger
  157. * only privileged processes can have access to them
  158. */
  159. // mute/unmute microphone
  160. static status_t muteMicrophone(bool state);
  161. static status_t isMicrophoneMuted(bool *state);
  162. // set/get master volume
  163. static status_t setMasterVolume(float value);
  164. static status_t getMasterVolume(float* volume);
  165. // mute/unmute audio outputs
  166. static status_t setMasterMute(bool mute);
  167. static status_t getMasterMute(bool* mute);
  168. // set/get stream volume on specified output
  169. static status_t setStreamVolume(int stream, float value, int output);
  170. static status_t getStreamVolume(int stream, float* volume, int output);
  171. // mute/unmute stream
  172. static status_t setStreamMute(int stream, bool mute);
  173. static status_t getStreamMute(int stream, bool* mute);
  174. // set audio mode in audio hardware (see AudioSystem::audio_mode)
  175. static status_t setMode(int mode);
  176. // returns true in *state if tracks are active on the specified stream
  177. static status_t isStreamActive(int stream, bool *state);
  178. // set/get audio hardware parameters. The function accepts a list of parameters
  179. // key value pairs in the form: key1=value1;key2=value2;...
  180. // Some keys are reserved for standard parameters (See AudioParameter class).
  181. static status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
  182. static String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
  183. static void setErrorCallback(audio_error_callback cb);
  184. // helper function to obtain AudioFlinger service handle
  185. static const sp<IAudioFlinger>& get_audio_flinger();
  186. static float linearToLog(int volume);
  187. static int logToLinear(float volume);
  188. static status_t getOutputSamplingRate(int* samplingRate, int stream = DEFAULT);
  189. static status_t getOutputFrameCount(int* frameCount, int stream = DEFAULT);
  190. static status_t getOutputLatency(uint32_t* latency, int stream = DEFAULT);
  191. static bool routedToA2dpOutput(int streamType);
  192. static status_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
  193. size_t* buffSize);
  194. static status_t setVoiceVolume(float volume);
  195. // return the number of audio frames written by AudioFlinger to audio HAL and
  196. // audio dsp to DAC since the output on which the specificed stream is playing
  197. // has exited standby.
  198. // returned status (from utils/Errors.h) can be:
  199. // - NO_ERROR: successful operation, halFrames and dspFrames point to valid data
  200. // - INVALID_OPERATION: Not supported on current hardware platform
  201. // - BAD_VALUE: invalid parameter
  202. // NOTE: this feature is not supported on all hardware platforms and it is
  203. // necessary to check returned status before using the returned values.
  204. static status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int stream = DEFAULT);
  205. //
  206. // AudioPolicyService interface
  207. //
  208. enum audio_devices {
  209. // output devices
  210. DEVICE_OUT_EARPIECE = 0x1,
  211. DEVICE_OUT_SPEAKER = 0x2,
  212. DEVICE_OUT_WIRED_HEADSET = 0x4,
  213. DEVICE_OUT_WIRED_HEADPHONE = 0x8,
  214. DEVICE_OUT_BLUETOOTH_SCO = 0x10,
  215. DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20,
  216. DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40,
  217. DEVICE_OUT_BLUETOOTH_A2DP = 0x80,
  218. DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100,
  219. DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200,
  220. DEVICE_OUT_AUX_DIGITAL = 0x400,
  221. DEVICE_OUT_DEFAULT = 0x8000,
  222. DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE | DEVICE_OUT_SPEAKER | DEVICE_OUT_WIRED_HEADSET |
  223. DEVICE_OUT_WIRED_HEADPHONE | DEVICE_OUT_BLUETOOTH_SCO | DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
  224. DEVICE_OUT_BLUETOOTH_SCO_CARKIT | DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
  225. DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER | DEVICE_OUT_AUX_DIGITAL | DEVICE_OUT_DEFAULT),
  226. DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
  227. DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER),
  228. // input devices
  229. DEVICE_IN_COMMUNICATION = 0x10000,
  230. DEVICE_IN_AMBIENT = 0x20000,
  231. DEVICE_IN_BUILTIN_MIC = 0x40000,
  232. DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x80000,
  233. DEVICE_IN_WIRED_HEADSET = 0x100000,
  234. DEVICE_IN_AUX_DIGITAL = 0x200000,
  235. DEVICE_IN_VOICE_CALL = 0x400000,
  236. DEVICE_IN_BACK_MIC = 0x800000,
  237. DEVICE_IN_DEFAULT = 0x80000000,
  238. DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION | DEVICE_IN_AMBIENT | DEVICE_IN_BUILTIN_MIC |
  239. DEVICE_IN_BLUETOOTH_SCO_HEADSET | DEVICE_IN_WIRED_HEADSET | DEVICE_IN_AUX_DIGITAL |
  240. DEVICE_IN_VOICE_CALL | DEVICE_IN_BACK_MIC | DEVICE_IN_DEFAULT)
  241. };
  242. // device connection states used for setDeviceConnectionState()
  243. enum device_connection_state {
  244. DEVICE_STATE_UNAVAILABLE,
  245. DEVICE_STATE_AVAILABLE,
  246. NUM_DEVICE_STATES
  247. };
  248. // request to open a direct output with getOutput() (by opposition to sharing an output with other AudioTracks)
  249. enum output_flags {
  250. OUTPUT_FLAG_INDIRECT = 0x0,
  251. OUTPUT_FLAG_DIRECT = 0x1
  252. };
  253. // device categories used for setForceUse()
  254. enum forced_config {
  255. FORCE_NONE,
  256. FORCE_SPEAKER,
  257. FORCE_HEADPHONES,
  258. FORCE_BT_SCO,
  259. FORCE_BT_A2DP,
  260. FORCE_WIRED_ACCESSORY,
  261. FORCE_BT_CAR_DOCK,
  262. FORCE_BT_DESK_DOCK,
  263. NUM_FORCE_CONFIG,
  264. FORCE_DEFAULT = FORCE_NONE
  265. };
  266. // usages used for setForceUse()
  267. enum force_use {
  268. FOR_COMMUNICATION,
  269. FOR_MEDIA,
  270. FOR_RECORD,
  271. FOR_DOCK,
  272. NUM_FORCE_USE
  273. };
  274. // types of io configuration change events received with ioConfigChanged()
  275. enum io_config_event {
  276. OUTPUT_OPENED,
  277. OUTPUT_CLOSED,
  278. OUTPUT_CONFIG_CHANGED,
  279. INPUT_OPENED,
  280. INPUT_CLOSED,
  281. INPUT_CONFIG_CHANGED,
  282. STREAM_CONFIG_CHANGED,
  283. NUM_CONFIG_EVENTS
  284. };
  285. // audio output descritor used to cache output configurations in client process to avoid frequent calls
  286. // through IAudioFlinger
  287. class OutputDescriptor {
  288. public:
  289. OutputDescriptor()
  290. : samplingRate(0), format(0), channels(0), frameCount(0), latency(0) {}
  291. uint32_t samplingRate;
  292. int32_t format;
  293. int32_t channels;
  294. size_t frameCount;
  295. uint32_t latency;
  296. };
  297. //
  298. // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions)
  299. //
  300. static status_t setDeviceConnectionState(audio_devices device, device_connection_state state, const char *device_address);
  301. static device_connection_state getDeviceConnectionState(audio_devices device, const char *device_address);
  302. static status_t setPhoneState(int state);
  303. static status_t setRingerMode(uint32_t mode, uint32_t mask);
  304. static status_t setForceUse(force_use usage, forced_config config);
  305. static forced_config getForceUse(force_use usage);
  306. static audio_io_handle_t getOutput(stream_type stream,
  307. uint32_t samplingRate = 0,
  308. uint32_t format = FORMAT_DEFAULT,
  309. uint32_t channels = CHANNEL_OUT_STEREO,
  310. output_flags flags = OUTPUT_FLAG_INDIRECT);
  311. static status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
  312. static status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
  313. static void releaseOutput(audio_io_handle_t output);
  314. static audio_io_handle_t getInput(int inputSource,
  315. uint32_t samplingRate = 0,
  316. uint32_t format = FORMAT_DEFAULT,
  317. uint32_t channels = CHANNEL_IN_MONO,
  318. audio_in_acoustics acoustics = (audio_in_acoustics)0);
  319. static status_t startInput(audio_io_handle_t input);
  320. static status_t stopInput(audio_io_handle_t input);
  321. static void releaseInput(audio_io_handle_t input);
  322. static status_t initStreamVolume(stream_type stream,
  323. int indexMin,
  324. int indexMax);
  325. static status_t setStreamVolumeIndex(stream_type stream, int index);
  326. static status_t getStreamVolumeIndex(stream_type stream, int *index);
  327. static const sp<IAudioPolicyService>& get_audio_policy_service();
  328. // ----------------------------------------------------------------------------
  329. static uint32_t popCount(uint32_t u);
  330. static bool isOutputDevice(audio_devices device);
  331. static bool isInputDevice(audio_devices device);
  332. static bool isA2dpDevice(audio_devices device);
  333. static bool isBluetoothScoDevice(audio_devices device);
  334. static bool isLowVisibility(stream_type stream);
  335. static bool isOutputChannel(uint32_t channel);
  336. static bool isInputChannel(uint32_t channel);
  337. static bool isValidFormat(uint32_t format);
  338. static bool isLinearPCM(uint32_t format);
  339. private:
  340. class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
  341. {
  342. public:
  343. AudioFlingerClient() {
  344. }
  345. // DeathRecipient
  346. virtual void binderDied(const wp<IBinder>& who);
  347. // IAudioFlingerClient
  348. // indicate a change in the configuration of an output or input: keeps the cached
  349. // values for output/input parameters upto date in client process
  350. virtual void ioConfigChanged(int event, int ioHandle, void *param2);
  351. };
  352. class AudioPolicyServiceClient: public IBinder::DeathRecipient
  353. {
  354. public:
  355. AudioPolicyServiceClient() {
  356. }
  357. // DeathRecipient
  358. virtual void binderDied(const wp<IBinder>& who);
  359. };
  360. static sp<AudioFlingerClient> gAudioFlingerClient;
  361. static sp<AudioPolicyServiceClient> gAudioPolicyServiceClient;
  362. friend class AudioFlingerClient;
  363. friend class AudioPolicyServiceClient;
  364. static Mutex gLock;
  365. static sp<IAudioFlinger> gAudioFlinger;
  366. static audio_error_callback gAudioErrorCallback;
  367. static size_t gInBuffSize;
  368. // previous parameters for recording buffer size queries
  369. static uint32_t gPrevInSamplingRate;
  370. static int gPrevInFormat;
  371. static int gPrevInChannelCount;
  372. static sp<IAudioPolicyService> gAudioPolicyService;
  373. // mapping between stream types and outputs
  374. static DefaultKeyedVector<int, audio_io_handle_t> gStreamOutputMap;
  375. // list of output descritor containing cached parameters (sampling rate, framecount, channel count...)
  376. static DefaultKeyedVector<audio_io_handle_t, OutputDescriptor *> gOutputs;
  377. };
  378. class AudioParameter {
  379. public:
  380. AudioParameter() {}
  381. AudioParameter(const String8& keyValuePairs);
  382. virtual ~AudioParameter();
  383. // reserved parameter keys for changeing standard parameters with setParameters() function.
  384. // Using these keys is mandatory for AudioFlinger to properly monitor audio output/input
  385. // configuration changes and act accordingly.
  386. // keyRouting: to change audio routing, value is an int in AudioSystem::audio_devices
  387. // keySamplingRate: to change sampling rate routing, value is an int
  388. // keyFormat: to change audio format, value is an int in AudioSystem::audio_format
  389. // keyChannels: to change audio channel configuration, value is an int in AudioSystem::audio_channels
  390. // keyFrameCount: to change audio output frame count, value is an int
  391. static const char *keyRouting;
  392. static const char *keySamplingRate;
  393. static const char *keyFormat;
  394. static const char *keyChannels;
  395. static const char *keyFrameCount;
  396. String8 toString();
  397. status_t add(const String8& key, const String8& value);
  398. status_t addInt(const String8& key, const int value);
  399. status_t addFloat(const String8& key, const float value);
  400. status_t remove(const String8& key);
  401. status_t get(const String8& key, String8& value);
  402. status_t getInt(const String8& key, int& value);
  403. status_t getFloat(const String8& key, float& value);
  404. status_t getAt(size_t index, String8& key, String8& value);
  405. size_t size() { return mParameters.size(); }
  406. private:
  407. String8 mKeyValuePairs;
  408. KeyedVector <String8, String8> mParameters;
  409. #endif
  410. };
  411. }; // namespace android
  412. #endif /*ANDROID_AUDIOSYSTEM_H_*/