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