blob: 34563caf60a4643169f291e20dff345884a2b529 [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,
Glenn Kasten28b269f2012-03-07 09:15:37 -080095 audio_policy_output_flags_t flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 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),
Glenn Kasten28b269f2012-03-07 09:15:37 -0800122 mIsTimed(false),
Andreas Huber28ea0132012-01-18 10:51:55 -0800123 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
124{
125 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
Glenn Kasten28b269f2012-03-07 09:15:37 -0800126 frameCount, (audio_policy_output_flags_t)flags, cbf, user, notificationFrames,
Andreas Huber28ea0132012-01-18 10:51:55 -0800127 0, false, sessionId);
128}
129
130AudioTrack::AudioTrack(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800131 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800133 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700134 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 const sp<IMemory>& sharedBuffer,
Glenn Kasten28b269f2012-03-07 09:15:37 -0800136 audio_policy_output_flags_t flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 callback_t cbf,
138 void* user,
Eric Laurent65b65452010-06-01 23:49:17 -0700139 int notificationFrames,
140 int sessionId)
Glenn Kasten99d54432011-06-22 16:15:25 -0700141 : mStatus(NO_INIT),
John Grossmand8cf2962012-02-08 16:37:41 -0800142 mIsTimed(false),
143 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
144 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145{
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700146 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurent619346f2010-06-21 09:27:30 -0700147 0, flags, cbf, user, notificationFrames,
148 sharedBuffer, false, sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149}
150
151AudioTrack::~AudioTrack()
152{
Steve Block71f2cf12011-10-20 11:56:00 +0100153 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
155 if (mStatus == NO_ERROR) {
156 // Make sure that callback function exits in the case where
157 // it is looping on buffer full condition in obtainBuffer().
158 // Otherwise the callback thread will never exit.
159 stop();
160 if (mAudioTrackThread != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 mAudioTrackThread->requestExitAndWait();
162 mAudioTrackThread.clear();
163 }
164 mAudioTrack.clear();
165 IPCThreadState::self()->flushCommands();
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700166 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
168}
169
170status_t AudioTrack::set(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800171 audio_stream_type_t streamType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800173 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700174 int channelMask,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 int frameCount,
Glenn Kasten28b269f2012-03-07 09:15:37 -0800176 audio_policy_output_flags_t flags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 callback_t cbf,
178 void* user,
179 int notificationFrames,
180 const sp<IMemory>& sharedBuffer,
Eric Laurent65b65452010-06-01 23:49:17 -0700181 bool threadCanCallJava,
182 int sessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183{
184
Steve Block71f2cf12011-10-20 11:56:00 +0100185 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Eric Laurent421ddc02011-03-07 14:52:59 -0800187 AutoMutex lock(mLock);
Eric Laurentef028272009-04-21 07:56:33 -0700188 if (mAudioTrack != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000189 ALOGE("Track already in use");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 return INVALID_OPERATION;
191 }
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 int afSampleRate;
194 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
195 return NO_INIT;
196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 uint32_t afLatency;
198 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
199 return NO_INIT;
200 }
201
202 // handle default values first.
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700203 if (streamType == AUDIO_STREAM_DEFAULT) {
204 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206 if (sampleRate == 0) {
207 sampleRate = afSampleRate;
208 }
209 // these below should probably come from the audioFlinger too...
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800210 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700211 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700213 if (channelMask == 0) {
214 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 }
216
217 // validate parameters
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700218 if (!audio_is_valid_format(format)) {
Steve Block3762c312012-01-06 19:20:56 +0000219 ALOGE("Invalid format");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 return BAD_VALUE;
221 }
Eric Laurenta553c252009-07-17 12:17:14 -0700222
223 // force direct flag if format is not linear PCM
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700224 if (!audio_is_linear_pcm(format)) {
Glenn Kasten28b269f2012-03-07 09:15:37 -0800225 flags = (audio_policy_output_flags_t) (flags | AUDIO_POLICY_OUTPUT_FLAG_DIRECT);
Eric Laurenta553c252009-07-17 12:17:14 -0700226 }
227
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700228 if (!audio_is_output_channel(channelMask)) {
Steve Block3762c312012-01-06 19:20:56 +0000229 ALOGE("Invalid channel mask");
Eric Laurenta553c252009-07-17 12:17:14 -0700230 return BAD_VALUE;
231 }
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700232 uint32_t channelCount = popcount(channelMask);
Eric Laurenta553c252009-07-17 12:17:14 -0700233
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700234 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800235 streamType,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800236 sampleRate, format, channelMask,
Glenn Kasten28b269f2012-03-07 09:15:37 -0800237 flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700238
239 if (output == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000240 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 return BAD_VALUE;
242 }
243
Eric Laurentbda74692009-11-04 08:27:26 -0800244 mVolume[LEFT] = 1.0f;
245 mVolume[RIGHT] = 1.0f;
Glenn Kasten4790bd82012-01-03 14:22:33 -0800246 mSendLevel = 0.0f;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700247 mFrameCount = frameCount;
248 mNotificationFramesReq = notificationFrames;
Eric Laurent65b65452010-06-01 23:49:17 -0700249 mSessionId = sessionId;
Eric Laurent7070b362010-07-16 07:43:46 -0700250 mAuxEffectId = 0;
Eric Laurent65b65452010-06-01 23:49:17 -0700251
Eric Laurentbda74692009-11-04 08:27:26 -0800252 // create the IAudioTrack
Eric Laurent421ddc02011-03-07 14:52:59 -0800253 status_t status = createTrack_l(streamType,
254 sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800255 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700256 (uint32_t)channelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -0800257 frameCount,
258 flags,
259 sharedBuffer,
260 output,
261 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Eric Laurentbda74692009-11-04 08:27:26 -0800263 if (status != NO_ERROR) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 return status;
265 }
Eric Laurentbda74692009-11-04 08:27:26 -0800266
Glenn Kasten3694ec12012-01-27 16:47:15 -0800267 if (cbf != NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 }
270
271 mStatus = NO_ERROR;
272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 mStreamType = streamType;
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800274 mFormat = format;
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700275 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 mChannelCount = channelCount;
277 mSharedBuffer = sharedBuffer;
278 mMuted = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800279 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 mCbf = cbf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 mUserData = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 mLoopCount = 0;
283 mMarkerPosition = 0;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700284 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 mNewPosition = 0;
286 mUpdatePeriod = 0;
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700287 mFlushed = false;
Eric Laurenta553c252009-07-17 12:17:14 -0700288 mFlags = flags;
Marco Nelissenc74b93f2011-08-02 13:33:41 -0700289 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurent7e8626f2011-09-13 15:04:17 -0700290 mRestoreStatus = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 return NO_ERROR;
292}
293
294status_t AudioTrack::initCheck() const
295{
296 return mStatus;
297}
298
299// -------------------------------------------------------------------------
300
301uint32_t AudioTrack::latency() const
302{
303 return mLatency;
304}
305
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800306audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307{
308 return mStreamType;
309}
310
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800311audio_format_t AudioTrack::format() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312{
313 return mFormat;
314}
315
316int AudioTrack::channelCount() const
317{
318 return mChannelCount;
319}
320
321uint32_t AudioTrack::frameCount() const
322{
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700323 return mCblk->frameCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324}
325
Glenn Kastenfaf354d2012-01-11 09:48:27 -0800326size_t AudioTrack::frameSize() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327{
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700328 if (audio_is_linear_pcm(mFormat)) {
Eric Laurentc310dcb2011-06-16 21:30:45 -0700329 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurenta553c252009-07-17 12:17:14 -0700330 } else {
331 return sizeof(uint8_t);
332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333}
334
335sp<IMemory>& AudioTrack::sharedBuffer()
336{
337 return mSharedBuffer;
338}
339
340// -------------------------------------------------------------------------
341
342void AudioTrack::start()
343{
344 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten028ab992011-06-22 16:18:04 -0700345 status_t status = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346
Steve Block71f2cf12011-10-20 11:56:00 +0100347 ALOGV("start %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 if (t != 0) {
349 if (t->exitPending()) {
350 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block3762c312012-01-06 19:20:56 +0000351 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 return;
353 }
354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
356
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800357 AutoMutex lock(mLock);
Eric Laurent421ddc02011-03-07 14:52:59 -0800358 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
359 // while we are accessing the cblk
360 sp <IAudioTrack> audioTrack = mAudioTrack;
361 sp <IMemory> iMem = mCblkMemory;
362 audio_track_cblk_t* cblk = mCblk;
363
Glenn Kastene6810ff2012-01-03 09:42:47 -0800364 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700365 mFlushed = false;
Glenn Kastene6810ff2012-01-03 09:42:47 -0800366 mActive = true;
Eric Laurent421ddc02011-03-07 14:52:59 -0800367 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent913af0b2011-03-17 09:36:51 -0700368 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800369 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
370 cblk->waitTimeMs = 0;
Eric Laurentae29b762011-03-28 18:37:07 -0700371 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800372 pid_t tid;
Eric Laurent059b4be2009-11-09 23:32:22 -0800373 if (t != 0) {
Glenn Kasten86e33622012-02-28 12:30:08 -0800374 t->run("AudioTrack", ANDROID_PRIORITY_AUDIO);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800375 tid = t->getTid(); // pid_t is unknown until run()
376 ALOGV("getTid=%d", tid);
377 if (tid == -1) {
378 tid = 0;
379 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800380 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700381 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
382 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
383 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800384 tid = 0; // not gettid()
Eric Laurent059b4be2009-11-09 23:32:22 -0800385 }
386
Steve Block71f2cf12011-10-20 11:56:00 +0100387 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent421ddc02011-03-07 14:52:59 -0800388 if (!(cblk->flags & CBLK_INVALID_MSK)) {
389 cblk->lock.unlock();
Glenn Kasten6a20b262012-02-02 10:56:47 -0800390 ALOGV("mAudioTrack->start(tid=%d)", tid);
391 status = mAudioTrack->start(tid);
Eric Laurent421ddc02011-03-07 14:52:59 -0800392 cblk->lock.lock();
393 if (status == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700394 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent49f02be2009-11-19 09:00:56 -0800395 }
Eric Laurent059b4be2009-11-09 23:32:22 -0800396 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800397 if (cblk->flags & CBLK_INVALID_MSK) {
398 status = restoreTrack_l(cblk, true);
399 }
400 cblk->lock.unlock();
Eric Laurent059b4be2009-11-09 23:32:22 -0800401 if (status != NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100402 ALOGV("start() failed");
Glenn Kastene6810ff2012-01-03 09:42:47 -0800403 mActive = false;
Eric Laurent059b4be2009-11-09 23:32:22 -0800404 if (t != 0) {
405 t->requestExit();
406 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700407 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
408 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent059b4be2009-11-09 23:32:22 -0800409 }
Eric Laurentbda74692009-11-04 08:27:26 -0800410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413}
414
415void AudioTrack::stop()
416{
417 sp<AudioTrackThread> t = mAudioTrackThread;
418
Steve Block71f2cf12011-10-20 11:56:00 +0100419 ALOGV("stop %p", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800421 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800422 if (mActive) {
423 mActive = false;
Eric Laurentef028272009-04-21 07:56:33 -0700424 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 mAudioTrack->stop();
426 // Cancel loops (If we are in the middle of a loop, playback
427 // would not stop until loopCount reaches 0).
Eric Laurent421ddc02011-03-07 14:52:59 -0800428 setLoop_l(0, 0, 0);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700429 // the playback head position will reset to 0, so if a marker is set, we need
430 // to activate it again
431 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 // Force flush if a shared buffer is used otherwise audioflinger
433 // will not stop before end of buffer is reached.
434 if (mSharedBuffer != 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800435 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
437 if (t != 0) {
438 t->requestExit();
439 } else {
Glenn Kasten99d54432011-06-22 16:15:25 -0700440 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
441 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443 }
444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445}
446
447bool AudioTrack::stopped() const
448{
Glenn Kastene6810ff2012-01-03 09:42:47 -0800449 AutoMutex lock(mLock);
450 return stopped_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451}
452
453void AudioTrack::flush()
454{
Eric Laurent421ddc02011-03-07 14:52:59 -0800455 AutoMutex lock(mLock);
456 flush_l();
457}
458
459// must be called with mLock held
460void AudioTrack::flush_l()
461{
Steve Block71f2cf12011-10-20 11:56:00 +0100462 ALOGV("flush");
Eric Laurenta553c252009-07-17 12:17:14 -0700463
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700464 // clear playback marker and periodic update counter
465 mMarkerPosition = 0;
466 mMarkerReached = false;
467 mUpdatePeriod = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 if (!mActive) {
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700470 mFlushed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 mAudioTrack->flush();
472 // Release AudioTrack callback thread in case it was waiting for new buffers
473 // in AudioTrack::obtainBuffer()
474 mCblk->cv.signal();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476}
477
478void AudioTrack::pause()
479{
Steve Block71f2cf12011-10-20 11:56:00 +0100480 ALOGV("pause");
Eric Laurentf3d6dd02010-11-18 08:40:16 -0800481 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800482 if (mActive) {
483 mActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 mAudioTrack->pause();
485 }
486}
487
488void AudioTrack::mute(bool e)
489{
490 mAudioTrack->mute(e);
491 mMuted = e;
492}
493
494bool AudioTrack::muted() const
495{
496 return mMuted;
497}
498
Eric Laurent65b65452010-06-01 23:49:17 -0700499status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500{
Glenn Kasten1c50a782011-11-30 09:46:04 -0800501 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700502 return BAD_VALUE;
503 }
504
Eric Laurent421ddc02011-03-07 14:52:59 -0800505 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 mVolume[LEFT] = left;
507 mVolume[RIGHT] = right;
508
Glenn Kastenbc4de882012-01-17 14:39:34 -0800509 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurent65b65452010-06-01 23:49:17 -0700510
511 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512}
513
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800514void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515{
Eric Laurent65b65452010-06-01 23:49:17 -0700516 if (left != NULL) {
517 *left = mVolume[LEFT];
518 }
519 if (right != NULL) {
520 *right = mVolume[RIGHT];
521 }
522}
523
Eric Laurent7070b362010-07-16 07:43:46 -0700524status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurent65b65452010-06-01 23:49:17 -0700525{
Steve Block71f2cf12011-10-20 11:56:00 +0100526 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten4790bd82012-01-03 14:22:33 -0800527 if (level < 0.0f || level > 1.0f) {
Eric Laurent65b65452010-06-01 23:49:17 -0700528 return BAD_VALUE;
529 }
Eric Laurent421ddc02011-03-07 14:52:59 -0800530 AutoMutex lock(mLock);
Eric Laurent65b65452010-06-01 23:49:17 -0700531
532 mSendLevel = level;
533
Glenn Kasten4790bd82012-01-03 14:22:33 -0800534 mCblk->setSendLevel(level);
Eric Laurent65b65452010-06-01 23:49:17 -0700535
536 return NO_ERROR;
537}
538
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800539void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurent65b65452010-06-01 23:49:17 -0700540{
541 if (level != NULL) {
542 *level = mSendLevel;
543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544}
545
Eric Laurent88e209d2009-07-07 07:10:45 -0700546status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547{
548 int afSamplingRate;
549
John Grossmand8cf2962012-02-08 16:37:41 -0800550 if (mIsTimed) {
551 return INVALID_OPERATION;
552 }
553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent88e209d2009-07-07 07:10:45 -0700555 return NO_INIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent88e209d2009-07-07 07:10:45 -0700558 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
Eric Laurent421ddc02011-03-07 14:52:59 -0800560 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700561 mCblk->sampleRate = rate;
562 return NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563}
564
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800565uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566{
John Grossmand8cf2962012-02-08 16:37:41 -0800567 if (mIsTimed) {
568 return INVALID_OPERATION;
569 }
570
Eric Laurent421ddc02011-03-07 14:52:59 -0800571 AutoMutex lock(mLock);
Eric Laurent88e209d2009-07-07 07:10:45 -0700572 return mCblk->sampleRate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573}
574
575status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
576{
Eric Laurent421ddc02011-03-07 14:52:59 -0800577 AutoMutex lock(mLock);
578 return setLoop_l(loopStart, loopEnd, loopCount);
579}
580
581// must be called with mLock held
582status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
583{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 audio_track_cblk_t* cblk = mCblk;
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 Mutex::Autolock _l(cblk->lock);
587
588 if (loopCount == 0) {
589 cblk->loopStart = UINT_MAX;
590 cblk->loopEnd = UINT_MAX;
591 cblk->loopCount = 0;
592 mLoopCount = 0;
593 return NO_ERROR;
594 }
595
John Grossmand8cf2962012-02-08 16:37:41 -0800596 if (mIsTimed) {
597 return INVALID_OPERATION;
598 }
599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 if (loopStart >= loopEnd ||
Eric Laurent6667ac32011-03-21 11:49:00 -0700601 loopEnd - loopStart > cblk->frameCount ||
602 cblk->server > loopStart) {
Steve Block3762c312012-01-06 19:20:56 +0000603 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 -0800604 return BAD_VALUE;
605 }
606
Eric Laurent6667ac32011-03-21 11:49:00 -0700607 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block3762c312012-01-06 19:20:56 +0000608 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700609 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 return BAD_VALUE;
Eric Laurenta553c252009-07-17 12:17:14 -0700611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612
613 cblk->loopStart = loopStart;
614 cblk->loopEnd = loopEnd;
615 cblk->loopCount = loopCount;
616 mLoopCount = loopCount;
617
618 return NO_ERROR;
619}
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621status_t AudioTrack::setMarkerPosition(uint32_t marker)
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 mMarkerPosition = marker;
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -0700626 mMarkerReached = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627
628 return NO_ERROR;
629}
630
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800631status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800633 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
635 *marker = mMarkerPosition;
636
637 return NO_ERROR;
638}
639
640status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
641{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800642 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
644 uint32_t curPosition;
645 getPosition(&curPosition);
646 mNewPosition = curPosition + updatePeriod;
647 mUpdatePeriod = updatePeriod;
648
649 return NO_ERROR;
650}
651
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800652status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800654 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655
656 *updatePeriod = mUpdatePeriod;
657
658 return NO_ERROR;
659}
660
661status_t AudioTrack::setPosition(uint32_t position)
662{
John Grossmand8cf2962012-02-08 16:37:41 -0800663 if (mIsTimed) return INVALID_OPERATION;
664
Eric Laurent421ddc02011-03-07 14:52:59 -0800665 AutoMutex lock(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666
Glenn Kastene6810ff2012-01-03 09:42:47 -0800667 if (!stopped_l()) return INVALID_OPERATION;
668
669 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670
671 if (position > mCblk->user) return BAD_VALUE;
672
673 mCblk->server = position;
Eric Laurentae29b762011-03-28 18:37:07 -0700674 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurenta553c252009-07-17 12:17:14 -0700675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 return NO_ERROR;
677}
678
679status_t AudioTrack::getPosition(uint32_t *position)
680{
Glenn Kasten3694ec12012-01-27 16:47:15 -0800681 if (position == NULL) return BAD_VALUE;
Eric Laurent421ddc02011-03-07 14:52:59 -0800682 AutoMutex lock(mLock);
Jean-Michel Trivi22cb2042011-08-25 16:47:23 -0700683 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684
685 return NO_ERROR;
686}
687
688status_t AudioTrack::reload()
689{
Eric Laurent421ddc02011-03-07 14:52:59 -0800690 AutoMutex lock(mLock);
691
Glenn Kastene6810ff2012-01-03 09:42:47 -0800692 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurenta553c252009-07-17 12:17:14 -0700693
Eric Laurent421ddc02011-03-07 14:52:59 -0800694 flush_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700696 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697
698 return NO_ERROR;
699}
700
Eric Laurenta553c252009-07-17 12:17:14 -0700701audio_io_handle_t AudioTrack::getOutput()
702{
Eric Laurent421ddc02011-03-07 14:52:59 -0800703 AutoMutex lock(mLock);
704 return getOutput_l();
705}
706
707// must be called with mLock held
708audio_io_handle_t AudioTrack::getOutput_l()
709{
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800710 return AudioSystem::getOutput(mStreamType,
Glenn Kasten28b269f2012-03-07 09:15:37 -0800711 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurenta553c252009-07-17 12:17:14 -0700712}
713
Glenn Kasten7c2e37d2012-01-04 12:41:44 -0800714int AudioTrack::getSessionId() const
Eric Laurent65b65452010-06-01 23:49:17 -0700715{
716 return mSessionId;
717}
718
719status_t AudioTrack::attachAuxEffect(int effectId)
720{
Steve Block71f2cf12011-10-20 11:56:00 +0100721 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -0700722 status_t status = mAudioTrack->attachAuxEffect(effectId);
723 if (status == NO_ERROR) {
724 mAuxEffectId = effectId;
725 }
726 return status;
Eric Laurent65b65452010-06-01 23:49:17 -0700727}
728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729// -------------------------------------------------------------------------
730
Eric Laurent421ddc02011-03-07 14:52:59 -0800731// must be called with mLock held
732status_t AudioTrack::createTrack_l(
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800733 audio_stream_type_t streamType,
Eric Laurentbda74692009-11-04 08:27:26 -0800734 uint32_t sampleRate,
Glenn Kasten1c5a89d2012-01-04 09:36:37 -0800735 audio_format_t format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700736 uint32_t channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800737 int frameCount,
Glenn Kasten28b269f2012-03-07 09:15:37 -0800738 audio_policy_output_flags_t flags,
Eric Laurentbda74692009-11-04 08:27:26 -0800739 const sp<IMemory>& sharedBuffer,
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700740 audio_io_handle_t output,
741 bool enforceFrameCount)
Eric Laurentbda74692009-11-04 08:27:26 -0800742{
743 status_t status;
744 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
745 if (audioFlinger == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000746 ALOGE("Could not get audioflinger");
Eric Laurentbda74692009-11-04 08:27:26 -0800747 return NO_INIT;
748 }
749
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700750 int afSampleRate;
751 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
752 return NO_INIT;
753 }
754 int afFrameCount;
755 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
756 return NO_INIT;
757 }
758 uint32_t afLatency;
759 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
760 return NO_INIT;
761 }
762
763 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700764 if (!audio_is_linear_pcm(format)) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700765 if (sharedBuffer != 0) {
766 frameCount = sharedBuffer->size();
767 }
768 } else {
769 // Ensure that buffer depth covers at least audio hardware latency
770 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
771 if (minBufCount < 2) minBufCount = 2;
772
773 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
774
775 if (sharedBuffer == 0) {
776 if (frameCount == 0) {
777 frameCount = minFrameCount;
778 }
779 if (mNotificationFramesAct == 0) {
780 mNotificationFramesAct = frameCount/2;
781 }
782 // Make sure that application is notified with sufficient margin
783 // before underrun
784 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
785 mNotificationFramesAct = frameCount/2;
786 }
787 if (frameCount < minFrameCount) {
Eric Laurentbd6b74c2012-03-05 17:06:40 -0800788 ALOGW_IF(enforceFrameCount, "Minimum buffer size corrected from %d to %d",
789 frameCount, minFrameCount);
790 frameCount = minFrameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700791 }
792 } else {
Glenn Kastenb3db2132012-01-19 08:59:58 -0800793 // Ensure that buffer alignment matches channelCount
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700794 int channelCount = popcount(channelMask);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700795 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000796 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700797 return BAD_VALUE;
798 }
799 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
800 }
801 }
802
Eric Laurentbda74692009-11-04 08:27:26 -0800803 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
804 streamType,
805 sampleRate,
806 format,
Jean-Michel Trivi54392232011-05-24 15:53:33 -0700807 channelMask,
Eric Laurentbda74692009-11-04 08:27:26 -0800808 frameCount,
809 ((uint16_t)flags) << 16,
810 sharedBuffer,
811 output,
John Grossmand8cf2962012-02-08 16:37:41 -0800812 mIsTimed,
Eric Laurent65b65452010-06-01 23:49:17 -0700813 &mSessionId,
Eric Laurentbda74692009-11-04 08:27:26 -0800814 &status);
815
816 if (track == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000817 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurentbda74692009-11-04 08:27:26 -0800818 return status;
819 }
820 sp<IMemory> cblk = track->getCblk();
821 if (cblk == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000822 ALOGE("Could not get control block");
Eric Laurentbda74692009-11-04 08:27:26 -0800823 return NO_INIT;
824 }
Eric Laurentbda74692009-11-04 08:27:26 -0800825 mAudioTrack = track;
Eric Laurentbda74692009-11-04 08:27:26 -0800826 mCblkMemory = cblk;
827 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurentae29b762011-03-28 18:37:07 -0700828 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurentbda74692009-11-04 08:27:26 -0800829 if (sharedBuffer == 0) {
830 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
831 } else {
832 mCblk->buffers = sharedBuffer->pointer();
833 // Force buffer full condition as data is already present in shared memory
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700834 mCblk->stepUser(mCblk->frameCount);
Eric Laurentbda74692009-11-04 08:27:26 -0800835 }
836
Glenn Kastenbc4de882012-01-17 14:39:34 -0800837 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten4790bd82012-01-03 14:22:33 -0800838 mCblk->setSendLevel(mSendLevel);
Eric Laurent7070b362010-07-16 07:43:46 -0700839 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent49f02be2009-11-19 09:00:56 -0800840 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
841 mCblk->waitTimeMs = 0;
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700842 mRemainingFrames = mNotificationFramesAct;
843 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurentbda74692009-11-04 08:27:26 -0800844 return NO_ERROR;
845}
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
848{
Eric Laurent421ddc02011-03-07 14:52:59 -0800849 AutoMutex lock(mLock);
Glenn Kastene6810ff2012-01-03 09:42:47 -0800850 bool active;
Glenn Kasten028ab992011-06-22 16:18:04 -0700851 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 audio_track_cblk_t* cblk = mCblk;
853 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurentef028272009-04-21 07:56:33 -0700854 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
856 audioBuffer->frameCount = 0;
857 audioBuffer->size = 0;
858
859 uint32_t framesAvail = cblk->framesAvailable();
860
Eric Laurent6667ac32011-03-21 11:49:00 -0700861 cblk->lock.lock();
862 if (cblk->flags & CBLK_INVALID_MSK) {
863 goto create_new_track;
864 }
865 cblk->lock.unlock();
866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 if (framesAvail == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800868 cblk->lock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 goto start_loop_here;
870 while (framesAvail == 0) {
871 active = mActive;
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800872 if (CC_UNLIKELY(!active)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100873 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurentbda74692009-11-04 08:27:26 -0800874 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 return NO_MORE_BUFFERS;
876 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800877 if (CC_UNLIKELY(!waitCount)) {
Eric Laurentbda74692009-11-04 08:27:26 -0800878 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 return WOULD_BLOCK;
Eric Laurentbda74692009-11-04 08:27:26 -0800880 }
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700881 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800882 mLock.unlock();
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700883 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700884 cblk->lock.unlock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800885 mLock.lock();
Glenn Kastene6810ff2012-01-03 09:42:47 -0800886 if (!mActive) {
Eric Laurent421ddc02011-03-07 14:52:59 -0800887 return status_t(STOPPED);
888 }
889 cblk->lock.lock();
890 }
891
892 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -0700893 goto create_new_track;
894 }
Glenn Kastene80a4cc2011-12-15 09:51:17 -0800895 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurentef028272009-04-21 07:56:33 -0700896 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
898 // timing out when a loop has been set and we have already written upto loop end
899 // is a normal condition: no need to wake AudioFlinger up.
900 if (cblk->user < cblk->loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +0000901 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurenta553c252009-07-17 12:17:14 -0700903 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 cblk->lock.unlock();
Glenn Kasten6a20b262012-02-02 10:56:47 -0800905 result = mAudioTrack->start(0); // callback thread hasn't changed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 cblk->lock.lock();
Eric Laurent421ddc02011-03-07 14:52:59 -0800907 if (result == DEAD_OBJECT) {
Eric Laurentae29b762011-03-28 18:37:07 -0700908 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -0800909create_new_track:
910 result = restoreTrack_l(cblk, false);
911 }
912 if (result != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000913 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -0800914 cblk->lock.unlock();
915 return result;
916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
918 cblk->waitTimeMs = 0;
919 }
Eric Laurenta553c252009-07-17 12:17:14 -0700920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 if (--waitCount == 0) {
Eric Laurentbda74692009-11-04 08:27:26 -0800922 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 return TIMED_OUT;
924 }
925 }
926 // read the server count again
927 start_loop_here:
928 framesAvail = cblk->framesAvailable_l();
929 }
Eric Laurentbda74692009-11-04 08:27:26 -0800930 cblk->lock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 }
932
Eric Laurent4712baa2010-09-30 16:12:31 -0700933 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent421ddc02011-03-07 14:52:59 -0800934 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurentae29b762011-03-28 18:37:07 -0700935 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +0000936 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Glenn Kasten6a20b262012-02-02 10:56:47 -0800937 mAudioTrack->start(0); // callback thread hasn't changed
Eric Laurent4712baa2010-09-30 16:12:31 -0700938 }
939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 cblk->waitTimeMs = 0;
Eric Laurenta553c252009-07-17 12:17:14 -0700941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 if (framesReq > framesAvail) {
943 framesReq = framesAvail;
944 }
945
946 uint32_t u = cblk->user;
947 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
948
949 if (u + framesReq > bufferEnd) {
950 framesReq = bufferEnd - u;
951 }
952
Eric Laurenta553c252009-07-17 12:17:14 -0700953 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
954 audioBuffer->channelCount = mChannelCount;
955 audioBuffer->frameCount = framesReq;
956 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700957 if (audio_is_linear_pcm(mFormat)) {
958 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurenta553c252009-07-17 12:17:14 -0700959 } else {
960 audioBuffer->format = mFormat;
961 }
962 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 active = mActive;
964 return active ? status_t(NO_ERROR) : status_t(STOPPED);
965}
966
967void AudioTrack::releaseBuffer(Buffer* audioBuffer)
968{
Eric Laurent421ddc02011-03-07 14:52:59 -0800969 AutoMutex lock(mLock);
970 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971}
972
973// -------------------------------------------------------------------------
974
975ssize_t AudioTrack::write(const void* buffer, size_t userSize)
976{
977
978 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossmand8cf2962012-02-08 16:37:41 -0800979 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980
981 if (ssize_t(userSize) < 0) {
Glenn Kastenb3db2132012-01-19 08:59:58 -0800982 // Sanity-check: user is most-likely passing an error code, and it would
983 // make the return value ambiguous (actualSize vs error).
Steve Block3762c312012-01-06 19:20:56 +0000984 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 buffer, userSize, userSize);
986 return BAD_VALUE;
987 }
988
Steve Block71f2cf12011-10-20 11:56:00 +0100989 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
Eric Laurent421ddc02011-03-07 14:52:59 -0800991 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
992 // while we are accessing the cblk
993 mLock.lock();
994 sp <IAudioTrack> audioTrack = mAudioTrack;
995 sp <IMemory> iMem = mCblkMemory;
996 mLock.unlock();
997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 ssize_t written = 0;
999 const int8_t *src = (const int8_t *)buffer;
1000 Buffer audioBuffer;
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001001 size_t frameSz = frameSize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002
1003 do {
Eric Laurent913af0b2011-03-17 09:36:51 -07001004 audioBuffer.frameCount = userSize/frameSz;
Eric Laurenta553c252009-07-17 12:17:14 -07001005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 status_t err = obtainBuffer(&audioBuffer, -1);
1007 if (err < 0) {
1008 // out of buffers, return #bytes written
1009 if (err == status_t(NO_MORE_BUFFERS))
1010 break;
1011 return ssize_t(err);
1012 }
1013
1014 size_t toWrite;
Eric Laurenta553c252009-07-17 12:17:14 -07001015
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001016 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 // Divide capacity by 2 to take expansion into account
1018 toWrite = audioBuffer.size>>1;
Glenn Kastencc2302d2012-01-11 09:52:19 -08001019 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent28ad42b2009-08-04 10:42:26 -07001020 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 toWrite = audioBuffer.size;
1022 memcpy(audioBuffer.i8, src, toWrite);
1023 src += toWrite;
1024 }
1025 userSize -= toWrite;
1026 written += toWrite;
1027
1028 releaseBuffer(&audioBuffer);
Eric Laurent913af0b2011-03-17 09:36:51 -07001029 } while (userSize >= frameSz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030
1031 return written;
1032}
1033
1034// -------------------------------------------------------------------------
1035
John Grossmand8cf2962012-02-08 16:37:41 -08001036TimedAudioTrack::TimedAudioTrack() {
1037 mIsTimed = true;
1038}
1039
1040status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1041{
1042 status_t result = UNKNOWN_ERROR;
1043
1044 // If the track is not invalid already, try to allocate a buffer. alloc
1045 // fails indicating that the server is dead, flag the track as invalid so
1046 // we can attempt to restore in in just a bit.
1047 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1048 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1049 if (result == DEAD_OBJECT) {
1050 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1051 }
1052 }
1053
1054 // If the track is invalid at this point, attempt to restore it. and try the
1055 // allocation one more time.
1056 if (mCblk->flags & CBLK_INVALID_MSK) {
1057 mCblk->lock.lock();
1058 result = restoreTrack_l(mCblk, false);
1059 mCblk->lock.unlock();
1060
1061 if (result == OK)
1062 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1063 }
1064
1065 return result;
1066}
1067
1068status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1069 int64_t pts)
1070{
1071 // restart track if it was disabled by audioflinger due to previous underrun
1072 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1073 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1074 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1075 mAudioTrack->start(0);
1076 }
1077
1078 return mAudioTrack->queueTimedBuffer(buffer, pts);
1079}
1080
1081status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1082 TargetTimeline target)
1083{
1084 return mAudioTrack->setMediaTimeTransform(xform, target);
1085}
1086
1087// -------------------------------------------------------------------------
1088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1090{
1091 Buffer audioBuffer;
1092 uint32_t frames;
1093 size_t writtenSize;
1094
Eric Laurent421ddc02011-03-07 14:52:59 -08001095 mLock.lock();
1096 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1097 // while we are accessing the cblk
1098 sp <IAudioTrack> audioTrack = mAudioTrack;
1099 sp <IMemory> iMem = mCblkMemory;
1100 audio_track_cblk_t* cblk = mCblk;
Glenn Kastene6810ff2012-01-03 09:42:47 -08001101 bool active = mActive;
Eric Laurent421ddc02011-03-07 14:52:59 -08001102 mLock.unlock();
1103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 // Manage underrun callback
Glenn Kastene6810ff2012-01-03 09:42:47 -08001105 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block71f2cf12011-10-20 11:56:00 +01001106 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurentae29b762011-03-28 18:37:07 -07001107 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent421ddc02011-03-07 14:52:59 -08001109 if (cblk->server == cblk->frameCount) {
Eric Laurenta553c252009-07-17 12:17:14 -07001110 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 if (mSharedBuffer != 0) return false;
1113 }
1114 }
Eric Laurenta553c252009-07-17 12:17:14 -07001115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 // Manage loop end callback
Eric Laurent421ddc02011-03-07 14:52:59 -08001117 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 int loopCount = -1;
1119 mLoopCount--;
1120 if (mLoopCount >= 0) loopCount = mLoopCount;
1121
1122 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1123 }
1124
1125 // Manage marker callback
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001126 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001127 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi4a5c1a72009-03-24 18:11:07 -07001129 mMarkerReached = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131 }
1132
1133 // Manage new position callback
Eric Laurenta553c252009-07-17 12:17:14 -07001134 if (mUpdatePeriod > 0) {
Eric Laurent421ddc02011-03-07 14:52:59 -08001135 while (cblk->server >= mNewPosition) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1137 mNewPosition += mUpdatePeriod;
1138 }
1139 }
1140
1141 // If Shared buffer is used, no data is requested from client.
1142 if (mSharedBuffer != 0) {
1143 frames = 0;
1144 } else {
1145 frames = mRemainingFrames;
1146 }
1147
Glenn Kastenb3db2132012-01-19 08:59:58 -08001148 // See description of waitCount parameter at declaration of obtainBuffer().
1149 // The logic below prevents us from being stuck below at obtainBuffer()
1150 // not being able to handle timed events (position, markers, loops).
Eric Laurentf1d360a2011-09-07 11:13:23 -07001151 int32_t waitCount = -1;
1152 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1153 waitCount = 1;
1154 }
1155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 do {
1157
1158 audioBuffer.frameCount = frames;
Eric Laurenta553c252009-07-17 12:17:14 -07001159
Eric Laurentf1d360a2011-09-07 11:13:23 -07001160 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 if (err < NO_ERROR) {
1162 if (err != TIMED_OUT) {
Steve Block3762c312012-01-06 19:20:56 +00001163 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 -08001164 return false;
1165 }
1166 break;
1167 }
1168 if (err == status_t(STOPPED)) return false;
1169
1170 // Divide buffer size by 2 to take into account the expansion
1171 // due to 8 to 16 bit conversion: the callback must fill only half
1172 // of the destination buffer
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001173 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 audioBuffer.size >>= 1;
1175 }
1176
1177 size_t reqSize = audioBuffer.size;
1178 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1179 writtenSize = audioBuffer.size;
1180
1181 // Sanity check on returned size
The Android Open Source Project4df24232009-03-05 14:34:35 -08001182 if (ssize_t(writtenSize) <= 0) {
1183 // The callback is done filling buffers
1184 // Keep this thread going to handle timed events and
1185 // still try to get more data in intervals of WAIT_PERIOD_MS
1186 // but don't just loop and block the CPU, so wait
1187 usleep(WAIT_PERIOD_MS*1000);
1188 break;
1189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 if (writtenSize > reqSize) writtenSize = reqSize;
1191
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001192 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kastencc2302d2012-01-11 09:52:19 -08001193 // 8 to 16 bit conversion, note that source and destination are the same address
1194 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 writtenSize <<= 1;
1196 }
1197
1198 audioBuffer.size = writtenSize;
Eric Laurenta553c252009-07-17 12:17:14 -07001199 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenfaf354d2012-01-11 09:48:27 -08001200 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurenta553c252009-07-17 12:17:14 -07001201 // 16 bit.
1202 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 frames -= audioBuffer.frameCount;
1205
1206 releaseBuffer(&audioBuffer);
1207 }
1208 while (frames);
1209
1210 if (frames == 0) {
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001211 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } else {
1213 mRemainingFrames = frames;
1214 }
1215 return true;
1216}
1217
Eric Laurent421ddc02011-03-07 14:52:59 -08001218// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1219// the IAudioTrack and IMemory in case they are recreated here.
1220// If the IAudioTrack is successfully restored, the cblk pointer is updated
1221status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1222{
1223 status_t result;
1224
Eric Laurentae29b762011-03-28 18:37:07 -07001225 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001226 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Eric Laurent7e8626f2011-09-13 15:04:17 -07001227 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001228
Eric Laurent421ddc02011-03-07 14:52:59 -08001229 // signal old cblk condition so that other threads waiting for available buffers stop
1230 // waiting now
1231 cblk->cv.broadcast();
1232 cblk->lock.unlock();
1233
Eric Laurent05ce0942011-08-30 10:18:54 -07001234 // refresh the audio configuration cache in this process to make sure we get new
1235 // output parameters in getOutput_l() and createTrack_l()
1236 AudioSystem::clearAudioConfigCache();
1237
Eric Laurent421ddc02011-03-07 14:52:59 -08001238 // if the new IAudioTrack is created, createTrack_l() will modify the
1239 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1240 // It will also delete the strong references on previous IAudioTrack and IMemory
1241 result = createTrack_l(mStreamType,
1242 cblk->sampleRate,
1243 mFormat,
Jean-Michel Trivi54392232011-05-24 15:53:33 -07001244 mChannelMask,
Eric Laurent421ddc02011-03-07 14:52:59 -08001245 mFrameCount,
1246 mFlags,
1247 mSharedBuffer,
1248 getOutput_l(),
1249 false);
1250
1251 if (result == NO_ERROR) {
Eric Laurentb0808f92011-09-06 12:36:15 -07001252 uint32_t user = cblk->user;
1253 uint32_t server = cblk->server;
Eric Laurent6667ac32011-03-21 11:49:00 -07001254 // restore write index and set other indexes to reflect empty buffer status
Eric Laurentb0808f92011-09-06 12:36:15 -07001255 mCblk->user = user;
1256 mCblk->server = user;
1257 mCblk->userBase = user;
1258 mCblk->serverBase = user;
Eric Laurent6667ac32011-03-21 11:49:00 -07001259 // restore loop: this is not guaranteed to succeed if new frame count is not
1260 // compatible with loop length
1261 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent421ddc02011-03-07 14:52:59 -08001262 if (!fromStart) {
1263 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurentb0808f92011-09-06 12:36:15 -07001264 // Make sure that a client relying on callback events indicating underrun or
1265 // the actual amount of audio frames played (e.g SoundPool) receives them.
1266 if (mSharedBuffer == 0) {
1267 uint32_t frames = 0;
1268 if (user > server) {
1269 frames = ((user - server) > mCblk->frameCount) ?
1270 mCblk->frameCount : (user - server);
1271 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1272 }
1273 // restart playback even if buffer is not completely filled.
1274 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1275 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1276 // the client
1277 mCblk->stepUser(frames);
1278 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001279 }
Eric Laurent6667ac32011-03-21 11:49:00 -07001280 if (mActive) {
Glenn Kasten6a20b262012-02-02 10:56:47 -08001281 result = mAudioTrack->start(0); // callback thread hasn't changed
Steve Block8564c8d2012-01-05 23:22:43 +00001282 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent6667ac32011-03-21 11:49:00 -07001283 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001284 if (fromStart && result == NO_ERROR) {
1285 mNewPosition = mCblk->server + mUpdatePeriod;
1286 }
1287 }
1288 if (result != NO_ERROR) {
Eric Laurent7e8626f2011-09-13 15:04:17 -07001289 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block8564c8d2012-01-05 23:22:43 +00001290 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent421ddc02011-03-07 14:52:59 -08001291 }
Eric Laurent7e8626f2011-09-13 15:04:17 -07001292 mRestoreStatus = result;
Eric Laurent421ddc02011-03-07 14:52:59 -08001293 // signal old cblk condition for other threads waiting for restore completion
Eric Laurentae29b762011-03-28 18:37:07 -07001294 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent421ddc02011-03-07 14:52:59 -08001295 cblk->cv.broadcast();
Eric Laurent421ddc02011-03-07 14:52:59 -08001296 } else {
1297 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block8564c8d2012-01-05 23:22:43 +00001298 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001299 mLock.unlock();
1300 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurent7e8626f2011-09-13 15:04:17 -07001301 if (result == NO_ERROR) {
1302 result = mRestoreStatus;
1303 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001304 cblk->lock.unlock();
1305 mLock.lock();
1306 } else {
Steve Block8564c8d2012-01-05 23:22:43 +00001307 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurent7e8626f2011-09-13 15:04:17 -07001308 result = mRestoreStatus;
Eric Laurent421ddc02011-03-07 14:52:59 -08001309 cblk->lock.unlock();
1310 }
Eric Laurent421ddc02011-03-07 14:52:59 -08001311 }
Steve Block71f2cf12011-10-20 11:56:00 +01001312 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Eric Laurent421ddc02011-03-07 14:52:59 -08001313 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
1314
1315 if (result == NO_ERROR) {
1316 // from now on we switch to the newly created cblk
1317 cblk = mCblk;
1318 }
1319 cblk->lock.lock();
1320
Steve Block8564c8d2012-01-05 23:22:43 +00001321 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent421ddc02011-03-07 14:52:59 -08001322
1323 return result;
1324}
1325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1327{
1328
1329 const size_t SIZE = 256;
1330 char buffer[SIZE];
1331 String8 result;
1332
1333 result.append(" AudioTrack::dump\n");
1334 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1335 result.append(buffer);
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001336 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 -08001337 result.append(buffer);
Eric Laurent88e209d2009-07-07 07:10:45 -07001338 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 -08001339 result.append(buffer);
1340 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1341 result.append(buffer);
1342 ::write(fd, result.string(), result.size());
1343 return NO_ERROR;
1344}
1345
1346// =========================================================================
1347
1348AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1349 : Thread(bCanCallJava), mReceiver(receiver)
1350{
1351}
1352
1353bool AudioTrack::AudioTrackThread::threadLoop()
1354{
1355 return mReceiver.processAudioBuffer(this);
1356}
1357
1358status_t AudioTrack::AudioTrackThread::readyToRun()
1359{
1360 return NO_ERROR;
1361}
1362
1363void AudioTrack::AudioTrackThread::onFirstRef()
1364{
1365}
1366
1367// =========================================================================
1368
Eric Laurentae29b762011-03-28 18:37:07 -07001369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopiana729f972010-03-19 16:14:13 -07001371 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kasten3694ec12012-01-27 16:47:15 -08001372 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kastenbc4de882012-01-17 14:39:34 -08001373 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten4790bd82012-01-03 14:22:33 -08001374 mSendLevel(0), flags(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375{
1376}
1377
1378uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1379{
Glenn Kastendb298a42011-12-13 11:45:07 -08001380 uint32_t u = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
1382 u += frameCount;
1383 // Ensure that user is never ahead of server for AudioRecord
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001384 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1386 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1387 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1388 }
Glenn Kastendb298a42011-12-13 11:45:07 -08001389 } else if (u > server) {
Steve Block8564c8d2012-01-05 23:22:43 +00001390 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001391 u = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393
1394 if (u >= userBase + this->frameCount) {
1395 userBase += this->frameCount;
1396 }
1397
Glenn Kastendb298a42011-12-13 11:45:07 -08001398 user = u;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399
1400 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent913af0b2011-03-17 09:36:51 -07001401 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurentae29b762011-03-28 18:37:07 -07001402 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent913af0b2011-03-17 09:36:51 -07001403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404
1405 return u;
1406}
1407
1408bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1409{
Eric Laurentae29b762011-03-28 18:37:07 -07001410 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001411 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 return false;
1413 }
1414
Glenn Kastendb298a42011-12-13 11:45:07 -08001415 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416
1417 s += frameCount;
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001418 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 // Mark that we have read the first buffer so that next time stepUser() is called
1420 // we switch to normal obtainBuffer() timeout period
1421 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurentbda74692009-11-04 08:27:26 -08001422 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurenta553c252009-07-17 12:17:14 -07001423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 // It is possible that we receive a flush()
1425 // while the mixer is processing a block: in this case,
1426 // stepServer() is called After the flush() has reset u & s and
1427 // we have s > u
Glenn Kastendb298a42011-12-13 11:45:07 -08001428 if (s > user) {
Steve Block8564c8d2012-01-05 23:22:43 +00001429 ALOGW("stepServer occurred after track reset");
Glenn Kastendb298a42011-12-13 11:45:07 -08001430 s = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432 }
1433
1434 if (s >= loopEnd) {
Steve Block8564c8d2012-01-05 23:22:43 +00001435 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 s = loopStart;
1437 if (--loopCount == 0) {
1438 loopEnd = UINT_MAX;
1439 loopStart = UINT_MAX;
1440 }
1441 }
1442 if (s >= serverBase + this->frameCount) {
1443 serverBase += this->frameCount;
1444 }
1445
Glenn Kastendb298a42011-12-13 11:45:07 -08001446 server = s;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447
Eric Laurent421ddc02011-03-07 14:52:59 -08001448 if (!(flags & CBLK_INVALID_MSK)) {
1449 cv.signal();
1450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 lock.unlock();
1452 return true;
1453}
1454
1455void* audio_track_cblk_t::buffer(uint32_t offset) const
1456{
Glenn Kastendb298a42011-12-13 11:45:07 -08001457 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458}
1459
1460uint32_t audio_track_cblk_t::framesAvailable()
1461{
1462 Mutex::Autolock _l(lock);
1463 return framesAvailable_l();
1464}
1465
1466uint32_t audio_track_cblk_t::framesAvailable_l()
1467{
Glenn Kastendb298a42011-12-13 11:45:07 -08001468 uint32_t u = user;
1469 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001471 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 uint32_t limit = (s < loopStart) ? s : loopStart;
1473 return limit + frameCount - u;
1474 } else {
1475 return frameCount + u - s;
1476 }
1477}
1478
1479uint32_t audio_track_cblk_t::framesReady()
1480{
Glenn Kastendb298a42011-12-13 11:45:07 -08001481 uint32_t u = user;
1482 uint32_t s = server;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483
Eric Laurenteb8f850d2010-05-14 03:26:45 -07001484 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 if (u < loopEnd) {
1486 return u - s;
1487 } else {
Eric Laurentae29b762011-03-28 18:37:07 -07001488 // do not block on mutex shared with client on AudioFlinger side
1489 if (!tryLock()) {
Steve Block8564c8d2012-01-05 23:22:43 +00001490 ALOGW("framesReady() could not lock cblk");
Eric Laurentae29b762011-03-28 18:37:07 -07001491 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 }
Eric Laurentae29b762011-03-28 18:37:07 -07001493 uint32_t frames = UINT_MAX;
1494 if (loopCount >= 0) {
1495 frames = (loopEnd - loopStart)*loopCount + u - s;
1496 }
1497 lock.unlock();
1498 return frames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 }
1500 } else {
1501 return s - u;
1502 }
1503}
1504
Eric Laurentae29b762011-03-28 18:37:07 -07001505bool audio_track_cblk_t::tryLock()
1506{
1507 // the code below simulates lock-with-timeout
1508 // we MUST do this to protect the AudioFlinger server
1509 // as this lock is shared with the client.
1510 status_t err;
1511
1512 err = lock.tryLock();
1513 if (err == -EBUSY) { // just wait a bit
1514 usleep(1000);
1515 err = lock.tryLock();
1516 }
1517 if (err != NO_ERROR) {
1518 // probably, the client just died.
1519 return false;
1520 }
1521 return true;
1522}
1523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524// -------------------------------------------------------------------------
1525
1526}; // namespace android