blob: aead9a1654bece058fe8e7835939f0d730960a82 [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),
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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
349
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800350 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800351 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
352 // while we are accessing the cblk
353 sp <IAudioTrack> audioTrack = mAudioTrack;
354 sp <IMemory> iMem = mCblkMemory;
355 audio_track_cblk_t* cblk = mCblk;
356
Glenn Kastene6810ff2012-01-03 09:42:47 -0800357 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700358 mFlushed = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800359 mActive = true;
Eric Laurent421ddc02011-03-07 14:52:59 -0800360 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700361 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800362 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
363 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700364 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800365 pid_t tid;
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);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800368 tid = t->getTid(); // pid_t is unknown until run()
369 ALOGV("getTid=%d", tid);
370 if (tid == -1) {
371 tid = 0;
372 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800373 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700374 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
375 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
376 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800377 tid = 0; // not gettid()
Eric Laurent059b4be2009-11-09 23:32:22 -0800378 }
379
Steve Block71f2cf12011-10-20 11:56:00 +0100380 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800381 if (!(cblk->flags & CBLK_INVALID_MSK)) {
382 cblk->lock.unlock();
Glenn Kasten6a20b262012-02-02 10:56:47 -0800383 ALOGV("mAudioTrack->start(tid=%d)", tid);
384 status = mAudioTrack->start(tid);
Eric Laurent421ddc02011-03-07 14:52:59 -0800385 cblk->lock.lock();
386 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700387 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800388 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800389 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800390 if (cblk->flags & CBLK_INVALID_MSK) {
391 status = restoreTrack_l(cblk, true);
392 }
393 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800394 if (status != NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100395 ALOGV("start() failed");
Glenn Kastene6810ff2012-01-03 09:42:47 -0800396 mActive = false;
Eric Laurent059b4be2009-11-09 23:32:22 -0800397 if (t != 0) {
398 t->requestExit();
399 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700400 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
401 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent059b4be2009-11-09 23:32:22 -0800402 }
Eric Laurentbda74692009-11-04 08:27:26 -0800403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406}
407
408void AudioTrack::stop()
409{
410 sp<AudioTrackThread> t = mAudioTrackThread;
411
Steve Block71f2cf12011-10-20 11:56:00 +0100412 ALOGV("stop %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800414 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800415 if (mActive) {
416 mActive = false;
Eric Laurentef028272009-04-21 07:56:33 -0700417 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 mAudioTrack->stop();
419 // Cancel loops (If we are in the middle of a loop, playback
420 // would not stop until loopCount reaches 0).
Eric Laurent421ddc02011-03-07 14:52:59 -0800421 setLoop_l(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700422 // the playback head position will reset to 0, so if a marker is set, we need
423 // to activate it again
424 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 // Force flush if a shared buffer is used otherwise audioflinger
426 // will not stop before end of buffer is reached.
427 if (mSharedBuffer != 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800428 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 }
430 if (t != 0) {
431 t->requestExit();
432 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700433 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
434 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
436 }
437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438}
439
440bool AudioTrack::stopped() const
441{
Glenn Kastene6810ff2012-01-03 09:42:47 -0800442 AutoMutex lock(mLock);
443 return stopped_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444}
445
446void AudioTrack::flush()
447{
Eric Laurent421ddc02011-03-07 14:52:59 -0800448 AutoMutex lock(mLock);
449 flush_l();
450}
451
452// must be called with mLock held
453void AudioTrack::flush_l()
454{
Steve Block71f2cf12011-10-20 11:56:00 +0100455 ALOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700456
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700457 // clear playback marker and periodic update counter
458 mMarkerPosition = 0;
459 mMarkerReached = false;
460 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700463 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 mAudioTrack->flush();
465 // Release AudioTrack callback thread in case it was waiting for new buffers
466 // in AudioTrack::obtainBuffer()
467 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469}
470
471void AudioTrack::pause()
472{
Steve Block71f2cf12011-10-20 11:56:00 +0100473 ALOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800474 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800475 if (mActive) {
476 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 mAudioTrack->pause();
478 }
479}
480
481void AudioTrack::mute(bool e)
482{
483 mAudioTrack->mute(e);
484 mMuted = e;
485}
486
487bool AudioTrack::muted() const
488{
489 return mMuted;
490}
491
Eric Laurent65b65452010-06-01 23:49:17 -0700492status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493{
Glenn Kasten1c50a782011-11-30 09:46:04 -0800494 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700495 return BAD_VALUE;
496 }
497
Eric Laurent421ddc02011-03-07 14:52:59 -0800498 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 mVolume[LEFT] = left;
500 mVolume[RIGHT] = right;
501
Glenn Kastenbc4de882012-01-17 14:39:34 -0800502 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurent65b65452010-06-01 23:49:17 -0700503
504 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505}
506
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800507void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508{
Eric Laurent65b65452010-06-01 23:49:17 -0700509 if (left != NULL) {
510 *left = mVolume[LEFT];
511 }
512 if (right != NULL) {
513 *right = mVolume[RIGHT];
514 }
515}
516
Eric Laurent7070b362010-07-16 07:43:46 -0700517status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700518{
Steve Block71f2cf12011-10-20 11:56:00 +0100519 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800520 if (level < 0.0f || level > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700521 return BAD_VALUE;
522 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800523 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700524
525 mSendLevel = level;
526
Glenn Kasten4790bd82012-01-03 14:22:33 -0800527 mCblk->setSendLevel(level);
Eric Laurent65b65452010-06-01 23:49:17 -0700528
529 return NO_ERROR;
530}
531
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800532void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurent65b65452010-06-01 23:49:17 -0700533{
534 if (level != NULL) {
535 *level = mSendLevel;
536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537}
538
Eric Laurent88e209d2009-07-07 07:10:45 -0700539status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540{
541 int afSamplingRate;
542
543 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700544 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 }
546 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700547 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548
Eric Laurent421ddc02011-03-07 14:52:59 -0800549 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700550 mCblk->sampleRate = rate;
551 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552}
553
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800554uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555{
Eric Laurent421ddc02011-03-07 14:52:59 -0800556 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700557 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558}
559
560status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
561{
Eric Laurent421ddc02011-03-07 14:52:59 -0800562 AutoMutex lock(mLock);
563 return setLoop_l(loopStart, loopEnd, loopCount);
564}
565
566// must be called with mLock held
567status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
568{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 audio_track_cblk_t* cblk = mCblk;
570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 Mutex::Autolock _l(cblk->lock);
572
573 if (loopCount == 0) {
574 cblk->loopStart = UINT_MAX;
575 cblk->loopEnd = UINT_MAX;
576 cblk->loopCount = 0;
577 mLoopCount = 0;
578 return NO_ERROR;
579 }
580
581 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700582 loopEnd - loopStart > cblk->frameCount ||
583 cblk->server > loopStart) {
Steve Block3762c312012-01-06 19:20:56 +0000584 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 -0800585 return BAD_VALUE;
586 }
587
Eric Laurent6667ac32011-03-21 11:49:00 -0700588 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000589 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700590 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593
594 cblk->loopStart = loopStart;
595 cblk->loopEnd = loopEnd;
596 cblk->loopCount = loopCount;
597 mLoopCount = loopCount;
598
599 return NO_ERROR;
600}
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602status_t AudioTrack::setMarkerPosition(uint32_t marker)
603{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800604 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605
606 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700607 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608
609 return NO_ERROR;
610}
611
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800612status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800614 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615
616 *marker = mMarkerPosition;
617
618 return NO_ERROR;
619}
620
621status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
622{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800623 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624
625 uint32_t curPosition;
626 getPosition(&curPosition);
627 mNewPosition = curPosition + updatePeriod;
628 mUpdatePeriod = updatePeriod;
629
630 return NO_ERROR;
631}
632
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800633status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800635 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636
637 *updatePeriod = mUpdatePeriod;
638
639 return NO_ERROR;
640}
641
642status_t AudioTrack::setPosition(uint32_t position)
643{
Eric Laurent421ddc02011-03-07 14:52:59 -0800644 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645
Glenn Kastene6810ff2012-01-03 09:42:47 -0800646 if (!stopped_l()) return INVALID_OPERATION;
647
648 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649
650 if (position > mCblk->user) return BAD_VALUE;
651
652 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700653 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 return NO_ERROR;
656}
657
658status_t AudioTrack::getPosition(uint32_t *position)
659{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800660 if (position == NULL) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800661 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700662 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663
664 return NO_ERROR;
665}
666
667status_t AudioTrack::reload()
668{
Eric Laurent421ddc02011-03-07 14:52:59 -0800669 AutoMutex lock(mLock);
670
Glenn Kastene6810ff2012-01-03 09:42:47 -0800671 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700672
Eric Laurent421ddc02011-03-07 14:52:59 -0800673 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700675 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676
677 return NO_ERROR;
678}
679
Eric Laurenta553c252009-07-17 12:17:14 -0700680audio_io_handle_t AudioTrack::getOutput()
681{
Eric Laurent421ddc02011-03-07 14:52:59 -0800682 AutoMutex lock(mLock);
683 return getOutput_l();
684}
685
686// must be called with mLock held
687audio_io_handle_t AudioTrack::getOutput_l()
688{
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800689 return AudioSystem::getOutput(mStreamType,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700690 mCblk->sampleRate, mFormat, mChannelMask, (audio_policy_output_flags_t)mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700691}
692
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800693int AudioTrack::getSessionId() const
Eric Laurent65b65452010-06-01 23:49:17 -0700694{
695 return mSessionId;
696}
697
698status_t AudioTrack::attachAuxEffect(int effectId)
699{
Steve Block71f2cf12011-10-20 11:56:00 +0100700 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700701 status_t status = mAudioTrack->attachAuxEffect(effectId);
702 if (status == NO_ERROR) {
703 mAuxEffectId = effectId;
704 }
705 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700706}
707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708// -------------------------------------------------------------------------
709
Eric Laurent421ddc02011-03-07 14:52:59 -0800710// must be called with mLock held
711status_t AudioTrack::createTrack_l(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800712 audio_stream_type_t streamType,
Eric Laurentbda74692009-11-04 08:27:26 -0800713 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800714 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700715 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800716 int frameCount,
717 uint32_t flags,
718 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700719 audio_io_handle_t output,
720 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800721{
722 status_t status;
723 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
724 if (audioFlinger == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000725 ALOGE("Could not get audioflinger");
Eric Laurentbda74692009-11-04 08:27:26 -0800726 return NO_INIT;
727 }
728
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700729 int afSampleRate;
730 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
731 return NO_INIT;
732 }
733 int afFrameCount;
734 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
735 return NO_INIT;
736 }
737 uint32_t afLatency;
738 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
739 return NO_INIT;
740 }
741
742 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700743 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700744 if (sharedBuffer != 0) {
745 frameCount = sharedBuffer->size();
746 }
747 } else {
748 // Ensure that buffer depth covers at least audio hardware latency
749 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
750 if (minBufCount < 2) minBufCount = 2;
751
752 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
753
754 if (sharedBuffer == 0) {
755 if (frameCount == 0) {
756 frameCount = minFrameCount;
757 }
758 if (mNotificationFramesAct == 0) {
759 mNotificationFramesAct = frameCount/2;
760 }
761 // Make sure that application is notified with sufficient margin
762 // before underrun
763 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
764 mNotificationFramesAct = frameCount/2;
765 }
766 if (frameCount < minFrameCount) {
767 if (enforceFrameCount) {
Steve Block3762c312012-01-06 19:20:56 +0000768 ALOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700769 return BAD_VALUE;
770 } else {
771 frameCount = minFrameCount;
772 }
773 }
774 } else {
Glenn Kastenb3db2132012-01-19 08:59:58 -0800775 // Ensure that buffer alignment matches channelCount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700776 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700777 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000778 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700779 return BAD_VALUE;
780 }
781 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
782 }
783 }
784
Eric Laurentbda74692009-11-04 08:27:26 -0800785 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
786 streamType,
787 sampleRate,
788 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700789 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800790 frameCount,
791 ((uint16_t)flags) << 16,
792 sharedBuffer,
793 output,
Eric Laurent65b65452010-06-01 23:49:17 -0700794 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800795 &status);
796
797 if (track == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000798 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurentbda74692009-11-04 08:27:26 -0800799 return status;
800 }
801 sp<IMemory> cblk = track->getCblk();
802 if (cblk == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000803 ALOGE("Could not get control block");
Eric Laurentbda74692009-11-04 08:27:26 -0800804 return NO_INIT;
805 }
Eric Laurentbda74692009-11-04 08:27:26 -0800806 mAudioTrack = track;
Eric Laurentbda74692009-11-04 08:27:26 -0800807 mCblkMemory = cblk;
808 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurentae29b762011-03-28 18:37:07 -0700809 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurentbda74692009-11-04 08:27:26 -0800810 if (sharedBuffer == 0) {
811 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
812 } else {
813 mCblk->buffers = sharedBuffer->pointer();
814 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700815 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800816 }
817
Glenn Kastenbc4de882012-01-17 14:39:34 -0800818 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten4790bd82012-01-03 14:22:33 -0800819 mCblk->setSendLevel(mSendLevel);
Eric Laurent7070b362010-07-16 07:43:46 -0700820 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent49f02be2009-11-19 09:00:56 -0800821 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
822 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700823 mRemainingFrames = mNotificationFramesAct;
824 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800825 return NO_ERROR;
826}
827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
829{
Eric Laurent421ddc02011-03-07 14:52:59 -0800830 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800831 bool active;
Glenn Kasten028ab992011-06-22 16:18:04 -0700832 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 audio_track_cblk_t* cblk = mCblk;
834 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700835 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
837 audioBuffer->frameCount = 0;
838 audioBuffer->size = 0;
839
840 uint32_t framesAvail = cblk->framesAvailable();
841
Eric Laurent6667ac32011-03-21 11:49:00 -0700842 cblk->lock.lock();
843 if (cblk->flags & CBLK_INVALID_MSK) {
844 goto create_new_track;
845 }
846 cblk->lock.unlock();
847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800849 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 goto start_loop_here;
851 while (framesAvail == 0) {
852 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800853 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100854 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800855 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 return NO_MORE_BUFFERS;
857 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800858 if (CC_UNLIKELY(!waitCount)) {
Eric Laurentbda74692009-11-04 08:27:26 -0800859 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800861 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700862 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800863 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700864 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700865 cblk->lock.unlock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800866 mLock.lock();
Glenn Kastene6810ff2012-01-03 09:42:47 -0800867 if (!mActive) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800868 return status_t(STOPPED);
869 }
870 cblk->lock.lock();
871 }
872
873 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700874 goto create_new_track;
875 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800876 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurentef028272009-04-21 07:56:33 -0700877 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
879 // timing out when a loop has been set and we have already written upto loop end
880 // is a normal condition: no need to wake AudioFlinger up.
881 if (cblk->user < cblk->loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +0000882 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700884 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 cblk->lock.unlock();
Glenn Kasten6a20b262012-02-02 10:56:47 -0800886 result = mAudioTrack->start(0); // callback thread hasn't changed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800888 if (result == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700889 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -0800890create_new_track:
891 result = restoreTrack_l(cblk, false);
892 }
893 if (result != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000894 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -0800895 cblk->lock.unlock();
896 return result;
897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
899 cblk->waitTimeMs = 0;
900 }
Eric Laurenta553c252009-07-17 12:17:14 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800903 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 return TIMED_OUT;
905 }
906 }
907 // read the server count again
908 start_loop_here:
909 framesAvail = cblk->framesAvailable_l();
910 }
Eric Laurentbda74692009-11-04 08:27:26 -0800911 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
913
Eric Laurent4712baa2010-09-30 16:12:31 -0700914 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent421ddc02011-03-07 14:52:59 -0800915 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurentae29b762011-03-28 18:37:07 -0700916 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +0000917 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800918 mAudioTrack->start(0); // callback thread hasn't changed
Eric Laurent4712baa2010-09-30 16:12:31 -0700919 }
920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 if (framesReq > framesAvail) {
924 framesReq = framesAvail;
925 }
926
927 uint32_t u = cblk->user;
928 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
929
930 if (u + framesReq > bufferEnd) {
931 framesReq = bufferEnd - u;
932 }
933
Eric Laurenta553c252009-07-17 12:17:14 -0700934 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
935 audioBuffer->channelCount = mChannelCount;
936 audioBuffer->frameCount = framesReq;
937 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700938 if (audio_is_linear_pcm(mFormat)) {
939 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700940 } else {
941 audioBuffer->format = mFormat;
942 }
943 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 active = mActive;
945 return active ? status_t(NO_ERROR) : status_t(STOPPED);
946}
947
948void AudioTrack::releaseBuffer(Buffer* audioBuffer)
949{
Eric Laurent421ddc02011-03-07 14:52:59 -0800950 AutoMutex lock(mLock);
951 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952}
953
954// -------------------------------------------------------------------------
955
956ssize_t AudioTrack::write(const void* buffer, size_t userSize)
957{
958
959 if (mSharedBuffer != 0) return INVALID_OPERATION;
960
961 if (ssize_t(userSize) < 0) {
Glenn Kastenb3db2132012-01-19 08:59:58 -0800962 // Sanity-check: user is most-likely passing an error code, and it would
963 // make the return value ambiguous (actualSize vs error).
Steve Block3762c312012-01-06 19:20:56 +0000964 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 buffer, userSize, userSize);
966 return BAD_VALUE;
967 }
968
Steve Block71f2cf12011-10-20 11:56:00 +0100969 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970
Eric Laurent421ddc02011-03-07 14:52:59 -0800971 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
972 // while we are accessing the cblk
973 mLock.lock();
974 sp <IAudioTrack> audioTrack = mAudioTrack;
975 sp <IMemory> iMem = mCblkMemory;
976 mLock.unlock();
977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 ssize_t written = 0;
979 const int8_t *src = (const int8_t *)buffer;
980 Buffer audioBuffer;
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800981 size_t frameSz = frameSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982
983 do {
Eric Laurent913af0b2011-03-17 09:36:51 -0700984 audioBuffer.frameCount = userSize/frameSz;
Eric Laurenta553c252009-07-17 12:17:14 -0700985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 status_t err = obtainBuffer(&audioBuffer, -1);
987 if (err < 0) {
988 // out of buffers, return #bytes written
989 if (err == status_t(NO_MORE_BUFFERS))
990 break;
991 return ssize_t(err);
992 }
993
994 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -0700995
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700996 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 // Divide capacity by 2 to take expansion into account
998 toWrite = audioBuffer.size>>1;
Glenn Kastencc2302d2012-01-11 09:52:19 -0800999 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent28ad42b2009-08-04 10:42:26 -07001000 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 toWrite = audioBuffer.size;
1002 memcpy(audioBuffer.i8, src, toWrite);
1003 src += toWrite;
1004 }
1005 userSize -= toWrite;
1006 written += toWrite;
1007
1008 releaseBuffer(&audioBuffer);
Eric Laurent913af0b2011-03-17 09:36:51 -07001009 } while (userSize >= frameSz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010
1011 return written;
1012}
1013
1014// -------------------------------------------------------------------------
1015
1016bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1017{
1018 Buffer audioBuffer;
1019 uint32_t frames;
1020 size_t writtenSize;
1021
Eric Laurent421ddc02011-03-07 14:52:59 -08001022 mLock.lock();
1023 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1024 // while we are accessing the cblk
1025 sp <IAudioTrack> audioTrack = mAudioTrack;
1026 sp <IMemory> iMem = mCblkMemory;
1027 audio_track_cblk_t* cblk = mCblk;
Glenn Kastene6810ff2012-01-03 09:42:47 -08001028 bool active = mActive;
Eric Laurent421ddc02011-03-07 14:52:59 -08001029 mLock.unlock();
1030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 // Manage underrun callback
Glenn Kastene6810ff2012-01-03 09:42:47 -08001032 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block71f2cf12011-10-20 11:56:00 +01001033 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001034 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001036 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001037 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 if (mSharedBuffer != 0) return false;
1040 }
1041 }
Eric Laurenta553c252009-07-17 12:17:14 -07001042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001044 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 int loopCount = -1;
1046 mLoopCount--;
1047 if (mLoopCount >= 0) loopCount = mLoopCount;
1048
1049 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1050 }
1051
1052 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001053 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001054 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001056 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 }
1059
1060 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001061 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001062 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1064 mNewPosition += mUpdatePeriod;
1065 }
1066 }
1067
1068 // If Shared buffer is used, no data is requested from client.
1069 if (mSharedBuffer != 0) {
1070 frames = 0;
1071 } else {
1072 frames = mRemainingFrames;
1073 }
1074
Glenn Kastenb3db2132012-01-19 08:59:58 -08001075 // See description of waitCount parameter at declaration of obtainBuffer().
1076 // The logic below prevents us from being stuck below at obtainBuffer()
1077 // not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001078 int32_t waitCount = -1;
1079 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1080 waitCount = 1;
1081 }
1082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 do {
1084
1085 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001086
Eric Laurentf1d360a2011-09-07 11:13:23 -07001087 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 if (err < NO_ERROR) {
1089 if (err != TIMED_OUT) {
Steve Block3762c312012-01-06 19:20:56 +00001090 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 -08001091 return false;
1092 }
1093 break;
1094 }
1095 if (err == status_t(STOPPED)) return false;
1096
1097 // Divide buffer size by 2 to take into account the expansion
1098 // due to 8 to 16 bit conversion: the callback must fill only half
1099 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001100 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 audioBuffer.size >>= 1;
1102 }
1103
1104 size_t reqSize = audioBuffer.size;
1105 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1106 writtenSize = audioBuffer.size;
1107
1108 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001109 if (ssize_t(writtenSize) <= 0) {
1110 // The callback is done filling buffers
1111 // Keep this thread going to handle timed events and
1112 // still try to get more data in intervals of WAIT_PERIOD_MS
1113 // but don't just loop and block the CPU, so wait
1114 usleep(WAIT_PERIOD_MS*1000);
1115 break;
1116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 if (writtenSize > reqSize) writtenSize = reqSize;
1118
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001119 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kastencc2302d2012-01-11 09:52:19 -08001120 // 8 to 16 bit conversion, note that source and destination are the same address
1121 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 writtenSize <<= 1;
1123 }
1124
1125 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001126 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001127 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurenta553c252009-07-17 12:17:14 -07001128 // 16 bit.
1129 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 frames -= audioBuffer.frameCount;
1132
1133 releaseBuffer(&audioBuffer);
1134 }
1135 while (frames);
1136
1137 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001138 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 } else {
1140 mRemainingFrames = frames;
1141 }
1142 return true;
1143}
1144
Eric Laurent421ddc02011-03-07 14:52:59 -08001145// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1146// the IAudioTrack and IMemory in case they are recreated here.
1147// If the IAudioTrack is successfully restored, the cblk pointer is updated
1148status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1149{
1150 status_t result;
1151
Eric Laurentae29b762011-03-28 18:37:07 -07001152 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001153 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Eric Laurent7e8626f2011-09-13 15:04:17 -07001154 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001155
Eric Laurent421ddc02011-03-07 14:52:59 -08001156 // signal old cblk condition so that other threads waiting for available buffers stop
1157 // waiting now
1158 cblk->cv.broadcast();
1159 cblk->lock.unlock();
1160
Eric Laurent05ce0942011-08-30 10:18:54 -07001161 // refresh the audio configuration cache in this process to make sure we get new
1162 // output parameters in getOutput_l() and createTrack_l()
1163 AudioSystem::clearAudioConfigCache();
1164
Eric Laurent421ddc02011-03-07 14:52:59 -08001165 // if the new IAudioTrack is created, createTrack_l() will modify the
1166 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1167 // It will also delete the strong references on previous IAudioTrack and IMemory
1168 result = createTrack_l(mStreamType,
1169 cblk->sampleRate,
1170 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001171 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001172 mFrameCount,
1173 mFlags,
1174 mSharedBuffer,
1175 getOutput_l(),
1176 false);
1177
1178 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001179 uint32_t user = cblk->user;
1180 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001181 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001182 mCblk->user = user;
1183 mCblk->server = user;
1184 mCblk->userBase = user;
1185 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001186 // restore loop: this is not guaranteed to succeed if new frame count is not
1187 // compatible with loop length
1188 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001189 if (!fromStart) {
1190 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001191 // Make sure that a client relying on callback events indicating underrun or
1192 // the actual amount of audio frames played (e.g SoundPool) receives them.
1193 if (mSharedBuffer == 0) {
1194 uint32_t frames = 0;
1195 if (user > server) {
1196 frames = ((user - server) > mCblk->frameCount) ?
1197 mCblk->frameCount : (user - server);
1198 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1199 }
1200 // restart playback even if buffer is not completely filled.
1201 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1202 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1203 // the client
1204 mCblk->stepUser(frames);
1205 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001206 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001207 if (mActive) {
Glenn Kasten6a20b262012-02-02 10:56:47 -08001208 result = mAudioTrack->start(0); // callback thread hasn't changed
Steve Block8564c8d2012-01-05 23:22:43 +00001209 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent6667ac32011-03-21 11:49:00 -07001210 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001211 if (fromStart && result == NO_ERROR) {
1212 mNewPosition = mCblk->server + mUpdatePeriod;
1213 }
1214 }
1215 if (result != NO_ERROR) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001216 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +00001217 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -08001218 }
Eric Laurent7e8626f2011-09-13 15:04:17 -07001219 mRestoreStatus = result;
Eric Laurent421ddc02011-03-07 14:52:59 -08001220 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001221 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001222 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001223 } else {
1224 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001225 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001226 mLock.unlock();
1227 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurent7e8626f2011-09-13 15:04:17 -07001228 if (result == NO_ERROR) {
1229 result = mRestoreStatus;
1230 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001231 cblk->lock.unlock();
1232 mLock.lock();
1233 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001234 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurent7e8626f2011-09-13 15:04:17 -07001235 result = mRestoreStatus;
Eric Laurent421ddc02011-03-07 14:52:59 -08001236 cblk->lock.unlock();
1237 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001238 }
Steve Block71f2cf12011-10-20 11:56:00 +01001239 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Eric Laurent421ddc02011-03-07 14:52:59 -08001240 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1241
1242 if (result == NO_ERROR) {
1243 // from now on we switch to the newly created cblk
1244 cblk = mCblk;
1245 }
1246 cblk->lock.lock();
1247
Steve Block8564c8d2012-01-05 23:22:43 +00001248 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001249
1250 return result;
1251}
1252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1254{
1255
1256 const size_t SIZE = 256;
1257 char buffer[SIZE];
1258 String8 result;
1259
1260 result.append(" AudioTrack::dump\n");
1261 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1262 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001263 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 -08001264 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001265 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 -08001266 result.append(buffer);
1267 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1268 result.append(buffer);
1269 ::write(fd, result.string(), result.size());
1270 return NO_ERROR;
1271}
1272
1273// =========================================================================
1274
1275AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1276 : Thread(bCanCallJava), mReceiver(receiver)
1277{
1278}
1279
1280bool AudioTrack::AudioTrackThread::threadLoop()
1281{
1282 return mReceiver.processAudioBuffer(this);
1283}
1284
1285status_t AudioTrack::AudioTrackThread::readyToRun()
1286{
1287 return NO_ERROR;
1288}
1289
1290void AudioTrack::AudioTrackThread::onFirstRef()
1291{
1292}
1293
1294// =========================================================================
1295
Eric Laurentae29b762011-03-28 18:37:07 -07001296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001298 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kasten3694ec12012-01-27 16:47:15 -08001299 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kastenbc4de882012-01-17 14:39:34 -08001300 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten4790bd82012-01-03 14:22:33 -08001301 mSendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302{
1303}
1304
1305uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1306{
Glenn Kastendb298a42011-12-13 11:45:07 -08001307 uint32_t u = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308
1309 u += frameCount;
1310 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001311 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1313 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1314 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1315 }
Glenn Kastendb298a42011-12-13 11:45:07 -08001316 } else if (u > server) {
Steve Block8564c8d2012-01-05 23:22:43 +00001317 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001318 u = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
1320
1321 if (u >= userBase + this->frameCount) {
1322 userBase += this->frameCount;
1323 }
1324
Glenn Kastendb298a42011-12-13 11:45:07 -08001325 user = u;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326
1327 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001328 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001329 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 return u;
1333}
1334
1335bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1336{
Eric Laurentae29b762011-03-28 18:37:07 -07001337 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001338 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 return false;
1340 }
1341
Glenn Kastendb298a42011-12-13 11:45:07 -08001342 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343
1344 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001345 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 // Mark that we have read the first buffer so that next time stepUser() is called
1347 // we switch to normal obtainBuffer() timeout period
1348 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001349 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 // It is possible that we receive a flush()
1352 // while the mixer is processing a block: in this case,
1353 // stepServer() is called After the flush() has reset u & s and
1354 // we have s > u
Glenn Kastendb298a42011-12-13 11:45:07 -08001355 if (s > user) {
Steve Block8564c8d2012-01-05 23:22:43 +00001356 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001357 s = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 }
1360
1361 if (s >= loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +00001362 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 s = loopStart;
1364 if (--loopCount == 0) {
1365 loopEnd = UINT_MAX;
1366 loopStart = UINT_MAX;
1367 }
1368 }
1369 if (s >= serverBase + this->frameCount) {
1370 serverBase += this->frameCount;
1371 }
1372
Glenn Kastendb298a42011-12-13 11:45:07 -08001373 server = s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374
Eric Laurent421ddc02011-03-07 14:52:59 -08001375 if (!(flags & CBLK_INVALID_MSK)) {
1376 cv.signal();
1377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 lock.unlock();
1379 return true;
1380}
1381
1382void* audio_track_cblk_t::buffer(uint32_t offset) const
1383{
Glenn Kastendb298a42011-12-13 11:45:07 -08001384 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385}
1386
1387uint32_t audio_track_cblk_t::framesAvailable()
1388{
1389 Mutex::Autolock _l(lock);
1390 return framesAvailable_l();
1391}
1392
1393uint32_t audio_track_cblk_t::framesAvailable_l()
1394{
Glenn Kastendb298a42011-12-13 11:45:07 -08001395 uint32_t u = user;
1396 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001398 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 uint32_t limit = (s < loopStart) ? s : loopStart;
1400 return limit + frameCount - u;
1401 } else {
1402 return frameCount + u - s;
1403 }
1404}
1405
1406uint32_t audio_track_cblk_t::framesReady()
1407{
Glenn Kastendb298a42011-12-13 11:45:07 -08001408 uint32_t u = user;
1409 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001411 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 if (u < loopEnd) {
1413 return u - s;
1414 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001415 // do not block on mutex shared with client on AudioFlinger side
1416 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001417 ALOGW("framesReady() could not lock cblk");
Eric Laurentae29b762011-03-28 18:37:07 -07001418 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
Eric Laurentae29b762011-03-28 18:37:07 -07001420 uint32_t frames = UINT_MAX;
1421 if (loopCount >= 0) {
1422 frames = (loopEnd - loopStart)*loopCount + u - s;
1423 }
1424 lock.unlock();
1425 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427 } else {
1428 return s - u;
1429 }
1430}
1431
Eric Laurentae29b762011-03-28 18:37:07 -07001432bool audio_track_cblk_t::tryLock()
1433{
1434 // the code below simulates lock-with-timeout
1435 // we MUST do this to protect the AudioFlinger server
1436 // as this lock is shared with the client.
1437 status_t err;
1438
1439 err = lock.tryLock();
1440 if (err == -EBUSY) { // just wait a bit
1441 usleep(1000);
1442 err = lock.tryLock();
1443 }
1444 if (err != NO_ERROR) {
1445 // probably, the client just died.
1446 return false;
1447 }
1448 return true;
1449}
1450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451// -------------------------------------------------------------------------
1452
1453}; // namespace android