blob: b22c25bd37b90657194b692d3a7713757f56ee0b [file] [log] [blame]
Glenn Kastene5fb2632011-12-14 10:28:06 -08001/* frameworks/base/media/libmedia/AudioTrack.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002**
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 Agopian07952722009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurentae29b762011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Dima Zavin24fc2fb2011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastene80a4cc2011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070042
Dima Zavin34bb4192011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070045
Glenn Kastencc2302d2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yehbd240c22010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
53 int* frameCount,
Glenn Kastenbc1d77b2012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yehbd240c22010-06-16 06:33:13 +080055 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 Project9066cfe2009-03-03 19:31:44 -080078
79// ---------------------------------------------------------------------------
80
81AudioTrack::AudioTrack()
Glenn Kasten99d54432011-06-22 16:15:25 -070082 : mStatus(NO_INIT),
83 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084{
85}
86
87AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -080088 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -080090 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -070091 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 int frameCount,
93 uint32_t flags,
94 callback_t cbf,
95 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -070096 int notificationFrames,
97 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -070098 : mStatus(NO_INIT),
99 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700101 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700102 frameCount, flags, cbf, user, notificationFrames,
103 0, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104}
105
106AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800107 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800109 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700110 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 const sp<IMemory>& sharedBuffer,
112 uint32_t flags,
113 callback_t cbf,
114 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -0700115 int notificationFrames,
116 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -0700117 : mStatus(NO_INIT),
118 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700120 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700121 0, flags, cbf, user, notificationFrames,
122 sharedBuffer, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123}
124
125AudioTrack::~AudioTrack()
126{
Steve Block71f2cf12011-10-20 11:56:00 +0100127 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
129 if (mStatus == NO_ERROR) {
130 // Make sure that callback function exits in the case where
131 // it is looping on buffer full condition in obtainBuffer().
132 // Otherwise the callback thread will never exit.
133 stop();
134 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 mAudioTrackThread->requestExitAndWait();
136 mAudioTrackThread.clear();
137 }
138 mAudioTrack.clear();
139 IPCThreadState::self()->flushCommands();
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700140 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
142}
143
144status_t AudioTrack::set(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800145 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800147 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700148 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 int frameCount,
150 uint32_t flags,
151 callback_t cbf,
152 void* user,
153 int notificationFrames,
154 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700155 bool threadCanCallJava,
156 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157{
158
Steve Block71f2cf12011-10-20 11:56:00 +0100159 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Eric Laurent421ddc02011-03-07 14:52:59 -0800161 AutoMutex lock(mLock);
Eric Laurentef028272009-04-21 07:56:33 -0700162 if (mAudioTrack != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000163 ALOGE("Track already in use");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 return INVALID_OPERATION;
165 }
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 int afSampleRate;
168 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
169 return NO_INIT;
170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 uint32_t afLatency;
172 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
173 return NO_INIT;
174 }
175
176 // handle default values first.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700177 if (streamType == AUDIO_STREAM_DEFAULT) {
178 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180 if (sampleRate == 0) {
181 sampleRate = afSampleRate;
182 }
183 // these below should probably come from the audioFlinger too...
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800184 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700185 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700187 if (channelMask == 0) {
188 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 }
190
191 // validate parameters
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700192 if (!audio_is_valid_format(format)) {
Steve Block3762c312012-01-06 19:20:56 +0000193 ALOGE("Invalid format");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 return BAD_VALUE;
195 }
Eric Laurenta553c252009-07-17 12:17:14 -0700196
197 // force direct flag if format is not linear PCM
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700198 if (!audio_is_linear_pcm(format)) {
199 flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT;
Eric Laurenta553c252009-07-17 12:17:14 -0700200 }
201
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700202 if (!audio_is_output_channel(channelMask)) {
Steve Block3762c312012-01-06 19:20:56 +0000203 ALOGE("Invalid channel mask");
Eric Laurenta553c252009-07-17 12:17:14 -0700204 return BAD_VALUE;
205 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700206 uint32_t channelCount = popcount(channelMask);
Eric Laurenta553c252009-07-17 12:17:14 -0700207
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700208 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800209 streamType,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800210 sampleRate, format, channelMask,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700211 (audio_policy_output_flags_t)flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700212
213 if (output == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000214 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 return BAD_VALUE;
216 }
217
Eric Laurentbda74692009-11-04 08:27:26 -0800218 mVolume[LEFT] = 1.0f;
219 mVolume[RIGHT] = 1.0f;
Glenn Kasten4790bd82012-01-03 14:22:33 -0800220 mSendLevel = 0.0f;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700221 mFrameCount = frameCount;
222 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700223 mSessionId = sessionId;
Eric Laurent7070b362010-07-16 07:43:46 -0700224 mAuxEffectId = 0;
Eric Laurent65b65452010-06-01 23:49:17 -0700225
Eric Laurentbda74692009-11-04 08:27:26 -0800226 // create the IAudioTrack
Eric Laurent421ddc02011-03-07 14:52:59 -0800227 status_t status = createTrack_l(streamType,
228 sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800229 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700230 (uint32_t)channelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -0800231 frameCount,
232 flags,
233 sharedBuffer,
234 output,
235 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Eric Laurentbda74692009-11-04 08:27:26 -0800237 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 return status;
239 }
Eric Laurentbda74692009-11-04 08:27:26 -0800240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 if (cbf != 0) {
242 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
244
245 mStatus = NO_ERROR;
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 mStreamType = streamType;
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800248 mFormat = format;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700249 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 mChannelCount = channelCount;
251 mSharedBuffer = sharedBuffer;
252 mMuted = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800253 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 mLoopCount = 0;
257 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700258 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 mNewPosition = 0;
260 mUpdatePeriod = 0;
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700261 mFlushed = false;
Eric Laurenta553c252009-07-17 12:17:14 -0700262 mFlags = flags;
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700263 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurent7e8626f2011-09-13 15:04:17 -0700264 mRestoreStatus = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 return NO_ERROR;
266}
267
268status_t AudioTrack::initCheck() const
269{
270 return mStatus;
271}
272
273// -------------------------------------------------------------------------
274
275uint32_t AudioTrack::latency() const
276{
277 return mLatency;
278}
279
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800280audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281{
282 return mStreamType;
283}
284
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800285audio_format_t AudioTrack::format() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286{
287 return mFormat;
288}
289
290int AudioTrack::channelCount() const
291{
292 return mChannelCount;
293}
294
295uint32_t AudioTrack::frameCount() const
296{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700297 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298}
299
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800300size_t AudioTrack::frameSize() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700302 if (audio_is_linear_pcm(mFormat)) {
Eric Laurentc310dcb2011-06-16 21:30:45 -0700303 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurenta553c252009-07-17 12:17:14 -0700304 } else {
305 return sizeof(uint8_t);
306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307}
308
309sp<IMemory>& AudioTrack::sharedBuffer()
310{
311 return mSharedBuffer;
312}
313
314// -------------------------------------------------------------------------
315
316void AudioTrack::start()
317{
318 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten028ab992011-06-22 16:18:04 -0700319 status_t status = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320
Steve Block71f2cf12011-10-20 11:56:00 +0100321 ALOGV("start %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 if (t != 0) {
323 if (t->exitPending()) {
324 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block3762c312012-01-06 19:20:56 +0000325 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 return;
327 }
328 }
329 t->mLock.lock();
330 }
331
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800332 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800333 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
334 // while we are accessing the cblk
335 sp <IAudioTrack> audioTrack = mAudioTrack;
336 sp <IMemory> iMem = mCblkMemory;
337 audio_track_cblk_t* cblk = mCblk;
338
Glenn Kastene6810ff2012-01-03 09:42:47 -0800339 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700340 mFlushed = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800341 mActive = true;
Eric Laurent421ddc02011-03-07 14:52:59 -0800342 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700343 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800344 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
345 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700346 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent059b4be2009-11-09 23:32:22 -0800347 if (t != 0) {
Glenn Kasten99d54432011-06-22 16:15:25 -0700348 t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800349 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700350 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
351 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
352 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800353 }
354
Steve Block71f2cf12011-10-20 11:56:00 +0100355 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800356 if (!(cblk->flags & CBLK_INVALID_MSK)) {
357 cblk->lock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700358 status = mAudioTrack->start();
Eric Laurent421ddc02011-03-07 14:52:59 -0800359 cblk->lock.lock();
360 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700361 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800362 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800363 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800364 if (cblk->flags & CBLK_INVALID_MSK) {
365 status = restoreTrack_l(cblk, true);
366 }
367 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800368 if (status != NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100369 ALOGV("start() failed");
Glenn Kastene6810ff2012-01-03 09:42:47 -0800370 mActive = false;
Eric Laurent059b4be2009-11-09 23:32:22 -0800371 if (t != 0) {
372 t->requestExit();
373 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700374 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
375 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent059b4be2009-11-09 23:32:22 -0800376 }
Eric Laurentbda74692009-11-04 08:27:26 -0800377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379
380 if (t != 0) {
381 t->mLock.unlock();
382 }
383}
384
385void AudioTrack::stop()
386{
387 sp<AudioTrackThread> t = mAudioTrackThread;
388
Steve Block71f2cf12011-10-20 11:56:00 +0100389 ALOGV("stop %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 if (t != 0) {
391 t->mLock.lock();
392 }
393
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800394 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800395 if (mActive) {
396 mActive = false;
Eric Laurentef028272009-04-21 07:56:33 -0700397 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 mAudioTrack->stop();
399 // Cancel loops (If we are in the middle of a loop, playback
400 // would not stop until loopCount reaches 0).
Eric Laurent421ddc02011-03-07 14:52:59 -0800401 setLoop_l(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700402 // the playback head position will reset to 0, so if a marker is set, we need
403 // to activate it again
404 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 // Force flush if a shared buffer is used otherwise audioflinger
406 // will not stop before end of buffer is reached.
407 if (mSharedBuffer != 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800408 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410 if (t != 0) {
411 t->requestExit();
412 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700413 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
414 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
416 }
417
418 if (t != 0) {
419 t->mLock.unlock();
420 }
421}
422
423bool AudioTrack::stopped() const
424{
Glenn Kastene6810ff2012-01-03 09:42:47 -0800425 AutoMutex lock(mLock);
426 return stopped_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427}
428
429void AudioTrack::flush()
430{
Eric Laurent421ddc02011-03-07 14:52:59 -0800431 AutoMutex lock(mLock);
432 flush_l();
433}
434
435// must be called with mLock held
436void AudioTrack::flush_l()
437{
Steve Block71f2cf12011-10-20 11:56:00 +0100438 ALOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700439
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700440 // clear playback marker and periodic update counter
441 mMarkerPosition = 0;
442 mMarkerReached = false;
443 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700446 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 mAudioTrack->flush();
448 // Release AudioTrack callback thread in case it was waiting for new buffers
449 // in AudioTrack::obtainBuffer()
450 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452}
453
454void AudioTrack::pause()
455{
Steve Block71f2cf12011-10-20 11:56:00 +0100456 ALOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800457 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800458 if (mActive) {
459 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 mAudioTrack->pause();
461 }
462}
463
464void AudioTrack::mute(bool e)
465{
466 mAudioTrack->mute(e);
467 mMuted = e;
468}
469
470bool AudioTrack::muted() const
471{
472 return mMuted;
473}
474
Eric Laurent65b65452010-06-01 23:49:17 -0700475status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476{
Glenn Kasten1c50a782011-11-30 09:46:04 -0800477 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700478 return BAD_VALUE;
479 }
480
Eric Laurent421ddc02011-03-07 14:52:59 -0800481 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 mVolume[LEFT] = left;
483 mVolume[RIGHT] = right;
484
485 // write must be atomic
Eric Laurent65b65452010-06-01 23:49:17 -0700486 mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000);
487
488 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489}
490
491void AudioTrack::getVolume(float* left, float* right)
492{
Eric Laurent65b65452010-06-01 23:49:17 -0700493 if (left != NULL) {
494 *left = mVolume[LEFT];
495 }
496 if (right != NULL) {
497 *right = mVolume[RIGHT];
498 }
499}
500
Eric Laurent7070b362010-07-16 07:43:46 -0700501status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700502{
Steve Block71f2cf12011-10-20 11:56:00 +0100503 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800504 if (level < 0.0f || level > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700505 return BAD_VALUE;
506 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800507 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700508
509 mSendLevel = level;
510
Glenn Kasten4790bd82012-01-03 14:22:33 -0800511 mCblk->setSendLevel(level);
Eric Laurent65b65452010-06-01 23:49:17 -0700512
513 return NO_ERROR;
514}
515
Eric Laurent7070b362010-07-16 07:43:46 -0700516void AudioTrack::getAuxEffectSendLevel(float* level)
Eric Laurent65b65452010-06-01 23:49:17 -0700517{
518 if (level != NULL) {
519 *level = mSendLevel;
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521}
522
Eric Laurent88e209d2009-07-07 07:10:45 -0700523status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524{
525 int afSamplingRate;
526
527 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700528 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
530 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700531 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532
Eric Laurent421ddc02011-03-07 14:52:59 -0800533 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700534 mCblk->sampleRate = rate;
535 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536}
537
538uint32_t AudioTrack::getSampleRate()
539{
Eric Laurent421ddc02011-03-07 14:52:59 -0800540 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700541 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542}
543
544status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
545{
Eric Laurent421ddc02011-03-07 14:52:59 -0800546 AutoMutex lock(mLock);
547 return setLoop_l(loopStart, loopEnd, loopCount);
548}
549
550// must be called with mLock held
551status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
552{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 audio_track_cblk_t* cblk = mCblk;
554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 Mutex::Autolock _l(cblk->lock);
556
557 if (loopCount == 0) {
558 cblk->loopStart = UINT_MAX;
559 cblk->loopEnd = UINT_MAX;
560 cblk->loopCount = 0;
561 mLoopCount = 0;
562 return NO_ERROR;
563 }
564
565 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700566 loopEnd - loopStart > cblk->frameCount ||
567 cblk->server > loopStart) {
Steve Block3762c312012-01-06 19:20:56 +0000568 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 Project9066cfe2009-03-03 19:31:44 -0800569 return BAD_VALUE;
570 }
571
Eric Laurent6667ac32011-03-21 11:49:00 -0700572 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000573 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700574 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577
578 cblk->loopStart = loopStart;
579 cblk->loopEnd = loopEnd;
580 cblk->loopCount = loopCount;
581 mLoopCount = loopCount;
582
583 return NO_ERROR;
584}
585
586status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount)
587{
Eric Laurent421ddc02011-03-07 14:52:59 -0800588 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 if (loopStart != 0) {
590 *loopStart = mCblk->loopStart;
591 }
592 if (loopEnd != 0) {
593 *loopEnd = mCblk->loopEnd;
594 }
595 if (loopCount != 0) {
596 if (mCblk->loopCount < 0) {
597 *loopCount = -1;
598 } else {
599 *loopCount = mCblk->loopCount;
600 }
601 }
602
603 return NO_ERROR;
604}
605
606status_t AudioTrack::setMarkerPosition(uint32_t marker)
607{
608 if (mCbf == 0) return INVALID_OPERATION;
609
610 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700611 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612
613 return NO_ERROR;
614}
615
616status_t AudioTrack::getMarkerPosition(uint32_t *marker)
617{
618 if (marker == 0) return BAD_VALUE;
619
620 *marker = mMarkerPosition;
621
622 return NO_ERROR;
623}
624
625status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
626{
627 if (mCbf == 0) return INVALID_OPERATION;
628
629 uint32_t curPosition;
630 getPosition(&curPosition);
631 mNewPosition = curPosition + updatePeriod;
632 mUpdatePeriod = updatePeriod;
633
634 return NO_ERROR;
635}
636
637status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod)
638{
639 if (updatePeriod == 0) return BAD_VALUE;
640
641 *updatePeriod = mUpdatePeriod;
642
643 return NO_ERROR;
644}
645
646status_t AudioTrack::setPosition(uint32_t position)
647{
Eric Laurent421ddc02011-03-07 14:52:59 -0800648 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649
Glenn Kastene6810ff2012-01-03 09:42:47 -0800650 if (!stopped_l()) return INVALID_OPERATION;
651
652 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653
654 if (position > mCblk->user) return BAD_VALUE;
655
656 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700657 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 return NO_ERROR;
660}
661
662status_t AudioTrack::getPosition(uint32_t *position)
663{
664 if (position == 0) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800665 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700666 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667
668 return NO_ERROR;
669}
670
671status_t AudioTrack::reload()
672{
Eric Laurent421ddc02011-03-07 14:52:59 -0800673 AutoMutex lock(mLock);
674
Glenn Kastene6810ff2012-01-03 09:42:47 -0800675 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700676
Eric Laurent421ddc02011-03-07 14:52:59 -0800677 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700679 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680
681 return NO_ERROR;
682}
683
Eric Laurenta553c252009-07-17 12:17:14 -0700684audio_io_handle_t AudioTrack::getOutput()
685{
Eric Laurent421ddc02011-03-07 14:52:59 -0800686 AutoMutex lock(mLock);
687 return getOutput_l();
688}
689
690// must be called with mLock held
691audio_io_handle_t AudioTrack::getOutput_l()
692{
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800693 return AudioSystem::getOutput(mStreamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700694 mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700695}
696
Eric Laurent65b65452010-06-01 23:49:17 -0700697int AudioTrack::getSessionId()
698{
699 return mSessionId;
700}
701
702status_t AudioTrack::attachAuxEffect(int effectId)
703{
Steve Block71f2cf12011-10-20 11:56:00 +0100704 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700705 status_t status = mAudioTrack->attachAuxEffect(effectId);
706 if (status == NO_ERROR) {
707 mAuxEffectId = effectId;
708 }
709 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700710}
711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712// -------------------------------------------------------------------------
713
Eric Laurent421ddc02011-03-07 14:52:59 -0800714// must be called with mLock held
715status_t AudioTrack::createTrack_l(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800716 audio_stream_type_t streamType,
Eric Laurentbda74692009-11-04 08:27:26 -0800717 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800718 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700719 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800720 int frameCount,
721 uint32_t flags,
722 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700723 audio_io_handle_t output,
724 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800725{
726 status_t status;
727 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
728 if (audioFlinger == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000729 ALOGE("Could not get audioflinger");
Eric Laurentbda74692009-11-04 08:27:26 -0800730 return NO_INIT;
731 }
732
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700733 int afSampleRate;
734 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
735 return NO_INIT;
736 }
737 int afFrameCount;
738 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
739 return NO_INIT;
740 }
741 uint32_t afLatency;
742 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
743 return NO_INIT;
744 }
745
746 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700747 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700748 if (sharedBuffer != 0) {
749 frameCount = sharedBuffer->size();
750 }
751 } else {
752 // Ensure that buffer depth covers at least audio hardware latency
753 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
754 if (minBufCount < 2) minBufCount = 2;
755
756 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
757
758 if (sharedBuffer == 0) {
759 if (frameCount == 0) {
760 frameCount = minFrameCount;
761 }
762 if (mNotificationFramesAct == 0) {
763 mNotificationFramesAct = frameCount/2;
764 }
765 // Make sure that application is notified with sufficient margin
766 // before underrun
767 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
768 mNotificationFramesAct = frameCount/2;
769 }
770 if (frameCount < minFrameCount) {
771 if (enforceFrameCount) {
Steve Block3762c312012-01-06 19:20:56 +0000772 ALOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700773 return BAD_VALUE;
774 } else {
775 frameCount = minFrameCount;
776 }
777 }
778 } else {
779 // Ensure that buffer alignment matches channelcount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700780 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700781 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000782 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700783 return BAD_VALUE;
784 }
785 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
786 }
787 }
788
Eric Laurentbda74692009-11-04 08:27:26 -0800789 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
790 streamType,
791 sampleRate,
792 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700793 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800794 frameCount,
795 ((uint16_t)flags) << 16,
796 sharedBuffer,
797 output,
Eric Laurent65b65452010-06-01 23:49:17 -0700798 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800799 &status);
800
801 if (track == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000802 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurentbda74692009-11-04 08:27:26 -0800803 return status;
804 }
805 sp<IMemory> cblk = track->getCblk();
806 if (cblk == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000807 ALOGE("Could not get control block");
Eric Laurentbda74692009-11-04 08:27:26 -0800808 return NO_INIT;
809 }
Eric Laurentbda74692009-11-04 08:27:26 -0800810 mAudioTrack = track;
Eric Laurentbda74692009-11-04 08:27:26 -0800811 mCblkMemory = cblk;
812 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurentae29b762011-03-28 18:37:07 -0700813 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurentbda74692009-11-04 08:27:26 -0800814 if (sharedBuffer == 0) {
815 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
816 } else {
817 mCblk->buffers = sharedBuffer->pointer();
818 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700819 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800820 }
821
Eric Laurent65b65452010-06-01 23:49:17 -0700822 mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800823 mCblk->setSendLevel(mSendLevel);
Eric Laurent7070b362010-07-16 07:43:46 -0700824 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent49f02be2009-11-19 09:00:56 -0800825 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
826 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700827 mRemainingFrames = mNotificationFramesAct;
828 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800829 return NO_ERROR;
830}
831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
833{
Eric Laurent421ddc02011-03-07 14:52:59 -0800834 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800835 bool active;
Glenn Kasten028ab992011-06-22 16:18:04 -0700836 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 audio_track_cblk_t* cblk = mCblk;
838 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700839 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840
841 audioBuffer->frameCount = 0;
842 audioBuffer->size = 0;
843
844 uint32_t framesAvail = cblk->framesAvailable();
845
Eric Laurent6667ac32011-03-21 11:49:00 -0700846 cblk->lock.lock();
847 if (cblk->flags & CBLK_INVALID_MSK) {
848 goto create_new_track;
849 }
850 cblk->lock.unlock();
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800853 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 goto start_loop_here;
855 while (framesAvail == 0) {
856 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800857 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100858 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800859 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 return NO_MORE_BUFFERS;
861 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800862 if (CC_UNLIKELY(!waitCount)) {
Eric Laurentbda74692009-11-04 08:27:26 -0800863 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800865 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700866 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800867 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700868 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700869 cblk->lock.unlock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800870 mLock.lock();
Glenn Kastene6810ff2012-01-03 09:42:47 -0800871 if (!mActive) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800872 return status_t(STOPPED);
873 }
874 cblk->lock.lock();
875 }
876
877 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700878 goto create_new_track;
879 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800880 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurentef028272009-04-21 07:56:33 -0700881 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
883 // timing out when a loop has been set and we have already written upto loop end
884 // is a normal condition: no need to wake AudioFlinger up.
885 if (cblk->user < cblk->loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +0000886 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700888 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 cblk->lock.unlock();
Eric Laurentbda74692009-11-04 08:27:26 -0800890 result = mAudioTrack->start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800892 if (result == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700893 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -0800894create_new_track:
895 result = restoreTrack_l(cblk, false);
896 }
897 if (result != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000898 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -0800899 cblk->lock.unlock();
900 return result;
901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903 cblk->waitTimeMs = 0;
904 }
Eric Laurenta553c252009-07-17 12:17:14 -0700905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800907 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 return TIMED_OUT;
909 }
910 }
911 // read the server count again
912 start_loop_here:
913 framesAvail = cblk->framesAvailable_l();
914 }
Eric Laurentbda74692009-11-04 08:27:26 -0800915 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
917
Eric Laurent4712baa2010-09-30 16:12:31 -0700918 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent421ddc02011-03-07 14:52:59 -0800919 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurentae29b762011-03-28 18:37:07 -0700920 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +0000921 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Eric Laurentae29b762011-03-28 18:37:07 -0700922 mAudioTrack->start();
Eric Laurent4712baa2010-09-30 16:12:31 -0700923 }
924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 if (framesReq > framesAvail) {
928 framesReq = framesAvail;
929 }
930
931 uint32_t u = cblk->user;
932 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
933
934 if (u + framesReq > bufferEnd) {
935 framesReq = bufferEnd - u;
936 }
937
Eric Laurenta553c252009-07-17 12:17:14 -0700938 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
939 audioBuffer->channelCount = mChannelCount;
940 audioBuffer->frameCount = framesReq;
941 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700942 if (audio_is_linear_pcm(mFormat)) {
943 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700944 } else {
945 audioBuffer->format = mFormat;
946 }
947 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 active = mActive;
949 return active ? status_t(NO_ERROR) : status_t(STOPPED);
950}
951
952void AudioTrack::releaseBuffer(Buffer* audioBuffer)
953{
Eric Laurent421ddc02011-03-07 14:52:59 -0800954 AutoMutex lock(mLock);
955 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956}
957
958// -------------------------------------------------------------------------
959
960ssize_t AudioTrack::write(const void* buffer, size_t userSize)
961{
962
963 if (mSharedBuffer != 0) return INVALID_OPERATION;
964
965 if (ssize_t(userSize) < 0) {
966 // sanity-check. user is most-likely passing an error code.
Steve Block3762c312012-01-06 19:20:56 +0000967 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 buffer, userSize, userSize);
969 return BAD_VALUE;
970 }
971
Steve Block71f2cf12011-10-20 11:56:00 +0100972 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
Eric Laurent421ddc02011-03-07 14:52:59 -0800974 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
975 // while we are accessing the cblk
976 mLock.lock();
977 sp <IAudioTrack> audioTrack = mAudioTrack;
978 sp <IMemory> iMem = mCblkMemory;
979 mLock.unlock();
980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 ssize_t written = 0;
982 const int8_t *src = (const int8_t *)buffer;
983 Buffer audioBuffer;
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800984 size_t frameSz = frameSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985
986 do {
Eric Laurent913af0b2011-03-17 09:36:51 -0700987 audioBuffer.frameCount = userSize/frameSz;
Eric Laurenta553c252009-07-17 12:17:14 -0700988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 // Calling obtainBuffer() with a negative wait count causes
990 // an (almost) infinite wait time.
991 status_t err = obtainBuffer(&audioBuffer, -1);
992 if (err < 0) {
993 // out of buffers, return #bytes written
994 if (err == status_t(NO_MORE_BUFFERS))
995 break;
996 return ssize_t(err);
997 }
998
999 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -07001000
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001001 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 // Divide capacity by 2 to take expansion into account
1003 toWrite = audioBuffer.size>>1;
Glenn Kastencc2302d2012-01-11 09:52:19 -08001004 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent28ad42b2009-08-04 10:42:26 -07001005 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 toWrite = audioBuffer.size;
1007 memcpy(audioBuffer.i8, src, toWrite);
1008 src += toWrite;
1009 }
1010 userSize -= toWrite;
1011 written += toWrite;
1012
1013 releaseBuffer(&audioBuffer);
Eric Laurent913af0b2011-03-17 09:36:51 -07001014 } while (userSize >= frameSz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015
1016 return written;
1017}
1018
1019// -------------------------------------------------------------------------
1020
1021bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1022{
1023 Buffer audioBuffer;
1024 uint32_t frames;
1025 size_t writtenSize;
1026
Eric Laurent421ddc02011-03-07 14:52:59 -08001027 mLock.lock();
1028 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1029 // while we are accessing the cblk
1030 sp <IAudioTrack> audioTrack = mAudioTrack;
1031 sp <IMemory> iMem = mCblkMemory;
1032 audio_track_cblk_t* cblk = mCblk;
Glenn Kastene6810ff2012-01-03 09:42:47 -08001033 bool active = mActive;
Eric Laurent421ddc02011-03-07 14:52:59 -08001034 mLock.unlock();
1035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 // Manage underrun callback
Glenn Kastene6810ff2012-01-03 09:42:47 -08001037 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block71f2cf12011-10-20 11:56:00 +01001038 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001039 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001041 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001042 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 if (mSharedBuffer != 0) return false;
1045 }
1046 }
Eric Laurenta553c252009-07-17 12:17:14 -07001047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001049 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 int loopCount = -1;
1051 mLoopCount--;
1052 if (mLoopCount >= 0) loopCount = mLoopCount;
1053
1054 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1055 }
1056
1057 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001058 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001059 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001061 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063 }
1064
1065 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001066 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001067 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1069 mNewPosition += mUpdatePeriod;
1070 }
1071 }
1072
1073 // If Shared buffer is used, no data is requested from client.
1074 if (mSharedBuffer != 0) {
1075 frames = 0;
1076 } else {
1077 frames = mRemainingFrames;
1078 }
1079
Eric Laurentf1d360a2011-09-07 11:13:23 -07001080 int32_t waitCount = -1;
1081 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1082 waitCount = 1;
1083 }
1084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 do {
1086
1087 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001088
1089 // Calling obtainBuffer() with a wait count of 1
1090 // limits wait time to WAIT_PERIOD_MS. This prevents from being
1091 // stuck here not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001092 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 if (err < NO_ERROR) {
1094 if (err != TIMED_OUT) {
Steve Block3762c312012-01-06 19:20:56 +00001095 ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 return false;
1097 }
1098 break;
1099 }
1100 if (err == status_t(STOPPED)) return false;
1101
1102 // Divide buffer size by 2 to take into account the expansion
1103 // due to 8 to 16 bit conversion: the callback must fill only half
1104 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001105 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 audioBuffer.size >>= 1;
1107 }
1108
1109 size_t reqSize = audioBuffer.size;
1110 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1111 writtenSize = audioBuffer.size;
1112
1113 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001114 if (ssize_t(writtenSize) <= 0) {
1115 // The callback is done filling buffers
1116 // Keep this thread going to handle timed events and
1117 // still try to get more data in intervals of WAIT_PERIOD_MS
1118 // but don't just loop and block the CPU, so wait
1119 usleep(WAIT_PERIOD_MS*1000);
1120 break;
1121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (writtenSize > reqSize) writtenSize = reqSize;
1123
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001124 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kastencc2302d2012-01-11 09:52:19 -08001125 // 8 to 16 bit conversion, note that source and destination are the same address
1126 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 writtenSize <<= 1;
1128 }
1129
1130 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001131 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001132 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurenta553c252009-07-17 12:17:14 -07001133 // 16 bit.
1134 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 frames -= audioBuffer.frameCount;
1137
1138 releaseBuffer(&audioBuffer);
1139 }
1140 while (frames);
1141
1142 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001143 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 } else {
1145 mRemainingFrames = frames;
1146 }
1147 return true;
1148}
1149
Eric Laurent421ddc02011-03-07 14:52:59 -08001150// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1151// the IAudioTrack and IMemory in case they are recreated here.
1152// If the IAudioTrack is successfully restored, the cblk pointer is updated
1153status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1154{
1155 status_t result;
1156
Eric Laurentae29b762011-03-28 18:37:07 -07001157 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001158 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Eric Laurent7e8626f2011-09-13 15:04:17 -07001159 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001160
Eric Laurent421ddc02011-03-07 14:52:59 -08001161 // signal old cblk condition so that other threads waiting for available buffers stop
1162 // waiting now
1163 cblk->cv.broadcast();
1164 cblk->lock.unlock();
1165
Eric Laurent05ce0942011-08-30 10:18:54 -07001166 // refresh the audio configuration cache in this process to make sure we get new
1167 // output parameters in getOutput_l() and createTrack_l()
1168 AudioSystem::clearAudioConfigCache();
1169
Eric Laurent421ddc02011-03-07 14:52:59 -08001170 // if the new IAudioTrack is created, createTrack_l() will modify the
1171 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1172 // It will also delete the strong references on previous IAudioTrack and IMemory
1173 result = createTrack_l(mStreamType,
1174 cblk->sampleRate,
1175 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001176 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001177 mFrameCount,
1178 mFlags,
1179 mSharedBuffer,
1180 getOutput_l(),
1181 false);
1182
1183 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001184 uint32_t user = cblk->user;
1185 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001186 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001187 mCblk->user = user;
1188 mCblk->server = user;
1189 mCblk->userBase = user;
1190 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001191 // restore loop: this is not guaranteed to succeed if new frame count is not
1192 // compatible with loop length
1193 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001194 if (!fromStart) {
1195 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001196 // Make sure that a client relying on callback events indicating underrun or
1197 // the actual amount of audio frames played (e.g SoundPool) receives them.
1198 if (mSharedBuffer == 0) {
1199 uint32_t frames = 0;
1200 if (user > server) {
1201 frames = ((user - server) > mCblk->frameCount) ?
1202 mCblk->frameCount : (user - server);
1203 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1204 }
1205 // restart playback even if buffer is not completely filled.
1206 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1207 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1208 // the client
1209 mCblk->stepUser(frames);
1210 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001211 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001212 if (mActive) {
1213 result = mAudioTrack->start();
Steve Block8564c8d2012-01-05 23:22:43 +00001214 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent6667ac32011-03-21 11:49:00 -07001215 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001216 if (fromStart && result == NO_ERROR) {
1217 mNewPosition = mCblk->server + mUpdatePeriod;
1218 }
1219 }
1220 if (result != NO_ERROR) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001221 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +00001222 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -08001223 }
Eric Laurent7e8626f2011-09-13 15:04:17 -07001224 mRestoreStatus = result;
Eric Laurent421ddc02011-03-07 14:52:59 -08001225 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001226 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001227 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001228 } else {
1229 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001230 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001231 mLock.unlock();
1232 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurent7e8626f2011-09-13 15:04:17 -07001233 if (result == NO_ERROR) {
1234 result = mRestoreStatus;
1235 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001236 cblk->lock.unlock();
1237 mLock.lock();
1238 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001239 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurent7e8626f2011-09-13 15:04:17 -07001240 result = mRestoreStatus;
Eric Laurent421ddc02011-03-07 14:52:59 -08001241 cblk->lock.unlock();
1242 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001243 }
Steve Block71f2cf12011-10-20 11:56:00 +01001244 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Eric Laurent421ddc02011-03-07 14:52:59 -08001245 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1246
1247 if (result == NO_ERROR) {
1248 // from now on we switch to the newly created cblk
1249 cblk = mCblk;
1250 }
1251 cblk->lock.lock();
1252
Steve Block8564c8d2012-01-05 23:22:43 +00001253 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001254
1255 return result;
1256}
1257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1259{
1260
1261 const size_t SIZE = 256;
1262 char buffer[SIZE];
1263 String8 result;
1264
1265 result.append(" AudioTrack::dump\n");
1266 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1267 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001268 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001270 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 result.append(buffer);
1272 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1273 result.append(buffer);
1274 ::write(fd, result.string(), result.size());
1275 return NO_ERROR;
1276}
1277
1278// =========================================================================
1279
1280AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1281 : Thread(bCanCallJava), mReceiver(receiver)
1282{
1283}
1284
1285bool AudioTrack::AudioTrackThread::threadLoop()
1286{
1287 return mReceiver.processAudioBuffer(this);
1288}
1289
1290status_t AudioTrack::AudioTrackThread::readyToRun()
1291{
1292 return NO_ERROR;
1293}
1294
1295void AudioTrack::AudioTrackThread::onFirstRef()
1296{
1297}
1298
1299// =========================================================================
1300
Eric Laurentae29b762011-03-28 18:37:07 -07001301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001303 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
1304 userBase(0), serverBase(0), buffers(0), frameCount(0),
1305 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0),
Glenn Kasten4790bd82012-01-03 14:22:33 -08001306 mSendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307{
1308}
1309
1310uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1311{
Glenn Kastendb298a42011-12-13 11:45:07 -08001312 uint32_t u = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313
1314 u += frameCount;
1315 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001316 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1318 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1319 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1320 }
Glenn Kastendb298a42011-12-13 11:45:07 -08001321 } else if (u > server) {
Steve Block8564c8d2012-01-05 23:22:43 +00001322 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001323 u = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 }
1325
1326 if (u >= userBase + this->frameCount) {
1327 userBase += this->frameCount;
1328 }
1329
Glenn Kastendb298a42011-12-13 11:45:07 -08001330 user = u;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001333 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001334 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336
1337 return u;
1338}
1339
1340bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1341{
Eric Laurentae29b762011-03-28 18:37:07 -07001342 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001343 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 return false;
1345 }
1346
Glenn Kastendb298a42011-12-13 11:45:07 -08001347 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348
1349 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001350 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 // Mark that we have read the first buffer so that next time stepUser() is called
1352 // we switch to normal obtainBuffer() timeout period
1353 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001354 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 // It is possible that we receive a flush()
1357 // while the mixer is processing a block: in this case,
1358 // stepServer() is called After the flush() has reset u & s and
1359 // we have s > u
Glenn Kastendb298a42011-12-13 11:45:07 -08001360 if (s > user) {
Steve Block8564c8d2012-01-05 23:22:43 +00001361 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001362 s = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 }
1365
1366 if (s >= loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +00001367 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 s = loopStart;
1369 if (--loopCount == 0) {
1370 loopEnd = UINT_MAX;
1371 loopStart = UINT_MAX;
1372 }
1373 }
1374 if (s >= serverBase + this->frameCount) {
1375 serverBase += this->frameCount;
1376 }
1377
Glenn Kastendb298a42011-12-13 11:45:07 -08001378 server = s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379
Eric Laurent421ddc02011-03-07 14:52:59 -08001380 if (!(flags & CBLK_INVALID_MSK)) {
1381 cv.signal();
1382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 lock.unlock();
1384 return true;
1385}
1386
1387void* audio_track_cblk_t::buffer(uint32_t offset) const
1388{
Glenn Kastendb298a42011-12-13 11:45:07 -08001389 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390}
1391
1392uint32_t audio_track_cblk_t::framesAvailable()
1393{
1394 Mutex::Autolock _l(lock);
1395 return framesAvailable_l();
1396}
1397
1398uint32_t audio_track_cblk_t::framesAvailable_l()
1399{
Glenn Kastendb298a42011-12-13 11:45:07 -08001400 uint32_t u = user;
1401 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001403 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 uint32_t limit = (s < loopStart) ? s : loopStart;
1405 return limit + frameCount - u;
1406 } else {
1407 return frameCount + u - s;
1408 }
1409}
1410
1411uint32_t audio_track_cblk_t::framesReady()
1412{
Glenn Kastendb298a42011-12-13 11:45:07 -08001413 uint32_t u = user;
1414 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001416 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 if (u < loopEnd) {
1418 return u - s;
1419 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001420 // do not block on mutex shared with client on AudioFlinger side
1421 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001422 ALOGW("framesReady() could not lock cblk");
Eric Laurentae29b762011-03-28 18:37:07 -07001423 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 }
Eric Laurentae29b762011-03-28 18:37:07 -07001425 uint32_t frames = UINT_MAX;
1426 if (loopCount >= 0) {
1427 frames = (loopEnd - loopStart)*loopCount + u - s;
1428 }
1429 lock.unlock();
1430 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432 } else {
1433 return s - u;
1434 }
1435}
1436
Eric Laurentae29b762011-03-28 18:37:07 -07001437bool audio_track_cblk_t::tryLock()
1438{
1439 // the code below simulates lock-with-timeout
1440 // we MUST do this to protect the AudioFlinger server
1441 // as this lock is shared with the client.
1442 status_t err;
1443
1444 err = lock.tryLock();
1445 if (err == -EBUSY) { // just wait a bit
1446 usleep(1000);
1447 err = lock.tryLock();
1448 }
1449 if (err != NO_ERROR) {
1450 // probably, the client just died.
1451 return false;
1452 }
1453 return true;
1454}
1455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456// -------------------------------------------------------------------------
1457
1458}; // namespace android