• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <stdint.h>
19 #include <sys/types.h>
20 #include <utils/Timers.h>
21 #include <utils/Errors.h>
22 #include <utils/KeyedVector.h>
23 #include <hardware_legacy/AudioPolicyInterface.h>
24 
25 
26 namespace android {
27 
28 // ----------------------------------------------------------------------------
29 
30 #define MAX_DEVICE_ADDRESS_LEN 20
31 // Attenuation applied to STRATEGY_SONIFICATION streams when a headset is connected: 6dB
32 #define SONIFICATION_HEADSET_VOLUME_FACTOR 0.5
33 // Min volume for STRATEGY_SONIFICATION streams when limited by music volume: -36dB
34 #define SONIFICATION_HEADSET_VOLUME_MIN  0.016
35 // Time in seconds during which we consider that music is still active after a music
36 // track was stopped - see computeVolume()
37 #define SONIFICATION_HEADSET_MUSIC_DELAY  5
38 
39 class AudioPolicyManager: public AudioPolicyInterface
40 {
41 
42 public:
43                 AudioPolicyManager(AudioPolicyClientInterface *clientInterface);
44         virtual ~AudioPolicyManager();
45 
46         // AudioPolicyInterface
47         virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device,
48                                                           AudioSystem::device_connection_state state,
49                                                           const char *device_address);
50         virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device,
51                                                                               const char *device_address);
52         virtual void setPhoneState(int state);
53         virtual void setRingerMode(uint32_t mode, uint32_t mask);
54         virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config);
55         virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage);
56         virtual void setSystemProperty(const char* property, const char* value);
57         virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream,
58                                             uint32_t samplingRate,
59                                             uint32_t format,
60                                             uint32_t channels,
61                                             AudioSystem::output_flags flags);
62         virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
63         virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream);
64         virtual void releaseOutput(audio_io_handle_t output);
65         virtual audio_io_handle_t getInput(int inputSource,
66                                             uint32_t samplingRate,
67                                             uint32_t format,
68                                             uint32_t channels,
69                                             AudioSystem::audio_in_acoustics acoustics);
70         // indicates to the audio policy manager that the input starts being used.
71         virtual status_t startInput(audio_io_handle_t input);
72         // indicates to the audio policy manager that the input stops being used.
73         virtual status_t stopInput(audio_io_handle_t input);
74         virtual void releaseInput(audio_io_handle_t input);
75         virtual void initStreamVolume(AudioSystem::stream_type stream,
76                                                     int indexMin,
77                                                     int indexMax);
78         virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index);
79         virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index);
80 
81         virtual status_t dump(int fd);
82 
83 private:
84 
85         enum routing_strategy {
86             STRATEGY_MEDIA,
87             STRATEGY_PHONE,
88             STRATEGY_SONIFICATION,
89             STRATEGY_DTMF,
90             NUM_STRATEGIES
91         };
92 
93         // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
94         // and keep track of the usage of this output by each audio stream type.
95         class AudioOutputDescriptor
96         {
97         public:
98             AudioOutputDescriptor();
99 
100             status_t    dump(int fd);
101 
102             uint32_t device();
103             void changeRefCount(AudioSystem::stream_type, int delta);
104             bool isUsedByStrategy(routing_strategy strategy);
isUsedByStream(AudioSystem::stream_type stream)105             bool isUsedByStream(AudioSystem::stream_type stream) { return mRefCount[stream] > 0 ? true : false; }
isDuplicated()106             bool isDuplicated() { return (mDevice == 0); } // by convention mDevice is 0 for duplicated outputs
107 
108             uint32_t mSamplingRate;             //
109             uint32_t mFormat;                   //
110             uint32_t mChannels;                 // output configuration
111             uint32_t mLatency;                  //
112             AudioSystem::output_flags mFlags;   //
113             uint32_t mDevice;                   // current device this output is routed to
114             uint32_t mRefCount[AudioSystem::NUM_STREAM_TYPES]; // number of streams of each type using this output
115             AudioOutputDescriptor *mOutput1;    // used by duplicated outputs: first output
116             AudioOutputDescriptor *mOutput2;    // used by duplicated outputs: second output
117             float mCurVolume[AudioSystem::NUM_STREAM_TYPES];   // current stream volume
118         };
119 
120         // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
121         // and keep track of the usage of this input.
122         class AudioInputDescriptor
123         {
124         public:
125             AudioInputDescriptor();
126 
127             status_t    dump(int fd);
128 
129             uint32_t mSamplingRate;                     //
130             uint32_t mFormat;                           // input configuration
131             uint32_t mChannels;                         //
132             AudioSystem::audio_in_acoustics mAcoustics; //
133             uint32_t mDevice;                           // current device this input is routed to
134             uint32_t mRefCount;                         // number of AudioRecord clients using this output
135             int      mInputSource;                     // input source selected by application (mediarecorder.h)
136         };
137 
138         // stream descriptor used for volume control
139         class StreamDescriptor
140         {
141         public:
StreamDescriptor()142             StreamDescriptor()
143             :   mIndexMin(0), mIndexMax(1), mIndexCur(1), mMuteCount(0), mCanBeMuted(true) {}
144 
145             void dump(char* buffer, size_t size);
146 
147             int mIndexMin;      // min volume index
148             int mIndexMax;      // max volume index
149             int mIndexCur;      // current volume index
150             int mMuteCount;     // mute request counter
151             bool mCanBeMuted;   // true is the stream can be muted
152         };
153 
154         // return the strategy corresponding to a given stream type
155         static routing_strategy getStrategy(AudioSystem::stream_type stream);
156         // return the output handle of an output routed to the specified device, 0 if no output
157         // is routed to the device
158         audio_io_handle_t getOutputForDevice(uint32_t device);
159         // return appropriate device for streams handled by the specified strategy according to current
160         // phone state, connected devices...
161         uint32_t getDeviceForStrategy(routing_strategy strategy);
162         // change the route of the specified output
163         void setOutputDevice(audio_io_handle_t output, uint32_t device, bool force = false, int delayMs = 0);
164         // select input device corresponding to requested audio source
165         uint32_t getDeviceForInputSource(int inputSource);
166         // return io handle of active input or 0 if no input is active
167         audio_io_handle_t getActiveInput();
168         // compute the actual volume for a given stream according to the requested index and a particular
169         // device
170         float computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device);
171         // check that volume change is permitted, compute and send new volume to audio hardware
172         status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs = 0, bool force = false);
173         // apply all stream volumes to the specified output and device
174         void applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs = 0);
175         // Mute or unmute all streams handled by the specified strategy on the specified output
176         void setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs = 0);
177         // Mute or unmute the stream on the specified output
178         void setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs = 0);
179         // handle special cases for sonification strategy while in call: mute streams or replace by
180         // a special tone in the device used for communication
181         void handleIncallSonification(int stream, bool starting, bool stateChange);
182 
183         AudioPolicyClientInterface *mpClientInterface;  // audio policy client interface
184         audio_io_handle_t mHardwareOutput;              // hardware output handler
185         audio_io_handle_t mA2dpOutput;                  // A2DP output handler
186         audio_io_handle_t mDuplicatedOutput;            // duplicated output handler: outputs to hardware and A2DP.
187 
188         KeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs;   // list of output descriptors
189         KeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs;     // list of input descriptors
190         uint32_t mAvailableOutputDevices;                                   // bit field of all available output devices
191         uint32_t mAvailableInputDevices;                                    // bit field of all available input devices
192         int mPhoneState;                                                    // current phone state
193         uint32_t                 mRingerMode;                               // current ringer mode
194         AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE];   // current forced use configuration
195 
196         StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES];           // stream descriptors for volume control
197         String8 mA2dpDeviceAddress;                                         // A2DP device MAC address
198         String8 mScoDeviceAddress;                                          // SCO device MAC address
199         nsecs_t mMusicStopTime;                                             // time when last music stream was stopped
200 };
201 
202 };
203