1de55cfb02db874136caf9c05927cee4028296147Eric Laurent/*
2de55cfb02db874136caf9c05927cee4028296147Eric Laurent * Copyright (C) 2009 The Android Open Source Project
3de55cfb02db874136caf9c05927cee4028296147Eric Laurent *
4de55cfb02db874136caf9c05927cee4028296147Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
5de55cfb02db874136caf9c05927cee4028296147Eric Laurent * you may not use this file except in compliance with the License.
6de55cfb02db874136caf9c05927cee4028296147Eric Laurent * You may obtain a copy of the License at
7de55cfb02db874136caf9c05927cee4028296147Eric Laurent *
8de55cfb02db874136caf9c05927cee4028296147Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
9de55cfb02db874136caf9c05927cee4028296147Eric Laurent *
10de55cfb02db874136caf9c05927cee4028296147Eric Laurent * Unless required by applicable law or agreed to in writing, software
11de55cfb02db874136caf9c05927cee4028296147Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
12de55cfb02db874136caf9c05927cee4028296147Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13de55cfb02db874136caf9c05927cee4028296147Eric Laurent * See the License for the specific language governing permissions and
14de55cfb02db874136caf9c05927cee4028296147Eric Laurent * limitations under the License.
15de55cfb02db874136caf9c05927cee4028296147Eric Laurent */
16de55cfb02db874136caf9c05927cee4028296147Eric Laurent
17de55cfb02db874136caf9c05927cee4028296147Eric Laurent
18de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <stdint.h>
19de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <sys/types.h>
205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#include <cutils/config_utils.h>
215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#include <cutils/misc.h>
22de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <utils/Timers.h>
23de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <utils/Errors.h>
24de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <utils/KeyedVector.h>
25b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent#include <utils/SortedVector.h>
26de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <hardware_legacy/AudioPolicyInterface.h>
27de55cfb02db874136caf9c05927cee4028296147Eric Laurent
28de55cfb02db874136caf9c05927cee4028296147Eric Laurent
29e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
30e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavin    using android::KeyedVector;
31b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    using android::DefaultKeyedVector;
32b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    using android::SortedVector;
33de55cfb02db874136caf9c05927cee4028296147Eric Laurent
34de55cfb02db874136caf9c05927cee4028296147Eric Laurent// ----------------------------------------------------------------------------
35de55cfb02db874136caf9c05927cee4028296147Eric Laurent
36de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define MAX_DEVICE_ADDRESS_LEN 20
37de55cfb02db874136caf9c05927cee4028296147Eric Laurent// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
38de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
39de55cfb02db874136caf9c05927cee4028296147Eric Laurent// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
40de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define SONIFICATION_HEADSET_VOLUME_MIN  0.016
4136291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent// Time in milliseconds during which we consider that music is still active after a music
42de55cfb02db874136caf9c05927cee4028296147Eric Laurent// track was stopped - see computeVolume()
4336291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent#define SONIFICATION_HEADSET_MUSIC_DELAY  5000
4412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi// Time in milliseconds after media stopped playing during which we consider that the
4512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi// sonification should be as unobtrusive as during the time media was playing.
4612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi#define SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY 5000
47195368626339aefc4732661d009e8de1aacc24d1Eric Laurent// Time in milliseconds during witch some streams are muted while the audio path
48195368626339aefc4732661d009e8de1aacc24d1Eric Laurent// is switched
49195368626339aefc4732661d009e8de1aacc24d1Eric Laurent#define MUTE_TIME_MS 2000
50de55cfb02db874136caf9c05927cee4028296147Eric Laurent
51de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define NUM_TEST_OUTPUTS 5
52de55cfb02db874136caf9c05927cee4028296147Eric Laurent
53f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi#define NUM_VOL_CURVE_KNEES 2
54f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi
55a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// Default minimum length allowed for offloading a compressed track
56a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// Can be overridden by the audio.offload.min.duration.secs property
578c3b7b67ac1d2df57fd0cdd03ff935d178d7fff3Eric Laurent#define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
58a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
59de55cfb02db874136caf9c05927cee4028296147Eric Laurent// ----------------------------------------------------------------------------
60de55cfb02db874136caf9c05927cee4028296147Eric Laurent// AudioPolicyManagerBase implements audio policy manager behavior common to all platforms.
61de55cfb02db874136caf9c05927cee4028296147Eric Laurent// Each platform must implement an AudioPolicyManager class derived from AudioPolicyManagerBase
62d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent// and override methods for which the platform specific behavior differs from the implementation
63de55cfb02db874136caf9c05927cee4028296147Eric Laurent// in AudioPolicyManagerBase. Even if no specific behavior is required, the AudioPolicyManager
64de55cfb02db874136caf9c05927cee4028296147Eric Laurent// class must be implemented as well as the class factory function createAudioPolicyManager()
65de55cfb02db874136caf9c05927cee4028296147Eric Laurent// and provided in a shared library libaudiopolicy.so.
66de55cfb02db874136caf9c05927cee4028296147Eric Laurent// ----------------------------------------------------------------------------
67de55cfb02db874136caf9c05927cee4028296147Eric Laurent
68de55cfb02db874136caf9c05927cee4028296147Eric Laurentclass AudioPolicyManagerBase: public AudioPolicyInterface
69de55cfb02db874136caf9c05927cee4028296147Eric Laurent#ifdef AUDIO_POLICY_TEST
70de55cfb02db874136caf9c05927cee4028296147Eric Laurent    , public Thread
71de55cfb02db874136caf9c05927cee4028296147Eric Laurent#endif //AUDIO_POLICY_TEST
72de55cfb02db874136caf9c05927cee4028296147Eric Laurent{
73de55cfb02db874136caf9c05927cee4028296147Eric Laurent
74de55cfb02db874136caf9c05927cee4028296147Eric Laurentpublic:
75de55cfb02db874136caf9c05927cee4028296147Eric Laurent                AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface);
76de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual ~AudioPolicyManagerBase();
77de55cfb02db874136caf9c05927cee4028296147Eric Laurent
78de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // AudioPolicyInterface
79c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        virtual status_t setDeviceConnectionState(audio_devices_t device,
80de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                          AudioSystem::device_connection_state state,
81de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                          const char *device_address);
82c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        virtual AudioSystem::device_connection_state getDeviceConnectionState(audio_devices_t device,
83de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                                              const char *device_address);
84de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void setPhoneState(int state);
85de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
86de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
87de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void setSystemProperty(const char* property, const char* value);
8861f357948648ff45d35ec79725db4e1b12c268dcEric Laurent        virtual status_t initCheck();
89de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
90d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            uint32_t samplingRate = 0,
91d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            uint32_t format = AudioSystem::FORMAT_DEFAULT,
92d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            uint32_t channels = 0,
93d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            AudioSystem::output_flags flags =
94b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                                    AudioSystem::OUTPUT_FLAG_INDIRECT,
95b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            const audio_offload_info_t *offloadInfo = NULL);
96d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual status_t startOutput(audio_io_handle_t output,
97d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                     AudioSystem::stream_type stream,
98d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                     int session = 0);
99d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual status_t stopOutput(audio_io_handle_t output,
100d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                    AudioSystem::stream_type stream,
101d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                    int session = 0);
102de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void releaseOutput(audio_io_handle_t output);
103de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual audio_io_handle_t getInput(int inputSource,
104de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            uint32_t samplingRate,
105de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            uint32_t format,
106de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            uint32_t channels,
107de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            AudioSystem::audio_in_acoustics acoustics);
108f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
109de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // indicates to the audio policy manager that the input starts being used.
110de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t startInput(audio_io_handle_t input);
111f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
112de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // indicates to the audio policy manager that the input stops being used.
113de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t stopInput(audio_io_handle_t input);
114de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void releaseInput(audio_io_handle_t input);
115de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void initStreamVolume(AudioSystem::stream_type stream,
116de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                    int indexMin,
117de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                    int indexMax);
118c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream,
119c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                              int index,
120c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                              audio_devices_t device);
121c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream,
122c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                              int *index,
123c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                              audio_devices_t device);
124de55cfb02db874136caf9c05927cee4028296147Eric Laurent
125d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // return the strategy corresponding to a given stream type
126d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream);
127d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
128fafb18fb31cc4ab3c73585ba508f7643c9a9e3adGlenn Kasten        // return the enabled output devices for the given stream type
129f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        virtual audio_devices_t getDevicesForStream(AudioSystem::stream_type stream);
130fafb18fb31cc4ab3c73585ba508f7643c9a9e3adGlenn Kasten
13145c763947b657b347211dc9388754e05d30d0467Eric Laurent        virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc = NULL);
132c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kasten        virtual status_t registerEffect(const effect_descriptor_t *desc,
1331c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                        audio_io_handle_t io,
134d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                        uint32_t strategy,
135d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                        int session,
136d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                        int id);
137d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual status_t unregisterEffect(int id);
138582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        virtual status_t setEffectEnabled(int id, bool enabled);
139d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
14036291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent        virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
141dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        // return whether a stream is playing remotely, override to change the definition of
142dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        //   local/remote playback, used for instance by notification manager to not make
143dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        //   media players lose audio focus when not playing locally
144dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        virtual bool isStreamActiveRemotely(int stream, uint32_t inPastMs = 0) const;
145abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        virtual bool isSourceActive(audio_source_t source) const;
14636291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent
147de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t dump(int fd);
148de55cfb02db874136caf9c05927cee4028296147Eric Laurent
149b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald        virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo);
150b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald
151de55cfb02db874136caf9c05927cee4028296147Eric Laurentprotected:
152de55cfb02db874136caf9c05927cee4028296147Eric Laurent
153de55cfb02db874136caf9c05927cee4028296147Eric Laurent        enum routing_strategy {
154de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_MEDIA,
155de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_PHONE,
156de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_SONIFICATION,
15712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            STRATEGY_SONIFICATION_RESPECTFUL,
158de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_DTMF,
159c16ac09f510437e8340be691720177a490ae78f0Eric Laurent            STRATEGY_ENFORCED_AUDIBLE,
160de55cfb02db874136caf9c05927cee4028296147Eric Laurent            NUM_STRATEGIES
161de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
162de55cfb02db874136caf9c05927cee4028296147Eric Laurent
163cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        // 4 points to define the volume attenuation curve, each characterized by the volume
164cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
165cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        // we use 100 steps to avoid rounding errors when computing the volume in volIndexToAmpl()
166cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
167cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3, VOLCNT = 4};
168cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
169cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        class VolumeCurvePoint
170cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        {
171cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        public:
172cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            int mIndex;
173cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            float mDBAttenuation;
174cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        };
175cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
176e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // device categories used for volume curve management.
177e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        enum device_category {
178e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            DEVICE_CATEGORY_HEADSET,
179e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            DEVICE_CATEGORY_SPEAKER,
180e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            DEVICE_CATEGORY_EARPIECE,
181e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            DEVICE_CATEGORY_CNT
182e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        };
183e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
18470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        class IOProfile;
18570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        class HwModule {
18770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        public:
18870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    HwModule(const char *name);
18970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    ~HwModule();
19070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
19170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            void dump(int fd);
19270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
19370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const char *const mName; // base name of the audio HW module (primary, a2dp ...)
19470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            audio_module_handle_t mHandle;
19570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            Vector <IOProfile *> mOutputProfiles; // output profiles exposed by this module
19670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            Vector <IOProfile *> mInputProfiles;  // input profiles exposed by this module
19770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        };
19870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // the IOProfile class describes the capabilities of an output or input stream.
2005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // It is currently assumed that all combination of listed parameters are supported.
2015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // It is used by the policy manager to determine if an output or input is suitable for
2025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // a given use case,  open/close it accordingly and connect/disconnect audio tracks
2035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // to/from it.
2045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        class IOProfile
2055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
20670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        public:
20770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile(HwModule *module);
2085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ~IOProfile();
2095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
21070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            bool isCompatibleProfile(audio_devices_t device,
21170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                     uint32_t samplingRate,
21270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                     uint32_t format,
21370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                     uint32_t channelMask,
2140977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                     audio_output_flags_t flags) const;
21570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
2165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            void dump(int fd);
2175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // by convention, "0' in the first entry in mSamplingRates, mChannelMasks or mFormats
2193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // indicates the supported parameters should be read from the output stream
2203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // after it is opened for the first time
2215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            Vector <uint32_t> mSamplingRates; // supported sampling rates
2225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            Vector <audio_channel_mask_t> mChannelMasks; // supported channel masks
2235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            Vector <audio_format_t> mFormats; // supported audio formats
2245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t mSupportedDevices; // supported devices (devices this output can be
2255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                               // routed to)
2260977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            audio_output_flags_t mFlags; // attribute flags (e.g primary output,
2275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                // direct output...). For outputs only.
22870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            HwModule *mModule;                     // audio HW module exposing this I/O stream
2295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        };
2305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
231e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // default volume curve
232e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        static const VolumeCurvePoint sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT];
233e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // default volume curve for media strategy
234e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        static const VolumeCurvePoint sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT];
235e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // volume curve for media strategy on speakers
236e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        static const VolumeCurvePoint sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT];
237e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // volume curve for sonification strategy on speakers
238e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        static const VolumeCurvePoint sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT];
23918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        static const VolumeCurvePoint sSpeakerSonificationVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT];
240ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        static const VolumeCurvePoint sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT];
24118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        static const VolumeCurvePoint sDefaultSystemVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT];
242ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        static const VolumeCurvePoint sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT];
2430d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        static const VolumeCurvePoint sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT];
2440d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        static const VolumeCurvePoint sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT];
245ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // default volume curves per stream and device category. See initializeVolumeCurves()
246ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        static const VolumeCurvePoint *sVolumeProfiles[AUDIO_STREAM_CNT][DEVICE_CATEGORY_CNT];
247cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
248de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
249de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // and keep track of the usage of this output by each audio stream type.
250de55cfb02db874136caf9c05927cee4028296147Eric Laurent        class AudioOutputDescriptor
251de55cfb02db874136caf9c05927cee4028296147Eric Laurent        {
252de55cfb02db874136caf9c05927cee4028296147Eric Laurent        public:
2535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor(const IOProfile *profile);
254de55cfb02db874136caf9c05927cee4028296147Eric Laurent
255de55cfb02db874136caf9c05927cee4028296147Eric Laurent            status_t    dump(int fd);
256de55cfb02db874136caf9c05927cee4028296147Eric Laurent
257dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            audio_devices_t device() const;
25880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            void changeRefCount(AudioSystem::stream_type stream, int delta);
259b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald
2605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            bool isDuplicated() const { return (mOutput1 != NULL && mOutput2 != NULL); }
261f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t supportedDevices();
2625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            uint32_t latency();
2635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            bool sharesHwModuleWith(const AudioOutputDescriptor *outputDesc);
26480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            bool isActive(uint32_t inPastMs = 0) const;
26580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            bool isStreamActive(AudioSystem::stream_type stream,
26680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                uint32_t inPastMs = 0,
26780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                nsecs_t sysTime = 0) const;
26880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            bool isStrategyActive(routing_strategy strategy,
26980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                             uint32_t inPastMs = 0,
27080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                             nsecs_t sysTime = 0) const;
271de55cfb02db874136caf9c05927cee4028296147Eric Laurent
272de55cfb02db874136caf9c05927cee4028296147Eric Laurent            audio_io_handle_t mId;              // output handle
273de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mSamplingRate;             //
27470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            audio_format_t mFormat;             //
27570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            audio_channel_mask_t mChannelMask;     // output configuration
276de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mLatency;                  //
2770977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            audio_output_flags_t mFlags;   //
278f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t mDevice;                   // current device this output is routed to
279de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mRefCount[AudioSystem::NUM_STREAM_TYPES]; // number of streams of each type using this output
28036291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent            nsecs_t mStopTime[AudioSystem::NUM_STREAM_TYPES];
281de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioOutputDescriptor *mOutput1;    // used by duplicated outputs: first output
282de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioOutputDescriptor *mOutput2;    // used by duplicated outputs: second output
283de55cfb02db874136caf9c05927cee4028296147Eric Laurent            float mCurVolume[AudioSystem::NUM_STREAM_TYPES];   // current stream volume
284de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int mMuteCount[AudioSystem::NUM_STREAM_TYPES];     // mute request counter
2855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            const IOProfile *mProfile;          // I/O profile this output derives from
2865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            bool mStrategyMutedByDevice[NUM_STRATEGIES]; // strategies muted because of incompatible
2875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                // device selection. See checkDeviceMuteStrategies()
2885a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            uint32_t mDirectOpenCount; // number of clients using this output (direct outputs only)
289de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
290de55cfb02db874136caf9c05927cee4028296147Eric Laurent
291de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
292de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // and keep track of the usage of this input.
293de55cfb02db874136caf9c05927cee4028296147Eric Laurent        class AudioInputDescriptor
294de55cfb02db874136caf9c05927cee4028296147Eric Laurent        {
295de55cfb02db874136caf9c05927cee4028296147Eric Laurent        public:
2965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioInputDescriptor(const IOProfile *profile);
297de55cfb02db874136caf9c05927cee4028296147Eric Laurent
298de55cfb02db874136caf9c05927cee4028296147Eric Laurent            status_t    dump(int fd);
299de55cfb02db874136caf9c05927cee4028296147Eric Laurent
300de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mSamplingRate;                     //
30170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            audio_format_t mFormat;                     // input configuration
30270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            audio_channel_mask_t mChannelMask;             //
303f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t mDevice;                    // current device this input is routed to
304de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mRefCount;                         // number of AudioRecord clients using this output
305f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            int      mInputSource;                      // input source selected by application (mediarecorder.h)
3065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            const IOProfile *mProfile;                  // I/O profile this output derives from
307de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
308de55cfb02db874136caf9c05927cee4028296147Eric Laurent
309de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // stream descriptor used for volume control
310de55cfb02db874136caf9c05927cee4028296147Eric Laurent        class StreamDescriptor
311de55cfb02db874136caf9c05927cee4028296147Eric Laurent        {
312de55cfb02db874136caf9c05927cee4028296147Eric Laurent        public:
313c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            StreamDescriptor();
314de55cfb02db874136caf9c05927cee4028296147Eric Laurent
315c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            int getVolumeIndex(audio_devices_t device);
316c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            void dump(int fd);
317de55cfb02db874136caf9c05927cee4028296147Eric Laurent
318de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int mIndexMin;      // min volume index
319de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int mIndexMax;      // max volume index
320c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            KeyedVector<audio_devices_t, int> mIndexCur;   // current volume index per device
321de55cfb02db874136caf9c05927cee4028296147Eric Laurent            bool mCanBeMuted;   // true is the stream can be muted
322f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi
323e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            const VolumeCurvePoint *mVolumeCurve[DEVICE_CATEGORY_CNT];
324de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
325de55cfb02db874136caf9c05927cee4028296147Eric Laurent
326d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // stream descriptor used for volume control
327d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        class EffectDescriptor
328d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        {
329d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        public:
330d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
331d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            status_t dump(int fd);
332d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
3331c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            int mIo;                // io the effect is attached to
334d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            routing_strategy mStrategy; // routing strategy the effect is associated to
335d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            int mSession;               // audio session the effect is on
336d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            effect_descriptor_t mDesc;  // effect descriptor
337582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            bool mEnabled;              // enabled state: CPU load being used or not
338d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        };
339d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
340de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc);
341de55cfb02db874136caf9c05927cee4028296147Eric Laurent
342de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // return the strategy corresponding to a given stream type
343de55cfb02db874136caf9c05927cee4028296147Eric Laurent        static routing_strategy getStrategy(AudioSystem::stream_type stream);
344f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
345de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // return appropriate device for streams handled by the specified strategy according to current
346de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // phone state, connected devices...
3475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // if fromCache is true, the device is returned from mDeviceForStrategy[],
3485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // otherwise it is determine by current state
3495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // (device connected,phone state, force use, a2dp output...)
350de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // This allows to:
351de55cfb02db874136caf9c05927cee4028296147Eric Laurent        //  1 speed up process when the state is stable (when starting or stopping an output)
352de55cfb02db874136caf9c05927cee4028296147Eric Laurent        //  2 access to either current device selection (fromCache == true) or
353de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // "future" device selection (fromCache == false) when called from a context
354de55cfb02db874136caf9c05927cee4028296147Eric Laurent        //  where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
355c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        //  before updateDevicesAndOutputs() is called.
3565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        virtual audio_devices_t getDeviceForStrategy(routing_strategy strategy,
3575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                     bool fromCache);
358f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
359b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        // change the route of the specified output. Returns the number of ms we have slept to
360b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        // allow new routing to take effect in certain cases.
361b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t setOutputDevice(audio_io_handle_t output,
362f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                             audio_devices_t device,
363f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                             bool force = false,
364f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                             int delayMs = 0);
365f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
366de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // select input device corresponding to requested audio source
367f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        virtual audio_devices_t getDeviceForInputSource(int inputSource);
368f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
369de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // return io handle of active input or 0 if no input is active
3706d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        //    Only considers inputs from physical devices (e.g. main mic, headset mic) when
3716d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        //    ignoreVirtualInputs is true.
3726d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        audio_io_handle_t getActiveInput(bool ignoreVirtualInputs = true);
373f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
374e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // initialize volume curves for each strategy and device category
375e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        void initializeVolumeCurves();
376f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
377de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // compute the actual volume for a given stream according to the requested index and a particular
378de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // device
379f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        virtual float computeVolume(int stream, int index, audio_io_handle_t output, audio_devices_t device);
380f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
381de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // check that volume change is permitted, compute and send new volume to audio hardware
382f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
383f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
384de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // apply all stream volumes to the specified output and device
385f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        void applyStreamVolumes(audio_io_handle_t output, audio_devices_t device, int delayMs = 0, bool force = false);
386f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
387de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // Mute or unmute all streams handled by the specified strategy on the specified output
38801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        void setStrategyMute(routing_strategy strategy,
38901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                             bool on,
39001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                             audio_io_handle_t output,
39101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                             int delayMs = 0,
39201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                             audio_devices_t device = (audio_devices_t)0);
393f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
394de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // Mute or unmute the stream on the specified output
39501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        void setStreamMute(int stream,
39601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                           bool on,
39701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                           audio_io_handle_t output,
39801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                           int delayMs = 0,
39901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                           audio_devices_t device = (audio_devices_t)0);
400f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
401de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // handle special cases for sonification strategy while in call: mute streams or replace by
402de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // a special tone in the device used for communication
403de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void handleIncallSonification(int stream, bool starting, bool stateChange);
404f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
4053e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        // true if device is in a telephony or VoIP call
4063e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        virtual bool isInCall();
407f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
4083e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        // true if given state represents a device in a telephony or VoIP call
4093e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        virtual bool isStateInCall(int state);
410f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
411b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // when a device is connected, checks if an open output can be routed
412b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // to this device. If none is open, tries to open one of the available outputs.
413b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // Returns an output suitable to this device or 0.
414b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // when a device is disconnected, checks if an output is not used any more and
415b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // returns its handle if any.
416b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // transfers the audio tracks and effects from one output thread to another accordingly.
4173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        status_t checkOutputsForDevice(audio_devices_t device,
4183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                       AudioSystem::device_connection_state state,
4193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                       SortedVector<audio_io_handle_t>& outputs);
420f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
421b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // close an output and its companion duplicating output.
422b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        void closeOutput(audio_io_handle_t output);
423f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
424b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // checks and if necessary changes outputs used for all strategies.
425b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // must be called every time a condition that affects the output choice for a given strategy
426b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // changes: connected device, phone state, force use...
427c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // Must be called before updateDevicesAndOutputs()
428d6e68c3ae5677bc6127628d1c700b11c8f0fcdafEric Laurent        void checkOutputForStrategy(routing_strategy strategy);
429f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
430de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // Same as checkOutputForStrategy() but for a all strategies in order of priority
431d6e68c3ae5677bc6127628d1c700b11c8f0fcdafEric Laurent        void checkOutputForAllStrategies();
432f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
4332f2db765228f110b37a2851d1199d3c72d313c6dEric Laurent        // manages A2DP output suspend/restore according to phone state and BT SCO usage
4342f2db765228f110b37a2851d1199d3c72d313c6dEric Laurent        void checkA2dpSuspend();
435f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
436b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // returns the A2DP output handle if it is open or 0 otherwise
437b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        audio_io_handle_t getA2dpOutput();
438f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
439de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // selects the most appropriate device on output for current state
440de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // must be called every time a condition that affects the device choice for a given output is
441de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // changed: connected device, phone state, force use, output start, output stop..
442de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // see getDeviceForStrategy() for the use of fromCache parameter
443f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
4445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t getNewDevice(audio_io_handle_t output, bool fromCache);
445de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // updates cache of device used by all strategies (mDeviceForStrategy[])
446de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // must be called every time a condition that affects the device choice for a given strategy is
447de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // changed: connected device, phone state, force use...
448de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
449de55cfb02db874136caf9c05927cee4028296147Eric Laurent         // Must be called after checkOutputForAllStrategies()
450f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
451c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        void updateDevicesAndOutputs();
452f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
453d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual uint32_t getMaxEffectsCpuLoad();
454d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual uint32_t getMaxEffectsMemory();
455de55cfb02db874136caf9c05927cee4028296147Eric Laurent#ifdef AUDIO_POLICY_TEST
456de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual     bool        threadLoop();
457de55cfb02db874136caf9c05927cee4028296147Eric Laurent                    void        exit();
458de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int testOutputIndex(audio_io_handle_t output);
459de55cfb02db874136caf9c05927cee4028296147Eric Laurent#endif //AUDIO_POLICY_TEST
460de55cfb02db874136caf9c05927cee4028296147Eric Laurent
461582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        status_t setEffectEnabled(EffectDescriptor *pDesc, bool enabled);
462582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
463e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // returns the category the device belongs to with regard to volume curve management
464f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        static device_category getDeviceCategory(audio_devices_t device);
465f7db03562644b03447eb6351156e53188ea0c616Eric Laurent
466c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        // extract one device relevant for volume control from multiple device selection
467c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        static audio_devices_t getDeviceForVolume(audio_devices_t device);
468e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
469c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        SortedVector<audio_io_handle_t> getOutputsForDevice(audio_devices_t device,
470c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs);
471b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        bool vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
472b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                           SortedVector<audio_io_handle_t>& outputs2);
473de55cfb02db874136caf9c05927cee4028296147Eric Laurent
474b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        // mute/unmute strategies using an incompatible device combination
475b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        // if muting, wait for the audio in pcm buffer to be drained before proceeding
476b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        // if unmuting, unmute only after the specified delay
477b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        // Returns the number of ms waited
478b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t  checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
4799029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                            audio_devices_t prevDevice,
4809029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                            uint32_t delayMs);
4815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
4825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t selectOutput(const SortedVector<audio_io_handle_t>& outputs,
4835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                       AudioSystem::output_flags flags);
4845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        IOProfile *getInputProfile(audio_devices_t device,
4855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                   uint32_t samplingRate,
4865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                   uint32_t format,
4875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                   uint32_t channelMask);
4883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        IOProfile *getProfileForDirectOutput(audio_devices_t device,
48970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                       uint32_t samplingRate,
49070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                       uint32_t format,
49170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                       uint32_t channelMask,
4920977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                       audio_output_flags_t flags);
49345c763947b657b347211dc9388754e05d30d0467Eric Laurent
49445c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t selectOutputForEffects(const SortedVector<audio_io_handle_t>& outputs);
49545c763947b657b347211dc9388754e05d30d0467Eric Laurent
496000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        bool isNonOffloadableEffectEnabled();
497000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
4985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        //
4995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Audio policy configuration file parsing (audio_policy.conf)
5005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        //
5015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        static uint32_t stringToEnum(const struct StringToEnum *table,
5025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                     size_t size,
5035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                     const char *name);
50418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        static bool stringToBool(const char *value);
5050977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent        static audio_output_flags_t parseFlagNames(char *name);
5065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        static audio_devices_t parseDeviceNames(char *name);
5075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        void loadSamplingRates(char *name, IOProfile *profile);
5085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        void loadFormats(char *name, IOProfile *profile);
5095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        void loadOutChannels(char *name, IOProfile *profile);
5105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        void loadInChannels(char *name, IOProfile *profile);
51170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        status_t loadOutput(cnode *root,  HwModule *module);
51270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        status_t loadInput(cnode *root,  HwModule *module);
5135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        void loadHwModule(cnode *root);
5145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        void loadHwModules(cnode *root);
5155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        void loadGlobalConfig(cnode *root);
5165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        status_t loadAudioPolicyConfig(const char *path);
517739022f26a7127ba76a98dda65411496086114a7Dima Zavin        void defaultAudioPolicyConfig(void);
5185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
5195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
520b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioPolicyClientInterface *mpClientInterface;  // audio policy client interface
521b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        audio_io_handle_t mPrimaryOutput;              // primary output handle
522c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // list of descriptors for outputs currently opened
523c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs;
524c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // copy of mOutputs before setDeviceConnectionState() opens new outputs
525c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // reset to mOutputs when updateDevicesAndOutputs() is called.
526c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mPreviousOutputs;
527b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        DefaultKeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs;     // list of input descriptors
5285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t mAvailableOutputDevices; // bit field of all available output devices
5295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t mAvailableInputDevices; // bit field of all available input devices
530ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                                                // without AUDIO_DEVICE_BIT_IN to allow direct bit
531ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                                                // field comparisons
532de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int mPhoneState;                                                    // current phone state
533de55cfb02db874136caf9c05927cee4028296147Eric Laurent        AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE];   // current forced use configuration
534de55cfb02db874136caf9c05927cee4028296147Eric Laurent
535de55cfb02db874136caf9c05927cee4028296147Eric Laurent        StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES];           // stream descriptors for volume control
536de55cfb02db874136caf9c05927cee4028296147Eric Laurent        String8 mA2dpDeviceAddress;                                         // A2DP device MAC address
537de55cfb02db874136caf9c05927cee4028296147Eric Laurent        String8 mScoDeviceAddress;                                          // SCO device MAC address
538599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        String8 mUsbCardAndDevice; // USB audio ALSA card and device numbers:
539599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                                   // card=<card_number>;device=<><device_number>
540de55cfb02db874136caf9c05927cee4028296147Eric Laurent        bool    mLimitRingtoneVolume;                                       // limit ringtone volume to music volume if headset connected
541f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        audio_devices_t mDeviceForStrategy[NUM_STRATEGIES];
542564c30323762685cdb811e2a985ad374a691745eEric Laurent        float   mLastVoiceVolume;                                           // last voice volume value sent to audio HAL
543de55cfb02db874136caf9c05927cee4028296147Eric Laurent
544d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
545d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
546d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // Maximum memory allocated to audio effects in KB
547d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        static const uint32_t MAX_EFFECTS_MEMORY = 512;
548d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
549d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        uint32_t mTotalEffectsMemory;  // current memory used by effects
550d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        KeyedVector<int, EffectDescriptor *> mEffects;  // list of registered audio effects
5512f2db765228f110b37a2851d1199d3c72d313c6dEric Laurent        bool    mA2dpSuspended;  // true if A2DP output is suspended
5525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mHasA2dp; // true on platforms with support for bluetooth A2DP
553599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        bool mHasUsb; // true on platforms with support for USB audio
55448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        bool mHasRemoteSubmix; // true on platforms with support for remote presentation of a submix
5555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t mAttachedOutputDevices; // output devices always available on the platform
5565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t mDefaultOutputDevice; // output device selected by default at boot time
5575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              // (must be in mAttachedOutputDevices)
55818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        bool mSpeakerDrcEnabled;// true on devices that use DRC on the DEVICE_CATEGORY_SPEAKER path
55918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                                // to boost soft sounds, used to adjust volume curves accordingly
5605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
56170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        Vector <HwModule *> mHwModules;
562d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
563de55cfb02db874136caf9c05927cee4028296147Eric Laurent#ifdef AUDIO_POLICY_TEST
564de55cfb02db874136caf9c05927cee4028296147Eric Laurent        Mutex   mLock;
565de55cfb02db874136caf9c05927cee4028296147Eric Laurent        Condition mWaitWorkCV;
566de55cfb02db874136caf9c05927cee4028296147Eric Laurent
567de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int             mCurOutput;
568de55cfb02db874136caf9c05927cee4028296147Eric Laurent        bool            mDirectOutput;
569de55cfb02db874136caf9c05927cee4028296147Eric Laurent        audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
570de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int             mTestInput;
571de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestDevice;
572de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestSamplingRate;
573de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestFormat;
574de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestChannels;
575de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestLatencyMs;
576de55cfb02db874136caf9c05927cee4028296147Eric Laurent#endif //AUDIO_POLICY_TEST
577f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi
578f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Triviprivate:
579f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        static float volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
580f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi                int indexInUi);
58112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        // updates device caching and output for streams that can influence the
58212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        //    routing of notifications
58312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        void handleNotificationRoutingForStream(AudioSystem::stream_type stream);
5846d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        static bool isVirtualInputDevice(audio_devices_t device);
585de55cfb02db874136caf9c05927cee4028296147Eric Laurent};
586de55cfb02db874136caf9c05927cee4028296147Eric Laurent
587de55cfb02db874136caf9c05927cee4028296147Eric Laurent};
588