Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 1 | /* |
| 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 Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 17 | #pragma once |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 18 | |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 19 | #include <math.h> |
Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 20 | #include <type_traits> |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 21 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 22 | #include <audio_utils/minifloat.h> |
Glenn Kasten | 21e8c50 | 2012-04-12 09:39:42 -0700 | [diff] [blame] | 23 | #include <system/audio.h> |
jiabin | 77270b8 | 2018-12-18 15:41:29 -0800 | [diff] [blame] | 24 | #include <media/AudioMixer.h> |
Glenn Kasten | fc7992b | 2012-08-29 11:10:32 -0700 | [diff] [blame] | 25 | #include <media/ExtendedAudioBufferProvider.h> |
| 26 | #include <media/nbaio/NBAIO.h> |
Glenn Kasten | 8589ce7 | 2017-09-08 17:03:42 -0700 | [diff] [blame] | 27 | #include <media/nblog/NBLog.h> |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 28 | #include <vibrator/ExternalVibrationUtils.h> |
Glenn Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 29 | #include "FastThreadState.h" |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | struct FastMixerDumpState; |
| 34 | |
| 35 | class VolumeProvider { |
| 36 | public: |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 37 | // The provider implementation is responsible for validating that the return value is in range. |
| 38 | virtual gain_minifloat_packed_t getVolumeLR() = 0; |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 39 | protected: |
| 40 | VolumeProvider() { } |
Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 41 | virtual ~VolumeProvider() = default; |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | // Represents the state of a fast track |
| 45 | struct FastTrack { |
Andy Hung | fd18f3c | 2023-05-26 16:24:28 -0700 | [diff] [blame^] | 46 | // must be nullptr if inactive, or non-nullptr if active |
| 47 | ExtendedAudioBufferProvider* mBufferProvider = nullptr; |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 48 | |
Andy Hung | fd18f3c | 2023-05-26 16:24:28 -0700 | [diff] [blame^] | 49 | // 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 |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 56 | bool mHapticPlaybackEnabled = false; // haptic playback is enabled or not |
jiabin | e70bc7f | 2020-06-30 22:07:55 -0700 | [diff] [blame] | 57 | os::HapticScale mHapticIntensity = os::HapticScale::MUTE; // intensity of haptic data |
Lais Andrade | bc3f37a | 2021-07-02 00:13:19 +0100 | [diff] [blame] | 58 | float mHapticMaxAmplitude = NAN; // max amplitude allowed for haptic data |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 61 | // No virtuals. |
| 62 | static_assert(!std::is_polymorphic_v<FastTrack>); |
| 63 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 64 | // Represents a single state of the fast mixer |
Glenn Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 65 | struct FastMixerState : FastThreadState { |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 66 | FastMixerState(); |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 67 | |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 68 | // These are the minimum, maximum, and default values for maximum number of fast tracks |
| 69 | static const unsigned kMinFastTracks = 2; |
| 70 | static const unsigned kMaxFastTracks = 32; |
| 71 | static const unsigned kDefaultFastTracks = 8; |
| 72 | |
| 73 | static unsigned sMaxFastTracks; // Configured maximum number of fast tracks |
| 74 | static pthread_once_t sMaxFastTracksOnce; // Protects initializer for sMaxFastTracks |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 75 | |
| 76 | // all pointer fields use raw pointers; objects are owned and ref-counted by the normal mixer |
| 77 | FastTrack mFastTracks[kMaxFastTracks]; |
Andy Hung | fd18f3c | 2023-05-26 16:24:28 -0700 | [diff] [blame^] | 78 | 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 |
jiabin | 245cdd9 | 2018-12-07 17:55:15 -0800 | [diff] [blame] | 84 | 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 Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 87 | |
| 88 | // Extends FastThreadState::Command |
| 89 | static const Command |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 90 | // 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 Kasten | f7160b5 | 2014-03-18 17:01:15 -0700 | [diff] [blame] | 93 | MIX_WRITE = 0x18; // mix tracks and write to output sink |
| 94 | |
Glenn Kasten | d702a56 | 2015-03-02 15:51:38 -0800 | [diff] [blame] | 95 | // never returns NULL; asserts if command is invalid |
| 96 | static const char *commandToString(Command command); |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 97 | |
| 98 | // initialize sMaxFastTracks |
| 99 | static void sMaxFastTracksInit(); |
| 100 | |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 101 | }; // struct FastMixerState |
| 102 | |
Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 103 | // No virtuals. |
| 104 | static_assert(!std::is_polymorphic_v<FastMixerState>); |
Glenn Kasten | 97b5d0d | 2012-03-23 18:54:19 -0700 | [diff] [blame] | 105 | |
Andy Hung | 4dcd7e9 | 2023-05-25 16:28:04 -0700 | [diff] [blame] | 106 | } // namespace android |