1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_IAUDIOFLINGER_H 18 #define ANDROID_IAUDIOFLINGER_H 19 20 #include <stdint.h> 21 #include <sys/types.h> 22 #include <unistd.h> 23 24 #include <utils/RefBase.h> 25 #include <utils/Errors.h> 26 #include <binder/IInterface.h> 27 #include <binder/Parcel.h> 28 #include <binder/Parcelable.h> 29 #include <media/AudioClient.h> 30 #include <media/DeviceDescriptorBase.h> 31 #include <media/IAudioTrack.h> 32 #include <media/IAudioFlingerClient.h> 33 #include <system/audio.h> 34 #include <system/audio_effect.h> 35 #include <system/audio_policy.h> 36 #include <media/IEffect.h> 37 #include <media/IEffectClient.h> 38 #include <utils/String8.h> 39 #include <media/MicrophoneInfo.h> 40 #include <vector> 41 42 #include "android/media/IAudioRecord.h" 43 #include "android/media/IAudioTrackCallback.h" 44 45 namespace android { 46 47 // ---------------------------------------------------------------------------- 48 49 class IAudioFlinger : public IInterface 50 { 51 public: 52 DECLARE_META_INTERFACE(AudioFlinger); 53 54 /* CreateTrackInput contains all input arguments sent by AudioTrack to AudioFlinger 55 * when calling createTrack() including arguments that will be updated by AudioFlinger 56 * and returned in CreateTrackOutput object 57 */ 58 class CreateTrackInput : public Parcelable { 59 public: readFromParcel(const Parcel * parcel)60 status_t readFromParcel(const Parcel *parcel) override { 61 /* input arguments*/ 62 memset(&attr, 0, sizeof(audio_attributes_t)); 63 if (parcel->read(&attr, sizeof(audio_attributes_t)) != NO_ERROR) { 64 return DEAD_OBJECT; 65 } 66 attr.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE -1] = '\0'; 67 memset(&config, 0, sizeof(audio_config_t)); 68 if (parcel->read(&config, sizeof(audio_config_t)) != NO_ERROR) { 69 return DEAD_OBJECT; 70 } 71 if (clientInfo.readFromParcel(parcel) != NO_ERROR) { 72 return DEAD_OBJECT; 73 } 74 if (parcel->readInt32() != 0) { 75 // TODO: Using unsecurePointer() has some associated security 76 // pitfalls (see declaration for details). 77 // Either document why it is safe in this case or address 78 // the issue (e.g. by copying). 79 sharedBuffer = interface_cast<IMemory>(parcel->readStrongBinder()); 80 if (sharedBuffer == 0 || sharedBuffer->unsecurePointer() == NULL) { 81 return BAD_VALUE; 82 } 83 } 84 notificationsPerBuffer = parcel->readInt32(); 85 speed = parcel->readFloat(); 86 audioTrackCallback = interface_cast<media::IAudioTrackCallback>( 87 parcel->readStrongBinder()); 88 89 /* input/output arguments*/ 90 (void)parcel->read(&flags, sizeof(audio_output_flags_t)); 91 frameCount = parcel->readInt64(); 92 notificationFrameCount = parcel->readInt64(); 93 (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t)); 94 (void)parcel->read(&sessionId, sizeof(audio_session_t)); 95 return NO_ERROR; 96 } 97 writeToParcel(Parcel * parcel)98 status_t writeToParcel(Parcel *parcel) const override { 99 /* input arguments*/ 100 (void)parcel->write(&attr, sizeof(audio_attributes_t)); 101 (void)parcel->write(&config, sizeof(audio_config_t)); 102 (void)clientInfo.writeToParcel(parcel); 103 if (sharedBuffer != 0) { 104 (void)parcel->writeInt32(1); 105 (void)parcel->writeStrongBinder(IInterface::asBinder(sharedBuffer)); 106 } else { 107 (void)parcel->writeInt32(0); 108 } 109 (void)parcel->writeInt32(notificationsPerBuffer); 110 (void)parcel->writeFloat(speed); 111 (void)parcel->writeStrongBinder(IInterface::asBinder(audioTrackCallback)); 112 113 /* input/output arguments*/ 114 (void)parcel->write(&flags, sizeof(audio_output_flags_t)); 115 (void)parcel->writeInt64(frameCount); 116 (void)parcel->writeInt64(notificationFrameCount); 117 (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t)); 118 (void)parcel->write(&sessionId, sizeof(audio_session_t)); 119 return NO_ERROR; 120 } 121 122 /* input */ 123 audio_attributes_t attr; 124 audio_config_t config; 125 AudioClient clientInfo; 126 sp<IMemory> sharedBuffer; 127 uint32_t notificationsPerBuffer; 128 float speed; 129 sp<media::IAudioTrackCallback> audioTrackCallback; 130 131 /* input/output */ 132 audio_output_flags_t flags; 133 size_t frameCount; 134 size_t notificationFrameCount; 135 audio_port_handle_t selectedDeviceId; 136 audio_session_t sessionId; 137 }; 138 139 /* CreateTrackOutput contains all output arguments returned by AudioFlinger to AudioTrack 140 * when calling createTrack() including arguments that were passed as I/O for update by 141 * CreateTrackInput. 142 */ 143 class CreateTrackOutput : public Parcelable { 144 public: readFromParcel(const Parcel * parcel)145 status_t readFromParcel(const Parcel *parcel) override { 146 /* input/output arguments*/ 147 (void)parcel->read(&flags, sizeof(audio_output_flags_t)); 148 frameCount = parcel->readInt64(); 149 notificationFrameCount = parcel->readInt64(); 150 (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t)); 151 (void)parcel->read(&sessionId, sizeof(audio_session_t)); 152 153 /* output arguments*/ 154 sampleRate = parcel->readUint32(); 155 afFrameCount = parcel->readInt64(); 156 afSampleRate = parcel->readInt64(); 157 afLatencyMs = parcel->readInt32(); 158 (void)parcel->read(&outputId, sizeof(audio_io_handle_t)); 159 (void)parcel->read(&portId, sizeof(audio_port_handle_t)); 160 return NO_ERROR; 161 } 162 writeToParcel(Parcel * parcel)163 status_t writeToParcel(Parcel *parcel) const override { 164 /* input/output arguments*/ 165 (void)parcel->write(&flags, sizeof(audio_output_flags_t)); 166 (void)parcel->writeInt64(frameCount); 167 (void)parcel->writeInt64(notificationFrameCount); 168 (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t)); 169 (void)parcel->write(&sessionId, sizeof(audio_session_t)); 170 171 /* output arguments*/ 172 (void)parcel->writeUint32(sampleRate); 173 (void)parcel->writeInt64(afFrameCount); 174 (void)parcel->writeInt64(afSampleRate); 175 (void)parcel->writeInt32(afLatencyMs); 176 (void)parcel->write(&outputId, sizeof(audio_io_handle_t)); 177 (void)parcel->write(&portId, sizeof(audio_port_handle_t)); 178 return NO_ERROR; 179 } 180 181 /* input/output */ 182 audio_output_flags_t flags; 183 size_t frameCount; 184 size_t notificationFrameCount; 185 audio_port_handle_t selectedDeviceId; 186 audio_session_t sessionId; 187 188 /* output */ 189 uint32_t sampleRate; 190 size_t afFrameCount; 191 uint32_t afSampleRate; 192 uint32_t afLatencyMs; 193 audio_io_handle_t outputId; 194 audio_port_handle_t portId; 195 }; 196 197 /* CreateRecordInput contains all input arguments sent by AudioRecord to AudioFlinger 198 * when calling createRecord() including arguments that will be updated by AudioFlinger 199 * and returned in CreateRecordOutput object 200 */ 201 class CreateRecordInput : public Parcelable { 202 public: readFromParcel(const Parcel * parcel)203 status_t readFromParcel(const Parcel *parcel) override { 204 /* input arguments*/ 205 memset(&attr, 0, sizeof(audio_attributes_t)); 206 if (parcel->read(&attr, sizeof(audio_attributes_t)) != NO_ERROR) { 207 return DEAD_OBJECT; 208 } 209 attr.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE -1] = '\0'; 210 memset(&config, 0, sizeof(audio_config_base_t)); 211 if (parcel->read(&config, sizeof(audio_config_base_t)) != NO_ERROR) { 212 return DEAD_OBJECT; 213 } 214 if (clientInfo.readFromParcel(parcel) != NO_ERROR) { 215 return DEAD_OBJECT; 216 } 217 opPackageName = parcel->readString16(); 218 if (parcel->read(&riid, sizeof(audio_unique_id_t)) != NO_ERROR) { 219 return DEAD_OBJECT; 220 } 221 222 /* input/output arguments*/ 223 (void)parcel->read(&flags, sizeof(audio_input_flags_t)); 224 frameCount = parcel->readInt64(); 225 notificationFrameCount = parcel->readInt64(); 226 (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t)); 227 (void)parcel->read(&sessionId, sizeof(audio_session_t)); 228 return NO_ERROR; 229 } 230 writeToParcel(Parcel * parcel)231 status_t writeToParcel(Parcel *parcel) const override { 232 /* input arguments*/ 233 (void)parcel->write(&attr, sizeof(audio_attributes_t)); 234 (void)parcel->write(&config, sizeof(audio_config_base_t)); 235 (void)clientInfo.writeToParcel(parcel); 236 (void)parcel->writeString16(opPackageName); 237 (void)parcel->write(&riid, sizeof(audio_unique_id_t)); 238 239 /* input/output arguments*/ 240 (void)parcel->write(&flags, sizeof(audio_input_flags_t)); 241 (void)parcel->writeInt64(frameCount); 242 (void)parcel->writeInt64(notificationFrameCount); 243 (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t)); 244 (void)parcel->write(&sessionId, sizeof(audio_session_t)); 245 return NO_ERROR; 246 } 247 248 /* input */ 249 audio_attributes_t attr; 250 audio_config_base_t config; 251 AudioClient clientInfo; 252 String16 opPackageName; 253 audio_unique_id_t riid; 254 255 /* input/output */ 256 audio_input_flags_t flags; 257 size_t frameCount; 258 size_t notificationFrameCount; 259 audio_port_handle_t selectedDeviceId; 260 audio_session_t sessionId; 261 }; 262 263 /* CreateRecordOutput contains all output arguments returned by AudioFlinger to AudioRecord 264 * when calling createRecord() including arguments that were passed as I/O for update by 265 * CreateRecordInput. 266 */ 267 class CreateRecordOutput : public Parcelable { 268 public: readFromParcel(const Parcel * parcel)269 status_t readFromParcel(const Parcel *parcel) override { 270 /* input/output arguments*/ 271 (void)parcel->read(&flags, sizeof(audio_input_flags_t)); 272 frameCount = parcel->readInt64(); 273 notificationFrameCount = parcel->readInt64(); 274 (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t)); 275 (void)parcel->read(&sessionId, sizeof(audio_session_t)); 276 277 /* output arguments*/ 278 sampleRate = parcel->readUint32(); 279 (void)parcel->read(&inputId, sizeof(audio_io_handle_t)); 280 if (parcel->readInt32() != 0) { 281 cblk = interface_cast<IMemory>(parcel->readStrongBinder()); 282 // TODO: Using unsecurePointer() has some associated security 283 // pitfalls (see declaration for details). 284 // Either document why it is safe in this case or address 285 // the issue (e.g. by copying). 286 if (cblk == 0 || cblk->unsecurePointer() == NULL) { 287 return BAD_VALUE; 288 } 289 } 290 if (parcel->readInt32() != 0) { 291 buffers = interface_cast<IMemory>(parcel->readStrongBinder()); 292 // TODO: Using unsecurePointer() has some associated security 293 // pitfalls (see declaration for details). 294 // Either document why it is safe in this case or address 295 // the issue (e.g. by copying). 296 if (buffers == 0 || buffers->unsecurePointer() == NULL) { 297 return BAD_VALUE; 298 } 299 } 300 (void)parcel->read(&portId, sizeof(audio_port_handle_t)); 301 return NO_ERROR; 302 } 303 writeToParcel(Parcel * parcel)304 status_t writeToParcel(Parcel *parcel) const override { 305 /* input/output arguments*/ 306 (void)parcel->write(&flags, sizeof(audio_input_flags_t)); 307 (void)parcel->writeInt64(frameCount); 308 (void)parcel->writeInt64(notificationFrameCount); 309 (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t)); 310 (void)parcel->write(&sessionId, sizeof(audio_session_t)); 311 312 /* output arguments*/ 313 (void)parcel->writeUint32(sampleRate); 314 (void)parcel->write(&inputId, sizeof(audio_io_handle_t)); 315 if (cblk != 0) { 316 (void)parcel->writeInt32(1); 317 (void)parcel->writeStrongBinder(IInterface::asBinder(cblk)); 318 } else { 319 (void)parcel->writeInt32(0); 320 } 321 if (buffers != 0) { 322 (void)parcel->writeInt32(1); 323 (void)parcel->writeStrongBinder(IInterface::asBinder(buffers)); 324 } else { 325 (void)parcel->writeInt32(0); 326 } 327 (void)parcel->write(&portId, sizeof(audio_port_handle_t)); 328 329 return NO_ERROR; 330 } 331 332 /* input/output */ 333 audio_input_flags_t flags; 334 size_t frameCount; 335 size_t notificationFrameCount; 336 audio_port_handle_t selectedDeviceId; 337 audio_session_t sessionId; 338 339 /* output */ 340 uint32_t sampleRate; 341 audio_io_handle_t inputId; 342 sp<IMemory> cblk; 343 sp<IMemory> buffers; 344 audio_port_handle_t portId; 345 }; 346 347 // invariant on exit for all APIs that return an sp<>: 348 // (return value != 0) == (*status == NO_ERROR) 349 350 /* create an audio track and registers it with AudioFlinger. 351 * return null if the track cannot be created. 352 */ 353 virtual sp<IAudioTrack> createTrack(const CreateTrackInput& input, 354 CreateTrackOutput& output, 355 status_t *status) = 0; 356 357 virtual sp<media::IAudioRecord> createRecord(const CreateRecordInput& input, 358 CreateRecordOutput& output, 359 status_t *status) = 0; 360 361 // FIXME Surprisingly, format/latency don't work for input handles 362 363 /* query the audio hardware state. This state never changes, 364 * and therefore can be cached. 365 */ 366 virtual uint32_t sampleRate(audio_io_handle_t ioHandle) const = 0; 367 368 // reserved; formerly channelCount() 369 370 virtual audio_format_t format(audio_io_handle_t output) const = 0; 371 virtual size_t frameCount(audio_io_handle_t ioHandle) const = 0; 372 373 // return estimated latency in milliseconds 374 virtual uint32_t latency(audio_io_handle_t output) const = 0; 375 376 /* set/get the audio hardware state. This will probably be used by 377 * the preference panel, mostly. 378 */ 379 virtual status_t setMasterVolume(float value) = 0; 380 virtual status_t setMasterMute(bool muted) = 0; 381 382 virtual float masterVolume() const = 0; 383 virtual bool masterMute() const = 0; 384 385 virtual status_t setMasterBalance(float balance) = 0; 386 virtual status_t getMasterBalance(float *balance) const = 0; 387 388 /* set/get stream type state. This will probably be used by 389 * the preference panel, mostly. 390 */ 391 virtual status_t setStreamVolume(audio_stream_type_t stream, float value, 392 audio_io_handle_t output) = 0; 393 virtual status_t setStreamMute(audio_stream_type_t stream, bool muted) = 0; 394 395 virtual float streamVolume(audio_stream_type_t stream, 396 audio_io_handle_t output) const = 0; 397 virtual bool streamMute(audio_stream_type_t stream) const = 0; 398 399 // set audio mode 400 virtual status_t setMode(audio_mode_t mode) = 0; 401 402 // mic mute/state 403 virtual status_t setMicMute(bool state) = 0; 404 virtual bool getMicMute() const = 0; 405 virtual void setRecordSilenced(audio_port_handle_t portId, bool silenced) = 0; 406 407 virtual status_t setParameters(audio_io_handle_t ioHandle, 408 const String8& keyValuePairs) = 0; 409 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) 410 const = 0; 411 412 // Register an object to receive audio input/output change and track notifications. 413 // For a given calling pid, AudioFlinger disregards any registrations after the first. 414 // Thus the IAudioFlingerClient must be a singleton per process. 415 virtual void registerClient(const sp<IAudioFlingerClient>& client) = 0; 416 417 // retrieve the audio recording buffer size in bytes 418 // FIXME This API assumes a route, and so should be deprecated. 419 virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format, 420 audio_channel_mask_t channelMask) const = 0; 421 422 virtual status_t openOutput(audio_module_handle_t module, 423 audio_io_handle_t *output, 424 audio_config_t *config, 425 const sp<DeviceDescriptorBase>& device, 426 uint32_t *latencyMs, 427 audio_output_flags_t flags) = 0; 428 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, 429 audio_io_handle_t output2) = 0; 430 virtual status_t closeOutput(audio_io_handle_t output) = 0; 431 virtual status_t suspendOutput(audio_io_handle_t output) = 0; 432 virtual status_t restoreOutput(audio_io_handle_t output) = 0; 433 434 virtual status_t openInput(audio_module_handle_t module, 435 audio_io_handle_t *input, 436 audio_config_t *config, 437 audio_devices_t *device, 438 const String8& address, 439 audio_source_t source, 440 audio_input_flags_t flags) = 0; 441 virtual status_t closeInput(audio_io_handle_t input) = 0; 442 443 virtual status_t invalidateStream(audio_stream_type_t stream) = 0; 444 445 virtual status_t setVoiceVolume(float volume) = 0; 446 447 virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, 448 audio_io_handle_t output) const = 0; 449 450 virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const = 0; 451 452 virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) = 0; 453 454 virtual void acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) = 0; 455 virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid) = 0; 456 457 virtual status_t queryNumberEffects(uint32_t *numEffects) const = 0; 458 459 virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const = 0; 460 461 virtual status_t getEffectDescriptor(const effect_uuid_t *pEffectUUID, 462 const effect_uuid_t *pTypeUUID, 463 uint32_t preferredTypeFlag, 464 effect_descriptor_t *pDescriptor) const = 0; 465 466 virtual sp<IEffect> createEffect( 467 effect_descriptor_t *pDesc, 468 const sp<IEffectClient>& client, 469 int32_t priority, 470 // AudioFlinger doesn't take over handle reference from client 471 audio_io_handle_t output, 472 audio_session_t sessionId, 473 const AudioDeviceTypeAddr& device, 474 const String16& callingPackage, 475 pid_t pid, 476 bool probe, 477 status_t *status, 478 int *id, 479 int *enabled) = 0; 480 481 virtual status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput, 482 audio_io_handle_t dstOutput) = 0; 483 484 virtual void setEffectSuspended(int effectId, 485 audio_session_t sessionId, 486 bool suspended) = 0; 487 488 virtual audio_module_handle_t loadHwModule(const char *name) = 0; 489 490 // helpers for android.media.AudioManager.getProperty(), see description there for meaning 491 // FIXME move these APIs to AudioPolicy to permit a more accurate implementation 492 // that looks on primary device for a stream with fast flag, primary flag, or first one. 493 virtual uint32_t getPrimaryOutputSamplingRate() = 0; 494 virtual size_t getPrimaryOutputFrameCount() = 0; 495 496 // Intended for AudioService to inform AudioFlinger of device's low RAM attribute, 497 // and should be called at most once. For a definition of what "low RAM" means, see 498 // android.app.ActivityManager.isLowRamDevice(). The totalMemory parameter 499 // is obtained from android.app.ActivityManager.MemoryInfo.totalMem. 500 virtual status_t setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) = 0; 501 502 /* List available audio ports and their attributes */ 503 virtual status_t listAudioPorts(unsigned int *num_ports, 504 struct audio_port *ports) = 0; 505 506 /* Get attributes for a given audio port */ 507 virtual status_t getAudioPort(struct audio_port *port) = 0; 508 509 /* Create an audio patch between several source and sink ports */ 510 virtual status_t createAudioPatch(const struct audio_patch *patch, 511 audio_patch_handle_t *handle) = 0; 512 513 /* Release an audio patch */ 514 virtual status_t releaseAudioPatch(audio_patch_handle_t handle) = 0; 515 516 /* List existing audio patches */ 517 virtual status_t listAudioPatches(unsigned int *num_patches, 518 struct audio_patch *patches) = 0; 519 /* Set audio port configuration */ 520 virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0; 521 522 /* Get the HW synchronization source used for an audio session */ 523 virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId) = 0; 524 525 /* Indicate JAVA services are ready (scheduling, power management ...) */ 526 virtual status_t systemReady() = 0; 527 528 // Returns the number of frames per audio HAL buffer. 529 virtual size_t frameCountHAL(audio_io_handle_t ioHandle) const = 0; 530 531 /* List available microphones and their characteristics */ 532 virtual status_t getMicrophones(std::vector<media::MicrophoneInfo> *microphones) = 0; 533 534 virtual status_t setAudioHalPids(const std::vector<pid_t>& pids) = 0; 535 }; 536 537 538 // ---------------------------------------------------------------------------- 539 540 class BnAudioFlinger : public BnInterface<IAudioFlinger> 541 { 542 public: 543 virtual status_t onTransact( uint32_t code, 544 const Parcel& data, 545 Parcel* reply, 546 uint32_t flags = 0); 547 548 // Requests media.log to start merging log buffers 549 virtual void requestLogMerge() = 0; 550 }; 551 552 // ---------------------------------------------------------------------------- 553 554 }; // namespace android 555 556 #endif // ANDROID_IAUDIOFLINGER_H 557