blob: 9f087c21789497a5fe9597718dfa1a30c1b9fc9c [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-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 Agopian75624082009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Glenn Kasten511754b2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
53 int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 uint32_t sampleRate)
56{
Glenn Kasten04cd0182012-06-25 11:49:27 -070057 if (frameCount == NULL) return BAD_VALUE;
58
59 // default to 0 in case of error
60 *frameCount = 0;
61
Glenn Kastene0fa4672012-04-24 14:35:14 -070062 // FIXME merge with similar code in createTrack_l(), except we're missing
63 // some information here that is available in createTrack_l():
64 // audio_io_handle_t output
65 // audio_format_t format
66 // audio_channel_mask_t channelMask
67 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 int afSampleRate;
69 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
70 return NO_INIT;
71 }
72 int afFrameCount;
73 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 uint32_t afLatency;
77 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80
81 // Ensure that buffer depth covers at least audio hardware latency
82 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
83 if (minBufCount < 2) minBufCount = 2;
84
85 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070086 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080087 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
88 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080089 return NO_ERROR;
90}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92// ---------------------------------------------------------------------------
93
94AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070095 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080096 mIsTimed(false),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070098 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099{
100}
101
102AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800103 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800105 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700106 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700108 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 callback_t cbf,
110 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700111 int notificationFrames,
112 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700113 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800114 mIsTimed(false),
115 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700116 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700118 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700119 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800120 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121}
122
Andreas Huberc8139852012-01-18 10:51:55 -0800123AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800124 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800126 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700127 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800128 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700129 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 callback_t cbf,
131 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700132 int notificationFrames,
133 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700134 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800135 mIsTimed(false),
136 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700137 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700139 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800140 0 /*frameCount*/, flags, cbf, user, notificationFrames,
141 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142}
143
144AudioTrack::~AudioTrack()
145{
Steve Block3856b092011-10-20 11:56:00 +0100146 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-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) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800154 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 mAudioTrackThread->requestExitAndWait();
156 mAudioTrackThread.clear();
157 }
158 mAudioTrack.clear();
159 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700160 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162}
163
164status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800165 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800167 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700168 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700170 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 callback_t cbf,
172 void* user,
173 int notificationFrames,
174 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700175 bool threadCanCallJava,
176 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
178
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700179 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
180 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181
Eric Laurent1a9ed112012-03-20 18:36:01 -0700182 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
183
Eric Laurent1703cdf2011-03-07 14:52:59 -0800184 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700185 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000186 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 return INVALID_OPERATION;
188 }
189
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700191 if (streamType == AUDIO_STREAM_DEFAULT) {
192 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700194
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700196 int afSampleRate;
197 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
198 return NO_INIT;
199 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 sampleRate = afSampleRate;
201 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800204 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700205 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700207 if (channelMask == 0) {
208 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 }
210
211 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000213 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214 return BAD_VALUE;
215 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700216
Glenn Kastene0fa4672012-04-24 14:35:14 -0700217 // AudioFlinger does not currently support 8-bit data in shared memory
218 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
219 ALOGE("8-bit data in shared memory is not supported");
220 return BAD_VALUE;
221 }
222
Eric Laurentc2f1f072009-07-17 12:17:14 -0700223 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700225 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800226 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700227 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700228 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700229 // only allow deep buffering for music stream type
230 if (streamType != AUDIO_STREAM_MUSIC) {
231 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
232 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700233
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700234 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700235 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 return BAD_VALUE;
237 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700238 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700239
Dima Zavinfce7a472011-04-19 22:30:36 -0700240 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800241 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800242 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800243 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700244
245 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000246 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 return BAD_VALUE;
248 }
249
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800250 mVolume[LEFT] = 1.0f;
251 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800252 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700253 mFrameCount = frameCount;
254 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700255 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700256 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700257 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700258 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700259
Glenn Kastena997e7a2012-08-07 09:44:19 -0700260 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700261 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700262 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
263 }
264
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800265 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800266 status_t status = createTrack_l(streamType,
267 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800268 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700269 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800270 frameCount,
271 flags,
272 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700273 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800274
Glenn Kastena997e7a2012-08-07 09:44:19 -0700275 if (status != NO_ERROR) {
276 if (mAudioTrackThread != 0) {
277 mAudioTrackThread->requestExit();
278 mAudioTrackThread.clear();
279 }
280 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700281 }
282
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283 mStatus = NO_ERROR;
284
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800286 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700287 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 mChannelCount = channelCount;
289 mSharedBuffer = sharedBuffer;
290 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800291 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800293 mLoopCount = 0;
294 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700295 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 mNewPosition = 0;
297 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700298 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700299 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700300 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 return NO_ERROR;
302}
303
304status_t AudioTrack::initCheck() const
305{
306 return mStatus;
307}
308
309// -------------------------------------------------------------------------
310
311uint32_t AudioTrack::latency() const
312{
313 return mLatency;
314}
315
Glenn Kastenfff6d712012-01-12 16:38:12 -0800316audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317{
318 return mStreamType;
319}
320
Glenn Kastene1c39622012-01-04 09:36:37 -0800321audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322{
323 return mFormat;
324}
325
326int AudioTrack::channelCount() const
327{
328 return mChannelCount;
329}
330
331uint32_t AudioTrack::frameCount() const
332{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700333 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334}
335
Glenn Kastenb9980652012-01-11 09:48:27 -0800336size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337{
Dima Zavinfce7a472011-04-19 22:30:36 -0700338 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700339 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700340 } else {
341 return sizeof(uint8_t);
342 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343}
344
345sp<IMemory>& AudioTrack::sharedBuffer()
346{
347 return mSharedBuffer;
348}
349
350// -------------------------------------------------------------------------
351
352void AudioTrack::start()
353{
354 sp<AudioTrackThread> t = mAudioTrackThread;
355
Steve Block3856b092011-10-20 11:56:00 +0100356 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357
Eric Laurentf5aafb22010-11-18 08:40:16 -0800358 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800359 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
360 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700361 sp<IAudioTrack> audioTrack = mAudioTrack;
362 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800363 audio_track_cblk_t* cblk = mCblk;
364
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800365 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700366 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800367 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800368 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700369 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800370 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
371 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800372 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800373 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800374 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800375 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700376 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700377 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700378 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800379 }
380
Steve Block3856b092011-10-20 11:56:00 +0100381 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700382 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800383 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800384 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800385 ALOGV("mAudioTrack->start()");
386 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800387 cblk->lock.lock();
388 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800389 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800390 }
Eric Laurent2b584242009-11-09 23:32:22 -0800391 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800392 if (cblk->flags & CBLK_INVALID) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800393 status = restoreTrack_l(cblk, true);
394 }
395 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800396 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100397 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800398 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800399 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800400 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800401 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700402 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700403 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800404 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800405 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 }
407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408}
409
410void AudioTrack::stop()
411{
412 sp<AudioTrackThread> t = mAudioTrackThread;
413
Steve Block3856b092011-10-20 11:56:00 +0100414 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415
Eric Laurentf5aafb22010-11-18 08:40:16 -0800416 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800417 if (mActive) {
418 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700419 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800420 mAudioTrack->stop();
421 // Cancel loops (If we are in the middle of a loop, playback
422 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800423 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700424 // the playback head position will reset to 0, so if a marker is set, we need
425 // to activate it again
426 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427 // Force flush if a shared buffer is used otherwise audioflinger
428 // will not stop before end of buffer is reached.
429 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800430 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 }
432 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800433 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700435 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700436 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437 }
438 }
439
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440}
441
442bool AudioTrack::stopped() const
443{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800444 AutoMutex lock(mLock);
445 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446}
447
448void AudioTrack::flush()
449{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800450 AutoMutex lock(mLock);
451 flush_l();
452}
453
454// must be called with mLock held
455void AudioTrack::flush_l()
456{
Steve Block3856b092011-10-20 11:56:00 +0100457 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700458
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700459 // clear playback marker and periodic update counter
460 mMarkerPosition = 0;
461 mMarkerReached = false;
462 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700463
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700465 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 mAudioTrack->flush();
467 // Release AudioTrack callback thread in case it was waiting for new buffers
468 // in AudioTrack::obtainBuffer()
469 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 }
471}
472
473void AudioTrack::pause()
474{
Steve Block3856b092011-10-20 11:56:00 +0100475 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800476 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800477 if (mActive) {
478 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700479 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 mAudioTrack->pause();
481 }
482}
483
484void AudioTrack::mute(bool e)
485{
486 mAudioTrack->mute(e);
487 mMuted = e;
488}
489
490bool AudioTrack::muted() const
491{
492 return mMuted;
493}
494
Eric Laurentbe916aa2010-06-01 23:49:17 -0700495status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800497 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700498 return BAD_VALUE;
499 }
500
Eric Laurent1703cdf2011-03-07 14:52:59 -0800501 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 mVolume[LEFT] = left;
503 mVolume[RIGHT] = right;
504
Glenn Kasten83d86532012-01-17 14:39:34 -0800505 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700506
507 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508}
509
Glenn Kastena5224f32012-01-04 12:41:44 -0800510void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700512 if (left != NULL) {
513 *left = mVolume[LEFT];
514 }
515 if (right != NULL) {
516 *right = mVolume[RIGHT];
517 }
518}
519
Eric Laurent2beeb502010-07-16 07:43:46 -0700520status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700521{
Steve Block3856b092011-10-20 11:56:00 +0100522 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800523 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700524 return BAD_VALUE;
525 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800526 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700527
528 mSendLevel = level;
529
Glenn Kasten05632a52012-01-03 14:22:33 -0800530 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700531
532 return NO_ERROR;
533}
534
Glenn Kastena5224f32012-01-04 12:41:44 -0800535void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700536{
537 if (level != NULL) {
538 *level = mSendLevel;
539 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540}
541
Eric Laurent57326622009-07-07 07:10:45 -0700542status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543{
544 int afSamplingRate;
545
John Grossman4ff14ba2012-02-08 16:37:41 -0800546 if (mIsTimed) {
547 return INVALID_OPERATION;
548 }
549
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700551 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 }
553 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700554 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555
Eric Laurent1703cdf2011-03-07 14:52:59 -0800556 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700557 mCblk->sampleRate = rate;
558 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559}
560
Glenn Kastena5224f32012-01-04 12:41:44 -0800561uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562{
John Grossman4ff14ba2012-02-08 16:37:41 -0800563 if (mIsTimed) {
564 return INVALID_OPERATION;
565 }
566
Eric Laurent1703cdf2011-03-07 14:52:59 -0800567 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700568 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800569}
570
571status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
572{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800573 AutoMutex lock(mLock);
574 return setLoop_l(loopStart, loopEnd, loopCount);
575}
576
577// must be called with mLock held
578status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
579{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800580 audio_track_cblk_t* cblk = mCblk;
581
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 Mutex::Autolock _l(cblk->lock);
583
584 if (loopCount == 0) {
585 cblk->loopStart = UINT_MAX;
586 cblk->loopEnd = UINT_MAX;
587 cblk->loopCount = 0;
588 mLoopCount = 0;
589 return NO_ERROR;
590 }
591
John Grossman4ff14ba2012-02-08 16:37:41 -0800592 if (mIsTimed) {
593 return INVALID_OPERATION;
594 }
595
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700597 loopEnd - loopStart > cblk->frameCount ||
598 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700599 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
600 "user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601 return BAD_VALUE;
602 }
603
Eric Laurent9b7d9502011-03-21 11:49:00 -0700604 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700605 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
606 "framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700607 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700609 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610
611 cblk->loopStart = loopStart;
612 cblk->loopEnd = loopEnd;
613 cblk->loopCount = loopCount;
614 mLoopCount = loopCount;
615
616 return NO_ERROR;
617}
618
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619status_t AudioTrack::setMarkerPosition(uint32_t marker)
620{
Glenn Kastena0d68332012-01-27 16:47:15 -0800621 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622
623 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700624 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625
626 return NO_ERROR;
627}
628
Glenn Kastena5224f32012-01-04 12:41:44 -0800629status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630{
Glenn Kastena0d68332012-01-27 16:47:15 -0800631 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632
633 *marker = mMarkerPosition;
634
635 return NO_ERROR;
636}
637
638status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
639{
Glenn Kastena0d68332012-01-27 16:47:15 -0800640 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641
642 uint32_t curPosition;
643 getPosition(&curPosition);
644 mNewPosition = curPosition + updatePeriod;
645 mUpdatePeriod = updatePeriod;
646
647 return NO_ERROR;
648}
649
Glenn Kastena5224f32012-01-04 12:41:44 -0800650status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800651{
Glenn Kastena0d68332012-01-27 16:47:15 -0800652 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653
654 *updatePeriod = mUpdatePeriod;
655
656 return NO_ERROR;
657}
658
659status_t AudioTrack::setPosition(uint32_t position)
660{
John Grossman4ff14ba2012-02-08 16:37:41 -0800661 if (mIsTimed) return INVALID_OPERATION;
662
Eric Laurent1703cdf2011-03-07 14:52:59 -0800663 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800665 if (!stopped_l()) return INVALID_OPERATION;
666
667 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668
669 if (position > mCblk->user) return BAD_VALUE;
670
671 mCblk->server = position;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800672 android_atomic_or(CBLK_FORCEREADY, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700673
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674 return NO_ERROR;
675}
676
677status_t AudioTrack::getPosition(uint32_t *position)
678{
Glenn Kastena0d68332012-01-27 16:47:15 -0800679 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800680 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700681 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682
683 return NO_ERROR;
684}
685
686status_t AudioTrack::reload()
687{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800688 AutoMutex lock(mLock);
689
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800690 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700691
Eric Laurent1703cdf2011-03-07 14:52:59 -0800692 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693
Eric Laurentd1b449a2010-05-14 03:26:45 -0700694 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695
696 return NO_ERROR;
697}
698
Eric Laurentc2f1f072009-07-17 12:17:14 -0700699audio_io_handle_t AudioTrack::getOutput()
700{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800701 AutoMutex lock(mLock);
702 return getOutput_l();
703}
704
705// must be called with mLock held
706audio_io_handle_t AudioTrack::getOutput_l()
707{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800708 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800709 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700710}
711
Glenn Kastena5224f32012-01-04 12:41:44 -0800712int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700713{
714 return mSessionId;
715}
716
717status_t AudioTrack::attachAuxEffect(int effectId)
718{
Steve Block3856b092011-10-20 11:56:00 +0100719 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700720 status_t status = mAudioTrack->attachAuxEffect(effectId);
721 if (status == NO_ERROR) {
722 mAuxEffectId = effectId;
723 }
724 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700725}
726
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800727// -------------------------------------------------------------------------
728
Eric Laurent1703cdf2011-03-07 14:52:59 -0800729// must be called with mLock held
730status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800731 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800732 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800733 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700734 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800735 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700736 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800737 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700738 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800739{
740 status_t status;
741 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
742 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700743 ALOGE("Could not get audioflinger");
744 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800745 }
746
Eric Laurentd1b449a2010-05-14 03:26:45 -0700747 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700748 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700749 return NO_INIT;
750 }
751
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700752 // Client decides whether the track is TIMED (see below), but can only express a preference
753 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700754 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700755 // either of these use cases:
756 // use case 1: shared buffer
757 (sharedBuffer != 0) ||
758 // use case 2: callback handler
759 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800760 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700761 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700762 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700763 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700764 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700765 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700766
Eric Laurentd1b449a2010-05-14 03:26:45 -0700767 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700768
Dima Zavinfce7a472011-04-19 22:30:36 -0700769 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700770
Eric Laurentd1b449a2010-05-14 03:26:45 -0700771 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700772 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700773 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700774 } else if (frameCount == 0) {
775 int afFrameCount;
776 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
777 return NO_INIT;
778 }
779 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700780 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700781
782 } else if (sharedBuffer != 0) {
783
784 // Ensure that buffer alignment matches channelCount
785 int channelCount = popcount(channelMask);
786 // 8-bit data in shared memory is not currently supported by AudioFlinger
787 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
788 if (channelCount > 1) {
789 // More than 2 channels does not require stronger alignment than stereo
790 alignment <<= 1;
791 }
792 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
793 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
794 sharedBuffer->pointer(), channelCount);
795 return BAD_VALUE;
796 }
797
798 // When initializing a shared buffer AudioTrack via constructors,
799 // there's no frameCount parameter.
800 // But when initializing a shared buffer AudioTrack via set(),
801 // there _is_ a frameCount parameter. We silently ignore it.
802 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
803
804 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
805
806 // FIXME move these calculations and associated checks to server
807 int afSampleRate;
808 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
809 return NO_INIT;
810 }
811 int afFrameCount;
812 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
813 return NO_INIT;
814 }
815
Eric Laurentd1b449a2010-05-14 03:26:45 -0700816 // Ensure that buffer depth covers at least audio hardware latency
817 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
818 if (minBufCount < 2) minBufCount = 2;
819
820 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800821 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
822 ", afLatency=%d",
823 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700824
825 if (frameCount == 0) {
826 frameCount = minFrameCount;
827 }
828 if (mNotificationFramesAct == 0) {
829 mNotificationFramesAct = frameCount/2;
830 }
831 // Make sure that application is notified with sufficient margin
832 // before underrun
833 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
834 mNotificationFramesAct = frameCount/2;
835 }
836 if (frameCount < minFrameCount) {
837 // not ALOGW because it happens all the time when playing key clicks over A2DP
838 ALOGV("Minimum buffer size corrected from %d to %d",
839 frameCount, minFrameCount);
840 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800841 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700842
Glenn Kastene0fa4672012-04-24 14:35:14 -0700843 } else {
844 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700845 }
846
Glenn Kastena075db42012-03-06 11:22:44 -0800847 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
848 if (mIsTimed) {
849 trackFlags |= IAudioFlinger::TRACK_TIMED;
850 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800851
852 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700853 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700854 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800855 if (mAudioTrackThread != 0) {
856 tid = mAudioTrackThread->getTid();
857 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700858 }
859
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800860 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
861 streamType,
862 sampleRate,
863 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700864 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800865 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800866 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800867 sharedBuffer,
868 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800869 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700870 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800871 &status);
872
873 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000874 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800875 return status;
876 }
877 sp<IMemory> cblk = track->getCblk();
878 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000879 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800880 return NO_INIT;
881 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800882 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800883 mCblkMemory = cblk;
884 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -0800885 // old has the previous value of mCblk->flags before the "or" operation
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800886 int32_t old = android_atomic_or(CBLK_DIRECTION, &mCblk->flags);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800887 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
888 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700889 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800890 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700891 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700892 // once denied, do not request again if IAudioTrack is re-created
893 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
894 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800895 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700896 if (sharedBuffer == 0) {
897 mNotificationFramesAct = mCblk->frameCount/2;
898 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800899 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800900 if (sharedBuffer == 0) {
901 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
902 } else {
903 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700904 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700905 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800906 }
907
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700908 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
909 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800910 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700911 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800912 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
913 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700914 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700915 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -0700916 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700917 // If IAudioTrack is re-created, don't let the requested frameCount
918 // decrease. This can confuse clients that cache frameCount().
919 if (mCblk->frameCount > mFrameCount) {
920 mFrameCount = mCblk->frameCount;
921 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800922 return NO_ERROR;
923}
924
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
926{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800927 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800928 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700929 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800930 audio_track_cblk_t* cblk = mCblk;
931 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700932 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800933
934 audioBuffer->frameCount = 0;
935 audioBuffer->size = 0;
936
937 uint32_t framesAvail = cblk->framesAvailable();
938
Eric Laurent9b7d9502011-03-21 11:49:00 -0700939 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800940 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700941 goto create_new_track;
942 }
943 cblk->lock.unlock();
944
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800946 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 goto start_loop_here;
948 while (framesAvail == 0) {
949 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800950 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100951 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800952 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953 return NO_MORE_BUFFERS;
954 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800955 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800956 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800957 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800958 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800959 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800960 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700961 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700962 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800963 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800964 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800965 return status_t(STOPPED);
966 }
967 cblk->lock.lock();
968 }
969
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800970 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700971 goto create_new_track;
972 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800973 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700974 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
976 // timing out when a loop has been set and we have already written upto loop end
977 // is a normal condition: no need to wake AudioFlinger up.
978 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700979 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
980 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700981 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800983 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800984 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800985 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800986 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800987create_new_track:
988 result = restoreTrack_l(cblk, false);
989 }
990 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000991 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800992 cblk->lock.unlock();
993 return result;
994 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 }
996 cblk->waitTimeMs = 0;
997 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700998
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800999 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001000 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 return TIMED_OUT;
1002 }
1003 }
1004 // read the server count again
1005 start_loop_here:
1006 framesAvail = cblk->framesAvailable_l();
1007 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001008 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001009 }
1010
1011 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001012
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 if (framesReq > framesAvail) {
1014 framesReq = framesAvail;
1015 }
1016
1017 uint32_t u = cblk->user;
1018 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1019
Marco Nelissena1472d92012-03-30 14:36:54 -07001020 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 framesReq = bufferEnd - u;
1022 }
1023
Eric Laurentc2f1f072009-07-17 12:17:14 -07001024 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1025 audioBuffer->channelCount = mChannelCount;
1026 audioBuffer->frameCount = framesReq;
1027 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001028 if (audio_is_linear_pcm(mFormat)) {
1029 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001030 } else {
1031 audioBuffer->format = mFormat;
1032 }
1033 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001034 active = mActive;
1035 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1036}
1037
1038void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1039{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001040 AutoMutex lock(mLock);
1041 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001042 if (audioBuffer->frameCount > 0) {
1043 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001044 if (mActive && (mCblk->flags & CBLK_DISABLED)) {
1045 android_atomic_and(~CBLK_DISABLED, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001046 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001047 mAudioTrack->start();
1048 }
1049 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001050}
1051
1052// -------------------------------------------------------------------------
1053
1054ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1055{
1056
1057 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001058 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001059
1060 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001061 // Sanity-check: user is most-likely passing an error code, and it would
1062 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001063 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001064 buffer, userSize, userSize);
1065 return BAD_VALUE;
1066 }
1067
Steve Block3856b092011-10-20 11:56:00 +01001068 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001069
Eric Laurentdf839842012-05-31 14:27:14 -07001070 if (userSize == 0) {
1071 return 0;
1072 }
1073
Eric Laurent1703cdf2011-03-07 14:52:59 -08001074 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1075 // while we are accessing the cblk
1076 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001077 sp<IAudioTrack> audioTrack = mAudioTrack;
1078 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001079 mLock.unlock();
1080
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001081 ssize_t written = 0;
1082 const int8_t *src = (const int8_t *)buffer;
1083 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001084 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001085
1086 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001087 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001088
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001089 status_t err = obtainBuffer(&audioBuffer, -1);
1090 if (err < 0) {
1091 // out of buffers, return #bytes written
1092 if (err == status_t(NO_MORE_BUFFERS))
1093 break;
1094 return ssize_t(err);
1095 }
1096
1097 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001098
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001099 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001100 // Divide capacity by 2 to take expansion into account
1101 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001102 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001103 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104 toWrite = audioBuffer.size;
1105 memcpy(audioBuffer.i8, src, toWrite);
1106 src += toWrite;
1107 }
1108 userSize -= toWrite;
1109 written += toWrite;
1110
1111 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001112 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001113
1114 return written;
1115}
1116
1117// -------------------------------------------------------------------------
1118
John Grossman4ff14ba2012-02-08 16:37:41 -08001119TimedAudioTrack::TimedAudioTrack() {
1120 mIsTimed = true;
1121}
1122
1123status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1124{
1125 status_t result = UNKNOWN_ERROR;
1126
1127 // If the track is not invalid already, try to allocate a buffer. alloc
1128 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001129 // we can attempt to restore in just a bit.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001130 if (!(mCblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001131 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1132 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001133 android_atomic_or(CBLK_INVALID, &mCblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001134 }
1135 }
1136
1137 // If the track is invalid at this point, attempt to restore it. and try the
1138 // allocation one more time.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001139 if (mCblk->flags & CBLK_INVALID) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001140 mCblk->lock.lock();
1141 result = restoreTrack_l(mCblk, false);
1142 mCblk->lock.unlock();
1143
1144 if (result == OK)
1145 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1146 }
1147
1148 return result;
1149}
1150
1151status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1152 int64_t pts)
1153{
Eric Laurentdf839842012-05-31 14:27:14 -07001154 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1155 {
1156 AutoMutex lock(mLock);
1157 // restart track if it was disabled by audioflinger due to previous underrun
1158 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001159 mActive && (mCblk->flags & CBLK_DISABLED)) {
1160 android_atomic_and(~CBLK_DISABLED, &mCblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001161 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1162 mAudioTrack->start();
1163 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001164 }
Eric Laurentdf839842012-05-31 14:27:14 -07001165 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001166}
1167
1168status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1169 TargetTimeline target)
1170{
1171 return mAudioTrack->setMediaTimeTransform(xform, target);
1172}
1173
1174// -------------------------------------------------------------------------
1175
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001176bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1177{
1178 Buffer audioBuffer;
1179 uint32_t frames;
1180 size_t writtenSize;
1181
Eric Laurent1703cdf2011-03-07 14:52:59 -08001182 mLock.lock();
1183 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1184 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001185 sp<IAudioTrack> audioTrack = mAudioTrack;
1186 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001187 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001188 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001189 mLock.unlock();
1190
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001191 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001192 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001193 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001194 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001195 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001196 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001197 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001198 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001199 if (mSharedBuffer != 0) return false;
1200 }
1201 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001204 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 int loopCount = -1;
1206 mLoopCount--;
1207 if (mLoopCount >= 0) loopCount = mLoopCount;
1208
1209 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1210 }
1211
1212 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001213 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001214 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001216 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217 }
1218 }
1219
1220 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001221 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001222 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001223 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1224 mNewPosition += mUpdatePeriod;
1225 }
1226 }
1227
1228 // If Shared buffer is used, no data is requested from client.
1229 if (mSharedBuffer != 0) {
1230 frames = 0;
1231 } else {
1232 frames = mRemainingFrames;
1233 }
1234
Glenn Kasten99e53b82012-01-19 08:59:58 -08001235 // See description of waitCount parameter at declaration of obtainBuffer().
1236 // The logic below prevents us from being stuck below at obtainBuffer()
1237 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001238 int32_t waitCount = -1;
1239 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1240 waitCount = 1;
1241 }
1242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001243 do {
1244
1245 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001246
Eric Laurent2267ba12011-09-07 11:13:23 -07001247 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248 if (err < NO_ERROR) {
1249 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001250 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1251 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252 return false;
1253 }
1254 break;
1255 }
1256 if (err == status_t(STOPPED)) return false;
1257
1258 // Divide buffer size by 2 to take into account the expansion
1259 // due to 8 to 16 bit conversion: the callback must fill only half
1260 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001261 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 audioBuffer.size >>= 1;
1263 }
1264
1265 size_t reqSize = audioBuffer.size;
1266 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1267 writtenSize = audioBuffer.size;
1268
1269 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001270 if (ssize_t(writtenSize) <= 0) {
1271 // The callback is done filling buffers
1272 // Keep this thread going to handle timed events and
1273 // still try to get more data in intervals of WAIT_PERIOD_MS
1274 // but don't just loop and block the CPU, so wait
1275 usleep(WAIT_PERIOD_MS*1000);
1276 break;
1277 }
Eric Laurentdf839842012-05-31 14:27:14 -07001278
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279 if (writtenSize > reqSize) writtenSize = reqSize;
1280
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001281 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001282 // 8 to 16 bit conversion, note that source and destination are the same address
1283 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 writtenSize <<= 1;
1285 }
1286
1287 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001288 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001289 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001290 // 16 bit.
1291 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1292
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 frames -= audioBuffer.frameCount;
1294
1295 releaseBuffer(&audioBuffer);
1296 }
1297 while (frames);
1298
1299 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001300 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301 } else {
1302 mRemainingFrames = frames;
1303 }
1304 return true;
1305}
1306
Eric Laurent1703cdf2011-03-07 14:52:59 -08001307// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1308// the IAudioTrack and IMemory in case they are recreated here.
1309// If the IAudioTrack is successfully restored, the cblk pointer is updated
1310status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1311{
1312 status_t result;
1313
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001314 if (!(android_atomic_or(CBLK_RESTORING, &cblk->flags) & CBLK_RESTORING)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001315 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001316 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001317
Eric Laurent1703cdf2011-03-07 14:52:59 -08001318 // signal old cblk condition so that other threads waiting for available buffers stop
1319 // waiting now
1320 cblk->cv.broadcast();
1321 cblk->lock.unlock();
1322
Eric Laurent9f6530f2011-08-30 10:18:54 -07001323 // refresh the audio configuration cache in this process to make sure we get new
1324 // output parameters in getOutput_l() and createTrack_l()
1325 AudioSystem::clearAudioConfigCache();
1326
Eric Laurent1703cdf2011-03-07 14:52:59 -08001327 // if the new IAudioTrack is created, createTrack_l() will modify the
1328 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1329 // It will also delete the strong references on previous IAudioTrack and IMemory
1330 result = createTrack_l(mStreamType,
1331 cblk->sampleRate,
1332 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001333 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001334 mFrameCount,
1335 mFlags,
1336 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001337 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001338
1339 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001340 uint32_t user = cblk->user;
1341 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001342 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001343 mCblk->user = user;
1344 mCblk->server = user;
1345 mCblk->userBase = user;
1346 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001347 // restore loop: this is not guaranteed to succeed if new frame count is not
1348 // compatible with loop length
1349 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001350 if (!fromStart) {
1351 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001352 // Make sure that a client relying on callback events indicating underrun or
1353 // the actual amount of audio frames played (e.g SoundPool) receives them.
1354 if (mSharedBuffer == 0) {
1355 uint32_t frames = 0;
1356 if (user > server) {
1357 frames = ((user - server) > mCblk->frameCount) ?
1358 mCblk->frameCount : (user - server);
1359 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1360 }
1361 // restart playback even if buffer is not completely filled.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001362 android_atomic_or(CBLK_FORCEREADY, &mCblk->flags);
1363 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
Eric Laurent408b8dc2011-09-06 12:36:15 -07001364 // the client
1365 mCblk->stepUser(frames);
1366 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001367 }
Eric Laurent29864602012-05-08 18:57:51 -07001368 if (mSharedBuffer != 0) {
1369 mCblk->stepUser(mCblk->frameCount);
1370 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001371 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001372 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001373 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001374 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001375 if (fromStart && result == NO_ERROR) {
1376 mNewPosition = mCblk->server + mUpdatePeriod;
1377 }
1378 }
1379 if (result != NO_ERROR) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001380 android_atomic_and(~CBLK_RESTORING, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001381 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001382 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001383 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001384 // signal old cblk condition for other threads waiting for restore completion
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001385 android_atomic_or(CBLK_RESTORED, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001386 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001387 } else {
Glenn Kasten9a08ebc2012-11-02 09:59:51 -07001388 bool haveLogged = false;
1389 for (;;) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001390 if (cblk->flags & CBLK_RESTORED) {
Glenn Kasten9a08ebc2012-11-02 09:59:51 -07001391 ALOGW("dead IAudioTrack restored");
1392 result = mRestoreStatus;
1393 cblk->lock.unlock();
1394 break;
1395 }
1396 if (!haveLogged) {
1397 ALOGW("dead IAudioTrack, waiting for a new one");
1398 haveLogged = true;
1399 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001400 mLock.unlock();
1401 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
1402 cblk->lock.unlock();
1403 mLock.lock();
Glenn Kasten9a08ebc2012-11-02 09:59:51 -07001404 if (result != NO_ERROR) {
1405 ALOGW("timed out");
1406 break;
1407 }
1408 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001409 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001410 }
Steve Block3856b092011-10-20 11:56:00 +01001411 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001412 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001413
1414 if (result == NO_ERROR) {
1415 // from now on we switch to the newly created cblk
1416 cblk = mCblk;
1417 }
1418 cblk->lock.lock();
1419
Steve Block5ff1dd52012-01-05 23:22:43 +00001420 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001421
1422 return result;
1423}
1424
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1426{
1427
1428 const size_t SIZE = 256;
1429 char buffer[SIZE];
1430 String8 result;
1431
1432 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001433 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1434 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001435 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001436 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
1437 mChannelCount, mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001439 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n",
1440 (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 result.append(buffer);
1442 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1443 result.append(buffer);
1444 ::write(fd, result.string(), result.size());
1445 return NO_ERROR;
1446}
1447
1448// =========================================================================
1449
1450AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001451 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1452{
1453}
1454
1455AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456{
1457}
1458
1459bool AudioTrack::AudioTrackThread::threadLoop()
1460{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001461 {
1462 AutoMutex _l(mMyLock);
1463 if (mPaused) {
1464 mMyCond.wait(mMyLock);
1465 // caller will check for exitPending()
1466 return true;
1467 }
1468 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001469 if (!mReceiver.processAudioBuffer(this)) {
1470 pause();
1471 }
1472 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473}
1474
Glenn Kasten3acbd052012-02-28 10:39:56 -08001475void AudioTrack::AudioTrackThread::requestExit()
1476{
1477 // must be in this order to avoid a race condition
1478 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001479 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001480}
1481
1482void AudioTrack::AudioTrackThread::pause()
1483{
1484 AutoMutex _l(mMyLock);
1485 mPaused = true;
1486}
1487
1488void AudioTrack::AudioTrackThread::resume()
1489{
1490 AutoMutex _l(mMyLock);
1491 if (mPaused) {
1492 mPaused = false;
1493 mMyCond.signal();
1494 }
1495}
1496
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001497// =========================================================================
1498
Eric Laurent38ccae22011-03-28 18:37:07 -07001499
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001501 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001502 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001503 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001504 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505{
1506}
1507
1508uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1509{
Marco Nelissena1472d92012-03-30 14:36:54 -07001510 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001511
Marco Nelissena1472d92012-03-30 14:36:54 -07001512 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001513 u += frameCount;
1514 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001515 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001516 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1517 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1518 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1519 }
Glenn Kasten90548972011-12-13 11:45:07 -08001520 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001521 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001522 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001523 }
1524
Marco Nelissena1472d92012-03-30 14:36:54 -07001525 uint32_t fc = this->frameCount;
1526 if (u >= fc) {
1527 // common case, user didn't just wrap
1528 if (u - fc >= userBase ) {
1529 userBase += fc;
1530 }
1531 } else if (u >= userBase + fc) {
1532 // user just wrapped
1533 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001534 }
1535
Glenn Kasten90548972011-12-13 11:45:07 -08001536 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001537
1538 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001539 if (flags & CBLK_UNDERRUN) {
1540 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001541 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542
1543 return u;
1544}
1545
1546bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1547{
Marco Nelissena1472d92012-03-30 14:36:54 -07001548 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1549
Eric Laurent38ccae22011-03-28 18:37:07 -07001550 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001551 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552 return false;
1553 }
1554
Glenn Kasten90548972011-12-13 11:45:07 -08001555 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001556 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001557
1558 s += frameCount;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001559 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001560 // Mark that we have read the first buffer so that next time stepUser() is called
1561 // we switch to normal obtainBuffer() timeout period
1562 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001563 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001564 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001565 // It is possible that we receive a flush()
1566 // while the mixer is processing a block: in this case,
1567 // stepServer() is called After the flush() has reset u & s and
1568 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001569 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001570 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001571 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001572 }
1573 }
1574
1575 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001576 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577 s = loopStart;
1578 if (--loopCount == 0) {
1579 loopEnd = UINT_MAX;
1580 loopStart = UINT_MAX;
1581 }
1582 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001583
1584 uint32_t fc = this->frameCount;
1585 if (s >= fc) {
1586 // common case, server didn't just wrap
1587 if (s - fc >= serverBase ) {
1588 serverBase += fc;
1589 }
1590 } else if (s >= serverBase + fc) {
1591 // server just wrapped
1592 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001593 }
1594
Glenn Kasten90548972011-12-13 11:45:07 -08001595 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001597 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001598 cv.signal();
1599 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001600 lock.unlock();
1601 return true;
1602}
1603
1604void* audio_track_cblk_t::buffer(uint32_t offset) const
1605{
Glenn Kasten90548972011-12-13 11:45:07 -08001606 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001607}
1608
1609uint32_t audio_track_cblk_t::framesAvailable()
1610{
1611 Mutex::Autolock _l(lock);
1612 return framesAvailable_l();
1613}
1614
1615uint32_t audio_track_cblk_t::framesAvailable_l()
1616{
Glenn Kasten90548972011-12-13 11:45:07 -08001617 uint32_t u = user;
1618 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001619
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001620 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621 uint32_t limit = (s < loopStart) ? s : loopStart;
1622 return limit + frameCount - u;
1623 } else {
1624 return frameCount + u - s;
1625 }
1626}
1627
1628uint32_t audio_track_cblk_t::framesReady()
1629{
Glenn Kasten90548972011-12-13 11:45:07 -08001630 uint32_t u = user;
1631 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001632
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001633 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001634 if (u < loopEnd) {
1635 return u - s;
1636 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001637 // do not block on mutex shared with client on AudioFlinger side
1638 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001639 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001640 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001641 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001642 uint32_t frames = UINT_MAX;
1643 if (loopCount >= 0) {
1644 frames = (loopEnd - loopStart)*loopCount + u - s;
1645 }
1646 lock.unlock();
1647 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648 }
1649 } else {
1650 return s - u;
1651 }
1652}
1653
Eric Laurent38ccae22011-03-28 18:37:07 -07001654bool audio_track_cblk_t::tryLock()
1655{
1656 // the code below simulates lock-with-timeout
1657 // we MUST do this to protect the AudioFlinger server
1658 // as this lock is shared with the client.
1659 status_t err;
1660
1661 err = lock.tryLock();
1662 if (err == -EBUSY) { // just wait a bit
1663 usleep(1000);
1664 err = lock.tryLock();
1665 }
1666 if (err != NO_ERROR) {
1667 // probably, the client just died.
1668 return false;
1669 }
1670 return true;
1671}
1672
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001673// -------------------------------------------------------------------------
1674
1675}; // namespace android