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