blob: 6efb8b1dabf036e62e16eda80ec08fcf7c4acaf0 [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 "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
26namespace android {
27
Glenn Kasten820ba702012-05-30 16:34:17 -070028MonoPipe::MonoPipe(size_t reqFrames, NBAIO_Format format, bool writeCanBlock) :
Glenn Kasten01066232012-02-27 11:50:44 -080029 NBAIO_Sink(format),
Glenn Kasten820ba702012-05-30 16:34:17 -070030 mReqFrames(reqFrames),
31 mMaxFrames(roundup(reqFrames)),
Glenn Kasten01066232012-02-27 11:50:44 -080032 mBuffer(malloc(mMaxFrames * Format_frameSize(format))),
33 mFront(0),
34 mRear(0),
35 mWriteCanBlock(writeCanBlock)
36{
Glenn Kasten01066232012-02-27 11:50:44 -080037}
38
39MonoPipe::~MonoPipe()
40{
41 free(mBuffer);
42}
43
44ssize_t MonoPipe::availableToWrite() const
45{
46 if (CC_UNLIKELY(!mNegotiated)) {
47 return NEGOTIATE;
48 }
Glenn Kasten820ba702012-05-30 16:34:17 -070049 // uses mMaxFrames not mReqFrames, so allows "over-filling" the pipe beyond requested limit
Glenn Kasten01066232012-02-27 11:50:44 -080050 ssize_t ret = mMaxFrames - (mRear - android_atomic_acquire_load(&mFront));
51 ALOG_ASSERT((0 <= ret) && (ret <= mMaxFrames));
52 return ret;
53}
54
55ssize_t MonoPipe::write(const void *buffer, size_t count)
56{
Glenn Kasten01066232012-02-27 11:50:44 -080057 if (CC_UNLIKELY(!mNegotiated)) {
58 return NEGOTIATE;
59 }
60 size_t totalFramesWritten = 0;
Glenn Kasten6d8aabe2012-05-18 10:47:28 -070061 while (count > 0) {
Glenn Kasten820ba702012-05-30 16:34:17 -070062 // can't return a negative value, as we already checked for !mNegotiated
Glenn Kasten6d8aabe2012-05-18 10:47:28 -070063 size_t avail = availableToWrite();
64 size_t written = avail;
Glenn Kasten01066232012-02-27 11:50:44 -080065 if (CC_LIKELY(written > count)) {
66 written = count;
67 }
68 size_t rear = mRear & (mMaxFrames - 1);
69 size_t part1 = mMaxFrames - rear;
70 if (part1 > written) {
71 part1 = written;
72 }
73 if (CC_LIKELY(part1 > 0)) {
74 memcpy((char *) mBuffer + (rear << mBitShift), buffer, part1 << mBitShift);
75 if (CC_UNLIKELY(rear + part1 == mMaxFrames)) {
76 size_t part2 = written - part1;
77 if (CC_LIKELY(part2 > 0)) {
78 memcpy(mBuffer, (char *) buffer + (part1 << mBitShift), part2 << mBitShift);
79 }
80 }
81 android_atomic_release_store(written + mRear, &mRear);
82 totalFramesWritten += written;
83 }
Glenn Kasten6d8aabe2012-05-18 10:47:28 -070084 if (!mWriteCanBlock) {
Glenn Kasten01066232012-02-27 11:50:44 -080085 break;
86 }
Glenn Kasten6d8aabe2012-05-18 10:47:28 -070087 count -= written;
Glenn Kasten01066232012-02-27 11:50:44 -080088 buffer = (char *) buffer + (written << mBitShift);
Glenn Kasten6d8aabe2012-05-18 10:47:28 -070089 // Simulate blocking I/O by sleeping at different rates, depending on a throttle.
Glenn Kasten820ba702012-05-30 16:34:17 -070090 // The throttle tries to keep the pipe about 11/16 full on average, with a slight jitter.
91 uint32_t ns;
Glenn Kasten6d8aabe2012-05-18 10:47:28 -070092 if (written > 0) {
Glenn Kasten820ba702012-05-30 16:34:17 -070093 size_t filled = (mMaxFrames - avail) + written;
94 // FIXME cache these values to avoid re-computation
95 if (filled <= mReqFrames / 4) {
96 // pipe is (nearly) empty, fill quickly
Glenn Kasten6d8aabe2012-05-18 10:47:28 -070097 ns = written * ( 500000000 / Format_sampleRate(mFormat));
Glenn Kasten820ba702012-05-30 16:34:17 -070098 } else if (filled <= mReqFrames / 2) {
99 // pipe is normal, fill at slightly faster rate
Glenn Kasten6d8aabe2012-05-18 10:47:28 -0700100 ns = written * ( 750000000 / Format_sampleRate(mFormat));
Glenn Kasten820ba702012-05-30 16:34:17 -0700101 } else if (filled <= (mReqFrames * 5) / 8) {
102 // pipe is normal, fill at nominal rate
Glenn Kasten6d8aabe2012-05-18 10:47:28 -0700103 ns = written * (1000000000 / Format_sampleRate(mFormat));
Glenn Kasten820ba702012-05-30 16:34:17 -0700104 } else if (filled <= (mReqFrames * 3) / 4) {
105 // pipe is normal, fill at slightly slower rate
Glenn Kasten6d8aabe2012-05-18 10:47:28 -0700106 ns = written * (1100000000 / Format_sampleRate(mFormat));
Glenn Kasten820ba702012-05-30 16:34:17 -0700107 } else {
108 // pipe is (nearly) full, fill slowly
Glenn Kasten6d8aabe2012-05-18 10:47:28 -0700109 ns = written * (1250000000 / Format_sampleRate(mFormat));
Glenn Kasten6d8aabe2012-05-18 10:47:28 -0700110 }
111 } else {
Glenn Kasten820ba702012-05-30 16:34:17 -0700112 ns = mReqFrames * (250000000 / Format_sampleRate(mFormat));
Glenn Kasten6d8aabe2012-05-18 10:47:28 -0700113 }
114 if (ns > 999999999) {
115 ns = 999999999;
116 }
117 struct timespec sleep;
118 sleep.tv_sec = 0;
119 sleep.tv_nsec = ns;
120 nanosleep(&sleep, NULL);
Glenn Kasten01066232012-02-27 11:50:44 -0800121 }
122 mFramesWritten += totalFramesWritten;
123 return totalFramesWritten;
124}
125
126} // namespace android