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 };
60 
61 // No virtuals.
62 static_assert(!std::is_polymorphic_v<FastTrack>);
63 
64 // Represents a single state of the fast mixer
65 struct FastMixerState : FastThreadState {
66     FastMixerState();
67 
68     // These are the minimum, maximum, and default values for maximum number of fast tracks
69     static constexpr unsigned kMinFastTracks = 2;
70     static constexpr unsigned kMaxFastTracks = 32;
71     static constexpr unsigned kDefaultFastTracks = 8;
72 
73     static unsigned sMaxFastTracks;             // Configured maximum number of fast tracks
74     static pthread_once_t sMaxFastTracksOnce;   // Protects initializer for sMaxFastTracks
75 
76     // all pointer fields use raw pointers; objects are owned and ref-counted by the normal mixer
77     FastTrack   mFastTracks[kMaxFastTracks];
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
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
87 
88     // Extends FastThreadState::Command
89     static const Command
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
93         MIX_WRITE = 0x18;       // mix tracks and write to output sink
94 
95     // never returns NULL; asserts if command is invalid
96     static const char *commandToString(Command command);
97 
98     // initialize sMaxFastTracks
99     static void sMaxFastTracksInit();
100 
101 };  // struct FastMixerState
102 
103 // No virtuals.
104 static_assert(!std::is_polymorphic_v<FastMixerState>);
105 
106 }   // namespace android
107