blob: 930fa8d8ccd6613c46673c638c0ed14f5103ae01 [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"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070025
26namespace android {
27
Glenn Kasten22340022014-04-07 12:04:41 -070028class AudioMixer;
29
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070030typedef StateQueue<FastMixerState> FastMixerStateQueue;
31
Glenn Kastenf7160b52014-03-18 17:01:15 -070032class FastMixer : public FastThread {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070033
34public:
Glenn Kasten22340022014-04-07 12:04:41 -070035 FastMixer();
36 virtual ~FastMixer();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070037
Glenn Kasten22340022014-04-07 12:04:41 -070038 FastMixerStateQueue* sq();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070039
Andy Hung2ddee192015-12-18 17:34:44 -080040 virtual void setMasterMono(bool mono) { mMasterMono.store(mono); /* memory_order_seq_cst */ }
Andy Hung818e7a32016-02-16 18:08:07 -080041 virtual void setBoottimeOffset(int64_t boottimeOffset) {
42 mBoottimeOffset.store(boottimeOffset); /* memory_order_seq_cst */
43 }
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070044private:
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070045 FastMixerStateQueue mSQ;
46
Glenn Kasten22340022014-04-07 12:04:41 -070047 // callouts
48 virtual const FastThreadState *poll();
Glenn Kasten3ab8d662017-04-03 14:35:09 -070049 virtual void setNBLogWriter(NBLog::Writer *logWriter);
Glenn Kasten22340022014-04-07 12:04:41 -070050 virtual void onIdle();
51 virtual void onExit();
52 virtual bool isSubClassCommand(FastThreadState::Command command);
53 virtual void onStateChange();
54 virtual void onWork();
55
Glenn Kastene4a7ce22015-03-03 11:23:17 -080056 // FIXME these former local variables need comments
57 static const FastMixerState sInitial;
58
59 FastMixerState mPreIdle; // copy of state before we went into idle
Glenn Kastene4a7ce22015-03-03 11:23:17 -080060 int mFastTrackNames[FastMixerState::kMaxFastTracks];
61 // handles used by mixer to identify tracks
62 int mGenerations[FastMixerState::kMaxFastTracks];
63 // last observed mFastTracks[i].mGeneration
64 NBAIO_Sink* mOutputSink;
65 int mOutputSinkGen;
66 AudioMixer* mMixer;
Andy Hung1258c1a2014-05-23 21:22:17 -070067
68 // mSinkBuffer audio format is stored in format.mFormat.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080069 void* mSinkBuffer; // used for mixer output format translation
Andy Hung1258c1a2014-05-23 21:22:17 -070070 // if sink format is different than mixer output.
Glenn Kastene4a7ce22015-03-03 11:23:17 -080071 size_t mSinkBufferSize;
72 uint32_t mSinkChannelCount;
Andy Hung9a592762014-07-21 21:56:01 -070073 audio_channel_mask_t mSinkChannelMask;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080074 void* mMixerBuffer; // mixer output buffer.
75 size_t mMixerBufferSize;
76 audio_format_t mMixerBufferFormat; // mixer output format: AUDIO_FORMAT_PCM_(16_BIT|FLOAT).
Andy Hung1258c1a2014-05-23 21:22:17 -070077
Andy Hung45d68d32014-05-23 21:13:31 -070078 enum {UNDEFINED, MIXED, ZEROED} mMixerBufferState;
Glenn Kastene4a7ce22015-03-03 11:23:17 -080079 NBAIO_Format mFormat;
80 unsigned mSampleRate;
81 int mFastTracksGen;
82 FastMixerDumpState mDummyFastMixerDumpState;
Andy Hung818e7a32016-02-16 18:08:07 -080083 int64_t mTotalNativeFramesWritten; // copied to dumpState->mFramesWritten
Glenn Kasten22340022014-04-07 12:04:41 -070084
85 // next 2 fields are valid only when timestampStatus == NO_ERROR
Andy Hung818e7a32016-02-16 18:08:07 -080086 ExtendedTimestamp mTimestamp;
87 int64_t mNativeFramesWrittenButNotPresented;
Glenn Kasten22340022014-04-07 12:04:41 -070088
Andy Hung2ddee192015-12-18 17:34:44 -080089 // accessed without lock between multiple threads.
90 std::atomic_bool mMasterMono;
Andy Hung818e7a32016-02-16 18:08:07 -080091 std::atomic_int_fast64_t mBoottimeOffset;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070092}; // class FastMixer
93
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070094} // namespace android
95
96#endif // ANDROID_AUDIO_FAST_MIXER_H