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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/* //device/extlibs/pv/android/IAudioflinger.cpp
**
** Copyright 2007, 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 "IAudioFlinger"
#include <utils/Log.h>
#include <stdint.h>
#include <sys/types.h>
#include <binder/Parcel.h>
#include <media/IAudioFlinger.h>
namespace android {
enum {
CREATE_TRACK = IBinder::FIRST_CALL_TRANSACTION,
OPEN_RECORD,
SAMPLE_RATE,
CHANNEL_COUNT,
FORMAT,
FRAME_COUNT,
LATENCY,
SET_MASTER_VOLUME,
SET_MASTER_MUTE,
MASTER_VOLUME,
MASTER_MUTE,
SET_STREAM_VOLUME,
SET_STREAM_MUTE,
STREAM_VOLUME,
STREAM_MUTE,
SET_MODE,
GET_MODE,
SET_ROUTING,
GET_ROUTING,
SET_MIC_MUTE,
GET_MIC_MUTE,
IS_MUSIC_ACTIVE,
SET_PARAMETER,
REGISTER_CLIENT,
GET_INPUTBUFFERSIZE,
WAKE_UP,
IS_A2DP_ENABLED
};
class BpAudioFlinger : public BpInterface<IAudioFlinger>
{
public:
BpAudioFlinger(const sp<IBinder>& impl)
: BpInterface<IAudioFlinger>(impl)
{
}
virtual sp<IAudioTrack> createTrack(
pid_t pid,
int streamType,
uint32_t sampleRate,
int format,
int channelCount,
int frameCount,
uint32_t flags,
const sp<IMemory>& sharedBuffer,
status_t *status)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(pid);
data.writeInt32(streamType);
data.writeInt32(sampleRate);
data.writeInt32(format);
data.writeInt32(channelCount);
data.writeInt32(frameCount);
data.writeInt32(flags);
data.writeStrongBinder(sharedBuffer->asBinder());
status_t lStatus = remote()->transact(CREATE_TRACK, data, &reply);
if (lStatus != NO_ERROR) {
LOGE("createTrack error: %s", strerror(-lStatus));
}
lStatus = reply.readInt32();
if (status) {
*status = lStatus;
}
return interface_cast<IAudioTrack>(reply.readStrongBinder());
}
virtual sp<IAudioRecord> openRecord(
pid_t pid,
int inputSource,
uint32_t sampleRate,
int format,
int channelCount,
int frameCount,
uint32_t flags,
status_t *status)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(pid);
data.writeInt32(inputSource);
data.writeInt32(sampleRate);
data.writeInt32(format);
data.writeInt32(channelCount);
data.writeInt32(frameCount);
data.writeInt32(flags);
remote()->transact(OPEN_RECORD, data, &reply);
status_t lStatus = reply.readInt32();
if (status) {
*status = lStatus;
}
return interface_cast<IAudioRecord>(reply.readStrongBinder());
}
virtual uint32_t sampleRate(int output) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(output);
remote()->transact(SAMPLE_RATE, data, &reply);
return reply.readInt32();
}
virtual int channelCount(int output) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(output);
remote()->transact(CHANNEL_COUNT, data, &reply);
return reply.readInt32();
}
virtual int format(int output) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(output);
remote()->transact(FORMAT, data, &reply);
return reply.readInt32();
}
virtual size_t frameCount(int output) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(output);
remote()->transact(FRAME_COUNT, data, &reply);
return reply.readInt32();
}
virtual uint32_t latency(int output) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(output);
remote()->transact(LATENCY, data, &reply);
return reply.readInt32();
}
virtual status_t setMasterVolume(float value)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeFloat(value);
remote()->transact(SET_MASTER_VOLUME, data, &reply);
return reply.readInt32();
}
virtual status_t setMasterMute(bool muted)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(muted);
remote()->transact(SET_MASTER_MUTE, data, &reply);
return reply.readInt32();
}
virtual float masterVolume() const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
remote()->transact(MASTER_VOLUME, data, &reply);
return reply.readFloat();
}
virtual bool masterMute() const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
remote()->transact(MASTER_MUTE, data, &reply);
return reply.readInt32();
}
virtual status_t setStreamVolume(int stream, float value)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(stream);
data.writeFloat(value);
remote()->transact(SET_STREAM_VOLUME, data, &reply);
return reply.readInt32();
}
virtual status_t setStreamMute(int stream, bool muted)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(stream);
data.writeInt32(muted);
remote()->transact(SET_STREAM_MUTE, data, &reply);
return reply.readInt32();
}
virtual float streamVolume(int stream) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(stream);
remote()->transact(STREAM_VOLUME, data, &reply);
return reply.readFloat();
}
virtual bool streamMute(int stream) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(stream);
remote()->transact(STREAM_MUTE, data, &reply);
return reply.readInt32();
}
virtual status_t setRouting(int mode, uint32_t routes, uint32_t mask)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(mode);
data.writeInt32(routes);
data.writeInt32(mask);
remote()->transact(SET_ROUTING, data, &reply);
return reply.readInt32();
}
virtual uint32_t getRouting(int mode) const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(mode);
remote()->transact(GET_ROUTING, data, &reply);
return reply.readInt32();
}
virtual status_t setMode(int mode)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(mode);
remote()->transact(SET_MODE, data, &reply);
return reply.readInt32();
}
virtual int getMode() const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
remote()->transact(GET_MODE, data, &reply);
return reply.readInt32();
}
virtual status_t setMicMute(bool state)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(state);
remote()->transact(SET_MIC_MUTE, data, &reply);
return reply.readInt32();
}
virtual bool getMicMute() const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
remote()->transact(GET_MIC_MUTE, data, &reply);
return reply.readInt32();
}
virtual bool isMusicActive() const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
remote()->transact(IS_MUSIC_ACTIVE, data, &reply);
return reply.readInt32();
}
virtual status_t setParameter(const char* key, const char* value)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeCString(key);
data.writeCString(value);
remote()->transact(SET_PARAMETER, data, &reply);
return reply.readInt32();
}
virtual void registerClient(const sp<IAudioFlingerClient>& client)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeStrongBinder(client->asBinder());
remote()->transact(REGISTER_CLIENT, data, &reply);
}
virtual size_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
data.writeInt32(sampleRate);
data.writeInt32(format);
data.writeInt32(channelCount);
remote()->transact(GET_INPUTBUFFERSIZE, data, &reply);
return reply.readInt32();
}
virtual void wakeUp()
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
remote()->transact(WAKE_UP, data, &reply, IBinder::FLAG_ONEWAY);
return;
}
virtual bool isA2dpEnabled() const
{
Parcel data, reply;
data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
remote()->transact(IS_A2DP_ENABLED, data, &reply);
return (bool)reply.readInt32();
}
};
IMPLEMENT_META_INTERFACE(AudioFlinger, "android.media.IAudioFlinger");
// ----------------------------------------------------------------------
#define CHECK_INTERFACE(interface, data, reply) \
do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \
LOGW("Call incorrectly routed to " #interface); \
return PERMISSION_DENIED; \
} } while (0)
status_t BnAudioFlinger::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch(code) {
case CREATE_TRACK: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
pid_t pid = data.readInt32();
int streamType = data.readInt32();
uint32_t sampleRate = data.readInt32();
int format = data.readInt32();
int channelCount = data.readInt32();
size_t bufferCount = data.readInt32();
uint32_t flags = data.readInt32();
sp<IMemory> buffer = interface_cast<IMemory>(data.readStrongBinder());
status_t status;
sp<IAudioTrack> track = createTrack(pid,
streamType, sampleRate, format,
channelCount, bufferCount, flags, buffer, &status);
reply->writeInt32(status);
reply->writeStrongBinder(track->asBinder());
return NO_ERROR;
} break;
case OPEN_RECORD: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
pid_t pid = data.readInt32();
int inputSource = data.readInt32();
uint32_t sampleRate = data.readInt32();
int format = data.readInt32();
int channelCount = data.readInt32();
size_t bufferCount = data.readInt32();
uint32_t flags = data.readInt32();
status_t status;
sp<IAudioRecord> record = openRecord(pid, inputSource,
sampleRate, format, channelCount, bufferCount, flags, &status);
reply->writeInt32(status);
reply->writeStrongBinder(record->asBinder());
return NO_ERROR;
} break;
case SAMPLE_RATE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int output = data.readInt32();
reply->writeInt32( sampleRate(output) );
return NO_ERROR;
} break;
case CHANNEL_COUNT: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int output = data.readInt32();
reply->writeInt32( channelCount(output) );
return NO_ERROR;
} break;
case FORMAT: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int output = data.readInt32();
reply->writeInt32( format(output) );
return NO_ERROR;
} break;
case FRAME_COUNT: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int output = data.readInt32();
reply->writeInt32( frameCount(output) );
return NO_ERROR;
} break;
case LATENCY: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int output = data.readInt32();
reply->writeInt32( latency(output) );
return NO_ERROR;
} break;
case SET_MASTER_VOLUME: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeInt32( setMasterVolume(data.readFloat()) );
return NO_ERROR;
} break;
case SET_MASTER_MUTE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeInt32( setMasterMute(data.readInt32()) );
return NO_ERROR;
} break;
case MASTER_VOLUME: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeFloat( masterVolume() );
return NO_ERROR;
} break;
case MASTER_MUTE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeInt32( masterMute() );
return NO_ERROR;
} break;
case SET_STREAM_VOLUME: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int stream = data.readInt32();
reply->writeInt32( setStreamVolume(stream, data.readFloat()) );
return NO_ERROR;
} break;
case SET_STREAM_MUTE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int stream = data.readInt32();
reply->writeInt32( setStreamMute(stream, data.readInt32()) );
return NO_ERROR;
} break;
case STREAM_VOLUME: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int stream = data.readInt32();
reply->writeFloat( streamVolume(stream) );
return NO_ERROR;
} break;
case STREAM_MUTE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int stream = data.readInt32();
reply->writeInt32( streamMute(stream) );
return NO_ERROR;
} break;
case SET_ROUTING: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int mode = data.readInt32();
uint32_t routes = data.readInt32();
uint32_t mask = data.readInt32();
reply->writeInt32( setRouting(mode, routes, mask) );
return NO_ERROR;
} break;
case GET_ROUTING: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int mode = data.readInt32();
reply->writeInt32( getRouting(mode) );
return NO_ERROR;
} break;
case SET_MODE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int mode = data.readInt32();
reply->writeInt32( setMode(mode) );
return NO_ERROR;
} break;
case GET_MODE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeInt32( getMode() );
return NO_ERROR;
} break;
case SET_MIC_MUTE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
int state = data.readInt32();
reply->writeInt32( setMicMute(state) );
return NO_ERROR;
} break;
case GET_MIC_MUTE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeInt32( getMicMute() );
return NO_ERROR;
} break;
case IS_MUSIC_ACTIVE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeInt32( isMusicActive() );
return NO_ERROR;
} break;
case SET_PARAMETER: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
const char *key = data.readCString();
const char *value = data.readCString();
reply->writeInt32( setParameter(key, value) );
return NO_ERROR;
} break;
case REGISTER_CLIENT: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient>(data.readStrongBinder());
registerClient(client);
return NO_ERROR;
} break;
case GET_INPUTBUFFERSIZE: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
uint32_t sampleRate = data.readInt32();
int format = data.readInt32();
int channelCount = data.readInt32();
reply->writeInt32( getInputBufferSize(sampleRate, format, channelCount) );
return NO_ERROR;
} break;
case WAKE_UP: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
wakeUp();
return NO_ERROR;
} break;
case IS_A2DP_ENABLED: {
CHECK_INTERFACE(IAudioFlinger, data, reply);
reply->writeInt32( (int)isA2dpEnabled() );
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
// ----------------------------------------------------------------------------
}; // namespace android
|