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