PageRenderTime 23ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/android-headers-jbmr2/frameworks/av/include/media/AudioSystem.h

https://github.com/mstorsjo/vlc-android
C Header | 304 lines | 173 code | 54 blank | 77 comment | 0 complexity | c39560444d830ee343e51e4b74ffa26a MD5 | raw file
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_AUDIOSYSTEM_H_
  17. #define ANDROID_AUDIOSYSTEM_H_
  18. #include <utils/RefBase.h>
  19. #include <utils/threads.h>
  20. #include <media/IAudioFlinger.h>
  21. #include <system/audio.h>
  22. #include <system/audio_policy.h>
  23. /* XXX: Should be include by all the users instead */
  24. #include <media/AudioParameter.h>
  25. namespace android {
  26. typedef void (*audio_error_callback)(status_t err);
  27. class IAudioPolicyService;
  28. class String8;
  29. class AudioSystem
  30. {
  31. public:
  32. /* These are static methods to control the system-wide AudioFlinger
  33. * only privileged processes can have access to them
  34. */
  35. // mute/unmute microphone
  36. static status_t muteMicrophone(bool state);
  37. static status_t isMicrophoneMuted(bool *state);
  38. // set/get master volume
  39. static status_t setMasterVolume(float value);
  40. static status_t getMasterVolume(float* volume);
  41. // mute/unmute audio outputs
  42. static status_t setMasterMute(bool mute);
  43. static status_t getMasterMute(bool* mute);
  44. // set/get stream volume on specified output
  45. static status_t setStreamVolume(audio_stream_type_t stream, float value,
  46. audio_io_handle_t output);
  47. static status_t getStreamVolume(audio_stream_type_t stream, float* volume,
  48. audio_io_handle_t output);
  49. // mute/unmute stream
  50. static status_t setStreamMute(audio_stream_type_t stream, bool mute);
  51. static status_t getStreamMute(audio_stream_type_t stream, bool* mute);
  52. // set audio mode in audio hardware
  53. static status_t setMode(audio_mode_t mode);
  54. // returns true in *state if tracks are active on the specified stream or have been active
  55. // in the past inPastMs milliseconds
  56. static status_t isStreamActive(audio_stream_type_t stream, bool *state, uint32_t inPastMs = 0);
  57. // returns true in *state if tracks are active for what qualifies as remote playback
  58. // on the specified stream or have been active in the past inPastMs milliseconds. Remote
  59. // playback isn't mutually exclusive with local playback.
  60. static status_t isStreamActiveRemotely(audio_stream_type_t stream, bool *state,
  61. uint32_t inPastMs = 0);
  62. // returns true in *state if a recorder is currently recording with the specified source
  63. static status_t isSourceActive(audio_source_t source, bool *state);
  64. // set/get audio hardware parameters. The function accepts a list of parameters
  65. // key value pairs in the form: key1=value1;key2=value2;...
  66. // Some keys are reserved for standard parameters (See AudioParameter class).
  67. static status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
  68. static String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
  69. static void setErrorCallback(audio_error_callback cb);
  70. // helper function to obtain AudioFlinger service handle
  71. static const sp<IAudioFlinger>& get_audio_flinger();
  72. static float linearToLog(int volume);
  73. static int logToLinear(float volume);
  74. static status_t getOutputSamplingRate(uint32_t* samplingRate,
  75. audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
  76. static status_t getOutputFrameCount(size_t* frameCount,
  77. audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
  78. static status_t getOutputLatency(uint32_t* latency,
  79. audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
  80. static status_t getSamplingRate(audio_io_handle_t output,
  81. audio_stream_type_t streamType,
  82. uint32_t* samplingRate);
  83. // returns the number of frames per audio HAL write buffer. Corresponds to
  84. // audio_stream->get_buffer_size()/audio_stream_frame_size()
  85. static status_t getFrameCount(audio_io_handle_t output,
  86. audio_stream_type_t stream,
  87. size_t* frameCount);
  88. // returns the audio output stream latency in ms. Corresponds to
  89. // audio_stream_out->get_latency()
  90. static status_t getLatency(audio_io_handle_t output,
  91. audio_stream_type_t stream,
  92. uint32_t* latency);
  93. static bool routedToA2dpOutput(audio_stream_type_t streamType);
  94. static status_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
  95. audio_channel_mask_t channelMask, size_t* buffSize);
  96. static status_t setVoiceVolume(float volume);
  97. // return the number of audio frames written by AudioFlinger to audio HAL and
  98. // audio dsp to DAC since the output on which the specified stream is playing
  99. // has exited standby.
  100. // returned status (from utils/Errors.h) can be:
  101. // - NO_ERROR: successful operation, halFrames and dspFrames point to valid data
  102. // - INVALID_OPERATION: Not supported on current hardware platform
  103. // - BAD_VALUE: invalid parameter
  104. // NOTE: this feature is not supported on all hardware platforms and it is
  105. // necessary to check returned status before using the returned values.
  106. static status_t getRenderPosition(size_t *halFrames, size_t *dspFrames,
  107. audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
  108. // return the number of input frames lost by HAL implementation, or 0 if the handle is invalid
  109. static size_t getInputFramesLost(audio_io_handle_t ioHandle);
  110. static int newAudioSessionId();
  111. static void acquireAudioSessionId(int audioSession);
  112. static void releaseAudioSessionId(int audioSession);
  113. // types of io configuration change events received with ioConfigChanged()
  114. enum io_config_event {
  115. OUTPUT_OPENED,
  116. OUTPUT_CLOSED,
  117. OUTPUT_CONFIG_CHANGED,
  118. INPUT_OPENED,
  119. INPUT_CLOSED,
  120. INPUT_CONFIG_CHANGED,
  121. STREAM_CONFIG_CHANGED,
  122. NUM_CONFIG_EVENTS
  123. };
  124. // audio output descriptor used to cache output configurations in client process to avoid
  125. // frequent calls through IAudioFlinger
  126. class OutputDescriptor {
  127. public:
  128. OutputDescriptor()
  129. : samplingRate(0), format(AUDIO_FORMAT_DEFAULT), channels(0), frameCount(0), latency(0) {}
  130. uint32_t samplingRate;
  131. int32_t format;
  132. int32_t channels;
  133. size_t frameCount;
  134. uint32_t latency;
  135. };
  136. // Events used to synchronize actions between audio sessions.
  137. // For instance SYNC_EVENT_PRESENTATION_COMPLETE can be used to delay recording start until
  138. // playback is complete on another audio session.
  139. // See definitions in MediaSyncEvent.java
  140. enum sync_event_t {
  141. SYNC_EVENT_SAME = -1, // used internally to indicate restart with same event
  142. SYNC_EVENT_NONE = 0,
  143. SYNC_EVENT_PRESENTATION_COMPLETE,
  144. //
  145. // Define new events here: SYNC_EVENT_START, SYNC_EVENT_STOP, SYNC_EVENT_TIME ...
  146. //
  147. SYNC_EVENT_CNT,
  148. };
  149. // Timeout for synchronous record start. Prevents from blocking the record thread forever
  150. // if the trigger event is not fired.
  151. static const uint32_t kSyncRecordStartTimeOutMs = 30000;
  152. //
  153. // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions)
  154. //
  155. static status_t setDeviceConnectionState(audio_devices_t device, audio_policy_dev_state_t state,
  156. const char *device_address);
  157. static audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
  158. const char *device_address);
  159. static status_t setPhoneState(audio_mode_t state);
  160. static status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
  161. static audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
  162. static audio_io_handle_t getOutput(audio_stream_type_t stream,
  163. uint32_t samplingRate = 0,
  164. audio_format_t format = AUDIO_FORMAT_DEFAULT,
  165. audio_channel_mask_t channelMask = AUDIO_CHANNEL_OUT_STEREO,
  166. audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE);
  167. static status_t startOutput(audio_io_handle_t output,
  168. audio_stream_type_t stream,
  169. int session = 0);
  170. static status_t stopOutput(audio_io_handle_t output,
  171. audio_stream_type_t stream,
  172. int session = 0);
  173. static void releaseOutput(audio_io_handle_t output);
  174. static audio_io_handle_t getInput(audio_source_t inputSource,
  175. uint32_t samplingRate = 0,
  176. audio_format_t format = AUDIO_FORMAT_DEFAULT,
  177. audio_channel_mask_t channelMask = AUDIO_CHANNEL_IN_MONO,
  178. int sessionId = 0);
  179. static status_t startInput(audio_io_handle_t input);
  180. static status_t stopInput(audio_io_handle_t input);
  181. static void releaseInput(audio_io_handle_t input);
  182. static status_t initStreamVolume(audio_stream_type_t stream,
  183. int indexMin,
  184. int indexMax);
  185. static status_t setStreamVolumeIndex(audio_stream_type_t stream,
  186. int index,
  187. audio_devices_t device);
  188. static status_t getStreamVolumeIndex(audio_stream_type_t stream,
  189. int *index,
  190. audio_devices_t device);
  191. static uint32_t getStrategyForStream(audio_stream_type_t stream);
  192. static audio_devices_t getDevicesForStream(audio_stream_type_t stream);
  193. static audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
  194. static status_t registerEffect(const effect_descriptor_t *desc,
  195. audio_io_handle_t io,
  196. uint32_t strategy,
  197. int session,
  198. int id);
  199. static status_t unregisterEffect(int id);
  200. static status_t setEffectEnabled(int id, bool enabled);
  201. // clear stream to output mapping cache (gStreamOutputMap)
  202. // and output configuration cache (gOutputs)
  203. static void clearAudioConfigCache();
  204. static const sp<IAudioPolicyService>& get_audio_policy_service();
  205. // helpers for android.media.AudioManager.getProperty(), see description there for meaning
  206. static uint32_t getPrimaryOutputSamplingRate();
  207. static size_t getPrimaryOutputFrameCount();
  208. // ----------------------------------------------------------------------------
  209. private:
  210. class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
  211. {
  212. public:
  213. AudioFlingerClient() {
  214. }
  215. // DeathRecipient
  216. virtual void binderDied(const wp<IBinder>& who);
  217. // IAudioFlingerClient
  218. // indicate a change in the configuration of an output or input: keeps the cached
  219. // values for output/input parameters up-to-date in client process
  220. virtual void ioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2);
  221. };
  222. class AudioPolicyServiceClient: public IBinder::DeathRecipient
  223. {
  224. public:
  225. AudioPolicyServiceClient() {
  226. }
  227. // DeathRecipient
  228. virtual void binderDied(const wp<IBinder>& who);
  229. };
  230. static sp<AudioFlingerClient> gAudioFlingerClient;
  231. static sp<AudioPolicyServiceClient> gAudioPolicyServiceClient;
  232. friend class AudioFlingerClient;
  233. friend class AudioPolicyServiceClient;
  234. static Mutex gLock;
  235. static sp<IAudioFlinger> gAudioFlinger;
  236. static audio_error_callback gAudioErrorCallback;
  237. static size_t gInBuffSize;
  238. // previous parameters for recording buffer size queries
  239. static uint32_t gPrevInSamplingRate;
  240. static audio_format_t gPrevInFormat;
  241. static audio_channel_mask_t gPrevInChannelMask;
  242. static sp<IAudioPolicyService> gAudioPolicyService;
  243. // mapping between stream types and outputs
  244. static DefaultKeyedVector<audio_stream_type_t, audio_io_handle_t> gStreamOutputMap;
  245. // list of output descriptors containing cached parameters
  246. // (sampling rate, framecount, channel count...)
  247. static DefaultKeyedVector<audio_io_handle_t, OutputDescriptor *> gOutputs;
  248. };
  249. }; // namespace android
  250. #endif /*ANDROID_AUDIOSYSTEM_H_*/