1 /*
2  * Copyright (C) 2009 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 
17 
18 #include <stdint.h>
19 #include <sys/types.h>
20 #include <cutils/config_utils.h>
21 #include <cutils/misc.h>
22 #include <utils/Timers.h>
23 #include <utils/Errors.h>
24 #include <utils/KeyedVector.h>
25 #include <utils/SortedVector.h>
26 #include <hardware_legacy/AudioPolicyInterface.h>
27 
28 
29 namespace android_audio_legacy {
30     using android::KeyedVector;
31     using android::DefaultKeyedVector;
32     using android::SortedVector;
33 
34 // ----------------------------------------------------------------------------
35 
36 #define MAX_DEVICE_ADDRESS_LEN 20
37 // Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
38 #define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
39 // Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
40 #define SONIFICATION_HEADSET_VOLUME_MIN  0.016
41 // Time in milliseconds during which we consider that music is still active after a music
42 // track was stopped - see computeVolume()
43 #define SONIFICATION_HEADSET_MUSIC_DELAY  5000
44 // Time in milliseconds after media stopped playing during which we consider that the
45 // sonification should be as unobtrusive as during the time media was playing.
46 #define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000
47 // Time in milliseconds during witch some streams are muted while the audio path
48 // is switched
49 #define MUTE_TIME_MS 2000
50 
51 #define NUM_TEST_OUTPUTS 5
52 
53 #define NUM_VOL_CURVE_KNEES 2
54 
55 // Default minimum length allowed for offloading a compressed track
56 // Can be overridden by the audio.offload.min.duration.secs property
57 #define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
58 
59 // ----------------------------------------------------------------------------
60 // AudioPolicyManagerBase implements audio policy manager behavior common to all platforms.
61 // Each platform must implement an AudioPolicyManager class derived from AudioPolicyManagerBase
62 // and override methods for which the platform specific behavior differs from the implementation
63 // in AudioPolicyManagerBase. Even if no specific behavior is required, the AudioPolicyManager
64 // class must be implemented as well as the class factory function createAudioPolicyManager()
65 // and provided in a shared library libaudiopolicy.so.
66 // ----------------------------------------------------------------------------
67 
68 class AudioPolicyManagerBase: public AudioPolicyInterface
69 #ifdef AUDIO_POLICY_TEST
70     , public Thread
71 #endif //AUDIO_POLICY_TEST
72 {
73 
74 public:
75                 AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface);
76         virtual ~AudioPolicyManagerBase();
77 
78         // AudioPolicyInterface
79         virtual status_t setDeviceConnectionState(audio_devices_t device,
80                                                           AudioSystem::device_connection_state state,
81                                                           const char *device_address);
82         virtual AudioSystem::device_connection_state getDeviceConnectionState(audio_devices_t device,
83                                                                               const char *device_address);
84         virtual void setPhoneState(int state);
85         virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
86         virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
87         virtual void setSystemProperty(const char* property, const char* value);
88         virtual status_t initCheck();
89         virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
90                                             uint32_t samplingRate,
91                                             audio_format_t format,
92                                             audio_channel_mask_t channelMask,
93                                             AudioSystem::output_flags flags,
94                                             const audio_offload_info_t *offloadInfo);
95         virtual status_t startOutput(audio_io_handle_t output,
96                                      AudioSystem::stream_type stream,
97                                      audio_session_t session = AUDIO_SESSION_NONE);
98         virtual status_t stopOutput(audio_io_handle_t output,
99                                     AudioSystem::stream_type stream,
100                                     audio_session_t session = AUDIO_SESSION_NONE);
101         virtual void releaseOutput(audio_io_handle_t output);
102         virtual audio_io_handle_t getInput(int inputSource,
103                                             uint32_t samplingRate,
104                                             audio_format_t format,
105                                             audio_channel_mask_t channelMask,
106                                             AudioSystem::audio_in_acoustics acoustics);
107 
108         // indicates to the audio policy manager that the input starts being used.
109         virtual status_t startInput(audio_io_handle_t input);
110 
111         // indicates to the audio policy manager that the input stops being used.
112         virtual status_t stopInput(audio_io_handle_t input);
113         virtual void releaseInput(audio_io_handle_t input);
114         virtual void closeAllInputs();
115         virtual void initStreamVolume(AudioSystem::stream_type stream,
116                                                     int indexMin,
117                                                     int indexMax);
118         virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream,
119                                               int index,
120                                               audio_devices_t device);
121         virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream,
122                                               int *index,
123                                               audio_devices_t device);
124 
125         // return the strategy corresponding to a given stream type
126         virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream);
127 
128         // return the enabled output devices for the given stream type
129         virtual audio_devices_t getDevicesForStream(AudioSystem::stream_type stream);
130 
131         virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
132         virtual status_t registerEffect(const effect_descriptor_t *desc,
133                                         audio_io_handle_t io,
134                                         uint32_t strategy,
135                                         audio_session_t session,
136                                         int id);
137         virtual status_t unregisterEffect(int id);
138         virtual status_t setEffectEnabled(int id, bool enabled);
139 
140         virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
141         // return whether a stream is playing remotely, override to change the definition of
142         //   local/remote playback, used for instance by notification manager to not make
143         //   media players lose audio focus when not playing locally
144         virtual bool isStreamActiveRemotely(int stream, uint32_t inPastMs = 0) const;
145         virtual bool isSourceActive(audio_source_t source) const;
146 
147         virtual status_t dump(int fd);
148 
149         virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
150 
151 protected:
152 
153         enum routing_strategy {
154             STRATEGY_MEDIA,
155             STRATEGY_PHONE,
156             STRATEGY_SONIFICATION,
157             STRATEGY_SONIFICATION_RESPECTFUL,
158             STRATEGY_DTMF,
159             STRATEGY_ENFORCED_AUDIBLE,
160             NUM_STRATEGIES
161         };
162 
163         // 4 points to define the volume attenuation curve, each characterized by the volume
164         // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
165         // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
166 
167         enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
168 
169         class VolumeCurvePoint
170         {
171         public:
172             int mIndex;
173             float mDBAttenuation;
174         };
175 
176         // device categories used for volume curve management.
177         enum device_category {
178             DEVICE_CATEGORY_HEADSET,
179             DEVICE_CATEGORY_SPEAKER,
180             DEVICE_CATEGORY_EARPIECE,
181             DEVICE_CATEGORY_CNT
182         };
183 
184         class IOProfile;
185 
186         class HwModule {
187         public:
188                     HwModule(const char *name);
189                     ~HwModule();
190 
191             void dump(int fd);
192 
193             const char *const mName; // base name of the audio HW module (primary, a2dp ...)
194             audio_module_handle_t mHandle;
195             Vector <IOProfile *> mOutputProfiles; // output profiles exposed by this module
196             Vector <IOProfile *> mInputProfiles;  // input profiles exposed by this module
197         };
198 
199         // the IOProfile class describes the capabilities of an output or input stream.
200         // It is currently assumed that all combination of listed parameters are supported.
201         // It is used by the policy manager to determine if an output or input is suitable for
202         // a given use case,  open/close it accordingly and connect/disconnect audio tracks
203         // to/from it.
204         class IOProfile
205         {
206         public:
207             IOProfile(HwModule *module);
208             ~IOProfile();
209 
210             bool isCompatibleProfile(audio_devices_t device,
211                                      uint32_t samplingRate,
212                                      audio_format_t format,
213                                      audio_channel_mask_t channelMask,
214                                      audio_output_flags_t flags) const;
215 
216             void dump(int fd);
217             void log();
218 
219             // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
220             // indicates the supported parameters should be read from the output stream
221             // after it is opened for the first time
222             Vector <uint32_t> mSamplingRates; // supported sampling rates
223             Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
224             Vector <audio_format_t> mFormats; // supported audio formats
225             audio_devices_t mSupportedDevices; // supported devices (devices this output can be
226                                                // routed to)
227             audio_output_flags_t mFlags; // attribute flags (e.g primary output,
228                                                 // direct output...). For outputs only.
229             HwModule *mModule;                     // audio HW module exposing this I/O stream
230         };
231 
232         // default volume curve
233         static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT];
234         // default volume curve for media strategy
235         static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT];
236         // volume curve for media strategy on speakers
237         static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT];
238         // volume curve for sonification strategy on speakers
239         static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT];
240         static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT];
241         static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT];
242         static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT];
243         static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT];
244         static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT];
245         static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT];
246         // default volume curves per stream and device category. See initializeVolumeCurves()
247         static const VolumeCurvePoint *sVolumeProfiles[AudioSystem::NUM_STREAM_TYPES][DEVICE_CATEGORY_CNT];
248 
249         // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
250         // and keep track of the usage of this output by each audio stream type.
251         class AudioOutputDescriptor
252         {
253         public:
254             AudioOutputDescriptor(const IOProfile *profile);
255 
256             status_t    dump(int fd);
257 
258             audio_devices_t device() const;
259             void changeRefCount(AudioSystem::stream_type stream, int delta);
260 
isDuplicated()261             bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
262             audio_devices_t supportedDevices();
263             uint32_t latency();
264             bool sharesHwModuleWith(const AudioOutputDescriptor *outputDesc);
265             bool isActive(uint32_t inPastMs = 0) const;
266             bool isStreamActive(AudioSystem::stream_type stream,
267                                 uint32_t inPastMs = 0,
268                                 nsecs_t sysTime = 0) const;
269             bool isStrategyActive(routing_strategy strategy,
270                              uint32_t inPastMs = 0,
271                              nsecs_t sysTime = 0) const;
272 
273             audio_io_handle_t mId;              // output handle
274             uint32_t mSamplingRate;             //
275             audio_format_t mFormat;             //
276             audio_channel_mask_t mChannelMask;     // output configuration
277             uint32_t mLatency;                  //
278             audio_output_flags_t mFlags;   //
279             audio_devices_t mDevice;                   // current device this output is routed to
280             uint32_t mRefCount[AudioSystem::NUM_STREAM_TYPES]; // number of streams of each type using this output
281             nsecs_t mStopTime[AudioSystem::NUM_STREAM_TYPES];
282             AudioOutputDescriptor *mOutput1;    // used by duplicated outputs: first output
283             AudioOutputDescriptor *mOutput2;    // used by duplicated outputs: second output
284             float mCurVolume[AudioSystem::NUM_STREAM_TYPES];   // current stream volume
285             int mMuteCount[AudioSystem::NUM_STREAM_TYPES];     // mute request counter
286             const IOProfile *mProfile;          // I/O profile this output derives from
287             bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
288                                                 // device selection. See checkDeviceMuteStrategies()
289             uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
290             bool mForceRouting; // Next routing for this output will be forced as current device routed is null
291         };
292 
293         // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
294         // and keep track of the usage of this input.
295         class AudioInputDescriptor
296         {
297         public:
298             AudioInputDescriptor(const IOProfile *profile);
299 
300             status_t    dump(int fd);
301 
302             audio_io_handle_t mId;                      // input handle
303             uint32_t mSamplingRate;                     //
304             audio_format_t mFormat;                     // input configuration
305             audio_channel_mask_t mChannelMask;             //
306             audio_devices_t mDevice;                    // current device this input is routed to
307             uint32_t mRefCount;                         // number of AudioRecord clients using this output
308             int      mInputSource;                      // input source selected by application (mediarecorder.h)
309             const IOProfile *mProfile;                  // I/O profile this output derives from
310         };
311 
312         // stream descriptor used for volume control
313         class StreamDescriptor
314         {
315         public:
316             StreamDescriptor();
317 
318             int getVolumeIndex(audio_devices_t device);
319             void dump(int fd);
320 
321             int mIndexMin;      // min volume index
322             int mIndexMax;      // max volume index
323             KeyedVector<audio_devices_t, int> mIndexCur;   // current volume index per device
324             bool mCanBeMuted;   // true is the stream can be muted
325 
326             const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
327         };
328 
329         // stream descriptor used for volume control
330         class EffectDescriptor
331         {
332         public:
333 
334             status_t dump(int fd);
335 
336             int mIo;                // io the effect is attached to
337             routing_strategy mStrategy; // routing strategy the effect is associated to
338             audio_session_t mSession;   // audio session the effect is on
339             effect_descriptor_t mDesc;  // effect descriptor
340             bool mEnabled;              // enabled state: CPU load being used or not
341         };
342 
343         void addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc);
344         void addInput(audio_io_handle_t id, AudioInputDescriptor *inputDesc);
345 
346         // return the strategy corresponding to a given stream type
347         static routing_strategy getStrategy(AudioSystem::stream_type stream);
348 
349         // return appropriate device for streams handled by the specified strategy according to current
350         // phone state, connected devices...
351         // if fromCache is true, the device is returned from mDeviceForStrategy[],
352         // otherwise it is determine by current state
353         // (device connected,phone state, force use, a2dp output...)
354         // This allows to:
355         //  1 speed up process when the state is stable (when starting or stopping an output)
356         //  2 access to either current device selection (fromCache == true) or
357         // "future" device selection (fromCache == false) when called from a context
358         //  where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
359         //  before updateDevicesAndOutputs() is called.
360         virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
361                                                      bool fromCache);
362 
363         // change the route of the specified output. Returns the number of ms we have slept to
364         // allow new routing to take effect in certain cases.
365         uint32_t setOutputDevice(audio_io_handle_t output,
366                              audio_devices_t device,
367                              bool force = false,
368                              int delayMs = 0);
369 
370         // select input device corresponding to requested audio source
371         virtual audio_devices_t getDeviceForInputSource(int inputSource);
372 
373         // return io handle of active input or 0 if no input is active
374         //    Only considers inputs from physical devices (e.g. main mic, headset mic) when
375         //    ignoreVirtualInputs is true.
376         audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
377 
378         // initialize volume curves for each strategy and device category
379         void initializeVolumeCurves();
380 
381         // compute the actual volume for a given stream according to the requested index and a particular
382         // device
383         virtual float computeVolume(int stream, int index, audio_io_handle_t output, audio_devices_t device);
384 
385         // check that volume change is permitted, compute and send new volume to audio hardware
386         status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
387 
388         // apply all stream volumes to the specified output and device
389         void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
390 
391         // Mute or unmute all streams handled by the specified strategy on the specified output
392         void setStrategyMute(routing_strategy strategy,
393                              bool on,
394                              audio_io_handle_t output,
395                              int delayMs = 0,
396                              audio_devices_t device = (audio_devices_t)0);
397 
398         // Mute or unmute the stream on the specified output
399         void setStreamMute(int stream,
400                            bool on,
401                            audio_io_handle_t output,
402                            int delayMs = 0,
403                            audio_devices_t device = (audio_devices_t)0);
404 
405         // handle special cases for sonification strategy while in call: mute streams or replace by
406         // a special tone in the device used for communication
407         void handleIncallSonification(int stream, bool starting, bool stateChange);
408 
409         // true if device is in a telephony or VoIP call
410         virtual bool isInCall();
411 
412         // true if given state represents a device in a telephony or VoIP call
413         virtual bool isStateInCall(int state);
414 
415         // when a device is connected, checks if an open output can be routed
416         // to this device. If none is open, tries to open one of the available outputs.
417         // Returns an output suitable to this device or 0.
418         // when a device is disconnected, checks if an output is not used any more and
419         // returns its handle if any.
420         // transfers the audio tracks and effects from one output thread to another accordingly.
421         status_t checkOutputsForDevice(audio_devices_t device,
422                                        AudioSystem::device_connection_state state,
423                                        SortedVector<audio_io_handle_t>& outputs,
424                                        const String8 paramStr);
425 
426         status_t checkInputsForDevice(audio_devices_t device,
427                                       AudioSystem::device_connection_state state,
428                                       SortedVector<audio_io_handle_t>& inputs,
429                                       const String8 paramStr);
430 
431         // close an output and its companion duplicating output.
432         void closeOutput(audio_io_handle_t output);
433 
434         // checks and if necessary changes outputs used for all strategies.
435         // must be called every time a condition that affects the output choice for a given strategy
436         // changes: connected device, phone state, force use...
437         // Must be called before updateDevicesAndOutputs()
438         void checkOutputForStrategy(routing_strategy strategy);
439 
440         // Same as checkOutputForStrategy() but for a all strategies in order of priority
441         void checkOutputForAllStrategies();
442 
443         // manages A2DP output suspend/restore according to phone state and BT SCO usage
444         void checkA2dpSuspend();
445 
446         // returns the A2DP output handle if it is open or 0 otherwise
447         audio_io_handle_t getA2dpOutput();
448 
449         // selects the most appropriate device on output for current state
450         // must be called every time a condition that affects the device choice for a given output is
451         // changed: connected device, phone state, force use, output start, output stop..
452         // see getDeviceForStrategy() for the use of fromCache parameter
453 
454         audio_devices_t getNewDevice(audio_io_handle_t output, bool fromCache);
455         // updates cache of device used by all strategies (mDeviceForStrategy[])
456         // must be called every time a condition that affects the device choice for a given strategy is
457         // changed: connected device, phone state, force use...
458         // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
459          // Must be called after checkOutputForAllStrategies()
460 
461         void updateDevicesAndOutputs();
462 
463         virtual uint32_t getMaxEffectsCpuLoad();
464         virtual uint32_t getMaxEffectsMemory();
465 #ifdef AUDIO_POLICY_TEST
466         virtual     bool        threadLoop();
467                     void        exit();
468         int testOutputIndex(audio_io_handle_t output);
469 #endif //AUDIO_POLICY_TEST
470 
471         status_t setEffectEnabled(EffectDescriptor *pDesc, bool enabled);
472 
473         // returns the category the device belongs to with regard to volume curve management
474         static device_category getDeviceCategory(audio_devices_t device);
475 
476         // extract one device relevant for volume control from multiple device selection
477         static audio_devices_t getDeviceForVolume(audio_devices_t device);
478 
479         SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
480                         DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs);
481         bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
482                                            SortedVector<audio_io_handle_t>& outputs2);
483 
484         // mute/unmute strategies using an incompatible device combination
485         // if muting, wait for the audio in pcm buffer to be drained before proceeding
486         // if unmuting, unmute only after the specified delay
487         // Returns the number of ms waited
488         uint32_t  checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
489                                             audio_devices_t prevDevice,
490                                             uint32_t delayMs);
491 
492         audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
493                                        AudioSystem::output_flags flags);
494         IOProfile *getInputProfile(audio_devices_t device,
495                                    uint32_t samplingRate,
496                                    audio_format_t format,
497                                    audio_channel_mask_t channelMask);
498         IOProfile *getProfileForDirectOutput(audio_devices_t device,
499                                                        uint32_t samplingRate,
500                                                        audio_format_t format,
501                                                        audio_channel_mask_t channelMask,
502                                                        audio_output_flags_t flags);
503 
504         audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
505 
506         bool isNonOffloadableEffectEnabled();
507 
508         //
509         // Audio policy configuration file parsing (audio_policy.conf)
510         //
511         static uint32_t stringToEnum(const struct StringToEnum *table,
512                                      size_t size,
513                                      const char *name);
514         static bool stringToBool(const char *value);
515         static audio_output_flags_t parseFlagNames(char *name);
516         static audio_devices_t parseDeviceNames(char *name);
517         void loadSamplingRates(char *name, IOProfile *profile);
518         void loadFormats(char *name, IOProfile *profile);
519         void loadOutChannels(char *name, IOProfile *profile);
520         void loadInChannels(char *name, IOProfile *profile);
521         status_t loadOutput(cnode *root,  HwModule *module);
522         status_t loadInput(cnode *root,  HwModule *module);
523         void loadHwModule(cnode *root);
524         void loadHwModules(cnode *root);
525         void loadGlobalConfig(cnode *root);
526         status_t loadAudioPolicyConfig(const char *path);
527         void defaultAudioPolicyConfig(void);
528 
529 
530         AudioPolicyClientInterface *mpClientInterface;  // audio policy client interface
531         audio_io_handle_t mPrimaryOutput;              // primary output handle
532         // list of descriptors for outputs currently opened
533         DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs;
534         // copy of mOutputs before setDeviceConnectionState() opens new outputs
535         // reset to mOutputs when updateDevicesAndOutputs() is called.
536         DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mPreviousOutputs;
537 
538         // list of input descriptors currently opened
539         DefaultKeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs;
540 
541         audio_devices_t mAvailableOutputDevices; // bit field of all available output devices
542         audio_devices_t mAvailableInputDevices; // bit field of all available input devices
543                                                 // without AUDIO_DEVICE_BIT_IN to allow direct bit
544                                                 // field comparisons
545         int mPhoneState;                                                    // current phone state
546         AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE];   // current forced use configuration
547 
548         StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES];           // stream descriptors for volume control
549         String8 mA2dpDeviceAddress;                                         // A2DP device MAC address
550         String8 mScoDeviceAddress;                                          // SCO device MAC address
551         String8 mUsbOutCardAndDevice;                                       // USB audio ALSA card and device numbers:
552                                                                             // card=<card_number>;device=<><device_number>
553         bool    mLimitRingtoneVolume;                                       // limit ringtone volume to music volume if headset connected
554         audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
555         float   mLastVoiceVolume;                                           // last voice volume value sent to audio HAL
556 
557         // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
558         static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
559         // Maximum memory allocated to audio effects in KB
560         static const uint32_t MAX_EFFECTS_MEMORY = 512;
561         uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
562         uint32_t mTotalEffectsMemory;  // current memory used by effects
563         KeyedVector<int, EffectDescriptor *> mEffects;  // list of registered audio effects
564         bool    mA2dpSuspended;  // true if A2DP output is suspended
565         bool mHasA2dp; // true on platforms with support for bluetooth A2DP
566         bool mHasUsb; // true on platforms with support for USB audio
567         bool mHasRemoteSubmix; // true on platforms with support for remote presentation of a submix
568         audio_devices_t mAttachedOutputDevices; // output devices always available on the platform
569         audio_devices_t mDefaultOutputDevice; // output device selected by default at boot time
570                                               // (must be in mAttachedOutputDevices)
571         bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
572                                 // to boost soft sounds, used to adjust volume curves accordingly
573 
574         Vector <HwModule *> mHwModules;
575 
576 #ifdef AUDIO_POLICY_TEST
577         Mutex   mLock;
578         Condition mWaitWorkCV;
579 
580         int             mCurOutput;
581         bool            mDirectOutput;
582         audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
583         int             mTestInput;
584         uint32_t        mTestDevice;
585         uint32_t        mTestSamplingRate;
586         uint32_t        mTestFormat;
587         uint32_t        mTestChannels;
588         uint32_t        mTestLatencyMs;
589 #endif //AUDIO_POLICY_TEST
590 
591 private:
592         static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
593                 int indexInUi);
594         // updates device caching and output for streams that can influence the
595         //    routing of notifications
596         void handleNotificationRoutingForStream(AudioSystem::stream_type stream);
597         static bool isVirtualInputDevice(audio_devices_t device);
598 };
599 
600 };
601