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