Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 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 "PipeReader" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <cutils/compiler.h> |
| 21 | #include <utils/Log.h> |
Glenn Kasten | 2dd4bdd | 2012-08-29 11:10:32 -0700 | [diff] [blame] | 22 | #include <media/nbaio/PipeReader.h> |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | PipeReader::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 | |
| 37 | PipeReader::~PipeReader() |
| 38 | { |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 39 | #if !LOG_NDEBUG |
| 40 | int32_t readers = |
| 41 | #else |
| 42 | (void) |
| 43 | #endif |
| 44 | android_atomic_dec(&mPipe.mReaders); |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 45 | ALOG_ASSERT(readers > 0); |
| 46 | } |
| 47 | |
Andy Hung | ad6d52d | 2016-07-18 13:42:03 -0700 | [diff] [blame] | 48 | __attribute__((no_sanitize("integer"))) |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 49 | ssize_t PipeReader::availableToRead() |
| 50 | { |
| 51 | if (CC_UNLIKELY(!mNegotiated)) { |
| 52 | return NEGOTIATE; |
| 53 | } |
| 54 | int32_t rear = android_atomic_acquire_load(&mPipe.mRear); |
| 55 | // read() is not multi-thread safe w.r.t. itself, so no mutex or atomic op needed to read mFront |
| 56 | size_t avail = rear - mFront; |
| 57 | if (CC_UNLIKELY(avail > mPipe.mMaxFrames)) { |
Glenn Kasten | 1b29184 | 2016-07-18 14:55:21 -0700 | [diff] [blame] | 58 | // Discard all data in pipe to avoid another overrun immediately |
| 59 | mFront = rear; |
| 60 | mFramesOverrun += avail; |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 61 | ++mOverruns; |
| 62 | return OVERRUN; |
| 63 | } |
| 64 | return avail; |
| 65 | } |
| 66 | |
Andy Hung | ad6d52d | 2016-07-18 13:42:03 -0700 | [diff] [blame] | 67 | __attribute__((no_sanitize("integer"))) |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 68 | ssize_t PipeReader::read(void *buffer, size_t count) |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 69 | { |
| 70 | ssize_t avail = availableToRead(); |
| 71 | if (CC_UNLIKELY(avail <= 0)) { |
| 72 | return avail; |
| 73 | } |
| 74 | // An overrun can occur from here on and be silently ignored, |
| 75 | // but it will be caught at next read() |
| 76 | if (CC_LIKELY(count > (size_t) avail)) { |
| 77 | count = avail; |
| 78 | } |
| 79 | size_t front = mFront & (mPipe.mMaxFrames - 1); |
| 80 | size_t red = mPipe.mMaxFrames - front; |
| 81 | if (CC_LIKELY(red > count)) { |
| 82 | red = count; |
| 83 | } |
| 84 | // In particular, an overrun during the memcpy will result in reading corrupt data |
Glenn Kasten | 4d693d6 | 2014-03-06 07:53:11 -0800 | [diff] [blame] | 85 | memcpy(buffer, (char *) mPipe.mBuffer + (front * mFrameSize), red * mFrameSize); |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 86 | // We could re-read the rear pointer here to detect the corruption, but why bother? |
| 87 | if (CC_UNLIKELY(front + red == mPipe.mMaxFrames)) { |
| 88 | if (CC_UNLIKELY((count -= red) > front)) { |
| 89 | count = front; |
| 90 | } |
| 91 | if (CC_LIKELY(count > 0)) { |
Glenn Kasten | 4d693d6 | 2014-03-06 07:53:11 -0800 | [diff] [blame] | 92 | memcpy((char *) buffer + (red * mFrameSize), mPipe.mBuffer, count * mFrameSize); |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 93 | red += count; |
| 94 | } |
| 95 | } |
| 96 | mFront += red; |
| 97 | mFramesRead += red; |
| 98 | return red; |
| 99 | } |
| 100 | |
Andy Hung | ad6d52d | 2016-07-18 13:42:03 -0700 | [diff] [blame] | 101 | __attribute__((no_sanitize("integer"))) |
| 102 | ssize_t PipeReader::flush() |
| 103 | { |
| 104 | if (CC_UNLIKELY(!mNegotiated)) { |
| 105 | return NEGOTIATE; |
| 106 | } |
| 107 | const int32_t rear = android_atomic_acquire_load(&mPipe.mRear); |
| 108 | const size_t flushed = rear - mFront; |
| 109 | // We don't check if flushed > mPipe.mMaxFrames (an overrun occurred) as the |
| 110 | // distinction is unimportant; all data is dropped. |
| 111 | mFront = rear; |
| 112 | mFramesRead += flushed; // we consider flushed frames as read. |
| 113 | return flushed; |
| 114 | } |
| 115 | |
Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 116 | } // namespace android |