1/*
2 * Copyright (C) 2012 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#define LOG_TAG "MonoPipeReader"
18//#define LOG_NDEBUG 0
19
20#include <cutils/compiler.h>
21#include <utils/Log.h>
22#include <media/nbaio/MonoPipeReader.h>
23
24namespace android {
25
26MonoPipeReader::MonoPipeReader(MonoPipe* pipe) :
27        NBAIO_Source(pipe->mFormat),
28        mPipe(pipe), mFifoReader(mPipe->mFifo, true /*throttlesWriter*/)
29{
30}
31
32MonoPipeReader::~MonoPipeReader()
33{
34}
35
36ssize_t MonoPipeReader::availableToRead()
37{
38    if (CC_UNLIKELY(!mNegotiated)) {
39        return NEGOTIATE;
40    }
41    ssize_t ret = mFifoReader.available();
42    ALOG_ASSERT(ret <= mPipe->mMaxFrames);
43    return ret;
44}
45
46ssize_t MonoPipeReader::read(void *buffer, size_t count)
47{
48    // count == 0 is unlikely and not worth checking for explicitly; will be handled automatically
49    ssize_t actual = mFifoReader.read(buffer, count);
50    ALOG_ASSERT(actual <= count);
51    if (CC_UNLIKELY(actual <= 0)) {
52        return actual;
53    }
54    mFramesRead += (size_t) actual;
55    return actual;
56}
57
58void MonoPipeReader::onTimestamp(const ExtendedTimestamp &timestamp)
59{
60    mPipe->mTimestampMutator.push(timestamp);
61}
62
63}   // namespace android
64