BufferProviders.h revision f097cae65bfce7200938c5bd89e7e9b61cba78b3
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 <hardware/audio_effect.h>
24#include <media/AudioBufferProvider.h>
25#include <system/audio.h>
26#include <sonic.h>
27
28namespace android {
29
30// ----------------------------------------------------------------------------
31
32class PassthruBufferProvider : public AudioBufferProvider {
33public:
34    PassthruBufferProvider() : mTrackBufferProvider(NULL) { }
35
36    virtual ~PassthruBufferProvider() { }
37
38    // call this to release the buffer to the upstream provider.
39    // treat it as an audio discontinuity for future samples.
40    virtual void reset() { }
41
42    // set the upstream buffer provider. Consider calling "reset" before this function.
43    virtual void setBufferProvider(AudioBufferProvider *p) {
44        mTrackBufferProvider = p;
45    }
46
47protected:
48    AudioBufferProvider *mTrackBufferProvider;
49};
50
51// Base AudioBufferProvider class used for DownMixerBufferProvider, RemixBufferProvider,
52// and ReformatBufferProvider.
53// It handles a private buffer for use in converting format or channel masks from the
54// input data to a form acceptable by the mixer.
55// TODO: Make a ResamplerBufferProvider when integers are entirely removed from the
56// processing pipeline.
57class CopyBufferProvider : public PassthruBufferProvider {
58public:
59    // Use a private buffer of bufferFrameCount frames (each frame is outputFrameSize bytes).
60    // If bufferFrameCount is 0, no private buffer is created and in-place modification of
61    // the upstream buffer provider's buffers is performed by copyFrames().
62    CopyBufferProvider(size_t inputFrameSize, size_t outputFrameSize,
63            size_t bufferFrameCount);
64    virtual ~CopyBufferProvider();
65
66    // Overrides AudioBufferProvider methods
67    virtual status_t getNextBuffer(Buffer *buffer, int64_t pts);
68    virtual void releaseBuffer(Buffer *buffer);
69
70    // Overrides PassthruBufferProvider
71    virtual void reset();
72
73    // this function should be supplied by the derived class.  It converts
74    // #frames in the *src pointer to the *dst pointer.  It is public because
75    // some providers will allow this to work on arbitrary buffers outside
76    // of the internal buffers.
77    virtual void copyFrames(void *dst, const void *src, size_t frames) = 0;
78
79protected:
80    const size_t         mInputFrameSize;
81    const size_t         mOutputFrameSize;
82private:
83    AudioBufferProvider::Buffer mBuffer;
84    const size_t         mLocalBufferFrameCount;
85    void                *mLocalBufferData;
86    size_t               mConsumed;
87};
88
89// DownmixerBufferProvider derives from CopyBufferProvider to provide
90// position dependent downmixing by an Audio Effect.
91class DownmixerBufferProvider : public CopyBufferProvider {
92public:
93    DownmixerBufferProvider(audio_channel_mask_t inputChannelMask,
94            audio_channel_mask_t outputChannelMask, audio_format_t format,
95            uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount);
96    virtual ~DownmixerBufferProvider();
97    //Overrides
98    virtual void copyFrames(void *dst, const void *src, size_t frames);
99
100    bool isValid() const { return mDownmixHandle != NULL; }
101    static status_t init();
102    static bool isMultichannelCapable() { return sIsMultichannelCapable; }
103
104protected:
105    effect_handle_t    mDownmixHandle;
106    effect_config_t    mDownmixConfig;
107
108    // effect descriptor for the downmixer used by the mixer
109    static effect_descriptor_t sDwnmFxDesc;
110    // indicates whether a downmix effect has been found and is usable by this mixer
111    static bool                sIsMultichannelCapable;
112    // FIXME: should we allow effects outside of the framework?
113    // We need to here. A special ioId that must be <= -2 so it does not map to a session.
114    static const int32_t SESSION_ID_INVALID_AND_IGNORED = -2;
115};
116
117// RemixBufferProvider derives from CopyBufferProvider to perform an
118// upmix or downmix to the proper channel count and mask.
119class RemixBufferProvider : public CopyBufferProvider {
120public:
121    RemixBufferProvider(audio_channel_mask_t inputChannelMask,
122            audio_channel_mask_t outputChannelMask, audio_format_t format,
123            size_t bufferFrameCount);
124    //Overrides
125    virtual void copyFrames(void *dst, const void *src, size_t frames);
126
127protected:
128    const audio_format_t mFormat;
129    const size_t         mSampleSize;
130    const size_t         mInputChannels;
131    const size_t         mOutputChannels;
132    int8_t               mIdxAry[sizeof(uint32_t) * 8]; // 32 bits => channel indices
133};
134
135// ReformatBufferProvider derives from CopyBufferProvider to convert the input data
136// to an acceptable mixer input format type.
137class ReformatBufferProvider : public CopyBufferProvider {
138public:
139    ReformatBufferProvider(int32_t channelCount,
140            audio_format_t inputFormat, audio_format_t outputFormat,
141            size_t bufferFrameCount);
142    virtual void copyFrames(void *dst, const void *src, size_t frames);
143
144protected:
145    const uint32_t       mChannelCount;
146    const audio_format_t mInputFormat;
147    const audio_format_t mOutputFormat;
148};
149
150// TimestretchBufferProvider derives from PassthruBufferProvider for time stretching
151class TimestretchBufferProvider : public PassthruBufferProvider {
152public:
153    TimestretchBufferProvider(int32_t channelCount,
154            audio_format_t format, uint32_t sampleRate, float speed, float pitch);
155    virtual ~TimestretchBufferProvider();
156
157    // Overrides AudioBufferProvider methods
158    virtual status_t getNextBuffer(Buffer* buffer, int64_t pts);
159    virtual void releaseBuffer(Buffer* buffer);
160
161    // Overrides PassthruBufferProvider
162    virtual void reset();
163
164    virtual status_t setPlaybackRate(float speed, float pitch);
165
166    // processes frames
167    // dstBuffer is where to place the data
168    // dstFrames [in/out] is the desired frames (return with actual placed in buffer)
169    // srcBuffer is the source data
170    // srcFrames [in/out] is the available source frames (return with consumed)
171    virtual void processFrames(void *dstBuffer, size_t *dstFrames,
172            const void *srcBuffer, size_t *srcFrames);
173
174protected:
175    const uint32_t       mChannelCount;
176    const audio_format_t mFormat;
177    const uint32_t       mSampleRate; // const for now (TODO change this)
178    const size_t         mFrameSize;
179    float                mSpeed;
180    float                mPitch;
181
182private:
183    AudioBufferProvider::Buffer mBuffer;
184    size_t               mLocalBufferFrameCount;
185    void                *mLocalBufferData;
186    size_t               mRemaining;
187    sonicStream          mSonicStream;
188};
189
190// ----------------------------------------------------------------------------
191} // namespace android
192
193#endif // ANDROID_BUFFER_PROVIDERS_H
194