blob: 5fb36ee63e0040efadac1cf0ed4f171ffb59a8da [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
Glenn Kasten3b16c762012-11-14 08:44:39 -080068 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080069 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 Kasten3b16c762012-11-14 08:44:39 -0800196 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700197 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;
Glenn Kasten83a03822012-11-12 07:58:20 -0800289
290 if (audio_is_linear_pcm(format)) {
291 mFrameSize = channelCount * audio_bytes_per_sample(format);
292 mFrameSizeAF = channelCount * sizeof(int16_t);
293 } else {
294 mFrameSize = sizeof(uint8_t);
295 mFrameSizeAF = sizeof(uint8_t);
296 }
297
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 mSharedBuffer = sharedBuffer;
299 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800300 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302 mLoopCount = 0;
303 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700304 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 mNewPosition = 0;
306 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700307 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700308 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 return NO_ERROR;
310}
311
312status_t AudioTrack::initCheck() const
313{
314 return mStatus;
315}
316
317// -------------------------------------------------------------------------
318
319uint32_t AudioTrack::latency() const
320{
321 return mLatency;
322}
323
Glenn Kastenfff6d712012-01-12 16:38:12 -0800324audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325{
326 return mStreamType;
327}
328
Glenn Kastene1c39622012-01-04 09:36:37 -0800329audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330{
331 return mFormat;
332}
333
334int AudioTrack::channelCount() const
335{
336 return mChannelCount;
337}
338
339uint32_t AudioTrack::frameCount() const
340{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700341 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342}
343
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344sp<IMemory>& AudioTrack::sharedBuffer()
345{
346 return mSharedBuffer;
347}
348
349// -------------------------------------------------------------------------
350
351void AudioTrack::start()
352{
353 sp<AudioTrackThread> t = mAudioTrackThread;
354
Steve Block3856b092011-10-20 11:56:00 +0100355 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356
Eric Laurentf5aafb22010-11-18 08:40:16 -0800357 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-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
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700360 sp<IAudioTrack> audioTrack = mAudioTrack;
361 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800362 audio_track_cblk_t* cblk = mCblk;
363
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800364 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700365 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800366 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800367 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700368 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800369 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
370 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800371 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800372 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800373 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800374 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700375 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700376 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700377 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800378 }
379
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700380 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700381 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800382 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800383 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800384 ALOGV("mAudioTrack->start()");
385 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800386 cblk->lock.lock();
387 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800388 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800389 }
Eric Laurent2b584242009-11-09 23:32:22 -0800390 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800391 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700392 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800393 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700394 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800395 }
396 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800397 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800399 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800400 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800401 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800402 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700403 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700404 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800405 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800406 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 }
408
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409}
410
411void AudioTrack::stop()
412{
413 sp<AudioTrackThread> t = mAudioTrackThread;
414
Steve Block3856b092011-10-20 11:56:00 +0100415 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416
Eric Laurentf5aafb22010-11-18 08:40:16 -0800417 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800418 if (mActive) {
419 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700420 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 mAudioTrack->stop();
422 // Cancel loops (If we are in the middle of a loop, playback
423 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800424 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700425 // the playback head position will reset to 0, so if a marker is set, we need
426 // to activate it again
427 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 // Force flush if a shared buffer is used otherwise audioflinger
429 // will not stop before end of buffer is reached.
430 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800431 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 }
433 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800434 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700436 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700437 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 }
439 }
440
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441}
442
443bool AudioTrack::stopped() const
444{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800445 AutoMutex lock(mLock);
446 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447}
448
449void AudioTrack::flush()
450{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800451 AutoMutex lock(mLock);
452 flush_l();
453}
454
455// must be called with mLock held
456void AudioTrack::flush_l()
457{
Steve Block3856b092011-10-20 11:56:00 +0100458 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700459
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700460 // clear playback marker and periodic update counter
461 mMarkerPosition = 0;
462 mMarkerReached = false;
463 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700464
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800465 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700466 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 mAudioTrack->flush();
468 // Release AudioTrack callback thread in case it was waiting for new buffers
469 // in AudioTrack::obtainBuffer()
470 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 }
472}
473
474void AudioTrack::pause()
475{
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800477 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800478 if (mActive) {
479 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700480 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800481 mAudioTrack->pause();
482 }
483}
484
485void AudioTrack::mute(bool e)
486{
487 mAudioTrack->mute(e);
488 mMuted = e;
489}
490
491bool AudioTrack::muted() const
492{
493 return mMuted;
494}
495
Eric Laurentbe916aa2010-06-01 23:49:17 -0700496status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800498 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700499 return BAD_VALUE;
500 }
501
Eric Laurent1703cdf2011-03-07 14:52:59 -0800502 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503 mVolume[LEFT] = left;
504 mVolume[RIGHT] = right;
505
Glenn Kasten83d86532012-01-17 14:39:34 -0800506 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700507
508 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509}
510
Glenn Kastenb1c09932012-02-27 16:21:04 -0800511status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800513 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700514}
515
Eric Laurent2beeb502010-07-16 07:43:46 -0700516status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700517{
Steve Block3856b092011-10-20 11:56:00 +0100518 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800519 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700520 return BAD_VALUE;
521 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800522 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700523
524 mSendLevel = level;
525
Glenn Kasten05632a52012-01-03 14:22:33 -0800526 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700527
528 return NO_ERROR;
529}
530
Glenn Kastena5224f32012-01-04 12:41:44 -0800531void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700532{
533 if (level != NULL) {
534 *level = mSendLevel;
535 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536}
537
Glenn Kasten3b16c762012-11-14 08:44:39 -0800538status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539{
Glenn Kasten3b16c762012-11-14 08:44:39 -0800540 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541
John Grossman4ff14ba2012-02-08 16:37:41 -0800542 if (mIsTimed) {
543 return INVALID_OPERATION;
544 }
545
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700547 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 }
549 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kasten3b16c762012-11-14 08:44:39 -0800550 if (rate == 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551
Eric Laurent1703cdf2011-03-07 14:52:59 -0800552 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700553 mCblk->sampleRate = rate;
554 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555}
556
Glenn Kastena5224f32012-01-04 12:41:44 -0800557uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558{
John Grossman4ff14ba2012-02-08 16:37:41 -0800559 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800560 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800561 }
562
Eric Laurent1703cdf2011-03-07 14:52:59 -0800563 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700564 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565}
566
567status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
568{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800569 AutoMutex lock(mLock);
570 return setLoop_l(loopStart, loopEnd, loopCount);
571}
572
573// must be called with mLock held
574status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
575{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 audio_track_cblk_t* cblk = mCblk;
577
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578 Mutex::Autolock _l(cblk->lock);
579
580 if (loopCount == 0) {
581 cblk->loopStart = UINT_MAX;
582 cblk->loopEnd = UINT_MAX;
583 cblk->loopCount = 0;
584 mLoopCount = 0;
585 return NO_ERROR;
586 }
587
John Grossman4ff14ba2012-02-08 16:37:41 -0800588 if (mIsTimed) {
589 return INVALID_OPERATION;
590 }
591
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700593 loopEnd - loopStart > cblk->frameCount ||
594 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700595 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
596 "user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 return BAD_VALUE;
598 }
599
Eric Laurent9b7d9502011-03-21 11:49:00 -0700600 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700601 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
602 "framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700603 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700605 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606
607 cblk->loopStart = loopStart;
608 cblk->loopEnd = loopEnd;
609 cblk->loopCount = loopCount;
610 mLoopCount = loopCount;
611
612 return NO_ERROR;
613}
614
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615status_t AudioTrack::setMarkerPosition(uint32_t marker)
616{
Glenn Kastena0d68332012-01-27 16:47:15 -0800617 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618
619 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700620 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621
622 return NO_ERROR;
623}
624
Glenn Kastena5224f32012-01-04 12:41:44 -0800625status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626{
Glenn Kastena0d68332012-01-27 16:47:15 -0800627 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628
629 *marker = mMarkerPosition;
630
631 return NO_ERROR;
632}
633
634status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
635{
Glenn Kastena0d68332012-01-27 16:47:15 -0800636 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637
638 uint32_t curPosition;
639 getPosition(&curPosition);
640 mNewPosition = curPosition + updatePeriod;
641 mUpdatePeriod = updatePeriod;
642
643 return NO_ERROR;
644}
645
Glenn Kastena5224f32012-01-04 12:41:44 -0800646status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647{
Glenn Kastena0d68332012-01-27 16:47:15 -0800648 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649
650 *updatePeriod = mUpdatePeriod;
651
652 return NO_ERROR;
653}
654
655status_t AudioTrack::setPosition(uint32_t position)
656{
John Grossman4ff14ba2012-02-08 16:37:41 -0800657 if (mIsTimed) return INVALID_OPERATION;
658
Eric Laurent1703cdf2011-03-07 14:52:59 -0800659 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800661 if (!stopped_l()) return INVALID_OPERATION;
662
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700663 audio_track_cblk_t* cblk = mCblk;
664 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700666 if (position > cblk->user) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700668 cblk->server = position;
669 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700670
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671 return NO_ERROR;
672}
673
674status_t AudioTrack::getPosition(uint32_t *position)
675{
Glenn Kastena0d68332012-01-27 16:47:15 -0800676 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800677 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700678 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679
680 return NO_ERROR;
681}
682
683status_t AudioTrack::reload()
684{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800685 AutoMutex lock(mLock);
686
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800687 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700688
Eric Laurent1703cdf2011-03-07 14:52:59 -0800689 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700691 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten864585d2012-11-06 16:15:41 -0800692 cblk->stepUserOut(cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693
694 return NO_ERROR;
695}
696
Eric Laurentc2f1f072009-07-17 12:17:14 -0700697audio_io_handle_t AudioTrack::getOutput()
698{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800699 AutoMutex lock(mLock);
700 return getOutput_l();
701}
702
703// must be called with mLock held
704audio_io_handle_t AudioTrack::getOutput_l()
705{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800706 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800707 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700708}
709
Glenn Kastena5224f32012-01-04 12:41:44 -0800710int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700711{
712 return mSessionId;
713}
714
715status_t AudioTrack::attachAuxEffect(int effectId)
716{
Steve Block3856b092011-10-20 11:56:00 +0100717 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700718 status_t status = mAudioTrack->attachAuxEffect(effectId);
719 if (status == NO_ERROR) {
720 mAuxEffectId = effectId;
721 }
722 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700723}
724
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800725// -------------------------------------------------------------------------
726
Eric Laurent1703cdf2011-03-07 14:52:59 -0800727// must be called with mLock held
728status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800729 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800730 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800731 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700732 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800733 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700734 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800735 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700736 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800737{
738 status_t status;
739 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
740 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700741 ALOGE("Could not get audioflinger");
742 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800743 }
744
Eric Laurentd1b449a2010-05-14 03:26:45 -0700745 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700746 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700747 return NO_INIT;
748 }
749
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700750 // Client decides whether the track is TIMED (see below), but can only express a preference
751 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700752 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700753 // either of these use cases:
754 // use case 1: shared buffer
755 (sharedBuffer != 0) ||
756 // use case 2: callback handler
757 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800758 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700759 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700760 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700761 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700762 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700763 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700764
Eric Laurentd1b449a2010-05-14 03:26:45 -0700765 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700766
Dima Zavinfce7a472011-04-19 22:30:36 -0700767 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700768
Eric Laurentd1b449a2010-05-14 03:26:45 -0700769 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700770 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700771 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700772 } else if (frameCount == 0) {
773 int afFrameCount;
774 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
775 return NO_INIT;
776 }
777 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700778 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700779
780 } else if (sharedBuffer != 0) {
781
782 // Ensure that buffer alignment matches channelCount
783 int channelCount = popcount(channelMask);
784 // 8-bit data in shared memory is not currently supported by AudioFlinger
785 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
786 if (channelCount > 1) {
787 // More than 2 channels does not require stronger alignment than stereo
788 alignment <<= 1;
789 }
790 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
791 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
792 sharedBuffer->pointer(), channelCount);
793 return BAD_VALUE;
794 }
795
796 // When initializing a shared buffer AudioTrack via constructors,
797 // there's no frameCount parameter.
798 // But when initializing a shared buffer AudioTrack via set(),
799 // there _is_ a frameCount parameter. We silently ignore it.
800 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
801
802 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
803
804 // FIXME move these calculations and associated checks to server
Glenn Kasten3b16c762012-11-14 08:44:39 -0800805 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700806 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
807 return NO_INIT;
808 }
809 int afFrameCount;
810 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
811 return NO_INIT;
812 }
813
Eric Laurentd1b449a2010-05-14 03:26:45 -0700814 // Ensure that buffer depth covers at least audio hardware latency
815 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
816 if (minBufCount < 2) minBufCount = 2;
817
818 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3b16c762012-11-14 08:44:39 -0800819 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800820 ", afLatency=%d",
821 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700822
823 if (frameCount == 0) {
824 frameCount = minFrameCount;
825 }
826 if (mNotificationFramesAct == 0) {
827 mNotificationFramesAct = frameCount/2;
828 }
829 // Make sure that application is notified with sufficient margin
830 // before underrun
831 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
832 mNotificationFramesAct = frameCount/2;
833 }
834 if (frameCount < minFrameCount) {
835 // not ALOGW because it happens all the time when playing key clicks over A2DP
836 ALOGV("Minimum buffer size corrected from %d to %d",
837 frameCount, minFrameCount);
838 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800839 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700840
Glenn Kastene0fa4672012-04-24 14:35:14 -0700841 } else {
842 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700843 }
844
Glenn Kastena075db42012-03-06 11:22:44 -0800845 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
846 if (mIsTimed) {
847 trackFlags |= IAudioFlinger::TRACK_TIMED;
848 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800849
850 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700851 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700852 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800853 if (mAudioTrackThread != 0) {
854 tid = mAudioTrackThread->getTid();
855 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700856 }
857
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800858 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
859 streamType,
860 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700861 // AudioFlinger only sees 16-bit PCM
862 format == AUDIO_FORMAT_PCM_8_BIT ?
863 AUDIO_FORMAT_PCM_16_BIT : format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700864 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800865 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -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 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700877 sp<IMemory> iMem = track->getCblk();
878 if (iMem == 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;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700883 mCblkMemory = iMem;
884 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
885 mCblk = cblk;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800886 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800887 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700888 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", cblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800889 } else {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700890 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", cblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700891 // once denied, do not request again if IAudioTrack is re-created
892 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
893 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800894 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700895 if (sharedBuffer == 0) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700896 mNotificationFramesAct = cblk->frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700897 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800898 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800899 if (sharedBuffer == 0) {
Glenn Kastenb929e412012-11-08 12:13:58 -0800900 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800901 } else {
Glenn Kastenb929e412012-11-08 12:13:58 -0800902 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700903 // Force buffer full condition as data is already present in shared memory
Glenn Kasten864585d2012-11-06 16:15:41 -0800904 cblk->stepUserOut(cblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800905 }
906
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700907 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700908 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700909 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700910 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700911 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
912 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700913 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914 // FIXME don't believe this lie
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700915 mLatency = afLatency + (1000*cblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700916 // If IAudioTrack is re-created, don't let the requested frameCount
917 // decrease. This can confuse clients that cache frameCount().
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700918 if (cblk->frameCount > mFrameCount) {
919 mFrameCount = cblk->frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700920 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800921 return NO_ERROR;
922}
923
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800924status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
925{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800926 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800927 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700928 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929 audio_track_cblk_t* cblk = mCblk;
930 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700931 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932
933 audioBuffer->frameCount = 0;
934 audioBuffer->size = 0;
935
Glenn Kasten864585d2012-11-06 16:15:41 -0800936 uint32_t framesAvail = cblk->framesAvailableOut();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937
Eric Laurent9b7d9502011-03-21 11:49:00 -0700938 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800939 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700940 goto create_new_track;
941 }
942 cblk->lock.unlock();
943
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800945 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946 goto start_loop_here;
947 while (framesAvail == 0) {
948 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800949 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100950 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800951 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800952 return NO_MORE_BUFFERS;
953 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800954 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800955 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800957 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800958 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800959 mLock.unlock();
Glenn Kastena47f3162012-11-07 10:13:08 -0800960 // this condition is in shared memory, so if IAudioTrack and control block
961 // are replaced due to mediaserver death or IAudioTrack invalidation then
962 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700963 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700964 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800965 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800966 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800967 return status_t(STOPPED);
968 }
Glenn Kastena47f3162012-11-07 10:13:08 -0800969 // IAudioTrack may have been re-created while mLock was unlocked
970 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800971 cblk->lock.lock();
972 }
973
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800974 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975 goto create_new_track;
976 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800977 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700978 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
980 // timing out when a loop has been set and we have already written upto loop end
981 // is a normal condition: no need to wake AudioFlinger up.
982 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700983 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
984 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800987 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800989 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800990 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800991create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700992 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800993 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700994 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800995 }
996 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000997 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800998 cblk->lock.unlock();
999 return result;
1000 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 }
1002 cblk->waitTimeMs = 0;
1003 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001004
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001005 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001006 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 return TIMED_OUT;
1008 }
1009 }
1010 // read the server count again
1011 start_loop_here:
Glenn Kasten864585d2012-11-06 16:15:41 -08001012 framesAvail = cblk->framesAvailableOut_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001014 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001015 }
1016
1017 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001018
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001019 if (framesReq > framesAvail) {
1020 framesReq = framesAvail;
1021 }
1022
1023 uint32_t u = cblk->user;
1024 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1025
Marco Nelissena1472d92012-03-30 14:36:54 -07001026 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001027 framesReq = bufferEnd - u;
1028 }
1029
Eric Laurentc2f1f072009-07-17 12:17:14 -07001030 audioBuffer->frameCount = framesReq;
Glenn Kasten83a03822012-11-12 07:58:20 -08001031 audioBuffer->size = framesReq * mFrameSizeAF;
1032 audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 active = mActive;
1034 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1035}
1036
1037void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1038{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001039 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001040 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten864585d2012-11-06 16:15:41 -08001041 cblk->stepUserOut(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 Kastend2c38fc2012-11-01 14:58:02 -07001044 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1045 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1046 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->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
Glenn Kastena47f3162012-11-07 10:13:08 -08001081 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1082 // so all cblk references might still refer to old shared memory, but that should be benign
1083
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001084 ssize_t written = 0;
1085 const int8_t *src = (const int8_t *)buffer;
1086 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001087 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001088
1089 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001090 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001091
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001092 status_t err = obtainBuffer(&audioBuffer, -1);
1093 if (err < 0) {
1094 // out of buffers, return #bytes written
1095 if (err == status_t(NO_MORE_BUFFERS))
1096 break;
1097 return ssize_t(err);
1098 }
1099
1100 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001101
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001102 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001103 // Divide capacity by 2 to take expansion into account
1104 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001105 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001106 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001107 toWrite = audioBuffer.size;
1108 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001109 }
Glenn Kastenbc0f6b92012-11-12 14:32:06 -08001110 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001111 userSize -= toWrite;
1112 written += toWrite;
1113
1114 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001115 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116
1117 return written;
1118}
1119
1120// -------------------------------------------------------------------------
1121
John Grossman4ff14ba2012-02-08 16:37:41 -08001122TimedAudioTrack::TimedAudioTrack() {
1123 mIsTimed = true;
1124}
1125
1126status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1127{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001128 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001129 status_t result = UNKNOWN_ERROR;
1130
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001131 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1132 // while we are accessing the cblk
1133 sp<IAudioTrack> audioTrack = mAudioTrack;
1134 sp<IMemory> iMem = mCblkMemory;
1135
John Grossman4ff14ba2012-02-08 16:37:41 -08001136 // If the track is not invalid already, try to allocate a buffer. alloc
1137 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001138 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001139 audio_track_cblk_t* cblk = mCblk;
1140 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001141 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1142 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001143 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001144 }
1145 }
1146
1147 // If the track is invalid at this point, attempt to restore it. and try the
1148 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001149 if (cblk->flags & CBLK_INVALID) {
1150 cblk->lock.lock();
1151 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001152 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001153 cblk = temp;
1154 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001155
1156 if (result == OK)
1157 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1158 }
1159
1160 return result;
1161}
1162
1163status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1164 int64_t pts)
1165{
Eric Laurentdf839842012-05-31 14:27:14 -07001166 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1167 {
1168 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001169 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001170 // restart track if it was disabled by audioflinger due to previous underrun
1171 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001172 mActive && (cblk->flags & CBLK_DISABLED)) {
1173 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001174 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1175 mAudioTrack->start();
1176 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001177 }
Eric Laurentdf839842012-05-31 14:27:14 -07001178 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001179}
1180
1181status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1182 TargetTimeline target)
1183{
1184 return mAudioTrack->setMediaTimeTransform(xform, target);
1185}
1186
1187// -------------------------------------------------------------------------
1188
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001189bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1190{
1191 Buffer audioBuffer;
1192 uint32_t frames;
1193 size_t writtenSize;
1194
Eric Laurent1703cdf2011-03-07 14:52:59 -08001195 mLock.lock();
1196 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1197 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001198 sp<IAudioTrack> audioTrack = mAudioTrack;
1199 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001200 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001201 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001202 mLock.unlock();
1203
Glenn Kastena47f3162012-11-07 10:13:08 -08001204 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1205 // so all cblk references might still refer to old shared memory, but that should be benign
1206
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 // Manage underrun callback
Glenn Kasten864585d2012-11-06 16:15:41 -08001208 if (active && (cblk->framesAvailableOut() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001209 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001210 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001211 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001212 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001213 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 if (mSharedBuffer != 0) return false;
1216 }
1217 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001218
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001220 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001221 int loopCount = -1;
1222 mLoopCount--;
1223 if (mLoopCount >= 0) loopCount = mLoopCount;
1224
1225 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1226 }
1227
1228 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001229 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001230 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001231 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001232 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001233 }
1234 }
1235
1236 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001237 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001238 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001239 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1240 mNewPosition += mUpdatePeriod;
1241 }
1242 }
1243
1244 // If Shared buffer is used, no data is requested from client.
1245 if (mSharedBuffer != 0) {
1246 frames = 0;
1247 } else {
1248 frames = mRemainingFrames;
1249 }
1250
Glenn Kasten99e53b82012-01-19 08:59:58 -08001251 // See description of waitCount parameter at declaration of obtainBuffer().
1252 // The logic below prevents us from being stuck below at obtainBuffer()
1253 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001254 int32_t waitCount = -1;
1255 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1256 waitCount = 1;
1257 }
1258
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 do {
1260
1261 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001262
Eric Laurent2267ba12011-09-07 11:13:23 -07001263 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 if (err < NO_ERROR) {
1265 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001266 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1267 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 return false;
1269 }
1270 break;
1271 }
1272 if (err == status_t(STOPPED)) return false;
1273
1274 // Divide buffer size by 2 to take into account the expansion
1275 // due to 8 to 16 bit conversion: the callback must fill only half
1276 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001277 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278 audioBuffer.size >>= 1;
1279 }
1280
1281 size_t reqSize = audioBuffer.size;
1282 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1283 writtenSize = audioBuffer.size;
1284
1285 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001286 if (ssize_t(writtenSize) <= 0) {
1287 // The callback is done filling buffers
1288 // Keep this thread going to handle timed events and
1289 // still try to get more data in intervals of WAIT_PERIOD_MS
1290 // but don't just loop and block the CPU, so wait
1291 usleep(WAIT_PERIOD_MS*1000);
1292 break;
1293 }
Eric Laurentdf839842012-05-31 14:27:14 -07001294
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 if (writtenSize > reqSize) writtenSize = reqSize;
1296
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001297 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001298 // 8 to 16 bit conversion, note that source and destination are the same address
1299 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300 writtenSize <<= 1;
1301 }
1302
1303 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001304 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1305 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001306 // 16 bit.
Glenn Kasten83a03822012-11-12 07:58:20 -08001307 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001308
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001309 frames -= audioBuffer.frameCount;
1310
1311 releaseBuffer(&audioBuffer);
1312 }
1313 while (frames);
1314
1315 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001316 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001317 } else {
1318 mRemainingFrames = frames;
1319 }
1320 return true;
1321}
1322
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001323// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001324// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001325// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1326// FIXME Don't depend on caller to hold strong references.
1327status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001328{
1329 status_t result;
1330
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001331 audio_track_cblk_t* cblk = refCblk;
1332 audio_track_cblk_t* newCblk = cblk;
Glenn Kastena47f3162012-11-07 10:13:08 -08001333 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
1334 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001335
Glenn Kastena47f3162012-11-07 10:13:08 -08001336 // signal old cblk condition so that other threads waiting for available buffers stop
1337 // waiting now
1338 cblk->cv.broadcast();
1339 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001340
Glenn Kastena47f3162012-11-07 10:13:08 -08001341 // refresh the audio configuration cache in this process to make sure we get new
1342 // output parameters in getOutput_l() and createTrack_l()
1343 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001344
Glenn Kastena47f3162012-11-07 10:13:08 -08001345 // if the new IAudioTrack is created, createTrack_l() will modify the
1346 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1347 // It will also delete the strong references on previous IAudioTrack and IMemory
1348 result = createTrack_l(mStreamType,
1349 cblk->sampleRate,
1350 mFormat,
1351 mChannelMask,
1352 mFrameCount,
1353 mFlags,
1354 mSharedBuffer,
1355 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001356
Glenn Kastena47f3162012-11-07 10:13:08 -08001357 if (result == NO_ERROR) {
1358 uint32_t user = cblk->user;
1359 uint32_t server = cblk->server;
1360 // restore write index and set other indexes to reflect empty buffer status
1361 newCblk = mCblk;
1362 newCblk->user = user;
1363 newCblk->server = user;
1364 newCblk->userBase = user;
1365 newCblk->serverBase = user;
1366 // restore loop: this is not guaranteed to succeed if new frame count is not
1367 // compatible with loop length
1368 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1369 if (!fromStart) {
1370 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1371 // Make sure that a client relying on callback events indicating underrun or
1372 // the actual amount of audio frames played (e.g SoundPool) receives them.
1373 if (mSharedBuffer == 0) {
1374 uint32_t frames = 0;
1375 if (user > server) {
1376 frames = ((user - server) > newCblk->frameCount) ?
1377 newCblk->frameCount : (user - server);
Glenn Kasten83a03822012-11-12 07:58:20 -08001378 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001379 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001380 // restart playback even if buffer is not completely filled.
1381 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1382 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1383 // the client
1384 newCblk->stepUserOut(frames);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001385 }
1386 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001387 if (mSharedBuffer != 0) {
1388 newCblk->stepUserOut(newCblk->frameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001389 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001390 if (mActive) {
1391 result = mAudioTrack->start();
1392 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1393 }
1394 if (fromStart && result == NO_ERROR) {
1395 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001396 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001397 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001398 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001399 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001400 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001401
1402 if (result == NO_ERROR) {
1403 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001404 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001405 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001406 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001407
Steve Block5ff1dd52012-01-05 23:22:43 +00001408 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001409
1410 return result;
1411}
1412
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001413status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1414{
1415
1416 const size_t SIZE = 256;
1417 char buffer[SIZE];
1418 String8 result;
1419
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001420 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001421 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001422 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1423 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001425 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001426 mChannelCount, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427 result.append(buffer);
Glenn Kasten3b16c762012-11-14 08:44:39 -08001428 snprintf(buffer, 255, " sample rate(%u), status(%d), muted(%d)\n",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001429 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001430 result.append(buffer);
1431 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1432 result.append(buffer);
1433 ::write(fd, result.string(), result.size());
1434 return NO_ERROR;
1435}
1436
1437// =========================================================================
1438
1439AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001440 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1441{
1442}
1443
1444AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001445{
1446}
1447
1448bool AudioTrack::AudioTrackThread::threadLoop()
1449{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001450 {
1451 AutoMutex _l(mMyLock);
1452 if (mPaused) {
1453 mMyCond.wait(mMyLock);
1454 // caller will check for exitPending()
1455 return true;
1456 }
1457 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001458 if (!mReceiver.processAudioBuffer(this)) {
1459 pause();
1460 }
1461 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001462}
1463
Glenn Kasten3acbd052012-02-28 10:39:56 -08001464void AudioTrack::AudioTrackThread::requestExit()
1465{
1466 // must be in this order to avoid a race condition
1467 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001468 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001469}
1470
1471void AudioTrack::AudioTrackThread::pause()
1472{
1473 AutoMutex _l(mMyLock);
1474 mPaused = true;
1475}
1476
1477void AudioTrack::AudioTrackThread::resume()
1478{
1479 AutoMutex _l(mMyLock);
1480 if (mPaused) {
1481 mPaused = false;
1482 mMyCond.signal();
1483 }
1484}
1485
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486// =========================================================================
1487
Eric Laurent38ccae22011-03-28 18:37:07 -07001488
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001489audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001490 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastenb929e412012-11-08 12:13:58 -08001491 userBase(0), serverBase(0), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001492 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001493 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001494{
1495}
1496
Glenn Kasten864585d2012-11-06 16:15:41 -08001497uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001498{
Marco Nelissena1472d92012-03-30 14:36:54 -07001499 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500
Marco Nelissena1472d92012-03-30 14:36:54 -07001501 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502 u += frameCount;
1503 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten864585d2012-11-06 16:15:41 -08001504 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1506 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1507 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1508 }
Glenn Kasten90548972011-12-13 11:45:07 -08001509 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001510 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001511 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001512 }
1513
Marco Nelissena1472d92012-03-30 14:36:54 -07001514 uint32_t fc = this->frameCount;
1515 if (u >= fc) {
1516 // common case, user didn't just wrap
1517 if (u - fc >= userBase ) {
1518 userBase += fc;
1519 }
1520 } else if (u >= userBase + fc) {
1521 // user just wrapped
1522 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001523 }
1524
Glenn Kasten90548972011-12-13 11:45:07 -08001525 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001526
1527 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001528 if (flags & CBLK_UNDERRUN) {
1529 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001530 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001531
1532 return u;
1533}
1534
Glenn Kasten864585d2012-11-06 16:15:41 -08001535bool audio_track_cblk_t::stepServer(uint32_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001536{
Marco Nelissena1472d92012-03-30 14:36:54 -07001537 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1538
Eric Laurent38ccae22011-03-28 18:37:07 -07001539 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001540 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001541 return false;
1542 }
1543
Glenn Kasten90548972011-12-13 11:45:07 -08001544 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001545 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001546
1547 s += frameCount;
Glenn Kasten864585d2012-11-06 16:15:41 -08001548 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001549 // Mark that we have read the first buffer so that next time stepUser() is called
1550 // we switch to normal obtainBuffer() timeout period
1551 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001552 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001553 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001554 // It is possible that we receive a flush()
1555 // while the mixer is processing a block: in this case,
1556 // stepServer() is called After the flush() has reset u & s and
1557 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001558 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001559 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001560 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561 }
1562 }
1563
1564 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001565 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001566 s = loopStart;
1567 if (--loopCount == 0) {
1568 loopEnd = UINT_MAX;
1569 loopStart = UINT_MAX;
1570 }
1571 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001572
1573 uint32_t fc = this->frameCount;
1574 if (s >= fc) {
1575 // common case, server didn't just wrap
1576 if (s - fc >= serverBase ) {
1577 serverBase += fc;
1578 }
1579 } else if (s >= serverBase + fc) {
1580 // server just wrapped
1581 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 }
1583
Glenn Kasten90548972011-12-13 11:45:07 -08001584 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001585
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001586 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001587 cv.signal();
1588 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001589 lock.unlock();
1590 return true;
1591}
1592
Glenn Kasten83a03822012-11-12 07:58:20 -08001593void* audio_track_cblk_t::buffer(void *buffers, size_t frameSize, uint32_t offset) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594{
Glenn Kasten90548972011-12-13 11:45:07 -08001595 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596}
1597
Glenn Kasten864585d2012-11-06 16:15:41 -08001598uint32_t audio_track_cblk_t::framesAvailable(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599{
1600 Mutex::Autolock _l(lock);
Glenn Kasten864585d2012-11-06 16:15:41 -08001601 return framesAvailable_l(isOut);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001602}
1603
Glenn Kasten864585d2012-11-06 16:15:41 -08001604uint32_t audio_track_cblk_t::framesAvailable_l(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605{
Glenn Kasten90548972011-12-13 11:45:07 -08001606 uint32_t u = user;
1607 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608
Glenn Kasten864585d2012-11-06 16:15:41 -08001609 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001610 uint32_t limit = (s < loopStart) ? s : loopStart;
1611 return limit + frameCount - u;
1612 } else {
1613 return frameCount + u - s;
1614 }
1615}
1616
Glenn Kasten864585d2012-11-06 16:15:41 -08001617uint32_t audio_track_cblk_t::framesReady(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001618{
Glenn Kasten90548972011-12-13 11:45:07 -08001619 uint32_t u = user;
1620 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621
Glenn Kasten864585d2012-11-06 16:15:41 -08001622 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001623 if (u < loopEnd) {
1624 return u - s;
1625 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001626 // do not block on mutex shared with client on AudioFlinger side
1627 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001628 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001629 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001631 uint32_t frames = UINT_MAX;
1632 if (loopCount >= 0) {
1633 frames = (loopEnd - loopStart)*loopCount + u - s;
1634 }
1635 lock.unlock();
1636 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001637 }
1638 } else {
1639 return s - u;
1640 }
1641}
1642
Eric Laurent38ccae22011-03-28 18:37:07 -07001643bool audio_track_cblk_t::tryLock()
1644{
1645 // the code below simulates lock-with-timeout
1646 // we MUST do this to protect the AudioFlinger server
1647 // as this lock is shared with the client.
1648 status_t err;
1649
1650 err = lock.tryLock();
1651 if (err == -EBUSY) { // just wait a bit
1652 usleep(1000);
1653 err = lock.tryLock();
1654 }
1655 if (err != NO_ERROR) {
1656 // probably, the client just died.
1657 return false;
1658 }
1659 return true;
1660}
1661
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001662// -------------------------------------------------------------------------
1663
1664}; // namespace android