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