The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/extlibs/pv/android/AudioTrack.cpp |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "AudioTrack" |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <limits.h> |
| 25 | |
| 26 | #include <sched.h> |
| 27 | #include <sys/resource.h> |
| 28 | |
| 29 | #include <private/media/AudioTrackShared.h> |
| 30 | |
| 31 | #include <media/AudioSystem.h> |
| 32 | #include <media/AudioTrack.h> |
| 33 | |
| 34 | #include <utils/Log.h> |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/Parcel.h> |
| 36 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <utils/Timers.h> |
| 38 | #include <cutils/atomic.h> |
| 39 | |
| 40 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 41 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | AudioTrack::AudioTrack() |
| 48 | : mStatus(NO_INIT) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | AudioTrack::AudioTrack( |
| 53 | int streamType, |
| 54 | uint32_t sampleRate, |
| 55 | int format, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 56 | int channels, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | int frameCount, |
| 58 | uint32_t flags, |
| 59 | callback_t cbf, |
| 60 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 61 | int notificationFrames, |
| 62 | int sessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | : mStatus(NO_INIT) |
| 64 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 65 | mStatus = set(streamType, sampleRate, format, channels, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | frameCount, flags, cbf, user, notificationFrames, 0); |
| 67 | } |
| 68 | |
| 69 | AudioTrack::AudioTrack( |
| 70 | int streamType, |
| 71 | uint32_t sampleRate, |
| 72 | int format, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 73 | int channels, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | const sp<IMemory>& sharedBuffer, |
| 75 | uint32_t flags, |
| 76 | callback_t cbf, |
| 77 | void* user, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 78 | int notificationFrames, |
| 79 | int sessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | : mStatus(NO_INIT) |
| 81 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 82 | mStatus = set(streamType, sampleRate, format, channels, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | 0, flags, cbf, user, notificationFrames, sharedBuffer); |
| 84 | } |
| 85 | |
| 86 | AudioTrack::~AudioTrack() |
| 87 | { |
| 88 | LOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer()); |
| 89 | |
| 90 | if (mStatus == NO_ERROR) { |
| 91 | // Make sure that callback function exits in the case where |
| 92 | // it is looping on buffer full condition in obtainBuffer(). |
| 93 | // Otherwise the callback thread will never exit. |
| 94 | stop(); |
| 95 | if (mAudioTrackThread != 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | mAudioTrackThread->requestExitAndWait(); |
| 97 | mAudioTrackThread.clear(); |
| 98 | } |
| 99 | mAudioTrack.clear(); |
| 100 | IPCThreadState::self()->flushCommands(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | status_t AudioTrack::set( |
| 105 | int streamType, |
| 106 | uint32_t sampleRate, |
| 107 | int format, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 108 | int channels, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | int frameCount, |
| 110 | uint32_t flags, |
| 111 | callback_t cbf, |
| 112 | void* user, |
| 113 | int notificationFrames, |
| 114 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 115 | bool threadCanCallJava, |
| 116 | int sessionId) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | { |
| 118 | |
| 119 | LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
| 120 | |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 121 | if (mAudioTrack != 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | LOGE("Track already in use"); |
| 123 | return INVALID_OPERATION; |
| 124 | } |
| 125 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | int afSampleRate; |
| 127 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 128 | return NO_INIT; |
| 129 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | uint32_t afLatency; |
| 131 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 132 | return NO_INIT; |
| 133 | } |
| 134 | |
| 135 | // handle default values first. |
| 136 | if (streamType == AudioSystem::DEFAULT) { |
| 137 | streamType = AudioSystem::MUSIC; |
| 138 | } |
| 139 | if (sampleRate == 0) { |
| 140 | sampleRate = afSampleRate; |
| 141 | } |
| 142 | // these below should probably come from the audioFlinger too... |
| 143 | if (format == 0) { |
| 144 | format = AudioSystem::PCM_16_BIT; |
| 145 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 146 | if (channels == 0) { |
| 147 | channels = AudioSystem::CHANNEL_OUT_STEREO; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // validate parameters |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 151 | if (!AudioSystem::isValidFormat(format)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | LOGE("Invalid format"); |
| 153 | return BAD_VALUE; |
| 154 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 155 | |
| 156 | // force direct flag if format is not linear PCM |
| 157 | if (!AudioSystem::isLinearPCM(format)) { |
| 158 | flags |= AudioSystem::OUTPUT_FLAG_DIRECT; |
| 159 | } |
| 160 | |
| 161 | if (!AudioSystem::isOutputChannel(channels)) { |
| 162 | LOGE("Invalid channel mask"); |
| 163 | return BAD_VALUE; |
| 164 | } |
| 165 | uint32_t channelCount = AudioSystem::popCount(channels); |
| 166 | |
| 167 | audio_io_handle_t output = AudioSystem::getOutput((AudioSystem::stream_type)streamType, |
| 168 | sampleRate, format, channels, (AudioSystem::output_flags)flags); |
| 169 | |
| 170 | if (output == 0) { |
| 171 | LOGE("Could not get audio output for stream type %d", streamType); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | return BAD_VALUE; |
| 173 | } |
| 174 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 175 | mVolume[LEFT] = 1.0f; |
| 176 | mVolume[RIGHT] = 1.0f; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 177 | mSendLevel = 0; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 178 | mFrameCount = frameCount; |
| 179 | mNotificationFramesReq = notificationFrames; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 180 | mSessionId = sessionId; |
| 181 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 182 | // create the IAudioTrack |
| 183 | status_t status = createTrack(streamType, sampleRate, format, channelCount, |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 184 | frameCount, flags, sharedBuffer, output, true); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 186 | if (status != NO_ERROR) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | return status; |
| 188 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 189 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | if (cbf != 0) { |
| 191 | mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); |
| 192 | if (mAudioTrackThread == 0) { |
| 193 | LOGE("Could not create callback thread"); |
| 194 | return NO_INIT; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | mStatus = NO_ERROR; |
| 199 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | mStreamType = streamType; |
| 201 | mFormat = format; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 202 | mChannels = channels; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | mChannelCount = channelCount; |
| 204 | mSharedBuffer = sharedBuffer; |
| 205 | mMuted = false; |
| 206 | mActive = 0; |
| 207 | mCbf = cbf; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | mUserData = user; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | mLoopCount = 0; |
| 210 | mMarkerPosition = 0; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 211 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | mNewPosition = 0; |
| 213 | mUpdatePeriod = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 214 | mFlags = flags; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | |
| 216 | return NO_ERROR; |
| 217 | } |
| 218 | |
| 219 | status_t AudioTrack::initCheck() const |
| 220 | { |
| 221 | return mStatus; |
| 222 | } |
| 223 | |
| 224 | // ------------------------------------------------------------------------- |
| 225 | |
| 226 | uint32_t AudioTrack::latency() const |
| 227 | { |
| 228 | return mLatency; |
| 229 | } |
| 230 | |
| 231 | int AudioTrack::streamType() const |
| 232 | { |
| 233 | return mStreamType; |
| 234 | } |
| 235 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | int AudioTrack::format() const |
| 237 | { |
| 238 | return mFormat; |
| 239 | } |
| 240 | |
| 241 | int AudioTrack::channelCount() const |
| 242 | { |
| 243 | return mChannelCount; |
| 244 | } |
| 245 | |
| 246 | uint32_t AudioTrack::frameCount() const |
| 247 | { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 248 | return mCblk->frameCount; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | int AudioTrack::frameSize() const |
| 252 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 253 | if (AudioSystem::isLinearPCM(mFormat)) { |
| 254 | return channelCount()*((format() == AudioSystem::PCM_8_BIT) ? sizeof(uint8_t) : sizeof(int16_t)); |
| 255 | } else { |
| 256 | return sizeof(uint8_t); |
| 257 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | sp<IMemory>& AudioTrack::sharedBuffer() |
| 261 | { |
| 262 | return mSharedBuffer; |
| 263 | } |
| 264 | |
| 265 | // ------------------------------------------------------------------------- |
| 266 | |
| 267 | void AudioTrack::start() |
| 268 | { |
| 269 | sp<AudioTrackThread> t = mAudioTrackThread; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 270 | status_t status; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | |
| 272 | LOGV("start %p", this); |
| 273 | if (t != 0) { |
| 274 | if (t->exitPending()) { |
| 275 | if (t->requestExitAndWait() == WOULD_BLOCK) { |
| 276 | LOGE("AudioTrack::start called from thread"); |
| 277 | return; |
| 278 | } |
| 279 | } |
| 280 | t->mLock.lock(); |
| 281 | } |
| 282 | |
| 283 | if (android_atomic_or(1, &mActive) == 0) { |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 284 | mNewPosition = mCblk->server + mUpdatePeriod; |
| 285 | mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 286 | mCblk->waitTimeMs = 0; |
| 287 | if (t != 0) { |
| 288 | t->run("AudioTrackThread", THREAD_PRIORITY_AUDIO_CLIENT); |
| 289 | } else { |
| 290 | setpriority(PRIO_PROCESS, 0, THREAD_PRIORITY_AUDIO_CLIENT); |
| 291 | } |
| 292 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 293 | if (mCblk->flags & CBLK_INVALID_MSK) { |
| 294 | LOGW("start() track %p invalidated, creating a new one", this); |
| 295 | // no need to clear the invalid flag as this cblk will not be used anymore |
| 296 | // force new track creation |
| 297 | status = DEAD_OBJECT; |
| 298 | } else { |
| 299 | status = mAudioTrack->start(); |
| 300 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 301 | if (status == DEAD_OBJECT) { |
| 302 | LOGV("start() dead IAudioTrack: creating a new one"); |
| 303 | status = createTrack(mStreamType, mCblk->sampleRate, mFormat, mChannelCount, |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 304 | mFrameCount, mFlags, mSharedBuffer, getOutput(), false); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 305 | if (status == NO_ERROR) { |
| 306 | status = mAudioTrack->start(); |
| 307 | if (status == NO_ERROR) { |
| 308 | mNewPosition = mCblk->server + mUpdatePeriod; |
| 309 | } |
| 310 | } |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 311 | } |
| 312 | if (status != NO_ERROR) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 313 | LOGV("start() failed"); |
| 314 | android_atomic_and(~1, &mActive); |
Eric Laurent | 2b58424 | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 315 | if (t != 0) { |
| 316 | t->requestExit(); |
| 317 | } else { |
| 318 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL); |
| 319 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 320 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (t != 0) { |
| 324 | t->mLock.unlock(); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void AudioTrack::stop() |
| 329 | { |
| 330 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 331 | |
| 332 | LOGV("stop %p", this); |
| 333 | if (t != 0) { |
| 334 | t->mLock.lock(); |
| 335 | } |
| 336 | |
| 337 | if (android_atomic_and(~1, &mActive) == 1) { |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 338 | mCblk->cv.signal(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | mAudioTrack->stop(); |
| 340 | // Cancel loops (If we are in the middle of a loop, playback |
| 341 | // would not stop until loopCount reaches 0). |
| 342 | setLoop(0, 0, 0); |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 343 | // the playback head position will reset to 0, so if a marker is set, we need |
| 344 | // to activate it again |
| 345 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | // Force flush if a shared buffer is used otherwise audioflinger |
| 347 | // will not stop before end of buffer is reached. |
| 348 | if (mSharedBuffer != 0) { |
| 349 | flush(); |
| 350 | } |
| 351 | if (t != 0) { |
| 352 | t->requestExit(); |
| 353 | } else { |
| 354 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (t != 0) { |
| 359 | t->mLock.unlock(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | bool AudioTrack::stopped() const |
| 364 | { |
| 365 | return !mActive; |
| 366 | } |
| 367 | |
| 368 | void AudioTrack::flush() |
| 369 | { |
| 370 | LOGV("flush"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 371 | |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 372 | // clear playback marker and periodic update counter |
| 373 | mMarkerPosition = 0; |
| 374 | mMarkerReached = false; |
| 375 | mUpdatePeriod = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 376 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | |
| 378 | if (!mActive) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | mAudioTrack->flush(); |
| 380 | // Release AudioTrack callback thread in case it was waiting for new buffers |
| 381 | // in AudioTrack::obtainBuffer() |
| 382 | mCblk->cv.signal(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | void AudioTrack::pause() |
| 387 | { |
| 388 | LOGV("pause"); |
| 389 | if (android_atomic_and(~1, &mActive) == 1) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | mAudioTrack->pause(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void AudioTrack::mute(bool e) |
| 395 | { |
| 396 | mAudioTrack->mute(e); |
| 397 | mMuted = e; |
| 398 | } |
| 399 | |
| 400 | bool AudioTrack::muted() const |
| 401 | { |
| 402 | return mMuted; |
| 403 | } |
| 404 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 405 | status_t AudioTrack::setVolume(float left, float right) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 407 | if (left > 1.0f || right > 1.0f) { |
| 408 | return BAD_VALUE; |
| 409 | } |
| 410 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | mVolume[LEFT] = left; |
| 412 | mVolume[RIGHT] = right; |
| 413 | |
| 414 | // write must be atomic |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 415 | mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000); |
| 416 | |
| 417 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void AudioTrack::getVolume(float* left, float* right) |
| 421 | { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 422 | if (left != NULL) { |
| 423 | *left = mVolume[LEFT]; |
| 424 | } |
| 425 | if (right != NULL) { |
| 426 | *right = mVolume[RIGHT]; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | status_t AudioTrack::setSendLevel(float level) |
| 431 | { |
| 432 | if (level > 1.0f) { |
| 433 | return BAD_VALUE; |
| 434 | } |
| 435 | |
| 436 | mSendLevel = level; |
| 437 | |
| 438 | mCblk->sendLevel = uint16_t(level * 0x1000); |
| 439 | |
| 440 | return NO_ERROR; |
| 441 | } |
| 442 | |
| 443 | void AudioTrack::getSendLevel(float* level) |
| 444 | { |
| 445 | if (level != NULL) { |
| 446 | *level = mSendLevel; |
| 447 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 450 | status_t AudioTrack::setSampleRate(int rate) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | { |
| 452 | int afSamplingRate; |
| 453 | |
| 454 | if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) { |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 455 | return NO_INIT; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | } |
| 457 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 458 | if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 460 | mCblk->sampleRate = rate; |
| 461 | return NO_ERROR; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | uint32_t AudioTrack::getSampleRate() |
| 465 | { |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 466 | return mCblk->sampleRate; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 470 | { |
| 471 | audio_track_cblk_t* cblk = mCblk; |
| 472 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | Mutex::Autolock _l(cblk->lock); |
| 474 | |
| 475 | if (loopCount == 0) { |
| 476 | cblk->loopStart = UINT_MAX; |
| 477 | cblk->loopEnd = UINT_MAX; |
| 478 | cblk->loopCount = 0; |
| 479 | mLoopCount = 0; |
| 480 | return NO_ERROR; |
| 481 | } |
| 482 | |
| 483 | if (loopStart >= loopEnd || |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 484 | loopEnd - loopStart > cblk->frameCount) { |
| 485 | LOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | return BAD_VALUE; |
| 487 | } |
| 488 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 489 | if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | LOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d", |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 491 | loopStart, loopEnd, cblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 493 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | |
| 495 | cblk->loopStart = loopStart; |
| 496 | cblk->loopEnd = loopEnd; |
| 497 | cblk->loopCount = loopCount; |
| 498 | mLoopCount = loopCount; |
| 499 | |
| 500 | return NO_ERROR; |
| 501 | } |
| 502 | |
| 503 | status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount) |
| 504 | { |
| 505 | if (loopStart != 0) { |
| 506 | *loopStart = mCblk->loopStart; |
| 507 | } |
| 508 | if (loopEnd != 0) { |
| 509 | *loopEnd = mCblk->loopEnd; |
| 510 | } |
| 511 | if (loopCount != 0) { |
| 512 | if (mCblk->loopCount < 0) { |
| 513 | *loopCount = -1; |
| 514 | } else { |
| 515 | *loopCount = mCblk->loopCount; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | return NO_ERROR; |
| 520 | } |
| 521 | |
| 522 | status_t AudioTrack::setMarkerPosition(uint32_t marker) |
| 523 | { |
| 524 | if (mCbf == 0) return INVALID_OPERATION; |
| 525 | |
| 526 | mMarkerPosition = marker; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 527 | mMarkerReached = false; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | |
| 529 | return NO_ERROR; |
| 530 | } |
| 531 | |
| 532 | status_t AudioTrack::getMarkerPosition(uint32_t *marker) |
| 533 | { |
| 534 | if (marker == 0) return BAD_VALUE; |
| 535 | |
| 536 | *marker = mMarkerPosition; |
| 537 | |
| 538 | return NO_ERROR; |
| 539 | } |
| 540 | |
| 541 | status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 542 | { |
| 543 | if (mCbf == 0) return INVALID_OPERATION; |
| 544 | |
| 545 | uint32_t curPosition; |
| 546 | getPosition(&curPosition); |
| 547 | mNewPosition = curPosition + updatePeriod; |
| 548 | mUpdatePeriod = updatePeriod; |
| 549 | |
| 550 | return NO_ERROR; |
| 551 | } |
| 552 | |
| 553 | status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) |
| 554 | { |
| 555 | if (updatePeriod == 0) return BAD_VALUE; |
| 556 | |
| 557 | *updatePeriod = mUpdatePeriod; |
| 558 | |
| 559 | return NO_ERROR; |
| 560 | } |
| 561 | |
| 562 | status_t AudioTrack::setPosition(uint32_t position) |
| 563 | { |
| 564 | Mutex::Autolock _l(mCblk->lock); |
| 565 | |
| 566 | if (!stopped()) return INVALID_OPERATION; |
| 567 | |
| 568 | if (position > mCblk->user) return BAD_VALUE; |
| 569 | |
| 570 | mCblk->server = position; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 571 | mCblk->flags |= CBLK_FORCEREADY_ON; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 572 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | return NO_ERROR; |
| 574 | } |
| 575 | |
| 576 | status_t AudioTrack::getPosition(uint32_t *position) |
| 577 | { |
| 578 | if (position == 0) return BAD_VALUE; |
| 579 | |
| 580 | *position = mCblk->server; |
| 581 | |
| 582 | return NO_ERROR; |
| 583 | } |
| 584 | |
| 585 | status_t AudioTrack::reload() |
| 586 | { |
| 587 | if (!stopped()) return INVALID_OPERATION; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 588 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | flush(); |
| 590 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 591 | mCblk->stepUser(mCblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | |
| 593 | return NO_ERROR; |
| 594 | } |
| 595 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 596 | audio_io_handle_t AudioTrack::getOutput() |
| 597 | { |
| 598 | return AudioSystem::getOutput((AudioSystem::stream_type)mStreamType, |
| 599 | mCblk->sampleRate, mFormat, mChannels, (AudioSystem::output_flags)mFlags); |
| 600 | } |
| 601 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 602 | int AudioTrack::getSessionId() |
| 603 | { |
| 604 | return mSessionId; |
| 605 | } |
| 606 | |
| 607 | status_t AudioTrack::attachAuxEffect(int effectId) |
| 608 | { |
| 609 | return mAudioTrack->attachAuxEffect(effectId); |
| 610 | } |
| 611 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | // ------------------------------------------------------------------------- |
| 613 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 614 | status_t AudioTrack::createTrack( |
| 615 | int streamType, |
| 616 | uint32_t sampleRate, |
| 617 | int format, |
| 618 | int channelCount, |
| 619 | int frameCount, |
| 620 | uint32_t flags, |
| 621 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 622 | audio_io_handle_t output, |
| 623 | bool enforceFrameCount) |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 624 | { |
| 625 | status_t status; |
| 626 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 627 | if (audioFlinger == 0) { |
| 628 | LOGE("Could not get audioflinger"); |
| 629 | return NO_INIT; |
| 630 | } |
| 631 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 632 | int afSampleRate; |
| 633 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 634 | return NO_INIT; |
| 635 | } |
| 636 | int afFrameCount; |
| 637 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 638 | return NO_INIT; |
| 639 | } |
| 640 | uint32_t afLatency; |
| 641 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 642 | return NO_INIT; |
| 643 | } |
| 644 | |
| 645 | mNotificationFramesAct = mNotificationFramesReq; |
| 646 | if (!AudioSystem::isLinearPCM(format)) { |
| 647 | if (sharedBuffer != 0) { |
| 648 | frameCount = sharedBuffer->size(); |
| 649 | } |
| 650 | } else { |
| 651 | // Ensure that buffer depth covers at least audio hardware latency |
| 652 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
| 653 | if (minBufCount < 2) minBufCount = 2; |
| 654 | |
| 655 | int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate; |
| 656 | |
| 657 | if (sharedBuffer == 0) { |
| 658 | if (frameCount == 0) { |
| 659 | frameCount = minFrameCount; |
| 660 | } |
| 661 | if (mNotificationFramesAct == 0) { |
| 662 | mNotificationFramesAct = frameCount/2; |
| 663 | } |
| 664 | // Make sure that application is notified with sufficient margin |
| 665 | // before underrun |
| 666 | if (mNotificationFramesAct > (uint32_t)frameCount/2) { |
| 667 | mNotificationFramesAct = frameCount/2; |
| 668 | } |
| 669 | if (frameCount < minFrameCount) { |
| 670 | if (enforceFrameCount) { |
| 671 | LOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount); |
| 672 | return BAD_VALUE; |
| 673 | } else { |
| 674 | frameCount = minFrameCount; |
| 675 | } |
| 676 | } |
| 677 | } else { |
| 678 | // Ensure that buffer alignment matches channelcount |
| 679 | if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) { |
| 680 | LOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount); |
| 681 | return BAD_VALUE; |
| 682 | } |
| 683 | frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t); |
| 684 | } |
| 685 | } |
| 686 | |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 687 | sp<IAudioTrack> track = audioFlinger->createTrack(getpid(), |
| 688 | streamType, |
| 689 | sampleRate, |
| 690 | format, |
| 691 | channelCount, |
| 692 | frameCount, |
| 693 | ((uint16_t)flags) << 16, |
| 694 | sharedBuffer, |
| 695 | output, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 696 | &mSessionId, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 697 | &status); |
| 698 | |
| 699 | if (track == 0) { |
| 700 | LOGE("AudioFlinger could not create track, status: %d", status); |
| 701 | return status; |
| 702 | } |
| 703 | sp<IMemory> cblk = track->getCblk(); |
| 704 | if (cblk == 0) { |
| 705 | LOGE("Could not get control block"); |
| 706 | return NO_INIT; |
| 707 | } |
| 708 | mAudioTrack.clear(); |
| 709 | mAudioTrack = track; |
| 710 | mCblkMemory.clear(); |
| 711 | mCblkMemory = cblk; |
| 712 | mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer()); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 713 | mCblk->flags |= CBLK_DIRECTION_OUT; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 714 | if (sharedBuffer == 0) { |
| 715 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 716 | } else { |
| 717 | mCblk->buffers = sharedBuffer->pointer(); |
| 718 | // Force buffer full condition as data is already present in shared memory |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 719 | mCblk->stepUser(mCblk->frameCount); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 722 | mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000); |
| 723 | mCblk->sendLevel = uint16_t(mSendLevel * 0x1000); |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 724 | mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 725 | mCblk->waitTimeMs = 0; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 726 | mRemainingFrames = mNotificationFramesAct; |
| 727 | mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 728 | return NO_ERROR; |
| 729 | } |
| 730 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 732 | { |
| 733 | int active; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | status_t result; |
| 735 | audio_track_cblk_t* cblk = mCblk; |
| 736 | uint32_t framesReq = audioBuffer->frameCount; |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 737 | uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | |
| 739 | audioBuffer->frameCount = 0; |
| 740 | audioBuffer->size = 0; |
| 741 | |
| 742 | uint32_t framesAvail = cblk->framesAvailable(); |
| 743 | |
| 744 | if (framesAvail == 0) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 745 | cblk->lock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | goto start_loop_here; |
| 747 | while (framesAvail == 0) { |
| 748 | active = mActive; |
| 749 | if (UNLIKELY(!active)) { |
| 750 | LOGV("Not active and NO_MORE_BUFFERS"); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 751 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | return NO_MORE_BUFFERS; |
| 753 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 754 | if (UNLIKELY(!waitCount)) { |
| 755 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | return WOULD_BLOCK; |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 757 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 758 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
| 759 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
| 760 | } |
| 761 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 762 | LOGW("obtainBuffer() track %p invalidated, creating a new one", this); |
| 763 | // no need to clear the invalid flag as this cblk will not be used anymore |
| 764 | cblk->lock.unlock(); |
| 765 | goto create_new_track; |
| 766 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 767 | if (__builtin_expect(result!=NO_ERROR, false)) { |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 768 | cblk->waitTimeMs += waitTimeMs; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) { |
| 770 | // timing out when a loop has been set and we have already written upto loop end |
| 771 | // is a normal condition: no need to wake AudioFlinger up. |
| 772 | if (cblk->user < cblk->loopEnd) { |
| 773 | LOGW( "obtainBuffer timed out (is the CPU pegged?) %p " |
| 774 | "user=%08x, server=%08x", this, cblk->user, cblk->server); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 775 | //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | cblk->lock.unlock(); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 777 | result = mAudioTrack->start(); |
| 778 | if (result == DEAD_OBJECT) { |
| 779 | LOGW("obtainBuffer() dead IAudioTrack: creating a new one"); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 780 | create_new_track: |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 781 | result = createTrack(mStreamType, cblk->sampleRate, mFormat, mChannelCount, |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 782 | mFrameCount, mFlags, mSharedBuffer, getOutput(), false); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 783 | if (result == NO_ERROR) { |
| 784 | cblk = mCblk; |
| 785 | cblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
Eric Laurent | 6100d2d | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 786 | mAudioTrack->start(); |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 787 | } |
| 788 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | cblk->lock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | } |
| 791 | cblk->waitTimeMs = 0; |
| 792 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 793 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | if (--waitCount == 0) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 795 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | return TIMED_OUT; |
| 797 | } |
| 798 | } |
| 799 | // read the server count again |
| 800 | start_loop_here: |
| 801 | framesAvail = cblk->framesAvailable_l(); |
| 802 | } |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 803 | cblk->lock.unlock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | cblk->waitTimeMs = 0; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 807 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | if (framesReq > framesAvail) { |
| 809 | framesReq = framesAvail; |
| 810 | } |
| 811 | |
| 812 | uint32_t u = cblk->user; |
| 813 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 814 | |
| 815 | if (u + framesReq > bufferEnd) { |
| 816 | framesReq = bufferEnd - u; |
| 817 | } |
| 818 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 819 | audioBuffer->flags = mMuted ? Buffer::MUTE : 0; |
| 820 | audioBuffer->channelCount = mChannelCount; |
| 821 | audioBuffer->frameCount = framesReq; |
| 822 | audioBuffer->size = framesReq * cblk->frameSize; |
| 823 | if (AudioSystem::isLinearPCM(mFormat)) { |
| 824 | audioBuffer->format = AudioSystem::PCM_16_BIT; |
| 825 | } else { |
| 826 | audioBuffer->format = mFormat; |
| 827 | } |
| 828 | audioBuffer->raw = (int8_t *)cblk->buffer(u); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | active = mActive; |
| 830 | return active ? status_t(NO_ERROR) : status_t(STOPPED); |
| 831 | } |
| 832 | |
| 833 | void AudioTrack::releaseBuffer(Buffer* audioBuffer) |
| 834 | { |
| 835 | audio_track_cblk_t* cblk = mCblk; |
| 836 | cblk->stepUser(audioBuffer->frameCount); |
| 837 | } |
| 838 | |
| 839 | // ------------------------------------------------------------------------- |
| 840 | |
| 841 | ssize_t AudioTrack::write(const void* buffer, size_t userSize) |
| 842 | { |
| 843 | |
| 844 | if (mSharedBuffer != 0) return INVALID_OPERATION; |
| 845 | |
| 846 | if (ssize_t(userSize) < 0) { |
| 847 | // sanity-check. user is most-likely passing an error code. |
| 848 | LOGE("AudioTrack::write(buffer=%p, size=%u (%d)", |
| 849 | buffer, userSize, userSize); |
| 850 | return BAD_VALUE; |
| 851 | } |
| 852 | |
| 853 | LOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive); |
| 854 | |
| 855 | ssize_t written = 0; |
| 856 | const int8_t *src = (const int8_t *)buffer; |
| 857 | Buffer audioBuffer; |
| 858 | |
| 859 | do { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 860 | audioBuffer.frameCount = userSize/frameSize(); |
| 861 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | // Calling obtainBuffer() with a negative wait count causes |
| 863 | // an (almost) infinite wait time. |
| 864 | status_t err = obtainBuffer(&audioBuffer, -1); |
| 865 | if (err < 0) { |
| 866 | // out of buffers, return #bytes written |
| 867 | if (err == status_t(NO_MORE_BUFFERS)) |
| 868 | break; |
| 869 | return ssize_t(err); |
| 870 | } |
| 871 | |
| 872 | size_t toWrite; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 873 | |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 874 | if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | // Divide capacity by 2 to take expansion into account |
| 876 | toWrite = audioBuffer.size>>1; |
| 877 | // 8 to 16 bit conversion |
| 878 | int count = toWrite; |
| 879 | int16_t *dst = (int16_t *)(audioBuffer.i8); |
| 880 | while(count--) { |
| 881 | *dst++ = (int16_t)(*src++^0x80) << 8; |
| 882 | } |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 883 | } else { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | toWrite = audioBuffer.size; |
| 885 | memcpy(audioBuffer.i8, src, toWrite); |
| 886 | src += toWrite; |
| 887 | } |
| 888 | userSize -= toWrite; |
| 889 | written += toWrite; |
| 890 | |
| 891 | releaseBuffer(&audioBuffer); |
| 892 | } while (userSize); |
| 893 | |
| 894 | return written; |
| 895 | } |
| 896 | |
| 897 | // ------------------------------------------------------------------------- |
| 898 | |
| 899 | bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread) |
| 900 | { |
| 901 | Buffer audioBuffer; |
| 902 | uint32_t frames; |
| 903 | size_t writtenSize; |
| 904 | |
| 905 | // Manage underrun callback |
| 906 | if (mActive && (mCblk->framesReady() == 0)) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 907 | LOGV("Underrun user: %x, server: %x, flags %04x", mCblk->user, mCblk->server, mCblk->flags); |
| 908 | if ((mCblk->flags & CBLK_UNDERRUN_MSK) == CBLK_UNDERRUN_OFF) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | mCbf(EVENT_UNDERRUN, mUserData, 0); |
| 910 | if (mCblk->server == mCblk->frameCount) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 911 | mCbf(EVENT_BUFFER_END, mUserData, 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | } |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 913 | mCblk->flags |= CBLK_UNDERRUN_ON; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | if (mSharedBuffer != 0) return false; |
| 915 | } |
| 916 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 917 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | // Manage loop end callback |
| 919 | while (mLoopCount > mCblk->loopCount) { |
| 920 | int loopCount = -1; |
| 921 | mLoopCount--; |
| 922 | if (mLoopCount >= 0) loopCount = mLoopCount; |
| 923 | |
| 924 | mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount); |
| 925 | } |
| 926 | |
| 927 | // Manage marker callback |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 928 | if (!mMarkerReached && (mMarkerPosition > 0)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | if (mCblk->server >= mMarkerPosition) { |
| 930 | mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition); |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 931 | mMarkerReached = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | // Manage new position callback |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 936 | if (mUpdatePeriod > 0) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | while (mCblk->server >= mNewPosition) { |
| 938 | mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition); |
| 939 | mNewPosition += mUpdatePeriod; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | // If Shared buffer is used, no data is requested from client. |
| 944 | if (mSharedBuffer != 0) { |
| 945 | frames = 0; |
| 946 | } else { |
| 947 | frames = mRemainingFrames; |
| 948 | } |
| 949 | |
| 950 | do { |
| 951 | |
| 952 | audioBuffer.frameCount = frames; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 953 | |
| 954 | // Calling obtainBuffer() with a wait count of 1 |
| 955 | // limits wait time to WAIT_PERIOD_MS. This prevents from being |
| 956 | // stuck here not being able to handle timed events (position, markers, loops). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | status_t err = obtainBuffer(&audioBuffer, 1); |
| 958 | if (err < NO_ERROR) { |
| 959 | if (err != TIMED_OUT) { |
Eric Laurent | 1dd70b9 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 960 | LOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up."); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | return false; |
| 962 | } |
| 963 | break; |
| 964 | } |
| 965 | if (err == status_t(STOPPED)) return false; |
| 966 | |
| 967 | // Divide buffer size by 2 to take into account the expansion |
| 968 | // due to 8 to 16 bit conversion: the callback must fill only half |
| 969 | // of the destination buffer |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 970 | if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | audioBuffer.size >>= 1; |
| 972 | } |
| 973 | |
| 974 | size_t reqSize = audioBuffer.size; |
| 975 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
| 976 | writtenSize = audioBuffer.size; |
| 977 | |
| 978 | // Sanity check on returned size |
The Android Open Source Project | 8555d08 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 979 | if (ssize_t(writtenSize) <= 0) { |
| 980 | // The callback is done filling buffers |
| 981 | // Keep this thread going to handle timed events and |
| 982 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 983 | // but don't just loop and block the CPU, so wait |
| 984 | usleep(WAIT_PERIOD_MS*1000); |
| 985 | break; |
| 986 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | if (writtenSize > reqSize) writtenSize = reqSize; |
| 988 | |
Eric Laurent | 3302526 | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 989 | if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | // 8 to 16 bit conversion |
| 991 | const int8_t *src = audioBuffer.i8 + writtenSize-1; |
| 992 | int count = writtenSize; |
| 993 | int16_t *dst = audioBuffer.i16 + writtenSize-1; |
| 994 | while(count--) { |
| 995 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 996 | } |
| 997 | writtenSize <<= 1; |
| 998 | } |
| 999 | |
| 1000 | audioBuffer.size = writtenSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1001 | // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for |
| 1002 | // 8 bit PCM data: in this case, mCblk->frameSize is based on a sampel size of |
| 1003 | // 16 bit. |
| 1004 | audioBuffer.frameCount = writtenSize/mCblk->frameSize; |
| 1005 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | frames -= audioBuffer.frameCount; |
| 1007 | |
| 1008 | releaseBuffer(&audioBuffer); |
| 1009 | } |
| 1010 | while (frames); |
| 1011 | |
| 1012 | if (frames == 0) { |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1013 | mRemainingFrames = mNotificationFramesAct; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | } else { |
| 1015 | mRemainingFrames = frames; |
| 1016 | } |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | status_t AudioTrack::dump(int fd, const Vector<String16>& args) const |
| 1021 | { |
| 1022 | |
| 1023 | const size_t SIZE = 256; |
| 1024 | char buffer[SIZE]; |
| 1025 | String8 result; |
| 1026 | |
| 1027 | result.append(" AudioTrack::dump\n"); |
| 1028 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]); |
| 1029 | result.append(buffer); |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1030 | snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | result.append(buffer); |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 1032 | snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | result.append(buffer); |
| 1034 | snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency); |
| 1035 | result.append(buffer); |
| 1036 | ::write(fd, result.string(), result.size()); |
| 1037 | return NO_ERROR; |
| 1038 | } |
| 1039 | |
| 1040 | // ========================================================================= |
| 1041 | |
| 1042 | AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) |
| 1043 | : Thread(bCanCallJava), mReceiver(receiver) |
| 1044 | { |
| 1045 | } |
| 1046 | |
| 1047 | bool AudioTrack::AudioTrackThread::threadLoop() |
| 1048 | { |
| 1049 | return mReceiver.processAudioBuffer(this); |
| 1050 | } |
| 1051 | |
| 1052 | status_t AudioTrack::AudioTrackThread::readyToRun() |
| 1053 | { |
| 1054 | return NO_ERROR; |
| 1055 | } |
| 1056 | |
| 1057 | void AudioTrack::AudioTrackThread::onFirstRef() |
| 1058 | { |
| 1059 | } |
| 1060 | |
| 1061 | // ========================================================================= |
| 1062 | |
| 1063 | audio_track_cblk_t::audio_track_cblk_t() |
Mathias Agopian | 54b1a05 | 2010-03-19 16:14:13 -0700 | [diff] [blame] | 1064 | : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0), |
| 1065 | userBase(0), serverBase(0), buffers(0), frameCount(0), |
| 1066 | loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0), |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1067 | flags(0), sendLevel(0) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | { |
| 1069 | } |
| 1070 | |
| 1071 | uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount) |
| 1072 | { |
| 1073 | uint32_t u = this->user; |
| 1074 | |
| 1075 | u += frameCount; |
| 1076 | // Ensure that user is never ahead of server for AudioRecord |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1077 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1078 | // If stepServer() has been called once, switch to normal obtainBuffer() timeout period |
| 1079 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) { |
| 1080 | bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 1081 | } |
| 1082 | } else if (u > this->server) { |
| 1083 | LOGW("stepServer occured after track reset"); |
| 1084 | u = this->server; |
| 1085 | } |
| 1086 | |
| 1087 | if (u >= userBase + this->frameCount) { |
| 1088 | userBase += this->frameCount; |
| 1089 | } |
| 1090 | |
| 1091 | this->user = u; |
| 1092 | |
| 1093 | // Clear flow control error condition as new data has been written/read to/from buffer. |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1094 | flags &= ~CBLK_UNDERRUN_MSK; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | |
| 1096 | return u; |
| 1097 | } |
| 1098 | |
| 1099 | bool audio_track_cblk_t::stepServer(uint32_t frameCount) |
| 1100 | { |
| 1101 | // the code below simulates lock-with-timeout |
| 1102 | // we MUST do this to protect the AudioFlinger server |
| 1103 | // as this lock is shared with the client. |
| 1104 | status_t err; |
| 1105 | |
| 1106 | err = lock.tryLock(); |
| 1107 | if (err == -EBUSY) { // just wait a bit |
| 1108 | usleep(1000); |
| 1109 | err = lock.tryLock(); |
| 1110 | } |
| 1111 | if (err != NO_ERROR) { |
| 1112 | // probably, the client just died. |
| 1113 | return false; |
| 1114 | } |
| 1115 | |
| 1116 | uint32_t s = this->server; |
| 1117 | |
| 1118 | s += frameCount; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1119 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | // Mark that we have read the first buffer so that next time stepUser() is called |
| 1121 | // we switch to normal obtainBuffer() timeout period |
| 1122 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) { |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1123 | bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1124 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | // It is possible that we receive a flush() |
| 1126 | // while the mixer is processing a block: in this case, |
| 1127 | // stepServer() is called After the flush() has reset u & s and |
| 1128 | // we have s > u |
| 1129 | if (s > this->user) { |
| 1130 | LOGW("stepServer occured after track reset"); |
| 1131 | s = this->user; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | if (s >= loopEnd) { |
| 1136 | LOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd); |
| 1137 | s = loopStart; |
| 1138 | if (--loopCount == 0) { |
| 1139 | loopEnd = UINT_MAX; |
| 1140 | loopStart = UINT_MAX; |
| 1141 | } |
| 1142 | } |
| 1143 | if (s >= serverBase + this->frameCount) { |
| 1144 | serverBase += this->frameCount; |
| 1145 | } |
| 1146 | |
| 1147 | this->server = s; |
| 1148 | |
| 1149 | cv.signal(); |
| 1150 | lock.unlock(); |
| 1151 | return true; |
| 1152 | } |
| 1153 | |
| 1154 | void* audio_track_cblk_t::buffer(uint32_t offset) const |
| 1155 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1156 | return (int8_t *)this->buffers + (offset - userBase) * this->frameSize; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | uint32_t audio_track_cblk_t::framesAvailable() |
| 1160 | { |
| 1161 | Mutex::Autolock _l(lock); |
| 1162 | return framesAvailable_l(); |
| 1163 | } |
| 1164 | |
| 1165 | uint32_t audio_track_cblk_t::framesAvailable_l() |
| 1166 | { |
| 1167 | uint32_t u = this->user; |
| 1168 | uint32_t s = this->server; |
| 1169 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1170 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | uint32_t limit = (s < loopStart) ? s : loopStart; |
| 1172 | return limit + frameCount - u; |
| 1173 | } else { |
| 1174 | return frameCount + u - s; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | uint32_t audio_track_cblk_t::framesReady() |
| 1179 | { |
| 1180 | uint32_t u = this->user; |
| 1181 | uint32_t s = this->server; |
| 1182 | |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1183 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1184 | if (u < loopEnd) { |
| 1185 | return u - s; |
| 1186 | } else { |
| 1187 | Mutex::Autolock _l(lock); |
| 1188 | if (loopCount >= 0) { |
| 1189 | return (loopEnd - loopStart)*loopCount + u - s; |
| 1190 | } else { |
| 1191 | return UINT_MAX; |
| 1192 | } |
| 1193 | } |
| 1194 | } else { |
| 1195 | return s - u; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | // ------------------------------------------------------------------------- |
| 1200 | |
| 1201 | }; // namespace android |
| 1202 | |