AudioPolicyManagerBase.h revision 582a15744be109fb630db7dbd1d1bdf22ff44f12
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>
20de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <utils/Timers.h>
21de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <utils/Errors.h>
22de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <utils/KeyedVector.h>
23de55cfb02db874136caf9c05927cee4028296147Eric Laurent#include <hardware_legacy/AudioPolicyInterface.h>
24de55cfb02db874136caf9c05927cee4028296147Eric Laurent
25de55cfb02db874136caf9c05927cee4028296147Eric Laurent
26e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
27e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavin    using android::KeyedVector;
28de55cfb02db874136caf9c05927cee4028296147Eric Laurent
29de55cfb02db874136caf9c05927cee4028296147Eric Laurent// ----------------------------------------------------------------------------
30de55cfb02db874136caf9c05927cee4028296147Eric Laurent
31de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define MAX_DEVICE_ADDRESS_LEN 20
32de55cfb02db874136caf9c05927cee4028296147Eric Laurent// Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
33de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
34de55cfb02db874136caf9c05927cee4028296147Eric Laurent// Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
35de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define SONIFICATION_HEADSET_VOLUME_MIN  0.016
3636291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent// Time in milliseconds during which we consider that music is still active after a music
37de55cfb02db874136caf9c05927cee4028296147Eric Laurent// track was stopped - see computeVolume()
3836291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent#define SONIFICATION_HEADSET_MUSIC_DELAY  5000
39195368626339aefc4732661d009e8de1aacc24d1Eric Laurent// Time in milliseconds during witch some streams are muted while the audio path
40195368626339aefc4732661d009e8de1aacc24d1Eric Laurent// is switched
41195368626339aefc4732661d009e8de1aacc24d1Eric Laurent#define MUTE_TIME_MS 2000
42de55cfb02db874136caf9c05927cee4028296147Eric Laurent
43de55cfb02db874136caf9c05927cee4028296147Eric Laurent#define NUM_TEST_OUTPUTS 5
44de55cfb02db874136caf9c05927cee4028296147Eric Laurent
45f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi#define NUM_VOL_CURVE_KNEES 2
46f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi
47de55cfb02db874136caf9c05927cee4028296147Eric Laurent// ----------------------------------------------------------------------------
48de55cfb02db874136caf9c05927cee4028296147Eric Laurent// AudioPolicyManagerBase implements audio policy manager behavior common to all platforms.
49de55cfb02db874136caf9c05927cee4028296147Eric Laurent// Each platform must implement an AudioPolicyManager class derived from AudioPolicyManagerBase
50d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent// and override methods for which the platform specific behavior differs from the implementation
51de55cfb02db874136caf9c05927cee4028296147Eric Laurent// in AudioPolicyManagerBase. Even if no specific behavior is required, the AudioPolicyManager
52de55cfb02db874136caf9c05927cee4028296147Eric Laurent// class must be implemented as well as the class factory function createAudioPolicyManager()
53de55cfb02db874136caf9c05927cee4028296147Eric Laurent// and provided in a shared library libaudiopolicy.so.
54de55cfb02db874136caf9c05927cee4028296147Eric Laurent// ----------------------------------------------------------------------------
55de55cfb02db874136caf9c05927cee4028296147Eric Laurent
56de55cfb02db874136caf9c05927cee4028296147Eric Laurentclass AudioPolicyManagerBase: public AudioPolicyInterface
57de55cfb02db874136caf9c05927cee4028296147Eric Laurent#ifdef AUDIO_POLICY_TEST
58de55cfb02db874136caf9c05927cee4028296147Eric Laurent    , public Thread
59de55cfb02db874136caf9c05927cee4028296147Eric Laurent#endif //AUDIO_POLICY_TEST
60de55cfb02db874136caf9c05927cee4028296147Eric Laurent{
61de55cfb02db874136caf9c05927cee4028296147Eric Laurent
62de55cfb02db874136caf9c05927cee4028296147Eric Laurentpublic:
63de55cfb02db874136caf9c05927cee4028296147Eric Laurent                AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface);
64de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual ~AudioPolicyManagerBase();
65de55cfb02db874136caf9c05927cee4028296147Eric Laurent
66de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // AudioPolicyInterface
67de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
68de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                          AudioSystem::device_connection_state state,
69de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                          const char *device_address);
70de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device,
71de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                                              const char *device_address);
72de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void setPhoneState(int state);
73de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void setRingerMode(uint32_t mode, uint32_t mask);
74de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
75de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
76de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void setSystemProperty(const char* property, const char* value);
7761f357948648ff45d35ec79725db4e1b12c268dcEric Laurent        virtual status_t initCheck();
78de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
79d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            uint32_t samplingRate = 0,
80d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            uint32_t format = AudioSystem::FORMAT_DEFAULT,
81d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            uint32_t channels = 0,
82d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                            AudioSystem::output_flags flags =
83d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                                    AudioSystem::OUTPUT_FLAG_INDIRECT);
84d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual status_t startOutput(audio_io_handle_t output,
85d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                     AudioSystem::stream_type stream,
86d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                     int session = 0);
87d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual status_t stopOutput(audio_io_handle_t output,
88d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                    AudioSystem::stream_type stream,
89d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                    int session = 0);
90de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void releaseOutput(audio_io_handle_t output);
91de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual audio_io_handle_t getInput(int inputSource,
92de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            uint32_t samplingRate,
93de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            uint32_t format,
94de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            uint32_t channels,
95de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                            AudioSystem::audio_in_acoustics acoustics);
96de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // indicates to the audio policy manager that the input starts being used.
97de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t startInput(audio_io_handle_t input);
98de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // indicates to the audio policy manager that the input stops being used.
99de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t stopInput(audio_io_handle_t input);
100de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void releaseInput(audio_io_handle_t input);
101de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual void initStreamVolume(AudioSystem::stream_type stream,
102de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                    int indexMin,
103de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                    int indexMax);
104de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index);
105de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index);
106de55cfb02db874136caf9c05927cee4028296147Eric Laurent
107d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // return the strategy corresponding to a given stream type
108d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream);
109d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
110fafb18fb31cc4ab3c73585ba508f7643c9a9e3adGlenn Kasten        // return the enabled output devices for the given stream type
111fafb18fb31cc4ab3c73585ba508f7643c9a9e3adGlenn Kasten        virtual uint32_t getDevicesForStream(AudioSystem::stream_type stream);
112fafb18fb31cc4ab3c73585ba508f7643c9a9e3adGlenn Kasten
113d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc);
114d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual status_t registerEffect(effect_descriptor_t *desc,
1151c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                        audio_io_handle_t io,
116d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                        uint32_t strategy,
117d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                        int session,
118d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent                                        int id);
119d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual status_t unregisterEffect(int id);
120582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        virtual status_t setEffectEnabled(int id, bool enabled);
121d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
12236291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent        virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
12336291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent
124de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual status_t dump(int fd);
125de55cfb02db874136caf9c05927cee4028296147Eric Laurent
126de55cfb02db874136caf9c05927cee4028296147Eric Laurentprotected:
127de55cfb02db874136caf9c05927cee4028296147Eric Laurent
128de55cfb02db874136caf9c05927cee4028296147Eric Laurent        enum routing_strategy {
129de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_MEDIA,
130de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_PHONE,
131de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_SONIFICATION,
132de55cfb02db874136caf9c05927cee4028296147Eric Laurent            STRATEGY_DTMF,
133de55cfb02db874136caf9c05927cee4028296147Eric Laurent            NUM_STRATEGIES
134de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
135de55cfb02db874136caf9c05927cee4028296147Eric Laurent
136de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
137de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // and keep track of the usage of this output by each audio stream type.
138de55cfb02db874136caf9c05927cee4028296147Eric Laurent        class AudioOutputDescriptor
139de55cfb02db874136caf9c05927cee4028296147Eric Laurent        {
140de55cfb02db874136caf9c05927cee4028296147Eric Laurent        public:
141de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioOutputDescriptor();
142de55cfb02db874136caf9c05927cee4028296147Eric Laurent
143de55cfb02db874136caf9c05927cee4028296147Eric Laurent            status_t    dump(int fd);
144de55cfb02db874136caf9c05927cee4028296147Eric Laurent
145de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t device();
146de55cfb02db874136caf9c05927cee4028296147Eric Laurent            void changeRefCount(AudioSystem::stream_type, int delta);
147de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t refCount();
148de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t strategyRefCount(routing_strategy strategy);
149de55cfb02db874136caf9c05927cee4028296147Eric Laurent            bool isUsedByStrategy(routing_strategy strategy) { return (strategyRefCount(strategy) != 0);}
150de55cfb02db874136caf9c05927cee4028296147Eric Laurent            bool isDuplicated() { return (mOutput1 != NULL && mOutput2 != NULL); }
151de55cfb02db874136caf9c05927cee4028296147Eric Laurent
152de55cfb02db874136caf9c05927cee4028296147Eric Laurent            audio_io_handle_t mId;              // output handle
153de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mSamplingRate;             //
154de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mFormat;                   //
155de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mChannels;                 // output configuration
156de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mLatency;                  //
157de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioSystem::output_flags mFlags;   //
158de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mDevice;                   // current device this output is routed to
159de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mRefCount[AudioSystem::NUM_STREAM_TYPES]; // number of streams of each type using this output
16036291a74cd212c4172ac054ad0eec686b7e15c96Eric Laurent            nsecs_t mStopTime[AudioSystem::NUM_STREAM_TYPES];
161de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioOutputDescriptor *mOutput1;    // used by duplicated outputs: first output
162de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioOutputDescriptor *mOutput2;    // used by duplicated outputs: second output
163de55cfb02db874136caf9c05927cee4028296147Eric Laurent            float mCurVolume[AudioSystem::NUM_STREAM_TYPES];   // current stream volume
164de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int mMuteCount[AudioSystem::NUM_STREAM_TYPES];     // mute request counter
165de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
166de55cfb02db874136caf9c05927cee4028296147Eric Laurent
167de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
168de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // and keep track of the usage of this input.
169de55cfb02db874136caf9c05927cee4028296147Eric Laurent        class AudioInputDescriptor
170de55cfb02db874136caf9c05927cee4028296147Eric Laurent        {
171de55cfb02db874136caf9c05927cee4028296147Eric Laurent        public:
172de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioInputDescriptor();
173de55cfb02db874136caf9c05927cee4028296147Eric Laurent
174de55cfb02db874136caf9c05927cee4028296147Eric Laurent            status_t    dump(int fd);
175de55cfb02db874136caf9c05927cee4028296147Eric Laurent
176de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mSamplingRate;                     //
177de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mFormat;                           // input configuration
178de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mChannels;                         //
179de55cfb02db874136caf9c05927cee4028296147Eric Laurent            AudioSystem::audio_in_acoustics mAcoustics; //
180de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mDevice;                           // current device this input is routed to
181de55cfb02db874136caf9c05927cee4028296147Eric Laurent            uint32_t mRefCount;                         // number of AudioRecord clients using this output
182de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int      mInputSource;                     // input source selected by application (mediarecorder.h)
183de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
184de55cfb02db874136caf9c05927cee4028296147Eric Laurent
185de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // stream descriptor used for volume control
186de55cfb02db874136caf9c05927cee4028296147Eric Laurent        class StreamDescriptor
187de55cfb02db874136caf9c05927cee4028296147Eric Laurent        {
188de55cfb02db874136caf9c05927cee4028296147Eric Laurent        public:
189de55cfb02db874136caf9c05927cee4028296147Eric Laurent            StreamDescriptor()
190de55cfb02db874136caf9c05927cee4028296147Eric Laurent            :   mIndexMin(0), mIndexMax(1), mIndexCur(1), mCanBeMuted(true) {}
191de55cfb02db874136caf9c05927cee4028296147Eric Laurent
192de55cfb02db874136caf9c05927cee4028296147Eric Laurent            void dump(char* buffer, size_t size);
193de55cfb02db874136caf9c05927cee4028296147Eric Laurent
194de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int mIndexMin;      // min volume index
195de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int mIndexMax;      // max volume index
196de55cfb02db874136caf9c05927cee4028296147Eric Laurent            int mIndexCur;      // current volume index
197de55cfb02db874136caf9c05927cee4028296147Eric Laurent            bool mCanBeMuted;   // true is the stream can be muted
198f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi
199f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi            // 4 points to define the volume attenuation curve, each characterized by the volume
200f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi            // index (from 0 to 100) at which they apply, and the attenuation in dB at that index.
201f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi            int mVolIndex[NUM_VOL_CURVE_KNEES+2];   // minimum index, index at knees, and max index
202f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi            float mVolDbAtt[NUM_VOL_CURVE_KNEES+2]; // largest attenuation, attenuation at knees,
203f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi                                                    //     and attenuation at max vol (usually 0dB)
204f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi            // indices in mVolIndex and mVolDbAtt respectively for points at lowest volume, knee 1,
205f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi            //    knee 2 and highest volume.
206f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi            enum { VOLMIN = 0, VOLKNEE1 = 1, VOLKNEE2 = 2, VOLMAX = 3 };
207de55cfb02db874136caf9c05927cee4028296147Eric Laurent        };
208de55cfb02db874136caf9c05927cee4028296147Eric Laurent
209d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // stream descriptor used for volume control
210d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        class EffectDescriptor
211d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        {
212d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        public:
213d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
214d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            status_t dump(int fd);
215d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
2161c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            int mIo;                // io the effect is attached to
217d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            routing_strategy mStrategy; // routing strategy the effect is associated to
218d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            int mSession;               // audio session the effect is on
219d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent            effect_descriptor_t mDesc;  // effect descriptor
220582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            bool mEnabled;              // enabled state: CPU load being used or not
221d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        };
222d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
223de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc);
224de55cfb02db874136caf9c05927cee4028296147Eric Laurent
225de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // return the strategy corresponding to a given stream type
226de55cfb02db874136caf9c05927cee4028296147Eric Laurent        static routing_strategy getStrategy(AudioSystem::stream_type stream);
227de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // return appropriate device for streams handled by the specified strategy according to current
228de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // phone state, connected devices...
229de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // if fromCache is true, the device is returned from mDeviceForStrategy[], otherwise it is determined
230de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // by current state (device connected, phone state, force use, a2dp output...)
231de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // This allows to:
232de55cfb02db874136caf9c05927cee4028296147Eric Laurent        //  1 speed up process when the state is stable (when starting or stopping an output)
233de55cfb02db874136caf9c05927cee4028296147Eric Laurent        //  2 access to either current device selection (fromCache == true) or
234de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // "future" device selection (fromCache == false) when called from a context
235de55cfb02db874136caf9c05927cee4028296147Eric Laurent        //  where conditions are changing (setDeviceConnectionState(), setPhoneState()...) AND
236de55cfb02db874136caf9c05927cee4028296147Eric Laurent        //  before updateDeviceForStrategy() is called.
237de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual uint32_t getDeviceForStrategy(routing_strategy strategy, bool fromCache = true);
238de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // change the route of the specified output
239de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void setOutputDevice(audio_io_handle_t output, uint32_t device, bool force = false, int delayMs = 0);
240de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // select input device corresponding to requested audio source
241de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual uint32_t getDeviceForInputSource(int inputSource);
242de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // return io handle of active input or 0 if no input is active
243de55cfb02db874136caf9c05927cee4028296147Eric Laurent        audio_io_handle_t getActiveInput();
244f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi        virtual void initializeVolumeCurves();
245de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // compute the actual volume for a given stream according to the requested index and a particular
246de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // device
247e6c221466fe4ec2940c672126999d28ce01a49f1Eric Laurent        virtual float computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device);
248de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // check that volume change is permitted, compute and send new volume to audio hardware
249de55cfb02db874136caf9c05927cee4028296147Eric Laurent        status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs = 0, bool force = false);
250de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // apply all stream volumes to the specified output and device
2513775706211178bf0d252fd30b9ff1c00926c8535Eric Laurent        void applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs = 0, bool force = false);
252de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // Mute or unmute all streams handled by the specified strategy on the specified output
253de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs = 0);
254de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // Mute or unmute the stream on the specified output
255de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs = 0);
256de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // handle special cases for sonification strategy while in call: mute streams or replace by
257de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // a special tone in the device used for communication
258de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void handleIncallSonification(int stream, bool starting, bool stateChange);
259de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // true is current platform implements a back microphone
260de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual bool hasBackMicrophone() const { return false; }
2613e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        // true if device is in a telephony or VoIP call
2623e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        virtual bool isInCall();
2633e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        // true if given state represents a device in a telephony or VoIP call
2643e480fa067136253ae05faaba77f814f42cd56f0Jean-Michel Trivi        virtual bool isStateInCall(int state);
265de55cfb02db874136caf9c05927cee4028296147Eric Laurent
266de55cfb02db874136caf9c05927cee4028296147Eric Laurent#ifdef WITH_A2DP
267de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // true is current platform supports suplication of notifications and ringtones over A2DP output
268de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual bool a2dpUsedForSonification() const { return true; }
269de55cfb02db874136caf9c05927cee4028296147Eric Laurent        status_t handleA2dpConnection(AudioSystem::audio_devices device,
270de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                            const char *device_address);
271de55cfb02db874136caf9c05927cee4028296147Eric Laurent        status_t handleA2dpDisconnection(AudioSystem::audio_devices device,
272de55cfb02db874136caf9c05927cee4028296147Eric Laurent                                                            const char *device_address);
273de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void closeA2dpOutputs();
274de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // checks and if necessary changes output (a2dp, duplicated or hardware) used for all strategies.
275de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // must be called every time a condition that affects the output choice for a given strategy is
276de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // changed: connected device, phone state, force use...
277de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // Must be called before updateDeviceForStrategy()
278d6e68c3ae5677bc6127628d1c700b11c8f0fcdafEric Laurent        void checkOutputForStrategy(routing_strategy strategy);
279de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // Same as checkOutputForStrategy() but for a all strategies in order of priority
280d6e68c3ae5677bc6127628d1c700b11c8f0fcdafEric Laurent        void checkOutputForAllStrategies();
2812f2db765228f110b37a2851d1199d3c72d313c6dEric Laurent        // manages A2DP output suspend/restore according to phone state and BT SCO usage
2822f2db765228f110b37a2851d1199d3c72d313c6dEric Laurent        void checkA2dpSuspend();
283de55cfb02db874136caf9c05927cee4028296147Eric Laurent#endif
284de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // selects the most appropriate device on output for current state
285de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // must be called every time a condition that affects the device choice for a given output is
286de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // changed: connected device, phone state, force use, output start, output stop..
287de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // see getDeviceForStrategy() for the use of fromCache parameter
288de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t getNewDevice(audio_io_handle_t output, bool fromCache = true);
289de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // updates cache of device used by all strategies (mDeviceForStrategy[])
290de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // must be called every time a condition that affects the device choice for a given strategy is
291de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // changed: connected device, phone state, force use...
292de55cfb02db874136caf9c05927cee4028296147Eric Laurent        // cached values are used by getDeviceForStrategy() if parameter fromCache is true.
293de55cfb02db874136caf9c05927cee4028296147Eric Laurent         // Must be called after checkOutputForAllStrategies()
294de55cfb02db874136caf9c05927cee4028296147Eric Laurent        void updateDeviceForStrategy();
29568f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent        // true if current platform requires a specific output to be opened for this particular
29668f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent        // set of parameters. This function is called by getOutput() and is implemented by platform
29768f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent        // specific audio policy manager.
29868f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent        virtual bool needsDirectOuput(AudioSystem::stream_type stream,
29968f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent                                    uint32_t samplingRate,
30068f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent                                    uint32_t format,
30168f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent                                    uint32_t channels,
30268f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent                                    AudioSystem::output_flags flags,
30368f279fa86145752e424a9cbed1a2e2ee21e10b1Eric Laurent                                    uint32_t device);
304d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual uint32_t getMaxEffectsCpuLoad();
305d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        virtual uint32_t getMaxEffectsMemory();
306de55cfb02db874136caf9c05927cee4028296147Eric Laurent#ifdef AUDIO_POLICY_TEST
307de55cfb02db874136caf9c05927cee4028296147Eric Laurent        virtual     bool        threadLoop();
308de55cfb02db874136caf9c05927cee4028296147Eric Laurent                    void        exit();
309de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int testOutputIndex(audio_io_handle_t output);
310de55cfb02db874136caf9c05927cee4028296147Eric Laurent#endif //AUDIO_POLICY_TEST
311de55cfb02db874136caf9c05927cee4028296147Eric Laurent
312582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        status_t setEffectEnabled(EffectDescriptor *pDesc, bool enabled);
313582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
314de55cfb02db874136caf9c05927cee4028296147Eric Laurent        AudioPolicyClientInterface *mpClientInterface;  // audio policy client interface
315de55cfb02db874136caf9c05927cee4028296147Eric Laurent        audio_io_handle_t mHardwareOutput;              // hardware output handler
316de55cfb02db874136caf9c05927cee4028296147Eric Laurent        audio_io_handle_t mA2dpOutput;                  // A2DP output handler
317de55cfb02db874136caf9c05927cee4028296147Eric Laurent        audio_io_handle_t mDuplicatedOutput;            // duplicated output handler: outputs to hardware and A2DP.
318de55cfb02db874136caf9c05927cee4028296147Eric Laurent
319de55cfb02db874136caf9c05927cee4028296147Eric Laurent        KeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs;   // list of output descriptors
320de55cfb02db874136caf9c05927cee4028296147Eric Laurent        KeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs;     // list of input descriptors
321de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t mAvailableOutputDevices;                                   // bit field of all available output devices
322de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t mAvailableInputDevices;                                    // bit field of all available input devices
323de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int mPhoneState;                                                    // current phone state
324de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t                 mRingerMode;                               // current ringer mode
325de55cfb02db874136caf9c05927cee4028296147Eric Laurent        AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE];   // current forced use configuration
326de55cfb02db874136caf9c05927cee4028296147Eric Laurent
327de55cfb02db874136caf9c05927cee4028296147Eric Laurent        StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES];           // stream descriptors for volume control
328de55cfb02db874136caf9c05927cee4028296147Eric Laurent        String8 mA2dpDeviceAddress;                                         // A2DP device MAC address
329de55cfb02db874136caf9c05927cee4028296147Eric Laurent        String8 mScoDeviceAddress;                                          // SCO device MAC address
330de55cfb02db874136caf9c05927cee4028296147Eric Laurent        bool    mLimitRingtoneVolume;                                       // limit ringtone volume to music volume if headset connected
331de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t mDeviceForStrategy[NUM_STRATEGIES];
332564c30323762685cdb811e2a985ad374a691745eEric Laurent        float   mLastVoiceVolume;                                           // last voice volume value sent to audio HAL
333de55cfb02db874136caf9c05927cee4028296147Eric Laurent
334d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // Maximum CPU load allocated to audio effects in 0.1 MIPS (ARMv5TE, 0 WS memory) units
335d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        static const uint32_t MAX_EFFECTS_CPU_LOAD = 1000;
336d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        // Maximum memory allocated to audio effects in KB
337d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        static const uint32_t MAX_EFFECTS_MEMORY = 512;
338d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        uint32_t mTotalEffectsCpuLoad; // current CPU load used by effects
339d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        uint32_t mTotalEffectsMemory;  // current memory used by effects
340d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent        KeyedVector<int, EffectDescriptor *> mEffects;  // list of registered audio effects
3412f2db765228f110b37a2851d1199d3c72d313c6dEric Laurent        bool    mA2dpSuspended;  // true if A2DP output is suspended
342d2b227e76033d8c44b71d2982028a63436d3fe84Eric Laurent
343de55cfb02db874136caf9c05927cee4028296147Eric Laurent#ifdef AUDIO_POLICY_TEST
344de55cfb02db874136caf9c05927cee4028296147Eric Laurent        Mutex   mLock;
345de55cfb02db874136caf9c05927cee4028296147Eric Laurent        Condition mWaitWorkCV;
346de55cfb02db874136caf9c05927cee4028296147Eric Laurent
347de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int             mCurOutput;
348de55cfb02db874136caf9c05927cee4028296147Eric Laurent        bool            mDirectOutput;
349de55cfb02db874136caf9c05927cee4028296147Eric Laurent        audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS];
350de55cfb02db874136caf9c05927cee4028296147Eric Laurent        int             mTestInput;
351de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestDevice;
352de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestSamplingRate;
353de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestFormat;
354de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestChannels;
355de55cfb02db874136caf9c05927cee4028296147Eric Laurent        uint32_t        mTestLatencyMs;
356de55cfb02db874136caf9c05927cee4028296147Eric Laurent#endif //AUDIO_POLICY_TEST
357f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi
358f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Triviprivate:
359f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi        static float volIndexToAmpl(uint32_t device, const StreamDescriptor& streamDesc,
360f2faac71fd54ef16807b94d19e46d7ad99b001a7Jean-Michel Trivi                int indexInUi);
361de55cfb02db874136caf9c05927cee4028296147Eric Laurent};
362de55cfb02db874136caf9c05927cee4028296147Eric Laurent
363de55cfb02db874136caf9c05927cee4028296147Eric Laurent};
364