blob: a1c99e5a0dec55acc1deec894999347529299793 [file] [log] [blame]
Glenn Kastenb3db2132012-01-19 08:59:58 -08001/*
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),
John Grossmand8cf2962012-02-08 16:37:41 -080083 mIsTimed(false),
84 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
85 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086{
87}
88
89AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -080090 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -080092 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -070093 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 int frameCount,
95 uint32_t flags,
96 callback_t cbf,
97 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -070098 int notificationFrames,
99 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -0700100 : mStatus(NO_INIT),
John Grossmand8cf2962012-02-08 16:37:41 -0800101 mIsTimed(false),
102 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
103 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700105 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700106 frameCount, flags, cbf, user, notificationFrames,
107 0, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108}
109
110AudioTrack::AudioTrack(
Andreas Huber28ea0132012-01-18 10:51:55 -0800111 int streamType,
112 uint32_t sampleRate,
113 int format,
114 int channelMask,
115 int frameCount,
116 uint32_t flags,
117 callback_t cbf,
118 void* user,
119 int notificationFrames,
120 int sessionId)
121 : mStatus(NO_INIT),
122 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
123{
124 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
125 frameCount, flags, cbf, user, notificationFrames,
126 0, false, sessionId);
127}
128
129AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800130 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800132 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700133 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 const sp<IMemory>& sharedBuffer,
135 uint32_t flags,
136 callback_t cbf,
137 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -0700138 int notificationFrames,
139 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -0700140 : mStatus(NO_INIT),
John Grossmand8cf2962012-02-08 16:37:41 -0800141 mIsTimed(false),
142 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
143 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700145 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700146 0, flags, cbf, user, notificationFrames,
147 sharedBuffer, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148}
149
150AudioTrack::~AudioTrack()
151{
Steve Block71f2cf12011-10-20 11:56:00 +0100152 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 if (mStatus == NO_ERROR) {
155 // Make sure that callback function exits in the case where
156 // it is looping on buffer full condition in obtainBuffer().
157 // Otherwise the callback thread will never exit.
158 stop();
159 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 mAudioTrackThread->requestExitAndWait();
161 mAudioTrackThread.clear();
162 }
163 mAudioTrack.clear();
164 IPCThreadState::self()->flushCommands();
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700165 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
167}
168
169status_t AudioTrack::set(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800170 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800172 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700173 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 int frameCount,
175 uint32_t flags,
176 callback_t cbf,
177 void* user,
178 int notificationFrames,
179 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700180 bool threadCanCallJava,
181 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182{
183
Steve Block71f2cf12011-10-20 11:56:00 +0100184 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Eric Laurent421ddc02011-03-07 14:52:59 -0800186 AutoMutex lock(mLock);
Eric Laurentef028272009-04-21 07:56:33 -0700187 if (mAudioTrack != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000188 ALOGE("Track already in use");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 return INVALID_OPERATION;
190 }
191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 int afSampleRate;
193 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
194 return NO_INIT;
195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 uint32_t afLatency;
197 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
198 return NO_INIT;
199 }
200
201 // handle default values first.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700202 if (streamType == AUDIO_STREAM_DEFAULT) {
203 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205 if (sampleRate == 0) {
206 sampleRate = afSampleRate;
207 }
208 // these below should probably come from the audioFlinger too...
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800209 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700210 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700212 if (channelMask == 0) {
213 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
215
216 // validate parameters
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700217 if (!audio_is_valid_format(format)) {
Steve Block3762c312012-01-06 19:20:56 +0000218 ALOGE("Invalid format");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 return BAD_VALUE;
220 }
Eric Laurenta553c252009-07-17 12:17:14 -0700221
222 // force direct flag if format is not linear PCM
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700223 if (!audio_is_linear_pcm(format)) {
224 flags |= AUDIO_POLICY_OUTPUT_FLAG_DIRECT;
Eric Laurenta553c252009-07-17 12:17:14 -0700225 }
226
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700227 if (!audio_is_output_channel(channelMask)) {
Steve Block3762c312012-01-06 19:20:56 +0000228 ALOGE("Invalid channel mask");
Eric Laurenta553c252009-07-17 12:17:14 -0700229 return BAD_VALUE;
230 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700231 uint32_t channelCount = popcount(channelMask);
Eric Laurenta553c252009-07-17 12:17:14 -0700232
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700233 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800234 streamType,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800235 sampleRate, format, channelMask,
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700236 (audio_policy_output_flags_t)flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700237
238 if (output == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000239 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 return BAD_VALUE;
241 }
242
Eric Laurentbda74692009-11-04 08:27:26 -0800243 mVolume[LEFT] = 1.0f;
244 mVolume[RIGHT] = 1.0f;
Glenn Kasten4790bd82012-01-03 14:22:33 -0800245 mSendLevel = 0.0f;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700246 mFrameCount = frameCount;
247 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700248 mSessionId = sessionId;
Eric Laurent7070b362010-07-16 07:43:46 -0700249 mAuxEffectId = 0;
Eric Laurent65b65452010-06-01 23:49:17 -0700250
Eric Laurentbda74692009-11-04 08:27:26 -0800251 // create the IAudioTrack
Eric Laurent421ddc02011-03-07 14:52:59 -0800252 status_t status = createTrack_l(streamType,
253 sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800254 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700255 (uint32_t)channelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -0800256 frameCount,
257 flags,
258 sharedBuffer,
259 output,
260 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
Eric Laurentbda74692009-11-04 08:27:26 -0800262 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 return status;
264 }
Eric Laurentbda74692009-11-04 08:27:26 -0800265
Glenn Kasten3694ec12012-01-27 16:47:15 -0800266 if (cbf != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 }
269
270 mStatus = NO_ERROR;
271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mStreamType = streamType;
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800273 mFormat = format;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700274 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mChannelCount = channelCount;
276 mSharedBuffer = sharedBuffer;
277 mMuted = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800278 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 mLoopCount = 0;
282 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700283 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 mNewPosition = 0;
285 mUpdatePeriod = 0;
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700286 mFlushed = false;
Eric Laurenta553c252009-07-17 12:17:14 -0700287 mFlags = flags;
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700288 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurent7e8626f2011-09-13 15:04:17 -0700289 mRestoreStatus = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 return NO_ERROR;
291}
292
293status_t AudioTrack::initCheck() const
294{
295 return mStatus;
296}
297
298// -------------------------------------------------------------------------
299
300uint32_t AudioTrack::latency() const
301{
302 return mLatency;
303}
304
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800305audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306{
307 return mStreamType;
308}
309
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800310audio_format_t AudioTrack::format() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311{
312 return mFormat;
313}
314
315int AudioTrack::channelCount() const
316{
317 return mChannelCount;
318}
319
320uint32_t AudioTrack::frameCount() const
321{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700322 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323}
324
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800325size_t AudioTrack::frameSize() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700327 if (audio_is_linear_pcm(mFormat)) {
Eric Laurentc310dcb2011-06-16 21:30:45 -0700328 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurenta553c252009-07-17 12:17:14 -0700329 } else {
330 return sizeof(uint8_t);
331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332}
333
334sp<IMemory>& AudioTrack::sharedBuffer()
335{
336 return mSharedBuffer;
337}
338
339// -------------------------------------------------------------------------
340
341void AudioTrack::start()
342{
343 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten028ab992011-06-22 16:18:04 -0700344 status_t status = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345
Steve Block71f2cf12011-10-20 11:56:00 +0100346 ALOGV("start %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 if (t != 0) {
348 if (t->exitPending()) {
349 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block3762c312012-01-06 19:20:56 +0000350 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 return;
352 }
353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 }
355
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800356 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800357 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
358 // while we are accessing the cblk
359 sp <IAudioTrack> audioTrack = mAudioTrack;
360 sp <IMemory> iMem = mCblkMemory;
361 audio_track_cblk_t* cblk = mCblk;
362
Glenn Kastene6810ff2012-01-03 09:42:47 -0800363 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700364 mFlushed = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800365 mActive = true;
Eric Laurent421ddc02011-03-07 14:52:59 -0800366 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700367 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800368 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
369 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700370 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800371 pid_t tid;
Eric Laurent059b4be2009-11-09 23:32:22 -0800372 if (t != 0) {
Glenn Kasten86e33622012-02-28 12:30:08 -0800373 t->run("AudioTrack", ANDROID_PRIORITY_AUDIO);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800374 tid = t->getTid(); // pid_t is unknown until run()
375 ALOGV("getTid=%d", tid);
376 if (tid == -1) {
377 tid = 0;
378 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800379 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700380 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
381 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
382 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800383 tid = 0; // not gettid()
Eric Laurent059b4be2009-11-09 23:32:22 -0800384 }
385
Steve Block71f2cf12011-10-20 11:56:00 +0100386 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800387 if (!(cblk->flags & CBLK_INVALID_MSK)) {
388 cblk->lock.unlock();
Glenn Kasten6a20b262012-02-02 10:56:47 -0800389 ALOGV("mAudioTrack->start(tid=%d)", tid);
390 status = mAudioTrack->start(tid);
Eric Laurent421ddc02011-03-07 14:52:59 -0800391 cblk->lock.lock();
392 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700393 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800394 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800395 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800396 if (cblk->flags & CBLK_INVALID_MSK) {
397 status = restoreTrack_l(cblk, true);
398 }
399 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800400 if (status != NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100401 ALOGV("start() failed");
Glenn Kastene6810ff2012-01-03 09:42:47 -0800402 mActive = false;
Eric Laurent059b4be2009-11-09 23:32:22 -0800403 if (t != 0) {
404 t->requestExit();
405 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700406 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
407 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent059b4be2009-11-09 23:32:22 -0800408 }
Eric Laurentbda74692009-11-04 08:27:26 -0800409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 }
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412}
413
414void AudioTrack::stop()
415{
416 sp<AudioTrackThread> t = mAudioTrackThread;
417
Steve Block71f2cf12011-10-20 11:56:00 +0100418 ALOGV("stop %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800420 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800421 if (mActive) {
422 mActive = false;
Eric Laurentef028272009-04-21 07:56:33 -0700423 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 mAudioTrack->stop();
425 // Cancel loops (If we are in the middle of a loop, playback
426 // would not stop until loopCount reaches 0).
Eric Laurent421ddc02011-03-07 14:52:59 -0800427 setLoop_l(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700428 // the playback head position will reset to 0, so if a marker is set, we need
429 // to activate it again
430 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 // Force flush if a shared buffer is used otherwise audioflinger
432 // will not stop before end of buffer is reached.
433 if (mSharedBuffer != 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800434 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436 if (t != 0) {
437 t->requestExit();
438 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700439 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
440 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442 }
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444}
445
446bool AudioTrack::stopped() const
447{
Glenn Kastene6810ff2012-01-03 09:42:47 -0800448 AutoMutex lock(mLock);
449 return stopped_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450}
451
452void AudioTrack::flush()
453{
Eric Laurent421ddc02011-03-07 14:52:59 -0800454 AutoMutex lock(mLock);
455 flush_l();
456}
457
458// must be called with mLock held
459void AudioTrack::flush_l()
460{
Steve Block71f2cf12011-10-20 11:56:00 +0100461 ALOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700462
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700463 // clear playback marker and periodic update counter
464 mMarkerPosition = 0;
465 mMarkerReached = false;
466 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700469 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mAudioTrack->flush();
471 // Release AudioTrack callback thread in case it was waiting for new buffers
472 // in AudioTrack::obtainBuffer()
473 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
475}
476
477void AudioTrack::pause()
478{
Steve Block71f2cf12011-10-20 11:56:00 +0100479 ALOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800480 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800481 if (mActive) {
482 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 mAudioTrack->pause();
484 }
485}
486
487void AudioTrack::mute(bool e)
488{
489 mAudioTrack->mute(e);
490 mMuted = e;
491}
492
493bool AudioTrack::muted() const
494{
495 return mMuted;
496}
497
Eric Laurent65b65452010-06-01 23:49:17 -0700498status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499{
Glenn Kasten1c50a782011-11-30 09:46:04 -0800500 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700501 return BAD_VALUE;
502 }
503
Eric Laurent421ddc02011-03-07 14:52:59 -0800504 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 mVolume[LEFT] = left;
506 mVolume[RIGHT] = right;
507
Glenn Kastenbc4de882012-01-17 14:39:34 -0800508 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurent65b65452010-06-01 23:49:17 -0700509
510 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511}
512
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800513void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514{
Eric Laurent65b65452010-06-01 23:49:17 -0700515 if (left != NULL) {
516 *left = mVolume[LEFT];
517 }
518 if (right != NULL) {
519 *right = mVolume[RIGHT];
520 }
521}
522
Eric Laurent7070b362010-07-16 07:43:46 -0700523status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700524{
Steve Block71f2cf12011-10-20 11:56:00 +0100525 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800526 if (level < 0.0f || level > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700527 return BAD_VALUE;
528 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800529 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700530
531 mSendLevel = level;
532
Glenn Kasten4790bd82012-01-03 14:22:33 -0800533 mCblk->setSendLevel(level);
Eric Laurent65b65452010-06-01 23:49:17 -0700534
535 return NO_ERROR;
536}
537
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800538void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurent65b65452010-06-01 23:49:17 -0700539{
540 if (level != NULL) {
541 *level = mSendLevel;
542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543}
544
Eric Laurent88e209d2009-07-07 07:10:45 -0700545status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546{
547 int afSamplingRate;
548
John Grossmand8cf2962012-02-08 16:37:41 -0800549 if (mIsTimed) {
550 return INVALID_OPERATION;
551 }
552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700554 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700557 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558
Eric Laurent421ddc02011-03-07 14:52:59 -0800559 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700560 mCblk->sampleRate = rate;
561 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562}
563
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800564uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565{
John Grossmand8cf2962012-02-08 16:37:41 -0800566 if (mIsTimed) {
567 return INVALID_OPERATION;
568 }
569
Eric Laurent421ddc02011-03-07 14:52:59 -0800570 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700571 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572}
573
574status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
575{
Eric Laurent421ddc02011-03-07 14:52:59 -0800576 AutoMutex lock(mLock);
577 return setLoop_l(loopStart, loopEnd, loopCount);
578}
579
580// must be called with mLock held
581status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
582{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 audio_track_cblk_t* cblk = mCblk;
584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 Mutex::Autolock _l(cblk->lock);
586
587 if (loopCount == 0) {
588 cblk->loopStart = UINT_MAX;
589 cblk->loopEnd = UINT_MAX;
590 cblk->loopCount = 0;
591 mLoopCount = 0;
592 return NO_ERROR;
593 }
594
John Grossmand8cf2962012-02-08 16:37:41 -0800595 if (mIsTimed) {
596 return INVALID_OPERATION;
597 }
598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700600 loopEnd - loopStart > cblk->frameCount ||
601 cblk->server > loopStart) {
Steve Block3762c312012-01-06 19:20:56 +0000602 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 -0800603 return BAD_VALUE;
604 }
605
Eric Laurent6667ac32011-03-21 11:49:00 -0700606 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000607 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700608 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611
612 cblk->loopStart = loopStart;
613 cblk->loopEnd = loopEnd;
614 cblk->loopCount = loopCount;
615 mLoopCount = loopCount;
616
617 return NO_ERROR;
618}
619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620status_t AudioTrack::setMarkerPosition(uint32_t marker)
621{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800622 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700625 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626
627 return NO_ERROR;
628}
629
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800630status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800632 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633
634 *marker = mMarkerPosition;
635
636 return NO_ERROR;
637}
638
639status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
640{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800641 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642
643 uint32_t curPosition;
644 getPosition(&curPosition);
645 mNewPosition = curPosition + updatePeriod;
646 mUpdatePeriod = updatePeriod;
647
648 return NO_ERROR;
649}
650
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800651status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800653 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654
655 *updatePeriod = mUpdatePeriod;
656
657 return NO_ERROR;
658}
659
660status_t AudioTrack::setPosition(uint32_t position)
661{
John Grossmand8cf2962012-02-08 16:37:41 -0800662 if (mIsTimed) return INVALID_OPERATION;
663
Eric Laurent421ddc02011-03-07 14:52:59 -0800664 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665
Glenn Kastene6810ff2012-01-03 09:42:47 -0800666 if (!stopped_l()) return INVALID_OPERATION;
667
668 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
670 if (position > mCblk->user) return BAD_VALUE;
671
672 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700673 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 return NO_ERROR;
676}
677
678status_t AudioTrack::getPosition(uint32_t *position)
679{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800680 if (position == NULL) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800681 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700682 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
684 return NO_ERROR;
685}
686
687status_t AudioTrack::reload()
688{
Eric Laurent421ddc02011-03-07 14:52:59 -0800689 AutoMutex lock(mLock);
690
Glenn Kastene6810ff2012-01-03 09:42:47 -0800691 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700692
Eric Laurent421ddc02011-03-07 14:52:59 -0800693 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700695 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
697 return NO_ERROR;
698}
699
Eric Laurenta553c252009-07-17 12:17:14 -0700700audio_io_handle_t AudioTrack::getOutput()
701{
Eric Laurent421ddc02011-03-07 14:52:59 -0800702 AutoMutex lock(mLock);
703 return getOutput_l();
704}
705
706// must be called with mLock held
707audio_io_handle_t AudioTrack::getOutput_l()
708{
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800709 return AudioSystem::getOutput(mStreamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700710 mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700711}
712
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800713int AudioTrack::getSessionId() const
Eric Laurent65b65452010-06-01 23:49:17 -0700714{
715 return mSessionId;
716}
717
718status_t AudioTrack::attachAuxEffect(int effectId)
719{
Steve Block71f2cf12011-10-20 11:56:00 +0100720 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700721 status_t status = mAudioTrack->attachAuxEffect(effectId);
722 if (status == NO_ERROR) {
723 mAuxEffectId = effectId;
724 }
725 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700726}
727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728// -------------------------------------------------------------------------
729
Eric Laurent421ddc02011-03-07 14:52:59 -0800730// must be called with mLock held
731status_t AudioTrack::createTrack_l(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800732 audio_stream_type_t streamType,
Eric Laurentbda74692009-11-04 08:27:26 -0800733 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800734 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700735 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800736 int frameCount,
737 uint32_t flags,
738 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700739 audio_io_handle_t output,
740 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800741{
742 status_t status;
743 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
744 if (audioFlinger == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000745 ALOGE("Could not get audioflinger");
Eric Laurentbda74692009-11-04 08:27:26 -0800746 return NO_INIT;
747 }
748
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700749 int afSampleRate;
750 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
751 return NO_INIT;
752 }
753 int afFrameCount;
754 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
755 return NO_INIT;
756 }
757 uint32_t afLatency;
758 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
759 return NO_INIT;
760 }
761
762 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700763 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700764 if (sharedBuffer != 0) {
765 frameCount = sharedBuffer->size();
766 }
767 } else {
768 // Ensure that buffer depth covers at least audio hardware latency
769 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
770 if (minBufCount < 2) minBufCount = 2;
771
772 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
773
774 if (sharedBuffer == 0) {
775 if (frameCount == 0) {
776 frameCount = minFrameCount;
777 }
778 if (mNotificationFramesAct == 0) {
779 mNotificationFramesAct = frameCount/2;
780 }
781 // Make sure that application is notified with sufficient margin
782 // before underrun
783 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
784 mNotificationFramesAct = frameCount/2;
785 }
786 if (frameCount < minFrameCount) {
Eric Laurentbd6b74c2012-03-05 17:06:40 -0800787 ALOGW_IF(enforceFrameCount, "Minimum buffer size corrected from %d to %d",
788 frameCount, minFrameCount);
789 frameCount = minFrameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700790 }
791 } else {
Glenn Kastenb3db2132012-01-19 08:59:58 -0800792 // Ensure that buffer alignment matches channelCount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700793 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700794 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000795 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700796 return BAD_VALUE;
797 }
798 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
799 }
800 }
801
Eric Laurentbda74692009-11-04 08:27:26 -0800802 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
803 streamType,
804 sampleRate,
805 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700806 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800807 frameCount,
808 ((uint16_t)flags) << 16,
809 sharedBuffer,
810 output,
John Grossmand8cf2962012-02-08 16:37:41 -0800811 mIsTimed,
Eric Laurent65b65452010-06-01 23:49:17 -0700812 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800813 &status);
814
815 if (track == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000816 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurentbda74692009-11-04 08:27:26 -0800817 return status;
818 }
819 sp<IMemory> cblk = track->getCblk();
820 if (cblk == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000821 ALOGE("Could not get control block");
Eric Laurentbda74692009-11-04 08:27:26 -0800822 return NO_INIT;
823 }
Eric Laurentbda74692009-11-04 08:27:26 -0800824 mAudioTrack = track;
Eric Laurentbda74692009-11-04 08:27:26 -0800825 mCblkMemory = cblk;
826 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurentae29b762011-03-28 18:37:07 -0700827 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurentbda74692009-11-04 08:27:26 -0800828 if (sharedBuffer == 0) {
829 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
830 } else {
831 mCblk->buffers = sharedBuffer->pointer();
832 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700833 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800834 }
835
Glenn Kastenbc4de882012-01-17 14:39:34 -0800836 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten4790bd82012-01-03 14:22:33 -0800837 mCblk->setSendLevel(mSendLevel);
Eric Laurent7070b362010-07-16 07:43:46 -0700838 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent49f02be2009-11-19 09:00:56 -0800839 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
840 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700841 mRemainingFrames = mNotificationFramesAct;
842 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800843 return NO_ERROR;
844}
845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
847{
Eric Laurent421ddc02011-03-07 14:52:59 -0800848 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800849 bool active;
Glenn Kasten028ab992011-06-22 16:18:04 -0700850 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 audio_track_cblk_t* cblk = mCblk;
852 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700853 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854
855 audioBuffer->frameCount = 0;
856 audioBuffer->size = 0;
857
858 uint32_t framesAvail = cblk->framesAvailable();
859
Eric Laurent6667ac32011-03-21 11:49:00 -0700860 cblk->lock.lock();
861 if (cblk->flags & CBLK_INVALID_MSK) {
862 goto create_new_track;
863 }
864 cblk->lock.unlock();
865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800867 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 goto start_loop_here;
869 while (framesAvail == 0) {
870 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800871 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100872 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800873 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 return NO_MORE_BUFFERS;
875 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800876 if (CC_UNLIKELY(!waitCount)) {
Eric Laurentbda74692009-11-04 08:27:26 -0800877 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800879 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700880 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800881 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700882 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700883 cblk->lock.unlock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800884 mLock.lock();
Glenn Kastene6810ff2012-01-03 09:42:47 -0800885 if (!mActive) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800886 return status_t(STOPPED);
887 }
888 cblk->lock.lock();
889 }
890
891 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700892 goto create_new_track;
893 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800894 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurentef028272009-04-21 07:56:33 -0700895 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
897 // timing out when a loop has been set and we have already written upto loop end
898 // is a normal condition: no need to wake AudioFlinger up.
899 if (cblk->user < cblk->loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +0000900 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700902 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 cblk->lock.unlock();
Glenn Kasten6a20b262012-02-02 10:56:47 -0800904 result = mAudioTrack->start(0); // callback thread hasn't changed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800906 if (result == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700907 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -0800908create_new_track:
909 result = restoreTrack_l(cblk, false);
910 }
911 if (result != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000912 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -0800913 cblk->lock.unlock();
914 return result;
915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
917 cblk->waitTimeMs = 0;
918 }
Eric Laurenta553c252009-07-17 12:17:14 -0700919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800921 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 return TIMED_OUT;
923 }
924 }
925 // read the server count again
926 start_loop_here:
927 framesAvail = cblk->framesAvailable_l();
928 }
Eric Laurentbda74692009-11-04 08:27:26 -0800929 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
931
Eric Laurent4712baa2010-09-30 16:12:31 -0700932 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent421ddc02011-03-07 14:52:59 -0800933 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurentae29b762011-03-28 18:37:07 -0700934 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +0000935 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800936 mAudioTrack->start(0); // callback thread hasn't changed
Eric Laurent4712baa2010-09-30 16:12:31 -0700937 }
938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 if (framesReq > framesAvail) {
942 framesReq = framesAvail;
943 }
944
945 uint32_t u = cblk->user;
946 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
947
948 if (u + framesReq > bufferEnd) {
949 framesReq = bufferEnd - u;
950 }
951
Eric Laurenta553c252009-07-17 12:17:14 -0700952 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
953 audioBuffer->channelCount = mChannelCount;
954 audioBuffer->frameCount = framesReq;
955 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700956 if (audio_is_linear_pcm(mFormat)) {
957 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700958 } else {
959 audioBuffer->format = mFormat;
960 }
961 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 active = mActive;
963 return active ? status_t(NO_ERROR) : status_t(STOPPED);
964}
965
966void AudioTrack::releaseBuffer(Buffer* audioBuffer)
967{
Eric Laurent421ddc02011-03-07 14:52:59 -0800968 AutoMutex lock(mLock);
969 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970}
971
972// -------------------------------------------------------------------------
973
974ssize_t AudioTrack::write(const void* buffer, size_t userSize)
975{
976
977 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossmand8cf2962012-02-08 16:37:41 -0800978 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979
980 if (ssize_t(userSize) < 0) {
Glenn Kastenb3db2132012-01-19 08:59:58 -0800981 // Sanity-check: user is most-likely passing an error code, and it would
982 // make the return value ambiguous (actualSize vs error).
Steve Block3762c312012-01-06 19:20:56 +0000983 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 buffer, userSize, userSize);
985 return BAD_VALUE;
986 }
987
Steve Block71f2cf12011-10-20 11:56:00 +0100988 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989
Eric Laurent421ddc02011-03-07 14:52:59 -0800990 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
991 // while we are accessing the cblk
992 mLock.lock();
993 sp <IAudioTrack> audioTrack = mAudioTrack;
994 sp <IMemory> iMem = mCblkMemory;
995 mLock.unlock();
996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 ssize_t written = 0;
998 const int8_t *src = (const int8_t *)buffer;
999 Buffer audioBuffer;
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001000 size_t frameSz = frameSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001
1002 do {
Eric Laurent913af0b2011-03-17 09:36:51 -07001003 audioBuffer.frameCount = userSize/frameSz;
Eric Laurenta553c252009-07-17 12:17:14 -07001004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 status_t err = obtainBuffer(&audioBuffer, -1);
1006 if (err < 0) {
1007 // out of buffers, return #bytes written
1008 if (err == status_t(NO_MORE_BUFFERS))
1009 break;
1010 return ssize_t(err);
1011 }
1012
1013 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -07001014
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001015 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 // Divide capacity by 2 to take expansion into account
1017 toWrite = audioBuffer.size>>1;
Glenn Kastencc2302d2012-01-11 09:52:19 -08001018 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent28ad42b2009-08-04 10:42:26 -07001019 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 toWrite = audioBuffer.size;
1021 memcpy(audioBuffer.i8, src, toWrite);
1022 src += toWrite;
1023 }
1024 userSize -= toWrite;
1025 written += toWrite;
1026
1027 releaseBuffer(&audioBuffer);
Eric Laurent913af0b2011-03-17 09:36:51 -07001028 } while (userSize >= frameSz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
1030 return written;
1031}
1032
1033// -------------------------------------------------------------------------
1034
John Grossmand8cf2962012-02-08 16:37:41 -08001035TimedAudioTrack::TimedAudioTrack() {
1036 mIsTimed = true;
1037}
1038
1039status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1040{
1041 status_t result = UNKNOWN_ERROR;
1042
1043 // If the track is not invalid already, try to allocate a buffer. alloc
1044 // fails indicating that the server is dead, flag the track as invalid so
1045 // we can attempt to restore in in just a bit.
1046 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1047 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1048 if (result == DEAD_OBJECT) {
1049 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1050 }
1051 }
1052
1053 // If the track is invalid at this point, attempt to restore it. and try the
1054 // allocation one more time.
1055 if (mCblk->flags & CBLK_INVALID_MSK) {
1056 mCblk->lock.lock();
1057 result = restoreTrack_l(mCblk, false);
1058 mCblk->lock.unlock();
1059
1060 if (result == OK)
1061 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1062 }
1063
1064 return result;
1065}
1066
1067status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1068 int64_t pts)
1069{
1070 // restart track if it was disabled by audioflinger due to previous underrun
1071 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1072 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1073 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1074 mAudioTrack->start(0);
1075 }
1076
1077 return mAudioTrack->queueTimedBuffer(buffer, pts);
1078}
1079
1080status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1081 TargetTimeline target)
1082{
1083 return mAudioTrack->setMediaTimeTransform(xform, target);
1084}
1085
1086// -------------------------------------------------------------------------
1087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1089{
1090 Buffer audioBuffer;
1091 uint32_t frames;
1092 size_t writtenSize;
1093
Eric Laurent421ddc02011-03-07 14:52:59 -08001094 mLock.lock();
1095 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1096 // while we are accessing the cblk
1097 sp <IAudioTrack> audioTrack = mAudioTrack;
1098 sp <IMemory> iMem = mCblkMemory;
1099 audio_track_cblk_t* cblk = mCblk;
Glenn Kastene6810ff2012-01-03 09:42:47 -08001100 bool active = mActive;
Eric Laurent421ddc02011-03-07 14:52:59 -08001101 mLock.unlock();
1102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 // Manage underrun callback
Glenn Kastene6810ff2012-01-03 09:42:47 -08001104 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block71f2cf12011-10-20 11:56:00 +01001105 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001106 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001108 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001109 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 if (mSharedBuffer != 0) return false;
1112 }
1113 }
Eric Laurenta553c252009-07-17 12:17:14 -07001114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001116 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 int loopCount = -1;
1118 mLoopCount--;
1119 if (mLoopCount >= 0) loopCount = mLoopCount;
1120
1121 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1122 }
1123
1124 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001125 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001126 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001128 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
1130 }
1131
1132 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001133 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001134 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1136 mNewPosition += mUpdatePeriod;
1137 }
1138 }
1139
1140 // If Shared buffer is used, no data is requested from client.
1141 if (mSharedBuffer != 0) {
1142 frames = 0;
1143 } else {
1144 frames = mRemainingFrames;
1145 }
1146
Glenn Kastenb3db2132012-01-19 08:59:58 -08001147 // See description of waitCount parameter at declaration of obtainBuffer().
1148 // The logic below prevents us from being stuck below at obtainBuffer()
1149 // not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001150 int32_t waitCount = -1;
1151 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1152 waitCount = 1;
1153 }
1154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 do {
1156
1157 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001158
Eric Laurentf1d360a2011-09-07 11:13:23 -07001159 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 if (err < NO_ERROR) {
1161 if (err != TIMED_OUT) {
Steve Block3762c312012-01-06 19:20:56 +00001162 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 -08001163 return false;
1164 }
1165 break;
1166 }
1167 if (err == status_t(STOPPED)) return false;
1168
1169 // Divide buffer size by 2 to take into account the expansion
1170 // due to 8 to 16 bit conversion: the callback must fill only half
1171 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001172 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 audioBuffer.size >>= 1;
1174 }
1175
1176 size_t reqSize = audioBuffer.size;
1177 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1178 writtenSize = audioBuffer.size;
1179
1180 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001181 if (ssize_t(writtenSize) <= 0) {
1182 // The callback is done filling buffers
1183 // Keep this thread going to handle timed events and
1184 // still try to get more data in intervals of WAIT_PERIOD_MS
1185 // but don't just loop and block the CPU, so wait
1186 usleep(WAIT_PERIOD_MS*1000);
1187 break;
1188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 if (writtenSize > reqSize) writtenSize = reqSize;
1190
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001191 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kastencc2302d2012-01-11 09:52:19 -08001192 // 8 to 16 bit conversion, note that source and destination are the same address
1193 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 writtenSize <<= 1;
1195 }
1196
1197 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001198 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001199 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurenta553c252009-07-17 12:17:14 -07001200 // 16 bit.
1201 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 frames -= audioBuffer.frameCount;
1204
1205 releaseBuffer(&audioBuffer);
1206 }
1207 while (frames);
1208
1209 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001210 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 } else {
1212 mRemainingFrames = frames;
1213 }
1214 return true;
1215}
1216
Eric Laurent421ddc02011-03-07 14:52:59 -08001217// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1218// the IAudioTrack and IMemory in case they are recreated here.
1219// If the IAudioTrack is successfully restored, the cblk pointer is updated
1220status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1221{
1222 status_t result;
1223
Eric Laurentae29b762011-03-28 18:37:07 -07001224 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001225 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Eric Laurent7e8626f2011-09-13 15:04:17 -07001226 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001227
Eric Laurent421ddc02011-03-07 14:52:59 -08001228 // signal old cblk condition so that other threads waiting for available buffers stop
1229 // waiting now
1230 cblk->cv.broadcast();
1231 cblk->lock.unlock();
1232
Eric Laurent05ce0942011-08-30 10:18:54 -07001233 // refresh the audio configuration cache in this process to make sure we get new
1234 // output parameters in getOutput_l() and createTrack_l()
1235 AudioSystem::clearAudioConfigCache();
1236
Eric Laurent421ddc02011-03-07 14:52:59 -08001237 // if the new IAudioTrack is created, createTrack_l() will modify the
1238 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1239 // It will also delete the strong references on previous IAudioTrack and IMemory
1240 result = createTrack_l(mStreamType,
1241 cblk->sampleRate,
1242 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001243 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001244 mFrameCount,
1245 mFlags,
1246 mSharedBuffer,
1247 getOutput_l(),
1248 false);
1249
1250 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001251 uint32_t user = cblk->user;
1252 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001253 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001254 mCblk->user = user;
1255 mCblk->server = user;
1256 mCblk->userBase = user;
1257 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001258 // restore loop: this is not guaranteed to succeed if new frame count is not
1259 // compatible with loop length
1260 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001261 if (!fromStart) {
1262 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001263 // Make sure that a client relying on callback events indicating underrun or
1264 // the actual amount of audio frames played (e.g SoundPool) receives them.
1265 if (mSharedBuffer == 0) {
1266 uint32_t frames = 0;
1267 if (user > server) {
1268 frames = ((user - server) > mCblk->frameCount) ?
1269 mCblk->frameCount : (user - server);
1270 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1271 }
1272 // restart playback even if buffer is not completely filled.
1273 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1274 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1275 // the client
1276 mCblk->stepUser(frames);
1277 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001278 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001279 if (mActive) {
Glenn Kasten6a20b262012-02-02 10:56:47 -08001280 result = mAudioTrack->start(0); // callback thread hasn't changed
Steve Block8564c8d2012-01-05 23:22:43 +00001281 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent6667ac32011-03-21 11:49:00 -07001282 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001283 if (fromStart && result == NO_ERROR) {
1284 mNewPosition = mCblk->server + mUpdatePeriod;
1285 }
1286 }
1287 if (result != NO_ERROR) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001288 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +00001289 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -08001290 }
Eric Laurent7e8626f2011-09-13 15:04:17 -07001291 mRestoreStatus = result;
Eric Laurent421ddc02011-03-07 14:52:59 -08001292 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001293 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001294 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001295 } else {
1296 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001297 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001298 mLock.unlock();
1299 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurent7e8626f2011-09-13 15:04:17 -07001300 if (result == NO_ERROR) {
1301 result = mRestoreStatus;
1302 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001303 cblk->lock.unlock();
1304 mLock.lock();
1305 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001306 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurent7e8626f2011-09-13 15:04:17 -07001307 result = mRestoreStatus;
Eric Laurent421ddc02011-03-07 14:52:59 -08001308 cblk->lock.unlock();
1309 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001310 }
Steve Block71f2cf12011-10-20 11:56:00 +01001311 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Eric Laurent421ddc02011-03-07 14:52:59 -08001312 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1313
1314 if (result == NO_ERROR) {
1315 // from now on we switch to the newly created cblk
1316 cblk = mCblk;
1317 }
1318 cblk->lock.lock();
1319
Steve Block8564c8d2012-01-05 23:22:43 +00001320 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001321
1322 return result;
1323}
1324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1326{
1327
1328 const size_t SIZE = 256;
1329 char buffer[SIZE];
1330 String8 result;
1331
1332 result.append(" AudioTrack::dump\n");
1333 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1334 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001335 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 -08001336 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001337 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 -08001338 result.append(buffer);
1339 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1340 result.append(buffer);
1341 ::write(fd, result.string(), result.size());
1342 return NO_ERROR;
1343}
1344
1345// =========================================================================
1346
1347AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1348 : Thread(bCanCallJava), mReceiver(receiver)
1349{
1350}
1351
1352bool AudioTrack::AudioTrackThread::threadLoop()
1353{
1354 return mReceiver.processAudioBuffer(this);
1355}
1356
1357status_t AudioTrack::AudioTrackThread::readyToRun()
1358{
1359 return NO_ERROR;
1360}
1361
1362void AudioTrack::AudioTrackThread::onFirstRef()
1363{
1364}
1365
1366// =========================================================================
1367
Eric Laurentae29b762011-03-28 18:37:07 -07001368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001370 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kasten3694ec12012-01-27 16:47:15 -08001371 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kastenbc4de882012-01-17 14:39:34 -08001372 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten4790bd82012-01-03 14:22:33 -08001373 mSendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374{
1375}
1376
1377uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1378{
Glenn Kastendb298a42011-12-13 11:45:07 -08001379 uint32_t u = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380
1381 u += frameCount;
1382 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001383 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1385 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1386 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1387 }
Glenn Kastendb298a42011-12-13 11:45:07 -08001388 } else if (u > server) {
Steve Block8564c8d2012-01-05 23:22:43 +00001389 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001390 u = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
1392
1393 if (u >= userBase + this->frameCount) {
1394 userBase += this->frameCount;
1395 }
1396
Glenn Kastendb298a42011-12-13 11:45:07 -08001397 user = u;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398
1399 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001400 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001401 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403
1404 return u;
1405}
1406
1407bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1408{
Eric Laurentae29b762011-03-28 18:37:07 -07001409 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001410 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 return false;
1412 }
1413
Glenn Kastendb298a42011-12-13 11:45:07 -08001414 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415
1416 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001417 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 // Mark that we have read the first buffer so that next time stepUser() is called
1419 // we switch to normal obtainBuffer() timeout period
1420 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001421 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 // It is possible that we receive a flush()
1424 // while the mixer is processing a block: in this case,
1425 // stepServer() is called After the flush() has reset u & s and
1426 // we have s > u
Glenn Kastendb298a42011-12-13 11:45:07 -08001427 if (s > user) {
Steve Block8564c8d2012-01-05 23:22:43 +00001428 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001429 s = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431 }
1432
1433 if (s >= loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +00001434 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 s = loopStart;
1436 if (--loopCount == 0) {
1437 loopEnd = UINT_MAX;
1438 loopStart = UINT_MAX;
1439 }
1440 }
1441 if (s >= serverBase + this->frameCount) {
1442 serverBase += this->frameCount;
1443 }
1444
Glenn Kastendb298a42011-12-13 11:45:07 -08001445 server = s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446
Eric Laurent421ddc02011-03-07 14:52:59 -08001447 if (!(flags & CBLK_INVALID_MSK)) {
1448 cv.signal();
1449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 lock.unlock();
1451 return true;
1452}
1453
1454void* audio_track_cblk_t::buffer(uint32_t offset) const
1455{
Glenn Kastendb298a42011-12-13 11:45:07 -08001456 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457}
1458
1459uint32_t audio_track_cblk_t::framesAvailable()
1460{
1461 Mutex::Autolock _l(lock);
1462 return framesAvailable_l();
1463}
1464
1465uint32_t audio_track_cblk_t::framesAvailable_l()
1466{
Glenn Kastendb298a42011-12-13 11:45:07 -08001467 uint32_t u = user;
1468 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001470 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 uint32_t limit = (s < loopStart) ? s : loopStart;
1472 return limit + frameCount - u;
1473 } else {
1474 return frameCount + u - s;
1475 }
1476}
1477
1478uint32_t audio_track_cblk_t::framesReady()
1479{
Glenn Kastendb298a42011-12-13 11:45:07 -08001480 uint32_t u = user;
1481 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001483 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 if (u < loopEnd) {
1485 return u - s;
1486 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001487 // do not block on mutex shared with client on AudioFlinger side
1488 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001489 ALOGW("framesReady() could not lock cblk");
Eric Laurentae29b762011-03-28 18:37:07 -07001490 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 }
Eric Laurentae29b762011-03-28 18:37:07 -07001492 uint32_t frames = UINT_MAX;
1493 if (loopCount >= 0) {
1494 frames = (loopEnd - loopStart)*loopCount + u - s;
1495 }
1496 lock.unlock();
1497 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 }
1499 } else {
1500 return s - u;
1501 }
1502}
1503
Eric Laurentae29b762011-03-28 18:37:07 -07001504bool audio_track_cblk_t::tryLock()
1505{
1506 // the code below simulates lock-with-timeout
1507 // we MUST do this to protect the AudioFlinger server
1508 // as this lock is shared with the client.
1509 status_t err;
1510
1511 err = lock.tryLock();
1512 if (err == -EBUSY) { // just wait a bit
1513 usleep(1000);
1514 err = lock.tryLock();
1515 }
1516 if (err != NO_ERROR) {
1517 // probably, the client just died.
1518 return false;
1519 }
1520 return true;
1521}
1522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523// -------------------------------------------------------------------------
1524
1525}; // namespace android