blob: 1c86d9ae7d89e822a20d896b24e0bab295dafe29 [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>
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 Hung8946a282018-04-19 20:04:56 -070025#include "NBAIO_Tee.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070026
27namespace android {
28
Glenn Kasten22340022014-04-07 12:04:41 -070029class AudioMixer;
30
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070031typedef StateQueue<FastMixerState> FastMixerStateQueue;
32
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 Kasten22340022014-04-07 12:04:41 -070039 virtual ~FastMixer();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070040
Glenn Kasten22340022014-04-07 12:04:41 -070041 FastMixerStateQueue* sq();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070042
Andy Hung2ddee192015-12-18 17:34:44 -080043 virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ }
Andy Hung818e7a32016-02-16 18:08:07 -080044 virtual void setBoottimeOffset(int64_t boottimeOffset) {
45 mBoottimeOffset.store(boottimeOffset); /* memory_order_seq_cst */
46 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070047private:
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070048 FastMixerStateQueue mSQ;
49
Glenn Kasten22340022014-04-07 12:04:41 -070050 // callouts
51 virtual const FastThreadState *poll();
Glenn Kasten3ab8d662017-04-03 14:35:09 -070052 virtual void setNBLogWriter(NBLog::Writer *logWriter);
Glenn Kasten22340022014-04-07 12:04:41 -070053 virtual void onIdle();
54 virtual void onExit();
55 virtual bool isSubClassCommand(FastThreadState::Command command);
56 virtual void onStateChange();
57 virtual void onWork();
58
Glenn Kastene4a7ce22015-03-03 11:23:17 -080059 // FIXME these former local variables need comments
60 static const FastMixerState sInitial;
61
62 FastMixerState mPreIdle; // copy of state before we went into idle
Glenn Kastene4a7ce22015-03-03 11:23:17 -080063 int mGenerations[FastMixerState::kMaxFastTracks];
64 // last observed mFastTracks[i].mGeneration
65 NBAIO_Sink* mOutputSink;
66 int mOutputSinkGen;
67 AudioMixer* mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -070068
69 // mSinkBuffer audio format is stored in format.mFormat.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080070 void* mSinkBuffer; // used for mixer output format translation
Andy Hung1258c1a2014-05-23 21:22:17 -070071 // if sink format is different than mixer output.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080072 size_t mSinkBufferSize;
73 uint32_t mSinkChannelCount;
Andy Hung9a592762014-07-21 21:56:01 -070074 audio_channel_mask_t mSinkChannelMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080075 void* mMixerBuffer; // mixer output buffer.
76 size_t mMixerBufferSize;
77 audio_format_t mMixerBufferFormat; // mixer output format: AUDIO_FORMAT_PCM_(16_BIT|FLOAT).
Andy Hung1258c1a2014-05-23 21:22:17 -070078
Andy Hung45d68d32014-05-23 21:13:31 -070079 enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080080 NBAIO_Format mFormat;
81 unsigned mSampleRate;
82 int mFastTracksGen;
83 FastMixerDumpState mDummyFastMixerDumpState;
Andy Hung818e7a32016-02-16 18:08:07 -080084 int64_t mTotalNativeFramesWritten; // copied to dumpState->mFramesWritten
Glenn Kasten22340022014-04-07 12:04:41 -070085
86 // next 2 fields are valid only when timestampStatus == NO_ERROR
Andy Hung818e7a32016-02-16 18:08:07 -080087 ExtendedTimestamp mTimestamp;
88 int64_t mNativeFramesWrittenButNotPresented;
Glenn Kasten22340022014-04-07 12:04:41 -070089
Andy Hung2ddee192015-12-18 17:34:44 -080090 // accessed without lock between multiple threads.
91 std::atomic_bool mMasterMono;
Andy Hung818e7a32016-02-16 18:08:07 -080092 std::atomic_int_fast64_t mBoottimeOffset;
Andy Hung8946a282018-04-19 20:04:56 -070093
94 const audio_io_handle_t mThreadIoHandle; // parent thread id for debugging purposes
95#ifdef TEE_SINK
96 NBAIO_Tee mTee;
97#endif
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070098}; // class FastMixer
99
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700100} // namespace android
101
102#endif // ANDROID_AUDIO_FAST_MIXER_H