Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 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> |
Jean-Michel Trivi | 195d08b | 2009-10-09 15:48:36 -0700 | [diff] [blame] | 20 | #include <utils/Timers.h> |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 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 |
Jean-Michel Trivi | 195d08b | 2009-10-09 15:48:36 -0700 | [diff] [blame] | 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 |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 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 | |
Eric Laurent | 63e2c0a | 2009-11-03 09:18:39 -0800 | [diff] [blame^] | 81 | virtual status_t dump(int fd); |
| 82 | |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 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 | |
Eric Laurent | 63e2c0a | 2009-11-03 09:18:39 -0800 | [diff] [blame^] | 100 | status_t dump(int fd); |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 101 | |
| 102 | uint32_t device(); |
| 103 | void changeRefCount(AudioSystem::stream_type, int delta); |
| 104 | bool isUsedByStrategy(routing_strategy strategy); |
| 105 | bool isUsedByStream(AudioSystem::stream_type stream) { return mRefCount[stream] > 0 ? true : false; } |
| 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 | |
Eric Laurent | 63e2c0a | 2009-11-03 09:18:39 -0800 | [diff] [blame^] | 127 | status_t dump(int fd); |
| 128 | |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 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 | }; |
| 136 | |
| 137 | // stream descriptor used for volume control |
| 138 | class StreamDescriptor |
| 139 | { |
| 140 | public: |
| 141 | StreamDescriptor() |
| 142 | : mIndexMin(0), mIndexMax(1), mIndexCur(1), mMuteCount(0), mCanBeMuted(true) {} |
| 143 | |
Eric Laurent | 63e2c0a | 2009-11-03 09:18:39 -0800 | [diff] [blame^] | 144 | void dump(char* buffer, size_t size); |
| 145 | |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 146 | int mIndexMin; // min volume index |
| 147 | int mIndexMax; // max volume index |
| 148 | int mIndexCur; // current volume index |
| 149 | int mMuteCount; // mute request counter |
| 150 | bool mCanBeMuted; // true is the stream can be muted |
| 151 | }; |
| 152 | |
| 153 | // return the strategy corresponding to a given stream type |
| 154 | static routing_strategy getStrategy(AudioSystem::stream_type stream); |
| 155 | // return the output handle of an output routed to the specified device, 0 if no output |
| 156 | // is routed to the device |
| 157 | audio_io_handle_t getOutputForDevice(uint32_t device); |
| 158 | // return appropriate device for streams handled by the specified strategy according to current |
| 159 | // phone state, connected devices... |
| 160 | uint32_t getDeviceForStrategy(routing_strategy strategy); |
| 161 | // change the route of the specified output |
| 162 | void setOutputDevice(audio_io_handle_t output, uint32_t device, bool force = false, int delayMs = 0); |
| 163 | // compute the actual volume for a given stream according to the requested index and a particular |
| 164 | // device |
Jean-Michel Trivi | 195d08b | 2009-10-09 15:48:36 -0700 | [diff] [blame] | 165 | float computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device); |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 166 | // check that volume change is permitted, compute and send new volume to audio hardware |
Eric Laurent | 7eda941 | 2009-10-21 08:19:36 -0700 | [diff] [blame] | 167 | status_t checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs = 0, bool force = false); |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 168 | // apply all stream volumes to the specified output and device |
| 169 | void applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs = 0); |
| 170 | // Mute or unmute all streams handled by the specified strategy on the specified output |
| 171 | void setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs = 0); |
| 172 | // Mute or unmute the stream on the specified output |
| 173 | void setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs = 0); |
| 174 | // handle special cases for sonification strategy while in call: mute streams or replace by |
| 175 | // a special tone in the device used for communication |
Jean-Michel Trivi | 195d08b | 2009-10-09 15:48:36 -0700 | [diff] [blame] | 176 | void handleIncallSonification(int stream, bool starting, bool stateChange); |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 177 | |
| 178 | AudioPolicyClientInterface *mpClientInterface; // audio policy client interface |
| 179 | audio_io_handle_t mHardwareOutput; // hardware output handler |
| 180 | audio_io_handle_t mA2dpOutput; // A2DP output handler |
| 181 | audio_io_handle_t mDuplicatedOutput; // duplicated output handler: outputs to hardware and A2DP. |
| 182 | |
Jean-Michel Trivi | 195d08b | 2009-10-09 15:48:36 -0700 | [diff] [blame] | 183 | KeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs; // list of output descriptors |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 184 | KeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs; // list of input descriptors |
| 185 | uint32_t mAvailableOutputDevices; // bit field of all available output devices |
| 186 | uint32_t mAvailableInputDevices; // bit field of all available input devices |
| 187 | int mPhoneState; // current phone state |
| 188 | uint32_t mRingerMode; // current ringer mode |
| 189 | AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE]; // current forced use configuration |
| 190 | |
| 191 | StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES]; // stream descriptors for volume control |
| 192 | String8 mA2dpDeviceAddress; // A2DP device MAC address |
| 193 | String8 mScoDeviceAddress; // SCO device MAC address |
Jean-Michel Trivi | 195d08b | 2009-10-09 15:48:36 -0700 | [diff] [blame] | 194 | nsecs_t mMusicStopTime; // time when last music stream was stopped |
Brian Swetland | 2aec439 | 2009-09-01 18:52:58 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | }; |