blob: ce3cc145f0b2a1e8d61035ab1237d8d4d5d27919 [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_STATE_H
18#define ANDROID_AUDIO_FAST_MIXER_STATE_H
19
Lais Andradebc3f37a2021-07-02 00:13:19 +010020#include <math.h>
21
Glenn Kastenc56f3422014-03-21 17:53:17 -070022#include <audio_utils/minifloat.h>
Glenn Kasten21e8c502012-04-12 09:39:42 -070023#include <system/audio.h>
jiabin77270b82018-12-18 15:41:29 -080024#include <media/AudioMixer.h>
Glenn Kastenfc7992b2012-08-29 11:10:32 -070025#include <media/ExtendedAudioBufferProvider.h>
26#include <media/nbaio/NBAIO.h>
Glenn Kasten8589ce72017-09-08 17:03:42 -070027#include <media/nblog/NBLog.h>
jiabine70bc7f2020-06-30 22:07:55 -070028#include <vibrator/ExternalVibrationUtils.h>
Glenn Kastenf7160b52014-03-18 17:01:15 -070029#include "FastThreadState.h"
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070030
31namespace android {
32
33struct FastMixerDumpState;
34
35class VolumeProvider {
36public:
Glenn Kastenc56f3422014-03-21 17:53:17 -070037 // The provider implementation is responsible for validating that the return value is in range.
38 virtual gain_minifloat_packed_t getVolumeLR() = 0;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070039protected:
40 VolumeProvider() { }
41 virtual ~VolumeProvider() { }
42};
43
44// Represents the state of a fast track
45struct FastTrack {
46 FastTrack();
47 /*virtual*/ ~FastTrack();
48
Glenn Kasten288ed212012-04-25 17:52:27 -070049 ExtendedAudioBufferProvider* mBufferProvider; // must be NULL if inactive, or non-NULL if active
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070050 VolumeProvider* mVolumeProvider; // optional; if NULL then full-scale
Glenn Kasten21e8c502012-04-12 09:39:42 -070051 audio_channel_mask_t mChannelMask; // AUDIO_CHANNEL_OUT_MONO or AUDIO_CHANNEL_OUT_STEREO
Andy Hunge8a1ced2014-05-09 15:02:21 -070052 audio_format_t mFormat; // track format
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070053 int mGeneration; // increment when any field is assigned
jiabin245cdd92018-12-07 17:55:15 -080054 bool mHapticPlaybackEnabled = false; // haptic playback is enabled or not
jiabine70bc7f2020-06-30 22:07:55 -070055 os::HapticScale mHapticIntensity = os::HapticScale::MUTE; // intensity of haptic data
Lais Andradebc3f37a2021-07-02 00:13:19 +010056 float mHapticMaxAmplitude = NAN; // max amplitude allowed for haptic data
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070057};
58
59// Represents a single state of the fast mixer
Glenn Kastenf7160b52014-03-18 17:01:15 -070060struct FastMixerState : FastThreadState {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070061 FastMixerState();
62 /*virtual*/ ~FastMixerState();
63
Glenn Kastendc2c50b2016-04-21 08:13:14 -070064 // These are the minimum, maximum, and default values for maximum number of fast tracks
65 static const unsigned kMinFastTracks = 2;
66 static const unsigned kMaxFastTracks = 32;
67 static const unsigned kDefaultFastTracks = 8;
68
69 static unsigned sMaxFastTracks; // Configured maximum number of fast tracks
70 static pthread_once_t sMaxFastTracksOnce; // Protects initializer for sMaxFastTracks
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070071
72 // all pointer fields use raw pointers; objects are owned and ref-counted by the normal mixer
73 FastTrack mFastTracks[kMaxFastTracks];
74 int mFastTracksGen; // increment when any mFastTracks[i].mGeneration is incremented
Glenn Kasten288ed212012-04-25 17:52:27 -070075 unsigned mTrackMask; // bit i is set if and only if mFastTracks[i] is active
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070076 NBAIO_Sink* mOutputSink; // HAL output device, must already be negotiated
77 int mOutputSinkGen; // increment when mOutputSink is assigned
78 size_t mFrameCount; // number of frames per fast mix buffer
jiabin245cdd92018-12-07 17:55:15 -080079 audio_channel_mask_t mSinkChannelMask; // If not AUDIO_CHANNEL_NONE, specifies sink channel
80 // mask when it cannot be directly calculated from
81 // channel count
Glenn Kastenf7160b52014-03-18 17:01:15 -070082
83 // Extends FastThreadState::Command
84 static const Command
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070085 // The following commands also process configuration changes, and can be "or"ed:
86 MIX = 0x8, // mix tracks
87 WRITE = 0x10, // write to output sink
Glenn Kastenf7160b52014-03-18 17:01:15 -070088 MIX_WRITE = 0x18; // mix tracks and write to output sink
89
Glenn Kastend702a562015-03-02 15:51:38 -080090 // never returns NULL; asserts if command is invalid
91 static const char *commandToString(Command command);
Glenn Kastendc2c50b2016-04-21 08:13:14 -070092
93 // initialize sMaxFastTracks
94 static void sMaxFastTracksInit();
95
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070096}; // struct FastMixerState
97
98} // namespace android
99
100#endif // ANDROID_AUDIO_FAST_MIXER_STATE_H