| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2015 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_BUFFER_PROVIDERS_H | 
 | 18 | #define ANDROID_BUFFER_PROVIDERS_H | 
 | 19 |  | 
 | 20 | #include <stdint.h> | 
 | 21 | #include <sys/types.h> | 
 | 22 |  | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 23 | #include <media/AudioBufferProvider.h> | 
 | 24 | #include <system/audio.h> | 
| Mikhail Naganov | 00260b5 | 2016-10-13 12:54:24 -0700 | [diff] [blame] | 25 | #include <system/audio_effect.h> | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 26 | #include <utils/StrongPointer.h> | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 27 |  | 
| Andy Hung | 068561c | 2017-01-03 17:09:32 -0800 | [diff] [blame^] | 28 | // external forward declaration from external/sonic/sonic.h | 
 | 29 | struct sonicStreamStruct; | 
 | 30 | typedef struct sonicStreamStruct *sonicStream; | 
 | 31 |  | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 32 | namespace android { | 
 | 33 |  | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 34 | class EffectBufferHalInterface; | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 35 | class EffectHalInterface; | 
 | 36 | class EffectsFactoryHalInterface; | 
 | 37 |  | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- | 
 | 39 |  | 
 | 40 | class PassthruBufferProvider : public AudioBufferProvider { | 
 | 41 | public: | 
 | 42 |     PassthruBufferProvider() : mTrackBufferProvider(NULL) { } | 
 | 43 |  | 
 | 44 |     virtual ~PassthruBufferProvider() { } | 
 | 45 |  | 
 | 46 |     // call this to release the buffer to the upstream provider. | 
 | 47 |     // treat it as an audio discontinuity for future samples. | 
 | 48 |     virtual void reset() { } | 
 | 49 |  | 
 | 50 |     // set the upstream buffer provider. Consider calling "reset" before this function. | 
 | 51 |     virtual void setBufferProvider(AudioBufferProvider *p) { | 
 | 52 |         mTrackBufferProvider = p; | 
 | 53 |     } | 
 | 54 |  | 
 | 55 | protected: | 
 | 56 |     AudioBufferProvider *mTrackBufferProvider; | 
 | 57 | }; | 
 | 58 |  | 
 | 59 | // Base AudioBufferProvider class used for DownMixerBufferProvider, RemixBufferProvider, | 
 | 60 | // and ReformatBufferProvider. | 
 | 61 | // It handles a private buffer for use in converting format or channel masks from the | 
 | 62 | // input data to a form acceptable by the mixer. | 
 | 63 | // TODO: Make a ResamplerBufferProvider when integers are entirely removed from the | 
 | 64 | // processing pipeline. | 
 | 65 | class CopyBufferProvider : public PassthruBufferProvider { | 
 | 66 | public: | 
 | 67 |     // Use a private buffer of bufferFrameCount frames (each frame is outputFrameSize bytes). | 
 | 68 |     // If bufferFrameCount is 0, no private buffer is created and in-place modification of | 
 | 69 |     // the upstream buffer provider's buffers is performed by copyFrames(). | 
 | 70 |     CopyBufferProvider(size_t inputFrameSize, size_t outputFrameSize, | 
 | 71 |             size_t bufferFrameCount); | 
 | 72 |     virtual ~CopyBufferProvider(); | 
 | 73 |  | 
 | 74 |     // Overrides AudioBufferProvider methods | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 75 |     virtual status_t getNextBuffer(Buffer *buffer); | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 76 |     virtual void releaseBuffer(Buffer *buffer); | 
 | 77 |  | 
 | 78 |     // Overrides PassthruBufferProvider | 
 | 79 |     virtual void reset(); | 
 | 80 |  | 
 | 81 |     // this function should be supplied by the derived class.  It converts | 
 | 82 |     // #frames in the *src pointer to the *dst pointer.  It is public because | 
 | 83 |     // some providers will allow this to work on arbitrary buffers outside | 
 | 84 |     // of the internal buffers. | 
 | 85 |     virtual void copyFrames(void *dst, const void *src, size_t frames) = 0; | 
 | 86 |  | 
 | 87 | protected: | 
 | 88 |     const size_t         mInputFrameSize; | 
 | 89 |     const size_t         mOutputFrameSize; | 
 | 90 | private: | 
 | 91 |     AudioBufferProvider::Buffer mBuffer; | 
 | 92 |     const size_t         mLocalBufferFrameCount; | 
 | 93 |     void                *mLocalBufferData; | 
 | 94 |     size_t               mConsumed; | 
 | 95 | }; | 
 | 96 |  | 
 | 97 | // DownmixerBufferProvider derives from CopyBufferProvider to provide | 
 | 98 | // position dependent downmixing by an Audio Effect. | 
 | 99 | class DownmixerBufferProvider : public CopyBufferProvider { | 
 | 100 | public: | 
 | 101 |     DownmixerBufferProvider(audio_channel_mask_t inputChannelMask, | 
 | 102 |             audio_channel_mask_t outputChannelMask, audio_format_t format, | 
 | 103 |             uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount); | 
 | 104 |     virtual ~DownmixerBufferProvider(); | 
 | 105 |     //Overrides | 
 | 106 |     virtual void copyFrames(void *dst, const void *src, size_t frames); | 
 | 107 |  | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 108 |     bool isValid() const { return mDownmixInterface.get() != NULL; } | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 109 |     static status_t init(); | 
 | 110 |     static bool isMultichannelCapable() { return sIsMultichannelCapable; } | 
 | 111 |  | 
 | 112 | protected: | 
| Mikhail Naganov | 4a3d5c2 | 2016-08-15 13:47:42 -0700 | [diff] [blame] | 113 |     sp<EffectsFactoryHalInterface> mEffectsFactory; | 
 | 114 |     sp<EffectHalInterface> mDownmixInterface; | 
| Mikhail Naganov | 022b995 | 2017-01-04 16:36:51 -0800 | [diff] [blame] | 115 |     sp<EffectBufferHalInterface> mInBuffer; | 
 | 116 |     sp<EffectBufferHalInterface> mOutBuffer; | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 117 |     effect_config_t    mDownmixConfig; | 
 | 118 |  | 
 | 119 |     // effect descriptor for the downmixer used by the mixer | 
 | 120 |     static effect_descriptor_t sDwnmFxDesc; | 
 | 121 |     // indicates whether a downmix effect has been found and is usable by this mixer | 
 | 122 |     static bool                sIsMultichannelCapable; | 
 | 123 |     // FIXME: should we allow effects outside of the framework? | 
 | 124 |     // We need to here. A special ioId that must be <= -2 so it does not map to a session. | 
 | 125 |     static const int32_t SESSION_ID_INVALID_AND_IGNORED = -2; | 
 | 126 | }; | 
 | 127 |  | 
 | 128 | // RemixBufferProvider derives from CopyBufferProvider to perform an | 
 | 129 | // upmix or downmix to the proper channel count and mask. | 
 | 130 | class RemixBufferProvider : public CopyBufferProvider { | 
 | 131 | public: | 
 | 132 |     RemixBufferProvider(audio_channel_mask_t inputChannelMask, | 
 | 133 |             audio_channel_mask_t outputChannelMask, audio_format_t format, | 
 | 134 |             size_t bufferFrameCount); | 
 | 135 |     //Overrides | 
 | 136 |     virtual void copyFrames(void *dst, const void *src, size_t frames); | 
 | 137 |  | 
 | 138 | protected: | 
 | 139 |     const audio_format_t mFormat; | 
 | 140 |     const size_t         mSampleSize; | 
 | 141 |     const size_t         mInputChannels; | 
 | 142 |     const size_t         mOutputChannels; | 
 | 143 |     int8_t               mIdxAry[sizeof(uint32_t) * 8]; // 32 bits => channel indices | 
 | 144 | }; | 
 | 145 |  | 
 | 146 | // ReformatBufferProvider derives from CopyBufferProvider to convert the input data | 
 | 147 | // to an acceptable mixer input format type. | 
 | 148 | class ReformatBufferProvider : public CopyBufferProvider { | 
 | 149 | public: | 
 | 150 |     ReformatBufferProvider(int32_t channelCount, | 
 | 151 |             audio_format_t inputFormat, audio_format_t outputFormat, | 
 | 152 |             size_t bufferFrameCount); | 
 | 153 |     virtual void copyFrames(void *dst, const void *src, size_t frames); | 
 | 154 |  | 
 | 155 | protected: | 
 | 156 |     const uint32_t       mChannelCount; | 
 | 157 |     const audio_format_t mInputFormat; | 
 | 158 |     const audio_format_t mOutputFormat; | 
 | 159 | }; | 
 | 160 |  | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 161 | // TimestretchBufferProvider derives from PassthruBufferProvider for time stretching | 
 | 162 | class TimestretchBufferProvider : public PassthruBufferProvider { | 
 | 163 | public: | 
 | 164 |     TimestretchBufferProvider(int32_t channelCount, | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 165 |             audio_format_t format, uint32_t sampleRate, | 
 | 166 |             const AudioPlaybackRate &playbackRate); | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 167 |     virtual ~TimestretchBufferProvider(); | 
 | 168 |  | 
 | 169 |     // Overrides AudioBufferProvider methods | 
| Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 170 |     virtual status_t getNextBuffer(Buffer* buffer); | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 171 |     virtual void releaseBuffer(Buffer* buffer); | 
 | 172 |  | 
 | 173 |     // Overrides PassthruBufferProvider | 
 | 174 |     virtual void reset(); | 
 | 175 |  | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 176 |     virtual status_t setPlaybackRate(const AudioPlaybackRate &playbackRate); | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 177 |  | 
 | 178 |     // processes frames | 
 | 179 |     // dstBuffer is where to place the data | 
 | 180 |     // dstFrames [in/out] is the desired frames (return with actual placed in buffer) | 
 | 181 |     // srcBuffer is the source data | 
 | 182 |     // srcFrames [in/out] is the available source frames (return with consumed) | 
 | 183 |     virtual void processFrames(void *dstBuffer, size_t *dstFrames, | 
 | 184 |             const void *srcBuffer, size_t *srcFrames); | 
 | 185 |  | 
 | 186 | protected: | 
 | 187 |     const uint32_t       mChannelCount; | 
 | 188 |     const audio_format_t mFormat; | 
 | 189 |     const uint32_t       mSampleRate; // const for now (TODO change this) | 
 | 190 |     const size_t         mFrameSize; | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 191 |     AudioPlaybackRate    mPlaybackRate; | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 192 |  | 
 | 193 | private: | 
| Ricardo Garcia | 5a8a95d | 2015-04-18 14:47:04 -0700 | [diff] [blame] | 194 |     AudioBufferProvider::Buffer mBuffer;          // for upstream request | 
 | 195 |     size_t               mLocalBufferFrameCount;  // size of local buffer | 
 | 196 |     void                *mLocalBufferData;        // internally allocated buffer for data returned | 
 | 197 |                                                   // to caller | 
 | 198 |     size_t               mRemaining;              // remaining data in local buffer | 
 | 199 |     sonicStream          mSonicStream;            // handle to sonic timestretch object | 
 | 200 |     //FIXME: this dependency should be abstracted out | 
 | 201 |     bool                 mFallbackFailErrorShown; // log fallback error only once | 
| Ricardo Garcia | 6c7f062 | 2015-04-30 18:39:16 -0700 | [diff] [blame] | 202 |     bool                 mAudioPlaybackRateValid; // flag for current parameters validity | 
| Andy Hung | c5656cc | 2015-03-26 19:04:33 -0700 | [diff] [blame] | 203 | }; | 
 | 204 |  | 
| Andy Hung | 857d5a2 | 2015-03-26 18:46:00 -0700 | [diff] [blame] | 205 | // ---------------------------------------------------------------------------- | 
 | 206 | } // namespace android | 
 | 207 |  | 
 | 208 | #endif // ANDROID_BUFFER_PROVIDERS_H |