blob: 5d5a3125ff720de8adf5cc11e9483f8f23f8d4d7 [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{
57 int afSampleRate;
58 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
59 return NO_INIT;
60 }
61 int afFrameCount;
62 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
63 return NO_INIT;
64 }
65 uint32_t afLatency;
66 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
67 return NO_INIT;
68 }
69
70 // Ensure that buffer depth covers at least audio hardware latency
71 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
72 if (minBufCount < 2) minBufCount = 2;
73
74 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070075 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080076 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
77 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080078 return NO_ERROR;
79}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080080
81// ---------------------------------------------------------------------------
82
83AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070084 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080085 mIsTimed(false),
86 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
87 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080088{
89}
90
91AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080092 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080094 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070095 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070097 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098 callback_t cbf,
99 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700100 int notificationFrames,
101 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700102 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800103 mIsTimed(false),
104 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
105 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800106{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700107 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700108 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800109 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110}
111
Glenn Kasten17a736c2012-02-14 08:52:15 -0800112// DEPRECATED
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800114 int streamType,
115 uint32_t sampleRate,
116 int format,
117 int channelMask,
118 int frameCount,
119 uint32_t flags,
120 callback_t cbf,
121 void* user,
122 int notificationFrames,
123 int sessionId)
124 : mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800125 mIsTimed(false),
Andreas Huberc8139852012-01-18 10:51:55 -0800126 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
127{
128 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700129 frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800130 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800131}
132
133AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800134 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800136 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700137 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700139 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140 callback_t cbf,
141 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700142 int notificationFrames,
143 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700144 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800145 mIsTimed(false),
146 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
147 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700149 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800150 0 /*frameCount*/, flags, cbf, user, notificationFrames,
151 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152}
153
154AudioTrack::~AudioTrack()
155{
Steve Block3856b092011-10-20 11:56:00 +0100156 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157
158 if (mStatus == NO_ERROR) {
159 // Make sure that callback function exits in the case where
160 // it is looping on buffer full condition in obtainBuffer().
161 // Otherwise the callback thread will never exit.
162 stop();
163 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800164 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 mAudioTrackThread->requestExitAndWait();
166 mAudioTrackThread.clear();
167 }
168 mAudioTrack.clear();
169 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700170 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 }
172}
173
174status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800175 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800177 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700178 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700180 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181 callback_t cbf,
182 void* user,
183 int notificationFrames,
184 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700185 bool threadCanCallJava,
186 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187{
188
Steve Block3856b092011-10-20 11:56:00 +0100189 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190
Eric Laurent1a9ed112012-03-20 18:36:01 -0700191 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
192
Eric Laurent1703cdf2011-03-07 14:52:59 -0800193 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700194 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000195 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196 return INVALID_OPERATION;
197 }
198
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700200 if (streamType == AUDIO_STREAM_DEFAULT) {
201 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700203
Eric Laurent1a9ed112012-03-20 18:36:01 -0700204 int afSampleRate;
205 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
206 return NO_INIT;
207 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 if (sampleRate == 0) {
209 sampleRate = afSampleRate;
210 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700211
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800213 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700214 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700216 if (channelMask == 0) {
217 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800218 }
219
220 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700221 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000222 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 return BAD_VALUE;
224 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700225
226 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700227 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700228 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800229 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700230 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700231 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700232 // only allow deep buffering for music stream type
233 if (streamType != AUDIO_STREAM_MUSIC) {
234 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
235 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700237 if (!audio_is_output_channel(channelMask)) {
Steve Block29357bc2012-01-06 19:20:56 +0000238 ALOGE("Invalid channel mask");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700239 return BAD_VALUE;
240 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700241 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700242
Dima Zavinfce7a472011-04-19 22:30:36 -0700243 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800244 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800245 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800246 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700247
248 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000249 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250 return BAD_VALUE;
251 }
252
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800253 mVolume[LEFT] = 1.0f;
254 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800255 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700256 mFrameCount = frameCount;
257 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700258 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700259 mAuxEffectId = 0;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700260 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700261
Glenn Kasten3acbd052012-02-28 10:39:56 -0800262 if (cbf != NULL) {
263 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
264 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
265 }
266
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800267 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800268 status_t status = createTrack_l(streamType,
269 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800270 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700271 (uint32_t)channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800272 frameCount,
273 flags,
274 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700275 output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800277 if (status != NO_ERROR) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800278 if (mAudioTrackThread != 0) {
279 mAudioTrackThread->requestExit();
280 mAudioTrackThread.clear();
281 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 return status;
283 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800284
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 mStatus = NO_ERROR;
286
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800288 mFormat = format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700289 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290 mChannelCount = channelCount;
291 mSharedBuffer = sharedBuffer;
292 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800293 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 mLoopCount = 0;
296 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700297 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 mNewPosition = 0;
299 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700300 mFlushed = false;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700301 mFlags = flags;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700302 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700303 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304 return NO_ERROR;
305}
306
307status_t AudioTrack::initCheck() const
308{
309 return mStatus;
310}
311
312// -------------------------------------------------------------------------
313
314uint32_t AudioTrack::latency() const
315{
316 return mLatency;
317}
318
Glenn Kastenfff6d712012-01-12 16:38:12 -0800319audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320{
321 return mStreamType;
322}
323
Glenn Kastene1c39622012-01-04 09:36:37 -0800324audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325{
326 return mFormat;
327}
328
329int AudioTrack::channelCount() const
330{
331 return mChannelCount;
332}
333
334uint32_t AudioTrack::frameCount() const
335{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700336 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337}
338
Glenn Kastenb9980652012-01-11 09:48:27 -0800339size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340{
Dima Zavinfce7a472011-04-19 22:30:36 -0700341 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700342 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700343 } else {
344 return sizeof(uint8_t);
345 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346}
347
348sp<IMemory>& AudioTrack::sharedBuffer()
349{
350 return mSharedBuffer;
351}
352
353// -------------------------------------------------------------------------
354
355void AudioTrack::start()
356{
357 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700358 status_t status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359
Steve Block3856b092011-10-20 11:56:00 +0100360 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361
Eric Laurentf5aafb22010-11-18 08:40:16 -0800362 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800363 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
364 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700365 sp<IAudioTrack> audioTrack = mAudioTrack;
366 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800367 audio_track_cblk_t* cblk = mCblk;
368
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800369 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700370 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800371 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800372 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700373 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800374 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
375 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700376 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800377 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800378 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800379 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700380 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
381 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
382 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800383 }
384
Steve Block3856b092011-10-20 11:56:00 +0100385 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800386 if (!(cblk->flags & CBLK_INVALID_MSK)) {
387 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800388 ALOGV("mAudioTrack->start()");
389 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800390 cblk->lock.lock();
391 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700392 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800393 }
Eric Laurent2b584242009-11-09 23:32:22 -0800394 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800395 if (cblk->flags & CBLK_INVALID_MSK) {
396 status = restoreTrack_l(cblk, true);
397 }
398 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800399 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100400 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800401 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800402 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800403 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800404 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700405 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
406 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800407 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800408 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 }
410
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411}
412
413void AudioTrack::stop()
414{
415 sp<AudioTrackThread> t = mAudioTrackThread;
416
Steve Block3856b092011-10-20 11:56:00 +0100417 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418
Eric Laurentf5aafb22010-11-18 08:40:16 -0800419 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800420 if (mActive) {
421 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700422 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 mAudioTrack->stop();
424 // Cancel loops (If we are in the middle of a loop, playback
425 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800426 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700427 // the playback head position will reset to 0, so if a marker is set, we need
428 // to activate it again
429 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 // Force flush if a shared buffer is used otherwise audioflinger
431 // will not stop before end of buffer is reached.
432 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800433 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434 }
435 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800436 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700438 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
439 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440 }
441 }
442
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443}
444
445bool AudioTrack::stopped() const
446{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800447 AutoMutex lock(mLock);
448 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449}
450
451void AudioTrack::flush()
452{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800453 AutoMutex lock(mLock);
454 flush_l();
455}
456
457// must be called with mLock held
458void AudioTrack::flush_l()
459{
Steve Block3856b092011-10-20 11:56:00 +0100460 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700461
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700462 // clear playback marker and periodic update counter
463 mMarkerPosition = 0;
464 mMarkerReached = false;
465 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700466
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700468 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 mAudioTrack->flush();
470 // Release AudioTrack callback thread in case it was waiting for new buffers
471 // in AudioTrack::obtainBuffer()
472 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 }
474}
475
476void AudioTrack::pause()
477{
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800479 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800480 if (mActive) {
481 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482 mAudioTrack->pause();
483 }
484}
485
486void AudioTrack::mute(bool e)
487{
488 mAudioTrack->mute(e);
489 mMuted = e;
490}
491
492bool AudioTrack::muted() const
493{
494 return mMuted;
495}
496
Eric Laurentbe916aa2010-06-01 23:49:17 -0700497status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800499 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700500 return BAD_VALUE;
501 }
502
Eric Laurent1703cdf2011-03-07 14:52:59 -0800503 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 mVolume[LEFT] = left;
505 mVolume[RIGHT] = right;
506
Glenn Kasten83d86532012-01-17 14:39:34 -0800507 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700508
509 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510}
511
Glenn Kastena5224f32012-01-04 12:41:44 -0800512void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700514 if (left != NULL) {
515 *left = mVolume[LEFT];
516 }
517 if (right != NULL) {
518 *right = mVolume[RIGHT];
519 }
520}
521
Eric Laurent2beeb502010-07-16 07:43:46 -0700522status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700523{
Steve Block3856b092011-10-20 11:56:00 +0100524 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800525 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700526 return BAD_VALUE;
527 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800528 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700529
530 mSendLevel = level;
531
Glenn Kasten05632a52012-01-03 14:22:33 -0800532 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700533
534 return NO_ERROR;
535}
536
Glenn Kastena5224f32012-01-04 12:41:44 -0800537void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700538{
539 if (level != NULL) {
540 *level = mSendLevel;
541 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542}
543
Eric Laurent57326622009-07-07 07:10:45 -0700544status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545{
546 int afSamplingRate;
547
John Grossman4ff14ba2012-02-08 16:37:41 -0800548 if (mIsTimed) {
549 return INVALID_OPERATION;
550 }
551
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700553 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 }
555 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700556 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557
Eric Laurent1703cdf2011-03-07 14:52:59 -0800558 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700559 mCblk->sampleRate = rate;
560 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561}
562
Glenn Kastena5224f32012-01-04 12:41:44 -0800563uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
John Grossman4ff14ba2012-02-08 16:37:41 -0800565 if (mIsTimed) {
566 return INVALID_OPERATION;
567 }
568
Eric Laurent1703cdf2011-03-07 14:52:59 -0800569 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700570 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571}
572
573status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
574{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800575 AutoMutex lock(mLock);
576 return setLoop_l(loopStart, loopEnd, loopCount);
577}
578
579// must be called with mLock held
580status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
581{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 audio_track_cblk_t* cblk = mCblk;
583
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 Mutex::Autolock _l(cblk->lock);
585
586 if (loopCount == 0) {
587 cblk->loopStart = UINT_MAX;
588 cblk->loopEnd = UINT_MAX;
589 cblk->loopCount = 0;
590 mLoopCount = 0;
591 return NO_ERROR;
592 }
593
John Grossman4ff14ba2012-02-08 16:37:41 -0800594 if (mIsTimed) {
595 return INVALID_OPERATION;
596 }
597
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700599 loopEnd - loopStart > cblk->frameCount ||
600 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000601 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602 return BAD_VALUE;
603 }
604
Eric Laurent9b7d9502011-03-21 11:49:00 -0700605 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000606 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, 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;
Eric Laurent38ccae22011-03-28 18:37:07 -0700672 android_atomic_or(CBLK_FORCEREADY_ON, &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,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700734 uint32_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 int afSampleRate;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700748 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700749 return NO_INIT;
750 }
751 int afFrameCount;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700752 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700753 return NO_INIT;
754 }
755 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700756 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700757 return NO_INIT;
758 }
759
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700760 // Client decides whether the track is TIMED (see below), but can only express a preference
761 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700762 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700763 // either of these use cases:
764 // use case 1: shared buffer
765 (sharedBuffer != 0) ||
766 // use case 2: callback handler
767 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800768 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700769 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700770 }
Eric Laurent1a9ed112012-03-20 18:36:01 -0700771 ALOGV("createTrack_l() output %d afFrameCount %d afLatency %d", output, afFrameCount, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700772
Eric Laurentd1b449a2010-05-14 03:26:45 -0700773 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavinfce7a472011-04-19 22:30:36 -0700774 if (!audio_is_linear_pcm(format)) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700775 if (sharedBuffer != 0) {
776 frameCount = sharedBuffer->size();
777 }
778 } else {
779 // Ensure that buffer depth covers at least audio hardware latency
780 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
781 if (minBufCount < 2) minBufCount = 2;
782
783 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800784 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
785 ", afLatency=%d",
786 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
787#define MIN_FRAME_COUNT_FAST 128 // FIXME hard-coded
788 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && (minFrameCount > MIN_FRAME_COUNT_FAST)) {
789 minFrameCount = MIN_FRAME_COUNT_FAST;
790 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700791
792 if (sharedBuffer == 0) {
793 if (frameCount == 0) {
794 frameCount = minFrameCount;
795 }
796 if (mNotificationFramesAct == 0) {
797 mNotificationFramesAct = frameCount/2;
798 }
799 // Make sure that application is notified with sufficient margin
800 // before underrun
801 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
802 mNotificationFramesAct = frameCount/2;
803 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800804 if (frameCount < minFrameCount) {
Glenn Kasten291f4d52012-03-19 12:16:56 -0700805 // not ALOGW because it happens all the time when playing key clicks over A2DP
806 ALOGV("Minimum buffer size corrected from %d to %d",
Eric Laurentd8d61852012-03-05 17:06:40 -0800807 frameCount, minFrameCount);
808 frameCount = minFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700809 }
810 } else {
Glenn Kasten99e53b82012-01-19 08:59:58 -0800811 // Ensure that buffer alignment matches channelCount
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700812 int channelCount = popcount(channelMask);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700813 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000814 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700815 return BAD_VALUE;
816 }
817 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
818 }
819 }
820
Glenn Kastena075db42012-03-06 11:22:44 -0800821 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
822 if (mIsTimed) {
823 trackFlags |= IAudioFlinger::TRACK_TIMED;
824 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800825
826 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700827 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700828 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800829 if (mAudioTrackThread != 0) {
830 tid = mAudioTrackThread->getTid();
831 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700832 }
833
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800834 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
835 streamType,
836 sampleRate,
837 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700838 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800839 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800840 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800841 sharedBuffer,
842 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800843 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700844 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800845 &status);
846
847 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000848 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800849 return status;
850 }
851 sp<IMemory> cblk = track->getCblk();
852 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000853 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800854 return NO_INIT;
855 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800856 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800857 mCblkMemory = cblk;
858 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -0800859 // old has the previous value of mCblk->flags before the "or" operation
860 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
861 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
862 if (old & CBLK_FAST) {
863 ALOGI("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
864 } else {
865 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
866 }
867 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800868 if (sharedBuffer == 0) {
869 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
870 } else {
871 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700872 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700873 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800874 }
875
Glenn Kasten83d86532012-01-17 14:39:34 -0800876 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800877 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700878 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800879 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
880 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700881 mRemainingFrames = mNotificationFramesAct;
882 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800883 return NO_ERROR;
884}
885
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
887{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800888 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800889 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700890 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800891 audio_track_cblk_t* cblk = mCblk;
892 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700893 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800894
895 audioBuffer->frameCount = 0;
896 audioBuffer->size = 0;
897
898 uint32_t framesAvail = cblk->framesAvailable();
899
Eric Laurent9b7d9502011-03-21 11:49:00 -0700900 cblk->lock.lock();
901 if (cblk->flags & CBLK_INVALID_MSK) {
902 goto create_new_track;
903 }
904 cblk->lock.unlock();
905
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800907 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908 goto start_loop_here;
909 while (framesAvail == 0) {
910 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800911 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100912 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800913 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914 return NO_MORE_BUFFERS;
915 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800916 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800917 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800919 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700920 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800921 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700922 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700923 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800924 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800925 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800926 return status_t(STOPPED);
927 }
928 cblk->lock.lock();
929 }
930
931 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700932 goto create_new_track;
933 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800934 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700935 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
937 // timing out when a loop has been set and we have already written upto loop end
938 // is a normal condition: no need to wake AudioFlinger up.
939 if (cblk->user < cblk->loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000940 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800941 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700942 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800944 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800946 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700947 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800948create_new_track:
949 result = restoreTrack_l(cblk, false);
950 }
951 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000952 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800953 cblk->lock.unlock();
954 return result;
955 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 }
957 cblk->waitTimeMs = 0;
958 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800960 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800961 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 return TIMED_OUT;
963 }
964 }
965 // read the server count again
966 start_loop_here:
967 framesAvail = cblk->framesAvailable_l();
968 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800969 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 }
971
Eric Laurent44d98482010-09-30 16:12:31 -0700972 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent1703cdf2011-03-07 14:52:59 -0800973 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700974 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +0000975 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800976 mAudioTrack->start();
Eric Laurent44d98482010-09-30 16:12:31 -0700977 }
978
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700980
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981 if (framesReq > framesAvail) {
982 framesReq = framesAvail;
983 }
984
985 uint32_t u = cblk->user;
986 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
987
Marco Nelissena1472d92012-03-30 14:36:54 -0700988 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800989 framesReq = bufferEnd - u;
990 }
991
Eric Laurentc2f1f072009-07-17 12:17:14 -0700992 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
993 audioBuffer->channelCount = mChannelCount;
994 audioBuffer->frameCount = framesReq;
995 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -0700996 if (audio_is_linear_pcm(mFormat)) {
997 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700998 } else {
999 audioBuffer->format = mFormat;
1000 }
1001 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001002 active = mActive;
1003 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1004}
1005
1006void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1007{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001008 AutoMutex lock(mLock);
1009 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001010}
1011
1012// -------------------------------------------------------------------------
1013
1014ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1015{
1016
1017 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001018 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001019
1020 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001021 // Sanity-check: user is most-likely passing an error code, and it would
1022 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001023 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024 buffer, userSize, userSize);
1025 return BAD_VALUE;
1026 }
1027
Steve Block3856b092011-10-20 11:56:00 +01001028 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001029
Eric Laurent1703cdf2011-03-07 14:52:59 -08001030 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1031 // while we are accessing the cblk
1032 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001033 sp<IAudioTrack> audioTrack = mAudioTrack;
1034 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001035 mLock.unlock();
1036
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001037 ssize_t written = 0;
1038 const int8_t *src = (const int8_t *)buffer;
1039 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001040 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001041
1042 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001043 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001044
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001045 status_t err = obtainBuffer(&audioBuffer, -1);
1046 if (err < 0) {
1047 // out of buffers, return #bytes written
1048 if (err == status_t(NO_MORE_BUFFERS))
1049 break;
1050 return ssize_t(err);
1051 }
1052
1053 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001054
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001055 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001056 // Divide capacity by 2 to take expansion into account
1057 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001058 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001059 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001060 toWrite = audioBuffer.size;
1061 memcpy(audioBuffer.i8, src, toWrite);
1062 src += toWrite;
1063 }
1064 userSize -= toWrite;
1065 written += toWrite;
1066
1067 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001068 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001069
1070 return written;
1071}
1072
1073// -------------------------------------------------------------------------
1074
John Grossman4ff14ba2012-02-08 16:37:41 -08001075TimedAudioTrack::TimedAudioTrack() {
1076 mIsTimed = true;
1077}
1078
1079status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1080{
1081 status_t result = UNKNOWN_ERROR;
1082
1083 // If the track is not invalid already, try to allocate a buffer. alloc
1084 // fails indicating that the server is dead, flag the track as invalid so
1085 // we can attempt to restore in in just a bit.
1086 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1087 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1088 if (result == DEAD_OBJECT) {
1089 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1090 }
1091 }
1092
1093 // If the track is invalid at this point, attempt to restore it. and try the
1094 // allocation one more time.
1095 if (mCblk->flags & CBLK_INVALID_MSK) {
1096 mCblk->lock.lock();
1097 result = restoreTrack_l(mCblk, false);
1098 mCblk->lock.unlock();
1099
1100 if (result == OK)
1101 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1102 }
1103
1104 return result;
1105}
1106
1107status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1108 int64_t pts)
1109{
1110 // restart track if it was disabled by audioflinger due to previous underrun
1111 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1112 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1113 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten3acbd052012-02-28 10:39:56 -08001114 mAudioTrack->start();
John Grossman4ff14ba2012-02-08 16:37:41 -08001115 }
1116
1117 return mAudioTrack->queueTimedBuffer(buffer, pts);
1118}
1119
1120status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1121 TargetTimeline target)
1122{
1123 return mAudioTrack->setMediaTimeTransform(xform, target);
1124}
1125
1126// -------------------------------------------------------------------------
1127
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1129{
1130 Buffer audioBuffer;
1131 uint32_t frames;
1132 size_t writtenSize;
1133
Eric Laurent1703cdf2011-03-07 14:52:59 -08001134 mLock.lock();
1135 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1136 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001137 sp<IAudioTrack> audioTrack = mAudioTrack;
1138 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001139 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001140 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001141 mLock.unlock();
1142
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001143 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001144 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001145 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001146 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001147 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001148 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001149 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001150 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001151 if (mSharedBuffer != 0) return false;
1152 }
1153 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001154
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001155 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001156 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001157 int loopCount = -1;
1158 mLoopCount--;
1159 if (mLoopCount >= 0) loopCount = mLoopCount;
1160
1161 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1162 }
1163
1164 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001165 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001166 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001168 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001169 }
1170 }
1171
1172 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001173 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001174 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001175 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1176 mNewPosition += mUpdatePeriod;
1177 }
1178 }
1179
1180 // If Shared buffer is used, no data is requested from client.
1181 if (mSharedBuffer != 0) {
1182 frames = 0;
1183 } else {
1184 frames = mRemainingFrames;
1185 }
1186
Glenn Kasten99e53b82012-01-19 08:59:58 -08001187 // See description of waitCount parameter at declaration of obtainBuffer().
1188 // The logic below prevents us from being stuck below at obtainBuffer()
1189 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001190 int32_t waitCount = -1;
1191 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1192 waitCount = 1;
1193 }
1194
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001195 do {
1196
1197 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001198
Eric Laurent2267ba12011-09-07 11:13:23 -07001199 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001200 if (err < NO_ERROR) {
1201 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001202 ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203 return false;
1204 }
1205 break;
1206 }
1207 if (err == status_t(STOPPED)) return false;
1208
1209 // Divide buffer size by 2 to take into account the expansion
1210 // due to 8 to 16 bit conversion: the callback must fill only half
1211 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001212 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 audioBuffer.size >>= 1;
1214 }
1215
1216 size_t reqSize = audioBuffer.size;
1217 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1218 writtenSize = audioBuffer.size;
1219
1220 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001221 if (ssize_t(writtenSize) <= 0) {
1222 // The callback is done filling buffers
1223 // Keep this thread going to handle timed events and
1224 // still try to get more data in intervals of WAIT_PERIOD_MS
1225 // but don't just loop and block the CPU, so wait
1226 usleep(WAIT_PERIOD_MS*1000);
1227 break;
1228 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229 if (writtenSize > reqSize) writtenSize = reqSize;
1230
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001231 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001232 // 8 to 16 bit conversion, note that source and destination are the same address
1233 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234 writtenSize <<= 1;
1235 }
1236
1237 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001238 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001239 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001240 // 16 bit.
1241 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001243 frames -= audioBuffer.frameCount;
1244
1245 releaseBuffer(&audioBuffer);
1246 }
1247 while (frames);
1248
1249 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001250 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 } else {
1252 mRemainingFrames = frames;
1253 }
1254 return true;
1255}
1256
Eric Laurent1703cdf2011-03-07 14:52:59 -08001257// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1258// the IAudioTrack and IMemory in case they are recreated here.
1259// If the IAudioTrack is successfully restored, the cblk pointer is updated
1260status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1261{
1262 status_t result;
1263
Eric Laurent38ccae22011-03-28 18:37:07 -07001264 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001265 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001266 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001267
Eric Laurent1703cdf2011-03-07 14:52:59 -08001268 // signal old cblk condition so that other threads waiting for available buffers stop
1269 // waiting now
1270 cblk->cv.broadcast();
1271 cblk->lock.unlock();
1272
Eric Laurent9f6530f2011-08-30 10:18:54 -07001273 // refresh the audio configuration cache in this process to make sure we get new
1274 // output parameters in getOutput_l() and createTrack_l()
1275 AudioSystem::clearAudioConfigCache();
1276
Eric Laurent1703cdf2011-03-07 14:52:59 -08001277 // if the new IAudioTrack is created, createTrack_l() will modify the
1278 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1279 // It will also delete the strong references on previous IAudioTrack and IMemory
1280 result = createTrack_l(mStreamType,
1281 cblk->sampleRate,
1282 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001283 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001284 mFrameCount,
1285 mFlags,
1286 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001287 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001288
1289 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001290 uint32_t user = cblk->user;
1291 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001292 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001293 mCblk->user = user;
1294 mCblk->server = user;
1295 mCblk->userBase = user;
1296 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001297 // restore loop: this is not guaranteed to succeed if new frame count is not
1298 // compatible with loop length
1299 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001300 if (!fromStart) {
1301 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001302 // Make sure that a client relying on callback events indicating underrun or
1303 // the actual amount of audio frames played (e.g SoundPool) receives them.
1304 if (mSharedBuffer == 0) {
1305 uint32_t frames = 0;
1306 if (user > server) {
1307 frames = ((user - server) > mCblk->frameCount) ?
1308 mCblk->frameCount : (user - server);
1309 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1310 }
1311 // restart playback even if buffer is not completely filled.
1312 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1313 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1314 // the client
1315 mCblk->stepUser(frames);
1316 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001317 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001318 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001319 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001320 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001321 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001322 if (fromStart && result == NO_ERROR) {
1323 mNewPosition = mCblk->server + mUpdatePeriod;
1324 }
1325 }
1326 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001327 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001328 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001329 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001330 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001331 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001332 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001333 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001334 } else {
1335 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001336 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001337 mLock.unlock();
1338 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001339 if (result == NO_ERROR) {
1340 result = mRestoreStatus;
1341 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001342 cblk->lock.unlock();
1343 mLock.lock();
1344 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001345 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001346 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001347 cblk->lock.unlock();
1348 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001349 }
Steve Block3856b092011-10-20 11:56:00 +01001350 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001351 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001352
1353 if (result == NO_ERROR) {
1354 // from now on we switch to the newly created cblk
1355 cblk = mCblk;
1356 }
1357 cblk->lock.lock();
1358
Steve Block5ff1dd52012-01-05 23:22:43 +00001359 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001360
1361 return result;
1362}
1363
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1365{
1366
1367 const size_t SIZE = 256;
1368 char buffer[SIZE];
1369 String8 result;
1370
1371 result.append(" AudioTrack::dump\n");
1372 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1373 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001374 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001375 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001376 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001377 result.append(buffer);
1378 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1379 result.append(buffer);
1380 ::write(fd, result.string(), result.size());
1381 return NO_ERROR;
1382}
1383
1384// =========================================================================
1385
1386AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001387 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1388{
1389}
1390
1391AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001392{
1393}
1394
1395bool AudioTrack::AudioTrackThread::threadLoop()
1396{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001397 {
1398 AutoMutex _l(mMyLock);
1399 if (mPaused) {
1400 mMyCond.wait(mMyLock);
1401 // caller will check for exitPending()
1402 return true;
1403 }
1404 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001405 if (!mReceiver.processAudioBuffer(this)) {
1406 pause();
1407 }
1408 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001409}
1410
1411status_t AudioTrack::AudioTrackThread::readyToRun()
1412{
1413 return NO_ERROR;
1414}
1415
1416void AudioTrack::AudioTrackThread::onFirstRef()
1417{
1418}
1419
Glenn Kasten3acbd052012-02-28 10:39:56 -08001420void AudioTrack::AudioTrackThread::requestExit()
1421{
1422 // must be in this order to avoid a race condition
1423 Thread::requestExit();
1424 mMyCond.signal();
1425}
1426
1427void AudioTrack::AudioTrackThread::pause()
1428{
1429 AutoMutex _l(mMyLock);
1430 mPaused = true;
1431}
1432
1433void AudioTrack::AudioTrackThread::resume()
1434{
1435 AutoMutex _l(mMyLock);
1436 if (mPaused) {
1437 mPaused = false;
1438 mMyCond.signal();
1439 }
1440}
1441
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001442// =========================================================================
1443
Eric Laurent38ccae22011-03-28 18:37:07 -07001444
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001445audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001446 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001447 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001448 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001449 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001450{
1451}
1452
1453uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1454{
Marco Nelissena1472d92012-03-30 14:36:54 -07001455 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456
Marco Nelissena1472d92012-03-30 14:36:54 -07001457 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458 u += frameCount;
1459 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001460 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1462 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1463 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1464 }
Glenn Kasten90548972011-12-13 11:45:07 -08001465 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001466 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001467 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001468 }
1469
Marco Nelissena1472d92012-03-30 14:36:54 -07001470 uint32_t fc = this->frameCount;
1471 if (u >= fc) {
1472 // common case, user didn't just wrap
1473 if (u - fc >= userBase ) {
1474 userBase += fc;
1475 }
1476 } else if (u >= userBase + fc) {
1477 // user just wrapped
1478 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001479 }
1480
Glenn Kasten90548972011-12-13 11:45:07 -08001481 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001482
1483 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001484 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001485 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001486 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001487
1488 return u;
1489}
1490
1491bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1492{
Marco Nelissena1472d92012-03-30 14:36:54 -07001493 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1494
Eric Laurent38ccae22011-03-28 18:37:07 -07001495 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001496 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001497 return false;
1498 }
1499
Glenn Kasten90548972011-12-13 11:45:07 -08001500 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001501 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502
1503 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001504 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505 // Mark that we have read the first buffer so that next time stepUser() is called
1506 // we switch to normal obtainBuffer() timeout period
1507 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001508 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001509 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001510 // It is possible that we receive a flush()
1511 // while the mixer is processing a block: in this case,
1512 // stepServer() is called After the flush() has reset u & s and
1513 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001514 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001515 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001516 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001517 }
1518 }
1519
1520 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001521 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001522 s = loopStart;
1523 if (--loopCount == 0) {
1524 loopEnd = UINT_MAX;
1525 loopStart = UINT_MAX;
1526 }
1527 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001528
1529 uint32_t fc = this->frameCount;
1530 if (s >= fc) {
1531 // common case, server didn't just wrap
1532 if (s - fc >= serverBase ) {
1533 serverBase += fc;
1534 }
1535 } else if (s >= serverBase + fc) {
1536 // server just wrapped
1537 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001538 }
1539
Glenn Kasten90548972011-12-13 11:45:07 -08001540 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001541
Eric Laurent1703cdf2011-03-07 14:52:59 -08001542 if (!(flags & CBLK_INVALID_MSK)) {
1543 cv.signal();
1544 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001545 lock.unlock();
1546 return true;
1547}
1548
1549void* audio_track_cblk_t::buffer(uint32_t offset) const
1550{
Glenn Kasten90548972011-12-13 11:45:07 -08001551 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552}
1553
1554uint32_t audio_track_cblk_t::framesAvailable()
1555{
1556 Mutex::Autolock _l(lock);
1557 return framesAvailable_l();
1558}
1559
1560uint32_t audio_track_cblk_t::framesAvailable_l()
1561{
Glenn Kasten90548972011-12-13 11:45:07 -08001562 uint32_t u = user;
1563 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001564
Eric Laurentd1b449a2010-05-14 03:26:45 -07001565 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001566 uint32_t limit = (s < loopStart) ? s : loopStart;
1567 return limit + frameCount - u;
1568 } else {
1569 return frameCount + u - s;
1570 }
1571}
1572
1573uint32_t audio_track_cblk_t::framesReady()
1574{
Glenn Kasten90548972011-12-13 11:45:07 -08001575 uint32_t u = user;
1576 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577
Eric Laurentd1b449a2010-05-14 03:26:45 -07001578 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001579 if (u < loopEnd) {
1580 return u - s;
1581 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001582 // do not block on mutex shared with client on AudioFlinger side
1583 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001584 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001585 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001586 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001587 uint32_t frames = UINT_MAX;
1588 if (loopCount >= 0) {
1589 frames = (loopEnd - loopStart)*loopCount + u - s;
1590 }
1591 lock.unlock();
1592 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001593 }
1594 } else {
1595 return s - u;
1596 }
1597}
1598
Eric Laurent38ccae22011-03-28 18:37:07 -07001599bool audio_track_cblk_t::tryLock()
1600{
1601 // the code below simulates lock-with-timeout
1602 // we MUST do this to protect the AudioFlinger server
1603 // as this lock is shared with the client.
1604 status_t err;
1605
1606 err = lock.tryLock();
1607 if (err == -EBUSY) { // just wait a bit
1608 usleep(1000);
1609 err = lock.tryLock();
1610 }
1611 if (err != NO_ERROR) {
1612 // probably, the client just died.
1613 return false;
1614 }
1615 return true;
1616}
1617
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001618// -------------------------------------------------------------------------
1619
1620}; // namespace android