Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "FastCapture" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
| 21 | |
| 22 | #include "Configuration.h" |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 23 | #include <audio_utils/format.h> |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 24 | #include <linux/futex.h> |
| 25 | #include <sys/syscall.h> |
| 26 | #include <media/AudioBufferProvider.h> |
| 27 | #include <utils/Log.h> |
| 28 | #include <utils/Trace.h> |
| 29 | #include "FastCapture.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 33 | /*static*/ const FastCaptureState FastCapture::sInitial; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 34 | |
Glenn Kasten | f9715e4 | 2016-07-13 14:02:03 -0700 | [diff] [blame] | 35 | FastCapture::FastCapture() : FastThread("cycleC_ms", "loadC_us"), |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 36 | mInputSource(NULL), mInputSourceGen(0), mPipeSink(NULL), mPipeSinkGen(0), |
| 37 | mReadBuffer(NULL), mReadBufferState(-1), mFormat(Format_Invalid), mSampleRate(0), |
| 38 | // mDummyDumpState |
| 39 | mTotalNativeFramesRead(0) |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 40 | { |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 41 | mPrevious = &sInitial; |
| 42 | mCurrent = &sInitial; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 43 | |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 44 | mDummyDumpState = &mDummyFastCaptureDumpState; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | FastCapture::~FastCapture() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | FastCaptureStateQueue* FastCapture::sq() |
| 52 | { |
| 53 | return &mSQ; |
| 54 | } |
| 55 | |
| 56 | const FastThreadState *FastCapture::poll() |
| 57 | { |
| 58 | return mSQ.poll(); |
| 59 | } |
| 60 | |
Glenn Kasten | 3ab8d66 | 2017-04-03 14:35:09 -0700 | [diff] [blame] | 61 | void FastCapture::setNBLogWriter(NBLog::Writer *logWriter __unused) |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 62 | { |
| 63 | } |
| 64 | |
| 65 | void FastCapture::onIdle() |
| 66 | { |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 67 | mPreIdle = *(const FastCaptureState *)mCurrent; |
| 68 | mCurrent = &mPreIdle; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void FastCapture::onExit() |
| 72 | { |
Glenn Kasten | 5115777 | 2015-03-03 11:48:45 -0800 | [diff] [blame] | 73 | free(mReadBuffer); |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool FastCapture::isSubClassCommand(FastThreadState::Command command) |
| 77 | { |
| 78 | switch ((FastCaptureState::Command) command) { |
| 79 | case FastCaptureState::READ: |
| 80 | case FastCaptureState::WRITE: |
| 81 | case FastCaptureState::READ_WRITE: |
| 82 | return true; |
| 83 | default: |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void FastCapture::onStateChange() |
| 89 | { |
Glenn Kasten | 4dd03b5 | 2015-03-03 11:32:04 -0800 | [diff] [blame] | 90 | const FastCaptureState * const current = (const FastCaptureState *) mCurrent; |
| 91 | const FastCaptureState * const previous = (const FastCaptureState *) mPrevious; |
| 92 | FastCaptureDumpState * const dumpState = (FastCaptureDumpState *) mDumpState; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 93 | const size_t frameCount = current->mFrameCount; |
| 94 | |
| 95 | bool eitherChanged = false; |
| 96 | |
| 97 | // check for change in input HAL configuration |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 98 | NBAIO_Format previousFormat = mFormat; |
| 99 | if (current->mInputSourceGen != mInputSourceGen) { |
| 100 | mInputSource = current->mInputSource; |
| 101 | mInputSourceGen = current->mInputSourceGen; |
| 102 | if (mInputSource == NULL) { |
| 103 | mFormat = Format_Invalid; |
| 104 | mSampleRate = 0; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 105 | } else { |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 106 | mFormat = mInputSource->format(); |
| 107 | mSampleRate = Format_sampleRate(mFormat); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 108 | #if !LOG_NDEBUG |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 109 | unsigned channelCount = Format_channelCount(mFormat); |
Andy Hung | d330ee4 | 2015-04-20 13:23:41 -0700 | [diff] [blame] | 110 | ALOG_ASSERT(channelCount >= 1 && channelCount <= FCC_8); |
Glenn Kasten | 57c4e6f | 2016-03-18 14:54:07 -0700 | [diff] [blame] | 111 | #endif |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 112 | } |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 113 | dumpState->mSampleRate = mSampleRate; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 114 | eitherChanged = true; |
| 115 | } |
| 116 | |
| 117 | // check for change in pipe |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 118 | if (current->mPipeSinkGen != mPipeSinkGen) { |
| 119 | mPipeSink = current->mPipeSink; |
| 120 | mPipeSinkGen = current->mPipeSinkGen; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 121 | eitherChanged = true; |
| 122 | } |
| 123 | |
| 124 | // input source and pipe sink must be compatible |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 125 | if (eitherChanged && mInputSource != NULL && mPipeSink != NULL) { |
| 126 | ALOG_ASSERT(Format_isEqual(mFormat, mPipeSink->format())); |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 129 | if ((!Format_isEqual(mFormat, previousFormat)) || (frameCount != previous->mFrameCount)) { |
Glenn Kasten | 5115777 | 2015-03-03 11:48:45 -0800 | [diff] [blame] | 130 | // FIXME to avoid priority inversion, don't free here |
| 131 | free(mReadBuffer); |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 132 | mReadBuffer = NULL; |
| 133 | if (frameCount > 0 && mSampleRate > 0) { |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 134 | // FIXME new may block for unbounded time at internal mutex of the heap |
| 135 | // implementation; it would be better to have normal capture thread allocate for |
| 136 | // us to avoid blocking here and to prevent possible priority inversion |
Andy Hung | 0a01c2f | 2015-09-21 12:44:54 -0700 | [diff] [blame] | 137 | size_t bufferSize = frameCount * Format_frameSize(mFormat); |
| 138 | (void)posix_memalign(&mReadBuffer, 32, bufferSize); |
| 139 | memset(mReadBuffer, 0, bufferSize); // if posix_memalign fails, will segv here. |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 140 | mPeriodNs = (frameCount * 1000000000LL) / mSampleRate; // 1.00 |
| 141 | mUnderrunNs = (frameCount * 1750000000LL) / mSampleRate; // 1.75 |
| 142 | mOverrunNs = (frameCount * 500000000LL) / mSampleRate; // 0.50 |
| 143 | mForceNs = (frameCount * 950000000LL) / mSampleRate; // 0.95 |
| 144 | mWarmupNsMin = (frameCount * 750000000LL) / mSampleRate; // 0.75 |
| 145 | mWarmupNsMax = (frameCount * 1250000000LL) / mSampleRate; // 1.25 |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 146 | } else { |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 147 | mPeriodNs = 0; |
| 148 | mUnderrunNs = 0; |
| 149 | mOverrunNs = 0; |
| 150 | mForceNs = 0; |
| 151 | mWarmupNsMin = 0; |
| 152 | mWarmupNsMax = LONG_MAX; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 153 | } |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 154 | mReadBufferState = -1; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 155 | dumpState->mFrameCount = frameCount; |
| 156 | } |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 157 | dumpState->mSilenced = current->mSilenceCapture; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void FastCapture::onWork() |
| 161 | { |
Glenn Kasten | 4dd03b5 | 2015-03-03 11:32:04 -0800 | [diff] [blame] | 162 | const FastCaptureState * const current = (const FastCaptureState *) mCurrent; |
| 163 | FastCaptureDumpState * const dumpState = (FastCaptureDumpState *) mDumpState; |
| 164 | const FastCaptureState::Command command = mCommand; |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 165 | size_t frameCount = current->mFrameCount; |
| 166 | AudioBufferProvider* fastPatchRecordBufferProvider = current->mFastPatchRecordBufferProvider; |
| 167 | AudioBufferProvider::Buffer patchBuffer; |
| 168 | |
| 169 | if (fastPatchRecordBufferProvider != 0) { |
| 170 | patchBuffer.frameCount = ~0; |
| 171 | status_t status = fastPatchRecordBufferProvider->getNextBuffer(&patchBuffer); |
| 172 | if (status != NO_ERROR) { |
| 173 | frameCount = 0; |
| 174 | } else if (patchBuffer.frameCount < frameCount) { |
| 175 | // TODO: Make sure that it doesn't cause any issues if we just get a small available |
| 176 | // buffer from the buffer provider. |
| 177 | frameCount = patchBuffer.frameCount; |
| 178 | } |
| 179 | } |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 180 | |
| 181 | if ((command & FastCaptureState::READ) /*&& isWarm*/) { |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 182 | ALOG_ASSERT(mInputSource != NULL); |
| 183 | ALOG_ASSERT(mReadBuffer != NULL); |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 184 | dumpState->mReadSequence++; |
| 185 | ATRACE_BEGIN("read"); |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 186 | ssize_t framesRead = mInputSource->read(mReadBuffer, frameCount); |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 187 | ATRACE_END(); |
| 188 | dumpState->mReadSequence++; |
| 189 | if (framesRead >= 0) { |
| 190 | LOG_ALWAYS_FATAL_IF((size_t) framesRead > frameCount); |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 191 | mTotalNativeFramesRead += framesRead; |
| 192 | dumpState->mFramesRead = mTotalNativeFramesRead; |
| 193 | mReadBufferState = framesRead; |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 194 | patchBuffer.frameCount = framesRead; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 195 | } else { |
| 196 | dumpState->mReadErrors++; |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 197 | mReadBufferState = 0; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 198 | } |
| 199 | // FIXME rename to attemptedIO |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 200 | mAttemptedWrite = true; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (command & FastCaptureState::WRITE) { |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 204 | ALOG_ASSERT(mPipeSink != NULL); |
| 205 | ALOG_ASSERT(mReadBuffer != NULL); |
| 206 | if (mReadBufferState < 0) { |
Glenn Kasten | 5115777 | 2015-03-03 11:48:45 -0800 | [diff] [blame] | 207 | memset(mReadBuffer, 0, frameCount * Format_frameSize(mFormat)); |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 208 | mReadBufferState = frameCount; |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 209 | } |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 210 | if (mReadBufferState > 0) { |
Eric Laurent | 33403f0 | 2020-05-29 18:35:06 -0700 | [diff] [blame] | 211 | if (current->mSilenceCapture) { |
| 212 | memset(mReadBuffer, 0, mReadBufferState * Format_frameSize(mFormat)); |
| 213 | } |
Glenn Kasten | e4a7ce2 | 2015-03-03 11:23:17 -0800 | [diff] [blame] | 214 | ssize_t framesWritten = mPipeSink->write(mReadBuffer, mReadBufferState); |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 215 | audio_track_cblk_t* cblk = current->mCblk; |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 216 | if (fastPatchRecordBufferProvider != 0) { |
| 217 | // This indicates the fast track is a patch record, update the cblk by |
| 218 | // calling releaseBuffer(). |
| 219 | memcpy_by_audio_format(patchBuffer.raw, current->mFastPatchRecordFormat, |
| 220 | mReadBuffer, mFormat.mFormat, framesWritten * mFormat.mChannelCount); |
| 221 | patchBuffer.frameCount = framesWritten; |
| 222 | fastPatchRecordBufferProvider->releaseBuffer(&patchBuffer); |
| 223 | } else if (cblk != NULL && framesWritten > 0) { |
| 224 | // FIXME This supports at most one fast capture client. |
| 225 | // To handle multiple clients this could be converted to an array, |
| 226 | // or with a lot more work the control block could be shared by all clients. |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 227 | int32_t rear = cblk->u.mStreaming.mRear; |
| 228 | android_atomic_release_store(framesWritten + rear, &cblk->u.mStreaming.mRear); |
| 229 | cblk->mServer += framesWritten; |
| 230 | int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); |
| 231 | if (!(old & CBLK_FUTEX_WAKE)) { |
| 232 | // client is never in server process, so don't use FUTEX_WAKE_PRIVATE |
| 233 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, 1); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
Glenn Kasten | f91df1b | 2014-03-13 14:59:31 -0700 | [diff] [blame] | 240 | } // namespace android |