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 "FastThread.h"
22 #include "StateQueue.h"
23 #include "FastMixerState.h"
24 #include "FastMixerDumpState.h"
25 
26 namespace android {
27 
28 class AudioMixer;
29 
30 typedef StateQueue<FastMixerState> FastMixerStateQueue;
31 
32 class FastMixer : public FastThread {
33 
34 public:
35             FastMixer();
36     virtual ~FastMixer();
37 
38             FastMixerStateQueue* sq();
39 
setMasterMono(bool mono)40     virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ }
setBoottimeOffset(int64_t boottimeOffset)41     virtual void setBoottimeOffset(int64_t boottimeOffset) {
42         mBoottimeOffset.store(boottimeOffset); /* memory_order_seq_cst */
43     }
44 private:
45             FastMixerStateQueue mSQ;
46 
47     // callouts
48     virtual const FastThreadState *poll();
49     virtual void setNBLogWriter(NBLog::Writer *logWriter);
50     virtual void onIdle();
51     virtual void onExit();
52     virtual bool isSubClassCommand(FastThreadState::Command command);
53     virtual void onStateChange();
54     virtual void onWork();
55 
56     // FIXME these former local variables need comments
57     static const FastMixerState sInitial;
58 
59     FastMixerState  mPreIdle;   // copy of state before we went into idle
60     int             mGenerations[FastMixerState::kMaxFastTracks];
61                                 // last observed mFastTracks[i].mGeneration
62     NBAIO_Sink*     mOutputSink;
63     int             mOutputSinkGen;
64     AudioMixer*     mMixer;
65 
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     audio_format_t  mMixerBufferFormat; // mixer output format: AUDIO_FORMAT_PCM_(16_BIT|FLOAT).
75 
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 
83     // next 2 fields are valid only when timestampStatus == NO_ERROR
84     ExtendedTimestamp mTimestamp;
85     int64_t         mNativeFramesWrittenButNotPresented;
86 
87     // accessed without lock between multiple threads.
88     std::atomic_bool mMasterMono;
89     std::atomic_int_fast64_t mBoottimeOffset;
90 };  // class FastMixer
91 
92 }   // namespace android
93 
94 #endif  // ANDROID_AUDIO_FAST_MIXER_H
95