1 /* 2 ** 3 ** Copyright 2009, 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 "IAudioPolicyService" 19 #include <utils/Log.h> 20 21 #include <stdint.h> 22 #include <math.h> 23 #include <sys/types.h> 24 25 #include <binder/IPCThreadState.h> 26 #include <binder/Parcel.h> 27 #include <cutils/multiuser.h> 28 #include <media/AudioEffect.h> 29 #include <media/IAudioPolicyService.h> 30 #include <media/TimeCheck.h> 31 #include <private/android_filesystem_config.h> 32 #include <system/audio.h> 33 34 namespace android { 35 36 enum { 37 SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION, 38 GET_DEVICE_CONNECTION_STATE, 39 HANDLE_DEVICE_CONFIG_CHANGE, 40 SET_PHONE_STATE, 41 SET_RINGER_MODE, // reserved, no longer used 42 SET_FORCE_USE, 43 GET_FORCE_USE, 44 GET_OUTPUT, 45 START_OUTPUT, 46 STOP_OUTPUT, 47 RELEASE_OUTPUT, 48 GET_INPUT_FOR_ATTR, 49 START_INPUT, 50 STOP_INPUT, 51 RELEASE_INPUT, 52 INIT_STREAM_VOLUME, 53 SET_STREAM_VOLUME, 54 GET_STREAM_VOLUME, 55 GET_STRATEGY_FOR_STREAM, 56 GET_OUTPUT_FOR_EFFECT, 57 REGISTER_EFFECT, 58 UNREGISTER_EFFECT, 59 IS_STREAM_ACTIVE, 60 IS_SOURCE_ACTIVE, 61 GET_DEVICES_FOR_STREAM, 62 QUERY_DEFAULT_PRE_PROCESSING, 63 SET_EFFECT_ENABLED, 64 IS_STREAM_ACTIVE_REMOTELY, 65 IS_OFFLOAD_SUPPORTED, 66 LIST_AUDIO_PORTS, 67 GET_AUDIO_PORT, 68 CREATE_AUDIO_PATCH, 69 RELEASE_AUDIO_PATCH, 70 LIST_AUDIO_PATCHES, 71 SET_AUDIO_PORT_CONFIG, 72 REGISTER_CLIENT, 73 GET_OUTPUT_FOR_ATTR, 74 ACQUIRE_SOUNDTRIGGER_SESSION, 75 RELEASE_SOUNDTRIGGER_SESSION, 76 GET_PHONE_STATE, 77 REGISTER_POLICY_MIXES, 78 START_AUDIO_SOURCE, 79 STOP_AUDIO_SOURCE, 80 SET_AUDIO_PORT_CALLBACK_ENABLED, 81 SET_MASTER_MONO, 82 GET_MASTER_MONO, 83 GET_STREAM_VOLUME_DB, 84 GET_SURROUND_FORMATS, 85 SET_SURROUND_FORMAT_ENABLED 86 }; 87 88 #define MAX_ITEMS_PER_LIST 1024 89 90 class BpAudioPolicyService : public BpInterface<IAudioPolicyService> 91 { 92 public: 93 explicit BpAudioPolicyService(const sp<IBinder>& impl) 94 : BpInterface<IAudioPolicyService>(impl) 95 { 96 } 97 98 virtual status_t setDeviceConnectionState( 99 audio_devices_t device, 100 audio_policy_dev_state_t state, 101 const char *device_address, 102 const char *device_name) 103 { 104 Parcel data, reply; 105 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 106 data.writeInt32(static_cast <uint32_t>(device)); 107 data.writeInt32(static_cast <uint32_t>(state)); 108 data.writeCString(device_address); 109 data.writeCString(device_name); 110 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply); 111 return static_cast <status_t> (reply.readInt32()); 112 } 113 114 virtual audio_policy_dev_state_t getDeviceConnectionState( 115 audio_devices_t device, 116 const char *device_address) 117 { 118 Parcel data, reply; 119 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 120 data.writeInt32(static_cast <uint32_t>(device)); 121 data.writeCString(device_address); 122 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply); 123 return static_cast <audio_policy_dev_state_t>(reply.readInt32()); 124 } 125 126 virtual status_t handleDeviceConfigChange(audio_devices_t device, 127 const char *device_address, 128 const char *device_name) 129 { 130 Parcel data, reply; 131 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 132 data.writeInt32(static_cast <uint32_t>(device)); 133 data.writeCString(device_address); 134 data.writeCString(device_name); 135 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply); 136 return static_cast <status_t> (reply.readInt32()); 137 } 138 139 virtual status_t setPhoneState(audio_mode_t state) 140 { 141 Parcel data, reply; 142 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 143 data.writeInt32(state); 144 remote()->transact(SET_PHONE_STATE, data, &reply); 145 return static_cast <status_t> (reply.readInt32()); 146 } 147 148 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) 149 { 150 Parcel data, reply; 151 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 152 data.writeInt32(static_cast <uint32_t>(usage)); 153 data.writeInt32(static_cast <uint32_t>(config)); 154 remote()->transact(SET_FORCE_USE, data, &reply); 155 return static_cast <status_t> (reply.readInt32()); 156 } 157 158 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage) 159 { 160 Parcel data, reply; 161 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 162 data.writeInt32(static_cast <uint32_t>(usage)); 163 remote()->transact(GET_FORCE_USE, data, &reply); 164 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32()); 165 } 166 167 virtual audio_io_handle_t getOutput(audio_stream_type_t stream) 168 { 169 Parcel data, reply; 170 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 171 data.writeInt32(static_cast <uint32_t>(stream)); 172 remote()->transact(GET_OUTPUT, data, &reply); 173 return static_cast <audio_io_handle_t> (reply.readInt32()); 174 } 175 176 virtual status_t getOutputForAttr(const audio_attributes_t *attr, 177 audio_io_handle_t *output, 178 audio_session_t session, 179 audio_stream_type_t *stream, 180 pid_t pid, 181 uid_t uid, 182 const audio_config_t *config, 183 audio_output_flags_t flags, 184 audio_port_handle_t *selectedDeviceId, 185 audio_port_handle_t *portId) 186 { 187 Parcel data, reply; 188 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 189 if (attr == NULL) { 190 if (stream == NULL) { 191 ALOGE("getOutputForAttr(): NULL audio attributes and stream type"); 192 return BAD_VALUE; 193 } 194 if (*stream == AUDIO_STREAM_DEFAULT) { 195 ALOGE("getOutputForAttr unspecified stream type"); 196 return BAD_VALUE; 197 } 198 } 199 if (output == NULL) { 200 ALOGE("getOutputForAttr NULL output - shouldn't happen"); 201 return BAD_VALUE; 202 } 203 if (selectedDeviceId == NULL) { 204 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen"); 205 return BAD_VALUE; 206 } 207 if (portId == NULL) { 208 ALOGE("getOutputForAttr NULL portId - shouldn't happen"); 209 return BAD_VALUE; 210 } 211 if (attr == NULL) { 212 data.writeInt32(0); 213 } else { 214 data.writeInt32(1); 215 data.write(attr, sizeof(audio_attributes_t)); 216 } 217 data.writeInt32(session); 218 if (stream == NULL) { 219 data.writeInt32(0); 220 } else { 221 data.writeInt32(1); 222 data.writeInt32(*stream); 223 } 224 data.writeInt32(pid); 225 data.writeInt32(uid); 226 data.write(config, sizeof(audio_config_t)); 227 data.writeInt32(static_cast <uint32_t>(flags)); 228 data.writeInt32(*selectedDeviceId); 229 data.writeInt32(*portId); 230 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply); 231 if (status != NO_ERROR) { 232 return status; 233 } 234 status = (status_t)reply.readInt32(); 235 if (status != NO_ERROR) { 236 return status; 237 } 238 *output = (audio_io_handle_t)reply.readInt32(); 239 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32(); 240 if (stream != NULL) { 241 *stream = lStream; 242 } 243 *selectedDeviceId = (audio_port_handle_t)reply.readInt32(); 244 *portId = (audio_port_handle_t)reply.readInt32(); 245 return status; 246 } 247 248 virtual status_t startOutput(audio_io_handle_t output, 249 audio_stream_type_t stream, 250 audio_session_t session) 251 { 252 Parcel data, reply; 253 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 254 data.writeInt32(output); 255 data.writeInt32((int32_t) stream); 256 data.writeInt32((int32_t) session); 257 remote()->transact(START_OUTPUT, data, &reply); 258 return static_cast <status_t> (reply.readInt32()); 259 } 260 261 virtual status_t stopOutput(audio_io_handle_t output, 262 audio_stream_type_t stream, 263 audio_session_t session) 264 { 265 Parcel data, reply; 266 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 267 data.writeInt32(output); 268 data.writeInt32((int32_t) stream); 269 data.writeInt32((int32_t) session); 270 remote()->transact(STOP_OUTPUT, data, &reply); 271 return static_cast <status_t> (reply.readInt32()); 272 } 273 274 virtual void releaseOutput(audio_io_handle_t output, 275 audio_stream_type_t stream, 276 audio_session_t session) 277 { 278 Parcel data, reply; 279 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 280 data.writeInt32(output); 281 data.writeInt32((int32_t)stream); 282 data.writeInt32((int32_t)session); 283 remote()->transact(RELEASE_OUTPUT, data, &reply); 284 } 285 286 virtual status_t getInputForAttr(const audio_attributes_t *attr, 287 audio_io_handle_t *input, 288 audio_session_t session, 289 pid_t pid, 290 uid_t uid, 291 const String16& opPackageName, 292 const audio_config_base_t *config, 293 audio_input_flags_t flags, 294 audio_port_handle_t *selectedDeviceId, 295 audio_port_handle_t *portId) 296 { 297 Parcel data, reply; 298 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 299 if (attr == NULL) { 300 ALOGE("getInputForAttr NULL attr - shouldn't happen"); 301 return BAD_VALUE; 302 } 303 if (input == NULL) { 304 ALOGE("getInputForAttr NULL input - shouldn't happen"); 305 return BAD_VALUE; 306 } 307 if (selectedDeviceId == NULL) { 308 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen"); 309 return BAD_VALUE; 310 } 311 if (portId == NULL) { 312 ALOGE("getInputForAttr NULL portId - shouldn't happen"); 313 return BAD_VALUE; 314 } 315 data.write(attr, sizeof(audio_attributes_t)); 316 data.writeInt32(*input); 317 data.writeInt32(session); 318 data.writeInt32(pid); 319 data.writeInt32(uid); 320 data.writeString16(opPackageName); 321 data.write(config, sizeof(audio_config_base_t)); 322 data.writeInt32(flags); 323 data.writeInt32(*selectedDeviceId); 324 data.writeInt32(*portId); 325 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply); 326 if (status != NO_ERROR) { 327 return status; 328 } 329 status = reply.readInt32(); 330 if (status != NO_ERROR) { 331 return status; 332 } 333 *input = (audio_io_handle_t)reply.readInt32(); 334 *selectedDeviceId = (audio_port_handle_t)reply.readInt32(); 335 *portId = (audio_port_handle_t)reply.readInt32(); 336 return NO_ERROR; 337 } 338 339 virtual status_t startInput(audio_port_handle_t portId, 340 bool *silenced) 341 { 342 Parcel data, reply; 343 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 344 data.writeInt32(portId); 345 data.writeInt32(*silenced ? 1 : 0); 346 remote()->transact(START_INPUT, data, &reply); 347 status_t status = static_cast <status_t> (reply.readInt32()); 348 *silenced = reply.readInt32() == 1; 349 return status; 350 } 351 352 virtual status_t stopInput(audio_port_handle_t portId) 353 { 354 Parcel data, reply; 355 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 356 data.writeInt32(portId); 357 remote()->transact(STOP_INPUT, data, &reply); 358 return static_cast <status_t> (reply.readInt32()); 359 } 360 361 virtual void releaseInput(audio_port_handle_t portId) 362 { 363 Parcel data, reply; 364 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 365 data.writeInt32(portId); 366 remote()->transact(RELEASE_INPUT, data, &reply); 367 } 368 369 virtual status_t initStreamVolume(audio_stream_type_t stream, 370 int indexMin, 371 int indexMax) 372 { 373 Parcel data, reply; 374 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 375 data.writeInt32(static_cast <uint32_t>(stream)); 376 data.writeInt32(indexMin); 377 data.writeInt32(indexMax); 378 remote()->transact(INIT_STREAM_VOLUME, data, &reply); 379 return static_cast <status_t> (reply.readInt32()); 380 } 381 382 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, 383 int index, 384 audio_devices_t device) 385 { 386 Parcel data, reply; 387 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 388 data.writeInt32(static_cast <uint32_t>(stream)); 389 data.writeInt32(index); 390 data.writeInt32(static_cast <uint32_t>(device)); 391 remote()->transact(SET_STREAM_VOLUME, data, &reply); 392 return static_cast <status_t> (reply.readInt32()); 393 } 394 395 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, 396 int *index, 397 audio_devices_t device) 398 { 399 Parcel data, reply; 400 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 401 data.writeInt32(static_cast <uint32_t>(stream)); 402 data.writeInt32(static_cast <uint32_t>(device)); 403 404 remote()->transact(GET_STREAM_VOLUME, data, &reply); 405 int lIndex = reply.readInt32(); 406 if (index) *index = lIndex; 407 return static_cast <status_t> (reply.readInt32()); 408 } 409 410 virtual uint32_t getStrategyForStream(audio_stream_type_t stream) 411 { 412 Parcel data, reply; 413 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 414 data.writeInt32(static_cast <uint32_t>(stream)); 415 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply); 416 return reply.readInt32(); 417 } 418 419 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream) 420 { 421 Parcel data, reply; 422 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 423 data.writeInt32(static_cast <uint32_t>(stream)); 424 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply); 425 return (audio_devices_t) reply.readInt32(); 426 } 427 428 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc) 429 { 430 Parcel data, reply; 431 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 432 data.write(desc, sizeof(effect_descriptor_t)); 433 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply); 434 return static_cast <audio_io_handle_t> (reply.readInt32()); 435 } 436 437 virtual status_t registerEffect(const effect_descriptor_t *desc, 438 audio_io_handle_t io, 439 uint32_t strategy, 440 audio_session_t session, 441 int id) 442 { 443 Parcel data, reply; 444 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 445 data.write(desc, sizeof(effect_descriptor_t)); 446 data.writeInt32(io); 447 data.writeInt32(strategy); 448 data.writeInt32(session); 449 data.writeInt32(id); 450 remote()->transact(REGISTER_EFFECT, data, &reply); 451 return static_cast <status_t> (reply.readInt32()); 452 } 453 454 virtual status_t unregisterEffect(int id) 455 { 456 Parcel data, reply; 457 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 458 data.writeInt32(id); 459 remote()->transact(UNREGISTER_EFFECT, data, &reply); 460 return static_cast <status_t> (reply.readInt32()); 461 } 462 463 virtual status_t setEffectEnabled(int id, bool enabled) 464 { 465 Parcel data, reply; 466 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 467 data.writeInt32(id); 468 data.writeInt32(enabled); 469 remote()->transact(SET_EFFECT_ENABLED, data, &reply); 470 return static_cast <status_t> (reply.readInt32()); 471 } 472 473 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const 474 { 475 Parcel data, reply; 476 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 477 data.writeInt32((int32_t) stream); 478 data.writeInt32(inPastMs); 479 remote()->transact(IS_STREAM_ACTIVE, data, &reply); 480 return reply.readInt32(); 481 } 482 483 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const 484 { 485 Parcel data, reply; 486 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 487 data.writeInt32((int32_t) stream); 488 data.writeInt32(inPastMs); 489 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply); 490 return reply.readInt32(); 491 } 492 493 virtual bool isSourceActive(audio_source_t source) const 494 { 495 Parcel data, reply; 496 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 497 data.writeInt32((int32_t) source); 498 remote()->transact(IS_SOURCE_ACTIVE, data, &reply); 499 return reply.readInt32(); 500 } 501 502 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession, 503 effect_descriptor_t *descriptors, 504 uint32_t *count) 505 { 506 if (descriptors == NULL || count == NULL) { 507 return BAD_VALUE; 508 } 509 Parcel data, reply; 510 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 511 data.writeInt32(audioSession); 512 data.writeInt32(*count); 513 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply); 514 if (status != NO_ERROR) { 515 return status; 516 } 517 status = static_cast <status_t> (reply.readInt32()); 518 uint32_t retCount = reply.readInt32(); 519 if (retCount != 0) { 520 uint32_t numDesc = (retCount < *count) ? retCount : *count; 521 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc); 522 } 523 *count = retCount; 524 return status; 525 } 526 527 virtual bool isOffloadSupported(const audio_offload_info_t& info) 528 { 529 Parcel data, reply; 530 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 531 data.write(&info, sizeof(audio_offload_info_t)); 532 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply); 533 return reply.readInt32(); 534 } 535 536 virtual status_t listAudioPorts(audio_port_role_t role, 537 audio_port_type_t type, 538 unsigned int *num_ports, 539 struct audio_port *ports, 540 unsigned int *generation) 541 { 542 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || 543 generation == NULL) { 544 return BAD_VALUE; 545 } 546 Parcel data, reply; 547 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 548 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports; 549 data.writeInt32(role); 550 data.writeInt32(type); 551 data.writeInt32(numPortsReq); 552 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply); 553 if (status == NO_ERROR) { 554 status = (status_t)reply.readInt32(); 555 *num_ports = (unsigned int)reply.readInt32(); 556 } 557 if (status == NO_ERROR) { 558 if (numPortsReq > *num_ports) { 559 numPortsReq = *num_ports; 560 } 561 if (numPortsReq > 0) { 562 reply.read(ports, numPortsReq * sizeof(struct audio_port)); 563 } 564 *generation = reply.readInt32(); 565 } 566 return status; 567 } 568 569 virtual status_t getAudioPort(struct audio_port *port) 570 { 571 if (port == NULL) { 572 return BAD_VALUE; 573 } 574 Parcel data, reply; 575 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 576 data.write(port, sizeof(struct audio_port)); 577 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply); 578 if (status != NO_ERROR || 579 (status = (status_t)reply.readInt32()) != NO_ERROR) { 580 return status; 581 } 582 reply.read(port, sizeof(struct audio_port)); 583 return status; 584 } 585 586 virtual status_t createAudioPatch(const struct audio_patch *patch, 587 audio_patch_handle_t *handle) 588 { 589 if (patch == NULL || handle == NULL) { 590 return BAD_VALUE; 591 } 592 Parcel data, reply; 593 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 594 data.write(patch, sizeof(struct audio_patch)); 595 data.write(handle, sizeof(audio_patch_handle_t)); 596 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply); 597 if (status != NO_ERROR || 598 (status = (status_t)reply.readInt32()) != NO_ERROR) { 599 return status; 600 } 601 reply.read(handle, sizeof(audio_patch_handle_t)); 602 return status; 603 } 604 605 virtual status_t releaseAudioPatch(audio_patch_handle_t handle) 606 { 607 Parcel data, reply; 608 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 609 data.write(&handle, sizeof(audio_patch_handle_t)); 610 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply); 611 if (status != NO_ERROR) { 612 status = (status_t)reply.readInt32(); 613 } 614 return status; 615 } 616 617 virtual status_t listAudioPatches(unsigned int *num_patches, 618 struct audio_patch *patches, 619 unsigned int *generation) 620 { 621 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) || 622 generation == NULL) { 623 return BAD_VALUE; 624 } 625 Parcel data, reply; 626 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 627 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches; 628 data.writeInt32(numPatchesReq); 629 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply); 630 if (status == NO_ERROR) { 631 status = (status_t)reply.readInt32(); 632 *num_patches = (unsigned int)reply.readInt32(); 633 } 634 if (status == NO_ERROR) { 635 if (numPatchesReq > *num_patches) { 636 numPatchesReq = *num_patches; 637 } 638 if (numPatchesReq > 0) { 639 reply.read(patches, numPatchesReq * sizeof(struct audio_patch)); 640 } 641 *generation = reply.readInt32(); 642 } 643 return status; 644 } 645 646 virtual status_t setAudioPortConfig(const struct audio_port_config *config) 647 { 648 if (config == NULL) { 649 return BAD_VALUE; 650 } 651 Parcel data, reply; 652 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 653 data.write(config, sizeof(struct audio_port_config)); 654 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply); 655 if (status != NO_ERROR) { 656 status = (status_t)reply.readInt32(); 657 } 658 return status; 659 } 660 661 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client) 662 { 663 Parcel data, reply; 664 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 665 data.writeStrongBinder(IInterface::asBinder(client)); 666 remote()->transact(REGISTER_CLIENT, data, &reply); 667 } 668 669 virtual void setAudioPortCallbacksEnabled(bool enabled) 670 { 671 Parcel data, reply; 672 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 673 data.writeInt32(enabled ? 1 : 0); 674 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply); 675 } 676 677 virtual status_t acquireSoundTriggerSession(audio_session_t *session, 678 audio_io_handle_t *ioHandle, 679 audio_devices_t *device) 680 { 681 if (session == NULL || ioHandle == NULL || device == NULL) { 682 return BAD_VALUE; 683 } 684 Parcel data, reply; 685 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 686 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply); 687 if (status != NO_ERROR) { 688 return status; 689 } 690 status = (status_t)reply.readInt32(); 691 if (status == NO_ERROR) { 692 *session = (audio_session_t)reply.readInt32(); 693 *ioHandle = (audio_io_handle_t)reply.readInt32(); 694 *device = (audio_devices_t)reply.readInt32(); 695 } 696 return status; 697 } 698 699 virtual status_t releaseSoundTriggerSession(audio_session_t session) 700 { 701 Parcel data, reply; 702 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 703 data.writeInt32(session); 704 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply); 705 if (status != NO_ERROR) { 706 return status; 707 } 708 return (status_t)reply.readInt32(); 709 } 710 711 virtual audio_mode_t getPhoneState() 712 { 713 Parcel data, reply; 714 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 715 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply); 716 if (status != NO_ERROR) { 717 return AUDIO_MODE_INVALID; 718 } 719 return (audio_mode_t)reply.readInt32(); 720 } 721 722 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) 723 { 724 Parcel data, reply; 725 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 726 data.writeInt32(registration ? 1 : 0); 727 size_t size = mixes.size(); 728 if (size > MAX_MIXES_PER_POLICY) { 729 size = MAX_MIXES_PER_POLICY; 730 } 731 size_t sizePosition = data.dataPosition(); 732 data.writeInt32(size); 733 size_t finalSize = size; 734 for (size_t i = 0; i < size; i++) { 735 size_t position = data.dataPosition(); 736 if (mixes[i].writeToParcel(&data) != NO_ERROR) { 737 data.setDataPosition(position); 738 finalSize--; 739 } 740 } 741 if (size != finalSize) { 742 size_t position = data.dataPosition(); 743 data.setDataPosition(sizePosition); 744 data.writeInt32(finalSize); 745 data.setDataPosition(position); 746 } 747 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply); 748 if (status == NO_ERROR) { 749 status = (status_t)reply.readInt32(); 750 } 751 return status; 752 } 753 754 virtual status_t startAudioSource(const struct audio_port_config *source, 755 const audio_attributes_t *attributes, 756 audio_patch_handle_t *handle) 757 { 758 Parcel data, reply; 759 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 760 if (source == NULL || attributes == NULL || handle == NULL) { 761 return BAD_VALUE; 762 } 763 data.write(source, sizeof(struct audio_port_config)); 764 data.write(attributes, sizeof(audio_attributes_t)); 765 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply); 766 if (status != NO_ERROR) { 767 return status; 768 } 769 status = (status_t)reply.readInt32(); 770 if (status != NO_ERROR) { 771 return status; 772 } 773 *handle = (audio_patch_handle_t)reply.readInt32(); 774 return status; 775 } 776 777 virtual status_t stopAudioSource(audio_patch_handle_t handle) 778 { 779 Parcel data, reply; 780 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 781 data.writeInt32(handle); 782 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply); 783 if (status != NO_ERROR) { 784 return status; 785 } 786 status = (status_t)reply.readInt32(); 787 return status; 788 } 789 790 virtual status_t setMasterMono(bool mono) 791 { 792 Parcel data, reply; 793 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 794 data.writeInt32(static_cast<int32_t>(mono)); 795 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply); 796 if (status != NO_ERROR) { 797 return status; 798 } 799 return static_cast<status_t>(reply.readInt32()); 800 } 801 802 virtual status_t getMasterMono(bool *mono) 803 { 804 if (mono == nullptr) { 805 return BAD_VALUE; 806 } 807 Parcel data, reply; 808 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 809 810 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply); 811 if (status != NO_ERROR) { 812 return status; 813 } 814 status = static_cast<status_t>(reply.readInt32()); 815 if (status == NO_ERROR) { 816 *mono = static_cast<bool>(reply.readInt32()); 817 } 818 return status; 819 } 820 821 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) 822 { 823 Parcel data, reply; 824 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 825 data.writeInt32(static_cast <int32_t>(stream)); 826 data.writeInt32(static_cast <int32_t>(index)); 827 data.writeUint32(static_cast <uint32_t>(device)); 828 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply); 829 if (status != NO_ERROR) { 830 return NAN; 831 } 832 return reply.readFloat(); 833 } 834 835 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats, 836 audio_format_t *surroundFormats, 837 bool *surroundFormatsEnabled, 838 bool reported) 839 { 840 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && 841 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { 842 return BAD_VALUE; 843 } 844 Parcel data, reply; 845 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 846 unsigned int numSurroundFormatsReq = *numSurroundFormats; 847 data.writeUint32(numSurroundFormatsReq); 848 data.writeBool(reported); 849 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply); 850 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) { 851 *numSurroundFormats = reply.readUint32(); 852 } 853 if (status == NO_ERROR) { 854 if (numSurroundFormatsReq > *numSurroundFormats) { 855 numSurroundFormatsReq = *numSurroundFormats; 856 } 857 if (numSurroundFormatsReq > 0) { 858 status = reply.read(surroundFormats, 859 numSurroundFormatsReq * sizeof(audio_format_t)); 860 if (status != NO_ERROR) { 861 return status; 862 } 863 status = reply.read(surroundFormatsEnabled, 864 numSurroundFormatsReq * sizeof(bool)); 865 } 866 } 867 return status; 868 } 869 870 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) 871 { 872 Parcel data, reply; 873 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor()); 874 data.writeInt32(audioFormat); 875 data.writeBool(enabled); 876 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply); 877 if (status != NO_ERROR) { 878 return status; 879 } 880 return reply.readInt32(); 881 } 882 }; 883 884 IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService"); 885 886 // ---------------------------------------------------------------------- 887 888 status_t BnAudioPolicyService::onTransact( 889 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 890 { 891 // make sure transactions reserved to AudioFlinger do not come from other processes 892 switch (code) { 893 case START_OUTPUT: 894 case STOP_OUTPUT: 895 case RELEASE_OUTPUT: 896 case GET_INPUT_FOR_ATTR: 897 case START_INPUT: 898 case STOP_INPUT: 899 case RELEASE_INPUT: 900 case GET_STRATEGY_FOR_STREAM: 901 case GET_OUTPUT_FOR_EFFECT: 902 case REGISTER_EFFECT: 903 case UNREGISTER_EFFECT: 904 case SET_EFFECT_ENABLED: 905 case GET_OUTPUT_FOR_ATTR: 906 case ACQUIRE_SOUNDTRIGGER_SESSION: 907 case RELEASE_SOUNDTRIGGER_SESSION: 908 ALOGW("%s: transaction %d received from PID %d", 909 __func__, code, IPCThreadState::self()->getCallingPid()); 910 // return status only for non void methods 911 switch (code) { 912 case RELEASE_OUTPUT: 913 case RELEASE_INPUT: 914 break; 915 default: 916 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION)); 917 break; 918 } 919 return OK; 920 default: 921 break; 922 } 923 924 // make sure the following transactions come from system components 925 switch (code) { 926 case SET_DEVICE_CONNECTION_STATE: 927 case HANDLE_DEVICE_CONFIG_CHANGE: 928 case SET_PHONE_STATE: 929 //FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available 930 // case SET_FORCE_USE: 931 case INIT_STREAM_VOLUME: 932 case SET_STREAM_VOLUME: 933 case REGISTER_POLICY_MIXES: 934 case SET_MASTER_MONO: 935 case START_AUDIO_SOURCE: 936 case STOP_AUDIO_SOURCE: 937 case GET_SURROUND_FORMATS: 938 case SET_SURROUND_FORMAT_ENABLED: { 939 if (multiuser_get_app_id(IPCThreadState::self()->getCallingUid()) >= AID_APP_START) { 940 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d", 941 __func__, code, IPCThreadState::self()->getCallingPid(), 942 IPCThreadState::self()->getCallingUid()); 943 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION)); 944 return OK; 945 } 946 } break; 947 default: 948 break; 949 } 950 951 TimeCheck check("IAudioPolicyService"); 952 953 switch (code) { 954 case SET_DEVICE_CONNECTION_STATE: { 955 CHECK_INTERFACE(IAudioPolicyService, data, reply); 956 audio_devices_t device = 957 static_cast <audio_devices_t>(data.readInt32()); 958 audio_policy_dev_state_t state = 959 static_cast <audio_policy_dev_state_t>(data.readInt32()); 960 const char *device_address = data.readCString(); 961 const char *device_name = data.readCString(); 962 if (device_address == nullptr || device_name == nullptr) { 963 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device); 964 reply->writeInt32(static_cast<int32_t> (BAD_VALUE)); 965 } else { 966 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device, 967 state, 968 device_address, 969 device_name))); 970 } 971 return NO_ERROR; 972 } break; 973 974 case GET_DEVICE_CONNECTION_STATE: { 975 CHECK_INTERFACE(IAudioPolicyService, data, reply); 976 audio_devices_t device = 977 static_cast<audio_devices_t> (data.readInt32()); 978 const char *device_address = data.readCString(); 979 if (device_address == nullptr) { 980 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device); 981 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); 982 } else { 983 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device, 984 device_address))); 985 } 986 return NO_ERROR; 987 } break; 988 989 case HANDLE_DEVICE_CONFIG_CHANGE: { 990 CHECK_INTERFACE(IAudioPolicyService, data, reply); 991 audio_devices_t device = 992 static_cast <audio_devices_t>(data.readInt32()); 993 const char *device_address = data.readCString(); 994 const char *device_name = data.readCString(); 995 if (device_address == nullptr || device_name == nullptr) { 996 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device); 997 reply->writeInt32(static_cast<int32_t> (BAD_VALUE)); 998 } else { 999 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device, 1000 device_address, 1001 device_name))); 1002 } 1003 return NO_ERROR; 1004 } break; 1005 1006 case SET_PHONE_STATE: { 1007 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1008 reply->writeInt32(static_cast <uint32_t>(setPhoneState( 1009 (audio_mode_t) data.readInt32()))); 1010 return NO_ERROR; 1011 } break; 1012 1013 case SET_FORCE_USE: { 1014 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1015 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>( 1016 data.readInt32()); 1017 audio_policy_forced_cfg_t config = 1018 static_cast <audio_policy_forced_cfg_t>(data.readInt32()); 1019 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config))); 1020 return NO_ERROR; 1021 } break; 1022 1023 case GET_FORCE_USE: { 1024 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1025 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>( 1026 data.readInt32()); 1027 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage))); 1028 return NO_ERROR; 1029 } break; 1030 1031 case GET_OUTPUT: { 1032 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1033 audio_stream_type_t stream = 1034 static_cast <audio_stream_type_t>(data.readInt32()); 1035 audio_io_handle_t output = getOutput(stream); 1036 reply->writeInt32(static_cast <int>(output)); 1037 return NO_ERROR; 1038 } break; 1039 1040 case GET_OUTPUT_FOR_ATTR: { 1041 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1042 audio_attributes_t attr = {}; 1043 bool hasAttributes = data.readInt32() != 0; 1044 if (hasAttributes) { 1045 data.read(&attr, sizeof(audio_attributes_t)); 1046 sanetizeAudioAttributes(&attr); 1047 } 1048 audio_session_t session = (audio_session_t)data.readInt32(); 1049 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; 1050 bool hasStream = data.readInt32() != 0; 1051 if (hasStream) { 1052 stream = (audio_stream_type_t)data.readInt32(); 1053 } 1054 pid_t pid = (pid_t)data.readInt32(); 1055 uid_t uid = (uid_t)data.readInt32(); 1056 audio_config_t config; 1057 memset(&config, 0, sizeof(audio_config_t)); 1058 data.read(&config, sizeof(audio_config_t)); 1059 audio_output_flags_t flags = 1060 static_cast <audio_output_flags_t>(data.readInt32()); 1061 audio_port_handle_t selectedDeviceId = data.readInt32(); 1062 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32(); 1063 audio_io_handle_t output = 0; 1064 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL, 1065 &output, session, &stream, pid, uid, 1066 &config, 1067 flags, &selectedDeviceId, &portId); 1068 reply->writeInt32(status); 1069 reply->writeInt32(output); 1070 reply->writeInt32(stream); 1071 reply->writeInt32(selectedDeviceId); 1072 reply->writeInt32(portId); 1073 return NO_ERROR; 1074 } break; 1075 1076 case START_OUTPUT: { 1077 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1078 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32()); 1079 audio_stream_type_t stream = 1080 static_cast <audio_stream_type_t>(data.readInt32()); 1081 audio_session_t session = (audio_session_t)data.readInt32(); 1082 reply->writeInt32(static_cast <uint32_t>(startOutput(output, 1083 stream, 1084 session))); 1085 return NO_ERROR; 1086 } break; 1087 1088 case STOP_OUTPUT: { 1089 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1090 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32()); 1091 audio_stream_type_t stream = 1092 static_cast <audio_stream_type_t>(data.readInt32()); 1093 audio_session_t session = (audio_session_t)data.readInt32(); 1094 reply->writeInt32(static_cast <uint32_t>(stopOutput(output, 1095 stream, 1096 session))); 1097 return NO_ERROR; 1098 } break; 1099 1100 case RELEASE_OUTPUT: { 1101 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1102 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32()); 1103 audio_stream_type_t stream = (audio_stream_type_t)data.readInt32(); 1104 audio_session_t session = (audio_session_t)data.readInt32(); 1105 releaseOutput(output, stream, session); 1106 return NO_ERROR; 1107 } break; 1108 1109 case GET_INPUT_FOR_ATTR: { 1110 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1111 audio_attributes_t attr = {}; 1112 data.read(&attr, sizeof(audio_attributes_t)); 1113 sanetizeAudioAttributes(&attr); 1114 audio_io_handle_t input = (audio_io_handle_t)data.readInt32(); 1115 audio_session_t session = (audio_session_t)data.readInt32(); 1116 pid_t pid = (pid_t)data.readInt32(); 1117 uid_t uid = (uid_t)data.readInt32(); 1118 const String16 opPackageName = data.readString16(); 1119 audio_config_base_t config; 1120 memset(&config, 0, sizeof(audio_config_base_t)); 1121 data.read(&config, sizeof(audio_config_base_t)); 1122 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32(); 1123 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32(); 1124 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32(); 1125 status_t status = getInputForAttr(&attr, &input, session, pid, uid, 1126 opPackageName, &config, 1127 flags, &selectedDeviceId, &portId); 1128 reply->writeInt32(status); 1129 if (status == NO_ERROR) { 1130 reply->writeInt32(input); 1131 reply->writeInt32(selectedDeviceId); 1132 reply->writeInt32(portId); 1133 } 1134 return NO_ERROR; 1135 } break; 1136 1137 case START_INPUT: { 1138 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1139 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32()); 1140 bool silenced = data.readInt32() == 1; 1141 status_t status = startInput(portId, &silenced); 1142 reply->writeInt32(static_cast <uint32_t>(status)); 1143 reply->writeInt32(silenced ? 1 : 0); 1144 return NO_ERROR; 1145 } break; 1146 1147 case STOP_INPUT: { 1148 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1149 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32()); 1150 reply->writeInt32(static_cast <uint32_t>(stopInput(portId))); 1151 return NO_ERROR; 1152 } break; 1153 1154 case RELEASE_INPUT: { 1155 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1156 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32()); 1157 releaseInput(portId); 1158 return NO_ERROR; 1159 } break; 1160 1161 case INIT_STREAM_VOLUME: { 1162 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1163 audio_stream_type_t stream = 1164 static_cast <audio_stream_type_t>(data.readInt32()); 1165 int indexMin = data.readInt32(); 1166 int indexMax = data.readInt32(); 1167 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax))); 1168 return NO_ERROR; 1169 } break; 1170 1171 case SET_STREAM_VOLUME: { 1172 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1173 audio_stream_type_t stream = 1174 static_cast <audio_stream_type_t>(data.readInt32()); 1175 int index = data.readInt32(); 1176 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32()); 1177 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream, 1178 index, 1179 device))); 1180 return NO_ERROR; 1181 } break; 1182 1183 case GET_STREAM_VOLUME: { 1184 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1185 audio_stream_type_t stream = 1186 static_cast <audio_stream_type_t>(data.readInt32()); 1187 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32()); 1188 int index = 0; 1189 status_t status = getStreamVolumeIndex(stream, &index, device); 1190 reply->writeInt32(index); 1191 reply->writeInt32(static_cast <uint32_t>(status)); 1192 return NO_ERROR; 1193 } break; 1194 1195 case GET_STRATEGY_FOR_STREAM: { 1196 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1197 audio_stream_type_t stream = 1198 static_cast <audio_stream_type_t>(data.readInt32()); 1199 reply->writeInt32(getStrategyForStream(stream)); 1200 return NO_ERROR; 1201 } break; 1202 1203 case GET_DEVICES_FOR_STREAM: { 1204 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1205 audio_stream_type_t stream = 1206 static_cast <audio_stream_type_t>(data.readInt32()); 1207 reply->writeInt32(static_cast <int>(getDevicesForStream(stream))); 1208 return NO_ERROR; 1209 } break; 1210 1211 case GET_OUTPUT_FOR_EFFECT: { 1212 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1213 effect_descriptor_t desc = {}; 1214 if (data.read(&desc, sizeof(desc)) != NO_ERROR) { 1215 android_errorWriteLog(0x534e4554, "73126106"); 1216 } 1217 (void)sanitizeEffectDescriptor(&desc); 1218 audio_io_handle_t output = getOutputForEffect(&desc); 1219 reply->writeInt32(static_cast <int>(output)); 1220 return NO_ERROR; 1221 } break; 1222 1223 case REGISTER_EFFECT: { 1224 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1225 effect_descriptor_t desc = {}; 1226 if (data.read(&desc, sizeof(desc)) != NO_ERROR) { 1227 android_errorWriteLog(0x534e4554, "73126106"); 1228 } 1229 (void)sanitizeEffectDescriptor(&desc); 1230 audio_io_handle_t io = data.readInt32(); 1231 uint32_t strategy = data.readInt32(); 1232 audio_session_t session = (audio_session_t) data.readInt32(); 1233 int id = data.readInt32(); 1234 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc, 1235 io, 1236 strategy, 1237 session, 1238 id))); 1239 return NO_ERROR; 1240 } break; 1241 1242 case UNREGISTER_EFFECT: { 1243 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1244 int id = data.readInt32(); 1245 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id))); 1246 return NO_ERROR; 1247 } break; 1248 1249 case SET_EFFECT_ENABLED: { 1250 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1251 int id = data.readInt32(); 1252 bool enabled = static_cast <bool>(data.readInt32()); 1253 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled))); 1254 return NO_ERROR; 1255 } break; 1256 1257 case IS_STREAM_ACTIVE: { 1258 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1259 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32(); 1260 uint32_t inPastMs = (uint32_t)data.readInt32(); 1261 reply->writeInt32( isStreamActive(stream, inPastMs) ); 1262 return NO_ERROR; 1263 } break; 1264 1265 case IS_STREAM_ACTIVE_REMOTELY: { 1266 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1267 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32(); 1268 uint32_t inPastMs = (uint32_t)data.readInt32(); 1269 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) ); 1270 return NO_ERROR; 1271 } break; 1272 1273 case IS_SOURCE_ACTIVE: { 1274 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1275 audio_source_t source = (audio_source_t) data.readInt32(); 1276 reply->writeInt32( isSourceActive(source)); 1277 return NO_ERROR; 1278 } 1279 1280 case QUERY_DEFAULT_PRE_PROCESSING: { 1281 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1282 audio_session_t audioSession = (audio_session_t) data.readInt32(); 1283 uint32_t count = data.readInt32(); 1284 if (count > AudioEffect::kMaxPreProcessing) { 1285 count = AudioEffect::kMaxPreProcessing; 1286 } 1287 uint32_t retCount = count; 1288 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{}; 1289 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount); 1290 reply->writeInt32(status); 1291 if (status != NO_ERROR && status != NO_MEMORY) { 1292 retCount = 0; 1293 } 1294 reply->writeInt32(retCount); 1295 if (retCount != 0) { 1296 if (retCount < count) { 1297 count = retCount; 1298 } 1299 reply->write(descriptors, sizeof(effect_descriptor_t) * count); 1300 } 1301 delete[] descriptors; 1302 return status; 1303 } 1304 1305 case IS_OFFLOAD_SUPPORTED: { 1306 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1307 audio_offload_info_t info = {}; 1308 data.read(&info, sizeof(audio_offload_info_t)); 1309 bool isSupported = isOffloadSupported(info); 1310 reply->writeInt32(isSupported); 1311 return NO_ERROR; 1312 } 1313 1314 case LIST_AUDIO_PORTS: { 1315 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1316 audio_port_role_t role = (audio_port_role_t)data.readInt32(); 1317 audio_port_type_t type = (audio_port_type_t)data.readInt32(); 1318 unsigned int numPortsReq = data.readInt32(); 1319 if (numPortsReq > MAX_ITEMS_PER_LIST) { 1320 numPortsReq = MAX_ITEMS_PER_LIST; 1321 } 1322 unsigned int numPorts = numPortsReq; 1323 struct audio_port *ports = 1324 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port)); 1325 if (ports == NULL) { 1326 reply->writeInt32(NO_MEMORY); 1327 reply->writeInt32(0); 1328 return NO_ERROR; 1329 } 1330 unsigned int generation; 1331 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation); 1332 reply->writeInt32(status); 1333 reply->writeInt32(numPorts); 1334 1335 if (status == NO_ERROR) { 1336 if (numPortsReq > numPorts) { 1337 numPortsReq = numPorts; 1338 } 1339 reply->write(ports, numPortsReq * sizeof(struct audio_port)); 1340 reply->writeInt32(generation); 1341 } 1342 free(ports); 1343 return NO_ERROR; 1344 } 1345 1346 case GET_AUDIO_PORT: { 1347 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1348 struct audio_port port = {}; 1349 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) { 1350 ALOGE("b/23912202"); 1351 } 1352 status_t status = getAudioPort(&port); 1353 reply->writeInt32(status); 1354 if (status == NO_ERROR) { 1355 reply->write(&port, sizeof(struct audio_port)); 1356 } 1357 return NO_ERROR; 1358 } 1359 1360 case CREATE_AUDIO_PATCH: { 1361 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1362 struct audio_patch patch = {}; 1363 data.read(&patch, sizeof(struct audio_patch)); 1364 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; 1365 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) { 1366 ALOGE("b/23912202"); 1367 } 1368 status_t status = createAudioPatch(&patch, &handle); 1369 reply->writeInt32(status); 1370 if (status == NO_ERROR) { 1371 reply->write(&handle, sizeof(audio_patch_handle_t)); 1372 } 1373 return NO_ERROR; 1374 } 1375 1376 case RELEASE_AUDIO_PATCH: { 1377 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1378 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; 1379 data.read(&handle, sizeof(audio_patch_handle_t)); 1380 status_t status = releaseAudioPatch(handle); 1381 reply->writeInt32(status); 1382 return NO_ERROR; 1383 } 1384 1385 case LIST_AUDIO_PATCHES: { 1386 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1387 unsigned int numPatchesReq = data.readInt32(); 1388 if (numPatchesReq > MAX_ITEMS_PER_LIST) { 1389 numPatchesReq = MAX_ITEMS_PER_LIST; 1390 } 1391 unsigned int numPatches = numPatchesReq; 1392 struct audio_patch *patches = 1393 (struct audio_patch *)calloc(numPatchesReq, 1394 sizeof(struct audio_patch)); 1395 if (patches == NULL) { 1396 reply->writeInt32(NO_MEMORY); 1397 reply->writeInt32(0); 1398 return NO_ERROR; 1399 } 1400 unsigned int generation; 1401 status_t status = listAudioPatches(&numPatches, patches, &generation); 1402 reply->writeInt32(status); 1403 reply->writeInt32(numPatches); 1404 if (status == NO_ERROR) { 1405 if (numPatchesReq > numPatches) { 1406 numPatchesReq = numPatches; 1407 } 1408 reply->write(patches, numPatchesReq * sizeof(struct audio_patch)); 1409 reply->writeInt32(generation); 1410 } 1411 free(patches); 1412 return NO_ERROR; 1413 } 1414 1415 case SET_AUDIO_PORT_CONFIG: { 1416 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1417 struct audio_port_config config = {}; 1418 data.read(&config, sizeof(struct audio_port_config)); 1419 (void)sanitizeAudioPortConfig(&config); 1420 status_t status = setAudioPortConfig(&config); 1421 reply->writeInt32(status); 1422 return NO_ERROR; 1423 } 1424 1425 case REGISTER_CLIENT: { 1426 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1427 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>( 1428 data.readStrongBinder()); 1429 registerClient(client); 1430 return NO_ERROR; 1431 } break; 1432 1433 case SET_AUDIO_PORT_CALLBACK_ENABLED: { 1434 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1435 setAudioPortCallbacksEnabled(data.readInt32() == 1); 1436 return NO_ERROR; 1437 } break; 1438 1439 case ACQUIRE_SOUNDTRIGGER_SESSION: { 1440 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1441 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>( 1442 data.readStrongBinder()); 1443 audio_session_t session = AUDIO_SESSION_NONE; 1444 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE; 1445 audio_devices_t device = AUDIO_DEVICE_NONE; 1446 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device); 1447 reply->writeInt32(status); 1448 if (status == NO_ERROR) { 1449 reply->writeInt32(session); 1450 reply->writeInt32(ioHandle); 1451 reply->writeInt32(device); 1452 } 1453 return NO_ERROR; 1454 } break; 1455 1456 case RELEASE_SOUNDTRIGGER_SESSION: { 1457 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1458 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>( 1459 data.readStrongBinder()); 1460 audio_session_t session = (audio_session_t)data.readInt32(); 1461 status_t status = releaseSoundTriggerSession(session); 1462 reply->writeInt32(status); 1463 return NO_ERROR; 1464 } break; 1465 1466 case GET_PHONE_STATE: { 1467 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1468 reply->writeInt32((int32_t)getPhoneState()); 1469 return NO_ERROR; 1470 } break; 1471 1472 case REGISTER_POLICY_MIXES: { 1473 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1474 bool registration = data.readInt32() == 1; 1475 Vector<AudioMix> mixes; 1476 size_t size = (size_t)data.readInt32(); 1477 if (size > MAX_MIXES_PER_POLICY) { 1478 size = MAX_MIXES_PER_POLICY; 1479 } 1480 for (size_t i = 0; i < size; i++) { 1481 AudioMix mix; 1482 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) { 1483 mixes.add(mix); 1484 } 1485 } 1486 status_t status = registerPolicyMixes(mixes, registration); 1487 reply->writeInt32(status); 1488 return NO_ERROR; 1489 } break; 1490 1491 case START_AUDIO_SOURCE: { 1492 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1493 struct audio_port_config source = {}; 1494 data.read(&source, sizeof(struct audio_port_config)); 1495 (void)sanitizeAudioPortConfig(&source); 1496 audio_attributes_t attributes = {}; 1497 data.read(&attributes, sizeof(audio_attributes_t)); 1498 sanetizeAudioAttributes(&attributes); 1499 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; 1500 status_t status = startAudioSource(&source, &attributes, &handle); 1501 reply->writeInt32(status); 1502 reply->writeInt32(handle); 1503 return NO_ERROR; 1504 } break; 1505 1506 case STOP_AUDIO_SOURCE: { 1507 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1508 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32(); 1509 status_t status = stopAudioSource(handle); 1510 reply->writeInt32(status); 1511 return NO_ERROR; 1512 } break; 1513 1514 case SET_MASTER_MONO: { 1515 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1516 bool mono = static_cast<bool>(data.readInt32()); 1517 status_t status = setMasterMono(mono); 1518 reply->writeInt32(status); 1519 return NO_ERROR; 1520 } break; 1521 1522 case GET_MASTER_MONO: { 1523 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1524 bool mono; 1525 status_t status = getMasterMono(&mono); 1526 reply->writeInt32(status); 1527 if (status == NO_ERROR) { 1528 reply->writeInt32(static_cast<int32_t>(mono)); 1529 } 1530 return NO_ERROR; 1531 } break; 1532 1533 case GET_STREAM_VOLUME_DB: { 1534 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1535 audio_stream_type_t stream = 1536 static_cast <audio_stream_type_t>(data.readInt32()); 1537 int index = static_cast <int>(data.readInt32()); 1538 audio_devices_t device = 1539 static_cast <audio_devices_t>(data.readUint32()); 1540 reply->writeFloat(getStreamVolumeDB(stream, index, device)); 1541 return NO_ERROR; 1542 } 1543 1544 case GET_SURROUND_FORMATS: { 1545 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1546 unsigned int numSurroundFormatsReq = data.readUint32(); 1547 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) { 1548 numSurroundFormatsReq = MAX_ITEMS_PER_LIST; 1549 } 1550 bool reported = data.readBool(); 1551 unsigned int numSurroundFormats = numSurroundFormatsReq; 1552 audio_format_t *surroundFormats = (audio_format_t *)calloc( 1553 numSurroundFormats, sizeof(audio_format_t)); 1554 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool)); 1555 if (numSurroundFormatsReq > 0 && 1556 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) { 1557 free(surroundFormats); 1558 free(surroundFormatsEnabled); 1559 reply->writeInt32(NO_MEMORY); 1560 return NO_ERROR; 1561 } 1562 status_t status = getSurroundFormats( 1563 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); 1564 reply->writeInt32(status); 1565 1566 if (status == NO_ERROR) { 1567 reply->writeUint32(numSurroundFormats); 1568 if (numSurroundFormatsReq > numSurroundFormats) { 1569 numSurroundFormatsReq = numSurroundFormats; 1570 } 1571 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t)); 1572 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool)); 1573 } 1574 free(surroundFormats); 1575 free(surroundFormatsEnabled); 1576 return NO_ERROR; 1577 } 1578 1579 case SET_SURROUND_FORMAT_ENABLED: { 1580 CHECK_INTERFACE(IAudioPolicyService, data, reply); 1581 audio_format_t audioFormat = (audio_format_t) data.readInt32(); 1582 bool enabled = data.readBool(); 1583 status_t status = setSurroundFormatEnabled(audioFormat, enabled); 1584 reply->writeInt32(status); 1585 return NO_ERROR; 1586 } 1587 1588 default: 1589 return BBinder::onTransact(code, data, reply, flags); 1590 } 1591 } 1592 1593 /** returns true if string overflow was prevented by zero termination */ 1594 template <size_t size> 1595 static bool preventStringOverflow(char (&s)[size]) { 1596 if (strnlen(s, size) < size) return false; 1597 s[size - 1] = '\0'; 1598 return true; 1599 } 1600 1601 void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr) 1602 { 1603 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE; 1604 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) { 1605 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging 1606 } 1607 attr->tags[tagsMaxSize - 1] = '\0'; 1608 } 1609 1610 /** returns BAD_VALUE if sanitization was required. */ 1611 status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc) 1612 { 1613 if (preventStringOverflow(desc->name) 1614 | /* always */ preventStringOverflow(desc->implementor)) { 1615 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging 1616 return BAD_VALUE; 1617 } 1618 return NO_ERROR; 1619 } 1620 1621 /** returns BAD_VALUE if sanitization was required. */ 1622 status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config) 1623 { 1624 if (config->type == AUDIO_PORT_TYPE_DEVICE && 1625 preventStringOverflow(config->ext.device.address)) { 1626 return BAD_VALUE; 1627 } 1628 return NO_ERROR; 1629 } 1630 1631 // ---------------------------------------------------------------------------- 1632 1633 } // namespace android 1634