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