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