blob: f40f61258cc800ac45159fb79d619e83dc3d5361 [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:
40 VolumeProvider() { }
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 {
46 FastTrack();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070047
Glenn Kasten288ed212012-04-25 17:52:27 -070048 ExtendedAudioBufferProvider* mBufferProvider; // must be NULL if inactive, or non-NULL if active
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070049 VolumeProvider* mVolumeProvider; // optional; if NULL then full-scale
Glenn Kasten21e8c502012-04-12 09:39:42 -070050 audio_channel_mask_t mChannelMask; // AUDIO_CHANNEL_OUT_MONO or AUDIO_CHANNEL_OUT_STEREO
Andy Hunge8a1ced2014-05-09 15:02:21 -070051 audio_format_t mFormat; // track format
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070052 int mGeneration; // increment when any field is assigned
jiabin245cdd92018-12-07 17:55:15 -080053 bool mHapticPlaybackEnabled = false; // haptic playback is enabled or not
jiabine70bc7f2020-06-30 22:07:55 -070054 os::HapticScale mHapticIntensity = os::HapticScale::MUTE; // intensity of haptic data
Lais Andradebc3f37a2021-07-02 00:13:19 +010055 float mHapticMaxAmplitude = NAN; // max amplitude allowed for haptic data
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070056};
57
Andy Hung4dcd7e92023-05-25 16:28:04 -070058// No virtuals.
59static_assert(!std::is_polymorphic_v<FastTrack>);
60
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070061// Represents a single state of the fast mixer
Glenn Kastenf7160b52014-03-18 17:01:15 -070062struct FastMixerState : FastThreadState {
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070063 FastMixerState();
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070064
Glenn Kastendc2c50b2016-04-21 08:13:14 -070065 // These are the minimum, maximum, and default values for maximum number of fast tracks
66 static const unsigned kMinFastTracks = 2;
67 static const unsigned kMaxFastTracks = 32;
68 static const unsigned kDefaultFastTracks = 8;
69
70 static unsigned sMaxFastTracks; // Configured maximum number of fast tracks
71 static pthread_once_t sMaxFastTracksOnce; // Protects initializer for sMaxFastTracks
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070072
73 // all pointer fields use raw pointers; objects are owned and ref-counted by the normal mixer
74 FastTrack mFastTracks[kMaxFastTracks];
75 int mFastTracksGen; // increment when any mFastTracks[i].mGeneration is incremented
Glenn Kasten288ed212012-04-25 17:52:27 -070076 unsigned mTrackMask; // bit i is set if and only if mFastTracks[i] is active
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070077 NBAIO_Sink* mOutputSink; // HAL output device, must already be negotiated
78 int mOutputSinkGen; // increment when mOutputSink is assigned
79 size_t mFrameCount; // number of frames per fast mix buffer
jiabin245cdd92018-12-07 17:55:15 -080080 audio_channel_mask_t mSinkChannelMask; // If not AUDIO_CHANNEL_NONE, specifies sink channel
81 // mask when it cannot be directly calculated from
82 // channel count
Glenn Kastenf7160b52014-03-18 17:01:15 -070083
84 // Extends FastThreadState::Command
85 static const Command
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070086 // The following commands also process configuration changes, and can be "or"ed:
87 MIX = 0x8, // mix tracks
88 WRITE = 0x10, // write to output sink
Glenn Kastenf7160b52014-03-18 17:01:15 -070089 MIX_WRITE = 0x18; // mix tracks and write to output sink
90
Glenn Kastend702a562015-03-02 15:51:38 -080091 // never returns NULL; asserts if command is invalid
92 static const char *commandToString(Command command);
Glenn Kastendc2c50b2016-04-21 08:13:14 -070093
94 // initialize sMaxFastTracks
95 static void sMaxFastTracksInit();
96
Glenn Kasten97b5d0d2012-03-23 18:54:19 -070097}; // struct FastMixerState
98
Andy Hung4dcd7e92023-05-25 16:28:04 -070099// No virtuals.
100static_assert(!std::is_polymorphic_v<FastMixerState>);
Glenn Kasten97b5d0d2012-03-23 18:54:19 -0700101
Andy Hung4dcd7e92023-05-25 16:28:04 -0700102} // namespace android