summaryrefslogtreecommitdiffstats
path: root/media/libnbaio/SourceAudioBufferProvider.cpp
diff options
context:
space:
mode:
authorGlenn Kasten <gkasten@google.com>2012-08-29 11:10:32 -0700
committerGlenn Kasten <gkasten@google.com>2012-08-30 10:53:37 -0700
commit2dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0 (patch)
treed0f594b402b6d2a4de89386c30c3db4adab89894 /media/libnbaio/SourceAudioBufferProvider.cpp
parent0a24726a64ba1c50aa6ee1610de2d80602d49fe6 (diff)
downloadframeworks_av-2dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0.zip
frameworks_av-2dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0.tar.gz
frameworks_av-2dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0.tar.bz2
Move libnbaio out of AudioFlinger
libnbaio is now a separate shared library from AudioFlinger, rather than a static library used only by AudioFlinger. AudioBufferProvider interface is now also independent of AudioFlinger, moved to include/media/ Change-Id: I9bb62ffbc38d42a38b0af76e66da5e9ab1e0e21b
Diffstat (limited to 'media/libnbaio/SourceAudioBufferProvider.cpp')
-rw-r--r--media/libnbaio/SourceAudioBufferProvider.cpp104
1 files changed, 104 insertions, 0 deletions
diff --git a/media/libnbaio/SourceAudioBufferProvider.cpp b/media/libnbaio/SourceAudioBufferProvider.cpp
new file mode 100644
index 0000000..d11a86c
--- /dev/null
+++ b/media/libnbaio/SourceAudioBufferProvider.cpp
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "SourceAudioBufferProvider"
+//#define LOG_NDEBUG 0
+
+#include <utils/Log.h>
+#include <media/nbaio/SourceAudioBufferProvider.h>
+
+namespace android {
+
+SourceAudioBufferProvider::SourceAudioBufferProvider(const sp<NBAIO_Source>& source) :
+ mSource(source),
+ // mFrameBitShiftFormat below
+ mAllocated(NULL), mSize(0), mOffset(0), mRemaining(0), mGetCount(0)
+{
+ ALOG_ASSERT(source != 0);
+
+ // negotiate with source
+ NBAIO_Format counterOffers[1];
+ size_t numCounterOffers = 1;
+ ssize_t index = source->negotiate(NULL, 0, counterOffers, numCounterOffers);
+ ALOG_ASSERT(index == (ssize_t) NEGOTIATE && numCounterOffers > 0);
+ numCounterOffers = 0;
+ index = source->negotiate(counterOffers, 1, NULL, numCounterOffers);
+ ALOG_ASSERT(index == 0);
+ mFrameBitShift = Format_frameBitShift(source->format());
+}
+
+SourceAudioBufferProvider::~SourceAudioBufferProvider()
+{
+ free(mAllocated);
+}
+
+status_t SourceAudioBufferProvider::getNextBuffer(Buffer *buffer, int64_t pts)
+{
+ ALOG_ASSERT(buffer != NULL && buffer->frameCount > 0 && mGetCount == 0);
+ // any leftover data available?
+ if (mRemaining > 0) {
+ ALOG_ASSERT(mOffset + mRemaining <= mSize);
+ if (mRemaining < buffer->frameCount) {
+ buffer->frameCount = mRemaining;
+ }
+ buffer->raw = (char *) mAllocated + (mOffset << mFrameBitShift);
+ mGetCount = buffer->frameCount;
+ return OK;
+ }
+ // do we need to reallocate?
+ if (buffer->frameCount > mSize) {
+ free(mAllocated);
+ mAllocated = malloc(buffer->frameCount << mFrameBitShift);
+ mSize = buffer->frameCount;
+ }
+ // read from source
+ ssize_t actual = mSource->read(mAllocated, buffer->frameCount, pts);
+ if (actual > 0) {
+ ALOG_ASSERT((size_t) actual <= buffer->frameCount);
+ mOffset = 0;
+ mRemaining = actual;
+ buffer->raw = mAllocated;
+ buffer->frameCount = actual;
+ mGetCount = actual;
+ return OK;
+ }
+ buffer->raw = NULL;
+ buffer->frameCount = 0;
+ mGetCount = 0;
+ return NOT_ENOUGH_DATA;
+}
+
+void SourceAudioBufferProvider::releaseBuffer(Buffer *buffer)
+{
+ ALOG_ASSERT((buffer != NULL) &&
+ (buffer->raw == (char *) mAllocated + (mOffset << mFrameBitShift)) &&
+ (buffer->frameCount <= mGetCount) &&
+ (mGetCount <= mRemaining) &&
+ (mOffset + mRemaining <= mSize));
+ mOffset += buffer->frameCount;
+ mRemaining -= buffer->frameCount;
+ buffer->raw = NULL;
+ buffer->frameCount = 0;
+ mGetCount = 0;
+}
+
+size_t SourceAudioBufferProvider::framesReady() const
+{
+ ssize_t avail = mSource->availableToRead();
+ return avail < 0 ? 0 : (size_t) avail;
+}
+
+} // namespace android