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