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