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 17 #pragma once 18 19 #include <math.h> 20 #include <type_traits> 21 22 #include <audio_utils/minifloat.h> 23 #include <system/audio.h> 24 #include <media/AudioMixer.h> 25 #include <media/ExtendedAudioBufferProvider.h> 26 #include <media/nbaio/NBAIO.h> 27 #include <media/nblog/NBLog.h> 28 #include <vibrator/ExternalVibrationUtils.h> 29 #include "FastThreadState.h" 30 31 namespace android { 32 33 struct FastMixerDumpState; 34 35 class VolumeProvider { 36 public: 37 // The provider implementation is responsible for validating that the return value is in range. 38 virtual gain_minifloat_packed_t getVolumeLR() const = 0; 39 protected: 40 VolumeProvider() = default; 41 virtual ~VolumeProvider() = default; 42 }; 43 44 // Represents the state of a fast track 45 struct FastTrack { 46 // must be nullptr if inactive, or non-nullptr if active 47 ExtendedAudioBufferProvider* mBufferProvider = nullptr; 48 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 56 bool mHapticPlaybackEnabled = false; // haptic playback is enabled or not 57 os::HapticScale mHapticScale = os::HapticScale::mute(); // scale of haptic data 58 float mHapticMaxAmplitude = NAN; // max amplitude allowed for haptic data 59 char mTraceName[32]{}; 60 }; 61 62 // No virtuals. 63 static_assert(!std::is_polymorphic_v<FastTrack>); 64 65 // Represents a single state of the fast mixer 66 struct FastMixerState : FastThreadState { 67 FastMixerState(); 68 69 // These are the minimum, maximum, and default values for maximum number of fast tracks 70 static constexpr unsigned kMinFastTracks = 2; 71 static constexpr unsigned kMaxFastTracks = 32; 72 static constexpr unsigned kDefaultFastTracks = 8; 73 74 static unsigned sMaxFastTracks; // Configured maximum number of fast tracks 75 static pthread_once_t sMaxFastTracksOnce; // Protects initializer for sMaxFastTracks 76 77 // all pointer fields use raw pointers; objects are owned and ref-counted by the normal mixer 78 FastTrack mFastTracks[kMaxFastTracks]; 79 int mFastTracksGen = 0; // increment when any 80 // mFastTracks[i].mGeneration is incremented 81 unsigned mTrackMask = 0; // bit i is set if and only if mFastTracks[i] is active 82 NBAIO_Sink* mOutputSink = nullptr; // HAL output device, must already be negotiated 83 int mOutputSinkGen = 0; // increment when mOutputSink is assigned 84 size_t mFrameCount = 0; // number of frames per fast mix buffer 85 audio_channel_mask_t mSinkChannelMask; // If not AUDIO_CHANNEL_NONE, specifies sink channel 86 // mask when it cannot be directly calculated from 87 // channel count 88 89 // Extends FastThreadState::Command 90 static const Command 91 // The following commands also process configuration changes, and can be "or"ed: 92 MIX = 0x8, // mix tracks 93 WRITE = 0x10, // write to output sink 94 MIX_WRITE = 0x18; // mix tracks and write to output sink 95 96 // never returns NULL; asserts if command is invalid 97 static const char *commandToString(Command command); 98 99 // initialize sMaxFastTracks 100 static void sMaxFastTracksInit(); 101 102 }; // struct FastMixerState 103 104 // No virtuals. 105 static_assert(!std::is_polymorphic_v<FastMixerState>); 106 107 } // namespace android 108