blob: aa228aa5c99be7eac465e3273e72ea0b63c71f1b [file] [log] [blame]
Eric Laurentdce54a12014-03-10 12:19:46 -07001/*
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>
Eric Laurentdce54a12014-03-10 12:19:46 -070037//#include <media/IAudioFlinger.h>
38
39#include <hardware/hardware.h>
40#include <system/audio.h>
41#include <system/audio_policy.h>
42#include <hardware/audio_policy.h>
43#include <audio_effects/audio_effects_conf.h>
44#include <media/AudioParameter.h>
45
46
47namespace android {
48
49/* implementation of the interface to the policy manager */
50extern "C" {
51
52audio_module_handle_t aps_load_hw_module(void *service __unused,
53 const char *name)
54{
55 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
56 if (af == 0) {
57 ALOGW("%s: could not get AudioFlinger", __func__);
Chris Larsen2eae53c2016-07-12 14:52:27 -070058 return AUDIO_MODULE_HANDLE_NONE;
Eric Laurentdce54a12014-03-10 12:19:46 -070059 }
60
61 return af->loadHwModule(name);
62}
63
Eric Laurentcf2c0212014-07-25 16:20:43 -070064static audio_io_handle_t open_output(audio_module_handle_t module,
65 audio_devices_t *pDevices,
66 uint32_t *pSamplingRate,
67 audio_format_t *pFormat,
68 audio_channel_mask_t *pChannelMask,
69 uint32_t *pLatencyMs,
70 audio_output_flags_t flags,
71 const audio_offload_info_t *offloadInfo)
72{
73 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
74 if (af == 0) {
75 ALOGW("%s: could not get AudioFlinger", __func__);
76 return AUDIO_IO_HANDLE_NONE;
77 }
78
79 if (pSamplingRate == NULL || pFormat == NULL || pChannelMask == NULL ||
80 pDevices == NULL || pLatencyMs == NULL) {
81 return AUDIO_IO_HANDLE_NONE;
82 }
83 audio_config_t config = AUDIO_CONFIG_INITIALIZER;
84 config.sample_rate = *pSamplingRate;
85 config.format = *pFormat;
86 config.channel_mask = *pChannelMask;
87 if (offloadInfo != NULL) {
88 config.offload_info = *offloadInfo;
89 }
90 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
91 status_t status = af->openOutput(module, &output, &config, pDevices,
92 String8(""), pLatencyMs, flags);
93 if (status == NO_ERROR) {
94 *pSamplingRate = config.sample_rate;
95 *pFormat = config.format;
96 *pChannelMask = config.channel_mask;
97 if (offloadInfo != NULL) {
Eric Laurentf3806772014-10-07 09:19:31 -070098 *((audio_offload_info_t *)offloadInfo) = config.offload_info;
Eric Laurentcf2c0212014-07-25 16:20:43 -070099 }
100 }
101 return output;
102}
103
Eric Laurentdce54a12014-03-10 12:19:46 -0700104// deprecated: replaced by aps_open_output_on_module()
105audio_io_handle_t aps_open_output(void *service __unused,
106 audio_devices_t *pDevices,
107 uint32_t *pSamplingRate,
108 audio_format_t *pFormat,
109 audio_channel_mask_t *pChannelMask,
110 uint32_t *pLatencyMs,
111 audio_output_flags_t flags)
112{
Glenn Kastena13cde92016-03-28 15:26:02 -0700113 return open_output(AUDIO_MODULE_HANDLE_NONE, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700114 pLatencyMs, flags, NULL);
Eric Laurentdce54a12014-03-10 12:19:46 -0700115}
116
117audio_io_handle_t aps_open_output_on_module(void *service __unused,
118 audio_module_handle_t module,
119 audio_devices_t *pDevices,
120 uint32_t *pSamplingRate,
121 audio_format_t *pFormat,
122 audio_channel_mask_t *pChannelMask,
123 uint32_t *pLatencyMs,
124 audio_output_flags_t flags,
125 const audio_offload_info_t *offloadInfo)
126{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700127 return open_output(module, pDevices, pSamplingRate, pFormat, pChannelMask,
Eric Laurentdce54a12014-03-10 12:19:46 -0700128 pLatencyMs, flags, offloadInfo);
129}
130
131audio_io_handle_t aps_open_dup_output(void *service __unused,
132 audio_io_handle_t output1,
133 audio_io_handle_t output2)
134{
135 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
136 if (af == 0) {
137 ALOGW("%s: could not get AudioFlinger", __func__);
138 return 0;
139 }
140 return af->openDuplicateOutput(output1, output2);
141}
142
143int aps_close_output(void *service __unused, audio_io_handle_t output)
144{
145 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
146 if (af == 0) {
147 return PERMISSION_DENIED;
148 }
149
150 return af->closeOutput(output);
151}
152
153int aps_suspend_output(void *service __unused, audio_io_handle_t output)
154{
155 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
156 if (af == 0) {
157 ALOGW("%s: could not get AudioFlinger", __func__);
158 return PERMISSION_DENIED;
159 }
160
161 return af->suspendOutput(output);
162}
163
164int aps_restore_output(void *service __unused, audio_io_handle_t output)
165{
166 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
167 if (af == 0) {
168 ALOGW("%s: could not get AudioFlinger", __func__);
169 return PERMISSION_DENIED;
170 }
171
172 return af->restoreOutput(output);
173}
174
Eric Laurentcf2c0212014-07-25 16:20:43 -0700175static audio_io_handle_t open_input(audio_module_handle_t module,
176 audio_devices_t *pDevices,
177 uint32_t *pSamplingRate,
178 audio_format_t *pFormat,
179 audio_channel_mask_t *pChannelMask)
180{
181 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
182 if (af == 0) {
183 ALOGW("%s: could not get AudioFlinger", __func__);
184 return AUDIO_IO_HANDLE_NONE;
185 }
186
187 if (pSamplingRate == NULL || pFormat == NULL || pChannelMask == NULL || pDevices == NULL) {
188 return AUDIO_IO_HANDLE_NONE;
189 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800190
191 if (((*pDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) == AUDIO_DEVICE_IN_REMOTE_SUBMIX)
Eric Laurentb2379ba2016-05-23 17:42:12 -0700192 && !captureAudioOutputAllowed(IPCThreadState::self()->getCallingPid(),
193 IPCThreadState::self()->getCallingUid())) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800194 ALOGE("open_input() permission denied: capture not allowed");
195 return AUDIO_IO_HANDLE_NONE;
196 }
197
Eric Laurentcf2c0212014-07-25 16:20:43 -0700198 audio_config_t config = AUDIO_CONFIG_INITIALIZER;;
199 config.sample_rate = *pSamplingRate;
200 config.format = *pFormat;
201 config.channel_mask = *pChannelMask;
202 audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
203 status_t status = af->openInput(module, &input, &config, pDevices,
204 String8(""), AUDIO_SOURCE_MIC, AUDIO_INPUT_FLAG_FAST /*FIXME*/);
205 if (status == NO_ERROR) {
206 *pSamplingRate = config.sample_rate;
207 *pFormat = config.format;
208 *pChannelMask = config.channel_mask;
209 }
210 return input;
211}
212
213
Eric Laurentdce54a12014-03-10 12:19:46 -0700214// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
215audio_io_handle_t aps_open_input(void *service __unused,
216 audio_devices_t *pDevices,
217 uint32_t *pSamplingRate,
218 audio_format_t *pFormat,
219 audio_channel_mask_t *pChannelMask,
220 audio_in_acoustics_t acoustics __unused)
221{
Glenn Kastena13cde92016-03-28 15:26:02 -0700222 return open_input(AUDIO_MODULE_HANDLE_NONE, pDevices, pSamplingRate, pFormat, pChannelMask);
Eric Laurentdce54a12014-03-10 12:19:46 -0700223}
224
225audio_io_handle_t aps_open_input_on_module(void *service __unused,
226 audio_module_handle_t module,
227 audio_devices_t *pDevices,
228 uint32_t *pSamplingRate,
229 audio_format_t *pFormat,
230 audio_channel_mask_t *pChannelMask)
231{
Eric Laurentcf2c0212014-07-25 16:20:43 -0700232 return open_input(module, pDevices, pSamplingRate, pFormat, pChannelMask);
Eric Laurentdce54a12014-03-10 12:19:46 -0700233}
234
235int aps_close_input(void *service __unused, audio_io_handle_t input)
236{
237 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
238 if (af == 0) {
239 return PERMISSION_DENIED;
240 }
241
242 return af->closeInput(input);
243}
244
245int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream)
246{
247 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
248 if (af == 0) {
249 return PERMISSION_DENIED;
250 }
251
252 return af->invalidateStream(stream);
253}
254
Glenn Kastend848eb42016-03-08 13:42:11 -0800255int aps_move_effects(void *service __unused, audio_session_t session,
Eric Laurentdce54a12014-03-10 12:19:46 -0700256 audio_io_handle_t src_output,
257 audio_io_handle_t dst_output)
258{
259 sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
260 if (af == 0) {
261 return PERMISSION_DENIED;
262 }
263
264 return af->moveEffects(session, src_output, dst_output);
265}
266
267char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
268 const char *keys)
269{
270 String8 result = AudioSystem::getParameters(io_handle, String8(keys));
271 return strdup(result.string());
272}
273
274void aps_set_parameters(void *service, audio_io_handle_t io_handle,
275 const char *kv_pairs, int delay_ms)
276{
277 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
278
279 audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
280}
281
282int aps_set_stream_volume(void *service, audio_stream_type_t stream,
283 float volume, audio_io_handle_t output,
284 int delay_ms)
285{
286 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
287
288 return audioPolicyService->setStreamVolume(stream, volume, output,
289 delay_ms);
290}
291
292int aps_start_tone(void *service, audio_policy_tone_t tone,
293 audio_stream_type_t stream)
294{
295 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
296
297 return audioPolicyService->startTone(tone, stream);
298}
299
300int aps_stop_tone(void *service)
301{
302 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
303
304 return audioPolicyService->stopTone();
305}
306
307int aps_set_voice_volume(void *service, float volume, int delay_ms)
308{
309 AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
310
311 return audioPolicyService->setVoiceVolume(volume, delay_ms);
312}
313
314}; // extern "C"
315
316}; // namespace android