1 /* 2 ** 3 ** Copyright 2007, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #define LOG_TAG "IAudioFlinger" 19 //#define LOG_NDEBUG 0 20 #include <utils/Log.h> 21 22 #include <stdint.h> 23 #include <sys/types.h> 24 25 #include <binder/Parcel.h> 26 27 #include <media/IAudioFlinger.h> 28 29 namespace android { 30 31 enum { 32 CREATE_TRACK = IBinder::FIRST_CALL_TRANSACTION, 33 OPEN_RECORD, 34 SAMPLE_RATE, 35 RESERVED, // obsolete, was CHANNEL_COUNT 36 FORMAT, 37 FRAME_COUNT, 38 LATENCY, 39 SET_MASTER_VOLUME, 40 SET_MASTER_MUTE, 41 MASTER_VOLUME, 42 MASTER_MUTE, 43 SET_STREAM_VOLUME, 44 SET_STREAM_MUTE, 45 STREAM_VOLUME, 46 STREAM_MUTE, 47 SET_MODE, 48 SET_MIC_MUTE, 49 GET_MIC_MUTE, 50 SET_PARAMETERS, 51 GET_PARAMETERS, 52 REGISTER_CLIENT, 53 GET_INPUTBUFFERSIZE, 54 OPEN_OUTPUT, 55 OPEN_DUPLICATE_OUTPUT, 56 CLOSE_OUTPUT, 57 SUSPEND_OUTPUT, 58 RESTORE_OUTPUT, 59 OPEN_INPUT, 60 CLOSE_INPUT, 61 SET_STREAM_OUTPUT, 62 SET_VOICE_VOLUME, 63 GET_RENDER_POSITION, 64 GET_INPUT_FRAMES_LOST, 65 NEW_AUDIO_SESSION_ID, 66 ACQUIRE_AUDIO_SESSION_ID, 67 RELEASE_AUDIO_SESSION_ID, 68 QUERY_NUM_EFFECTS, 69 QUERY_EFFECT, 70 GET_EFFECT_DESCRIPTOR, 71 CREATE_EFFECT, 72 MOVE_EFFECTS, 73 LOAD_HW_MODULE, 74 GET_PRIMARY_OUTPUT_SAMPLING_RATE, 75 GET_PRIMARY_OUTPUT_FRAME_COUNT, 76 SET_LOW_RAM_DEVICE, 77 }; 78 79 class BpAudioFlinger : public BpInterface<IAudioFlinger> 80 { 81 public: 82 BpAudioFlinger(const sp<IBinder>& impl) 83 : BpInterface<IAudioFlinger>(impl) 84 { 85 } 86 87 virtual sp<IAudioTrack> createTrack( 88 audio_stream_type_t streamType, 89 uint32_t sampleRate, 90 audio_format_t format, 91 audio_channel_mask_t channelMask, 92 size_t frameCount, 93 track_flags_t *flags, 94 const sp<IMemory>& sharedBuffer, 95 audio_io_handle_t output, 96 pid_t tid, 97 int *sessionId, 98 String8& name, 99 int clientUid, 100 status_t *status) 101 { 102 Parcel data, reply; 103 sp<IAudioTrack> track; 104 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 105 data.writeInt32((int32_t) streamType); 106 data.writeInt32(sampleRate); 107 data.writeInt32(format); 108 data.writeInt32(channelMask); 109 data.writeInt32(frameCount); 110 track_flags_t lFlags = flags != NULL ? *flags : (track_flags_t) TRACK_DEFAULT; 111 data.writeInt32(lFlags); 112 if (sharedBuffer != 0) { 113 data.writeInt32(true); 114 data.writeStrongBinder(sharedBuffer->asBinder()); 115 } else { 116 data.writeInt32(false); 117 } 118 data.writeInt32((int32_t) output); 119 data.writeInt32((int32_t) tid); 120 int lSessionId = 0; 121 if (sessionId != NULL) { 122 lSessionId = *sessionId; 123 } 124 data.writeInt32(lSessionId); 125 data.writeInt32(clientUid); 126 status_t lStatus = remote()->transact(CREATE_TRACK, data, &reply); 127 if (lStatus != NO_ERROR) { 128 ALOGE("createTrack error: %s", strerror(-lStatus)); 129 } else { 130 lFlags = reply.readInt32(); 131 if (flags != NULL) { 132 *flags = lFlags; 133 } 134 lSessionId = reply.readInt32(); 135 if (sessionId != NULL) { 136 *sessionId = lSessionId; 137 } 138 name = reply.readString8(); 139 lStatus = reply.readInt32(); 140 track = interface_cast<IAudioTrack>(reply.readStrongBinder()); 141 } 142 if (status) { 143 *status = lStatus; 144 } 145 return track; 146 } 147 148 virtual sp<IAudioRecord> openRecord( 149 audio_io_handle_t input, 150 uint32_t sampleRate, 151 audio_format_t format, 152 audio_channel_mask_t channelMask, 153 size_t frameCount, 154 track_flags_t *flags, 155 pid_t tid, 156 int *sessionId, 157 status_t *status) 158 { 159 Parcel data, reply; 160 sp<IAudioRecord> record; 161 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 162 data.writeInt32((int32_t) input); 163 data.writeInt32(sampleRate); 164 data.writeInt32(format); 165 data.writeInt32(channelMask); 166 data.writeInt32(frameCount); 167 track_flags_t lFlags = flags != NULL ? *flags : (track_flags_t) TRACK_DEFAULT; 168 data.writeInt32(lFlags); 169 data.writeInt32((int32_t) tid); 170 int lSessionId = 0; 171 if (sessionId != NULL) { 172 lSessionId = *sessionId; 173 } 174 data.writeInt32(lSessionId); 175 status_t lStatus = remote()->transact(OPEN_RECORD, data, &reply); 176 if (lStatus != NO_ERROR) { 177 ALOGE("openRecord error: %s", strerror(-lStatus)); 178 } else { 179 lFlags = reply.readInt32(); 180 if (flags != NULL) { 181 *flags = lFlags; 182 } 183 lSessionId = reply.readInt32(); 184 if (sessionId != NULL) { 185 *sessionId = lSessionId; 186 } 187 lStatus = reply.readInt32(); 188 record = interface_cast<IAudioRecord>(reply.readStrongBinder()); 189 if (lStatus == NO_ERROR) { 190 if (record == 0) { 191 ALOGE("openRecord should have returned an IAudioRecord"); 192 lStatus = UNKNOWN_ERROR; 193 } 194 } else { 195 if (record != 0) { 196 ALOGE("openRecord returned an IAudioRecord but with status %d", lStatus); 197 record.clear(); 198 } 199 } 200 } 201 if (status) { 202 *status = lStatus; 203 } 204 return record; 205 } 206 207 virtual uint32_t sampleRate(audio_io_handle_t output) const 208 { 209 Parcel data, reply; 210 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 211 data.writeInt32((int32_t) output); 212 remote()->transact(SAMPLE_RATE, data, &reply); 213 return reply.readInt32(); 214 } 215 216 virtual audio_format_t format(audio_io_handle_t output) const 217 { 218 Parcel data, reply; 219 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 220 data.writeInt32((int32_t) output); 221 remote()->transact(FORMAT, data, &reply); 222 return (audio_format_t) reply.readInt32(); 223 } 224 225 virtual size_t frameCount(audio_io_handle_t output) const 226 { 227 Parcel data, reply; 228 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 229 data.writeInt32((int32_t) output); 230 remote()->transact(FRAME_COUNT, data, &reply); 231 return reply.readInt32(); 232 } 233 234 virtual uint32_t latency(audio_io_handle_t output) const 235 { 236 Parcel data, reply; 237 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 238 data.writeInt32((int32_t) output); 239 remote()->transact(LATENCY, data, &reply); 240 return reply.readInt32(); 241 } 242 243 virtual status_t setMasterVolume(float value) 244 { 245 Parcel data, reply; 246 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 247 data.writeFloat(value); 248 remote()->transact(SET_MASTER_VOLUME, data, &reply); 249 return reply.readInt32(); 250 } 251 252 virtual status_t setMasterMute(bool muted) 253 { 254 Parcel data, reply; 255 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 256 data.writeInt32(muted); 257 remote()->transact(SET_MASTER_MUTE, data, &reply); 258 return reply.readInt32(); 259 } 260 261 virtual float masterVolume() const 262 { 263 Parcel data, reply; 264 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 265 remote()->transact(MASTER_VOLUME, data, &reply); 266 return reply.readFloat(); 267 } 268 269 virtual bool masterMute() const 270 { 271 Parcel data, reply; 272 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 273 remote()->transact(MASTER_MUTE, data, &reply); 274 return reply.readInt32(); 275 } 276 277 virtual status_t setStreamVolume(audio_stream_type_t stream, float value, 278 audio_io_handle_t output) 279 { 280 Parcel data, reply; 281 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 282 data.writeInt32((int32_t) stream); 283 data.writeFloat(value); 284 data.writeInt32((int32_t) output); 285 remote()->transact(SET_STREAM_VOLUME, data, &reply); 286 return reply.readInt32(); 287 } 288 289 virtual status_t setStreamMute(audio_stream_type_t stream, bool muted) 290 { 291 Parcel data, reply; 292 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 293 data.writeInt32((int32_t) stream); 294 data.writeInt32(muted); 295 remote()->transact(SET_STREAM_MUTE, data, &reply); 296 return reply.readInt32(); 297 } 298 299 virtual float streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const 300 { 301 Parcel data, reply; 302 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 303 data.writeInt32((int32_t) stream); 304 data.writeInt32((int32_t) output); 305 remote()->transact(STREAM_VOLUME, data, &reply); 306 return reply.readFloat(); 307 } 308 309 virtual bool streamMute(audio_stream_type_t stream) const 310 { 311 Parcel data, reply; 312 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 313 data.writeInt32((int32_t) stream); 314 remote()->transact(STREAM_MUTE, data, &reply); 315 return reply.readInt32(); 316 } 317 318 virtual status_t setMode(audio_mode_t mode) 319 { 320 Parcel data, reply; 321 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 322 data.writeInt32(mode); 323 remote()->transact(SET_MODE, data, &reply); 324 return reply.readInt32(); 325 } 326 327 virtual status_t setMicMute(bool state) 328 { 329 Parcel data, reply; 330 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 331 data.writeInt32(state); 332 remote()->transact(SET_MIC_MUTE, data, &reply); 333 return reply.readInt32(); 334 } 335 336 virtual bool getMicMute() const 337 { 338 Parcel data, reply; 339 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 340 remote()->transact(GET_MIC_MUTE, data, &reply); 341 return reply.readInt32(); 342 } 343 344 virtual status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) 345 { 346 Parcel data, reply; 347 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 348 data.writeInt32((int32_t) ioHandle); 349 data.writeString8(keyValuePairs); 350 remote()->transact(SET_PARAMETERS, data, &reply); 351 return reply.readInt32(); 352 } 353 354 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) const 355 { 356 Parcel data, reply; 357 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 358 data.writeInt32((int32_t) ioHandle); 359 data.writeString8(keys); 360 remote()->transact(GET_PARAMETERS, data, &reply); 361 return reply.readString8(); 362 } 363 364 virtual void registerClient(const sp<IAudioFlingerClient>& client) 365 { 366 Parcel data, reply; 367 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 368 data.writeStrongBinder(client->asBinder()); 369 remote()->transact(REGISTER_CLIENT, data, &reply); 370 } 371 372 virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format, 373 audio_channel_mask_t channelMask) const 374 { 375 Parcel data, reply; 376 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 377 data.writeInt32(sampleRate); 378 data.writeInt32(format); 379 data.writeInt32(channelMask); 380 remote()->transact(GET_INPUTBUFFERSIZE, data, &reply); 381 return reply.readInt32(); 382 } 383 384 virtual audio_io_handle_t openOutput(audio_module_handle_t module, 385 audio_devices_t *pDevices, 386 uint32_t *pSamplingRate, 387 audio_format_t *pFormat, 388 audio_channel_mask_t *pChannelMask, 389 uint32_t *pLatencyMs, 390 audio_output_flags_t flags, 391 const audio_offload_info_t *offloadInfo) 392 { 393 Parcel data, reply; 394 audio_devices_t devices = pDevices != NULL ? *pDevices : (audio_devices_t)0; 395 uint32_t samplingRate = pSamplingRate != NULL ? *pSamplingRate : 0; 396 audio_format_t format = pFormat != NULL ? *pFormat : AUDIO_FORMAT_DEFAULT; 397 audio_channel_mask_t channelMask = pChannelMask != NULL ? 398 *pChannelMask : (audio_channel_mask_t)0; 399 uint32_t latency = pLatencyMs != NULL ? *pLatencyMs : 0; 400 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 401 data.writeInt32(module); 402 data.writeInt32(devices); 403 data.writeInt32(samplingRate); 404 data.writeInt32(format); 405 data.writeInt32(channelMask); 406 data.writeInt32(latency); 407 data.writeInt32((int32_t) flags); 408 if (offloadInfo == NULL) { 409 data.writeInt32(0); 410 } else { 411 data.writeInt32(1); 412 data.write(offloadInfo, sizeof(audio_offload_info_t)); 413 } 414 remote()->transact(OPEN_OUTPUT, data, &reply); 415 audio_io_handle_t output = (audio_io_handle_t) reply.readInt32(); 416 ALOGV("openOutput() returned output, %d", output); 417 devices = (audio_devices_t)reply.readInt32(); 418 if (pDevices != NULL) *pDevices = devices; 419 samplingRate = reply.readInt32(); 420 if (pSamplingRate != NULL) *pSamplingRate = samplingRate; 421 format = (audio_format_t) reply.readInt32(); 422 if (pFormat != NULL) *pFormat = format; 423 channelMask = (audio_channel_mask_t)reply.readInt32(); 424 if (pChannelMask != NULL) *pChannelMask = channelMask; 425 latency = reply.readInt32(); 426 if (pLatencyMs != NULL) *pLatencyMs = latency; 427 return output; 428 } 429 430 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, 431 audio_io_handle_t output2) 432 { 433 Parcel data, reply; 434 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 435 data.writeInt32((int32_t) output1); 436 data.writeInt32((int32_t) output2); 437 remote()->transact(OPEN_DUPLICATE_OUTPUT, data, &reply); 438 return (audio_io_handle_t) reply.readInt32(); 439 } 440 441 virtual status_t closeOutput(audio_io_handle_t output) 442 { 443 Parcel data, reply; 444 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 445 data.writeInt32((int32_t) output); 446 remote()->transact(CLOSE_OUTPUT, data, &reply); 447 return reply.readInt32(); 448 } 449 450 virtual status_t suspendOutput(audio_io_handle_t output) 451 { 452 Parcel data, reply; 453 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 454 data.writeInt32((int32_t) output); 455 remote()->transact(SUSPEND_OUTPUT, data, &reply); 456 return reply.readInt32(); 457 } 458 459 virtual status_t restoreOutput(audio_io_handle_t output) 460 { 461 Parcel data, reply; 462 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 463 data.writeInt32((int32_t) output); 464 remote()->transact(RESTORE_OUTPUT, data, &reply); 465 return reply.readInt32(); 466 } 467 468 virtual audio_io_handle_t openInput(audio_module_handle_t module, 469 audio_devices_t *pDevices, 470 uint32_t *pSamplingRate, 471 audio_format_t *pFormat, 472 audio_channel_mask_t *pChannelMask) 473 { 474 Parcel data, reply; 475 audio_devices_t devices = pDevices != NULL ? *pDevices : (audio_devices_t)0; 476 uint32_t samplingRate = pSamplingRate != NULL ? *pSamplingRate : 0; 477 audio_format_t format = pFormat != NULL ? *pFormat : AUDIO_FORMAT_DEFAULT; 478 audio_channel_mask_t channelMask = pChannelMask != NULL ? 479 *pChannelMask : (audio_channel_mask_t)0; 480 481 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 482 data.writeInt32(module); 483 data.writeInt32(devices); 484 data.writeInt32(samplingRate); 485 data.writeInt32(format); 486 data.writeInt32(channelMask); 487 remote()->transact(OPEN_INPUT, data, &reply); 488 audio_io_handle_t input = (audio_io_handle_t) reply.readInt32(); 489 devices = (audio_devices_t)reply.readInt32(); 490 if (pDevices != NULL) *pDevices = devices; 491 samplingRate = reply.readInt32(); 492 if (pSamplingRate != NULL) *pSamplingRate = samplingRate; 493 format = (audio_format_t) reply.readInt32(); 494 if (pFormat != NULL) *pFormat = format; 495 channelMask = (audio_channel_mask_t)reply.readInt32(); 496 if (pChannelMask != NULL) *pChannelMask = channelMask; 497 return input; 498 } 499 500 virtual status_t closeInput(int input) 501 { 502 Parcel data, reply; 503 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 504 data.writeInt32(input); 505 remote()->transact(CLOSE_INPUT, data, &reply); 506 return reply.readInt32(); 507 } 508 509 virtual status_t setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output) 510 { 511 Parcel data, reply; 512 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 513 data.writeInt32((int32_t) stream); 514 data.writeInt32((int32_t) output); 515 remote()->transact(SET_STREAM_OUTPUT, data, &reply); 516 return reply.readInt32(); 517 } 518 519 virtual status_t setVoiceVolume(float volume) 520 { 521 Parcel data, reply; 522 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 523 data.writeFloat(volume); 524 remote()->transact(SET_VOICE_VOLUME, data, &reply); 525 return reply.readInt32(); 526 } 527 528 virtual status_t getRenderPosition(size_t *halFrames, size_t *dspFrames, 529 audio_io_handle_t output) const 530 { 531 Parcel data, reply; 532 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 533 data.writeInt32((int32_t) output); 534 remote()->transact(GET_RENDER_POSITION, data, &reply); 535 status_t status = reply.readInt32(); 536 if (status == NO_ERROR) { 537 uint32_t tmp = reply.readInt32(); 538 if (halFrames) { 539 *halFrames = tmp; 540 } 541 tmp = reply.readInt32(); 542 if (dspFrames) { 543 *dspFrames = tmp; 544 } 545 } 546 return status; 547 } 548 549 virtual size_t getInputFramesLost(audio_io_handle_t ioHandle) const 550 { 551 Parcel data, reply; 552 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 553 data.writeInt32((int32_t) ioHandle); 554 remote()->transact(GET_INPUT_FRAMES_LOST, data, &reply); 555 return reply.readInt32(); 556 } 557 558 virtual int newAudioSessionId() 559 { 560 Parcel data, reply; 561 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 562 status_t status = remote()->transact(NEW_AUDIO_SESSION_ID, data, &reply); 563 int id = 0; 564 if (status == NO_ERROR) { 565 id = reply.readInt32(); 566 } 567 return id; 568 } 569 570 virtual void acquireAudioSessionId(int audioSession) 571 { 572 Parcel data, reply; 573 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 574 data.writeInt32(audioSession); 575 remote()->transact(ACQUIRE_AUDIO_SESSION_ID, data, &reply); 576 } 577 578 virtual void releaseAudioSessionId(int audioSession) 579 { 580 Parcel data, reply; 581 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 582 data.writeInt32(audioSession); 583 remote()->transact(RELEASE_AUDIO_SESSION_ID, data, &reply); 584 } 585 586 virtual status_t queryNumberEffects(uint32_t *numEffects) const 587 { 588 Parcel data, reply; 589 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 590 status_t status = remote()->transact(QUERY_NUM_EFFECTS, data, &reply); 591 if (status != NO_ERROR) { 592 return status; 593 } 594 status = reply.readInt32(); 595 if (status != NO_ERROR) { 596 return status; 597 } 598 if (numEffects != NULL) { 599 *numEffects = (uint32_t)reply.readInt32(); 600 } 601 return NO_ERROR; 602 } 603 604 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const 605 { 606 if (pDescriptor == NULL) { 607 return BAD_VALUE; 608 } 609 Parcel data, reply; 610 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 611 data.writeInt32(index); 612 status_t status = remote()->transact(QUERY_EFFECT, data, &reply); 613 if (status != NO_ERROR) { 614 return status; 615 } 616 status = reply.readInt32(); 617 if (status != NO_ERROR) { 618 return status; 619 } 620 reply.read(pDescriptor, sizeof(effect_descriptor_t)); 621 return NO_ERROR; 622 } 623 624 virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid, 625 effect_descriptor_t *pDescriptor) const 626 { 627 if (pUuid == NULL || pDescriptor == NULL) { 628 return BAD_VALUE; 629 } 630 Parcel data, reply; 631 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 632 data.write(pUuid, sizeof(effect_uuid_t)); 633 status_t status = remote()->transact(GET_EFFECT_DESCRIPTOR, data, &reply); 634 if (status != NO_ERROR) { 635 return status; 636 } 637 status = reply.readInt32(); 638 if (status != NO_ERROR) { 639 return status; 640 } 641 reply.read(pDescriptor, sizeof(effect_descriptor_t)); 642 return NO_ERROR; 643 } 644 645 virtual sp<IEffect> createEffect( 646 effect_descriptor_t *pDesc, 647 const sp<IEffectClient>& client, 648 int32_t priority, 649 audio_io_handle_t output, 650 int sessionId, 651 status_t *status, 652 int *id, 653 int *enabled) 654 { 655 Parcel data, reply; 656 sp<IEffect> effect; 657 658 if (pDesc == NULL) { 659 return effect; 660 if (status) { 661 *status = BAD_VALUE; 662 } 663 } 664 665 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 666 data.write(pDesc, sizeof(effect_descriptor_t)); 667 data.writeStrongBinder(client->asBinder()); 668 data.writeInt32(priority); 669 data.writeInt32((int32_t) output); 670 data.writeInt32(sessionId); 671 672 status_t lStatus = remote()->transact(CREATE_EFFECT, data, &reply); 673 if (lStatus != NO_ERROR) { 674 ALOGE("createEffect error: %s", strerror(-lStatus)); 675 } else { 676 lStatus = reply.readInt32(); 677 int tmp = reply.readInt32(); 678 if (id) { 679 *id = tmp; 680 } 681 tmp = reply.readInt32(); 682 if (enabled != NULL) { 683 *enabled = tmp; 684 } 685 effect = interface_cast<IEffect>(reply.readStrongBinder()); 686 reply.read(pDesc, sizeof(effect_descriptor_t)); 687 } 688 if (status) { 689 *status = lStatus; 690 } 691 692 return effect; 693 } 694 695 virtual status_t moveEffects(int session, audio_io_handle_t srcOutput, 696 audio_io_handle_t dstOutput) 697 { 698 Parcel data, reply; 699 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 700 data.writeInt32(session); 701 data.writeInt32((int32_t) srcOutput); 702 data.writeInt32((int32_t) dstOutput); 703 remote()->transact(MOVE_EFFECTS, data, &reply); 704 return reply.readInt32(); 705 } 706 707 virtual audio_module_handle_t loadHwModule(const char *name) 708 { 709 Parcel data, reply; 710 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 711 data.writeCString(name); 712 remote()->transact(LOAD_HW_MODULE, data, &reply); 713 return (audio_module_handle_t) reply.readInt32(); 714 } 715 716 virtual uint32_t getPrimaryOutputSamplingRate() 717 { 718 Parcel data, reply; 719 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 720 remote()->transact(GET_PRIMARY_OUTPUT_SAMPLING_RATE, data, &reply); 721 return reply.readInt32(); 722 } 723 724 virtual size_t getPrimaryOutputFrameCount() 725 { 726 Parcel data, reply; 727 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 728 remote()->transact(GET_PRIMARY_OUTPUT_FRAME_COUNT, data, &reply); 729 return reply.readInt32(); 730 } 731 732 virtual status_t setLowRamDevice(bool isLowRamDevice) 733 { 734 Parcel data, reply; 735 data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor()); 736 data.writeInt32((int) isLowRamDevice); 737 remote()->transact(SET_LOW_RAM_DEVICE, data, &reply); 738 return reply.readInt32(); 739 } 740 741 }; 742 743 IMPLEMENT_META_INTERFACE(AudioFlinger, "android.media.IAudioFlinger"); 744 745 // ---------------------------------------------------------------------- 746 747 status_t BnAudioFlinger::onTransact( 748 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 749 { 750 switch (code) { 751 case CREATE_TRACK: { 752 CHECK_INTERFACE(IAudioFlinger, data, reply); 753 int streamType = data.readInt32(); 754 uint32_t sampleRate = data.readInt32(); 755 audio_format_t format = (audio_format_t) data.readInt32(); 756 audio_channel_mask_t channelMask = data.readInt32(); 757 size_t frameCount = data.readInt32(); 758 track_flags_t flags = (track_flags_t) data.readInt32(); 759 bool haveSharedBuffer = data.readInt32() != 0; 760 sp<IMemory> buffer; 761 if (haveSharedBuffer) { 762 buffer = interface_cast<IMemory>(data.readStrongBinder()); 763 } 764 audio_io_handle_t output = (audio_io_handle_t) data.readInt32(); 765 pid_t tid = (pid_t) data.readInt32(); 766 int sessionId = data.readInt32(); 767 int clientUid = data.readInt32(); 768 String8 name; 769 status_t status; 770 sp<IAudioTrack> track; 771 if ((haveSharedBuffer && (buffer == 0)) || 772 ((buffer != 0) && (buffer->pointer() == NULL))) { 773 ALOGW("CREATE_TRACK: cannot retrieve shared memory"); 774 status = DEAD_OBJECT; 775 } else { 776 track = createTrack( 777 (audio_stream_type_t) streamType, sampleRate, format, 778 channelMask, frameCount, &flags, buffer, output, tid, 779 &sessionId, name, clientUid, &status); 780 } 781 reply->writeInt32(flags); 782 reply->writeInt32(sessionId); 783 reply->writeString8(name); 784 reply->writeInt32(status); 785 reply->writeStrongBinder(track->asBinder()); 786 return NO_ERROR; 787 } break; 788 case OPEN_RECORD: { 789 CHECK_INTERFACE(IAudioFlinger, data, reply); 790 audio_io_handle_t input = (audio_io_handle_t) data.readInt32(); 791 uint32_t sampleRate = data.readInt32(); 792 audio_format_t format = (audio_format_t) data.readInt32(); 793 audio_channel_mask_t channelMask = data.readInt32(); 794 size_t frameCount = data.readInt32(); 795 track_flags_t flags = (track_flags_t) data.readInt32(); 796 pid_t tid = (pid_t) data.readInt32(); 797 int sessionId = data.readInt32(); 798 status_t status; 799 sp<IAudioRecord> record = openRecord(input, 800 sampleRate, format, channelMask, frameCount, &flags, tid, &sessionId, &status); 801 LOG_ALWAYS_FATAL_IF((record != 0) != (status == NO_ERROR)); 802 reply->writeInt32(flags); 803 reply->writeInt32(sessionId); 804 reply->writeInt32(status); 805 reply->writeStrongBinder(record->asBinder()); 806 return NO_ERROR; 807 } break; 808 case SAMPLE_RATE: { 809 CHECK_INTERFACE(IAudioFlinger, data, reply); 810 reply->writeInt32( sampleRate((audio_io_handle_t) data.readInt32()) ); 811 return NO_ERROR; 812 } break; 813 case FORMAT: { 814 CHECK_INTERFACE(IAudioFlinger, data, reply); 815 reply->writeInt32( format((audio_io_handle_t) data.readInt32()) ); 816 return NO_ERROR; 817 } break; 818 case FRAME_COUNT: { 819 CHECK_INTERFACE(IAudioFlinger, data, reply); 820 reply->writeInt32( frameCount((audio_io_handle_t) data.readInt32()) ); 821 return NO_ERROR; 822 } break; 823 case LATENCY: { 824 CHECK_INTERFACE(IAudioFlinger, data, reply); 825 reply->writeInt32( latency((audio_io_handle_t) data.readInt32()) ); 826 return NO_ERROR; 827 } break; 828 case SET_MASTER_VOLUME: { 829 CHECK_INTERFACE(IAudioFlinger, data, reply); 830 reply->writeInt32( setMasterVolume(data.readFloat()) ); 831 return NO_ERROR; 832 } break; 833 case SET_MASTER_MUTE: { 834 CHECK_INTERFACE(IAudioFlinger, data, reply); 835 reply->writeInt32( setMasterMute(data.readInt32()) ); 836 return NO_ERROR; 837 } break; 838 case MASTER_VOLUME: { 839 CHECK_INTERFACE(IAudioFlinger, data, reply); 840 reply->writeFloat( masterVolume() ); 841 return NO_ERROR; 842 } break; 843 case MASTER_MUTE: { 844 CHECK_INTERFACE(IAudioFlinger, data, reply); 845 reply->writeInt32( masterMute() ); 846 return NO_ERROR; 847 } break; 848 case SET_STREAM_VOLUME: { 849 CHECK_INTERFACE(IAudioFlinger, data, reply); 850 int stream = data.readInt32(); 851 float volume = data.readFloat(); 852 audio_io_handle_t output = (audio_io_handle_t) data.readInt32(); 853 reply->writeInt32( setStreamVolume((audio_stream_type_t) stream, volume, output) ); 854 return NO_ERROR; 855 } break; 856 case SET_STREAM_MUTE: { 857 CHECK_INTERFACE(IAudioFlinger, data, reply); 858 int stream = data.readInt32(); 859 reply->writeInt32( setStreamMute((audio_stream_type_t) stream, data.readInt32()) ); 860 return NO_ERROR; 861 } break; 862 case STREAM_VOLUME: { 863 CHECK_INTERFACE(IAudioFlinger, data, reply); 864 int stream = data.readInt32(); 865 int output = data.readInt32(); 866 reply->writeFloat( streamVolume((audio_stream_type_t) stream, output) ); 867 return NO_ERROR; 868 } break; 869 case STREAM_MUTE: { 870 CHECK_INTERFACE(IAudioFlinger, data, reply); 871 int stream = data.readInt32(); 872 reply->writeInt32( streamMute((audio_stream_type_t) stream) ); 873 return NO_ERROR; 874 } break; 875 case SET_MODE: { 876 CHECK_INTERFACE(IAudioFlinger, data, reply); 877 audio_mode_t mode = (audio_mode_t) data.readInt32(); 878 reply->writeInt32( setMode(mode) ); 879 return NO_ERROR; 880 } break; 881 case SET_MIC_MUTE: { 882 CHECK_INTERFACE(IAudioFlinger, data, reply); 883 int state = data.readInt32(); 884 reply->writeInt32( setMicMute(state) ); 885 return NO_ERROR; 886 } break; 887 case GET_MIC_MUTE: { 888 CHECK_INTERFACE(IAudioFlinger, data, reply); 889 reply->writeInt32( getMicMute() ); 890 return NO_ERROR; 891 } break; 892 case SET_PARAMETERS: { 893 CHECK_INTERFACE(IAudioFlinger, data, reply); 894 audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32(); 895 String8 keyValuePairs(data.readString8()); 896 reply->writeInt32(setParameters(ioHandle, keyValuePairs)); 897 return NO_ERROR; 898 } break; 899 case GET_PARAMETERS: { 900 CHECK_INTERFACE(IAudioFlinger, data, reply); 901 audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32(); 902 String8 keys(data.readString8()); 903 reply->writeString8(getParameters(ioHandle, keys)); 904 return NO_ERROR; 905 } break; 906 907 case REGISTER_CLIENT: { 908 CHECK_INTERFACE(IAudioFlinger, data, reply); 909 sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient>( 910 data.readStrongBinder()); 911 registerClient(client); 912 return NO_ERROR; 913 } break; 914 case GET_INPUTBUFFERSIZE: { 915 CHECK_INTERFACE(IAudioFlinger, data, reply); 916 uint32_t sampleRate = data.readInt32(); 917 audio_format_t format = (audio_format_t) data.readInt32(); 918 audio_channel_mask_t channelMask = data.readInt32(); 919 reply->writeInt32( getInputBufferSize(sampleRate, format, channelMask) ); 920 return NO_ERROR; 921 } break; 922 case OPEN_OUTPUT: { 923 CHECK_INTERFACE(IAudioFlinger, data, reply); 924 audio_module_handle_t module = (audio_module_handle_t)data.readInt32(); 925 audio_devices_t devices = (audio_devices_t)data.readInt32(); 926 uint32_t samplingRate = data.readInt32(); 927 audio_format_t format = (audio_format_t) data.readInt32(); 928 audio_channel_mask_t channelMask = (audio_channel_mask_t)data.readInt32(); 929 uint32_t latency = data.readInt32(); 930 audio_output_flags_t flags = (audio_output_flags_t) data.readInt32(); 931 bool hasOffloadInfo = data.readInt32() != 0; 932 audio_offload_info_t offloadInfo; 933 if (hasOffloadInfo) { 934 data.read(&offloadInfo, sizeof(audio_offload_info_t)); 935 } 936 audio_io_handle_t output = openOutput(module, 937 &devices, 938 &samplingRate, 939 &format, 940 &channelMask, 941 &latency, 942 flags, 943 hasOffloadInfo ? &offloadInfo : NULL); 944 ALOGV("OPEN_OUTPUT output, %p", output); 945 reply->writeInt32((int32_t) output); 946 reply->writeInt32(devices); 947 reply->writeInt32(samplingRate); 948 reply->writeInt32(format); 949 reply->writeInt32(channelMask); 950 reply->writeInt32(latency); 951 return NO_ERROR; 952 } break; 953 case OPEN_DUPLICATE_OUTPUT: { 954 CHECK_INTERFACE(IAudioFlinger, data, reply); 955 audio_io_handle_t output1 = (audio_io_handle_t) data.readInt32(); 956 audio_io_handle_t output2 = (audio_io_handle_t) data.readInt32(); 957 reply->writeInt32((int32_t) openDuplicateOutput(output1, output2)); 958 return NO_ERROR; 959 } break; 960 case CLOSE_OUTPUT: { 961 CHECK_INTERFACE(IAudioFlinger, data, reply); 962 reply->writeInt32(closeOutput((audio_io_handle_t) data.readInt32())); 963 return NO_ERROR; 964 } break; 965 case SUSPEND_OUTPUT: { 966 CHECK_INTERFACE(IAudioFlinger, data, reply); 967 reply->writeInt32(suspendOutput((audio_io_handle_t) data.readInt32())); 968 return NO_ERROR; 969 } break; 970 case RESTORE_OUTPUT: { 971 CHECK_INTERFACE(IAudioFlinger, data, reply); 972 reply->writeInt32(restoreOutput((audio_io_handle_t) data.readInt32())); 973 return NO_ERROR; 974 } break; 975 case OPEN_INPUT: { 976 CHECK_INTERFACE(IAudioFlinger, data, reply); 977 audio_module_handle_t module = (audio_module_handle_t)data.readInt32(); 978 audio_devices_t devices = (audio_devices_t)data.readInt32(); 979 uint32_t samplingRate = data.readInt32(); 980 audio_format_t format = (audio_format_t) data.readInt32(); 981 audio_channel_mask_t channelMask = (audio_channel_mask_t)data.readInt32(); 982 983 audio_io_handle_t input = openInput(module, 984 &devices, 985 &samplingRate, 986 &format, 987 &channelMask); 988 reply->writeInt32((int32_t) input); 989 reply->writeInt32(devices); 990 reply->writeInt32(samplingRate); 991 reply->writeInt32(format); 992 reply->writeInt32(channelMask); 993 return NO_ERROR; 994 } break; 995 case CLOSE_INPUT: { 996 CHECK_INTERFACE(IAudioFlinger, data, reply); 997 reply->writeInt32(closeInput((audio_io_handle_t) data.readInt32())); 998 return NO_ERROR; 999 } break; 1000 case SET_STREAM_OUTPUT: { 1001 CHECK_INTERFACE(IAudioFlinger, data, reply); 1002 uint32_t stream = data.readInt32(); 1003 audio_io_handle_t output = (audio_io_handle_t) data.readInt32(); 1004 reply->writeInt32(setStreamOutput((audio_stream_type_t) stream, output)); 1005 return NO_ERROR; 1006 } break; 1007 case SET_VOICE_VOLUME: { 1008 CHECK_INTERFACE(IAudioFlinger, data, reply); 1009 float volume = data.readFloat(); 1010 reply->writeInt32( setVoiceVolume(volume) ); 1011 return NO_ERROR; 1012 } break; 1013 case GET_RENDER_POSITION: { 1014 CHECK_INTERFACE(IAudioFlinger, data, reply); 1015 audio_io_handle_t output = (audio_io_handle_t) data.readInt32(); 1016 size_t halFrames; 1017 size_t dspFrames; 1018 status_t status = getRenderPosition(&halFrames, &dspFrames, output); 1019 reply->writeInt32(status); 1020 if (status == NO_ERROR) { 1021 reply->writeInt32(halFrames); 1022 reply->writeInt32(dspFrames); 1023 } 1024 return NO_ERROR; 1025 } 1026 case GET_INPUT_FRAMES_LOST: { 1027 CHECK_INTERFACE(IAudioFlinger, data, reply); 1028 audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32(); 1029 reply->writeInt32(getInputFramesLost(ioHandle)); 1030 return NO_ERROR; 1031 } break; 1032 case NEW_AUDIO_SESSION_ID: { 1033 CHECK_INTERFACE(IAudioFlinger, data, reply); 1034 reply->writeInt32(newAudioSessionId()); 1035 return NO_ERROR; 1036 } break; 1037 case ACQUIRE_AUDIO_SESSION_ID: { 1038 CHECK_INTERFACE(IAudioFlinger, data, reply); 1039 int audioSession = data.readInt32(); 1040 acquireAudioSessionId(audioSession); 1041 return NO_ERROR; 1042 } break; 1043 case RELEASE_AUDIO_SESSION_ID: { 1044 CHECK_INTERFACE(IAudioFlinger, data, reply); 1045 int audioSession = data.readInt32(); 1046 releaseAudioSessionId(audioSession); 1047 return NO_ERROR; 1048 } break; 1049 case QUERY_NUM_EFFECTS: { 1050 CHECK_INTERFACE(IAudioFlinger, data, reply); 1051 uint32_t numEffects; 1052 status_t status = queryNumberEffects(&numEffects); 1053 reply->writeInt32(status); 1054 if (status == NO_ERROR) { 1055 reply->writeInt32((int32_t)numEffects); 1056 } 1057 return NO_ERROR; 1058 } 1059 case QUERY_EFFECT: { 1060 CHECK_INTERFACE(IAudioFlinger, data, reply); 1061 effect_descriptor_t desc; 1062 status_t status = queryEffect(data.readInt32(), &desc); 1063 reply->writeInt32(status); 1064 if (status == NO_ERROR) { 1065 reply->write(&desc, sizeof(effect_descriptor_t)); 1066 } 1067 return NO_ERROR; 1068 } 1069 case GET_EFFECT_DESCRIPTOR: { 1070 CHECK_INTERFACE(IAudioFlinger, data, reply); 1071 effect_uuid_t uuid; 1072 data.read(&uuid, sizeof(effect_uuid_t)); 1073 effect_descriptor_t desc; 1074 status_t status = getEffectDescriptor(&uuid, &desc); 1075 reply->writeInt32(status); 1076 if (status == NO_ERROR) { 1077 reply->write(&desc, sizeof(effect_descriptor_t)); 1078 } 1079 return NO_ERROR; 1080 } 1081 case CREATE_EFFECT: { 1082 CHECK_INTERFACE(IAudioFlinger, data, reply); 1083 effect_descriptor_t desc; 1084 data.read(&desc, sizeof(effect_descriptor_t)); 1085 sp<IEffectClient> client = interface_cast<IEffectClient>(data.readStrongBinder()); 1086 int32_t priority = data.readInt32(); 1087 audio_io_handle_t output = (audio_io_handle_t) data.readInt32(); 1088 int sessionId = data.readInt32(); 1089 status_t status; 1090 int id; 1091 int enabled; 1092 1093 sp<IEffect> effect = createEffect(&desc, client, priority, output, sessionId, 1094 &status, &id, &enabled); 1095 reply->writeInt32(status); 1096 reply->writeInt32(id); 1097 reply->writeInt32(enabled); 1098 reply->writeStrongBinder(effect->asBinder()); 1099 reply->write(&desc, sizeof(effect_descriptor_t)); 1100 return NO_ERROR; 1101 } break; 1102 case MOVE_EFFECTS: { 1103 CHECK_INTERFACE(IAudioFlinger, data, reply); 1104 int session = data.readInt32(); 1105 audio_io_handle_t srcOutput = (audio_io_handle_t) data.readInt32(); 1106 audio_io_handle_t dstOutput = (audio_io_handle_t) data.readInt32(); 1107 reply->writeInt32(moveEffects(session, srcOutput, dstOutput)); 1108 return NO_ERROR; 1109 } break; 1110 case LOAD_HW_MODULE: { 1111 CHECK_INTERFACE(IAudioFlinger, data, reply); 1112 reply->writeInt32(loadHwModule(data.readCString())); 1113 return NO_ERROR; 1114 } break; 1115 case GET_PRIMARY_OUTPUT_SAMPLING_RATE: { 1116 CHECK_INTERFACE(IAudioFlinger, data, reply); 1117 reply->writeInt32(getPrimaryOutputSamplingRate()); 1118 return NO_ERROR; 1119 } break; 1120 case GET_PRIMARY_OUTPUT_FRAME_COUNT: { 1121 CHECK_INTERFACE(IAudioFlinger, data, reply); 1122 reply->writeInt32(getPrimaryOutputFrameCount()); 1123 return NO_ERROR; 1124 } break; 1125 case SET_LOW_RAM_DEVICE: { 1126 CHECK_INTERFACE(IAudioFlinger, data, reply); 1127 bool isLowRamDevice = data.readInt32() != 0; 1128 reply->writeInt32(setLowRamDevice(isLowRamDevice)); 1129 return NO_ERROR; 1130 } break; 1131 default: 1132 return BBinder::onTransact(code, data, reply, flags); 1133 } 1134 } 1135 1136 // ---------------------------------------------------------------------------- 1137 1138 }; // namespace android 1139