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> |
Glenn Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 21 | #include "FastThread.h" |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 22 | #include "StateQueue.h" |
| 23 | #include "FastMixerState.h" |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 24 | #include "FastMixerDumpState.h" |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 28 | class AudioMixer; |
| 29 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 30 | typedef StateQueue<FastMixerState> FastMixerStateQueue; |
| 31 | |
Glenn Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 32 | class FastMixer : public FastThread { |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 33 | |
| 34 | public: |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 35 | FastMixer(); |
| 36 | virtual ~FastMixer(); |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 37 | |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 38 | FastMixerStateQueue* sq(); |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 39 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 40 | virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ } |
| 41 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 42 | private: |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 43 | FastMixerStateQueue mSQ; |
| 44 | |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 45 | // callouts |
| 46 | virtual const FastThreadState *poll(); |
| 47 | virtual void setLog(NBLog::Writer *logWriter); |
| 48 | virtual void onIdle(); |
| 49 | virtual void onExit(); |
| 50 | virtual bool isSubClassCommand(FastThreadState::Command command); |
| 51 | virtual void onStateChange(); |
| 52 | virtual void onWork(); |
| 53 | |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 54 | // FIXME these former local variables need comments |
| 55 | static const FastMixerState sInitial; |
| 56 | |
| 57 | FastMixerState mPreIdle; // copy of state before we went into idle |
| 58 | long mSlopNs; // accumulated time we've woken up too early (> 0) or too late (< 0) |
| 59 | int mFastTrackNames[FastMixerState::kMaxFastTracks]; |
| 60 | // handles used by mixer to identify tracks |
| 61 | int mGenerations[FastMixerState::kMaxFastTracks]; |
| 62 | // last observed mFastTracks[i].mGeneration |
| 63 | NBAIO_Sink* mOutputSink; |
| 64 | int mOutputSinkGen; |
| 65 | AudioMixer* mMixer; |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 66 | |
| 67 | // mSinkBuffer audio format is stored in format.mFormat. |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 68 | void* mSinkBuffer; // used for mixer output format translation |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 69 | // if sink format is different than mixer output. |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 70 | size_t mSinkBufferSize; |
| 71 | uint32_t mSinkChannelCount; |
Andy Hung | 9a59276 | 2014-07-21 21:56:01 -0700 | [diff] [blame] | 72 | audio_channel_mask_t mSinkChannelMask; |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 73 | void* mMixerBuffer; // mixer output buffer. |
| 74 | size_t mMixerBufferSize; |
| 75 | audio_format_t mMixerBufferFormat; // mixer output format: AUDIO_FORMAT_PCM_(16_BIT|FLOAT). |
Andy Hung | 1258c1a | 2014-05-23 21:22:17 -0700 | [diff] [blame] | 76 | |
Andy Hung | 45d68d3 | 2014-05-23 21:13:31 -0700 | [diff] [blame] | 77 | enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState; |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 78 | NBAIO_Format mFormat; |
| 79 | unsigned mSampleRate; |
| 80 | int mFastTracksGen; |
| 81 | FastMixerDumpState mDummyFastMixerDumpState; |
| 82 | uint32_t mTotalNativeFramesWritten; // copied to dumpState->mFramesWritten |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 83 | |
| 84 | // next 2 fields are valid only when timestampStatus == NO_ERROR |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 85 | AudioTimestamp mTimestamp; |
| 86 | uint32_t mNativeFramesWrittenButNotPresented; |
Glenn Kasten | 2234002 | 2014-04-07 12:04:41 -0700 | [diff] [blame] | 87 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 88 | // accessed without lock between multiple threads. |
| 89 | std::atomic_bool mMasterMono; |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 90 | }; // class FastMixer |
| 91 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 92 | } // namespace android |
| 93 | |
| 94 | #endif // ANDROID_AUDIO_FAST_MIXER_H |