forked from Decatf/android-audio-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaps_wrapper.cpp
263 lines (227 loc) · 10.2 KB
/
aps_wrapper.cpp
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
/*
* Copyright (C) 2013 Thomas Wendt <thoemy@gmx.net>
*
* 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 "AudioPolicyServiceWrapper"
// Set to 1 to strip verbose logging messages
#define LOG_NDEBUG 0
#include <cutils/log.h>
#include "aps_wrapper.h"
#include "common.h"
struct aps_wrapper_service {
void * wrapped_service;
struct audio_policy_service_ops * wrapped_aps_ops;
struct wrapper::audio_policy_service_ops aps_ops;
};
/**
* Calls a function of the wrapped service.
*/
#define WRAPPED_CALL(service, func, ...) ({\
ALOGV("%s", __FUNCTION__); \
aps_wrapper_service_t * __wrapped_aps = (aps_wrapper_service_t*) service; \
return __wrapped_aps->wrapped_aps_ops->func(__wrapped_aps->wrapped_service, ##__VA_ARGS__); \
})
#if WRAPPED_AUDIO_POLICY_VERSION >= ANDROID_VERSION(4, 1)
static audio_module_handle_t aps_load_hw_module(void *service,
const char *name)
{
WRAPPED_CALL(service, load_hw_module, name);
}
#endif
// deprecated: replaced by aps_open_output_on_module()
static audio_io_handle_t aps_open_output(void *service,
audio_devices_t *pDevices,
uint32_t *pSamplingRate,
audio_format_t *pFormat,
audio_channel_mask_t *pChannelMask,
uint32_t *pLatencyMs,
audio_output_flags_t flags)
{
ALOGV("%s: 0x%x, %d, %d, %x, %d, %x", __FUNCTION__, *pDevices, *pSamplingRate,
*pFormat, *pChannelMask, *pLatencyMs, flags);
audio_devices_t devices = convert_audio_devices(*pDevices, ICS_TO_JB);
// The old policy managers don't set the PRIMARY flag for the
// first output it opens but AudioFlinger needs at least one device with
// that flag.
// TODO: Should probably only be done for the first device opened.
uint32_t newflags = flags | AUDIO_OUTPUT_FLAG_PRIMARY;
flags = (audio_output_flags_t)newflags;
WRAPPED_CALL(service, open_output, &devices, pSamplingRate, pFormat,
pChannelMask, pLatencyMs, flags);
}
#if WRAPPED_AUDIO_POLICY_VERSION >= ANDROID_VERSION(4, 1)
static audio_io_handle_t aps_open_output_on_module(void *service,
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)
{
ALOGV("%s: 0x%x, %d, %d, %x, %d, %x", __FUNCTION__, *pDevices, *pSamplingRate,
*pFormat, *pChannelMask, *pLatencyMs, flags);
audio_devices_t devices = convert_audio_devices(*pDevices, ICS_TO_JB);
WRAPPED_CALL(service, open_output_on_module, module, &devices, pSamplingRate,
pFormat, pChannelMask, pLatencyMs, flags);
}
#endif
static audio_io_handle_t aps_open_dup_output(void *service,
audio_io_handle_t output1,
audio_io_handle_t output2)
{
WRAPPED_CALL(service, open_duplicate_output, output1, output2);
}
static int aps_close_output(void *service, audio_io_handle_t output)
{
WRAPPED_CALL(service, close_output, output);
}
static int aps_suspend_output(void *service, audio_io_handle_t output)
{
WRAPPED_CALL(service, suspend_output, output);
}
static int aps_restore_output(void *service, audio_io_handle_t output)
{
WRAPPED_CALL(service, restore_output, output);
}
// deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
static audio_io_handle_t aps_open_input(void *service,
audio_devices_t *pDevices,
uint32_t *pSamplingRate,
audio_format_t *pFormat,
audio_channel_mask_t *pChannelMask,
audio_in_acoustics_t acoustics)
{
ALOGV("%s: 0x%x, %d, %d, %x, %d", __FUNCTION__, *pDevices, *pSamplingRate,
*pFormat, *pChannelMask, acoustics);
audio_devices_t devices = convert_audio_devices(*pDevices, ICS_TO_JB);
WRAPPED_CALL(service, open_input, &devices, pSamplingRate, pFormat,
pChannelMask, acoustics);
}
#if WRAPPED_AUDIO_POLICY_VERSION >= ANDROID_VERSION(4, 1)
static audio_io_handle_t aps_open_input_on_module(void *service,
audio_module_handle_t module,
audio_devices_t *pDevices,
uint32_t *pSamplingRate,
audio_format_t *pFormat,
audio_channel_mask_t *pChannelMask)
{
ALOGV("%s: 0x%x, %d, %d, %x", __FUNCTION__, *pDevices, *pSamplingRate, *pFormat, *pChannelMask);
audio_devices_t devices = convert_audio_devices(*pDevices, ICS_TO_JB);
WRAPPED_CALL(service, open_input_on_module, module, &devices, pSamplingRate, pFormat,
pChannelMask);
}
#endif
static int aps_close_input(void *service, audio_io_handle_t input)
{
WRAPPED_CALL(service, close_input, input);
}
static int aps_set_stream_output(void *service, audio_stream_type_t stream,
audio_io_handle_t output)
{
WRAPPED_CALL(service, invalidate_stream, stream);
}
static int aps_move_effects(void *service, int session,
audio_io_handle_t src_output,
audio_io_handle_t dst_output)
{
WRAPPED_CALL(service, move_effects, session, src_output, dst_output);
}
static char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
const char *keys)
{
ALOGV("%s: io_handle: %d, keys: %s", __FUNCTION__, io_handle, keys);
aps_wrapper_service_t * waps = (aps_wrapper_service_t*) service;
char * kv_pairs;
char * fixed_kv_pairs;
kv_pairs = waps->wrapped_aps_ops->get_parameters(waps->wrapped_service, io_handle,
keys);
fixed_kv_pairs = fixup_audio_parameters(kv_pairs, JB_TO_ICS);
free(kv_pairs);
return fixed_kv_pairs;
}
static void aps_set_parameters(void *service, audio_io_handle_t io_handle,
const char *kv_pairs, int delay_ms)
{
ALOGV("%s: io_handle: %d, kv_pairs: %s", __FUNCTION__, io_handle, kv_pairs);
aps_wrapper_service_t * waps = (aps_wrapper_service_t*) service;
char * fixed_kv_pairs = fixup_audio_parameters(kv_pairs, ICS_TO_JB);
waps->wrapped_aps_ops->set_parameters(waps->wrapped_service, io_handle,
fixed_kv_pairs, delay_ms);
free(fixed_kv_pairs);
}
static int aps_set_stream_volume(void *service, audio_stream_type_t stream,
float volume, audio_io_handle_t output,
int delay_ms)
{
ALOGV("%s: stream: %d, volume: %f, output: %d, delay_ms: %d",
__FUNCTION__, stream, volume, output, delay_ms);
WRAPPED_CALL(service, set_stream_volume, stream, volume, output, delay_ms);
}
static int aps_start_tone(void *service, audio_policy_tone_t tone,
audio_stream_type_t stream)
{
WRAPPED_CALL(service, start_tone, tone, stream);
}
static int aps_stop_tone(void *service)
{
WRAPPED_CALL(service, stop_tone);
}
static int aps_set_voice_volume(void *service, float volume, int delay_ms)
{
ALOGV("%s: volume: %f, delay_ms: %d", __FUNCTION__, volume, delay_ms);
WRAPPED_CALL(service, set_voice_volume, volume, delay_ms);
}
/**
* Wraps a service and the corresponding service ops with our mock service.
*/
int aps_wrapper_create(void *wrapped_service,
struct audio_policy_service_ops * wrapped_aps_ops,
void ** service,
struct wrapper::audio_policy_service_ops ** aps_ops)
{
aps_wrapper_service_t * waps;
waps = (aps_wrapper_service_t *) malloc(sizeof(*waps));
if(!waps)
return -ENOMEM;
waps->wrapped_service = wrapped_service;
waps->wrapped_aps_ops = wrapped_aps_ops;
waps->aps_ops.open_output = aps_open_output;
waps->aps_ops.open_duplicate_output = aps_open_dup_output;
waps->aps_ops.close_output = aps_close_output;
waps->aps_ops.suspend_output = aps_suspend_output;
waps->aps_ops.restore_output = aps_restore_output;
waps->aps_ops.open_input = aps_open_input;
waps->aps_ops.close_input = aps_close_input;
waps->aps_ops.set_stream_volume = aps_set_stream_volume;
waps->aps_ops.set_stream_output = aps_set_stream_output;
waps->aps_ops.set_parameters = aps_set_parameters;
waps->aps_ops.get_parameters = aps_get_parameters;
waps->aps_ops.start_tone = aps_start_tone;
waps->aps_ops.stop_tone = aps_stop_tone;
waps->aps_ops.set_voice_volume = aps_set_voice_volume;
waps->aps_ops.move_effects = aps_move_effects;
#if WRAPPED_AUDIO_POLICY_VERSION >= ANDROID_VERSION(4, 1)
waps->aps_ops.load_hw_module = aps_load_hw_module;
waps->aps_ops.open_output_on_module = aps_open_output_on_module;
waps->aps_ops.open_input_on_module = aps_open_input_on_module;
#endif
*service = waps;
*aps_ops = &waps->aps_ops;
return 0;
}
void aps_wrapper_destroy(void * wrapped_service)
{
free(wrapped_service);
}