blob: a8796476f82171a68c9adc1ceeae4a6be5daad05 [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 "PipeReader"
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/PipeReader.h>
Glenn Kasten01066232012-02-27 11:50:44 -080023
24namespace android {
25
26PipeReader::PipeReader(Pipe& pipe) :
27 NBAIO_Source(pipe.mFormat),
28 mPipe(pipe),
29 // any data already in the pipe is not visible to this PipeReader
30 mFront(android_atomic_acquire_load(&pipe.mRear)),
31 mFramesOverrun(0),
32 mOverruns(0)
33{
34 android_atomic_inc(&pipe.mReaders);
35}
36
37PipeReader::~PipeReader()
38{
Glenn Kasten57c4e6f2016-03-18 14:54:07 -070039#if !LOG_NDEBUG
40 int32_t readers =
41#else
42 (void)
43#endif
44 android_atomic_dec(&mPipe.mReaders);
Glenn Kasten01066232012-02-27 11:50:44 -080045 ALOG_ASSERT(readers > 0);
46}
47
48ssize_t PipeReader::availableToRead()
49{
50 if (CC_UNLIKELY(!mNegotiated)) {
51 return NEGOTIATE;
52 }
53 int32_t rear = android_atomic_acquire_load(&mPipe.mRear);
54 // read() is not multi-thread safe w.r.t. itself, so no mutex or atomic op needed to read mFront
55 size_t avail = rear - mFront;
56 if (CC_UNLIKELY(avail > mPipe.mMaxFrames)) {
Glenn Kastenfbae5da2012-05-21 09:17:20 -070057 // Discard 1/16 of the most recent data in pipe to avoid another overrun immediately
Glenn Kasten01066232012-02-27 11:50:44 -080058 int32_t oldFront = mFront;
Glenn Kastenfbae5da2012-05-21 09:17:20 -070059 mFront = rear - mPipe.mMaxFrames + (mPipe.mMaxFrames >> 4);
Glenn Kasten01066232012-02-27 11:50:44 -080060 mFramesOverrun += (size_t) (mFront - oldFront);
61 ++mOverruns;
62 return OVERRUN;
63 }
64 return avail;
65}
66
Glenn Kastend79072e2016-01-06 08:41:20 -080067ssize_t PipeReader::read(void *buffer, size_t count)
Glenn Kasten01066232012-02-27 11:50:44 -080068{
69 ssize_t avail = availableToRead();
70 if (CC_UNLIKELY(avail <= 0)) {
71 return avail;
72 }
73 // An overrun can occur from here on and be silently ignored,
74 // but it will be caught at next read()
75 if (CC_LIKELY(count > (size_t) avail)) {
76 count = avail;
77 }
78 size_t front = mFront & (mPipe.mMaxFrames - 1);
79 size_t red = mPipe.mMaxFrames - front;
80 if (CC_LIKELY(red > count)) {
81 red = count;
82 }
83 // In particular, an overrun during the memcpy will result in reading corrupt data
Glenn Kasten4d693d62014-03-06 07:53:11 -080084 memcpy(buffer, (char *) mPipe.mBuffer + (front * mFrameSize), red * mFrameSize);
Glenn Kasten01066232012-02-27 11:50:44 -080085 // We could re-read the rear pointer here to detect the corruption, but why bother?
86 if (CC_UNLIKELY(front + red == mPipe.mMaxFrames)) {
87 if (CC_UNLIKELY((count -= red) > front)) {
88 count = front;
89 }
90 if (CC_LIKELY(count > 0)) {
Glenn Kasten4d693d62014-03-06 07:53:11 -080091 memcpy((char *) buffer + (red * mFrameSize), mPipe.mBuffer, count * mFrameSize);
Glenn Kasten01066232012-02-27 11:50:44 -080092 red += count;
93 }
94 }
95 mFront += red;
96 mFramesRead += red;
97 return red;
98}
99
100} // namespace android