| 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 "MonoPipe" | 
 | 18 | //#define LOG_NDEBUG 0 | 
 | 19 |  | 
 | 20 | #include <cutils/atomic.h> | 
 | 21 | #include <cutils/compiler.h> | 
 | 22 | #include <utils/Log.h> | 
 | 23 | #include "MonoPipe.h" | 
 | 24 | #include "roundup.h" | 
 | 25 |  | 
 | 26 | namespace android { | 
 | 27 |  | 
 | 28 | MonoPipe::MonoPipe(size_t maxFrames, NBAIO_Format format, bool writeCanBlock) : | 
 | 29 |         NBAIO_Sink(format), | 
 | 30 |         mMaxFrames(roundup(maxFrames)), | 
 | 31 |         mBuffer(malloc(mMaxFrames * Format_frameSize(format))), | 
 | 32 |         mFront(0), | 
 | 33 |         mRear(0), | 
 | 34 |         mWriteCanBlock(writeCanBlock) | 
 | 35 | { | 
 | 36 |     if (writeCanBlock) { | 
 | 37 |         // compute sleep time to be about 2/3 of a full pipe; | 
 | 38 |         // this gives a balance between risk of underrun vs. too-frequent wakeups | 
 | 39 |         mSleep.tv_sec = 0; | 
 | 40 |         uint64_t ns = mMaxFrames * (666666667 / Format_sampleRate(format)); | 
 | 41 |         if (ns > 999999999) { | 
 | 42 |             ns = 999999999; | 
 | 43 |         } | 
 | 44 |         mSleep.tv_nsec = ns; | 
 | 45 |     } | 
 | 46 | } | 
 | 47 |  | 
 | 48 | MonoPipe::~MonoPipe() | 
 | 49 | { | 
 | 50 |     free(mBuffer); | 
 | 51 | } | 
 | 52 |  | 
 | 53 | ssize_t MonoPipe::availableToWrite() const | 
 | 54 | { | 
 | 55 |     if (CC_UNLIKELY(!mNegotiated)) { | 
 | 56 |         return NEGOTIATE; | 
 | 57 |     } | 
 | 58 |     ssize_t ret = mMaxFrames - (mRear - android_atomic_acquire_load(&mFront)); | 
 | 59 |     ALOG_ASSERT((0 <= ret) && (ret <= mMaxFrames)); | 
 | 60 |     return ret; | 
 | 61 | } | 
 | 62 |  | 
 | 63 | ssize_t MonoPipe::write(const void *buffer, size_t count) | 
 | 64 | { | 
 | 65 |     // count == 0 is unlikely and not worth checking for explicitly; will be handled automatically | 
 | 66 |     if (CC_UNLIKELY(!mNegotiated)) { | 
 | 67 |         return NEGOTIATE; | 
 | 68 |     } | 
 | 69 |     size_t totalFramesWritten = 0; | 
 | 70 |     for (;;) { | 
 | 71 |         size_t written = availableToWrite(); | 
 | 72 |         if (CC_LIKELY(written > count)) { | 
 | 73 |             written = count; | 
 | 74 |         } | 
 | 75 |         size_t rear = mRear & (mMaxFrames - 1); | 
 | 76 |         size_t part1 = mMaxFrames - rear; | 
 | 77 |         if (part1 > written) { | 
 | 78 |             part1 = written; | 
 | 79 |         } | 
 | 80 |         if (CC_LIKELY(part1 > 0)) { | 
 | 81 |             memcpy((char *) mBuffer + (rear << mBitShift), buffer, part1 << mBitShift); | 
 | 82 |             if (CC_UNLIKELY(rear + part1 == mMaxFrames)) { | 
 | 83 |                 size_t part2 = written - part1; | 
 | 84 |                 if (CC_LIKELY(part2 > 0)) { | 
 | 85 |                     memcpy(mBuffer, (char *) buffer + (part1 << mBitShift), part2 << mBitShift); | 
 | 86 |                 } | 
 | 87 |             } | 
 | 88 |             android_atomic_release_store(written + mRear, &mRear); | 
 | 89 |             totalFramesWritten += written; | 
 | 90 |         } | 
 | 91 |         if ((count -= written) == 0 || !mWriteCanBlock) { | 
 | 92 |             break; | 
 | 93 |         } | 
 | 94 |         buffer = (char *) buffer + (written << mBitShift); | 
 | 95 |         // simulate blocking I/O by sleeping | 
 | 96 |         nanosleep(&mSleep, NULL); | 
 | 97 |     } | 
 | 98 |     mFramesWritten += totalFramesWritten; | 
 | 99 |     return totalFramesWritten; | 
 | 100 | } | 
 | 101 |  | 
 | 102 | }   // namespace android |