blob: 394f6acd95d56dd5d955e027ecf38f2cb4a58412 [file] [log] [blame]
Glenn Kasten01066232012-02-27 11:50:44 -08001/*
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>
Glenn Kasten2dd4bdd2012-08-29 11:10:32 -070022#include <media/nbaio/MonoPipeReader.h>
Glenn Kasten01066232012-02-27 11:50:44 -080023
24namespace android {
25
26MonoPipeReader::MonoPipeReader(MonoPipe* pipe) :
27 NBAIO_Source(pipe->mFormat),
28 mPipe(pipe)
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 = android_atomic_acquire_load(&mPipe->mRear) - mPipe->mFront;
42 ALOG_ASSERT((0 <= ret) && (ret <= mMaxFrames));
43 return ret;
44}
45
John Grossman2c3b2da2012-08-02 17:08:54 -070046ssize_t MonoPipeReader::read(void *buffer, size_t count, int64_t readPTS)
Glenn Kasten01066232012-02-27 11:50:44 -080047{
John Grossman2c3b2da2012-08-02 17:08:54 -070048 // Compute the "next read PTS" and cache it. Callers of read pass a read
49 // PTS indicating the local time for which they are requesting data along
50 // with a count (which is the number of audio frames they are going to
51 // ultimately pass to the next stage of the pipeline). Offsetting readPTS
52 // by the duration of count will give us the readPTS which will be passed to
53 // us next time, assuming they system continues to operate in steady state
54 // with no discontinuities. We stash this value so it can be used by the
55 // MonoPipe writer to imlement getNextWriteTimestamp.
56 int64_t nextReadPTS;
57 nextReadPTS = mPipe->offsetTimestampByAudioFrames(readPTS, count);
58
Glenn Kasten01066232012-02-27 11:50:44 -080059 // count == 0 is unlikely and not worth checking for explicitly; will be handled automatically
60 ssize_t red = availableToRead();
61 if (CC_UNLIKELY(red <= 0)) {
John Grossman2c3b2da2012-08-02 17:08:54 -070062 // Uh-oh, looks like we are underflowing. Update the next read PTS and
63 // get out.
64 mPipe->updateFrontAndNRPTS(mPipe->mFront, nextReadPTS);
Glenn Kasten01066232012-02-27 11:50:44 -080065 return red;
66 }
67 if (CC_LIKELY((size_t) red > count)) {
68 red = count;
69 }
70 size_t front = mPipe->mFront & (mPipe->mMaxFrames - 1);
71 size_t part1 = mPipe->mMaxFrames - front;
72 if (part1 > (size_t) red) {
73 part1 = red;
74 }
75 if (CC_LIKELY(part1 > 0)) {
76 memcpy(buffer, (char *) mPipe->mBuffer + (front << mBitShift), part1 << mBitShift);
77 if (CC_UNLIKELY(front + part1 == mPipe->mMaxFrames)) {
78 size_t part2 = red - part1;
79 if (CC_LIKELY(part2 > 0)) {
80 memcpy((char *) buffer + (part1 << mBitShift), mPipe->mBuffer, part2 << mBitShift);
81 }
82 }
John Grossman2c3b2da2012-08-02 17:08:54 -070083 mPipe->updateFrontAndNRPTS(red + mPipe->mFront, nextReadPTS);
Glenn Kasten01066232012-02-27 11:50:44 -080084 mFramesRead += red;
85 }
86 return red;
87}
88
89} // namespace android