| Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -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> | 
|  | 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 | private: | 
|  | 80 |  | 
|  | 81 | enum routing_strategy { | 
|  | 82 | STRATEGY_MEDIA, | 
|  | 83 | STRATEGY_PHONE, | 
|  | 84 | STRATEGY_SONIFICATION, | 
|  | 85 | STRATEGY_DTMF, | 
|  | 86 | NUM_STRATEGIES | 
|  | 87 | }; | 
|  | 88 |  | 
|  | 89 | // descriptor for audio outputs. Used to maintain current configuration of each opened audio output | 
|  | 90 | // and keep track of the usage of this output by each audio stream type. | 
|  | 91 | class AudioOutputDescriptor | 
|  | 92 | { | 
|  | 93 | public: | 
|  | 94 | AudioOutputDescriptor(); | 
|  | 95 |  | 
|  | 96 |  | 
|  | 97 | uint32_t device(); | 
|  | 98 | void changeRefCount(AudioSystem::stream_type, int delta); | 
|  | 99 | bool isUsedByStream(AudioSystem::stream_type stream) { return mRefCount[stream] > 0 ? true : false; } | 
|  | 100 | uint32_t refCount(); | 
|  | 101 |  | 
|  | 102 | uint32_t mSamplingRate;             // | 
|  | 103 | uint32_t mFormat;                   // | 
|  | 104 | uint32_t mChannels;                 // output configuration | 
|  | 105 | uint32_t mLatency;                  // | 
|  | 106 | AudioSystem::output_flags mFlags;   // | 
|  | 107 | uint32_t mDevice;                   // current device this output is routed to | 
|  | 108 | uint32_t mRefCount[AudioSystem::NUM_STREAM_TYPES]; // number of streams of each type using this output | 
|  | 109 | }; | 
|  | 110 |  | 
|  | 111 | // descriptor for audio inputs. Used to maintain current configuration of each opened audio input | 
|  | 112 | // and keep track of the usage of this input. | 
|  | 113 | class AudioInputDescriptor | 
|  | 114 | { | 
|  | 115 | public: | 
|  | 116 | AudioInputDescriptor(); | 
|  | 117 |  | 
|  | 118 | uint32_t mSamplingRate;                     // | 
|  | 119 | uint32_t mFormat;                           // input configuration | 
|  | 120 | uint32_t mChannels;                         // | 
|  | 121 | AudioSystem::audio_in_acoustics mAcoustics; // | 
|  | 122 | uint32_t mDevice;                           // current device this input is routed to | 
|  | 123 | uint32_t mRefCount;                         // number of AudioRecord clients using this output | 
|  | 124 | }; | 
|  | 125 |  | 
|  | 126 | // stream descriptor used for volume control | 
|  | 127 | class StreamDescriptor | 
|  | 128 | { | 
|  | 129 | public: | 
|  | 130 | StreamDescriptor() | 
|  | 131 | :   mIndexMin(0), mIndexMax(1), mIndexCur(1), mMuteCount(0), mCanBeMuted(true) {} | 
|  | 132 |  | 
|  | 133 | int mIndexMin;      // min volume index | 
|  | 134 | int mIndexMax;      // max volume index | 
|  | 135 | int mIndexCur;      // current volume index | 
|  | 136 | int mMuteCount;     // mute request counter | 
|  | 137 | bool mCanBeMuted;   // true is the stream can be muted | 
|  | 138 | }; | 
|  | 139 |  | 
|  | 140 | // return the strategy corresponding to a given stream type | 
|  | 141 | static routing_strategy getStrategy(AudioSystem::stream_type stream); | 
|  | 142 | // return the output handle of an output routed to the specified device, 0 if no output | 
|  | 143 | // is routed to the device | 
|  | 144 | float computeVolume(int stream, int index, uint32_t device); | 
|  | 145 | // Mute or unmute the stream on the specified output | 
|  | 146 | void setStreamMute(int stream, bool on, audio_io_handle_t output); | 
|  | 147 | // handle special cases for sonification strategy while in call: mute streams or replace by | 
|  | 148 | // a special tone in the device used for communication | 
|  | 149 | void handleIncallSonification(int stream, bool starting); | 
|  | 150 |  | 
|  | 151 |  | 
|  | 152 | #ifdef AUDIO_POLICY_TEST | 
|  | 153 | virtual     bool        threadLoop(); | 
|  | 154 | void        exit(); | 
|  | 155 | int testOutputIndex(audio_io_handle_t output); | 
|  | 156 | #endif //AUDIO_POLICY_TEST | 
|  | 157 |  | 
|  | 158 |  | 
|  | 159 | AudioPolicyClientInterface *mpClientInterface;  // audio policy client interface | 
|  | 160 | audio_io_handle_t mHardwareOutput;              // hardware output handler | 
|  | 161 |  | 
|  | 162 | KeyedVector<audio_io_handle_t, AudioOutputDescriptor *> mOutputs;   // list ot output descritors | 
|  | 163 | KeyedVector<audio_io_handle_t, AudioInputDescriptor *> mInputs;     // list of input descriptors | 
|  | 164 | uint32_t mAvailableOutputDevices;                                   // bit field of all available output devices | 
|  | 165 | uint32_t mAvailableInputDevices;                                    // bit field of all available input devices | 
|  | 166 | int mPhoneState;                                                    // current phone state | 
|  | 167 | uint32_t                 mRingerMode;                               // current ringer mode | 
|  | 168 | AudioSystem::forced_config mForceUse[AudioSystem::NUM_FORCE_USE];   // current forced use configuration | 
|  | 169 |  | 
|  | 170 | StreamDescriptor mStreams[AudioSystem::NUM_STREAM_TYPES];           // stream descriptors for volume control | 
|  | 171 |  | 
|  | 172 | #ifdef AUDIO_POLICY_TEST | 
|  | 173 | Mutex   mLock; | 
|  | 174 | Condition mWaitWorkCV; | 
|  | 175 |  | 
|  | 176 | int             mCurOutput; | 
|  | 177 | bool            mDirectOutput; | 
|  | 178 | audio_io_handle_t mTestOutputs[NUM_TEST_OUTPUTS]; | 
|  | 179 | int             mTestInput; | 
|  | 180 | uint32_t        mTestDevice; | 
|  | 181 | uint32_t        mTestSamplingRate; | 
|  | 182 | uint32_t        mTestFormat; | 
|  | 183 | uint32_t        mTestChannelcount; | 
|  | 184 | uint32_t        mTestLatencyMs; | 
|  | 185 | #endif //AUDIO_POLICY_TEST | 
|  | 186 |  | 
|  | 187 | }; | 
|  | 188 |  | 
|  | 189 | }; |