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 // for target TIMESTRETCH 54 PLAYBACK_RATE = 0x4300, // Configure timestretch on this track name; 55 // parameter 'value' is a pointer to the new playback rate. 56 }; 57 AudioMixer(size_t frameCount,uint32_t sampleRate)58 AudioMixer(size_t frameCount, uint32_t sampleRate) 59 : AudioMixerBase(frameCount, sampleRate) { 60 pthread_once(&sOnceControl, &sInitRoutine); 61 } 62 63 bool isValidChannelMask(audio_channel_mask_t channelMask) const override; 64 65 void setParameter(int name, int target, int param, void *value) override; 66 void setBufferProvider(int name, AudioBufferProvider* bufferProvider); 67 68 private: 69 70 struct Track : public TrackBase { TrackTrack71 Track() : TrackBase() {} 72 ~TrackTrack73 ~Track() 74 { 75 // mInputBufferProvider need not be deleted. 76 // Ensure the order of destruction of buffer providers as they 77 // release the upstream provider in the destructor. 78 mTimestretchBufferProvider.reset(nullptr); 79 mPostDownmixReformatBufferProvider.reset(nullptr); 80 mDownmixerBufferProvider.reset(nullptr); 81 mReformatBufferProvider.reset(nullptr); 82 mContractChannelsNonDestructiveBufferProvider.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(); 98 void unprepareForAdjustChannels(); 99 status_t prepareForAdjustChannelsNonDestructive(size_t frames); 100 void unprepareForAdjustChannelsNonDestructive(); 101 void clearContractedBuffer(); 102 bool setPlaybackRate(const AudioPlaybackRate &playbackRate); 103 void reconfigureBufferProviders(); 104 105 /* Buffer providers are constructed to translate the track input data as needed. 106 * See DownmixerBufferProvider below for how the Track buffer provider 107 * is wrapped by another one when dowmixing is required. 108 * 109 * TODO: perhaps make a single PlaybackConverterProvider class to move 110 * all pre-mixer track buffer conversions outside the AudioMixer class. 111 * 112 * 1) mInputBufferProvider: The AudioTrack buffer provider. 113 * 2) mAdjustChannelsBufferProvider: Expands or contracts sample data from one interleaved 114 * channel format to another. Expanded channels are filled with zeros and put at the end 115 * of each audio frame. Contracted channels are copied to the end of the buffer. 116 * 3) mContractChannelsNonDestructiveBufferProvider: Non-destructively contract sample data. 117 * This is currently using at audio-haptic coupled playback to separate audio and haptic 118 * data. Contracted channels could be written to given buffer. 119 * 4) mReformatBufferProvider: If not NULL, performs the audio reformat to 120 * match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer 121 * requires reformat. For example, it may convert floating point input to 122 * PCM_16_bit if that's required by the downmixer. 123 * 5) mDownmixerBufferProvider: If not NULL, performs the channel remixing to match 124 * the number of channels required by the mixer sink. 125 * 6) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from 126 * the downmixer requirements to the mixer engine input requirements. 127 * 7) mTimestretchBufferProvider: Adds timestretching for playback rate 128 */ 129 AudioBufferProvider* mInputBufferProvider; // externally provided buffer provider. 130 // TODO: combine mAdjustChannelsBufferProvider and 131 // mContractChannelsNonDestructiveBufferProvider 132 std::unique_ptr<PassthruBufferProvider> mAdjustChannelsBufferProvider; 133 std::unique_ptr<PassthruBufferProvider> mContractChannelsNonDestructiveBufferProvider; 134 std::unique_ptr<PassthruBufferProvider> mReformatBufferProvider; 135 std::unique_ptr<PassthruBufferProvider> mDownmixerBufferProvider; 136 std::unique_ptr<PassthruBufferProvider> mPostDownmixReformatBufferProvider; 137 std::unique_ptr<PassthruBufferProvider> mTimestretchBufferProvider; 138 139 audio_format_t mDownmixRequiresFormat; // required downmixer format 140 // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary 141 // AUDIO_FORMAT_INVALID if no required format 142 143 AudioPlaybackRate mPlaybackRate; 144 145 // Haptic 146 bool mHapticPlaybackEnabled; 147 os::HapticScale mHapticIntensity; 148 audio_channel_mask_t mHapticChannelMask; 149 uint32_t mHapticChannelCount; 150 audio_channel_mask_t mMixerHapticChannelMask; 151 uint32_t mMixerHapticChannelCount; 152 uint32_t mAdjustInChannelCount; 153 uint32_t mAdjustOutChannelCount; 154 uint32_t mAdjustNonDestructiveInChannelCount; 155 uint32_t mAdjustNonDestructiveOutChannelCount; 156 bool mKeepContractedChannels; 157 }; 158 getTrack(int name)159 inline std::shared_ptr<Track> getTrack(int name) { 160 return std::static_pointer_cast<Track>(mTracks[name]); 161 } 162 163 std::shared_ptr<TrackBase> preCreateTrack() override; 164 status_t postCreateTrack(TrackBase *track) override; 165 166 void preProcess() override; 167 void postProcess() override; 168 169 bool setChannelMasks(int name, 170 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) override; 171 172 static void sInitRoutine(); 173 174 static pthread_once_t sOnceControl; // initialized in constructor by first new 175 }; 176 177 // ---------------------------------------------------------------------------- 178 } // namespace android 179 180 #endif // ANDROID_AUDIO_MIXER_H 181