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