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