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 21 #include <utils/Errors.h> 22 #include <utils/KeyedVector.h> 23 #include <hardware_legacy/AudioPolicyInterface.h> 24 #include <utils/threads.h> 25 26 27 namespace android { 28 29 // ---------------------------------------------------------------------------- 30 31 #define MAX_DEVICE_ADDRESS_LEN 20 32 #define NUM_TEST_OUTPUTS 5 33 34 class AudioPolicyManagerGeneric: public AudioPolicyInterface 35 #ifdef AUDIO_POLICY_TEST 36 , public Thread 37 #endif //AUDIO_POLICY_TEST 38 { 39 40 public: 41 AudioPolicyManagerGeneric(AudioPolicyClientInterface *clientInterface); 42 virtual ~AudioPolicyManagerGeneric(); 43 44 // AudioPolicyInterface 45 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device, 46 AudioSystem::device_connection_state state, 47 const char *device_address); 48 virtual AudioSystem::device_connection_state getDeviceConnectionState(AudioSystem::audio_devices device, 49 const char *device_address); 50 virtual void setPhoneState(int state); 51 virtual void setRingerMode(uint32_t mode, uint32_t mask); 52 virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config); 53 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage); 54 virtual void setSystemProperty(const char* property, const char* value); 55 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream, 56 uint32_t samplingRate, 57 uint32_t format, 58 uint32_t channels, 59 AudioSystem::output_flags flags); 60 virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream); 61 virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream); 62 virtual void releaseOutput(audio_io_handle_t output); 63 virtual audio_io_handle_t getInput(int inputSource, 64 uint32_t samplingRate, 65 uint32_t format, 66 uint32_t channels, 67 AudioSystem::audio_in_acoustics acoustics); 68 // indicates to the audio policy manager that the input starts being used. 69 virtual status_t startInput(audio_io_handle_t input); 70 // indicates to the audio policy manager that the input stops being used. 71 virtual status_t stopInput(audio_io_handle_t input); 72 virtual void releaseInput(audio_io_handle_t input); 73 virtual void initStreamVolume(AudioSystem::stream_type stream, 74 int indexMin, 75 int indexMax); 76 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index); 77 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index); 78 79 virtual status_t dump(int fd); 80 81 private: 82 83 enum routing_strategy { 84 STRATEGY_MEDIA, 85 STRATEGY_PHONE, 86 STRATEGY_SONIFICATION, 87 STRATEGY_DTMF, 88 NUM_STRATEGIES 89 }; 90 91 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output 92 // and keep track of the usage of this output by each audio stream type. 93 class AudioOutputDescriptor 94 { 95 public: 96 AudioOutputDescriptor(); 97 98 status_t dump(int fd); 99 100 uint32_t device(); 101 void changeRefCount(AudioSystem::stream_type, int delta); isUsedByStream(AudioSystem::stream_type stream)102 bool isUsedByStream(AudioSystem::stream_type stream) { return mRefCount[stream] > 0 ? true : false; } 103 uint32_t refCount(); 104 105 uint32_t mSamplingRate; // 106 uint32_t mFormat; // 107 uint32_t mChannels; // output configuration 108 uint32_t mLatency; // 109 AudioSystem::output_flags mFlags; // 110 uint32_t mDevice; // current device this output is routed to 111 uint32_t mRefCount[AudioSystem::NUM_STREAM_TYPES]; // number of streams of each type using this output 112 }; 113 114 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input 115 // and keep track of the usage of this input. 116 class AudioInputDescriptor 117 { 118 public: 119 AudioInputDescriptor(); 120 121 status_t dump(int fd); 122 123 uint32_t mSamplingRate; // 124 uint32_t mFormat; // input configuration 125 uint32_t mChannels; // 126 AudioSystem::audio_in_acoustics mAcoustics; // 127 uint32_t mDevice; // current device this input is routed to 128 uint32_t mRefCount; // number of AudioRecord clients using this output 129 }; 130 131 // stream descriptor used for volume control 132 class StreamDescriptor 133 { 134 public: StreamDescriptor()135 StreamDescriptor() 136 : mIndexMin(0), mIndexMax(1), mIndexCur(1), mMuteCount(0), mCanBeMuted(true) {} 137 138 void dump(char* buffer, size_t size); 139 140 int mIndexMin; // min volume index 141 int mIndexMax; // max volume index 142 int mIndexCur; // current volume index 143 int mMuteCount; // mute request counter 144 bool mCanBeMuted; // true is the stream can be muted 145 }; 146 147 // return the strategy corresponding to a given stream type 148 static routing_strategy getStrategy(AudioSystem::stream_type stream); 149 // return the output handle of an output routed to the specified device, 0 if no output 150 // is routed to the device 151 float computeVolume(int stream, int index, uint32_t device); 152 // Mute or unmute the stream on the specified output 153 void setStreamMute(int stream, bool on, audio_io_handle_t output); 154 // handle special cases for sonification strategy while in call: mute streams or replace by 155 // a special tone in the device used for communication 156 void handleIncallSonification(int stream, bool starting); 157 158 159 #ifdef AUDIO_POLICY_TEST 160 virtual bool threadLoop(); 161 void exit(); 162 int testOutputIndex(audio_io_handle_t output); 163 #endif //AUDIO_POLICY_TEST 164 165 166 AudioPolicyClientInterface *mpClientInterface; // audio policy client interface 167 audio_io_handle_t mHardwareOutput; // hardware output handler 168 169 KeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs; // list ot output descritors 170 KeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs; // list of input descriptors 171 uint32_t mAvailableOutputDevices; // bit field of all available output devices 172 uint32_t mAvailableInputDevices; // bit field of all available input devices 173 int mPhoneState; // current phone state 174 uint32_t mRingerMode; // current ringer mode 175 AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE]; // current forced use configuration 176 177 StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES]; // stream descriptors for volume control 178 179 #ifdef AUDIO_POLICY_TEST 180 Mutex mLock; 181 Condition mWaitWorkCV; 182 183 int mCurOutput; 184 bool mDirectOutput; 185 audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS]; 186 int mTestInput; 187 uint32_t mTestDevice; 188 uint32_t mTestSamplingRate; 189 uint32_t mTestFormat; 190 uint32_t mTestChannels; 191 uint32_t mTestLatencyMs; 192 #endif //AUDIO_POLICY_TEST 193 194 }; 195 196 }; 197