blob: cad455d669508fe2fa1801b8c993e1c0410ee4ff [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
Alok Kediya601e9b52013-09-27 11:55:10 +05302 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08004 * Copyright (C) 2008 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef ANDROID_AUDIOSYSTEM_H_
20#define ANDROID_AUDIOSYSTEM_H_
21
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070022#include <hardware/audio_effect.h>
23#include <media/IAudioFlingerClient.h>
Dima Zavin64760242011-05-11 14:15:23 -070024#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070025#include <system/audio_policy.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070026#include <utils/Errors.h>
27#include <utils/Mutex.h>
Ethan Chen8d420ed2013-11-14 00:40:54 -080028#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +053029#include <media/AudioParameter.h>
Ethan Chen8d420ed2013-11-14 00:40:54 -080030#endif
Dima Zavindb5cb142011-04-19 22:20:55 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032namespace android {
33
34typedef void (*audio_error_callback)(status_t err);
Eric Laurentc2f1f072009-07-17 12:17:14 -070035
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070036class IAudioFlinger;
Eric Laurentc2f1f072009-07-17 12:17:14 -070037class IAudioPolicyService;
38class String8;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
40class AudioSystem
41{
42public:
43
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044 /* These are static methods to control the system-wide AudioFlinger
45 * only privileged processes can have access to them
46 */
47
Eric Laurentc2f1f072009-07-17 12:17:14 -070048 // mute/unmute microphone
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049 static status_t muteMicrophone(bool state);
50 static status_t isMicrophoneMuted(bool *state);
51
Eric Laurentc2f1f072009-07-17 12:17:14 -070052 // set/get master volume
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 static status_t setMasterVolume(float value);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054 static status_t getMasterVolume(float* volume);
Glenn Kasten4bcae822011-04-04 10:50:50 -070055
Eric Laurentc2f1f072009-07-17 12:17:14 -070056 // mute/unmute audio outputs
57 static status_t setMasterMute(bool mute);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080058 static status_t getMasterMute(bool* mute);
59
Eric Laurentc2f1f072009-07-17 12:17:14 -070060 // set/get stream volume on specified output
Glenn Kasten72ef00d2012-01-17 11:09:42 -080061 static status_t setStreamVolume(audio_stream_type_t stream, float value,
62 audio_io_handle_t output);
63 static status_t getStreamVolume(audio_stream_type_t stream, float* volume,
64 audio_io_handle_t output);
Eric Laurentc2f1f072009-07-17 12:17:14 -070065
66 // mute/unmute stream
Glenn Kastenfff6d712012-01-12 16:38:12 -080067 static status_t setStreamMute(audio_stream_type_t stream, bool mute);
68 static status_t getStreamMute(audio_stream_type_t stream, bool* mute);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069
Glenn Kastenf78aee72012-01-04 11:00:47 -080070 // set audio mode in audio hardware
71 static status_t setMode(audio_mode_t mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072
Jean-Michel Trivi272ab542013-02-04 16:26:02 -080073 // returns true in *state if tracks are active on the specified stream or have been active
Eric Laurenteda6c362011-02-02 09:33:30 -080074 // in the past inPastMs milliseconds
Glenn Kastenfff6d712012-01-12 16:38:12 -080075 static status_t isStreamActive(audio_stream_type_t stream, bool *state, uint32_t inPastMs = 0);
Jean-Michel Trivi272ab542013-02-04 16:26:02 -080076 // returns true in *state if tracks are active for what qualifies as remote playback
77 // on the specified stream or have been active in the past inPastMs milliseconds. Remote
78 // playback isn't mutually exclusive with local playback.
79 static status_t isStreamActiveRemotely(audio_stream_type_t stream, bool *state,
80 uint32_t inPastMs = 0);
Jean-Michel Trivid7086032012-10-10 12:11:16 -070081 // returns true in *state if a recorder is currently recording with the specified source
82 static status_t isSourceActive(audio_source_t source, bool *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083
Eric Laurentc2f1f072009-07-17 12:17:14 -070084 // set/get audio hardware parameters. The function accepts a list of parameters
85 // key value pairs in the form: key1=value1;key2=value2;...
86 // Some keys are reserved for standard parameters (See AudioParameter class).
87 static status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs);
88 static String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
89
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090 static void setErrorCallback(audio_error_callback cb);
91
92 // helper function to obtain AudioFlinger service handle
93 static const sp<IAudioFlinger>& get_audio_flinger();
94
95 static float linearToLog(int volume);
96 static int logToLinear(float volume);
97
Glenn Kasten3b16c762012-11-14 08:44:39 -080098 static status_t getOutputSamplingRate(uint32_t* samplingRate,
Glenn Kasten85ab62c2012-11-01 11:11:38 -070099 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
Glenn Kastene33054e2012-11-14 12:54:39 -0800100 static status_t getOutputFrameCount(size_t* frameCount,
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700101 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
102 static status_t getOutputLatency(uint32_t* latency,
103 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700104 static status_t getSamplingRate(audio_io_handle_t output,
105 audio_stream_type_t streamType,
Glenn Kasten3b16c762012-11-14 08:44:39 -0800106 uint32_t* samplingRate);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700107 // returns the number of frames per audio HAL write buffer. Corresponds to
108 // audio_stream->get_buffer_size()/audio_stream_frame_size()
109 static status_t getFrameCount(audio_io_handle_t output,
110 audio_stream_type_t stream,
Glenn Kastene33054e2012-11-14 12:54:39 -0800111 size_t* frameCount);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700112 // returns the audio output stream latency in ms. Corresponds to
113 // audio_stream_out->get_latency()
114 static status_t getLatency(audio_io_handle_t output,
115 audio_stream_type_t stream,
116 uint32_t* latency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117
Glenn Kastenfff6d712012-01-12 16:38:12 -0800118 static bool routedToA2dpOutput(audio_stream_type_t streamType);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700119
Glenn Kastendd8104c2012-07-02 12:42:44 -0700120 static status_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
121 audio_channel_mask_t channelMask, size_t* buffSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800122
Eric Laurentf0ee6f42009-10-21 08:14:22 -0700123 static status_t setVoiceVolume(float volume);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700124
Eric Laurent342e9cf2010-01-19 17:37:09 -0800125 // return the number of audio frames written by AudioFlinger to audio HAL and
Glenn Kasten4bcae822011-04-04 10:50:50 -0700126 // audio dsp to DAC since the output on which the specified stream is playing
Eric Laurent342e9cf2010-01-19 17:37:09 -0800127 // has exited standby.
128 // returned status (from utils/Errors.h) can be:
129 // - NO_ERROR: successful operation, halFrames and dspFrames point to valid data
130 // - INVALID_OPERATION: Not supported on current hardware platform
131 // - BAD_VALUE: invalid parameter
132 // NOTE: this feature is not supported on all hardware platforms and it is
133 // necessary to check returned status before using the returned values.
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000134 static status_t getRenderPosition(audio_io_handle_t output,
135 size_t *halFrames,
136 size_t *dspFrames,
137 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT);
Eric Laurent342e9cf2010-01-19 17:37:09 -0800138
Glenn Kastenbf04a5d2012-07-12 09:07:11 -0700139 // return the number of input frames lost by HAL implementation, or 0 if the handle is invalid
Glenn Kastene33054e2012-11-14 12:54:39 -0800140 static size_t getInputFramesLost(audio_io_handle_t ioHandle);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700141
142 static int newAudioSessionId();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700143 static void acquireAudioSessionId(int audioSession);
144 static void releaseAudioSessionId(int audioSession);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700145
146 // types of io configuration change events received with ioConfigChanged()
147 enum io_config_event {
148 OUTPUT_OPENED,
149 OUTPUT_CLOSED,
150 OUTPUT_CONFIG_CHANGED,
151 INPUT_OPENED,
152 INPUT_CLOSED,
153 INPUT_CONFIG_CHANGED,
154 STREAM_CONFIG_CHANGED,
Ethan Chen8d420ed2013-11-14 00:40:54 -0800155#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530156 EFFECT_CONFIG_CHANGED,
Ethan Chen8d420ed2013-11-14 00:40:54 -0800157#endif
Eric Laurentc2f1f072009-07-17 12:17:14 -0700158 NUM_CONFIG_EVENTS
159 };
160
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700161 // audio output descriptor used to cache output configurations in client process to avoid
162 // frequent calls through IAudioFlinger
Eric Laurentc2f1f072009-07-17 12:17:14 -0700163 class OutputDescriptor {
164 public:
165 OutputDescriptor()
Glenn Kastenfad226a2013-07-16 17:19:58 -0700166 : samplingRate(0), format(AUDIO_FORMAT_DEFAULT), channelMask(0), frameCount(0), latency(0) {}
Eric Laurentc2f1f072009-07-17 12:17:14 -0700167
168 uint32_t samplingRate;
Glenn Kasteneced2da2013-07-16 17:17:28 -0700169 audio_format_t format;
Glenn Kastenfad226a2013-07-16 17:19:58 -0700170 audio_channel_mask_t channelMask;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700171 size_t frameCount;
172 uint32_t latency;
173 };
174
Eric Laurenta011e352012-03-29 15:51:43 -0700175 // Events used to synchronize actions between audio sessions.
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700176 // For instance SYNC_EVENT_PRESENTATION_COMPLETE can be used to delay recording start until
177 // playback is complete on another audio session.
Eric Laurenta011e352012-03-29 15:51:43 -0700178 // See definitions in MediaSyncEvent.java
179 enum sync_event_t {
180 SYNC_EVENT_SAME = -1, // used internally to indicate restart with same event
181 SYNC_EVENT_NONE = 0,
182 SYNC_EVENT_PRESENTATION_COMPLETE,
183
184 //
185 // Define new events here: SYNC_EVENT_START, SYNC_EVENT_STOP, SYNC_EVENT_TIME ...
186 //
187 SYNC_EVENT_CNT,
188 };
189
Eric Laurent29864602012-05-08 18:57:51 -0700190 // Timeout for synchronous record start. Prevents from blocking the record thread forever
191 // if the trigger event is not fired.
192 static const uint32_t kSyncRecordStartTimeOutMs = 30000;
193
Eric Laurentc2f1f072009-07-17 12:17:14 -0700194 //
195 // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions)
196 //
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700197 static status_t setDeviceConnectionState(audio_devices_t device, audio_policy_dev_state_t state,
198 const char *device_address);
199 static audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
200 const char *device_address);
Glenn Kastenf78aee72012-01-04 11:00:47 -0800201 static status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -0700202 static status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
203 static audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
204 static audio_io_handle_t getOutput(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700205 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -0800206 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -0700207 audio_channel_mask_t channelMask = AUDIO_CHANNEL_OUT_STEREO,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000208 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
209 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurentde070132010-07-13 04:45:46 -0700210 static status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700212 int session = 0);
213 static status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700214 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700215 int session = 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700216 static void releaseOutput(audio_io_handle_t output);
Glenn Kasteneba51fb2012-01-23 13:58:49 -0800217 static audio_io_handle_t getInput(audio_source_t inputSource,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700218 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -0800219 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -0700220 audio_channel_mask_t channelMask = AUDIO_CHANNEL_IN_MONO,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700221 int sessionId = 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700222 static status_t startInput(audio_io_handle_t input);
223 static status_t stopInput(audio_io_handle_t input);
224 static void releaseInput(audio_io_handle_t input);
Dima Zavinfce7a472011-04-19 22:30:36 -0700225 static status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700226 int indexMin,
227 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -0800228 static status_t setStreamVolumeIndex(audio_stream_type_t stream,
229 int index,
230 audio_devices_t device);
231 static status_t getStreamVolumeIndex(audio_stream_type_t stream,
232 int *index,
233 audio_devices_t device);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234
Dima Zavinfce7a472011-04-19 22:30:36 -0700235 static uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800236 static audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700237
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700238 static audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
239 static status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700240 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700241 uint32_t strategy,
242 int session,
243 int id);
244 static status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700245 static status_t setEffectEnabled(int id, bool enabled);
Eric Laurentde070132010-07-13 04:45:46 -0700246
Eric Laurent9f6530f2011-08-30 10:18:54 -0700247 // clear stream to output mapping cache (gStreamOutputMap)
248 // and output configuration cache (gOutputs)
249 static void clearAudioConfigCache();
250
Eric Laurentc2f1f072009-07-17 12:17:14 -0700251 static const sp<IAudioPolicyService>& get_audio_policy_service();
252
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700253 // helpers for android.media.AudioManager.getProperty(), see description there for meaning
Glenn Kasten3b16c762012-11-14 08:44:39 -0800254 static uint32_t getPrimaryOutputSamplingRate();
Glenn Kastene33054e2012-11-14 12:54:39 -0800255 static size_t getPrimaryOutputFrameCount();
Glenn Kastencc0f1cf2012-09-24 11:27:18 -0700256
Glenn Kasten4182c4e2013-07-15 14:45:07 -0700257 static status_t setLowRamDevice(bool isLowRamDevice);
258
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000259 // Check if hw offload is possible for given format, stream type, sample rate,
260 // bit rate, duration, video and streaming or offload property is enabled
261 static bool isOffloadSupported(const audio_offload_info_t& info);
262
Eric Laurent46291612013-07-18 14:38:44 -0700263 // check presence of audio flinger service.
264 // returns NO_ERROR if binding to service succeeds, DEAD_OBJECT otherwise
265 static status_t checkAudioFlinger();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 // ----------------------------------------------------------------------------
267
268private:
269
270 class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
271 {
272 public:
Eric Laurentc2f1f072009-07-17 12:17:14 -0700273 AudioFlingerClient() {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 // DeathRecipient
277 virtual void binderDied(const wp<IBinder>& who);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700278
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 // IAudioFlingerClient
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280
281 // indicate a change in the configuration of an output or input: keeps the cached
Glenn Kastenb81cc8c2012-03-01 09:14:51 -0800282 // values for output/input parameters up-to-date in client process
283 virtual void ioConfigChanged(int event, audio_io_handle_t ioHandle, const void *param2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284 };
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285
286 class AudioPolicyServiceClient: public IBinder::DeathRecipient
287 {
288 public:
289 AudioPolicyServiceClient() {
290 }
291
292 // DeathRecipient
293 virtual void binderDied(const wp<IBinder>& who);
294 };
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295
296 static sp<AudioFlingerClient> gAudioFlingerClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700297 static sp<AudioPolicyServiceClient> gAudioPolicyServiceClient;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 friend class AudioFlingerClient;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 friend class AudioPolicyServiceClient;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300
301 static Mutex gLock;
302 static sp<IAudioFlinger> gAudioFlinger;
303 static audio_error_callback gAudioErrorCallback;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 static size_t gInBuffSize;
306 // previous parameters for recording buffer size queries
307 static uint32_t gPrevInSamplingRate;
Glenn Kasten58f30212012-01-12 12:27:51 -0800308 static audio_format_t gPrevInFormat;
Glenn Kastendd8104c2012-07-02 12:42:44 -0700309 static audio_channel_mask_t gPrevInChannelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311 static sp<IAudioPolicyService> gAudioPolicyService;
312
313 // mapping between stream types and outputs
Glenn Kasten211eeaf2012-01-20 09:37:45 -0800314 static DefaultKeyedVector<audio_stream_type_t, audio_io_handle_t> gStreamOutputMap;
Eric Laurent9f6530f2011-08-30 10:18:54 -0700315 // list of output descriptors containing cached parameters
316 // (sampling rate, framecount, channel count...)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317 static DefaultKeyedVector<audio_io_handle_t, OutputDescriptor *> gOutputs;
318};
319
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320}; // namespace android
321
322#endif /*ANDROID_AUDIOSYSTEM_H_*/