blob: 48b94a350e9c316986160dd642b1df33cdfde8ec [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
Andy Hung4dcd7e92023-05-25 16:28:04 -070017#pragma once
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070018
Andy Hung2ddee192015-12-18 17:34:44 -080019#include <atomic>
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010020#include <audio_utils/Balance.h>
Glenn Kastenf7160b52014-03-18 17:01:15 -070021#include "FastThread.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070022#include "StateQueue.h"
23#include "FastMixerState.h"
Glenn Kasten22340022014-04-07 12:04:41 -070024#include "FastMixerDumpState.h"
Andy Hung0077d8c2023-05-24 11:53:47 -070025#include <afutils/NBAIO_Tee.h>
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070026
27namespace android {
28
Glenn Kasten22340022014-04-07 12:04:41 -070029class AudioMixer;
30
Andy Hung4dcd7e92023-05-25 16:28:04 -070031using FastMixerStateQueue = StateQueue<FastMixerState>;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070032
Glenn Kastenf7160b52014-03-18 17:01:15 -070033class FastMixer : public FastThread {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070034
35public:
Andy Hung8946a282018-04-19 20:04:56 -070036 /** 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);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070039
Glenn Kasten22340022014-04-07 12:04:41 -070040 FastMixerStateQueue* sq();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070041
Andy Hung2ddee192015-12-18 17:34:44 -080042 virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ }
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010043 virtual void setMasterBalance(float balance) { mMasterBalance.store(balance); }
44 virtual float getMasterBalance() const { return mMasterBalance.load(); }
Andy Hung818e7a32016-02-16 18:08:07 -080045 virtual void setBoottimeOffset(int64_t boottimeOffset) {
46 mBoottimeOffset.store(boottimeOffset); /* memory_order_seq_cst */
47 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070048private:
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070049 FastMixerStateQueue mSQ;
50
Glenn Kasten22340022014-04-07 12:04:41 -070051 // callouts
Andy Hung4dcd7e92023-05-25 16:28:04 -070052 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;
Glenn Kasten22340022014-04-07 12:04:41 -070059
Andy Hung4d4ca6a2019-02-01 18:23:37 -080060 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
Glenn Kastene4a7ce22015-03-03 11:23:17 -080068 // FIXME these former local variables need comments
69 static const FastMixerState sInitial;
70
71 FastMixerState mPreIdle; // copy of state before we went into idle
Andy Hungfd18f3c2023-05-26 16:24:28 -070072 int mGenerations[FastMixerState::kMaxFastTracks]{};
Glenn Kastene4a7ce22015-03-03 11:23:17 -080073 // last observed mFastTracks[i].mGeneration
Andy Hungfd18f3c2023-05-26 16:24:28 -070074 NBAIO_Sink* mOutputSink = nullptr;
75 int mOutputSinkGen = 0;
76 AudioMixer* mMixer = nullptr;
Andy Hung1258c1a2014-05-23 21:22:17 -070077
78 // mSinkBuffer audio format is stored in format.mFormat.
Andy Hungfd18f3c2023-05-26 16:24:28 -070079 void* mSinkBuffer = nullptr; // used for mixer output format translation
Andy Hung1258c1a2014-05-23 21:22:17 -070080 // if sink format is different than mixer output.
Andy Hungfd18f3c2023-05-26 16:24:28 -070081 size_t mSinkBufferSize = 0;
82 uint32_t mSinkChannelCount = FCC_2;
Andy Hung9a592762014-07-21 21:56:01 -070083 audio_channel_mask_t mSinkChannelMask;
Andy Hungfd18f3c2023-05-26 16:24:28 -070084 void* mMixerBuffer = nullptr; // mixer output buffer.
85 size_t mMixerBufferSize = 0;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010086 static constexpr audio_format_t mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT;
Andy Hung1258c1a2014-05-23 21:22:17 -070087
jiabin245cdd92018-12-07 17:55:15 -080088 uint32_t mAudioChannelCount; // audio channel count, excludes haptic channels.
89
Andy Hungfd18f3c2023-05-26 16:24:28 -070090 enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState = UNDEFINED;
91 NBAIO_Format mFormat{Format_Invalid};
92 unsigned mSampleRate = 0;
93 int mFastTracksGen = 0;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080094 FastMixerDumpState mDummyFastMixerDumpState;
Andy Hungfd18f3c2023-05-26 16:24:28 -070095 int64_t mTotalNativeFramesWritten = 0; // copied to dumpState->mFramesWritten
Glenn Kasten22340022014-04-07 12:04:41 -070096
97 // next 2 fields are valid only when timestampStatus == NO_ERROR
Andy Hung818e7a32016-02-16 18:08:07 -080098 ExtendedTimestamp mTimestamp;
Andy Hungfd18f3c2023-05-26 16:24:28 -070099 int64_t mNativeFramesWrittenButNotPresented = 0;
Glenn Kasten22340022014-04-07 12:04:41 -0700100
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100101 audio_utils::Balance mBalance;
102
Andy Hung2ddee192015-12-18 17:34:44 -0800103 // accessed without lock between multiple threads.
Andy Hungfd18f3c2023-05-26 16:24:28 -0700104 std::atomic_bool mMasterMono{};
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100105 std::atomic<float> mMasterBalance{};
Andy Hung818e7a32016-02-16 18:08:07 -0800106 std::atomic_int_fast64_t mBoottimeOffset;
Andy Hung8946a282018-04-19 20:04:56 -0700107
Jing Mike537412f2023-03-12 11:01:47 +0800108 // parent thread id for debugging purposes
109 [[maybe_unused]] const audio_io_handle_t mThreadIoHandle;
Andy Hung8946a282018-04-19 20:04:56 -0700110#ifdef TEE_SINK
111 NBAIO_Tee mTee;
112#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700113}; // class FastMixer
114
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700115} // namespace android