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