Nate Jiang | 7a7fd84 | 2022-12-06 17:11:13 -0800 | [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 | #ifndef ANDROID_AUDIOPOLICYINTERFACE_H |
| 18 | #define ANDROID_AUDIOPOLICYINTERFACE_H |
| 19 | |
| 20 | #include <media/AudioSystem.h> |
| 21 | #include <media/ToneGenerator.h> |
| 22 | #include <utils/String8.h> |
| 23 | |
| 24 | #include <hardware/audio_policy.h> |
| 25 | #include <hardware_legacy/AudioSystemLegacy.h> |
| 26 | |
| 27 | namespace android_audio_legacy { |
| 28 | using android::String8; |
| 29 | using android::ToneGenerator; |
| 30 | using android::Vector; |
| 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | // The AudioPolicyInterface and AudioPolicyClientInterface classes define the communication |
| 35 | // interfaces between the platform specific audio policy manager and Android generic audio policy |
| 36 | // manager. The platform specific audio policy manager must implement methods of the |
| 37 | // AudioPolicyInterface class. This implementation makes use of the AudioPolicyClientInterface to |
| 38 | // control the activity and configuration of audio input and output streams. |
| 39 | // |
| 40 | // The platform specific audio policy manager is in charge of the audio routing and volume control |
| 41 | // policies for a given platform. |
| 42 | // The main roles of this module are: |
| 43 | // - keep track of current system state (removable device connections, phone state, user |
| 44 | // requests...). System state changes and user actions are notified to audio policy manager with |
| 45 | // methods of the AudioPolicyInterface. |
| 46 | // - process getOutput() queries received when AudioTrack objects are created: Those queries |
| 47 | // return a handler on an output that has been selected, configured and opened by the audio policy |
| 48 | // manager and that must be used by the AudioTrack when registering to the AudioFlinger with the |
| 49 | // createTrack() method. When the AudioTrack object is released, a putOutput() query is received |
| 50 | // and the audio policy manager can decide to close or reconfigure the output depending on other |
| 51 | // streams using this output and current system state. |
| 52 | // - similarly process getInput() and putInput() queries received from AudioRecord objects and |
| 53 | // configure audio inputs. |
| 54 | // - process volume control requests: the stream volume is converted from an index value (received |
| 55 | // from UI) to a float value applicable to each output as a function of platform specific settings |
| 56 | // and current output route (destination device). It also make sure that streams are not muted if |
| 57 | // not allowed (e.g. camera shutter sound in some countries). |
| 58 | // |
| 59 | // The platform specific audio policy manager is provided as a shared library by platform vendors |
| 60 | // (as for libaudio.so) and is linked with libaudioflinger.so |
| 61 | |
| 62 | // Audio Policy Manager Interface |
| 63 | class AudioPolicyInterface { |
| 64 | public: |
| 65 | virtual ~AudioPolicyInterface() {} |
| 66 | // |
| 67 | // configuration functions |
| 68 | // |
| 69 | |
| 70 | // indicate a change in device connection status |
| 71 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
| 72 | AudioSystem::device_connection_state state, |
| 73 | const char* device_address) = 0; |
| 74 | // retrieve a device connection status |
| 75 | virtual AudioSystem::device_connection_state getDeviceConnectionState( |
| 76 | audio_devices_t device, const char* device_address) = 0; |
| 77 | // indicate a change in phone state. Valid phones states are defined by AudioSystem::audio_mode |
| 78 | virtual void setPhoneState(int state) = 0; |
| 79 | // force using a specific device category for the specified usage |
| 80 | virtual void setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config) = 0; |
| 81 | // retrieve current device category forced for a given usage |
| 82 | virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage) = 0; |
| 83 | // set a system property (e.g. camera sound always audible) |
| 84 | virtual void setSystemProperty(const char* property, const char* value) = 0; |
| 85 | // check proper initialization |
| 86 | virtual status_t initCheck() = 0; |
| 87 | |
| 88 | // |
| 89 | // Audio routing query functions |
| 90 | // |
| 91 | |
| 92 | // request an output appropriate for playback of the supplied stream type and parameters |
| 93 | virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream, uint32_t samplingRate, |
| 94 | audio_format_t format, audio_channel_mask_t channelMask, |
| 95 | AudioSystem::output_flags flags, |
| 96 | const audio_offload_info_t* offloadInfo) = 0; |
| 97 | // indicates to the audio policy manager that the output starts being used by corresponding |
| 98 | // stream. |
| 99 | virtual status_t startOutput(audio_io_handle_t output, AudioSystem::stream_type stream, |
| 100 | audio_session_t session = AUDIO_SESSION_NONE) = 0; |
| 101 | // indicates to the audio policy manager that the output stops being used by corresponding |
| 102 | // stream. |
| 103 | virtual status_t stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream, |
| 104 | audio_session_t session = AUDIO_SESSION_NONE) = 0; |
| 105 | // releases the output. |
| 106 | virtual void releaseOutput(audio_io_handle_t output) = 0; |
| 107 | |
| 108 | // request an input appropriate for record from the supplied device with supplied parameters. |
| 109 | virtual audio_io_handle_t getInput(int inputSource, uint32_t samplingRate, |
| 110 | audio_format_t format, audio_channel_mask_t channelMask, |
| 111 | AudioSystem::audio_in_acoustics acoustics) = 0; |
| 112 | // indicates to the audio policy manager that the input starts being used. |
| 113 | virtual status_t startInput(audio_io_handle_t input) = 0; |
| 114 | // indicates to the audio policy manager that the input stops being used. |
| 115 | virtual status_t stopInput(audio_io_handle_t input) = 0; |
| 116 | // releases the input. |
| 117 | virtual void releaseInput(audio_io_handle_t input) = 0; |
| 118 | |
| 119 | // |
| 120 | // volume control functions |
| 121 | // |
| 122 | |
| 123 | // initialises stream volume conversion parameters by specifying volume index range. |
| 124 | virtual void initStreamVolume(AudioSystem::stream_type stream, int indexMin, int indexMax) = 0; |
| 125 | |
| 126 | // sets the new stream volume at a level corresponding to the supplied index for the |
| 127 | // supplied device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT means |
| 128 | // setting volume for all devices |
| 129 | virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index, |
| 130 | audio_devices_t device) = 0; |
| 131 | |
| 132 | // retrieve current volume index for the specified stream and the |
| 133 | // specified device. By convention, specifying AUDIO_DEVICE_OUT_DEFAULT means |
| 134 | // querying the volume of the active device. |
| 135 | virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int* index, |
| 136 | audio_devices_t device) = 0; |
| 137 | |
| 138 | // return the strategy corresponding to a given stream type |
| 139 | virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream) = 0; |
| 140 | |
| 141 | // return the enabled output devices for the given stream type |
| 142 | virtual audio_devices_t getDevicesForStream(AudioSystem::stream_type stream) = 0; |
| 143 | |
| 144 | // Audio effect management |
| 145 | virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t* desc) = 0; |
| 146 | virtual status_t registerEffect(const effect_descriptor_t* desc, audio_io_handle_t io, |
| 147 | uint32_t strategy, audio_session_t session, int id) = 0; |
| 148 | virtual status_t unregisterEffect(int id) = 0; |
| 149 | virtual status_t setEffectEnabled(int id, bool enabled) = 0; |
| 150 | |
| 151 | virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const = 0; |
| 152 | virtual bool isStreamActiveRemotely(int stream, uint32_t inPastMs = 0) const = 0; |
| 153 | virtual bool isSourceActive(audio_source_t source) const = 0; |
| 154 | |
| 155 | // dump state |
| 156 | virtual status_t dump(int fd) = 0; |
| 157 | |
| 158 | virtual bool isOffloadSupported(const audio_offload_info_t& offloadInfo) = 0; |
| 159 | }; |
| 160 | |
| 161 | // Audio Policy client Interface |
| 162 | class AudioPolicyClientInterface { |
| 163 | public: |
| 164 | virtual ~AudioPolicyClientInterface() {} |
| 165 | |
| 166 | // |
| 167 | // Audio HW module functions |
| 168 | // |
| 169 | |
| 170 | // loads a HW module. |
| 171 | virtual audio_module_handle_t loadHwModule(const char* name) = 0; |
| 172 | |
| 173 | // |
| 174 | // Audio output Control functions |
| 175 | // |
| 176 | |
| 177 | // opens an audio output with the requested parameters. The parameter values can indicate to use |
| 178 | // the default values in case the audio policy manager has no specific requirements for the |
| 179 | // output being opened. When the function returns, the parameter values reflect the actual |
| 180 | // values used by the audio hardware output stream. The audio policy manager can check if the |
| 181 | // proposed parameters are suitable or not and act accordingly. |
| 182 | virtual audio_io_handle_t openOutput(audio_module_handle_t module, audio_devices_t* pDevices, |
| 183 | uint32_t* pSamplingRate, audio_format_t* pFormat, |
| 184 | audio_channel_mask_t* pChannelMask, uint32_t* pLatencyMs, |
| 185 | audio_output_flags_t flags, |
| 186 | const audio_offload_info_t* offloadInfo = NULL) = 0; |
| 187 | // creates a special output that is duplicated to the two outputs passed as arguments. The |
| 188 | // duplication is performed by a special mixer thread in the AudioFlinger. |
| 189 | virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, |
| 190 | audio_io_handle_t output2) = 0; |
| 191 | // closes the output stream |
| 192 | virtual status_t closeOutput(audio_io_handle_t output) = 0; |
| 193 | // suspends the output. When an output is suspended, the corresponding audio hardware output |
| 194 | // stream is placed in standby and the AudioTracks attached to the mixer thread are still |
| 195 | // processed but the output mix is discarded. |
| 196 | virtual status_t suspendOutput(audio_io_handle_t output) = 0; |
| 197 | // restores a suspended output. |
| 198 | virtual status_t restoreOutput(audio_io_handle_t output) = 0; |
| 199 | |
| 200 | // |
| 201 | // Audio input Control functions |
| 202 | // |
| 203 | |
| 204 | // opens an audio input |
| 205 | virtual audio_io_handle_t openInput(audio_module_handle_t module, audio_devices_t* pDevices, |
| 206 | uint32_t* pSamplingRate, audio_format_t* pFormat, |
| 207 | audio_channel_mask_t* pChannelMask) = 0; |
| 208 | // closes an audio input |
| 209 | virtual status_t closeInput(audio_io_handle_t input) = 0; |
| 210 | // |
| 211 | // misc control functions |
| 212 | // |
| 213 | |
| 214 | // set a stream volume for a particular output. For the same user setting, a given stream type |
| 215 | // can have different volumes for each output (destination device) it is attached to. |
| 216 | virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, |
| 217 | audio_io_handle_t output, int delayMs = 0) = 0; |
| 218 | |
| 219 | // invalidate a stream type, causing a reroute to an unspecified new output |
| 220 | virtual status_t invalidateStream(AudioSystem::stream_type stream) = 0; |
| 221 | |
| 222 | // function enabling to send proprietary informations directly from audio policy manager to |
| 223 | // audio hardware interface. |
| 224 | virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, |
| 225 | int delayMs = 0) = 0; |
| 226 | // function enabling to receive proprietary informations directly from audio hardware interface |
| 227 | // to audio policy manager. |
| 228 | virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) = 0; |
| 229 | |
| 230 | // request the playback of a tone on the specified stream: used for instance to replace |
| 231 | // notification sounds when playing over a telephony device during a phone call. |
| 232 | virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream) = 0; |
| 233 | virtual status_t stopTone() = 0; |
| 234 | |
| 235 | // set down link audio volume. |
| 236 | virtual status_t setVoiceVolume(float volume, int delayMs = 0) = 0; |
| 237 | |
| 238 | // move effect to the specified output |
| 239 | virtual status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput, |
| 240 | audio_io_handle_t dstOutput) = 0; |
| 241 | }; |
| 242 | |
| 243 | extern "C" AudioPolicyInterface* createAudioPolicyManager( |
| 244 | AudioPolicyClientInterface* clientInterface); |
| 245 | extern "C" void destroyAudioPolicyManager(AudioPolicyInterface* interface); |
| 246 | |
| 247 | }; // namespace android_audio_legacy |
| 248 | |
| 249 | #endif // ANDROID_AUDIOPOLICYINTERFACE_H |