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_IAUDIOPOLICYSERVICE_H
18 #define ANDROID_IAUDIOPOLICYSERVICE_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <utils/RefBase.h>
24 #include <utils/Errors.h>
25 #include <binder/IInterface.h>
26 #include <media/AudioDeviceTypeAddr.h>
27 #include <media/AudioSystem.h>
28 #include <media/AudioPolicy.h>
29 #include <media/IAudioPolicyServiceClient.h>
30 #include <system/audio_policy.h>
31 #include <vector>
32 
33 namespace android {
34 namespace media {
35 // Must be pre-declared, or else there isn't a good way to generate a header
36 // library.
37 class ICaptureStateListener;
38 }
39 
40 // ----------------------------------------------------------------------------
41 
42 class IAudioPolicyService : public IInterface
43 {
44 public:
45     DECLARE_META_INTERFACE(AudioPolicyService);
46 
47     //
48     // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions)
49     //
50     virtual void onNewAudioModulesAvailable() = 0;
51     virtual status_t setDeviceConnectionState(audio_devices_t device,
52                                               audio_policy_dev_state_t state,
53                                               const char *device_address,
54                                               const char *device_name,
55                                               audio_format_t encodedFormat) = 0;
56     virtual audio_policy_dev_state_t getDeviceConnectionState(audio_devices_t device,
57                                                                   const char *device_address) = 0;
58     virtual status_t handleDeviceConfigChange(audio_devices_t device,
59                                               const char *device_address,
60                                               const char *device_name,
61                                               audio_format_t encodedFormat) = 0;
62     virtual status_t setPhoneState(audio_mode_t state, uid_t uid) = 0;
63     virtual status_t setForceUse(audio_policy_force_use_t usage,
64                                     audio_policy_forced_cfg_t config) = 0;
65     virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) = 0;
66     virtual audio_io_handle_t getOutput(audio_stream_type_t stream) = 0;
67     virtual status_t getOutputForAttr(audio_attributes_t *attr,
68                                       audio_io_handle_t *output,
69                                       audio_session_t session,
70                                       audio_stream_type_t *stream,
71                                       pid_t pid,
72                                       uid_t uid,
73                                       const audio_config_t *config,
74                                       audio_output_flags_t flags,
75                                       audio_port_handle_t *selectedDeviceId,
76                                       audio_port_handle_t *portId,
77                                       std::vector<audio_io_handle_t> *secondaryOutputs) = 0;
78     virtual status_t startOutput(audio_port_handle_t portId) = 0;
79     virtual status_t stopOutput(audio_port_handle_t portId) = 0;
80     virtual void releaseOutput(audio_port_handle_t portId) = 0;
81     virtual status_t  getInputForAttr(const audio_attributes_t *attr,
82                               audio_io_handle_t *input,
83                               audio_unique_id_t riid,
84                               audio_session_t session,
85                               pid_t pid,
86                               uid_t uid,
87                               const String16& opPackageName,
88                               const audio_config_base_t *config,
89                               audio_input_flags_t flags,
90                               audio_port_handle_t *selectedDeviceId,
91                               audio_port_handle_t *portId) = 0;
92     virtual status_t startInput(audio_port_handle_t portId) = 0;
93     virtual status_t stopInput(audio_port_handle_t portId) = 0;
94     virtual void releaseInput(audio_port_handle_t portId) = 0;
95     virtual status_t initStreamVolume(audio_stream_type_t stream,
96                                       int indexMin,
97                                       int indexMax) = 0;
98     virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
99                                           int index,
100                                           audio_devices_t device) = 0;
101     virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
102                                           int *index,
103                                           audio_devices_t device) = 0;
104 
105     virtual status_t setVolumeIndexForAttributes(const audio_attributes_t &attr,
106                                                  int index,
107                                                  audio_devices_t device) = 0;
108     virtual status_t getVolumeIndexForAttributes(const audio_attributes_t &attr,
109                                                  int &index,
110                                                  audio_devices_t device) = 0;
111     virtual status_t getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index) = 0;
112 
113     virtual status_t getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index) = 0;
114 
115     virtual uint32_t getStrategyForStream(audio_stream_type_t stream) = 0;
116     virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream) = 0;
117     virtual status_t getDevicesForAttributes(const AudioAttributes &aa,
118             AudioDeviceTypeAddrVector *devices) const = 0;
119     virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc) = 0;
120     virtual status_t registerEffect(const effect_descriptor_t *desc,
121                                     audio_io_handle_t io,
122                                     uint32_t strategy,
123                                     audio_session_t session,
124                                     int id) = 0;
125     virtual status_t unregisterEffect(int id) = 0;
126     virtual status_t setEffectEnabled(int id, bool enabled) = 0;
127     virtual status_t moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) = 0;
128     virtual bool     isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const = 0;
129     virtual bool     isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0)
130                              const = 0;
131     virtual bool     isSourceActive(audio_source_t source) const = 0;
132     virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
133                                               effect_descriptor_t *descriptors,
134                                               uint32_t *count) = 0;
135     virtual status_t addSourceDefaultEffect(const effect_uuid_t *type,
136                                             const String16& opPackageName,
137                                             const effect_uuid_t *uuid,
138                                             int32_t priority,
139                                             audio_source_t source,
140                                             audio_unique_id_t* id) = 0;
141     virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
142                                             const String16& opPackageName,
143                                             const effect_uuid_t *uuid,
144                                             int32_t priority,
145                                             audio_usage_t usage,
146                                             audio_unique_id_t* id) = 0;
147     virtual status_t removeSourceDefaultEffect(audio_unique_id_t id) = 0;
148     virtual status_t removeStreamDefaultEffect(audio_unique_id_t id) = 0;
149     virtual status_t setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) = 0;
150     virtual status_t setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) = 0;
151    // Check if offload is possible for given format, stream type, sample rate,
152     // bit rate, duration, video and streaming or offload property is enabled
153     virtual bool isOffloadSupported(const audio_offload_info_t& info) = 0;
154 
155     // Check if direct playback is possible for given format, sample rate, channel mask and flags.
156     virtual bool isDirectOutputSupported(const audio_config_base_t& config,
157                                          const audio_attributes_t& attributes) = 0;
158 
159     /* List available audio ports and their attributes */
160     virtual status_t listAudioPorts(audio_port_role_t role,
161                                     audio_port_type_t type,
162                                     unsigned int *num_ports,
163                                     struct audio_port *ports,
164                                     unsigned int *generation) = 0;
165 
166     /* Get attributes for a given audio port */
167     virtual status_t getAudioPort(struct audio_port *port) = 0;
168 
169     /* Create an audio patch between several source and sink ports */
170     virtual status_t createAudioPatch(const struct audio_patch *patch,
171                                        audio_patch_handle_t *handle) = 0;
172 
173     /* Release an audio patch */
174     virtual status_t releaseAudioPatch(audio_patch_handle_t handle) = 0;
175 
176     /* List existing audio patches */
177     virtual status_t listAudioPatches(unsigned int *num_patches,
178                                       struct audio_patch *patches,
179                                       unsigned int *generation) = 0;
180     /* Set audio port configuration */
181     virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0;
182 
183     virtual void registerClient(const sp<IAudioPolicyServiceClient>& client) = 0;
184 
185     virtual void setAudioPortCallbacksEnabled(bool enabled) = 0;
186 
187     virtual void setAudioVolumeGroupCallbacksEnabled(bool enabled) = 0;
188 
189     virtual status_t acquireSoundTriggerSession(audio_session_t *session,
190                                            audio_io_handle_t *ioHandle,
191                                            audio_devices_t *device) = 0;
192 
193     virtual status_t releaseSoundTriggerSession(audio_session_t session) = 0;
194 
195     virtual audio_mode_t getPhoneState() = 0;
196 
197     virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) = 0;
198 
199     virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
200             = 0;
201 
202     virtual status_t removeUidDeviceAffinities(uid_t uid) = 0;
203 
204     virtual status_t setUserIdDeviceAffinities(int userId,
205             const Vector<AudioDeviceTypeAddr>& devices) = 0;
206 
207     virtual status_t removeUserIdDeviceAffinities(int userId) = 0;
208 
209     virtual status_t startAudioSource(const struct audio_port_config *source,
210                                       const audio_attributes_t *attributes,
211                                       audio_port_handle_t *portId) = 0;
212     virtual status_t stopAudioSource(audio_port_handle_t portId) = 0;
213 
214     virtual status_t setMasterMono(bool mono) = 0;
215     virtual status_t getMasterMono(bool *mono) = 0;
216     virtual float    getStreamVolumeDB(
217             audio_stream_type_t stream, int index, audio_devices_t device) = 0;
218 
219     virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
220                                         audio_format_t *surroundFormats,
221                                         bool *surroundFormatsEnabled,
222                                         bool reported) = 0;
223     virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP(
224                                         std::vector<audio_format_t> *formats) = 0;
225     virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) = 0;
226 
227     virtual status_t setAssistantUid(uid_t uid) = 0;
228     virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids) = 0;
229     virtual status_t setCurrentImeUid(uid_t uid) = 0;
230 
231     virtual bool     isHapticPlaybackSupported() = 0;
232     virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies) = 0;
233     virtual status_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
234                                                            product_strategy_t &productStrategy) = 0;
235 
236     virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups) = 0;
237     virtual status_t getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
238                                                        volume_group_t &volumeGroup) = 0;
239 
240     virtual status_t setRttEnabled(bool enabled) = 0;
241 
242     virtual bool     isCallScreenModeSupported() = 0;
243 
244     virtual status_t setPreferredDeviceForStrategy(product_strategy_t strategy,
245                                                    const AudioDeviceTypeAddr &device) = 0;
246 
247     virtual status_t removePreferredDeviceForStrategy(product_strategy_t strategy) = 0;
248 
249     virtual status_t getPreferredDeviceForStrategy(product_strategy_t strategy,
250                                                    AudioDeviceTypeAddr &device) = 0;
251 
252     // The return code here is only intended to represent transport errors. The
253     // actual server implementation should always return NO_ERROR.
254     virtual status_t registerSoundTriggerCaptureStateListener(
255         const sp<media::ICaptureStateListener>& listener,
256         bool* result) = 0;
257 };
258 
259 
260 // ----------------------------------------------------------------------------
261 
262 class BnAudioPolicyService : public BnInterface<IAudioPolicyService>
263 {
264 public:
265     virtual status_t    onTransact( uint32_t code,
266                                     const Parcel& data,
267                                     Parcel* reply,
268                                     uint32_t flags = 0);
269 private:
270     void sanetizeAudioAttributes(audio_attributes_t* attr);
271     status_t sanitizeEffectDescriptor(effect_descriptor_t* desc);
272     status_t sanitizeAudioPortConfig(struct audio_port_config* config);
273 };
274 
275 // ----------------------------------------------------------------------------
276 
277 }; // namespace android
278 
279 #endif // ANDROID_IAUDIOPOLICYSERVICE_H
280