blob: 8c33f412fd3e7bbaed7bd7cce0225e90843eeadc [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(
Andreas Huber28ea0132012-01-18 10:51:55 -0800107 int streamType,
108 uint32_t sampleRate,
109 int format,
110 int channelMask,
111 int frameCount,
112 uint32_t flags,
113 callback_t cbf,
114 void* user,
115 int notificationFrames,
116 int sessionId)
117 : mStatus(NO_INIT),
118 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
119{
120 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
121 frameCount, flags, cbf, user, notificationFrames,
122 0, false, sessionId);
123}
124
125AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800126 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800128 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700129 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 const sp<IMemory>& sharedBuffer,
131 uint32_t flags,
132 callback_t cbf,
133 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -0700134 int notificationFrames,
135 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -0700136 : mStatus(NO_INIT),
137 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700139 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700140 0, flags, cbf, user, notificationFrames,
141 sharedBuffer, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142}
143
144AudioTrack::~AudioTrack()
145{
Steve Block71f2cf12011-10-20 11:56:00 +0100146 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 if (mStatus == NO_ERROR) {
149 // Make sure that callback function exits in the case where
150 // it is looping on buffer full condition in obtainBuffer().
151 // Otherwise the callback thread will never exit.
152 stop();
153 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 mAudioTrackThread->requestExitAndWait();
155 mAudioTrackThread.clear();
156 }
157 mAudioTrack.clear();
158 IPCThreadState::self()->flushCommands();
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700159 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
161}
162
163status_t AudioTrack::set(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800164 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800166 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700167 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 int frameCount,
169 uint32_t flags,
170 callback_t cbf,
171 void* user,
172 int notificationFrames,
173 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700174 bool threadCanCallJava,
175 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176{
177
Steve Block71f2cf12011-10-20 11:56:00 +0100178 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Eric Laurent421ddc02011-03-07 14:52:59 -0800180 AutoMutex lock(mLock);
Eric Laurentef028272009-04-21 07:56:33 -0700181 if (mAudioTrack != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000182 ALOGE("Track already in use");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 return INVALID_OPERATION;
184 }
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 int afSampleRate;
187 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
188 return NO_INIT;
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 uint32_t afLatency;
191 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
192 return NO_INIT;
193 }
194
195 // handle default values first.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700196 if (streamType == AUDIO_STREAM_DEFAULT) {
197 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
199 if (sampleRate == 0) {
200 sampleRate = afSampleRate;
201 }
202 // these below should probably come from the audioFlinger too...
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800203 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700204 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700206 if (channelMask == 0) {
207 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 }
209
210 // validate parameters
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700211 if (!audio_is_valid_format(format)) {
Steve Block3762c312012-01-06 19:20:56 +0000212 ALOGE("Invalid format");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 return BAD_VALUE;
214 }
Eric Laurenta553c252009-07-17 12:17:14 -0700215
216 // force direct flag if format is not linear PCM
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700217 if (!audio_is_linear_pcm(format)) {
218 flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT;
Eric Laurenta553c252009-07-17 12:17:14 -0700219 }
220
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700221 if (!audio_is_output_channel(channelMask)) {
Steve Block3762c312012-01-06 19:20:56 +0000222 ALOGE("Invalid channel mask");
Eric Laurenta553c252009-07-17 12:17:14 -0700223 return BAD_VALUE;
224 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700225 uint32_t channelCount = popcount(channelMask);
Eric Laurenta553c252009-07-17 12:17:14 -0700226
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700227 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800228 streamType,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800229 sampleRate, format, channelMask,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700230 (audio_policy_output_flags_t)flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700231
232 if (output == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000233 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 return BAD_VALUE;
235 }
236
Eric Laurentbda74692009-11-04 08:27:26 -0800237 mVolume[LEFT] = 1.0f;
238 mVolume[RIGHT] = 1.0f;
Glenn Kasten4790bd82012-01-03 14:22:33 -0800239 mSendLevel = 0.0f;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700240 mFrameCount = frameCount;
241 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700242 mSessionId = sessionId;
Eric Laurent7070b362010-07-16 07:43:46 -0700243 mAuxEffectId = 0;
Eric Laurent65b65452010-06-01 23:49:17 -0700244
Eric Laurentbda74692009-11-04 08:27:26 -0800245 // create the IAudioTrack
Eric Laurent421ddc02011-03-07 14:52:59 -0800246 status_t status = createTrack_l(streamType,
247 sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800248 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700249 (uint32_t)channelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -0800250 frameCount,
251 flags,
252 sharedBuffer,
253 output,
254 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
Eric Laurentbda74692009-11-04 08:27:26 -0800256 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 return status;
258 }
Eric Laurentbda74692009-11-04 08:27:26 -0800259
Glenn Kasten3694ec12012-01-27 16:47:15 -0800260 if (cbf != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263
264 mStatus = NO_ERROR;
265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 mStreamType = streamType;
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800267 mFormat = format;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700268 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 mChannelCount = channelCount;
270 mSharedBuffer = sharedBuffer;
271 mMuted = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800272 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mLoopCount = 0;
276 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700277 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 mNewPosition = 0;
279 mUpdatePeriod = 0;
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700280 mFlushed = false;
Eric Laurenta553c252009-07-17 12:17:14 -0700281 mFlags = flags;
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700282 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurent7e8626f2011-09-13 15:04:17 -0700283 mRestoreStatus = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 return NO_ERROR;
285}
286
287status_t AudioTrack::initCheck() const
288{
289 return mStatus;
290}
291
292// -------------------------------------------------------------------------
293
294uint32_t AudioTrack::latency() const
295{
296 return mLatency;
297}
298
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800299audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300{
301 return mStreamType;
302}
303
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800304audio_format_t AudioTrack::format() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305{
306 return mFormat;
307}
308
309int AudioTrack::channelCount() const
310{
311 return mChannelCount;
312}
313
314uint32_t AudioTrack::frameCount() const
315{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700316 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317}
318
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800319size_t AudioTrack::frameSize() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700321 if (audio_is_linear_pcm(mFormat)) {
Eric Laurentc310dcb2011-06-16 21:30:45 -0700322 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurenta553c252009-07-17 12:17:14 -0700323 } else {
324 return sizeof(uint8_t);
325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326}
327
328sp<IMemory>& AudioTrack::sharedBuffer()
329{
330 return mSharedBuffer;
331}
332
333// -------------------------------------------------------------------------
334
335void AudioTrack::start()
336{
337 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten028ab992011-06-22 16:18:04 -0700338 status_t status = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339
Steve Block71f2cf12011-10-20 11:56:00 +0100340 ALOGV("start %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 if (t != 0) {
342 if (t->exitPending()) {
343 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block3762c312012-01-06 19:20:56 +0000344 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 return;
346 }
347 }
348 t->mLock.lock();
349 }
350
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800351 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800352 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
353 // while we are accessing the cblk
354 sp <IAudioTrack> audioTrack = mAudioTrack;
355 sp <IMemory> iMem = mCblkMemory;
356 audio_track_cblk_t* cblk = mCblk;
357
Glenn Kastene6810ff2012-01-03 09:42:47 -0800358 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700359 mFlushed = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800360 mActive = true;
Eric Laurent421ddc02011-03-07 14:52:59 -0800361 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700362 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800363 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
364 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700365 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent059b4be2009-11-09 23:32:22 -0800366 if (t != 0) {
Glenn Kasten99d54432011-06-22 16:15:25 -0700367 t->run("AudioTrackThread", ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800368 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700369 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
370 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
371 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent059b4be2009-11-09 23:32:22 -0800372 }
373
Steve Block71f2cf12011-10-20 11:56:00 +0100374 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800375 if (!(cblk->flags & CBLK_INVALID_MSK)) {
376 cblk->lock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700377 status = mAudioTrack->start();
Eric Laurent421ddc02011-03-07 14:52:59 -0800378 cblk->lock.lock();
379 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700380 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800381 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800382 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800383 if (cblk->flags & CBLK_INVALID_MSK) {
384 status = restoreTrack_l(cblk, true);
385 }
386 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800387 if (status != NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100388 ALOGV("start() failed");
Glenn Kastene6810ff2012-01-03 09:42:47 -0800389 mActive = false;
Eric Laurent059b4be2009-11-09 23:32:22 -0800390 if (t != 0) {
391 t->requestExit();
392 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700393 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
394 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent059b4be2009-11-09 23:32:22 -0800395 }
Eric Laurentbda74692009-11-04 08:27:26 -0800396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398
399 if (t != 0) {
400 t->mLock.unlock();
401 }
402}
403
404void AudioTrack::stop()
405{
406 sp<AudioTrackThread> t = mAudioTrackThread;
407
Steve Block71f2cf12011-10-20 11:56:00 +0100408 ALOGV("stop %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 if (t != 0) {
410 t->mLock.lock();
411 }
412
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800413 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800414 if (mActive) {
415 mActive = false;
Eric Laurentef028272009-04-21 07:56:33 -0700416 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 mAudioTrack->stop();
418 // Cancel loops (If we are in the middle of a loop, playback
419 // would not stop until loopCount reaches 0).
Eric Laurent421ddc02011-03-07 14:52:59 -0800420 setLoop_l(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700421 // the playback head position will reset to 0, so if a marker is set, we need
422 // to activate it again
423 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 // Force flush if a shared buffer is used otherwise audioflinger
425 // will not stop before end of buffer is reached.
426 if (mSharedBuffer != 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800427 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429 if (t != 0) {
430 t->requestExit();
431 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700432 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
433 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
435 }
436
437 if (t != 0) {
438 t->mLock.unlock();
439 }
440}
441
442bool AudioTrack::stopped() const
443{
Glenn Kastene6810ff2012-01-03 09:42:47 -0800444 AutoMutex lock(mLock);
445 return stopped_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446}
447
448void AudioTrack::flush()
449{
Eric Laurent421ddc02011-03-07 14:52:59 -0800450 AutoMutex lock(mLock);
451 flush_l();
452}
453
454// must be called with mLock held
455void AudioTrack::flush_l()
456{
Steve Block71f2cf12011-10-20 11:56:00 +0100457 ALOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700458
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700459 // clear playback marker and periodic update counter
460 mMarkerPosition = 0;
461 mMarkerReached = false;
462 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700465 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 mAudioTrack->flush();
467 // Release AudioTrack callback thread in case it was waiting for new buffers
468 // in AudioTrack::obtainBuffer()
469 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471}
472
473void AudioTrack::pause()
474{
Steve Block71f2cf12011-10-20 11:56:00 +0100475 ALOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800476 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800477 if (mActive) {
478 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 mAudioTrack->pause();
480 }
481}
482
483void AudioTrack::mute(bool e)
484{
485 mAudioTrack->mute(e);
486 mMuted = e;
487}
488
489bool AudioTrack::muted() const
490{
491 return mMuted;
492}
493
Eric Laurent65b65452010-06-01 23:49:17 -0700494status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495{
Glenn Kasten1c50a782011-11-30 09:46:04 -0800496 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700497 return BAD_VALUE;
498 }
499
Eric Laurent421ddc02011-03-07 14:52:59 -0800500 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 mVolume[LEFT] = left;
502 mVolume[RIGHT] = right;
503
Glenn Kastenbc4de882012-01-17 14:39:34 -0800504 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurent65b65452010-06-01 23:49:17 -0700505
506 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507}
508
509void AudioTrack::getVolume(float* left, float* right)
510{
Eric Laurent65b65452010-06-01 23:49:17 -0700511 if (left != NULL) {
512 *left = mVolume[LEFT];
513 }
514 if (right != NULL) {
515 *right = mVolume[RIGHT];
516 }
517}
518
Eric Laurent7070b362010-07-16 07:43:46 -0700519status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700520{
Steve Block71f2cf12011-10-20 11:56:00 +0100521 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800522 if (level < 0.0f || level > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700523 return BAD_VALUE;
524 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800525 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700526
527 mSendLevel = level;
528
Glenn Kasten4790bd82012-01-03 14:22:33 -0800529 mCblk->setSendLevel(level);
Eric Laurent65b65452010-06-01 23:49:17 -0700530
531 return NO_ERROR;
532}
533
Eric Laurent7070b362010-07-16 07:43:46 -0700534void AudioTrack::getAuxEffectSendLevel(float* level)
Eric Laurent65b65452010-06-01 23:49:17 -0700535{
536 if (level != NULL) {
537 *level = mSendLevel;
538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539}
540
Eric Laurent88e209d2009-07-07 07:10:45 -0700541status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542{
543 int afSamplingRate;
544
545 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700546 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
548 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700549 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550
Eric Laurent421ddc02011-03-07 14:52:59 -0800551 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700552 mCblk->sampleRate = rate;
553 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554}
555
556uint32_t AudioTrack::getSampleRate()
557{
Eric Laurent421ddc02011-03-07 14:52:59 -0800558 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700559 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560}
561
562status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
563{
Eric Laurent421ddc02011-03-07 14:52:59 -0800564 AutoMutex lock(mLock);
565 return setLoop_l(loopStart, loopEnd, loopCount);
566}
567
568// must be called with mLock held
569status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
570{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 audio_track_cblk_t* cblk = mCblk;
572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 Mutex::Autolock _l(cblk->lock);
574
575 if (loopCount == 0) {
576 cblk->loopStart = UINT_MAX;
577 cblk->loopEnd = UINT_MAX;
578 cblk->loopCount = 0;
579 mLoopCount = 0;
580 return NO_ERROR;
581 }
582
583 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700584 loopEnd - loopStart > cblk->frameCount ||
585 cblk->server > loopStart) {
Steve Block3762c312012-01-06 19:20:56 +0000586 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 -0800587 return BAD_VALUE;
588 }
589
Eric Laurent6667ac32011-03-21 11:49:00 -0700590 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000591 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700592 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595
596 cblk->loopStart = loopStart;
597 cblk->loopEnd = loopEnd;
598 cblk->loopCount = loopCount;
599 mLoopCount = loopCount;
600
601 return NO_ERROR;
602}
603
604status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount)
605{
Eric Laurent421ddc02011-03-07 14:52:59 -0800606 AutoMutex lock(mLock);
Glenn Kasten3694ec12012-01-27 16:47:15 -0800607 if (loopStart != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 *loopStart = mCblk->loopStart;
609 }
Glenn Kasten3694ec12012-01-27 16:47:15 -0800610 if (loopEnd != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 *loopEnd = mCblk->loopEnd;
612 }
Glenn Kasten3694ec12012-01-27 16:47:15 -0800613 if (loopCount != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 if (mCblk->loopCount < 0) {
615 *loopCount = -1;
616 } else {
617 *loopCount = mCblk->loopCount;
618 }
619 }
620
621 return NO_ERROR;
622}
623
624status_t AudioTrack::setMarkerPosition(uint32_t marker)
625{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800626 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700629 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
631 return NO_ERROR;
632}
633
634status_t AudioTrack::getMarkerPosition(uint32_t *marker)
635{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800636 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637
638 *marker = mMarkerPosition;
639
640 return NO_ERROR;
641}
642
643status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
644{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800645 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646
647 uint32_t curPosition;
648 getPosition(&curPosition);
649 mNewPosition = curPosition + updatePeriod;
650 mUpdatePeriod = updatePeriod;
651
652 return NO_ERROR;
653}
654
655status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod)
656{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800657 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
659 *updatePeriod = mUpdatePeriod;
660
661 return NO_ERROR;
662}
663
664status_t AudioTrack::setPosition(uint32_t position)
665{
Eric Laurent421ddc02011-03-07 14:52:59 -0800666 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667
Glenn Kastene6810ff2012-01-03 09:42:47 -0800668 if (!stopped_l()) return INVALID_OPERATION;
669
670 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671
672 if (position > mCblk->user) return BAD_VALUE;
673
674 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700675 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 return NO_ERROR;
678}
679
680status_t AudioTrack::getPosition(uint32_t *position)
681{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800682 if (position == NULL) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800683 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700684 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685
686 return NO_ERROR;
687}
688
689status_t AudioTrack::reload()
690{
Eric Laurent421ddc02011-03-07 14:52:59 -0800691 AutoMutex lock(mLock);
692
Glenn Kastene6810ff2012-01-03 09:42:47 -0800693 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700694
Eric Laurent421ddc02011-03-07 14:52:59 -0800695 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700697 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698
699 return NO_ERROR;
700}
701
Eric Laurenta553c252009-07-17 12:17:14 -0700702audio_io_handle_t AudioTrack::getOutput()
703{
Eric Laurent421ddc02011-03-07 14:52:59 -0800704 AutoMutex lock(mLock);
705 return getOutput_l();
706}
707
708// must be called with mLock held
709audio_io_handle_t AudioTrack::getOutput_l()
710{
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800711 return AudioSystem::getOutput(mStreamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700712 mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700713}
714
Eric Laurent65b65452010-06-01 23:49:17 -0700715int AudioTrack::getSessionId()
716{
717 return mSessionId;
718}
719
720status_t AudioTrack::attachAuxEffect(int effectId)
721{
Steve Block71f2cf12011-10-20 11:56:00 +0100722 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700723 status_t status = mAudioTrack->attachAuxEffect(effectId);
724 if (status == NO_ERROR) {
725 mAuxEffectId = effectId;
726 }
727 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700728}
729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730// -------------------------------------------------------------------------
731
Eric Laurent421ddc02011-03-07 14:52:59 -0800732// must be called with mLock held
733status_t AudioTrack::createTrack_l(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800734 audio_stream_type_t streamType,
Eric Laurentbda74692009-11-04 08:27:26 -0800735 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800736 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700737 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800738 int frameCount,
739 uint32_t flags,
740 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700741 audio_io_handle_t output,
742 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800743{
744 status_t status;
745 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
746 if (audioFlinger == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000747 ALOGE("Could not get audioflinger");
Eric Laurentbda74692009-11-04 08:27:26 -0800748 return NO_INIT;
749 }
750
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700751 int afSampleRate;
752 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
753 return NO_INIT;
754 }
755 int afFrameCount;
756 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
757 return NO_INIT;
758 }
759 uint32_t afLatency;
760 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
761 return NO_INIT;
762 }
763
764 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700765 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700766 if (sharedBuffer != 0) {
767 frameCount = sharedBuffer->size();
768 }
769 } else {
770 // Ensure that buffer depth covers at least audio hardware latency
771 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
772 if (minBufCount < 2) minBufCount = 2;
773
774 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
775
776 if (sharedBuffer == 0) {
777 if (frameCount == 0) {
778 frameCount = minFrameCount;
779 }
780 if (mNotificationFramesAct == 0) {
781 mNotificationFramesAct = frameCount/2;
782 }
783 // Make sure that application is notified with sufficient margin
784 // before underrun
785 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
786 mNotificationFramesAct = frameCount/2;
787 }
788 if (frameCount < minFrameCount) {
789 if (enforceFrameCount) {
Steve Block3762c312012-01-06 19:20:56 +0000790 ALOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700791 return BAD_VALUE;
792 } else {
793 frameCount = minFrameCount;
794 }
795 }
796 } else {
797 // Ensure that buffer alignment matches channelcount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700798 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700799 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000800 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700801 return BAD_VALUE;
802 }
803 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
804 }
805 }
806
Eric Laurentbda74692009-11-04 08:27:26 -0800807 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
808 streamType,
809 sampleRate,
810 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700811 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800812 frameCount,
813 ((uint16_t)flags) << 16,
814 sharedBuffer,
815 output,
Eric Laurent65b65452010-06-01 23:49:17 -0700816 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800817 &status);
818
819 if (track == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000820 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurentbda74692009-11-04 08:27:26 -0800821 return status;
822 }
823 sp<IMemory> cblk = track->getCblk();
824 if (cblk == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000825 ALOGE("Could not get control block");
Eric Laurentbda74692009-11-04 08:27:26 -0800826 return NO_INIT;
827 }
Eric Laurentbda74692009-11-04 08:27:26 -0800828 mAudioTrack = track;
Eric Laurentbda74692009-11-04 08:27:26 -0800829 mCblkMemory = cblk;
830 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurentae29b762011-03-28 18:37:07 -0700831 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurentbda74692009-11-04 08:27:26 -0800832 if (sharedBuffer == 0) {
833 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
834 } else {
835 mCblk->buffers = sharedBuffer->pointer();
836 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700837 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800838 }
839
Glenn Kastenbc4de882012-01-17 14:39:34 -0800840 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten4790bd82012-01-03 14:22:33 -0800841 mCblk->setSendLevel(mSendLevel);
Eric Laurent7070b362010-07-16 07:43:46 -0700842 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent49f02be2009-11-19 09:00:56 -0800843 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
844 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700845 mRemainingFrames = mNotificationFramesAct;
846 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800847 return NO_ERROR;
848}
849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
851{
Eric Laurent421ddc02011-03-07 14:52:59 -0800852 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800853 bool active;
Glenn Kasten028ab992011-06-22 16:18:04 -0700854 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 audio_track_cblk_t* cblk = mCblk;
856 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700857 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858
859 audioBuffer->frameCount = 0;
860 audioBuffer->size = 0;
861
862 uint32_t framesAvail = cblk->framesAvailable();
863
Eric Laurent6667ac32011-03-21 11:49:00 -0700864 cblk->lock.lock();
865 if (cblk->flags & CBLK_INVALID_MSK) {
866 goto create_new_track;
867 }
868 cblk->lock.unlock();
869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800871 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 goto start_loop_here;
873 while (framesAvail == 0) {
874 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800875 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100876 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800877 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 return NO_MORE_BUFFERS;
879 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800880 if (CC_UNLIKELY(!waitCount)) {
Eric Laurentbda74692009-11-04 08:27:26 -0800881 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800883 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700884 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800885 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700886 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700887 cblk->lock.unlock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800888 mLock.lock();
Glenn Kastene6810ff2012-01-03 09:42:47 -0800889 if (!mActive) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800890 return status_t(STOPPED);
891 }
892 cblk->lock.lock();
893 }
894
895 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700896 goto create_new_track;
897 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800898 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurentef028272009-04-21 07:56:33 -0700899 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
901 // timing out when a loop has been set and we have already written upto loop end
902 // is a normal condition: no need to wake AudioFlinger up.
903 if (cblk->user < cblk->loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +0000904 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700906 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 cblk->lock.unlock();
Eric Laurentbda74692009-11-04 08:27:26 -0800908 result = mAudioTrack->start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800910 if (result == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700911 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -0800912create_new_track:
913 result = restoreTrack_l(cblk, false);
914 }
915 if (result != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000916 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -0800917 cblk->lock.unlock();
918 return result;
919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
921 cblk->waitTimeMs = 0;
922 }
Eric Laurenta553c252009-07-17 12:17:14 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800925 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 return TIMED_OUT;
927 }
928 }
929 // read the server count again
930 start_loop_here:
931 framesAvail = cblk->framesAvailable_l();
932 }
Eric Laurentbda74692009-11-04 08:27:26 -0800933 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 }
935
Eric Laurent4712baa2010-09-30 16:12:31 -0700936 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent421ddc02011-03-07 14:52:59 -0800937 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurentae29b762011-03-28 18:37:07 -0700938 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +0000939 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Eric Laurentae29b762011-03-28 18:37:07 -0700940 mAudioTrack->start();
Eric Laurent4712baa2010-09-30 16:12:31 -0700941 }
942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 if (framesReq > framesAvail) {
946 framesReq = framesAvail;
947 }
948
949 uint32_t u = cblk->user;
950 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
951
952 if (u + framesReq > bufferEnd) {
953 framesReq = bufferEnd - u;
954 }
955
Eric Laurenta553c252009-07-17 12:17:14 -0700956 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
957 audioBuffer->channelCount = mChannelCount;
958 audioBuffer->frameCount = framesReq;
959 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700960 if (audio_is_linear_pcm(mFormat)) {
961 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700962 } else {
963 audioBuffer->format = mFormat;
964 }
965 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 active = mActive;
967 return active ? status_t(NO_ERROR) : status_t(STOPPED);
968}
969
970void AudioTrack::releaseBuffer(Buffer* audioBuffer)
971{
Eric Laurent421ddc02011-03-07 14:52:59 -0800972 AutoMutex lock(mLock);
973 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974}
975
976// -------------------------------------------------------------------------
977
978ssize_t AudioTrack::write(const void* buffer, size_t userSize)
979{
980
981 if (mSharedBuffer != 0) return INVALID_OPERATION;
982
983 if (ssize_t(userSize) < 0) {
984 // sanity-check. user is most-likely passing an error code.
Steve Block3762c312012-01-06 19:20:56 +0000985 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 buffer, userSize, userSize);
987 return BAD_VALUE;
988 }
989
Steve Block71f2cf12011-10-20 11:56:00 +0100990 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991
Eric Laurent421ddc02011-03-07 14:52:59 -0800992 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
993 // while we are accessing the cblk
994 mLock.lock();
995 sp <IAudioTrack> audioTrack = mAudioTrack;
996 sp <IMemory> iMem = mCblkMemory;
997 mLock.unlock();
998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 ssize_t written = 0;
1000 const int8_t *src = (const int8_t *)buffer;
1001 Buffer audioBuffer;
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001002 size_t frameSz = frameSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003
1004 do {
Eric Laurent913af0b2011-03-17 09:36:51 -07001005 audioBuffer.frameCount = userSize/frameSz;
Eric Laurenta553c252009-07-17 12:17:14 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 // Calling obtainBuffer() with a negative wait count causes
1008 // an (almost) infinite wait time.
1009 status_t err = obtainBuffer(&audioBuffer, -1);
1010 if (err < 0) {
1011 // out of buffers, return #bytes written
1012 if (err == status_t(NO_MORE_BUFFERS))
1013 break;
1014 return ssize_t(err);
1015 }
1016
1017 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -07001018
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001019 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 // Divide capacity by 2 to take expansion into account
1021 toWrite = audioBuffer.size>>1;
Glenn Kastencc2302d2012-01-11 09:52:19 -08001022 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent28ad42b2009-08-04 10:42:26 -07001023 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 toWrite = audioBuffer.size;
1025 memcpy(audioBuffer.i8, src, toWrite);
1026 src += toWrite;
1027 }
1028 userSize -= toWrite;
1029 written += toWrite;
1030
1031 releaseBuffer(&audioBuffer);
Eric Laurent913af0b2011-03-17 09:36:51 -07001032 } while (userSize >= frameSz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033
1034 return written;
1035}
1036
1037// -------------------------------------------------------------------------
1038
1039bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1040{
1041 Buffer audioBuffer;
1042 uint32_t frames;
1043 size_t writtenSize;
1044
Eric Laurent421ddc02011-03-07 14:52:59 -08001045 mLock.lock();
1046 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1047 // while we are accessing the cblk
1048 sp <IAudioTrack> audioTrack = mAudioTrack;
1049 sp <IMemory> iMem = mCblkMemory;
1050 audio_track_cblk_t* cblk = mCblk;
Glenn Kastene6810ff2012-01-03 09:42:47 -08001051 bool active = mActive;
Eric Laurent421ddc02011-03-07 14:52:59 -08001052 mLock.unlock();
1053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 // Manage underrun callback
Glenn Kastene6810ff2012-01-03 09:42:47 -08001055 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block71f2cf12011-10-20 11:56:00 +01001056 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001057 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001059 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001060 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 if (mSharedBuffer != 0) return false;
1063 }
1064 }
Eric Laurenta553c252009-07-17 12:17:14 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001067 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 int loopCount = -1;
1069 mLoopCount--;
1070 if (mLoopCount >= 0) loopCount = mLoopCount;
1071
1072 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1073 }
1074
1075 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001076 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001077 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001079 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 }
1082
1083 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001084 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001085 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1087 mNewPosition += mUpdatePeriod;
1088 }
1089 }
1090
1091 // If Shared buffer is used, no data is requested from client.
1092 if (mSharedBuffer != 0) {
1093 frames = 0;
1094 } else {
1095 frames = mRemainingFrames;
1096 }
1097
Eric Laurentf1d360a2011-09-07 11:13:23 -07001098 int32_t waitCount = -1;
1099 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1100 waitCount = 1;
1101 }
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 do {
1104
1105 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001106
1107 // Calling obtainBuffer() with a wait count of 1
1108 // limits wait time to WAIT_PERIOD_MS. This prevents from being
1109 // stuck here not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001110 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 if (err < NO_ERROR) {
1112 if (err != TIMED_OUT) {
Steve Block3762c312012-01-06 19:20:56 +00001113 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 -08001114 return false;
1115 }
1116 break;
1117 }
1118 if (err == status_t(STOPPED)) return false;
1119
1120 // Divide buffer size by 2 to take into account the expansion
1121 // due to 8 to 16 bit conversion: the callback must fill only half
1122 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001123 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 audioBuffer.size >>= 1;
1125 }
1126
1127 size_t reqSize = audioBuffer.size;
1128 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1129 writtenSize = audioBuffer.size;
1130
1131 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001132 if (ssize_t(writtenSize) <= 0) {
1133 // The callback is done filling buffers
1134 // Keep this thread going to handle timed events and
1135 // still try to get more data in intervals of WAIT_PERIOD_MS
1136 // but don't just loop and block the CPU, so wait
1137 usleep(WAIT_PERIOD_MS*1000);
1138 break;
1139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 if (writtenSize > reqSize) writtenSize = reqSize;
1141
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001142 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kastencc2302d2012-01-11 09:52:19 -08001143 // 8 to 16 bit conversion, note that source and destination are the same address
1144 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 writtenSize <<= 1;
1146 }
1147
1148 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001149 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001150 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurenta553c252009-07-17 12:17:14 -07001151 // 16 bit.
1152 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 frames -= audioBuffer.frameCount;
1155
1156 releaseBuffer(&audioBuffer);
1157 }
1158 while (frames);
1159
1160 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001161 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 } else {
1163 mRemainingFrames = frames;
1164 }
1165 return true;
1166}
1167
Eric Laurent421ddc02011-03-07 14:52:59 -08001168// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1169// the IAudioTrack and IMemory in case they are recreated here.
1170// If the IAudioTrack is successfully restored, the cblk pointer is updated
1171status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1172{
1173 status_t result;
1174
Eric Laurentae29b762011-03-28 18:37:07 -07001175 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001176 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Eric Laurent7e8626f2011-09-13 15:04:17 -07001177 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001178
Eric Laurent421ddc02011-03-07 14:52:59 -08001179 // signal old cblk condition so that other threads waiting for available buffers stop
1180 // waiting now
1181 cblk->cv.broadcast();
1182 cblk->lock.unlock();
1183
Eric Laurent05ce0942011-08-30 10:18:54 -07001184 // refresh the audio configuration cache in this process to make sure we get new
1185 // output parameters in getOutput_l() and createTrack_l()
1186 AudioSystem::clearAudioConfigCache();
1187
Eric Laurent421ddc02011-03-07 14:52:59 -08001188 // if the new IAudioTrack is created, createTrack_l() will modify the
1189 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1190 // It will also delete the strong references on previous IAudioTrack and IMemory
1191 result = createTrack_l(mStreamType,
1192 cblk->sampleRate,
1193 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001194 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001195 mFrameCount,
1196 mFlags,
1197 mSharedBuffer,
1198 getOutput_l(),
1199 false);
1200
1201 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001202 uint32_t user = cblk->user;
1203 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001204 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001205 mCblk->user = user;
1206 mCblk->server = user;
1207 mCblk->userBase = user;
1208 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001209 // restore loop: this is not guaranteed to succeed if new frame count is not
1210 // compatible with loop length
1211 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001212 if (!fromStart) {
1213 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001214 // Make sure that a client relying on callback events indicating underrun or
1215 // the actual amount of audio frames played (e.g SoundPool) receives them.
1216 if (mSharedBuffer == 0) {
1217 uint32_t frames = 0;
1218 if (user > server) {
1219 frames = ((user - server) > mCblk->frameCount) ?
1220 mCblk->frameCount : (user - server);
1221 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1222 }
1223 // restart playback even if buffer is not completely filled.
1224 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1225 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1226 // the client
1227 mCblk->stepUser(frames);
1228 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001229 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001230 if (mActive) {
1231 result = mAudioTrack->start();
Steve Block8564c8d2012-01-05 23:22:43 +00001232 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent6667ac32011-03-21 11:49:00 -07001233 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001234 if (fromStart && result == NO_ERROR) {
1235 mNewPosition = mCblk->server + mUpdatePeriod;
1236 }
1237 }
1238 if (result != NO_ERROR) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001239 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +00001240 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -08001241 }
Eric Laurent7e8626f2011-09-13 15:04:17 -07001242 mRestoreStatus = result;
Eric Laurent421ddc02011-03-07 14:52:59 -08001243 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001244 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001245 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001246 } else {
1247 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001248 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001249 mLock.unlock();
1250 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurent7e8626f2011-09-13 15:04:17 -07001251 if (result == NO_ERROR) {
1252 result = mRestoreStatus;
1253 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001254 cblk->lock.unlock();
1255 mLock.lock();
1256 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001257 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurent7e8626f2011-09-13 15:04:17 -07001258 result = mRestoreStatus;
Eric Laurent421ddc02011-03-07 14:52:59 -08001259 cblk->lock.unlock();
1260 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001261 }
Steve Block71f2cf12011-10-20 11:56:00 +01001262 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Eric Laurent421ddc02011-03-07 14:52:59 -08001263 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1264
1265 if (result == NO_ERROR) {
1266 // from now on we switch to the newly created cblk
1267 cblk = mCblk;
1268 }
1269 cblk->lock.lock();
1270
Steve Block8564c8d2012-01-05 23:22:43 +00001271 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001272
1273 return result;
1274}
1275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1277{
1278
1279 const size_t SIZE = 256;
1280 char buffer[SIZE];
1281 String8 result;
1282
1283 result.append(" AudioTrack::dump\n");
1284 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1285 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001286 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 -08001287 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001288 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 -08001289 result.append(buffer);
1290 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1291 result.append(buffer);
1292 ::write(fd, result.string(), result.size());
1293 return NO_ERROR;
1294}
1295
1296// =========================================================================
1297
1298AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1299 : Thread(bCanCallJava), mReceiver(receiver)
1300{
1301}
1302
1303bool AudioTrack::AudioTrackThread::threadLoop()
1304{
1305 return mReceiver.processAudioBuffer(this);
1306}
1307
1308status_t AudioTrack::AudioTrackThread::readyToRun()
1309{
1310 return NO_ERROR;
1311}
1312
1313void AudioTrack::AudioTrackThread::onFirstRef()
1314{
1315}
1316
1317// =========================================================================
1318
Eric Laurentae29b762011-03-28 18:37:07 -07001319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001321 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kasten3694ec12012-01-27 16:47:15 -08001322 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kastenbc4de882012-01-17 14:39:34 -08001323 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten4790bd82012-01-03 14:22:33 -08001324 mSendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325{
1326}
1327
1328uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1329{
Glenn Kastendb298a42011-12-13 11:45:07 -08001330 uint32_t u = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 u += frameCount;
1333 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001334 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1336 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1337 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1338 }
Glenn Kastendb298a42011-12-13 11:45:07 -08001339 } else if (u > server) {
Steve Block8564c8d2012-01-05 23:22:43 +00001340 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001341 u = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343
1344 if (u >= userBase + this->frameCount) {
1345 userBase += this->frameCount;
1346 }
1347
Glenn Kastendb298a42011-12-13 11:45:07 -08001348 user = u;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
1350 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001351 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001352 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354
1355 return u;
1356}
1357
1358bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1359{
Eric Laurentae29b762011-03-28 18:37:07 -07001360 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001361 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 return false;
1363 }
1364
Glenn Kastendb298a42011-12-13 11:45:07 -08001365 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366
1367 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001368 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 // Mark that we have read the first buffer so that next time stepUser() is called
1370 // we switch to normal obtainBuffer() timeout period
1371 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001372 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 // It is possible that we receive a flush()
1375 // while the mixer is processing a block: in this case,
1376 // stepServer() is called After the flush() has reset u & s and
1377 // we have s > u
Glenn Kastendb298a42011-12-13 11:45:07 -08001378 if (s > user) {
Steve Block8564c8d2012-01-05 23:22:43 +00001379 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001380 s = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 }
1382 }
1383
1384 if (s >= loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +00001385 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 s = loopStart;
1387 if (--loopCount == 0) {
1388 loopEnd = UINT_MAX;
1389 loopStart = UINT_MAX;
1390 }
1391 }
1392 if (s >= serverBase + this->frameCount) {
1393 serverBase += this->frameCount;
1394 }
1395
Glenn Kastendb298a42011-12-13 11:45:07 -08001396 server = s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397
Eric Laurent421ddc02011-03-07 14:52:59 -08001398 if (!(flags & CBLK_INVALID_MSK)) {
1399 cv.signal();
1400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 lock.unlock();
1402 return true;
1403}
1404
1405void* audio_track_cblk_t::buffer(uint32_t offset) const
1406{
Glenn Kastendb298a42011-12-13 11:45:07 -08001407 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408}
1409
1410uint32_t audio_track_cblk_t::framesAvailable()
1411{
1412 Mutex::Autolock _l(lock);
1413 return framesAvailable_l();
1414}
1415
1416uint32_t audio_track_cblk_t::framesAvailable_l()
1417{
Glenn Kastendb298a42011-12-13 11:45:07 -08001418 uint32_t u = user;
1419 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001421 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 uint32_t limit = (s < loopStart) ? s : loopStart;
1423 return limit + frameCount - u;
1424 } else {
1425 return frameCount + u - s;
1426 }
1427}
1428
1429uint32_t audio_track_cblk_t::framesReady()
1430{
Glenn Kastendb298a42011-12-13 11:45:07 -08001431 uint32_t u = user;
1432 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001434 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (u < loopEnd) {
1436 return u - s;
1437 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001438 // do not block on mutex shared with client on AudioFlinger side
1439 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001440 ALOGW("framesReady() could not lock cblk");
Eric Laurentae29b762011-03-28 18:37:07 -07001441 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
Eric Laurentae29b762011-03-28 18:37:07 -07001443 uint32_t frames = UINT_MAX;
1444 if (loopCount >= 0) {
1445 frames = (loopEnd - loopStart)*loopCount + u - s;
1446 }
1447 lock.unlock();
1448 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450 } else {
1451 return s - u;
1452 }
1453}
1454
Eric Laurentae29b762011-03-28 18:37:07 -07001455bool audio_track_cblk_t::tryLock()
1456{
1457 // the code below simulates lock-with-timeout
1458 // we MUST do this to protect the AudioFlinger server
1459 // as this lock is shared with the client.
1460 status_t err;
1461
1462 err = lock.tryLock();
1463 if (err == -EBUSY) { // just wait a bit
1464 usleep(1000);
1465 err = lock.tryLock();
1466 }
1467 if (err != NO_ERROR) {
1468 // probably, the client just died.
1469 return false;
1470 }
1471 return true;
1472}
1473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474// -------------------------------------------------------------------------
1475
1476}; // namespace android