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 #ifndef ANDROID_AUDIO_FAST_MIXER_STATE_H
18 #define ANDROID_AUDIO_FAST_MIXER_STATE_H
19 
20 #include <math.h>
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() = 0;
39 protected:
VolumeProvider()40     VolumeProvider() { }
~VolumeProvider()41     virtual ~VolumeProvider() { }
42 };
43 
44 // Represents the state of a fast track
45 struct FastTrack {
46     FastTrack();
47     /*virtual*/ ~FastTrack();
48 
49     ExtendedAudioBufferProvider* mBufferProvider; // must be NULL if inactive, or non-NULL if active
50     VolumeProvider*         mVolumeProvider; // optional; if NULL then full-scale
51     audio_channel_mask_t    mChannelMask;    // AUDIO_CHANNEL_OUT_MONO or AUDIO_CHANNEL_OUT_STEREO
52     audio_format_t          mFormat;         // track format
53     int                     mGeneration;     // increment when any field is assigned
54     bool                    mHapticPlaybackEnabled = false; // haptic playback is enabled or not
55     os::HapticScale         mHapticIntensity = os::HapticScale::MUTE; // intensity of haptic data
56     float                   mHapticMaxAmplitude = NAN; // max amplitude allowed for haptic data
57 };
58 
59 // Represents a single state of the fast mixer
60 struct FastMixerState : FastThreadState {
61                 FastMixerState();
62     /*virtual*/ ~FastMixerState();
63 
64     // These are the minimum, maximum, and default values for maximum number of fast tracks
65     static const unsigned kMinFastTracks = 2;
66     static const unsigned kMaxFastTracks = 32;
67     static const unsigned kDefaultFastTracks = 8;
68 
69     static unsigned sMaxFastTracks;             // Configured maximum number of fast tracks
70     static pthread_once_t sMaxFastTracksOnce;   // Protects initializer for sMaxFastTracks
71 
72     // all pointer fields use raw pointers; objects are owned and ref-counted by the normal mixer
73     FastTrack   mFastTracks[kMaxFastTracks];
74     int         mFastTracksGen; // increment when any mFastTracks[i].mGeneration is incremented
75     unsigned    mTrackMask;     // bit i is set if and only if mFastTracks[i] is active
76     NBAIO_Sink* mOutputSink;    // HAL output device, must already be negotiated
77     int         mOutputSinkGen; // increment when mOutputSink is assigned
78     size_t      mFrameCount;    // number of frames per fast mix buffer
79     audio_channel_mask_t mSinkChannelMask; // If not AUDIO_CHANNEL_NONE, specifies sink channel
80                                            // mask when it cannot be directly calculated from
81                                            // channel count
82 
83     // Extends FastThreadState::Command
84     static const Command
85         // The following commands also process configuration changes, and can be "or"ed:
86         MIX = 0x8,              // mix tracks
87         WRITE = 0x10,           // write to output sink
88         MIX_WRITE = 0x18;       // mix tracks and write to output sink
89 
90     // never returns NULL; asserts if command is invalid
91     static const char *commandToString(Command command);
92 
93     // initialize sMaxFastTracks
94     static void sMaxFastTracksInit();
95 
96 };  // struct FastMixerState
97 
98 }   // namespace android
99 
100 #endif  // ANDROID_AUDIO_FAST_MIXER_STATE_H
101