FastMixer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2012 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. #ifndef ANDROID_AUDIO_FAST_MIXER_H
  17. #define ANDROID_AUDIO_FAST_MIXER_H
  18. #include <atomic>
  19. #include <audio_utils/Balance.h>
  20. #include "FastThread.h"
  21. #include "StateQueue.h"
  22. #include "FastMixerState.h"
  23. #include "FastMixerDumpState.h"
  24. #include "NBAIO_Tee.h"
  25. namespace android {
  26. class AudioMixer;
  27. typedef StateQueue<FastMixerState> FastMixerStateQueue;
  28. class FastMixer : public FastThread {
  29. public:
  30. /** FastMixer constructor takes as param the parent MixerThread's io handle (id)
  31. for purposes of identification. */
  32. explicit FastMixer(audio_io_handle_t threadIoHandle);
  33. virtual ~FastMixer();
  34. FastMixerStateQueue* sq();
  35. virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ }
  36. virtual void setMasterBalance(float balance) { mMasterBalance.store(balance); }
  37. virtual float getMasterBalance() const { return mMasterBalance.load(); }
  38. virtual void setBoottimeOffset(int64_t boottimeOffset) {
  39. mBoottimeOffset.store(boottimeOffset); /* memory_order_seq_cst */
  40. }
  41. private:
  42. FastMixerStateQueue mSQ;
  43. // callouts
  44. virtual const FastThreadState *poll();
  45. virtual void setNBLogWriter(NBLog::Writer *logWriter);
  46. virtual void onIdle();
  47. virtual void onExit();
  48. virtual bool isSubClassCommand(FastThreadState::Command command);
  49. virtual void onStateChange();
  50. virtual void onWork();
  51. enum Reason {
  52. REASON_REMOVE,
  53. REASON_ADD,
  54. REASON_MODIFY,
  55. };
  56. // called when a fast track of index has been removed, added, or modified
  57. void updateMixerTrack(int index, Reason reason);
  58. // FIXME these former local variables need comments
  59. static const FastMixerState sInitial;
  60. FastMixerState mPreIdle; // copy of state before we went into idle
  61. int mGenerations[FastMixerState::kMaxFastTracks];
  62. // last observed mFastTracks[i].mGeneration
  63. NBAIO_Sink* mOutputSink;
  64. int mOutputSinkGen;
  65. AudioMixer* mMixer;
  66. // mSinkBuffer audio format is stored in format.mFormat.
  67. void* mSinkBuffer; // used for mixer output format translation
  68. // if sink format is different than mixer output.
  69. size_t mSinkBufferSize;
  70. uint32_t mSinkChannelCount;
  71. audio_channel_mask_t mSinkChannelMask;
  72. void* mMixerBuffer; // mixer output buffer.
  73. size_t mMixerBufferSize;
  74. static constexpr audio_format_t mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT;
  75. uint32_t mAudioChannelCount; // audio channel count, excludes haptic channels.
  76. enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState;
  77. NBAIO_Format mFormat;
  78. unsigned mSampleRate;
  79. int mFastTracksGen;
  80. FastMixerDumpState mDummyFastMixerDumpState;
  81. int64_t mTotalNativeFramesWritten; // copied to dumpState->mFramesWritten
  82. // next 2 fields are valid only when timestampStatus == NO_ERROR
  83. ExtendedTimestamp mTimestamp;
  84. int64_t mNativeFramesWrittenButNotPresented;
  85. audio_utils::Balance mBalance;
  86. // accessed without lock between multiple threads.
  87. std::atomic_bool mMasterMono;
  88. std::atomic<float> mMasterBalance{};
  89. std::atomic_int_fast64_t mBoottimeOffset;
  90. const audio_io_handle_t mThreadIoHandle; // parent thread id for debugging purposes
  91. #ifdef TEE_SINK
  92. NBAIO_Tee mTee;
  93. #endif
  94. }; // class FastMixer
  95. } // namespace android
  96. #endif // ANDROID_AUDIO_FAST_MIXER_H