blob: c31d476c482ea65293f155ea597c46221bab13f1 [file] [log] [blame]
Glenn Kasten97b5d0d2012-03-23 18:54:19 -07001/*
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 Hung2ddee192015-12-18 17:34:44 -080020#include <atomic>
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010021#include <audio_utils/Balance.h>
Glenn Kastenf7160b52014-03-18 17:01:15 -070022#include "FastThread.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070023#include "StateQueue.h"
24#include "FastMixerState.h"
Glenn Kasten22340022014-04-07 12:04:41 -070025#include "FastMixerDumpState.h"
Andy Hung8946a282018-04-19 20:04:56 -070026#include "NBAIO_Tee.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070027
28namespace android {
29
Glenn Kasten22340022014-04-07 12:04:41 -070030class AudioMixer;
31
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070032typedef StateQueue<FastMixerState> FastMixerStateQueue;
33
Glenn Kastenf7160b52014-03-18 17:01:15 -070034class FastMixer : public FastThread {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070035
36public:
Andy Hung8946a282018-04-19 20:04:56 -070037 /** 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 Kasten22340022014-04-07 12:04:41 -070040 virtual ~FastMixer();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070041
Glenn Kasten22340022014-04-07 12:04:41 -070042 FastMixerStateQueue* sq();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070043
Andy Hung2ddee192015-12-18 17:34:44 -080044 virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010045 virtual void setMasterBalance(float balance) { mMasterBalance.store(balance); }
46 virtual float getMasterBalance() const { return mMasterBalance.load(); }
Andy Hung818e7a32016-02-16 18:08:07 -080047 virtual void setBoottimeOffset(int64_t boottimeOffset) {
48 mBoottimeOffset.store(boottimeOffset); /* memory_order_seq_cst */
49 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070050private:
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070051 FastMixerStateQueue mSQ;
52
Glenn Kasten22340022014-04-07 12:04:41 -070053 // callouts
54 virtual const FastThreadState *poll();
Glenn Kasten3ab8d662017-04-03 14:35:09 -070055 virtual void setNBLogWriter(NBLog::Writer *logWriter);
Glenn Kasten22340022014-04-07 12:04:41 -070056 virtual void onIdle();
57 virtual void onExit();
58 virtual bool isSubClassCommand(FastThreadState::Command command);
59 virtual void onStateChange();
60 virtual void onWork();
61
Glenn Kastene4a7ce22015-03-03 11:23:17 -080062 // 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 Kastene4a7ce22015-03-03 11:23:17 -080066 int mGenerations[FastMixerState::kMaxFastTracks];
67 // last observed mFastTracks[i].mGeneration
68 NBAIO_Sink* mOutputSink;
69 int mOutputSinkGen;
70 AudioMixer* mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -070071
72 // mSinkBuffer audio format is stored in format.mFormat.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080073 void* mSinkBuffer; // used for mixer output format translation
Andy Hung1258c1a2014-05-23 21:22:17 -070074 // if sink format is different than mixer output.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080075 size_t mSinkBufferSize;
76 uint32_t mSinkChannelCount;
Andy Hung9a592762014-07-21 21:56:01 -070077 audio_channel_mask_t mSinkChannelMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080078 void* mMixerBuffer; // mixer output buffer.
79 size_t mMixerBufferSize;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010080 static constexpr audio_format_t mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT;
Andy Hung1258c1a2014-05-23 21:22:17 -070081
jiabin245cdd92018-12-07 17:55:15 -080082 uint32_t mAudioChannelCount; // audio channel count, excludes haptic channels.
83
Andy Hung45d68d32014-05-23 21:13:31 -070084 enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080085 NBAIO_Format mFormat;
86 unsigned mSampleRate;
87 int mFastTracksGen;
88 FastMixerDumpState mDummyFastMixerDumpState;
Andy Hung818e7a32016-02-16 18:08:07 -080089 int64_t mTotalNativeFramesWritten; // copied to dumpState->mFramesWritten
Glenn Kasten22340022014-04-07 12:04:41 -070090
91 // next 2 fields are valid only when timestampStatus == NO_ERROR
Andy Hung818e7a32016-02-16 18:08:07 -080092 ExtendedTimestamp mTimestamp;
93 int64_t mNativeFramesWrittenButNotPresented;
Glenn Kasten22340022014-04-07 12:04:41 -070094
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010095 audio_utils::Balance mBalance;
96
Andy Hung2ddee192015-12-18 17:34:44 -080097 // accessed without lock between multiple threads.
98 std::atomic_bool mMasterMono;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010099 std::atomic<float> mMasterBalance{};
Andy Hung818e7a32016-02-16 18:08:07 -0800100 std::atomic_int_fast64_t mBoottimeOffset;
Andy Hung8946a282018-04-19 20:04:56 -0700101
102 const audio_io_handle_t mThreadIoHandle; // parent thread id for debugging purposes
103#ifdef TEE_SINK
104 NBAIO_Tee mTee;
105#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700106}; // class FastMixer
107
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700108} // namespace android
109
110#endif // ANDROID_AUDIO_FAST_MIXER_H