1 /* 2 * Copyright 2016 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 AAUDIO_AUDIOSTREAM_H 18 #define AAUDIO_AUDIOSTREAM_H 19 20 #include <atomic> 21 #include <mutex> 22 #include <stdint.h> 23 24 #include <android-base/thread_annotations.h> 25 #include <binder/IServiceManager.h> 26 #include <binder/Status.h> 27 #include <utils/StrongPointer.h> 28 29 #include <aaudio/AAudio.h> 30 #include <media/AudioSystem.h> 31 #include <media/PlayerBase.h> 32 #include <media/VolumeShaper.h> 33 34 #include "utility/AAudioUtilities.h" 35 #include "utility/MonotonicCounter.h" 36 37 // Cannot get android::media::VolumeShaper to compile! 38 #define AAUDIO_USE_VOLUME_SHAPER 0 39 40 namespace aaudio { 41 42 typedef void *(*aaudio_audio_thread_proc_t)(void *); 43 typedef uint32_t aaudio_stream_id_t; 44 45 class AudioStreamBuilder; 46 47 constexpr pid_t CALLBACK_THREAD_NONE = 0; 48 49 /** 50 * AAudio audio stream. 51 */ 52 // By extending AudioDeviceCallback, we also inherit from RefBase. 53 class AudioStream : public android::AudioSystem::AudioDeviceCallback { 54 public: 55 56 AudioStream(); 57 58 virtual ~AudioStream(); 59 60 protected: 61 62 /** 63 * Check the state to see if Pause is currently legal. 64 * 65 * @param result pointer to return code 66 * @return true if OK to continue, if false then return result 67 */ 68 bool checkPauseStateTransition(aaudio_result_t *result); 69 isFlushSupported()70 virtual bool isFlushSupported() const { 71 // Only implement FLUSH for OUTPUT streams. 72 return false; 73 } 74 isPauseSupported()75 virtual bool isPauseSupported() const { 76 // Only implement PAUSE for OUTPUT streams. 77 return false; 78 } 79 80 /* Asynchronous requests. 81 * Use waitForStateChange() to wait for completion. 82 */ 83 virtual aaudio_result_t requestStart_l() REQUIRES(mStreamLock) = 0; 84 requestPause_l()85 virtual aaudio_result_t requestPause_l() REQUIRES(mStreamLock) { 86 // Only implement this for OUTPUT streams. 87 return AAUDIO_ERROR_UNIMPLEMENTED; 88 } 89 requestFlush_l()90 virtual aaudio_result_t requestFlush_l() REQUIRES(mStreamLock) { 91 // Only implement this for OUTPUT streams. 92 return AAUDIO_ERROR_UNIMPLEMENTED; 93 } 94 95 virtual aaudio_result_t requestStop_l() REQUIRES(mStreamLock) = 0; 96 97 public: 98 virtual aaudio_result_t getTimestamp(clockid_t clockId, 99 int64_t *framePosition, 100 int64_t *timeNanoseconds) = 0; 101 102 /** 103 * Update state machine. 104 * @return result of the operation. 105 */ updateStateMachine()106 aaudio_result_t updateStateMachine() { 107 if (isDataCallbackActive()) { 108 return AAUDIO_OK; // state is getting updated by the callback thread read/write call 109 } 110 return processCommands(); 111 }; 112 113 virtual aaudio_result_t processCommands() = 0; 114 115 // =========== End ABSTRACT methods =========================== 116 117 virtual aaudio_result_t waitForStateChange(aaudio_stream_state_t currentState, 118 aaudio_stream_state_t *nextState, 119 int64_t timeoutNanoseconds); 120 121 /** 122 * Open the stream using the parameters in the builder. 123 * Allocate the necessary resources. 124 */ 125 virtual aaudio_result_t open(const AudioStreamBuilder& builder); 126 127 // log to MediaMetrics 128 virtual void logOpenActual(); 129 void logReleaseBufferState(); 130 131 /* Note about naming for "release" and "close" related methods. 132 * 133 * These names are intended to match the public AAudio API. 134 * The original AAudio API had an AAudioStream_close() function that 135 * released the hardware and deleted the stream. That made it difficult 136 * because apps want to release the HW ASAP but are not in a rush to delete 137 * the stream object. So in R we added an AAudioStream_release() function 138 * that just released the hardware. 139 * The AAudioStream_close() method releases if needed and then closes. 140 */ 141 142 protected: 143 /** 144 * Free any hardware or system resources from the open() call. 145 * It is safe to call release_l() multiple times. 146 */ release_l()147 virtual aaudio_result_t release_l() REQUIRES(mStreamLock) { 148 setState(AAUDIO_STREAM_STATE_CLOSING); 149 return AAUDIO_OK; 150 } 151 152 /** 153 * Free any resources not already freed by release_l(). 154 * Assume release_l() already called. 155 */ 156 virtual void close_l() REQUIRES(mStreamLock); 157 158 public: 159 // This is only used to identify a stream in the logs without 160 // revealing any pointers. getId()161 aaudio_stream_id_t getId() { 162 return mStreamId; 163 } 164 165 virtual aaudio_result_t setBufferSize(int32_t requestedFrames) = 0; 166 createThread(int64_t periodNanoseconds,aaudio_audio_thread_proc_t threadProc,void * threadArg)167 aaudio_result_t createThread(int64_t periodNanoseconds, 168 aaudio_audio_thread_proc_t threadProc, 169 void *threadArg) 170 EXCLUDES(mStreamLock) { 171 std::lock_guard<std::mutex> lock(mStreamLock); 172 return createThread_l(periodNanoseconds, threadProc, threadArg); 173 } 174 175 aaudio_result_t joinThread(void **returnArg) EXCLUDES(mStreamLock); 176 registerThread()177 virtual aaudio_result_t registerThread() { 178 return AAUDIO_OK; 179 } 180 unregisterThread()181 virtual aaudio_result_t unregisterThread() { 182 return AAUDIO_OK; 183 } 184 185 /** 186 * Internal function used to call the audio thread passed by the user. 187 * It is unfortunately public because it needs to be called by a static 'C' function. 188 */ 189 void* wrapUserThread(); 190 191 // ============== Queries =========================== 192 getState()193 aaudio_stream_state_t getState() const { 194 return mState.load(); 195 } 196 197 aaudio_stream_state_t getStateExternal() const; 198 getBufferSize()199 virtual int32_t getBufferSize() const { 200 return AAUDIO_ERROR_UNIMPLEMENTED; 201 } 202 getBufferCapacity()203 virtual int32_t getBufferCapacity() const { 204 return mBufferCapacity; 205 } 206 getDeviceBufferCapacity()207 virtual int32_t getDeviceBufferCapacity() const { 208 return mDeviceBufferCapacity; 209 } 210 getFramesPerBurst()211 virtual int32_t getFramesPerBurst() const { 212 return mFramesPerBurst; 213 } 214 getDeviceFramesPerBurst()215 virtual int32_t getDeviceFramesPerBurst() const { 216 return mDeviceFramesPerBurst; 217 } 218 getXRunCount()219 virtual int32_t getXRunCount() const { 220 return AAUDIO_ERROR_UNIMPLEMENTED; 221 } 222 isActive()223 bool isActive() const { 224 return mState == AAUDIO_STREAM_STATE_STARTING || mState == AAUDIO_STREAM_STATE_STARTED; 225 } 226 isMMap()227 virtual bool isMMap() { 228 return false; 229 } 230 getSampleRate()231 aaudio_result_t getSampleRate() const { 232 return mSampleRate; 233 } 234 getDeviceSampleRate()235 aaudio_result_t getDeviceSampleRate() const { 236 return mDeviceSampleRate; 237 } 238 getHardwareSampleRate()239 aaudio_result_t getHardwareSampleRate() const { 240 return mHardwareSampleRate; 241 } 242 getFormat()243 audio_format_t getFormat() const { 244 return mFormat; 245 } 246 getHardwareFormat()247 audio_format_t getHardwareFormat() const { 248 return mHardwareFormat; 249 } 250 getSamplesPerFrame()251 aaudio_result_t getSamplesPerFrame() const { 252 return mSamplesPerFrame; 253 } 254 getDeviceSamplesPerFrame()255 aaudio_result_t getDeviceSamplesPerFrame() const { 256 return mDeviceSamplesPerFrame; 257 } 258 getHardwareSamplesPerFrame()259 aaudio_result_t getHardwareSamplesPerFrame() const { 260 return mHardwareSamplesPerFrame; 261 } 262 getPerformanceMode()263 virtual int32_t getPerformanceMode() const { 264 return mPerformanceMode; 265 } 266 setPerformanceMode(aaudio_performance_mode_t performanceMode)267 void setPerformanceMode(aaudio_performance_mode_t performanceMode) { 268 mPerformanceMode = performanceMode; 269 } 270 getDeviceId()271 int32_t getDeviceId() const { 272 return mDeviceId; 273 } 274 getSharingMode()275 aaudio_sharing_mode_t getSharingMode() const { 276 return mSharingMode; 277 } 278 isSharingModeMatchRequired()279 bool isSharingModeMatchRequired() const { 280 return mSharingModeMatchRequired; 281 } 282 283 virtual aaudio_direction_t getDirection() const = 0; 284 getUsage()285 aaudio_usage_t getUsage() const { 286 return mUsage; 287 } 288 getContentType()289 aaudio_content_type_t getContentType() const { 290 return mContentType; 291 } 292 getSpatializationBehavior()293 aaudio_spatialization_behavior_t getSpatializationBehavior() const { 294 return mSpatializationBehavior; 295 } 296 isContentSpatialized()297 bool isContentSpatialized() const { 298 return mIsContentSpatialized; 299 } 300 getInputPreset()301 aaudio_input_preset_t getInputPreset() const { 302 return mInputPreset; 303 } 304 getAllowedCapturePolicy()305 aaudio_allowed_capture_policy_t getAllowedCapturePolicy() const { 306 return mAllowedCapturePolicy; 307 } 308 getSessionId()309 int32_t getSessionId() const { 310 return mSessionId; 311 } 312 isPrivacySensitive()313 bool isPrivacySensitive() const { 314 return mIsPrivacySensitive; 315 } 316 getRequireMonoBlend()317 bool getRequireMonoBlend() const { 318 return mRequireMonoBlend; 319 } 320 getAudioBalance()321 float getAudioBalance() const { 322 return mAudioBalance; 323 } 324 325 /** 326 * This is only valid after setChannelMask() and setFormat() 327 * have been called. 328 */ getBytesPerFrame()329 int32_t getBytesPerFrame() const { 330 return mSamplesPerFrame * getBytesPerSample(); 331 } 332 333 /** 334 * This is only valid after setFormat() has been called. 335 */ getBytesPerSample()336 int32_t getBytesPerSample() const { 337 return audio_bytes_per_sample(mFormat); 338 } 339 340 /** 341 * This is only valid after setDeviceSamplesPerFrame() and setDeviceFormat() have been called. 342 */ getBytesPerDeviceFrame()343 int32_t getBytesPerDeviceFrame() const { 344 return getDeviceSamplesPerFrame() * audio_bytes_per_sample(getDeviceFormat()); 345 } 346 347 virtual int64_t getFramesWritten() = 0; 348 349 virtual int64_t getFramesRead() = 0; 350 getDataCallbackProc()351 AAudioStream_dataCallback getDataCallbackProc() const { 352 return mDataCallbackProc; 353 } 354 getErrorCallbackProc()355 AAudioStream_errorCallback getErrorCallbackProc() const { 356 return mErrorCallbackProc; 357 } 358 359 aaudio_data_callback_result_t maybeCallDataCallback(void *audioData, int32_t numFrames); 360 361 void maybeCallErrorCallback(aaudio_result_t result); 362 getDataCallbackUserData()363 void *getDataCallbackUserData() const { 364 return mDataCallbackUserData; 365 } 366 getErrorCallbackUserData()367 void *getErrorCallbackUserData() const { 368 return mErrorCallbackUserData; 369 } 370 getFramesPerDataCallback()371 int32_t getFramesPerDataCallback() const { 372 return mFramesPerDataCallback; 373 } 374 getChannelMask()375 aaudio_channel_mask_t getChannelMask() const { 376 return mChannelMask; 377 } 378 setChannelMask(aaudio_channel_mask_t channelMask)379 void setChannelMask(aaudio_channel_mask_t channelMask) { 380 mChannelMask = channelMask; 381 mSamplesPerFrame = AAudioConvert_channelMaskToCount(channelMask); 382 } 383 setDeviceSamplesPerFrame(int32_t deviceSamplesPerFrame)384 void setDeviceSamplesPerFrame(int32_t deviceSamplesPerFrame) { 385 mDeviceSamplesPerFrame = deviceSamplesPerFrame; 386 } 387 388 389 /** 390 * @return true if data callback has been specified 391 */ isDataCallbackSet()392 bool isDataCallbackSet() const { 393 return mDataCallbackProc != nullptr; 394 } 395 396 /** 397 * @return true if data callback has been specified and stream is running 398 */ isDataCallbackActive()399 bool isDataCallbackActive() const { 400 return isDataCallbackSet() && isActive(); 401 } 402 403 /** 404 * @return true if called from the same thread as the callback 405 */ 406 bool collidesWithCallback() const; 407 408 // Implement AudioDeviceCallback onAudioDeviceUpdate(audio_io_handle_t audioIo,audio_port_handle_t deviceId)409 void onAudioDeviceUpdate(audio_io_handle_t audioIo, 410 audio_port_handle_t deviceId) override {}; 411 412 // ============== I/O =========================== 413 // A Stream will only implement read() or write() depending on its direction. write(const void * buffer __unused,int32_t numFrames __unused,int64_t timeoutNanoseconds __unused)414 virtual aaudio_result_t write(const void *buffer __unused, 415 int32_t numFrames __unused, 416 int64_t timeoutNanoseconds __unused) { 417 return AAUDIO_ERROR_UNIMPLEMENTED; 418 } 419 read(void * buffer __unused,int32_t numFrames __unused,int64_t timeoutNanoseconds __unused)420 virtual aaudio_result_t read(void *buffer __unused, 421 int32_t numFrames __unused, 422 int64_t timeoutNanoseconds __unused) { 423 return AAUDIO_ERROR_UNIMPLEMENTED; 424 } 425 426 // This is used by the AudioManager to duck and mute the stream when changing audio focus. 427 void setDuckAndMuteVolume(float duckAndMuteVolume) EXCLUDES(mStreamLock); 428 getDuckAndMuteVolume()429 float getDuckAndMuteVolume() const { 430 return mDuckAndMuteVolume; 431 } 432 433 // Implement this in the output subclasses. doSetVolume()434 virtual android::status_t doSetVolume() { return android::NO_ERROR; } 435 436 #if AAUDIO_USE_VOLUME_SHAPER 437 virtual ::android::binder::Status applyVolumeShaper( 438 const ::android::media::VolumeShaper::Configuration& configuration __unused, 439 const ::android::media::VolumeShaper::Operation& operation __unused); 440 #endif 441 442 /** 443 * Register this stream's PlayerBase with the AudioManager if needed. 444 * Only register output streams. 445 * This should only be called for client streams and not for streams 446 * that run in the service. 447 */ registerPlayerBase()448 virtual void registerPlayerBase() { 449 if (getDirection() == AAUDIO_DIRECTION_OUTPUT) { 450 mPlayerBase->registerWithAudioManager(this); 451 } 452 } 453 454 /** 455 * Unregister this stream's PlayerBase with the AudioManager. 456 * This will only unregister if already registered. 457 */ unregisterPlayerBase()458 void unregisterPlayerBase() { 459 mPlayerBase->unregisterWithAudioManager(); 460 } 461 462 aaudio_result_t systemStart() EXCLUDES(mStreamLock); 463 464 aaudio_result_t systemPause() EXCLUDES(mStreamLock); 465 466 aaudio_result_t safeFlush() EXCLUDES(mStreamLock); 467 468 /** 469 * This is called when an app calls AAudioStream_requestStop(); 470 * It prevents calls from a callback. 471 */ 472 aaudio_result_t systemStopFromApp(); 473 474 /** 475 * This is called internally when an app callback returns AAUDIO_CALLBACK_RESULT_STOP. 476 */ 477 aaudio_result_t systemStopInternal() EXCLUDES(mStreamLock); 478 479 /** 480 * Safely RELEASE a stream after taking mStreamLock and checking 481 * to make sure we are not being called from a callback. 482 * @return AAUDIO_OK or a negative error 483 */ 484 aaudio_result_t safeRelease() EXCLUDES(mStreamLock); 485 486 /** 487 * Safely RELEASE and CLOSE a stream after taking mStreamLock and checking 488 * to make sure we are not being called from a callback. 489 * @return AAUDIO_OK or a negative error 490 */ 491 aaudio_result_t safeReleaseClose(); 492 493 aaudio_result_t safeReleaseCloseInternal() EXCLUDES(mStreamLock); 494 495 protected: 496 497 // PlayerBase allows the system to control the stream volume. 498 class MyPlayerBase : public android::PlayerBase { 499 public: 500 MyPlayerBase() = default; 501 502 virtual ~MyPlayerBase() = default; 503 504 /** 505 * Register for volume changes and remote control. 506 */ 507 void registerWithAudioManager(const android::sp<AudioStream>& parent); 508 509 /** 510 * UnRegister. 511 */ 512 void unregisterWithAudioManager(); 513 514 /** 515 * Just calls unregisterWithAudioManager(). 516 */ 517 void destroy() override; 518 519 // Just a stub. The ability to start audio through PlayerBase is being deprecated. playerStart()520 android::status_t playerStart() override { 521 return android::NO_ERROR; 522 } 523 524 // Just a stub. The ability to pause audio through PlayerBase is being deprecated. playerPause()525 android::status_t playerPause() override { 526 return android::NO_ERROR; 527 } 528 529 // Just a stub. The ability to stop audio through PlayerBase is being deprecated. playerStop()530 android::status_t playerStop() override { 531 return android::NO_ERROR; 532 } 533 534 android::status_t playerSetVolume() override; 535 536 #if AAUDIO_USE_VOLUME_SHAPER 537 ::android::binder::Status applyVolumeShaper(); 538 #endif 539 getResult()540 aaudio_result_t getResult() { 541 return mResult; 542 } 543 544 // Returns the playerIId if registered, -1 otherwise. getPlayerIId()545 int32_t getPlayerIId() const { 546 return mPIId; 547 } 548 549 private: 550 // Use a weak pointer so the AudioStream can be deleted. 551 std::mutex mParentLock; 552 android::wp<AudioStream> mParent GUARDED_BY(mParentLock); 553 aaudio_result_t mResult = AAUDIO_OK; 554 bool mRegistered = false; 555 }; 556 557 /** 558 * This should not be called after the open() call. 559 * TODO for multiple setters: assert(mState == AAUDIO_STREAM_STATE_UNINITIALIZED) 560 */ setSampleRate(int32_t sampleRate)561 void setSampleRate(int32_t sampleRate) { 562 mSampleRate = sampleRate; 563 } 564 565 // This should not be called after the open() call. setDeviceSampleRate(int32_t deviceSampleRate)566 void setDeviceSampleRate(int32_t deviceSampleRate) { 567 mDeviceSampleRate = deviceSampleRate; 568 } 569 570 // This should not be called after the open() call. setHardwareSampleRate(int32_t hardwareSampleRate)571 void setHardwareSampleRate(int32_t hardwareSampleRate) { 572 mHardwareSampleRate = hardwareSampleRate; 573 } 574 575 // This should not be called after the open() call. setFramesPerBurst(int32_t framesPerBurst)576 void setFramesPerBurst(int32_t framesPerBurst) { 577 mFramesPerBurst = framesPerBurst; 578 } 579 580 // This should not be called after the open() call. setDeviceFramesPerBurst(int32_t deviceFramesPerBurst)581 void setDeviceFramesPerBurst(int32_t deviceFramesPerBurst) { 582 mDeviceFramesPerBurst = deviceFramesPerBurst; 583 } 584 585 // This should not be called after the open() call. setBufferCapacity(int32_t bufferCapacity)586 void setBufferCapacity(int32_t bufferCapacity) { 587 mBufferCapacity = bufferCapacity; 588 } 589 590 // This should not be called after the open() call. setDeviceBufferCapacity(int32_t deviceBufferCapacity)591 void setDeviceBufferCapacity(int32_t deviceBufferCapacity) { 592 mDeviceBufferCapacity = deviceBufferCapacity; 593 } 594 595 // This should not be called after the open() call. setSharingMode(aaudio_sharing_mode_t sharingMode)596 void setSharingMode(aaudio_sharing_mode_t sharingMode) { 597 mSharingMode = sharingMode; 598 } 599 600 // This should not be called after the open() call. setFormat(audio_format_t format)601 void setFormat(audio_format_t format) { 602 mFormat = format; 603 } 604 605 // This should not be called after the open() call. setHardwareFormat(audio_format_t format)606 void setHardwareFormat(audio_format_t format) { 607 mHardwareFormat = format; 608 } 609 610 // This should not be called after the open() call. setHardwareSamplesPerFrame(int32_t hardwareSamplesPerFrame)611 void setHardwareSamplesPerFrame(int32_t hardwareSamplesPerFrame) { 612 mHardwareSamplesPerFrame = hardwareSamplesPerFrame; 613 } 614 615 // This should not be called after the open() call. setDeviceFormat(audio_format_t format)616 void setDeviceFormat(audio_format_t format) { 617 mDeviceFormat = format; 618 } 619 getDeviceFormat()620 audio_format_t getDeviceFormat() const { 621 return mDeviceFormat; 622 } 623 624 void setState(aaudio_stream_state_t state); 625 isDisconnected()626 bool isDisconnected() const { 627 return mDisconnected.load(); 628 } 629 void setDisconnected(); 630 setDeviceId(int32_t deviceId)631 void setDeviceId(int32_t deviceId) { 632 mDeviceId = deviceId; 633 } 634 635 // This should not be called after the open() call. setSessionId(int32_t sessionId)636 void setSessionId(int32_t sessionId) { 637 mSessionId = sessionId; 638 } 639 640 aaudio_result_t createThread_l(int64_t periodNanoseconds, 641 aaudio_audio_thread_proc_t threadProc, 642 void *threadArg) 643 REQUIRES(mStreamLock); 644 645 aaudio_result_t joinThread_l(void **returnArg) REQUIRES(mStreamLock); 646 647 std::atomic<bool> mCallbackEnabled{false}; 648 649 float mDuckAndMuteVolume = 1.0f; 650 651 protected: 652 653 /** 654 * Either convert the data from device format to app format and return a pointer 655 * to the conversion buffer, 656 * OR just pass back the original pointer. 657 * 658 * Note that this is only used for the INPUT path. 659 * 660 * @param audioData 661 * @param numFrames 662 * @return original pointer or the conversion buffer 663 */ maybeConvertDeviceData(const void * audioData,int32_t)664 virtual const void * maybeConvertDeviceData(const void *audioData, int32_t /*numFrames*/) { 665 return audioData; 666 } 667 setPeriodNanoseconds(int64_t periodNanoseconds)668 void setPeriodNanoseconds(int64_t periodNanoseconds) { 669 mPeriodNanoseconds.store(periodNanoseconds, std::memory_order_release); 670 } 671 getPeriodNanoseconds()672 int64_t getPeriodNanoseconds() { 673 return mPeriodNanoseconds.load(std::memory_order_acquire); 674 } 675 676 /** 677 * This should not be called after the open() call. 678 */ setUsage(aaudio_usage_t usage)679 void setUsage(aaudio_usage_t usage) { 680 mUsage = usage; 681 } 682 683 /** 684 * This should not be called after the open() call. 685 */ setContentType(aaudio_content_type_t contentType)686 void setContentType(aaudio_content_type_t contentType) { 687 mContentType = contentType; 688 } 689 setSpatializationBehavior(aaudio_spatialization_behavior_t spatializationBehavior)690 void setSpatializationBehavior(aaudio_spatialization_behavior_t spatializationBehavior) { 691 mSpatializationBehavior = spatializationBehavior; 692 } 693 setIsContentSpatialized(bool isContentSpatialized)694 void setIsContentSpatialized(bool isContentSpatialized) { 695 mIsContentSpatialized = isContentSpatialized; 696 } 697 698 /** 699 * This should not be called after the open() call. 700 */ setInputPreset(aaudio_input_preset_t inputPreset)701 void setInputPreset(aaudio_input_preset_t inputPreset) { 702 mInputPreset = inputPreset; 703 } 704 705 /** 706 * This should not be called after the open() call. 707 */ setAllowedCapturePolicy(aaudio_allowed_capture_policy_t policy)708 void setAllowedCapturePolicy(aaudio_allowed_capture_policy_t policy) { 709 mAllowedCapturePolicy = policy; 710 } 711 712 /** 713 * This should not be called after the open() call. 714 */ setPrivacySensitive(bool privacySensitive)715 void setPrivacySensitive(bool privacySensitive) { 716 mIsPrivacySensitive = privacySensitive; 717 } 718 719 /** 720 * This should not be called after the open() call. 721 */ setRequireMonoBlend(bool requireMonoBlend)722 void setRequireMonoBlend(bool requireMonoBlend) { 723 mRequireMonoBlend = requireMonoBlend; 724 } 725 726 /** 727 * This should not be called after the open() call. 728 */ setAudioBalance(float audioBalance)729 void setAudioBalance(float audioBalance) { 730 mAudioBalance = audioBalance; 731 } 732 733 std::string mMetricsId; // set once during open() 734 735 std::mutex mStreamLock; 736 737 const android::sp<MyPlayerBase> mPlayerBase; 738 739 private: 740 741 aaudio_result_t safeStop_l() REQUIRES(mStreamLock); 742 743 /** 744 * Release then close the stream. 745 */ releaseCloseFinal_l()746 void releaseCloseFinal_l() REQUIRES(mStreamLock) { 747 if (getState() != AAUDIO_STREAM_STATE_CLOSING) { // not already released? 748 // Ignore result and keep closing. 749 (void) release_l(); 750 } 751 close_l(); 752 } 753 754 std::atomic<aaudio_stream_state_t> mState{AAUDIO_STREAM_STATE_UNINITIALIZED}; 755 756 std::atomic_bool mDisconnected{false}; 757 758 // These do not change after open(). 759 int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED; 760 int32_t mDeviceSamplesPerFrame = AAUDIO_UNSPECIFIED; 761 int32_t mHardwareSamplesPerFrame = AAUDIO_UNSPECIFIED; 762 aaudio_channel_mask_t mChannelMask = AAUDIO_UNSPECIFIED; 763 int32_t mSampleRate = AAUDIO_UNSPECIFIED; 764 int32_t mDeviceSampleRate = AAUDIO_UNSPECIFIED; 765 int32_t mHardwareSampleRate = AAUDIO_UNSPECIFIED; 766 int32_t mDeviceId = AAUDIO_UNSPECIFIED; 767 aaudio_sharing_mode_t mSharingMode = AAUDIO_SHARING_MODE_SHARED; 768 bool mSharingModeMatchRequired = false; // must match sharing mode requested 769 audio_format_t mFormat = AUDIO_FORMAT_DEFAULT; 770 audio_format_t mHardwareFormat = AUDIO_FORMAT_DEFAULT; 771 aaudio_performance_mode_t mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; 772 int32_t mFramesPerBurst = 0; 773 int32_t mDeviceFramesPerBurst = 0; 774 int32_t mBufferCapacity = 0; 775 int32_t mDeviceBufferCapacity = 0; 776 777 aaudio_usage_t mUsage = AAUDIO_UNSPECIFIED; 778 aaudio_content_type_t mContentType = AAUDIO_UNSPECIFIED; 779 aaudio_spatialization_behavior_t mSpatializationBehavior = AAUDIO_UNSPECIFIED; 780 bool mIsContentSpatialized = false; 781 aaudio_input_preset_t mInputPreset = AAUDIO_UNSPECIFIED; 782 aaudio_allowed_capture_policy_t mAllowedCapturePolicy = AAUDIO_ALLOW_CAPTURE_BY_ALL; 783 bool mIsPrivacySensitive = false; 784 bool mRequireMonoBlend = false; 785 float mAudioBalance = 0; 786 787 int32_t mSessionId = AAUDIO_UNSPECIFIED; 788 789 // Sometimes the hardware is operating with a different format from the app. 790 // Then we require conversion in AAudio. 791 audio_format_t mDeviceFormat = AUDIO_FORMAT_INVALID; 792 793 // callback ---------------------------------- 794 795 AAudioStream_dataCallback mDataCallbackProc = nullptr; // external callback functions 796 void *mDataCallbackUserData = nullptr; 797 int32_t mFramesPerDataCallback = AAUDIO_UNSPECIFIED; // frames 798 std::atomic<pid_t> mDataCallbackThread{CALLBACK_THREAD_NONE}; 799 800 AAudioStream_errorCallback mErrorCallbackProc = nullptr; 801 void *mErrorCallbackUserData = nullptr; 802 std::atomic<pid_t> mErrorCallbackThread{CALLBACK_THREAD_NONE}; 803 804 // background thread ---------------------------------- 805 // Use mHasThread to prevent joining twice, which has undefined behavior. 806 bool mHasThread GUARDED_BY(mStreamLock) = false; 807 pthread_t mThread GUARDED_BY(mStreamLock) = {}; 808 809 // These are set by the application thread and then read by the audio pthread. 810 std::atomic<int64_t> mPeriodNanoseconds; // for tuning SCHED_FIFO threads 811 // TODO make atomic? 812 aaudio_audio_thread_proc_t mThreadProc = nullptr; 813 void *mThreadArg = nullptr; 814 aaudio_result_t mThreadRegistrationResult = AAUDIO_OK; 815 816 const aaudio_stream_id_t mStreamId; 817 818 }; 819 820 } /* namespace aaudio */ 821 822 #endif /* AAUDIO_AUDIOSTREAM_H */ 823