blob: c70e42a2dd9af9f869b5c99e35a1134df9030637 [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
Lais Andradebc3f37a2021-07-02 00:13:19 +010019#include <math.h>
Andy Hung4dcd7e92023-05-25 16:28:04 -070020#include <type_traits>
Lais Andradebc3f37a2021-07-02 00:13:19 +010021
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:
Andy Hunga9b9adc2023-06-01 12:17:59 -070040 VolumeProvider() = default;
Andy Hung4dcd7e92023-05-25 16:28:04 -070041 virtual ~VolumeProvider() = default;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070042};
43
44// Represents the state of a fast track
45struct FastTrack {
Andy Hungfd18f3c2023-05-26 16:24:28 -070046 // must be nullptr if inactive, or non-nullptr if active
47 ExtendedAudioBufferProvider* mBufferProvider = nullptr;
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070048
Andy Hungfd18f3c2023-05-26 16:24:28 -070049 // optional: if nullptr then full-scale
50 VolumeProvider* mVolumeProvider = nullptr;
51
52 // AUDIO_CHANNEL_OUT_MONO or AUDIO_CHANNEL_OUT_STEREO
53 audio_channel_mask_t mChannelMask = AUDIO_CHANNEL_OUT_STEREO;
54 audio_format_t mFormat = AUDIO_FORMAT_INVALID; // track format
55 int mGeneration = 0; // increment when any field is assigned
jiabin245cdd92018-12-07 17:55:15 -080056 bool mHapticPlaybackEnabled = false; // haptic playback is enabled or not
jiabine70bc7f2020-06-30 22:07:55 -070057 os::HapticScale mHapticIntensity = os::HapticScale::MUTE; // intensity of haptic data
Lais Andradebc3f37a2021-07-02 00:13:19 +010058 float mHapticMaxAmplitude = NAN; // max amplitude allowed for haptic data
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070059};
60
Andy Hung4dcd7e92023-05-25 16:28:04 -070061// No virtuals.
62static_assert(!std::is_polymorphic_v<FastTrack>);
63
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070064// Represents a single state of the fast mixer
Glenn Kastenf7160b52014-03-18 17:01:15 -070065struct FastMixerState : FastThreadState {
Andy Hunga9b9adc2023-06-01 12:17:59 -070066 FastMixerState();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070067
Glenn Kastendc2c50b2016-04-21 08:13:14 -070068 // These are the minimum, maximum, and default values for maximum number of fast tracks
Andy Hunga9b9adc2023-06-01 12:17:59 -070069 static constexpr unsigned kMinFastTracks = 2;
70 static constexpr unsigned kMaxFastTracks = 32;
71 static constexpr unsigned kDefaultFastTracks = 8;
Glenn Kastendc2c50b2016-04-21 08:13:14 -070072
73 static unsigned sMaxFastTracks; // Configured maximum number of fast tracks
74 static pthread_once_t sMaxFastTracksOnce; // Protects initializer for sMaxFastTracks
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070075
76 // all pointer fields use raw pointers; objects are owned and ref-counted by the normal mixer
77 FastTrack mFastTracks[kMaxFastTracks];
Andy Hungfd18f3c2023-05-26 16:24:28 -070078 int mFastTracksGen = 0; // increment when any
79 // mFastTracks[i].mGeneration is incremented
80 unsigned mTrackMask = 0; // bit i is set if and only if mFastTracks[i] is active
81 NBAIO_Sink* mOutputSink = nullptr; // HAL output device, must already be negotiated
82 int mOutputSinkGen = 0; // increment when mOutputSink is assigned
83 size_t mFrameCount = 0; // number of frames per fast mix buffer
jiabin245cdd92018-12-07 17:55:15 -080084 audio_channel_mask_t mSinkChannelMask; // If not AUDIO_CHANNEL_NONE, specifies sink channel
85 // mask when it cannot be directly calculated from
86 // channel count
Glenn Kastenf7160b52014-03-18 17:01:15 -070087
88 // Extends FastThreadState::Command
89 static const Command
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070090 // The following commands also process configuration changes, and can be "or"ed:
91 MIX = 0x8, // mix tracks
92 WRITE = 0x10, // write to output sink
Glenn Kastenf7160b52014-03-18 17:01:15 -070093 MIX_WRITE = 0x18; // mix tracks and write to output sink
94
Glenn Kastend702a562015-03-02 15:51:38 -080095 // never returns NULL; asserts if command is invalid
96 static const char *commandToString(Command command);
Glenn Kastendc2c50b2016-04-21 08:13:14 -070097
98 // initialize sMaxFastTracks
99 static void sMaxFastTracksInit();
100
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700101}; // struct FastMixerState
102
Andy Hung4dcd7e92023-05-25 16:28:04 -0700103// No virtuals.
104static_assert(!std::is_polymorphic_v<FastMixerState>);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700105
Andy Hung4dcd7e92023-05-25 16:28:04 -0700106} // namespace android