Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 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 | |
| 17 | #ifndef ANDROID_AUDIO_FAST_MIXER_H |
| 18 | #define ANDROID_AUDIO_FAST_MIXER_H |
| 19 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 20 | #include <atomic> |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame^] | 21 | #include <audio_utils/Balance.h> |
Glenn Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 22 | #include "FastThread.h" |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 23 | #include "StateQueue.h" |
| 24 | #include "FastMixerState.h" |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 25 | #include "FastMixerDumpState.h" |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 26 | #include "NBAIO_Tee.h" |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 30 | class AudioMixer; |
| 31 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 32 | typedef StateQueue<FastMixerState> FastMixerStateQueue; |
| 33 | |
Glenn Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 34 | class FastMixer : public FastThread { |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 35 | |
| 36 | public: |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 37 | /** FastMixer constructor takes as param the parent MixerThread's io handle (id) |
| 38 | for purposes of identification. */ |
| 39 | explicit FastMixer(audio_io_handle_t threadIoHandle); |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 40 | virtual ~FastMixer(); |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 41 | |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 42 | FastMixerStateQueue* sq(); |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 43 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 44 | virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ } |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame^] | 45 | virtual void setMasterBalance(float balance) { mMasterBalance.store(balance); } |
| 46 | virtual float getMasterBalance() const { return mMasterBalance.load(); } |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 47 | virtual void setBoottimeOffset(int64_t boottimeOffset) { |
| 48 | mBoottimeOffset.store(boottimeOffset); /* memory_order_seq_cst */ |
| 49 | } |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 50 | private: |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 51 | FastMixerStateQueue mSQ; |
| 52 | |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 53 | // callouts |
| 54 | virtual const FastThreadState *poll(); |
Glenn Kasten | 3ab8d66 | 2017-04-03 14:35:09 -0700 | [diff] [blame] | 55 | virtual void setNBLogWriter(NBLog::Writer *logWriter); |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 56 | virtual void onIdle(); |
| 57 | virtual void onExit(); |
| 58 | virtual bool isSubClassCommand(FastThreadState::Command command); |
| 59 | virtual void onStateChange(); |
| 60 | virtual void onWork(); |
| 61 | |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 62 | // FIXME these former local variables need comments |
| 63 | static const FastMixerState sInitial; |
| 64 | |
| 65 | FastMixerState mPreIdle; // copy of state before we went into idle |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 66 | int mGenerations[FastMixerState::kMaxFastTracks]; |
| 67 | // last observed mFastTracks[i].mGeneration |
| 68 | NBAIO_Sink* mOutputSink; |
| 69 | int mOutputSinkGen; |
| 70 | AudioMixer* mMixer; |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 71 | |
| 72 | // mSinkBuffer audio format is stored in format.mFormat. |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 73 | void* mSinkBuffer; // used for mixer output format translation |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 74 | // if sink format is different than mixer output. |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 75 | size_t mSinkBufferSize; |
| 76 | uint32_t mSinkChannelCount; |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 77 | audio_channel_mask_t mSinkChannelMask; |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 78 | void* mMixerBuffer; // mixer output buffer. |
| 79 | size_t mMixerBufferSize; |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame^] | 80 | static constexpr audio_format_t mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 81 | |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 82 | uint32_t mAudioChannelCount; // audio channel count, excludes haptic channels. |
| 83 | |
Andy Hung | 45d68d3 | 2014-05-23 21:13:31 -0700 | [diff] [blame] | 84 | enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState; |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 85 | NBAIO_Format mFormat; |
| 86 | unsigned mSampleRate; |
| 87 | int mFastTracksGen; |
| 88 | FastMixerDumpState mDummyFastMixerDumpState; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 89 | int64_t mTotalNativeFramesWritten; // copied to dumpState->mFramesWritten |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 90 | |
| 91 | // next 2 fields are valid only when timestampStatus == NO_ERROR |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 92 | ExtendedTimestamp mTimestamp; |
| 93 | int64_t mNativeFramesWrittenButNotPresented; |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 94 | |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame^] | 95 | audio_utils::Balance mBalance; |
| 96 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 97 | // accessed without lock between multiple threads. |
| 98 | std::atomic_bool mMasterMono; |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame^] | 99 | std::atomic<float> mMasterBalance{}; |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 100 | std::atomic_int_fast64_t mBoottimeOffset; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 101 | |
| 102 | const audio_io_handle_t mThreadIoHandle; // parent thread id for debugging purposes |
| 103 | #ifdef TEE_SINK |
| 104 | NBAIO_Tee mTee; |
| 105 | #endif |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 106 | }; // class FastMixer |
| 107 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 108 | } // namespace android |
| 109 | |
| 110 | #endif // ANDROID_AUDIO_FAST_MIXER_H |