blob: 97ab635fa03aa8295d9e65078fe804e81415e563 [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
Andy Hung4d4ca6a2019-02-01 18:23:37 -080062 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
Glenn Kastene4a7ce22015-03-03 11:23:17 -080070 // FIXME these former local variables need comments
71 static const FastMixerState sInitial;
72
73 FastMixerState mPreIdle; // copy of state before we went into idle
Glenn Kastene4a7ce22015-03-03 11:23:17 -080074 int mGenerations[FastMixerState::kMaxFastTracks];
75 // last observed mFastTracks[i].mGeneration
76 NBAIO_Sink* mOutputSink;
77 int mOutputSinkGen;
78 AudioMixer* mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -070079
80 // mSinkBuffer audio format is stored in format.mFormat.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080081 void* mSinkBuffer; // used for mixer output format translation
Andy Hung1258c1a2014-05-23 21:22:17 -070082 // if sink format is different than mixer output.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080083 size_t mSinkBufferSize;
84 uint32_t mSinkChannelCount;
Andy Hung9a592762014-07-21 21:56:01 -070085 audio_channel_mask_t mSinkChannelMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080086 void* mMixerBuffer; // mixer output buffer.
87 size_t mMixerBufferSize;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +010088 static constexpr audio_format_t mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT;
Andy Hung1258c1a2014-05-23 21:22:17 -070089
jiabin245cdd92018-12-07 17:55:15 -080090 uint32_t mAudioChannelCount; // audio channel count, excludes haptic channels.
91
Andy Hung45d68d32014-05-23 21:13:31 -070092 enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080093 NBAIO_Format mFormat;
94 unsigned mSampleRate;
95 int mFastTracksGen;
96 FastMixerDumpState mDummyFastMixerDumpState;
Andy Hung818e7a32016-02-16 18:08:07 -080097 int64_t mTotalNativeFramesWritten; // copied to dumpState->mFramesWritten
Glenn Kasten22340022014-04-07 12:04:41 -070098
99 // next 2 fields are valid only when timestampStatus == NO_ERROR
Andy Hung818e7a32016-02-16 18:08:07 -0800100 ExtendedTimestamp mTimestamp;
101 int64_t mNativeFramesWrittenButNotPresented;
Glenn Kasten22340022014-04-07 12:04:41 -0700102
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100103 audio_utils::Balance mBalance;
104
Andy Hung2ddee192015-12-18 17:34:44 -0800105 // accessed without lock between multiple threads.
106 std::atomic_bool mMasterMono;
Richard Folke Tullberg3fae0372017-01-13 09:04:25 +0100107 std::atomic<float> mMasterBalance{};
Andy Hung818e7a32016-02-16 18:08:07 -0800108 std::atomic_int_fast64_t mBoottimeOffset;
Andy Hung8946a282018-04-19 20:04:56 -0700109
110 const audio_io_handle_t mThreadIoHandle; // parent thread id for debugging purposes
111#ifdef TEE_SINK
112 NBAIO_Tee mTee;
113#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700114}; // class FastMixer
115
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700116} // namespace android
117
118#endif // ANDROID_AUDIO_FAST_MIXER_H