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