1 /*
2 **
3 ** Copyright 2007, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_AUDIO_MIXER_H
19 #define ANDROID_AUDIO_MIXER_H
20 
21 #include <pthread.h>
22 #include <stdint.h>
23 #include <sys/types.h>
24 
25 #include <media/AudioMixerBase.h>
26 #include <media/BufferProviders.h>
27 #include <utils/threads.h>
28 #include <vibrator/ExternalVibrationUtils.h>
29 
30 // FIXME This is actually unity gain, which might not be max in future, expressed in U.12
31 #define MAX_GAIN_INT AudioMixerBase::UNITY_GAIN_INT
32 
33 namespace android {
34 
35 // ----------------------------------------------------------------------------
36 
37 // AudioMixer extends AudioMixerBase by adding support for down- and up-mixing
38 // and time stretch that are implemented via Effects HAL, and adding support
39 // for haptic channels which depends on Vibrator service. This is the version
40 // that is used by Audioflinger.
41 
42 class AudioMixer : public AudioMixerBase
43 {
44 public:
45     // maximum number of channels supported for the content
46     static const uint32_t MAX_NUM_CHANNELS_TO_DOWNMIX = AUDIO_CHANNEL_COUNT_MAX;
47 
48     enum { // extension of AudioMixerBase parameters
49         DOWNMIX_TYPE    = 0x4004,
50         // for haptic
51         HAPTIC_ENABLED  = 0x4007, // Set haptic data from this track should be played or not.
52         HAPTIC_INTENSITY = 0x4008, // Set the intensity to play haptic data.
53         HAPTIC_MAX_AMPLITUDE = 0x4009, // Set the max amplitude allowed for haptic data.
54         // for target TIMESTRETCH
55         PLAYBACK_RATE   = 0x4300, // Configure timestretch on this track name;
56                                   // parameter 'value' is a pointer to the new playback rate.
57     };
58 
AudioMixer(size_t frameCount,uint32_t sampleRate)59     AudioMixer(size_t frameCount, uint32_t sampleRate)
60             : AudioMixerBase(frameCount, sampleRate) {
61         pthread_once(&sOnceControl, &sInitRoutine);
62     }
63 
64     bool isValidChannelMask(audio_channel_mask_t channelMask) const override;
65 
66     void setParameter(int name, int target, int param, void *value) override;
67     void setBufferProvider(int name, AudioBufferProvider* bufferProvider);
68 
69 private:
70 
71     struct Track : public TrackBase {
TrackTrack72         Track() : TrackBase() {}
73 
~TrackTrack74         ~Track()
75         {
76             // mInputBufferProvider need not be deleted.
77             // Ensure the order of destruction of buffer providers as they
78             // release the upstream provider in the destructor.
79             mTimestretchBufferProvider.reset(nullptr);
80             mPostDownmixReformatBufferProvider.reset(nullptr);
81             mDownmixerBufferProvider.reset(nullptr);
82             mReformatBufferProvider.reset(nullptr);
83             mAdjustChannelsBufferProvider.reset(nullptr);
84         }
85 
getOutputChannelCountTrack86         uint32_t getOutputChannelCount() override {
87             return mDownmixerBufferProvider.get() != nullptr ? mMixerChannelCount : channelCount;
88         }
getMixerChannelCountTrack89         uint32_t getMixerChannelCount() override {
90             return mMixerChannelCount + mMixerHapticChannelCount;
91         }
92 
93         status_t    prepareForDownmix();
94         void        unprepareForDownmix();
95         status_t    prepareForReformat();
96         void        unprepareForReformat();
97         status_t    prepareForAdjustChannels(size_t frames);
98         void        unprepareForAdjustChannels();
99         void        clearContractedBuffer();
100         bool        setPlaybackRate(const AudioPlaybackRate &playbackRate);
101         void        reconfigureBufferProviders();
102 
103         /* Buffer providers are constructed to translate the track input data as needed.
104          * See DownmixerBufferProvider below for how the Track buffer provider
105          * is wrapped by another one when dowmixing is required.
106          *
107          * TODO: perhaps make a single PlaybackConverterProvider class to move
108          * all pre-mixer track buffer conversions outside the AudioMixer class.
109          *
110          * 1) mInputBufferProvider: The AudioTrack buffer provider.
111          * 2) mAdjustChannelsBufferProvider: Expands or contracts sample data from one interleaved
112          *    channel format to another. Expanded channels are filled with zeros and put at the end
113          *    of each audio frame. Contracted channels are copied to the end of the buffer.
114          * 3) mReformatBufferProvider: If not NULL, performs the audio reformat to
115          *    match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer
116          *    requires reformat. For example, it may convert floating point input to
117          *    PCM_16_bit if that's required by the downmixer.
118          * 4) mDownmixerBufferProvider: If not NULL, performs the channel remixing to match
119          *    the number of channels required by the mixer sink.
120          * 5) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from
121          *    the downmixer requirements to the mixer engine input requirements.
122          * 6) mTimestretchBufferProvider: Adds timestretching for playback rate
123          */
124         AudioBufferProvider* mInputBufferProvider;    // externally provided buffer provider.
125         std::unique_ptr<PassthruBufferProvider> mAdjustChannelsBufferProvider;
126         std::unique_ptr<PassthruBufferProvider> mReformatBufferProvider;
127         std::unique_ptr<PassthruBufferProvider> mDownmixerBufferProvider;
128         std::unique_ptr<PassthruBufferProvider> mPostDownmixReformatBufferProvider;
129         std::unique_ptr<PassthruBufferProvider> mTimestretchBufferProvider;
130 
131         audio_format_t mDownmixRequiresFormat;  // required downmixer format
132                                                 // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary
133                                                 // AUDIO_FORMAT_INVALID if no required format
134 
135         AudioPlaybackRate    mPlaybackRate;
136 
137         // Haptic
138         bool                 mHapticPlaybackEnabled;
139         os::HapticScale      mHapticIntensity;
140         float                mHapticMaxAmplitude;
141         audio_channel_mask_t mHapticChannelMask;
142         uint32_t             mHapticChannelCount;
143         audio_channel_mask_t mMixerHapticChannelMask;
144         uint32_t             mMixerHapticChannelCount;
145         uint32_t             mAdjustInChannelCount;
146         uint32_t             mAdjustOutChannelCount;
147         bool                 mKeepContractedChannels;
148     };
149 
getTrack(int name)150     inline std::shared_ptr<Track> getTrack(int name) {
151         return std::static_pointer_cast<Track>(mTracks[name]);
152     }
153 
154     std::shared_ptr<TrackBase> preCreateTrack() override;
155     status_t postCreateTrack(TrackBase *track) override;
156 
157     void preProcess() override;
158     void postProcess() override;
159 
160     bool setChannelMasks(int name,
161             audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) override;
162 
163     static void sInitRoutine();
164 
165     static pthread_once_t sOnceControl; // initialized in constructor by first new
166 };
167 
168 // ----------------------------------------------------------------------------
169 } // namespace android
170 
171 #endif // ANDROID_AUDIO_MIXER_H
172