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> |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 38 | #include <utils/Atomic.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 40 | #include <cutils/bitops.h> |
| 41 | |
Dima Zavin | 34bb419 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 42 | #include <system/audio.h> |
Dima Zavin | 290029d | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 43 | #include <system/audio_policy.h> |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 46 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 47 | |
| 48 | namespace android { |
Chia-chi Yeh | bd240c2 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 49 | // --------------------------------------------------------------------------- |
| 50 | |
| 51 | // static |
| 52 | status_t AudioTrack::getMinFrameCount( |
| 53 | int* frameCount, |
| 54 | int streamType, |
| 55 | uint32_t sampleRate) |
| 56 | { |
| 57 | int afSampleRate; |
| 58 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 59 | return NO_INIT; |
| 60 | } |
| 61 | int afFrameCount; |
| 62 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 63 | return NO_INIT; |
| 64 | } |
| 65 | uint32_t afLatency; |
| 66 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 67 | return NO_INIT; |
| 68 | } |
| 69 | |
| 70 | // Ensure that buffer depth covers at least audio hardware latency |
| 71 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate); |
| 72 | if (minBufCount < 2) minBufCount = 2; |
| 73 | |
| 74 | *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : |
| 75 | afFrameCount * minBufCount * sampleRate / afSampleRate; |
| 76 | return NO_ERROR; |
| 77 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
| 79 | // --------------------------------------------------------------------------- |
| 80 | |
| 81 | AudioTrack::AudioTrack() |
| 82 | : mStatus(NO_INIT) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | AudioTrack::AudioTrack( |
| 87 | int streamType, |
| 88 | uint32_t sampleRate, |
| 89 | int format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 90 | int channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | int frameCount, |
| 92 | uint32_t flags, |
| 93 | callback_t cbf, |
| 94 | void* user, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 95 | int notificationFrames, |
| 96 | int sessionId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | : mStatus(NO_INIT) |
| 98 | { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 99 | mStatus = set(streamType, sampleRate, format, channelMask, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 100 | frameCount, flags, cbf, user, notificationFrames, |
| 101 | 0, false, sessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | AudioTrack::AudioTrack( |
| 105 | int streamType, |
| 106 | uint32_t sampleRate, |
| 107 | int format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 108 | int channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | const sp<IMemory>& sharedBuffer, |
| 110 | uint32_t flags, |
| 111 | callback_t cbf, |
| 112 | void* user, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 113 | int notificationFrames, |
| 114 | int sessionId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | : mStatus(NO_INIT) |
| 116 | { |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 117 | mStatus = set(streamType, sampleRate, format, channelMask, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 118 | 0, flags, cbf, user, notificationFrames, |
| 119 | sharedBuffer, false, sessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | AudioTrack::~AudioTrack() |
| 123 | { |
| 124 | LOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer()); |
| 125 | |
| 126 | if (mStatus == NO_ERROR) { |
| 127 | // Make sure that callback function exits in the case where |
| 128 | // it is looping on buffer full condition in obtainBuffer(). |
| 129 | // Otherwise the callback thread will never exit. |
| 130 | stop(); |
| 131 | if (mAudioTrackThread != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | mAudioTrackThread->requestExitAndWait(); |
| 133 | mAudioTrackThread.clear(); |
| 134 | } |
| 135 | mAudioTrack.clear(); |
| 136 | IPCThreadState::self()->flushCommands(); |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 137 | AudioSystem::releaseAudioSessionId(mSessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | status_t AudioTrack::set( |
| 142 | int streamType, |
| 143 | uint32_t sampleRate, |
| 144 | int format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 145 | int channelMask, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | int frameCount, |
| 147 | uint32_t flags, |
| 148 | callback_t cbf, |
| 149 | void* user, |
| 150 | int notificationFrames, |
| 151 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 152 | bool threadCanCallJava, |
| 153 | int sessionId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | { |
| 155 | |
| 156 | LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
| 157 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 158 | AutoMutex lock(mLock); |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 159 | if (mAudioTrack != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | LOGE("Track already in use"); |
| 161 | return INVALID_OPERATION; |
| 162 | } |
| 163 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | int afSampleRate; |
| 165 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 166 | return NO_INIT; |
| 167 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | uint32_t afLatency; |
| 169 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 170 | return NO_INIT; |
| 171 | } |
| 172 | |
| 173 | // handle default values first. |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 174 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 175 | streamType = AUDIO_STREAM_MUSIC; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | } |
| 177 | if (sampleRate == 0) { |
| 178 | sampleRate = afSampleRate; |
| 179 | } |
| 180 | // these below should probably come from the audioFlinger too... |
| 181 | if (format == 0) { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 182 | format = AUDIO_FORMAT_PCM_16_BIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 184 | if (channelMask == 0) { |
| 185 | channelMask = AUDIO_CHANNEL_OUT_STEREO; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // validate parameters |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 189 | if (!audio_is_valid_format(format)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | LOGE("Invalid format"); |
| 191 | return BAD_VALUE; |
| 192 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 193 | |
| 194 | // force direct flag if format is not linear PCM |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 195 | if (!audio_is_linear_pcm(format)) { |
| 196 | flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 199 | if (!audio_is_output_channel(channelMask)) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 200 | LOGE("Invalid channel mask"); |
| 201 | return BAD_VALUE; |
| 202 | } |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 203 | uint32_t channelCount = popcount(channelMask); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 204 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 205 | audio_io_handle_t output = AudioSystem::getOutput( |
| 206 | (audio_stream_type_t)streamType, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 207 | sampleRate,format, channelMask, |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 208 | (audio_policy_output_flags_t)flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 209 | |
| 210 | if (output == 0) { |
| 211 | 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] | 212 | return BAD_VALUE; |
| 213 | } |
| 214 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 215 | mVolume[LEFT] = 1.0f; |
| 216 | mVolume[RIGHT] = 1.0f; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 217 | mSendLevel = 0; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 218 | mFrameCount = frameCount; |
| 219 | mNotificationFramesReq = notificationFrames; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 220 | mSessionId = sessionId; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 221 | mAuxEffectId = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 222 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 223 | // create the IAudioTrack |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 224 | status_t status = createTrack_l(streamType, |
| 225 | sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 226 | (uint32_t)format, |
| 227 | (uint32_t)channelMask, |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 228 | frameCount, |
| 229 | flags, |
| 230 | sharedBuffer, |
| 231 | output, |
| 232 | true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 234 | if (status != NO_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | return status; |
| 236 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | if (cbf != 0) { |
| 239 | mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); |
| 240 | if (mAudioTrackThread == 0) { |
| 241 | LOGE("Could not create callback thread"); |
| 242 | return NO_INIT; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | mStatus = NO_ERROR; |
| 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | mStreamType = streamType; |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 249 | mFormat = (uint32_t)format; |
| 250 | mChannelMask = (uint32_t)channelMask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | mChannelCount = channelCount; |
| 252 | mSharedBuffer = sharedBuffer; |
| 253 | mMuted = false; |
| 254 | mActive = 0; |
| 255 | mCbf = cbf; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | mUserData = user; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | mLoopCount = 0; |
| 258 | mMarkerPosition = 0; |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 259 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | mNewPosition = 0; |
| 261 | mUpdatePeriod = 0; |
Jean-Michel Trivi | 22cb204 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 262 | mFlushed = false; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 263 | mFlags = flags; |
Marco Nelissen | c74b93f | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 264 | AudioSystem::acquireAudioSessionId(mSessionId); |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 265 | mRestoreStatus = NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | return NO_ERROR; |
| 267 | } |
| 268 | |
| 269 | status_t AudioTrack::initCheck() const |
| 270 | { |
| 271 | return mStatus; |
| 272 | } |
| 273 | |
| 274 | // ------------------------------------------------------------------------- |
| 275 | |
| 276 | uint32_t AudioTrack::latency() const |
| 277 | { |
| 278 | return mLatency; |
| 279 | } |
| 280 | |
| 281 | int AudioTrack::streamType() const |
| 282 | { |
| 283 | return mStreamType; |
| 284 | } |
| 285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | int AudioTrack::format() const |
| 287 | { |
| 288 | return mFormat; |
| 289 | } |
| 290 | |
| 291 | int AudioTrack::channelCount() const |
| 292 | { |
| 293 | return mChannelCount; |
| 294 | } |
| 295 | |
| 296 | uint32_t AudioTrack::frameCount() const |
| 297 | { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 298 | return mCblk->frameCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | int AudioTrack::frameSize() const |
| 302 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 303 | if (audio_is_linear_pcm(mFormat)) { |
Eric Laurent | c310dcb | 2011-06-16 21:30:45 -0700 | [diff] [blame] | 304 | return channelCount()*audio_bytes_per_sample(mFormat); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 305 | } else { |
| 306 | return sizeof(uint8_t); |
| 307 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | sp<IMemory>& AudioTrack::sharedBuffer() |
| 311 | { |
| 312 | return mSharedBuffer; |
| 313 | } |
| 314 | |
| 315 | // ------------------------------------------------------------------------- |
| 316 | |
| 317 | void AudioTrack::start() |
| 318 | { |
| 319 | sp<AudioTrackThread> t = mAudioTrackThread; |
Glenn Kasten | 028ab99 | 2011-06-22 16:18:04 -0700 | [diff] [blame] | 320 | status_t status = NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | |
| 322 | LOGV("start %p", this); |
| 323 | if (t != 0) { |
| 324 | if (t->exitPending()) { |
| 325 | if (t->requestExitAndWait() == WOULD_BLOCK) { |
| 326 | LOGE("AudioTrack::start called from thread"); |
| 327 | return; |
| 328 | } |
| 329 | } |
| 330 | t->mLock.lock(); |
| 331 | } |
| 332 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 333 | AutoMutex lock(mLock); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 334 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 335 | // while we are accessing the cblk |
| 336 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 337 | sp <IMemory> iMem = mCblkMemory; |
| 338 | audio_track_cblk_t* cblk = mCblk; |
| 339 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 340 | if (mActive == 0) { |
Jean-Michel Trivi | 22cb204 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 341 | mFlushed = false; |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 342 | mActive = 1; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 343 | mNewPosition = cblk->server + mUpdatePeriod; |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 344 | cblk->lock.lock(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 345 | cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 346 | cblk->waitTimeMs = 0; |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 347 | android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 348 | if (t != 0) { |
Glenn Kasten | 993fcce | 2011-06-01 16:46:29 -0700 | [diff] [blame] | 349 | t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 350 | } else { |
Glenn Kasten | 993fcce | 2011-06-01 16:46:29 -0700 | [diff] [blame] | 351 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 354 | LOGV("start %p before lock cblk %p", this, mCblk); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 355 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
| 356 | cblk->lock.unlock(); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 357 | status = mAudioTrack->start(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 358 | cblk->lock.lock(); |
| 359 | if (status == DEAD_OBJECT) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 360 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 361 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 362 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 363 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 364 | status = restoreTrack_l(cblk, true); |
| 365 | } |
| 366 | cblk->lock.unlock(); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 367 | if (status != NO_ERROR) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 368 | LOGV("start() failed"); |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 369 | mActive = 0; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 370 | if (t != 0) { |
| 371 | t->requestExit(); |
| 372 | } else { |
| 373 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL); |
| 374 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 375 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | if (t != 0) { |
| 379 | t->mLock.unlock(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void AudioTrack::stop() |
| 384 | { |
| 385 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 386 | |
| 387 | LOGV("stop %p", this); |
| 388 | if (t != 0) { |
| 389 | t->mLock.lock(); |
| 390 | } |
| 391 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 392 | AutoMutex lock(mLock); |
| 393 | if (mActive == 1) { |
| 394 | mActive = 0; |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 395 | mCblk->cv.signal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | mAudioTrack->stop(); |
| 397 | // Cancel loops (If we are in the middle of a loop, playback |
| 398 | // would not stop until loopCount reaches 0). |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 399 | setLoop_l(0, 0, 0); |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 400 | // the playback head position will reset to 0, so if a marker is set, we need |
| 401 | // to activate it again |
| 402 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | // Force flush if a shared buffer is used otherwise audioflinger |
| 404 | // will not stop before end of buffer is reached. |
| 405 | if (mSharedBuffer != 0) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 406 | flush_l(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | } |
| 408 | if (t != 0) { |
| 409 | t->requestExit(); |
| 410 | } else { |
| 411 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (t != 0) { |
| 416 | t->mLock.unlock(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | bool AudioTrack::stopped() const |
| 421 | { |
| 422 | return !mActive; |
| 423 | } |
| 424 | |
| 425 | void AudioTrack::flush() |
| 426 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 427 | AutoMutex lock(mLock); |
| 428 | flush_l(); |
| 429 | } |
| 430 | |
| 431 | // must be called with mLock held |
| 432 | void AudioTrack::flush_l() |
| 433 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | LOGV("flush"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 435 | |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 436 | // clear playback marker and periodic update counter |
| 437 | mMarkerPosition = 0; |
| 438 | mMarkerReached = false; |
| 439 | mUpdatePeriod = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 440 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | if (!mActive) { |
Jean-Michel Trivi | 22cb204 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 442 | mFlushed = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | mAudioTrack->flush(); |
| 444 | // Release AudioTrack callback thread in case it was waiting for new buffers |
| 445 | // in AudioTrack::obtainBuffer() |
| 446 | mCblk->cv.signal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | void AudioTrack::pause() |
| 451 | { |
| 452 | LOGV("pause"); |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 453 | AutoMutex lock(mLock); |
| 454 | if (mActive == 1) { |
| 455 | mActive = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | mAudioTrack->pause(); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void AudioTrack::mute(bool e) |
| 461 | { |
| 462 | mAudioTrack->mute(e); |
| 463 | mMuted = e; |
| 464 | } |
| 465 | |
| 466 | bool AudioTrack::muted() const |
| 467 | { |
| 468 | return mMuted; |
| 469 | } |
| 470 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 471 | status_t AudioTrack::setVolume(float left, float right) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 473 | if (left > 1.0f || right > 1.0f) { |
| 474 | return BAD_VALUE; |
| 475 | } |
| 476 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 477 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | mVolume[LEFT] = left; |
| 479 | mVolume[RIGHT] = right; |
| 480 | |
| 481 | // write must be atomic |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 482 | mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000); |
| 483 | |
| 484 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void AudioTrack::getVolume(float* left, float* right) |
| 488 | { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 489 | if (left != NULL) { |
| 490 | *left = mVolume[LEFT]; |
| 491 | } |
| 492 | if (right != NULL) { |
| 493 | *right = mVolume[RIGHT]; |
| 494 | } |
| 495 | } |
| 496 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 497 | status_t AudioTrack::setAuxEffectSendLevel(float level) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 498 | { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 499 | LOGV("setAuxEffectSendLevel(%f)", level); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 500 | if (level > 1.0f) { |
| 501 | return BAD_VALUE; |
| 502 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 503 | AutoMutex lock(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 504 | |
| 505 | mSendLevel = level; |
| 506 | |
| 507 | mCblk->sendLevel = uint16_t(level * 0x1000); |
| 508 | |
| 509 | return NO_ERROR; |
| 510 | } |
| 511 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 512 | void AudioTrack::getAuxEffectSendLevel(float* level) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 513 | { |
| 514 | if (level != NULL) { |
| 515 | *level = mSendLevel; |
| 516 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 519 | status_t AudioTrack::setSampleRate(int rate) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | { |
| 521 | int afSamplingRate; |
| 522 | |
| 523 | if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) { |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 524 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | } |
| 526 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 527 | if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 529 | AutoMutex lock(mLock); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 530 | mCblk->sampleRate = rate; |
| 531 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | uint32_t AudioTrack::getSampleRate() |
| 535 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 536 | AutoMutex lock(mLock); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 537 | return mCblk->sampleRate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 541 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 542 | AutoMutex lock(mLock); |
| 543 | return setLoop_l(loopStart, loopEnd, loopCount); |
| 544 | } |
| 545 | |
| 546 | // must be called with mLock held |
| 547 | status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 548 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | audio_track_cblk_t* cblk = mCblk; |
| 550 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | Mutex::Autolock _l(cblk->lock); |
| 552 | |
| 553 | if (loopCount == 0) { |
| 554 | cblk->loopStart = UINT_MAX; |
| 555 | cblk->loopEnd = UINT_MAX; |
| 556 | cblk->loopCount = 0; |
| 557 | mLoopCount = 0; |
| 558 | return NO_ERROR; |
| 559 | } |
| 560 | |
| 561 | if (loopStart >= loopEnd || |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 562 | loopEnd - loopStart > cblk->frameCount || |
| 563 | cblk->server > loopStart) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 564 | 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] | 565 | return BAD_VALUE; |
| 566 | } |
| 567 | |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 568 | if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | 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] | 570 | loopStart, loopEnd, cblk->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | return BAD_VALUE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 572 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | |
| 574 | cblk->loopStart = loopStart; |
| 575 | cblk->loopEnd = loopEnd; |
| 576 | cblk->loopCount = loopCount; |
| 577 | mLoopCount = loopCount; |
| 578 | |
| 579 | return NO_ERROR; |
| 580 | } |
| 581 | |
| 582 | status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount) |
| 583 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 584 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | if (loopStart != 0) { |
| 586 | *loopStart = mCblk->loopStart; |
| 587 | } |
| 588 | if (loopEnd != 0) { |
| 589 | *loopEnd = mCblk->loopEnd; |
| 590 | } |
| 591 | if (loopCount != 0) { |
| 592 | if (mCblk->loopCount < 0) { |
| 593 | *loopCount = -1; |
| 594 | } else { |
| 595 | *loopCount = mCblk->loopCount; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | return NO_ERROR; |
| 600 | } |
| 601 | |
| 602 | status_t AudioTrack::setMarkerPosition(uint32_t marker) |
| 603 | { |
| 604 | if (mCbf == 0) return INVALID_OPERATION; |
| 605 | |
| 606 | mMarkerPosition = marker; |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 607 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | |
| 609 | return NO_ERROR; |
| 610 | } |
| 611 | |
| 612 | status_t AudioTrack::getMarkerPosition(uint32_t *marker) |
| 613 | { |
| 614 | if (marker == 0) return BAD_VALUE; |
| 615 | |
| 616 | *marker = mMarkerPosition; |
| 617 | |
| 618 | return NO_ERROR; |
| 619 | } |
| 620 | |
| 621 | status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 622 | { |
| 623 | if (mCbf == 0) return INVALID_OPERATION; |
| 624 | |
| 625 | uint32_t curPosition; |
| 626 | getPosition(&curPosition); |
| 627 | mNewPosition = curPosition + updatePeriod; |
| 628 | mUpdatePeriod = updatePeriod; |
| 629 | |
| 630 | return NO_ERROR; |
| 631 | } |
| 632 | |
| 633 | status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) |
| 634 | { |
| 635 | if (updatePeriod == 0) return BAD_VALUE; |
| 636 | |
| 637 | *updatePeriod = mUpdatePeriod; |
| 638 | |
| 639 | return NO_ERROR; |
| 640 | } |
| 641 | |
| 642 | status_t AudioTrack::setPosition(uint32_t position) |
| 643 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 644 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | Mutex::Autolock _l(mCblk->lock); |
| 646 | |
| 647 | if (!stopped()) return INVALID_OPERATION; |
| 648 | |
| 649 | if (position > mCblk->user) return BAD_VALUE; |
| 650 | |
| 651 | mCblk->server = position; |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 652 | android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | return NO_ERROR; |
| 655 | } |
| 656 | |
| 657 | status_t AudioTrack::getPosition(uint32_t *position) |
| 658 | { |
| 659 | if (position == 0) return BAD_VALUE; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 660 | AutoMutex lock(mLock); |
Jean-Michel Trivi | 22cb204 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 661 | *position = mFlushed ? 0 : mCblk->server; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | |
| 663 | return NO_ERROR; |
| 664 | } |
| 665 | |
| 666 | status_t AudioTrack::reload() |
| 667 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 668 | AutoMutex lock(mLock); |
| 669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | if (!stopped()) return INVALID_OPERATION; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 671 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 672 | flush_l(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 674 | mCblk->stepUser(mCblk->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | |
| 676 | return NO_ERROR; |
| 677 | } |
| 678 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 679 | audio_io_handle_t AudioTrack::getOutput() |
| 680 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 681 | AutoMutex lock(mLock); |
| 682 | return getOutput_l(); |
| 683 | } |
| 684 | |
| 685 | // must be called with mLock held |
| 686 | audio_io_handle_t AudioTrack::getOutput_l() |
| 687 | { |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 688 | return AudioSystem::getOutput((audio_stream_type_t)mStreamType, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 689 | mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 692 | int AudioTrack::getSessionId() |
| 693 | { |
| 694 | return mSessionId; |
| 695 | } |
| 696 | |
| 697 | status_t AudioTrack::attachAuxEffect(int effectId) |
| 698 | { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 699 | LOGV("attachAuxEffect(%d)", effectId); |
| 700 | status_t status = mAudioTrack->attachAuxEffect(effectId); |
| 701 | if (status == NO_ERROR) { |
| 702 | mAuxEffectId = effectId; |
| 703 | } |
| 704 | return status; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 705 | } |
| 706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | // ------------------------------------------------------------------------- |
| 708 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 709 | // must be called with mLock held |
| 710 | status_t AudioTrack::createTrack_l( |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 711 | int streamType, |
| 712 | uint32_t sampleRate, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 713 | uint32_t format, |
| 714 | uint32_t channelMask, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 715 | int frameCount, |
| 716 | uint32_t flags, |
| 717 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 718 | audio_io_handle_t output, |
| 719 | bool enforceFrameCount) |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 720 | { |
| 721 | status_t status; |
| 722 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 723 | if (audioFlinger == 0) { |
| 724 | LOGE("Could not get audioflinger"); |
| 725 | return NO_INIT; |
| 726 | } |
| 727 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 728 | int afSampleRate; |
| 729 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 730 | return NO_INIT; |
| 731 | } |
| 732 | int afFrameCount; |
| 733 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 734 | return NO_INIT; |
| 735 | } |
| 736 | uint32_t afLatency; |
| 737 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 738 | return NO_INIT; |
| 739 | } |
| 740 | |
| 741 | mNotificationFramesAct = mNotificationFramesReq; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 742 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 743 | if (sharedBuffer != 0) { |
| 744 | frameCount = sharedBuffer->size(); |
| 745 | } |
| 746 | } else { |
| 747 | // Ensure that buffer depth covers at least audio hardware latency |
| 748 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
| 749 | if (minBufCount < 2) minBufCount = 2; |
| 750 | |
| 751 | int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate; |
| 752 | |
| 753 | if (sharedBuffer == 0) { |
| 754 | if (frameCount == 0) { |
| 755 | frameCount = minFrameCount; |
| 756 | } |
| 757 | if (mNotificationFramesAct == 0) { |
| 758 | mNotificationFramesAct = frameCount/2; |
| 759 | } |
| 760 | // Make sure that application is notified with sufficient margin |
| 761 | // before underrun |
| 762 | if (mNotificationFramesAct > (uint32_t)frameCount/2) { |
| 763 | mNotificationFramesAct = frameCount/2; |
| 764 | } |
| 765 | if (frameCount < minFrameCount) { |
| 766 | if (enforceFrameCount) { |
| 767 | LOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount); |
| 768 | return BAD_VALUE; |
| 769 | } else { |
| 770 | frameCount = minFrameCount; |
| 771 | } |
| 772 | } |
| 773 | } else { |
| 774 | // Ensure that buffer alignment matches channelcount |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 775 | int channelCount = popcount(channelMask); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 776 | if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) { |
| 777 | LOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount); |
| 778 | return BAD_VALUE; |
| 779 | } |
| 780 | frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t); |
| 781 | } |
| 782 | } |
| 783 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 784 | sp<IAudioTrack> track = audioFlinger->createTrack(getpid(), |
| 785 | streamType, |
| 786 | sampleRate, |
| 787 | format, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 788 | channelMask, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 789 | frameCount, |
| 790 | ((uint16_t)flags) << 16, |
| 791 | sharedBuffer, |
| 792 | output, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 793 | &mSessionId, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 794 | &status); |
| 795 | |
| 796 | if (track == 0) { |
| 797 | LOGE("AudioFlinger could not create track, status: %d", status); |
| 798 | return status; |
| 799 | } |
| 800 | sp<IMemory> cblk = track->getCblk(); |
| 801 | if (cblk == 0) { |
| 802 | LOGE("Could not get control block"); |
| 803 | return NO_INIT; |
| 804 | } |
| 805 | mAudioTrack.clear(); |
| 806 | mAudioTrack = track; |
| 807 | mCblkMemory.clear(); |
| 808 | mCblkMemory = cblk; |
| 809 | mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer()); |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 810 | android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 811 | if (sharedBuffer == 0) { |
| 812 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 813 | } else { |
| 814 | mCblk->buffers = sharedBuffer->pointer(); |
| 815 | // Force buffer full condition as data is already present in shared memory |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 816 | mCblk->stepUser(mCblk->frameCount); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 819 | mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000); |
| 820 | mCblk->sendLevel = uint16_t(mSendLevel * 0x1000); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 821 | mAudioTrack->attachAuxEffect(mAuxEffectId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 822 | mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 823 | mCblk->waitTimeMs = 0; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 824 | mRemainingFrames = mNotificationFramesAct; |
| 825 | mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 826 | return NO_ERROR; |
| 827 | } |
| 828 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 830 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 831 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | int active; |
Glenn Kasten | 028ab99 | 2011-06-22 16:18:04 -0700 | [diff] [blame] | 833 | status_t result = NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | audio_track_cblk_t* cblk = mCblk; |
| 835 | uint32_t framesReq = audioBuffer->frameCount; |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 836 | 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] | 837 | |
| 838 | audioBuffer->frameCount = 0; |
| 839 | audioBuffer->size = 0; |
| 840 | |
| 841 | uint32_t framesAvail = cblk->framesAvailable(); |
| 842 | |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 843 | cblk->lock.lock(); |
| 844 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 845 | goto create_new_track; |
| 846 | } |
| 847 | cblk->lock.unlock(); |
| 848 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | if (framesAvail == 0) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 850 | cblk->lock.lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | goto start_loop_here; |
| 852 | while (framesAvail == 0) { |
| 853 | active = mActive; |
| 854 | if (UNLIKELY(!active)) { |
| 855 | LOGV("Not active and NO_MORE_BUFFERS"); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 856 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | return NO_MORE_BUFFERS; |
| 858 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 859 | if (UNLIKELY(!waitCount)) { |
| 860 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | return WOULD_BLOCK; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 862 | } |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 863 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 864 | mLock.unlock(); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 865 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 866 | cblk->lock.unlock(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 867 | mLock.lock(); |
| 868 | if (mActive == 0) { |
| 869 | return status_t(STOPPED); |
| 870 | } |
| 871 | cblk->lock.lock(); |
| 872 | } |
| 873 | |
| 874 | if (cblk->flags & CBLK_INVALID_MSK) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 875 | goto create_new_track; |
| 876 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 877 | if (__builtin_expect(result!=NO_ERROR, false)) { |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 878 | cblk->waitTimeMs += waitTimeMs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) { |
| 880 | // timing out when a loop has been set and we have already written upto loop end |
| 881 | // is a normal condition: no need to wake AudioFlinger up. |
| 882 | if (cblk->user < cblk->loopEnd) { |
| 883 | LOGW( "obtainBuffer timed out (is the CPU pegged?) %p " |
| 884 | "user=%08x, server=%08x", this, cblk->user, cblk->server); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 885 | //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] | 886 | cblk->lock.unlock(); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 887 | result = mAudioTrack->start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | cblk->lock.lock(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 889 | if (result == DEAD_OBJECT) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 890 | android_atomic_or(CBLK_INVALID_ON, &cblk->flags); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 891 | create_new_track: |
| 892 | result = restoreTrack_l(cblk, false); |
| 893 | } |
| 894 | if (result != NO_ERROR) { |
| 895 | LOGW("obtainBuffer create Track error %d", result); |
| 896 | cblk->lock.unlock(); |
| 897 | return result; |
| 898 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | } |
| 900 | cblk->waitTimeMs = 0; |
| 901 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 902 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | if (--waitCount == 0) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 904 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | return TIMED_OUT; |
| 906 | } |
| 907 | } |
| 908 | // read the server count again |
| 909 | start_loop_here: |
| 910 | framesAvail = cblk->framesAvailable_l(); |
| 911 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 912 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 915 | // restart track if it was disabled by audioflinger due to previous underrun |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 916 | if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 917 | android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags); |
| 918 | LOGW("obtainBuffer() track %p disabled, restarting", this); |
| 919 | mAudioTrack->start(); |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 920 | } |
| 921 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | cblk->waitTimeMs = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 923 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | if (framesReq > framesAvail) { |
| 925 | framesReq = framesAvail; |
| 926 | } |
| 927 | |
| 928 | uint32_t u = cblk->user; |
| 929 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 930 | |
| 931 | if (u + framesReq > bufferEnd) { |
| 932 | framesReq = bufferEnd - u; |
| 933 | } |
| 934 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 935 | audioBuffer->flags = mMuted ? Buffer::MUTE : 0; |
| 936 | audioBuffer->channelCount = mChannelCount; |
| 937 | audioBuffer->frameCount = framesReq; |
| 938 | audioBuffer->size = framesReq * cblk->frameSize; |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 939 | if (audio_is_linear_pcm(mFormat)) { |
| 940 | audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 941 | } else { |
| 942 | audioBuffer->format = mFormat; |
| 943 | } |
| 944 | audioBuffer->raw = (int8_t *)cblk->buffer(u); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | active = mActive; |
| 946 | return active ? status_t(NO_ERROR) : status_t(STOPPED); |
| 947 | } |
| 948 | |
| 949 | void AudioTrack::releaseBuffer(Buffer* audioBuffer) |
| 950 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 951 | AutoMutex lock(mLock); |
| 952 | mCblk->stepUser(audioBuffer->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | // ------------------------------------------------------------------------- |
| 956 | |
| 957 | ssize_t AudioTrack::write(const void* buffer, size_t userSize) |
| 958 | { |
| 959 | |
| 960 | if (mSharedBuffer != 0) return INVALID_OPERATION; |
| 961 | |
| 962 | if (ssize_t(userSize) < 0) { |
| 963 | // sanity-check. user is most-likely passing an error code. |
| 964 | LOGE("AudioTrack::write(buffer=%p, size=%u (%d)", |
| 965 | buffer, userSize, userSize); |
| 966 | return BAD_VALUE; |
| 967 | } |
| 968 | |
| 969 | LOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive); |
| 970 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 971 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 972 | // while we are accessing the cblk |
| 973 | mLock.lock(); |
| 974 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 975 | sp <IMemory> iMem = mCblkMemory; |
| 976 | mLock.unlock(); |
| 977 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | ssize_t written = 0; |
| 979 | const int8_t *src = (const int8_t *)buffer; |
| 980 | Buffer audioBuffer; |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 981 | size_t frameSz = (size_t)frameSize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | |
| 983 | do { |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 984 | audioBuffer.frameCount = userSize/frameSz; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 985 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | // Calling obtainBuffer() with a negative wait count causes |
| 987 | // an (almost) infinite wait time. |
| 988 | status_t err = obtainBuffer(&audioBuffer, -1); |
| 989 | if (err < 0) { |
| 990 | // out of buffers, return #bytes written |
| 991 | if (err == status_t(NO_MORE_BUFFERS)) |
| 992 | break; |
| 993 | return ssize_t(err); |
| 994 | } |
| 995 | |
| 996 | size_t toWrite; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 997 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 998 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | // Divide capacity by 2 to take expansion into account |
| 1000 | toWrite = audioBuffer.size>>1; |
| 1001 | // 8 to 16 bit conversion |
| 1002 | int count = toWrite; |
| 1003 | int16_t *dst = (int16_t *)(audioBuffer.i8); |
| 1004 | while(count--) { |
| 1005 | *dst++ = (int16_t)(*src++^0x80) << 8; |
| 1006 | } |
Eric Laurent | 28ad42b | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 1007 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | toWrite = audioBuffer.size; |
| 1009 | memcpy(audioBuffer.i8, src, toWrite); |
| 1010 | src += toWrite; |
| 1011 | } |
| 1012 | userSize -= toWrite; |
| 1013 | written += toWrite; |
| 1014 | |
| 1015 | releaseBuffer(&audioBuffer); |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1016 | } while (userSize >= frameSz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | |
| 1018 | return written; |
| 1019 | } |
| 1020 | |
| 1021 | // ------------------------------------------------------------------------- |
| 1022 | |
| 1023 | bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread) |
| 1024 | { |
| 1025 | Buffer audioBuffer; |
| 1026 | uint32_t frames; |
| 1027 | size_t writtenSize; |
| 1028 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1029 | mLock.lock(); |
| 1030 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 1031 | // while we are accessing the cblk |
| 1032 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 1033 | sp <IMemory> iMem = mCblkMemory; |
| 1034 | audio_track_cblk_t* cblk = mCblk; |
| 1035 | mLock.unlock(); |
| 1036 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | // Manage underrun callback |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1038 | if (mActive && (cblk->framesAvailable() == cblk->frameCount)) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1039 | LOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags); |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1040 | if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | mCbf(EVENT_UNDERRUN, mUserData, 0); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1042 | if (cblk->server == cblk->frameCount) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1043 | mCbf(EVENT_BUFFER_END, mUserData, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | if (mSharedBuffer != 0) return false; |
| 1046 | } |
| 1047 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1048 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | // Manage loop end callback |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1050 | while (mLoopCount > cblk->loopCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | int loopCount = -1; |
| 1052 | mLoopCount--; |
| 1053 | if (mLoopCount >= 0) loopCount = mLoopCount; |
| 1054 | |
| 1055 | mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount); |
| 1056 | } |
| 1057 | |
| 1058 | // Manage marker callback |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1059 | if (!mMarkerReached && (mMarkerPosition > 0)) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1060 | if (cblk->server >= mMarkerPosition) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition); |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1062 | mMarkerReached = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // Manage new position callback |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1067 | if (mUpdatePeriod > 0) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1068 | while (cblk->server >= mNewPosition) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition); |
| 1070 | mNewPosition += mUpdatePeriod; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | // If Shared buffer is used, no data is requested from client. |
| 1075 | if (mSharedBuffer != 0) { |
| 1076 | frames = 0; |
| 1077 | } else { |
| 1078 | frames = mRemainingFrames; |
| 1079 | } |
| 1080 | |
Eric Laurent | f1d360a | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1081 | int32_t waitCount = -1; |
| 1082 | if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) { |
| 1083 | waitCount = 1; |
| 1084 | } |
| 1085 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | do { |
| 1087 | |
| 1088 | audioBuffer.frameCount = frames; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1089 | |
| 1090 | // Calling obtainBuffer() with a wait count of 1 |
| 1091 | // limits wait time to WAIT_PERIOD_MS. This prevents from being |
| 1092 | // stuck here not being able to handle timed events (position, markers, loops). |
Eric Laurent | f1d360a | 2011-09-07 11:13:23 -0700 | [diff] [blame] | 1093 | status_t err = obtainBuffer(&audioBuffer, waitCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | if (err < NO_ERROR) { |
| 1095 | if (err != TIMED_OUT) { |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 1096 | 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] | 1097 | return false; |
| 1098 | } |
| 1099 | break; |
| 1100 | } |
| 1101 | if (err == status_t(STOPPED)) return false; |
| 1102 | |
| 1103 | // Divide buffer size by 2 to take into account the expansion |
| 1104 | // due to 8 to 16 bit conversion: the callback must fill only half |
| 1105 | // of the destination buffer |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1106 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | audioBuffer.size >>= 1; |
| 1108 | } |
| 1109 | |
| 1110 | size_t reqSize = audioBuffer.size; |
| 1111 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
| 1112 | writtenSize = audioBuffer.size; |
| 1113 | |
| 1114 | // Sanity check on returned size |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1115 | if (ssize_t(writtenSize) <= 0) { |
| 1116 | // The callback is done filling buffers |
| 1117 | // Keep this thread going to handle timed events and |
| 1118 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 1119 | // but don't just loop and block the CPU, so wait |
| 1120 | usleep(WAIT_PERIOD_MS*1000); |
| 1121 | break; |
| 1122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | if (writtenSize > reqSize) writtenSize = reqSize; |
| 1124 | |
Dima Zavin | 24fc2fb | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1125 | if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | // 8 to 16 bit conversion |
| 1127 | const int8_t *src = audioBuffer.i8 + writtenSize-1; |
| 1128 | int count = writtenSize; |
| 1129 | int16_t *dst = audioBuffer.i16 + writtenSize-1; |
| 1130 | while(count--) { |
| 1131 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 1132 | } |
| 1133 | writtenSize <<= 1; |
| 1134 | } |
| 1135 | |
| 1136 | audioBuffer.size = writtenSize; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1137 | // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for |
| 1138 | // 8 bit PCM data: in this case, mCblk->frameSize is based on a sampel size of |
| 1139 | // 16 bit. |
| 1140 | audioBuffer.frameCount = writtenSize/mCblk->frameSize; |
| 1141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | frames -= audioBuffer.frameCount; |
| 1143 | |
| 1144 | releaseBuffer(&audioBuffer); |
| 1145 | } |
| 1146 | while (frames); |
| 1147 | |
| 1148 | if (frames == 0) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1149 | mRemainingFrames = mNotificationFramesAct; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | } else { |
| 1151 | mRemainingFrames = frames; |
| 1152 | } |
| 1153 | return true; |
| 1154 | } |
| 1155 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1156 | // must be called with mLock and cblk.lock held. Callers must also hold strong references on |
| 1157 | // the IAudioTrack and IMemory in case they are recreated here. |
| 1158 | // If the IAudioTrack is successfully restored, the cblk pointer is updated |
| 1159 | status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart) |
| 1160 | { |
| 1161 | status_t result; |
| 1162 | |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1163 | if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) { |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1164 | LOGW("dead IAudioTrack, creating a new one from %s TID %d", |
| 1165 | fromStart ? "start()" : "obtainBuffer()", gettid()); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1166 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1167 | // signal old cblk condition so that other threads waiting for available buffers stop |
| 1168 | // waiting now |
| 1169 | cblk->cv.broadcast(); |
| 1170 | cblk->lock.unlock(); |
| 1171 | |
Eric Laurent | 05ce094 | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1172 | // refresh the audio configuration cache in this process to make sure we get new |
| 1173 | // output parameters in getOutput_l() and createTrack_l() |
| 1174 | AudioSystem::clearAudioConfigCache(); |
| 1175 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1176 | // if the new IAudioTrack is created, createTrack_l() will modify the |
| 1177 | // following member variables: mAudioTrack, mCblkMemory and mCblk. |
| 1178 | // It will also delete the strong references on previous IAudioTrack and IMemory |
| 1179 | result = createTrack_l(mStreamType, |
| 1180 | cblk->sampleRate, |
| 1181 | mFormat, |
Jean-Michel Trivi | 5439223 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 1182 | mChannelMask, |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1183 | mFrameCount, |
| 1184 | mFlags, |
| 1185 | mSharedBuffer, |
| 1186 | getOutput_l(), |
| 1187 | false); |
| 1188 | |
| 1189 | if (result == NO_ERROR) { |
Eric Laurent | b0808f9 | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1190 | uint32_t user = cblk->user; |
| 1191 | uint32_t server = cblk->server; |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1192 | // restore write index and set other indexes to reflect empty buffer status |
Eric Laurent | b0808f9 | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1193 | mCblk->user = user; |
| 1194 | mCblk->server = user; |
| 1195 | mCblk->userBase = user; |
| 1196 | mCblk->serverBase = user; |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1197 | // restore loop: this is not guaranteed to succeed if new frame count is not |
| 1198 | // compatible with loop length |
| 1199 | setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1200 | if (!fromStart) { |
| 1201 | mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
Eric Laurent | b0808f9 | 2011-09-06 12:36:15 -0700 | [diff] [blame] | 1202 | // Make sure that a client relying on callback events indicating underrun or |
| 1203 | // the actual amount of audio frames played (e.g SoundPool) receives them. |
| 1204 | if (mSharedBuffer == 0) { |
| 1205 | uint32_t frames = 0; |
| 1206 | if (user > server) { |
| 1207 | frames = ((user - server) > mCblk->frameCount) ? |
| 1208 | mCblk->frameCount : (user - server); |
| 1209 | memset(mCblk->buffers, 0, frames * mCblk->frameSize); |
| 1210 | } |
| 1211 | // restart playback even if buffer is not completely filled. |
| 1212 | android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags); |
| 1213 | // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to |
| 1214 | // the client |
| 1215 | mCblk->stepUser(frames); |
| 1216 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1217 | } |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1218 | if (mActive) { |
| 1219 | result = mAudioTrack->start(); |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1220 | LOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result); |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1221 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1222 | if (fromStart && result == NO_ERROR) { |
| 1223 | mNewPosition = mCblk->server + mUpdatePeriod; |
| 1224 | } |
| 1225 | } |
| 1226 | if (result != NO_ERROR) { |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1227 | android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags); |
| 1228 | LOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1229 | } |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1230 | mRestoreStatus = result; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1231 | // signal old cblk condition for other threads waiting for restore completion |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1232 | android_atomic_or(CBLK_RESTORED_ON, &cblk->flags); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1233 | cblk->cv.broadcast(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1234 | } else { |
| 1235 | if (!(cblk->flags & CBLK_RESTORED_MSK)) { |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1236 | LOGW("dead IAudioTrack, waiting for a new one TID %d", gettid()); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1237 | mLock.unlock(); |
| 1238 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS)); |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1239 | if (result == NO_ERROR) { |
| 1240 | result = mRestoreStatus; |
| 1241 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1242 | cblk->lock.unlock(); |
| 1243 | mLock.lock(); |
| 1244 | } else { |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1245 | LOGW("dead IAudioTrack, already restored TID %d", gettid()); |
| 1246 | result = mRestoreStatus; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1247 | cblk->lock.unlock(); |
| 1248 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1249 | } |
| 1250 | LOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x", |
| 1251 | result, mActive, mCblk, cblk, mCblk->flags, cblk->flags); |
| 1252 | |
| 1253 | if (result == NO_ERROR) { |
| 1254 | // from now on we switch to the newly created cblk |
| 1255 | cblk = mCblk; |
| 1256 | } |
| 1257 | cblk->lock.lock(); |
| 1258 | |
Eric Laurent | 7e8626f | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 1259 | LOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid()); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1260 | |
| 1261 | return result; |
| 1262 | } |
| 1263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1264 | status_t AudioTrack::dump(int fd, const Vector<String16>& args) const |
| 1265 | { |
| 1266 | |
| 1267 | const size_t SIZE = 256; |
| 1268 | char buffer[SIZE]; |
| 1269 | String8 result; |
| 1270 | |
| 1271 | result.append(" AudioTrack::dump\n"); |
| 1272 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]); |
| 1273 | result.append(buffer); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1274 | 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] | 1275 | result.append(buffer); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 1276 | 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] | 1277 | result.append(buffer); |
| 1278 | snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency); |
| 1279 | result.append(buffer); |
| 1280 | ::write(fd, result.string(), result.size()); |
| 1281 | return NO_ERROR; |
| 1282 | } |
| 1283 | |
| 1284 | // ========================================================================= |
| 1285 | |
| 1286 | AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) |
| 1287 | : Thread(bCanCallJava), mReceiver(receiver) |
| 1288 | { |
| 1289 | } |
| 1290 | |
| 1291 | bool AudioTrack::AudioTrackThread::threadLoop() |
| 1292 | { |
| 1293 | return mReceiver.processAudioBuffer(this); |
| 1294 | } |
| 1295 | |
| 1296 | status_t AudioTrack::AudioTrackThread::readyToRun() |
| 1297 | { |
| 1298 | return NO_ERROR; |
| 1299 | } |
| 1300 | |
| 1301 | void AudioTrack::AudioTrackThread::onFirstRef() |
| 1302 | { |
| 1303 | } |
| 1304 | |
| 1305 | // ========================================================================= |
| 1306 | |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1307 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | audio_track_cblk_t::audio_track_cblk_t() |
Mathias Agopian | a729f97 | 2010-03-19 16:14:13 -0700 | [diff] [blame] | 1309 | : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0), |
| 1310 | userBase(0), serverBase(0), buffers(0), frameCount(0), |
| 1311 | loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0), |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1312 | sendLevel(0), flags(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | { |
| 1314 | } |
| 1315 | |
| 1316 | uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount) |
| 1317 | { |
| 1318 | uint32_t u = this->user; |
| 1319 | |
| 1320 | u += frameCount; |
| 1321 | // Ensure that user is never ahead of server for AudioRecord |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1322 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | // If stepServer() has been called once, switch to normal obtainBuffer() timeout period |
| 1324 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) { |
| 1325 | bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 1326 | } |
| 1327 | } else if (u > this->server) { |
| 1328 | LOGW("stepServer occured after track reset"); |
| 1329 | u = this->server; |
| 1330 | } |
| 1331 | |
| 1332 | if (u >= userBase + this->frameCount) { |
| 1333 | userBase += this->frameCount; |
| 1334 | } |
| 1335 | |
| 1336 | this->user = u; |
| 1337 | |
| 1338 | // Clear flow control error condition as new data has been written/read to/from buffer. |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1339 | if (flags & CBLK_UNDERRUN_MSK) { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1340 | android_atomic_and(~CBLK_UNDERRUN_MSK, &flags); |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1341 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | |
| 1343 | return u; |
| 1344 | } |
| 1345 | |
| 1346 | bool audio_track_cblk_t::stepServer(uint32_t frameCount) |
| 1347 | { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1348 | if (!tryLock()) { |
| 1349 | LOGW("stepServer() could not lock cblk"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | return false; |
| 1351 | } |
| 1352 | |
| 1353 | uint32_t s = this->server; |
| 1354 | |
| 1355 | s += frameCount; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1356 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1357 | // Mark that we have read the first buffer so that next time stepUser() is called |
| 1358 | // we switch to normal obtainBuffer() timeout period |
| 1359 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1360 | bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | // It is possible that we receive a flush() |
| 1363 | // while the mixer is processing a block: in this case, |
| 1364 | // stepServer() is called After the flush() has reset u & s and |
| 1365 | // we have s > u |
| 1366 | if (s > this->user) { |
| 1367 | LOGW("stepServer occured after track reset"); |
| 1368 | s = this->user; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | if (s >= loopEnd) { |
| 1373 | LOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd); |
| 1374 | s = loopStart; |
| 1375 | if (--loopCount == 0) { |
| 1376 | loopEnd = UINT_MAX; |
| 1377 | loopStart = UINT_MAX; |
| 1378 | } |
| 1379 | } |
| 1380 | if (s >= serverBase + this->frameCount) { |
| 1381 | serverBase += this->frameCount; |
| 1382 | } |
| 1383 | |
| 1384 | this->server = s; |
| 1385 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1386 | if (!(flags & CBLK_INVALID_MSK)) { |
| 1387 | cv.signal(); |
| 1388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | lock.unlock(); |
| 1390 | return true; |
| 1391 | } |
| 1392 | |
| 1393 | void* audio_track_cblk_t::buffer(uint32_t offset) const |
| 1394 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1395 | return (int8_t *)this->buffers + (offset - userBase) * this->frameSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | uint32_t audio_track_cblk_t::framesAvailable() |
| 1399 | { |
| 1400 | Mutex::Autolock _l(lock); |
| 1401 | return framesAvailable_l(); |
| 1402 | } |
| 1403 | |
| 1404 | uint32_t audio_track_cblk_t::framesAvailable_l() |
| 1405 | { |
| 1406 | uint32_t u = this->user; |
| 1407 | uint32_t s = this->server; |
| 1408 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1409 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | uint32_t limit = (s < loopStart) ? s : loopStart; |
| 1411 | return limit + frameCount - u; |
| 1412 | } else { |
| 1413 | return frameCount + u - s; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | uint32_t audio_track_cblk_t::framesReady() |
| 1418 | { |
| 1419 | uint32_t u = this->user; |
| 1420 | uint32_t s = this->server; |
| 1421 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1422 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | if (u < loopEnd) { |
| 1424 | return u - s; |
| 1425 | } else { |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1426 | // do not block on mutex shared with client on AudioFlinger side |
| 1427 | if (!tryLock()) { |
| 1428 | LOGW("framesReady() could not lock cblk"); |
| 1429 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | } |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1431 | uint32_t frames = UINT_MAX; |
| 1432 | if (loopCount >= 0) { |
| 1433 | frames = (loopEnd - loopStart)*loopCount + u - s; |
| 1434 | } |
| 1435 | lock.unlock(); |
| 1436 | return frames; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | } |
| 1438 | } else { |
| 1439 | return s - u; |
| 1440 | } |
| 1441 | } |
| 1442 | |
Eric Laurent | ae29b76 | 2011-03-28 18:37:07 -0700 | [diff] [blame] | 1443 | bool audio_track_cblk_t::tryLock() |
| 1444 | { |
| 1445 | // the code below simulates lock-with-timeout |
| 1446 | // we MUST do this to protect the AudioFlinger server |
| 1447 | // as this lock is shared with the client. |
| 1448 | status_t err; |
| 1449 | |
| 1450 | err = lock.tryLock(); |
| 1451 | if (err == -EBUSY) { // just wait a bit |
| 1452 | usleep(1000); |
| 1453 | err = lock.tryLock(); |
| 1454 | } |
| 1455 | if (err != NO_ERROR) { |
| 1456 | // probably, the client just died. |
| 1457 | return false; |
| 1458 | } |
| 1459 | return true; |
| 1460 | } |
| 1461 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | // ------------------------------------------------------------------------- |
| 1463 | |
| 1464 | }; // namespace android |
| 1465 | |