• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include <media/AudioBufferProvider.h>
24 #include <media/AudioResamplerPublic.h>
25 #include <system/audio.h>
26 #include <system/audio_effect.h>
27 #include <utils/StrongPointer.h>
28 
29 // external forward declaration from external/sonic/sonic.h
30 struct sonicStreamStruct;
31 typedef struct sonicStreamStruct *sonicStream;
32 
33 namespace android {
34 
35 class EffectBufferHalInterface;
36 class EffectHalInterface;
37 class EffectsFactoryHalInterface;
38 
39 // ----------------------------------------------------------------------------
40 
41 class PassthruBufferProvider : public AudioBufferProvider {
42 public:
PassthruBufferProvider()43     PassthruBufferProvider() : mTrackBufferProvider(NULL) { }
44 
~PassthruBufferProvider()45     virtual ~PassthruBufferProvider() { }
46 
47     // call this to release the buffer to the upstream provider.
48     // treat it as an audio discontinuity for future samples.
reset()49     virtual void reset() { }
50 
51     // set the upstream buffer provider. Consider calling "reset" before this function.
setBufferProvider(AudioBufferProvider * p)52     virtual void setBufferProvider(AudioBufferProvider *p) {
53         mTrackBufferProvider = p;
54     }
55 
56 protected:
57     AudioBufferProvider *mTrackBufferProvider;
58 };
59 
60 // Base AudioBufferProvider class used for DownMixerBufferProvider, RemixBufferProvider,
61 // and ReformatBufferProvider.
62 // It handles a private buffer for use in converting format or channel masks from the
63 // input data to a form acceptable by the mixer.
64 // TODO: Make a ResamplerBufferProvider when integers are entirely removed from the
65 // processing pipeline.
66 class CopyBufferProvider : public PassthruBufferProvider {
67 public:
68     // Use a private buffer of bufferFrameCount frames (each frame is outputFrameSize bytes).
69     // If bufferFrameCount is 0, no private buffer is created and in-place modification of
70     // the upstream buffer provider's buffers is performed by copyFrames().
71     CopyBufferProvider(size_t inputFrameSize, size_t outputFrameSize,
72             size_t bufferFrameCount);
73     virtual ~CopyBufferProvider();
74 
75     // Overrides AudioBufferProvider methods
76     virtual status_t getNextBuffer(Buffer *buffer);
77     virtual void releaseBuffer(Buffer *buffer);
78 
79     // Overrides PassthruBufferProvider
80     virtual void reset();
81     void setBufferProvider(AudioBufferProvider *p) override;
82 
83     // this function should be supplied by the derived class.  It converts
84     // #frames in the *src pointer to the *dst pointer.  It is public because
85     // some providers will allow this to work on arbitrary buffers outside
86     // of the internal buffers.
87     virtual void copyFrames(void *dst, const void *src, size_t frames) = 0;
88 
89 protected:
90     const size_t         mInputFrameSize;
91     const size_t         mOutputFrameSize;
92 private:
93     AudioBufferProvider::Buffer mBuffer;
94     const size_t         mLocalBufferFrameCount;
95     void                *mLocalBufferData;
96     size_t               mConsumed;
97 };
98 
99 // DownmixerBufferProvider derives from CopyBufferProvider to provide
100 // position dependent downmixing by an Audio Effect.
101 class DownmixerBufferProvider : public CopyBufferProvider {
102 public:
103     DownmixerBufferProvider(audio_channel_mask_t inputChannelMask,
104             audio_channel_mask_t outputChannelMask, audio_format_t format,
105             uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount);
106     virtual ~DownmixerBufferProvider();
107     //Overrides
108     virtual void copyFrames(void *dst, const void *src, size_t frames);
109 
isValid()110     bool isValid() const { return mDownmixInterface.get() != NULL; }
111     static status_t init();
isMultichannelCapable()112     static bool isMultichannelCapable() { return sIsMultichannelCapable; }
113 
114 protected:
115     sp<EffectsFactoryHalInterface> mEffectsFactory;
116     sp<EffectHalInterface> mDownmixInterface;
117     size_t mInFrameSize;
118     size_t mOutFrameSize;
119     sp<EffectBufferHalInterface> mInBuffer;
120     sp<EffectBufferHalInterface> mOutBuffer;
121     effect_config_t    mDownmixConfig;
122 
123     // effect descriptor for the downmixer used by the mixer
124     static effect_descriptor_t sDwnmFxDesc;
125     // indicates whether a downmix effect has been found and is usable by this mixer
126     static bool                sIsMultichannelCapable;
127     // FIXME: should we allow effects outside of the framework?
128     // We need to here. A special ioId that must be <= -2 so it does not map to a session.
129     static const int32_t SESSION_ID_INVALID_AND_IGNORED = -2;
130 };
131 
132 // RemixBufferProvider derives from CopyBufferProvider to perform an
133 // upmix or downmix to the proper channel count and mask.
134 class RemixBufferProvider : public CopyBufferProvider {
135 public:
136     RemixBufferProvider(audio_channel_mask_t inputChannelMask,
137             audio_channel_mask_t outputChannelMask, audio_format_t format,
138             size_t bufferFrameCount);
139     //Overrides
140     virtual void copyFrames(void *dst, const void *src, size_t frames);
141 
142 protected:
143     const audio_format_t mFormat;
144     const size_t         mSampleSize;
145     const size_t         mInputChannels;
146     const size_t         mOutputChannels;
147     int8_t               mIdxAry[sizeof(uint32_t) * 8]; // 32 bits => channel indices
148 };
149 
150 // ReformatBufferProvider derives from CopyBufferProvider to convert the input data
151 // to an acceptable mixer input format type.
152 class ReformatBufferProvider : public CopyBufferProvider {
153 public:
154     ReformatBufferProvider(int32_t channelCount,
155             audio_format_t inputFormat, audio_format_t outputFormat,
156             size_t bufferFrameCount);
157     virtual void copyFrames(void *dst, const void *src, size_t frames);
158 
159 protected:
160     const uint32_t       mChannelCount;
161     const audio_format_t mInputFormat;
162     const audio_format_t mOutputFormat;
163 };
164 
165 // ClampFloatBufferProvider derives from CopyBufferProvider to clamp floats inside -3db
166 class ClampFloatBufferProvider : public CopyBufferProvider {
167 public:
168     ClampFloatBufferProvider(int32_t channelCount,
169             size_t bufferFrameCount);
170     virtual void copyFrames(void *dst, const void *src, size_t frames);
171 
172 protected:
173     const uint32_t       mChannelCount;
174 };
175 
176 // TimestretchBufferProvider derives from PassthruBufferProvider for time stretching
177 class TimestretchBufferProvider : public PassthruBufferProvider {
178 public:
179     TimestretchBufferProvider(int32_t channelCount,
180             audio_format_t format, uint32_t sampleRate,
181             const AudioPlaybackRate &playbackRate);
182     virtual ~TimestretchBufferProvider();
183 
184     // Overrides AudioBufferProvider methods
185     virtual status_t getNextBuffer(Buffer* buffer);
186     virtual void releaseBuffer(Buffer* buffer);
187 
188     // Overrides PassthruBufferProvider
189     virtual void reset();
190     void setBufferProvider(AudioBufferProvider *p) override;
191 
192     virtual status_t setPlaybackRate(const AudioPlaybackRate &playbackRate);
193 
194     // processes frames
195     // dstBuffer is where to place the data
196     // dstFrames [in/out] is the desired frames (return with actual placed in buffer)
197     // srcBuffer is the source data
198     // srcFrames [in/out] is the available source frames (return with consumed)
199     virtual void processFrames(void *dstBuffer, size_t *dstFrames,
200             const void *srcBuffer, size_t *srcFrames);
201 
202 protected:
203     const uint32_t       mChannelCount;
204     const audio_format_t mFormat;
205     const uint32_t       mSampleRate; // const for now (TODO change this)
206     const size_t         mFrameSize;
207     AudioPlaybackRate    mPlaybackRate;
208 
209 private:
210     AudioBufferProvider::Buffer mBuffer;          // for upstream request
211     size_t               mLocalBufferFrameCount;  // size of local buffer
212     void                *mLocalBufferData;        // internally allocated buffer for data returned
213                                                   // to caller
214     size_t               mRemaining;              // remaining data in local buffer
215     sonicStream          mSonicStream;            // handle to sonic timestretch object
216     //FIXME: this dependency should be abstracted out
217     bool                 mFallbackFailErrorShown; // log fallback error only once
218     bool                 mAudioPlaybackRateValid; // flag for current parameters validity
219 };
220 
221 // AdjustChannelsBufferProvider derives from CopyBufferProvider to adjust sample data.
222 // Expands or contracts sample data from one interleaved channel format to another.
223 // Extra expanded channels are filled with zeros and put at the end of each audio frame.
224 // Contracted channels are copied to the end of the output buffer(storage should be
225 // allocated appropriately).
226 // Contracted channels could be written to output buffer.
227 class AdjustChannelsBufferProvider : public CopyBufferProvider {
228 public:
AdjustChannelsBufferProvider(audio_format_t format,size_t inChannelCount,size_t outChannelCount,size_t frameCount)229     AdjustChannelsBufferProvider(audio_format_t format, size_t inChannelCount,
230             size_t outChannelCount, size_t frameCount) : AdjustChannelsBufferProvider(
231                     format, inChannelCount, outChannelCount,
232                     frameCount, AUDIO_FORMAT_INVALID, nullptr) { }
233     // Contracted data is converted to contractedFormat and put into contractedBuffer.
234     AdjustChannelsBufferProvider(audio_format_t format, size_t inChannelCount,
235             size_t outChannelCount, size_t frameCount, audio_format_t contractedFormat,
236             void* contractedBuffer);
237     //Overrides
238     status_t getNextBuffer(Buffer* pBuffer) override;
239     void copyFrames(void *dst, const void *src, size_t frames) override;
240     void reset() override;
241 
clearContractedFrames()242     void clearContractedFrames() { mContractedWrittenFrames = 0; }
243 
244 protected:
245     const audio_format_t mFormat;
246     const size_t         mInChannelCount;
247     const size_t         mOutChannelCount;
248     const size_t         mSampleSizeInBytes;
249     const size_t         mFrameCount;
250     const size_t         mContractedChannelCount;
251     const audio_format_t mContractedFormat;
252     void                *mContractedBuffer;
253     size_t               mContractedWrittenFrames;
254     size_t               mContractedFrameSize;
255 };
256 // ----------------------------------------------------------------------------
257 } // namespace android
258 
259 #endif // ANDROID_BUFFER_PROVIDERS_H
260