| 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 |  | 
| John Grossman | 2c3b2da | 2012-08-02 17:08:54 -0700 | [diff] [blame] | 20 | #include <common_time/cc_helper.h> | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 21 | #include <cutils/atomic.h> | 
 | 22 | #include <cutils/compiler.h> | 
| John Grossman | 2c3b2da | 2012-08-02 17:08:54 -0700 | [diff] [blame] | 23 | #include <utils/LinearTransform.h> | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 25 | #include <utils/Trace.h> | 
| Glenn Kasten | fc7992b | 2012-08-29 11:10:32 -0700 | [diff] [blame] | 26 | #include <media/AudioBufferProvider.h> | 
 | 27 | #include <media/nbaio/MonoPipe.h> | 
 | 28 | #include <media/nbaio/roundup.h> | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 29 |  | 
| John Grossman | 2c3b2da | 2012-08-02 17:08:54 -0700 | [diff] [blame] | 30 |  | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 31 | namespace android { | 
 | 32 |  | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 33 | MonoPipe::MonoPipe(size_t reqFrames, NBAIO_Format format, bool writeCanBlock) : | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 34 |         NBAIO_Sink(format), | 
| John Grossman | 2c3b2da | 2012-08-02 17:08:54 -0700 | [diff] [blame] | 35 |         mUpdateSeq(0), | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 36 |         mReqFrames(reqFrames), | 
 | 37 |         mMaxFrames(roundup(reqFrames)), | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 38 |         mBuffer(malloc(mMaxFrames * Format_frameSize(format))), | 
 | 39 |         mFront(0), | 
 | 40 |         mRear(0), | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 41 |         mWriteTsValid(false), | 
 | 42 |         // mWriteTs | 
 | 43 |         mSetpoint((reqFrames * 11) / 16), | 
| Glenn Kasten | 9bb6e65 | 2012-09-28 12:22:52 -0700 | [diff] [blame] | 44 |         mWriteCanBlock(writeCanBlock), | 
 | 45 |         mIsShutdown(false) | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 46 | { | 
| John Grossman | 2c3b2da | 2012-08-02 17:08:54 -0700 | [diff] [blame] | 47 |     CCHelper tmpHelper; | 
 | 48 |     status_t res; | 
 | 49 |     uint64_t N, D; | 
 | 50 |  | 
 | 51 |     mNextRdPTS = AudioBufferProvider::kInvalidPTS; | 
 | 52 |  | 
 | 53 |     mSamplesToLocalTime.a_zero = 0; | 
 | 54 |     mSamplesToLocalTime.b_zero = 0; | 
 | 55 |     mSamplesToLocalTime.a_to_b_numer = 0; | 
 | 56 |     mSamplesToLocalTime.a_to_b_denom = 0; | 
 | 57 |  | 
 | 58 |     D = Format_sampleRate(format); | 
 | 59 |     if (OK != (res = tmpHelper.getLocalFreq(&N))) { | 
 | 60 |         ALOGE("Failed to fetch local time frequency when constructing a" | 
 | 61 |               " MonoPipe (res = %d).  getNextWriteTimestamp calls will be" | 
 | 62 |               " non-functional", res); | 
 | 63 |         return; | 
 | 64 |     } | 
 | 65 |  | 
 | 66 |     LinearTransform::reduce(&N, &D); | 
 | 67 |     static const uint64_t kSignedHiBitsMask   = ~(0x7FFFFFFFull); | 
 | 68 |     static const uint64_t kUnsignedHiBitsMask = ~(0xFFFFFFFFull); | 
 | 69 |     if ((N & kSignedHiBitsMask) || (D & kUnsignedHiBitsMask)) { | 
 | 70 |         ALOGE("Cannot reduce sample rate to local clock frequency ratio to fit" | 
 | 71 |               " in a 32/32 bit rational.  (max reduction is 0x%016llx/0x%016llx" | 
 | 72 |               ").  getNextWriteTimestamp calls will be non-functional", N, D); | 
 | 73 |         return; | 
 | 74 |     } | 
 | 75 |  | 
 | 76 |     mSamplesToLocalTime.a_to_b_numer = static_cast<int32_t>(N); | 
 | 77 |     mSamplesToLocalTime.a_to_b_denom = static_cast<uint32_t>(D); | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 78 | } | 
 | 79 |  | 
 | 80 | MonoPipe::~MonoPipe() | 
 | 81 | { | 
 | 82 |     free(mBuffer); | 
 | 83 | } | 
 | 84 |  | 
 | 85 | ssize_t MonoPipe::availableToWrite() const | 
 | 86 | { | 
 | 87 |     if (CC_UNLIKELY(!mNegotiated)) { | 
 | 88 |         return NEGOTIATE; | 
 | 89 |     } | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 90 |     // uses mMaxFrames not mReqFrames, so allows "over-filling" the pipe beyond requested limit | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 91 |     ssize_t ret = mMaxFrames - (mRear - android_atomic_acquire_load(&mFront)); | 
 | 92 |     ALOG_ASSERT((0 <= ret) && (ret <= mMaxFrames)); | 
 | 93 |     return ret; | 
 | 94 | } | 
 | 95 |  | 
 | 96 | ssize_t MonoPipe::write(const void *buffer, size_t count) | 
 | 97 | { | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 98 |     if (CC_UNLIKELY(!mNegotiated)) { | 
 | 99 |         return NEGOTIATE; | 
 | 100 |     } | 
 | 101 |     size_t totalFramesWritten = 0; | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 102 |     while (count > 0) { | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 103 |         // can't return a negative value, as we already checked for !mNegotiated | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 104 |         size_t avail = availableToWrite(); | 
 | 105 |         size_t written = avail; | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 106 |         if (CC_LIKELY(written > count)) { | 
 | 107 |             written = count; | 
 | 108 |         } | 
 | 109 |         size_t rear = mRear & (mMaxFrames - 1); | 
 | 110 |         size_t part1 = mMaxFrames - rear; | 
 | 111 |         if (part1 > written) { | 
 | 112 |             part1 = written; | 
 | 113 |         } | 
 | 114 |         if (CC_LIKELY(part1 > 0)) { | 
 | 115 |             memcpy((char *) mBuffer + (rear << mBitShift), buffer, part1 << mBitShift); | 
 | 116 |             if (CC_UNLIKELY(rear + part1 == mMaxFrames)) { | 
 | 117 |                 size_t part2 = written - part1; | 
 | 118 |                 if (CC_LIKELY(part2 > 0)) { | 
 | 119 |                     memcpy(mBuffer, (char *) buffer + (part1 << mBitShift), part2 << mBitShift); | 
 | 120 |                 } | 
 | 121 |             } | 
 | 122 |             android_atomic_release_store(written + mRear, &mRear); | 
 | 123 |             totalFramesWritten += written; | 
 | 124 |         } | 
| Glenn Kasten | 9bb6e65 | 2012-09-28 12:22:52 -0700 | [diff] [blame] | 125 |         if (!mWriteCanBlock || mIsShutdown) { | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 126 |             break; | 
 | 127 |         } | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 128 |         count -= written; | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 129 |         buffer = (char *) buffer + (written << mBitShift); | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 130 |         // Simulate blocking I/O by sleeping at different rates, depending on a throttle. | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 131 |         // The throttle tries to keep the mean pipe depth near the setpoint, with a slight jitter. | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 132 |         uint32_t ns; | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 133 |         if (written > 0) { | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 134 |             size_t filled = (mMaxFrames - avail) + written; | 
 | 135 |             // FIXME cache these values to avoid re-computation | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 136 |             if (filled <= mSetpoint / 2) { | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 137 |                 // pipe is (nearly) empty, fill quickly | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 138 |                 ns = written * ( 500000000 / Format_sampleRate(mFormat)); | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 139 |             } else if (filled <= (mSetpoint * 3) / 4) { | 
 | 140 |                 // pipe is below setpoint, fill at slightly faster rate | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 141 |                 ns = written * ( 750000000 / Format_sampleRate(mFormat)); | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 142 |             } else if (filled <= (mSetpoint * 5) / 4) { | 
 | 143 |                 // pipe is at setpoint, fill at nominal rate | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 144 |                 ns = written * (1000000000 / Format_sampleRate(mFormat)); | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 145 |             } else if (filled <= (mSetpoint * 3) / 2) { | 
 | 146 |                 // pipe is above setpoint, fill at slightly slower rate | 
 | 147 |                 ns = written * (1150000000 / Format_sampleRate(mFormat)); | 
 | 148 |             } else if (filled <= (mSetpoint * 7) / 4) { | 
 | 149 |                 // pipe is overflowing, fill slowly | 
 | 150 |                 ns = written * (1350000000 / Format_sampleRate(mFormat)); | 
| Glenn Kasten | 820ba70 | 2012-05-30 16:34:17 -0700 | [diff] [blame] | 151 |             } else { | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 152 |                 // pipe is severely overflowing | 
 | 153 |                 ns = written * (1750000000 / Format_sampleRate(mFormat)); | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 154 |             } | 
 | 155 |         } else { | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 156 |             ns = count * (1350000000 / Format_sampleRate(mFormat)); | 
| Glenn Kasten | 6d8aabe | 2012-05-18 10:47:28 -0700 | [diff] [blame] | 157 |         } | 
 | 158 |         if (ns > 999999999) { | 
 | 159 |             ns = 999999999; | 
 | 160 |         } | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 161 |         struct timespec nowTs; | 
 | 162 |         bool nowTsValid = !clock_gettime(CLOCK_MONOTONIC, &nowTs); | 
 | 163 |         // deduct the elapsed time since previous write() completed | 
 | 164 |         if (nowTsValid && mWriteTsValid) { | 
 | 165 |             time_t sec = nowTs.tv_sec - mWriteTs.tv_sec; | 
 | 166 |             long nsec = nowTs.tv_nsec - mWriteTs.tv_nsec; | 
| Glenn Kasten | 99ae06b | 2012-09-24 11:29:00 -0700 | [diff] [blame] | 167 |             ALOGE_IF(sec < 0 || (sec == 0 && nsec < 0), | 
 | 168 |                     "clock_gettime(CLOCK_MONOTONIC) failed: was %ld.%09ld but now %ld.%09ld", | 
 | 169 |                     mWriteTs.tv_sec, mWriteTs.tv_nsec, nowTs.tv_sec, nowTs.tv_nsec); | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 170 |             if (nsec < 0) { | 
 | 171 |                 --sec; | 
 | 172 |                 nsec += 1000000000; | 
 | 173 |             } | 
 | 174 |             if (sec == 0) { | 
 | 175 |                 if ((long) ns > nsec) { | 
 | 176 |                     ns -= nsec; | 
 | 177 |                 } else { | 
 | 178 |                     ns = 0; | 
 | 179 |                 } | 
 | 180 |             } | 
 | 181 |         } | 
 | 182 |         if (ns > 0) { | 
| synergydev | 3b546ca | 2013-10-25 12:36:28 -0700 | [diff] [blame^] | 183 |             const struct timespec req = {0, static_cast<long>(ns)}; | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 184 |             nanosleep(&req, NULL); | 
 | 185 |         } | 
 | 186 |         // record the time that this write() completed | 
 | 187 |         if (nowTsValid) { | 
 | 188 |             mWriteTs = nowTs; | 
 | 189 |             if ((mWriteTs.tv_nsec += ns) >= 1000000000) { | 
 | 190 |                 mWriteTs.tv_nsec -= 1000000000; | 
 | 191 |                 ++mWriteTs.tv_sec; | 
 | 192 |             } | 
 | 193 |         } | 
 | 194 |         mWriteTsValid = nowTsValid; | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 195 |     } | 
 | 196 |     mFramesWritten += totalFramesWritten; | 
 | 197 |     return totalFramesWritten; | 
 | 198 | } | 
 | 199 |  | 
| Glenn Kasten | 28ed2f9 | 2012-06-07 10:17:54 -0700 | [diff] [blame] | 200 | void MonoPipe::setAvgFrames(size_t setpoint) | 
 | 201 | { | 
 | 202 |     mSetpoint = setpoint; | 
 | 203 | } | 
 | 204 |  | 
| John Grossman | 2c3b2da | 2012-08-02 17:08:54 -0700 | [diff] [blame] | 205 | status_t MonoPipe::getNextWriteTimestamp(int64_t *timestamp) | 
 | 206 | { | 
 | 207 |     int32_t front; | 
 | 208 |  | 
 | 209 |     ALOG_ASSERT(NULL != timestamp); | 
 | 210 |  | 
 | 211 |     if (0 == mSamplesToLocalTime.a_to_b_denom) | 
 | 212 |         return UNKNOWN_ERROR; | 
 | 213 |  | 
 | 214 |     observeFrontAndNRPTS(&front, timestamp); | 
 | 215 |  | 
 | 216 |     if (AudioBufferProvider::kInvalidPTS != *timestamp) { | 
 | 217 |         // If we have a valid read-pointer and next read timestamp pair, then | 
 | 218 |         // use the current value of the write pointer to figure out how many | 
 | 219 |         // frames are in the buffer, and offset the timestamp by that amt.  Then | 
 | 220 |         // next time we write to the MonoPipe, the data will hit the speakers at | 
 | 221 |         // the next read timestamp plus the current amount of data in the | 
 | 222 |         // MonoPipe. | 
 | 223 |         size_t pendingFrames = (mRear - front) & (mMaxFrames - 1); | 
 | 224 |         *timestamp = offsetTimestampByAudioFrames(*timestamp, pendingFrames); | 
 | 225 |     } | 
 | 226 |  | 
 | 227 |     return OK; | 
 | 228 | } | 
 | 229 |  | 
 | 230 | void MonoPipe::updateFrontAndNRPTS(int32_t newFront, int64_t newNextRdPTS) | 
 | 231 | { | 
 | 232 |     // Set the MSB of the update sequence number to indicate that there is a | 
 | 233 |     // multi-variable update in progress.  Use an atomic store with an "acquire" | 
 | 234 |     // barrier to make sure that the next operations cannot be re-ordered and | 
 | 235 |     // take place before the change to mUpdateSeq is commited.. | 
 | 236 |     int32_t tmp = mUpdateSeq | 0x80000000; | 
 | 237 |     android_atomic_acquire_store(tmp, &mUpdateSeq); | 
 | 238 |  | 
 | 239 |     // Update mFront and mNextRdPTS | 
 | 240 |     mFront = newFront; | 
 | 241 |     mNextRdPTS = newNextRdPTS; | 
 | 242 |  | 
 | 243 |     // We are finished with the update.  Compute the next sequnce number (which | 
 | 244 |     // should be the old sequence number, plus one, and with the MSB cleared) | 
 | 245 |     // and then store it in mUpdateSeq using an atomic store with a "release" | 
 | 246 |     // barrier so our update operations cannot be re-ordered past the update of | 
 | 247 |     // the sequence number. | 
 | 248 |     tmp = (tmp + 1) & 0x7FFFFFFF; | 
 | 249 |     android_atomic_release_store(tmp, &mUpdateSeq); | 
 | 250 | } | 
 | 251 |  | 
 | 252 | void MonoPipe::observeFrontAndNRPTS(int32_t *outFront, int64_t *outNextRdPTS) | 
 | 253 | { | 
 | 254 |     // Perform an atomic observation of mFront and mNextRdPTS.  Basically, | 
 | 255 |     // atomically observe the sequence number, then observer the variables, then | 
 | 256 |     // atomically observe the sequence number again.  If the two observations of | 
 | 257 |     // the sequence number match, and the update-in-progress bit was not set, | 
 | 258 |     // then we know we have a successful atomic observation.  Otherwise, we loop | 
 | 259 |     // around and try again. | 
 | 260 |     // | 
 | 261 |     // Note, it is very important that the observer be a lower priority thread | 
 | 262 |     // than the updater.  If the updater is lower than the observer, or they are | 
 | 263 |     // the same priority and running with SCHED_FIFO (implying that quantum | 
 | 264 |     // based premption is disabled) then we run the risk of deadlock. | 
 | 265 |     int32_t seqOne, seqTwo; | 
 | 266 |  | 
 | 267 |     do { | 
 | 268 |         seqOne        = android_atomic_acquire_load(&mUpdateSeq); | 
 | 269 |         *outFront     = mFront; | 
 | 270 |         *outNextRdPTS = mNextRdPTS; | 
 | 271 |         seqTwo        = android_atomic_release_load(&mUpdateSeq); | 
 | 272 |     } while ((seqOne != seqTwo) || (seqOne & 0x80000000)); | 
 | 273 | } | 
 | 274 |  | 
 | 275 | int64_t MonoPipe::offsetTimestampByAudioFrames(int64_t ts, size_t audFrames) | 
 | 276 | { | 
 | 277 |     if (0 == mSamplesToLocalTime.a_to_b_denom) | 
 | 278 |         return AudioBufferProvider::kInvalidPTS; | 
 | 279 |  | 
 | 280 |     if (ts == AudioBufferProvider::kInvalidPTS) | 
 | 281 |         return AudioBufferProvider::kInvalidPTS; | 
 | 282 |  | 
 | 283 |     int64_t frame_lt_duration; | 
 | 284 |     if (!mSamplesToLocalTime.doForwardTransform(audFrames, | 
 | 285 |                                                 &frame_lt_duration)) { | 
 | 286 |         // This should never fail, but if there is a bug which is causing it | 
 | 287 |         // to fail, this message would probably end up flooding the logs | 
 | 288 |         // because the conversion would probably fail forever.  Log the | 
 | 289 |         // error, but then zero out the ratio in the linear transform so | 
 | 290 |         // that we don't try to do any conversions from now on.  This | 
 | 291 |         // MonoPipe's getNextWriteTimestamp is now broken for good. | 
 | 292 |         ALOGE("Overflow when attempting to convert %d audio frames to" | 
 | 293 |               " duration in local time.  getNextWriteTimestamp will fail from" | 
 | 294 |               " now on.", audFrames); | 
 | 295 |         mSamplesToLocalTime.a_to_b_numer = 0; | 
 | 296 |         mSamplesToLocalTime.a_to_b_denom = 0; | 
 | 297 |         return AudioBufferProvider::kInvalidPTS; | 
 | 298 |     } | 
 | 299 |  | 
 | 300 |     return ts + frame_lt_duration; | 
 | 301 | } | 
 | 302 |  | 
| Glenn Kasten | 9bb6e65 | 2012-09-28 12:22:52 -0700 | [diff] [blame] | 303 | void MonoPipe::shutdown(bool newState) | 
 | 304 | { | 
 | 305 |     mIsShutdown = newState; | 
 | 306 | } | 
 | 307 |  | 
 | 308 | bool MonoPipe::isShutdown() | 
 | 309 | { | 
 | 310 |     return mIsShutdown; | 
 | 311 | } | 
 | 312 |  | 
| Glenn Kasten | 0106623 | 2012-02-27 11:50:44 -0800 | [diff] [blame] | 313 | }   // namespace android |