• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "AudioPolicyService"
18 //#define LOG_NDEBUG 0
19 
20 #include "Configuration.h"
21 #undef __STRICT_ANSI__
22 #define __STDINT_LIMITS
23 #define __STDC_LIMIT_MACROS
24 #include <stdint.h>
25 
26 #include <sys/time.h>
27 #include <binder/IServiceManager.h>
28 #include <utils/Log.h>
29 #include <cutils/properties.h>
30 #include <binder/IPCThreadState.h>
31 #include <utils/String16.h>
32 #include <utils/threads.h>
33 #include "AudioPolicyService.h"
34 #include "ServiceUtilities.h"
35 #include <hardware_legacy/power.h>
36 #include <media/AudioEffect.h>
37 #include <media/EffectsFactoryApi.h>
38 //#include <media/IAudioFlinger.h>
39 
40 #include <hardware/hardware.h>
41 #include <system/audio.h>
42 #include <system/audio_policy.h>
43 #include <hardware/audio_policy.h>
44 #include <audio_effects/audio_effects_conf.h>
45 #include <media/AudioParameter.h>
46 
47 
48 namespace android {
49 
50 /* implementation of the interface to the policy manager */
51 extern "C" {
52 
aps_load_hw_module(void * service __unused,const char * name)53 audio_module_handle_t aps_load_hw_module(void *service __unused,
54                                              const char *name)
55 {
56     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
57     if (af == 0) {
58         ALOGW("%s: could not get AudioFlinger", __func__);
59         return 0;
60     }
61 
62     return af->loadHwModule(name);
63 }
64 
open_output(audio_module_handle_t module,audio_devices_t * pDevices,uint32_t * pSamplingRate,audio_format_t * pFormat,audio_channel_mask_t * pChannelMask,uint32_t * pLatencyMs,audio_output_flags_t flags,const audio_offload_info_t * offloadInfo)65 static audio_io_handle_t open_output(audio_module_handle_t module,
66                                     audio_devices_t *pDevices,
67                                     uint32_t *pSamplingRate,
68                                     audio_format_t *pFormat,
69                                     audio_channel_mask_t *pChannelMask,
70                                     uint32_t *pLatencyMs,
71                                     audio_output_flags_t flags,
72                                     const audio_offload_info_t *offloadInfo)
73 {
74     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
75     if (af == 0) {
76         ALOGW("%s: could not get AudioFlinger", __func__);
77         return AUDIO_IO_HANDLE_NONE;
78     }
79 
80     if (pSamplingRate == NULL || pFormat == NULL || pChannelMask == NULL ||
81             pDevices == NULL || pLatencyMs == NULL) {
82         return AUDIO_IO_HANDLE_NONE;
83     }
84     audio_config_t config = AUDIO_CONFIG_INITIALIZER;
85     config.sample_rate = *pSamplingRate;
86     config.format = *pFormat;
87     config.channel_mask = *pChannelMask;
88     if (offloadInfo != NULL) {
89         config.offload_info = *offloadInfo;
90     }
91     audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
92     status_t status = af->openOutput(module, &output, &config, pDevices,
93                                      String8(""), pLatencyMs, flags);
94     if (status == NO_ERROR) {
95         *pSamplingRate = config.sample_rate;
96         *pFormat = config.format;
97         *pChannelMask = config.channel_mask;
98         if (offloadInfo != NULL) {
99             *((audio_offload_info_t *)offloadInfo) = config.offload_info;
100         }
101     }
102     return output;
103 }
104 
105 // deprecated: replaced by aps_open_output_on_module()
aps_open_output(void * service __unused,audio_devices_t * pDevices,uint32_t * pSamplingRate,audio_format_t * pFormat,audio_channel_mask_t * pChannelMask,uint32_t * pLatencyMs,audio_output_flags_t flags)106 audio_io_handle_t aps_open_output(void *service __unused,
107                                          audio_devices_t *pDevices,
108                                          uint32_t *pSamplingRate,
109                                          audio_format_t *pFormat,
110                                          audio_channel_mask_t *pChannelMask,
111                                          uint32_t *pLatencyMs,
112                                          audio_output_flags_t flags)
113 {
114     return open_output((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask,
115                           pLatencyMs, flags, NULL);
116 }
117 
aps_open_output_on_module(void * service __unused,audio_module_handle_t module,audio_devices_t * pDevices,uint32_t * pSamplingRate,audio_format_t * pFormat,audio_channel_mask_t * pChannelMask,uint32_t * pLatencyMs,audio_output_flags_t flags,const audio_offload_info_t * offloadInfo)118 audio_io_handle_t aps_open_output_on_module(void *service __unused,
119                                                    audio_module_handle_t module,
120                                                    audio_devices_t *pDevices,
121                                                    uint32_t *pSamplingRate,
122                                                    audio_format_t *pFormat,
123                                                    audio_channel_mask_t *pChannelMask,
124                                                    uint32_t *pLatencyMs,
125                                                    audio_output_flags_t flags,
126                                                    const audio_offload_info_t *offloadInfo)
127 {
128     return open_output(module, pDevices, pSamplingRate, pFormat, pChannelMask,
129                           pLatencyMs, flags, offloadInfo);
130 }
131 
aps_open_dup_output(void * service __unused,audio_io_handle_t output1,audio_io_handle_t output2)132 audio_io_handle_t aps_open_dup_output(void *service __unused,
133                                                  audio_io_handle_t output1,
134                                                  audio_io_handle_t output2)
135 {
136     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
137     if (af == 0) {
138         ALOGW("%s: could not get AudioFlinger", __func__);
139         return 0;
140     }
141     return af->openDuplicateOutput(output1, output2);
142 }
143 
aps_close_output(void * service __unused,audio_io_handle_t output)144 int aps_close_output(void *service __unused, audio_io_handle_t output)
145 {
146     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
147     if (af == 0) {
148         return PERMISSION_DENIED;
149     }
150 
151     return af->closeOutput(output);
152 }
153 
aps_suspend_output(void * service __unused,audio_io_handle_t output)154 int aps_suspend_output(void *service __unused, audio_io_handle_t output)
155 {
156     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
157     if (af == 0) {
158         ALOGW("%s: could not get AudioFlinger", __func__);
159         return PERMISSION_DENIED;
160     }
161 
162     return af->suspendOutput(output);
163 }
164 
aps_restore_output(void * service __unused,audio_io_handle_t output)165 int aps_restore_output(void *service __unused, audio_io_handle_t output)
166 {
167     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
168     if (af == 0) {
169         ALOGW("%s: could not get AudioFlinger", __func__);
170         return PERMISSION_DENIED;
171     }
172 
173     return af->restoreOutput(output);
174 }
175 
open_input(audio_module_handle_t module,audio_devices_t * pDevices,uint32_t * pSamplingRate,audio_format_t * pFormat,audio_channel_mask_t * pChannelMask)176 static audio_io_handle_t open_input(audio_module_handle_t module,
177                                     audio_devices_t *pDevices,
178                                     uint32_t *pSamplingRate,
179                                     audio_format_t *pFormat,
180                                     audio_channel_mask_t *pChannelMask)
181 {
182     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
183     if (af == 0) {
184         ALOGW("%s: could not get AudioFlinger", __func__);
185         return AUDIO_IO_HANDLE_NONE;
186     }
187 
188     if (pSamplingRate == NULL || pFormat == NULL || pChannelMask == NULL || pDevices == NULL) {
189         return AUDIO_IO_HANDLE_NONE;
190     }
191     audio_config_t config = AUDIO_CONFIG_INITIALIZER;;
192     config.sample_rate = *pSamplingRate;
193     config.format = *pFormat;
194     config.channel_mask = *pChannelMask;
195     audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
196     status_t status = af->openInput(module, &input, &config, pDevices,
197                                     String8(""), AUDIO_SOURCE_MIC, AUDIO_INPUT_FLAG_FAST /*FIXME*/);
198     if (status == NO_ERROR) {
199         *pSamplingRate = config.sample_rate;
200         *pFormat = config.format;
201         *pChannelMask = config.channel_mask;
202     }
203     return input;
204 }
205 
206 
207 // deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
aps_open_input(void * service __unused,audio_devices_t * pDevices,uint32_t * pSamplingRate,audio_format_t * pFormat,audio_channel_mask_t * pChannelMask,audio_in_acoustics_t acoustics __unused)208 audio_io_handle_t aps_open_input(void *service __unused,
209                                         audio_devices_t *pDevices,
210                                         uint32_t *pSamplingRate,
211                                         audio_format_t *pFormat,
212                                         audio_channel_mask_t *pChannelMask,
213                                         audio_in_acoustics_t acoustics __unused)
214 {
215     return  open_input((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
216 }
217 
aps_open_input_on_module(void * service __unused,audio_module_handle_t module,audio_devices_t * pDevices,uint32_t * pSamplingRate,audio_format_t * pFormat,audio_channel_mask_t * pChannelMask)218 audio_io_handle_t aps_open_input_on_module(void *service __unused,
219                                                   audio_module_handle_t module,
220                                                   audio_devices_t *pDevices,
221                                                   uint32_t *pSamplingRate,
222                                                   audio_format_t *pFormat,
223                                                   audio_channel_mask_t *pChannelMask)
224 {
225     return  open_input(module, pDevices, pSamplingRate, pFormat, pChannelMask);
226 }
227 
aps_close_input(void * service __unused,audio_io_handle_t input)228 int aps_close_input(void *service __unused, audio_io_handle_t input)
229 {
230     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
231     if (af == 0) {
232         return PERMISSION_DENIED;
233     }
234 
235     return af->closeInput(input);
236 }
237 
aps_invalidate_stream(void * service __unused,audio_stream_type_t stream)238 int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream)
239 {
240     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
241     if (af == 0) {
242         return PERMISSION_DENIED;
243     }
244 
245     return af->invalidateStream(stream);
246 }
247 
aps_move_effects(void * service __unused,int session,audio_io_handle_t src_output,audio_io_handle_t dst_output)248 int aps_move_effects(void *service __unused, int session,
249                                 audio_io_handle_t src_output,
250                                 audio_io_handle_t dst_output)
251 {
252     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
253     if (af == 0) {
254         return PERMISSION_DENIED;
255     }
256 
257     return af->moveEffects(session, src_output, dst_output);
258 }
259 
aps_get_parameters(void * service __unused,audio_io_handle_t io_handle,const char * keys)260 char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
261                                      const char *keys)
262 {
263     String8 result = AudioSystem::getParameters(io_handle, String8(keys));
264     return strdup(result.string());
265 }
266 
aps_set_parameters(void * service,audio_io_handle_t io_handle,const char * kv_pairs,int delay_ms)267 void aps_set_parameters(void *service, audio_io_handle_t io_handle,
268                                    const char *kv_pairs, int delay_ms)
269 {
270     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
271 
272     audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
273 }
274 
aps_set_stream_volume(void * service,audio_stream_type_t stream,float volume,audio_io_handle_t output,int delay_ms)275 int aps_set_stream_volume(void *service, audio_stream_type_t stream,
276                                      float volume, audio_io_handle_t output,
277                                      int delay_ms)
278 {
279     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
280 
281     return audioPolicyService->setStreamVolume(stream, volume, output,
282                                                delay_ms);
283 }
284 
aps_start_tone(void * service,audio_policy_tone_t tone,audio_stream_type_t stream)285 int aps_start_tone(void *service, audio_policy_tone_t tone,
286                               audio_stream_type_t stream)
287 {
288     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
289 
290     return audioPolicyService->startTone(tone, stream);
291 }
292 
aps_stop_tone(void * service)293 int aps_stop_tone(void *service)
294 {
295     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
296 
297     return audioPolicyService->stopTone();
298 }
299 
aps_set_voice_volume(void * service,float volume,int delay_ms)300 int aps_set_voice_volume(void *service, float volume, int delay_ms)
301 {
302     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
303 
304     return audioPolicyService->setVoiceVolume(volume, delay_ms);
305 }
306 
307 }; // extern "C"
308 
309 }; // namespace android
310