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_SCALE = 0x4008, // Set the scale 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 unprepareForTee(); 100 status_t prepareForTee(); 101 void clearContractedBuffer(); 102 void clearTeeFrameCopied(); 103 bool setPlaybackRate(const AudioPlaybackRate &playbackRate); 104 void reconfigureBufferProviders(); 105 106 /* Buffer providers are constructed to translate the track input data as needed. 107 * See DownmixerBufferProvider below for how the Track buffer provider 108 * is wrapped by another one when dowmixing is required. 109 * 110 * TODO: perhaps make a single PlaybackConverterProvider class to move 111 * all pre-mixer track buffer conversions outside the AudioMixer class. 112 * 113 * 1) mInputBufferProvider: The AudioTrack buffer provider. 114 * 2) mTeeBufferProvider: If not NULL, copy the data to tee buffer. 115 * 3) mAdjustChannelsBufferProvider: Expands or contracts sample data from one interleaved 116 * channel format to another. Expanded channels are filled with zeros and put at the end 117 * of each audio frame. Contracted channels are copied to the end of the buffer. 118 * 4) mReformatBufferProvider: If not NULL, performs the audio reformat to 119 * match either mMixerInFormat or mDownmixRequiresFormat, if the downmixer 120 * requires reformat. For example, it may convert floating point input to 121 * PCM_16_bit if that's required by the downmixer. 122 * 5) mDownmixerBufferProvider: If not NULL, performs the channel remixing to match 123 * the number of channels required by the mixer sink. 124 * 6) mPostDownmixReformatBufferProvider: If not NULL, performs reformatting from 125 * the downmixer requirements to the mixer engine input requirements. 126 * 7) mTimestretchBufferProvider: Adds timestretching for playback rate 127 */ 128 AudioBufferProvider* mInputBufferProvider; // externally provided buffer provider. 129 std::unique_ptr<PassthruBufferProvider> mTeeBufferProvider; 130 std::unique_ptr<PassthruBufferProvider> mAdjustChannelsBufferProvider; 131 std::unique_ptr<PassthruBufferProvider> mReformatBufferProvider; 132 std::unique_ptr<PassthruBufferProvider> mDownmixerBufferProvider; 133 std::unique_ptr<PassthruBufferProvider> mPostDownmixReformatBufferProvider; 134 std::unique_ptr<PassthruBufferProvider> mTimestretchBufferProvider; 135 136 audio_format_t mDownmixRequiresFormat; // required downmixer format 137 // AUDIO_FORMAT_PCM_16_BIT if 16 bit necessary 138 // AUDIO_FORMAT_INVALID if no required format 139 140 AudioPlaybackRate mPlaybackRate; 141 142 // Haptic 143 bool mHapticPlaybackEnabled; 144 os::HapticScale mHapticScale; 145 float mHapticMaxAmplitude; 146 audio_channel_mask_t mHapticChannelMask; 147 uint32_t mHapticChannelCount; 148 audio_channel_mask_t mMixerHapticChannelMask; 149 uint32_t mMixerHapticChannelCount; 150 uint32_t mAdjustInChannelCount; 151 uint32_t mAdjustOutChannelCount; 152 bool mKeepContractedChannels; 153 }; 154 getTrack(int name)155 inline std::shared_ptr<Track> getTrack(int name) { 156 return std::static_pointer_cast<Track>(mTracks[name]); 157 } 158 159 std::shared_ptr<TrackBase> preCreateTrack() override; 160 status_t postCreateTrack(TrackBase *track) override; 161 162 void preProcess() override; 163 void postProcess() override; 164 165 bool setChannelMasks(int name, 166 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) override; 167 168 static void sInitRoutine(); 169 170 static pthread_once_t sOnceControl; // initialized in constructor by first new 171 }; 172 173 // ---------------------------------------------------------------------------- 174 } // namespace android 175 176 #endif // ANDROID_AUDIO_MIXER_H 177