blob: 177359bcb59ed4b94c5db19b27026cf3dafd9b82 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
26#include <sched.h>
27#include <sys/resource.h>
28
29#include <private/media/AudioTrackShared.h>
30
31#include <media/AudioSystem.h>
32#include <media/AudioTrack.h>
33
34#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Glenn Kasten511754b2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
53 int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 uint32_t sampleRate)
56{
Glenn Kasten04cd0182012-06-25 11:49:27 -070057 if (frameCount == NULL) return BAD_VALUE;
58
59 // default to 0 in case of error
60 *frameCount = 0;
61
Glenn Kastene0fa4672012-04-24 14:35:14 -070062 // FIXME merge with similar code in createTrack_l(), except we're missing
63 // some information here that is available in createTrack_l():
64 // audio_io_handle_t output
65 // audio_format_t format
66 // audio_channel_mask_t channelMask
67 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 int afSampleRate;
69 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
70 return NO_INIT;
71 }
72 int afFrameCount;
73 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 uint32_t afLatency;
77 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80
81 // Ensure that buffer depth covers at least audio hardware latency
82 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
83 if (minBufCount < 2) minBufCount = 2;
84
85 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070086 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080087 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
88 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080089 return NO_ERROR;
90}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92// ---------------------------------------------------------------------------
93
94AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070095 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080096 mIsTimed(false),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070098 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099{
100}
101
102AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800103 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800105 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700106 int 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
Glenn Kasten17a736c2012-02-14 08:52:15 -0800123// DEPRECATED
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800125 int streamType,
126 uint32_t sampleRate,
127 int format,
128 int channelMask,
129 int frameCount,
130 uint32_t flags,
131 callback_t cbf,
132 void* user,
133 int notificationFrames,
134 int sessionId)
135 : mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800136 mIsTimed(false),
Glenn Kastena6364332012-04-19 09:35:04 -0700137 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
Andreas Huberc8139852012-01-18 10:51:55 -0800138{
139 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700140 frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800141 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800142}
143
144AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800145 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800147 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700148 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700150 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 callback_t cbf,
152 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700153 int notificationFrames,
154 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700155 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800156 mIsTimed(false),
157 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700158 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700160 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800161 0 /*frameCount*/, flags, cbf, user, notificationFrames,
162 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163}
164
165AudioTrack::~AudioTrack()
166{
Steve Block3856b092011-10-20 11:56:00 +0100167 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168
169 if (mStatus == NO_ERROR) {
170 // Make sure that callback function exits in the case where
171 // it is looping on buffer full condition in obtainBuffer().
172 // Otherwise the callback thread will never exit.
173 stop();
174 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800175 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 mAudioTrackThread->requestExitAndWait();
177 mAudioTrackThread.clear();
178 }
179 mAudioTrack.clear();
180 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700181 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 }
183}
184
185status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800186 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800188 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700189 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700191 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192 callback_t cbf,
193 void* user,
194 int notificationFrames,
195 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700196 bool threadCanCallJava,
197 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198{
199
Steve Block3856b092011-10-20 11:56:00 +0100200 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201
Eric Laurent1a9ed112012-03-20 18:36:01 -0700202 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
203
Eric Laurent1703cdf2011-03-07 14:52:59 -0800204 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700205 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000206 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 return INVALID_OPERATION;
208 }
209
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 if (streamType == AUDIO_STREAM_DEFAULT) {
212 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700214
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700216 int afSampleRate;
217 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
218 return NO_INIT;
219 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 sampleRate = afSampleRate;
221 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700222
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800224 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700225 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800226 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700227 if (channelMask == 0) {
228 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229 }
230
231 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700232 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000233 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234 return BAD_VALUE;
235 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236
Glenn Kastene0fa4672012-04-24 14:35:14 -0700237 // AudioFlinger does not currently support 8-bit data in shared memory
238 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
239 ALOGE("8-bit data in shared memory is not supported");
240 return BAD_VALUE;
241 }
242
Eric Laurentc2f1f072009-07-17 12:17:14 -0700243 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700245 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800246 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700247 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700248 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700249 // only allow deep buffering for music stream type
250 if (streamType != AUDIO_STREAM_MUSIC) {
251 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
252 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700253
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700254 if (!audio_is_output_channel(channelMask)) {
Steve Block29357bc2012-01-06 19:20:56 +0000255 ALOGE("Invalid channel mask");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700256 return BAD_VALUE;
257 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700258 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800261 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800262 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800263 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700264
265 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000266 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267 return BAD_VALUE;
268 }
269
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800270 mVolume[LEFT] = 1.0f;
271 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800272 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700273 mFrameCount = frameCount;
274 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700275 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700276 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700277 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700278 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700279
Glenn Kasten3acbd052012-02-28 10:39:56 -0800280 if (cbf != NULL) {
281 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
282 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
283 }
284
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800285 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800286 status_t status = createTrack_l(streamType,
287 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800288 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700289 (uint32_t)channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800290 frameCount,
291 flags,
292 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700293 output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800295 if (status != NO_ERROR) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800296 if (mAudioTrackThread != 0) {
297 mAudioTrackThread->requestExit();
298 mAudioTrackThread.clear();
299 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300 return status;
301 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800302
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 mStatus = NO_ERROR;
304
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800306 mFormat = format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700307 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 mChannelCount = channelCount;
309 mSharedBuffer = sharedBuffer;
310 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800311 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 mLoopCount = 0;
314 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700315 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 mNewPosition = 0;
317 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700318 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700319 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700320 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321 return NO_ERROR;
322}
323
324status_t AudioTrack::initCheck() const
325{
326 return mStatus;
327}
328
329// -------------------------------------------------------------------------
330
331uint32_t AudioTrack::latency() const
332{
333 return mLatency;
334}
335
Glenn Kastenfff6d712012-01-12 16:38:12 -0800336audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337{
338 return mStreamType;
339}
340
Glenn Kastene1c39622012-01-04 09:36:37 -0800341audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342{
343 return mFormat;
344}
345
346int AudioTrack::channelCount() const
347{
348 return mChannelCount;
349}
350
351uint32_t AudioTrack::frameCount() const
352{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700353 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354}
355
Glenn Kastenb9980652012-01-11 09:48:27 -0800356size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357{
Dima Zavinfce7a472011-04-19 22:30:36 -0700358 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700359 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 } else {
361 return sizeof(uint8_t);
362 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363}
364
365sp<IMemory>& AudioTrack::sharedBuffer()
366{
367 return mSharedBuffer;
368}
369
370// -------------------------------------------------------------------------
371
372void AudioTrack::start()
373{
374 sp<AudioTrackThread> t = mAudioTrackThread;
375
Steve Block3856b092011-10-20 11:56:00 +0100376 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377
Eric Laurentf5aafb22010-11-18 08:40:16 -0800378 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800379 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
380 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700381 sp<IAudioTrack> audioTrack = mAudioTrack;
382 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800383 audio_track_cblk_t* cblk = mCblk;
384
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800385 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700386 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800387 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800388 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700389 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800390 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
391 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700392 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800393 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800394 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800395 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700396 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700397 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700398 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800399 }
400
Steve Block3856b092011-10-20 11:56:00 +0100401 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700402 status_t status = NO_ERROR;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800403 if (!(cblk->flags & CBLK_INVALID_MSK)) {
404 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800405 ALOGV("mAudioTrack->start()");
406 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800407 cblk->lock.lock();
408 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700409 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800410 }
Eric Laurent2b584242009-11-09 23:32:22 -0800411 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800412 if (cblk->flags & CBLK_INVALID_MSK) {
413 status = restoreTrack_l(cblk, true);
414 }
415 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800416 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100417 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800418 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800419 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800420 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800421 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700422 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700423 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800424 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800425 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 }
427
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428}
429
430void AudioTrack::stop()
431{
432 sp<AudioTrackThread> t = mAudioTrackThread;
433
Steve Block3856b092011-10-20 11:56:00 +0100434 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435
Eric Laurentf5aafb22010-11-18 08:40:16 -0800436 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800437 if (mActive) {
438 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700439 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440 mAudioTrack->stop();
441 // Cancel loops (If we are in the middle of a loop, playback
442 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800443 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700444 // the playback head position will reset to 0, so if a marker is set, we need
445 // to activate it again
446 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 // Force flush if a shared buffer is used otherwise audioflinger
448 // will not stop before end of buffer is reached.
449 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800450 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 }
452 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800453 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700455 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700456 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 }
458 }
459
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460}
461
462bool AudioTrack::stopped() const
463{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800464 AutoMutex lock(mLock);
465 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466}
467
468void AudioTrack::flush()
469{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800470 AutoMutex lock(mLock);
471 flush_l();
472}
473
474// must be called with mLock held
475void AudioTrack::flush_l()
476{
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700478
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700479 // clear playback marker and periodic update counter
480 mMarkerPosition = 0;
481 mMarkerReached = false;
482 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700485 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 mAudioTrack->flush();
487 // Release AudioTrack callback thread in case it was waiting for new buffers
488 // in AudioTrack::obtainBuffer()
489 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490 }
491}
492
493void AudioTrack::pause()
494{
Steve Block3856b092011-10-20 11:56:00 +0100495 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800496 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800497 if (mActive) {
498 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700499 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500 mAudioTrack->pause();
501 }
502}
503
504void AudioTrack::mute(bool e)
505{
506 mAudioTrack->mute(e);
507 mMuted = e;
508}
509
510bool AudioTrack::muted() const
511{
512 return mMuted;
513}
514
Eric Laurentbe916aa2010-06-01 23:49:17 -0700515status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800517 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700518 return BAD_VALUE;
519 }
520
Eric Laurent1703cdf2011-03-07 14:52:59 -0800521 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 mVolume[LEFT] = left;
523 mVolume[RIGHT] = right;
524
Glenn Kasten83d86532012-01-17 14:39:34 -0800525 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700526
527 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528}
529
Glenn Kastena5224f32012-01-04 12:41:44 -0800530void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700532 if (left != NULL) {
533 *left = mVolume[LEFT];
534 }
535 if (right != NULL) {
536 *right = mVolume[RIGHT];
537 }
538}
539
Eric Laurent2beeb502010-07-16 07:43:46 -0700540status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700541{
Steve Block3856b092011-10-20 11:56:00 +0100542 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800543 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700544 return BAD_VALUE;
545 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800546 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700547
548 mSendLevel = level;
549
Glenn Kasten05632a52012-01-03 14:22:33 -0800550 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700551
552 return NO_ERROR;
553}
554
Glenn Kastena5224f32012-01-04 12:41:44 -0800555void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700556{
557 if (level != NULL) {
558 *level = mSendLevel;
559 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560}
561
Eric Laurent57326622009-07-07 07:10:45 -0700562status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563{
564 int afSamplingRate;
565
John Grossman4ff14ba2012-02-08 16:37:41 -0800566 if (mIsTimed) {
567 return INVALID_OPERATION;
568 }
569
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800570 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700571 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572 }
573 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700574 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575
Eric Laurent1703cdf2011-03-07 14:52:59 -0800576 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700577 mCblk->sampleRate = rate;
578 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579}
580
Glenn Kastena5224f32012-01-04 12:41:44 -0800581uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582{
John Grossman4ff14ba2012-02-08 16:37:41 -0800583 if (mIsTimed) {
584 return INVALID_OPERATION;
585 }
586
Eric Laurent1703cdf2011-03-07 14:52:59 -0800587 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700588 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589}
590
591status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
592{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800593 AutoMutex lock(mLock);
594 return setLoop_l(loopStart, loopEnd, loopCount);
595}
596
597// must be called with mLock held
598status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
599{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600 audio_track_cblk_t* cblk = mCblk;
601
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602 Mutex::Autolock _l(cblk->lock);
603
604 if (loopCount == 0) {
605 cblk->loopStart = UINT_MAX;
606 cblk->loopEnd = UINT_MAX;
607 cblk->loopCount = 0;
608 mLoopCount = 0;
609 return NO_ERROR;
610 }
611
John Grossman4ff14ba2012-02-08 16:37:41 -0800612 if (mIsTimed) {
613 return INVALID_OPERATION;
614 }
615
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800616 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700617 loopEnd - loopStart > cblk->frameCount ||
618 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000619 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 -0800620 return BAD_VALUE;
621 }
622
Eric Laurent9b7d9502011-03-21 11:49:00 -0700623 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000624 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700625 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700627 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628
629 cblk->loopStart = loopStart;
630 cblk->loopEnd = loopEnd;
631 cblk->loopCount = loopCount;
632 mLoopCount = loopCount;
633
634 return NO_ERROR;
635}
636
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637status_t AudioTrack::setMarkerPosition(uint32_t marker)
638{
Glenn Kastena0d68332012-01-27 16:47:15 -0800639 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640
641 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700642 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643
644 return NO_ERROR;
645}
646
Glenn Kastena5224f32012-01-04 12:41:44 -0800647status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648{
Glenn Kastena0d68332012-01-27 16:47:15 -0800649 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650
651 *marker = mMarkerPosition;
652
653 return NO_ERROR;
654}
655
656status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
657{
Glenn Kastena0d68332012-01-27 16:47:15 -0800658 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659
660 uint32_t curPosition;
661 getPosition(&curPosition);
662 mNewPosition = curPosition + updatePeriod;
663 mUpdatePeriod = updatePeriod;
664
665 return NO_ERROR;
666}
667
Glenn Kastena5224f32012-01-04 12:41:44 -0800668status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669{
Glenn Kastena0d68332012-01-27 16:47:15 -0800670 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671
672 *updatePeriod = mUpdatePeriod;
673
674 return NO_ERROR;
675}
676
677status_t AudioTrack::setPosition(uint32_t position)
678{
John Grossman4ff14ba2012-02-08 16:37:41 -0800679 if (mIsTimed) return INVALID_OPERATION;
680
Eric Laurent1703cdf2011-03-07 14:52:59 -0800681 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800683 if (!stopped_l()) return INVALID_OPERATION;
684
685 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686
687 if (position > mCblk->user) return BAD_VALUE;
688
689 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700690 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700691
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692 return NO_ERROR;
693}
694
695status_t AudioTrack::getPosition(uint32_t *position)
696{
Glenn Kastena0d68332012-01-27 16:47:15 -0800697 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800698 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700699 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700
701 return NO_ERROR;
702}
703
704status_t AudioTrack::reload()
705{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800706 AutoMutex lock(mLock);
707
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800708 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700709
Eric Laurent1703cdf2011-03-07 14:52:59 -0800710 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711
Eric Laurentd1b449a2010-05-14 03:26:45 -0700712 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800713
714 return NO_ERROR;
715}
716
Eric Laurentc2f1f072009-07-17 12:17:14 -0700717audio_io_handle_t AudioTrack::getOutput()
718{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800719 AutoMutex lock(mLock);
720 return getOutput_l();
721}
722
723// must be called with mLock held
724audio_io_handle_t AudioTrack::getOutput_l()
725{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800726 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800727 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700728}
729
Glenn Kastena5224f32012-01-04 12:41:44 -0800730int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700731{
732 return mSessionId;
733}
734
735status_t AudioTrack::attachAuxEffect(int effectId)
736{
Steve Block3856b092011-10-20 11:56:00 +0100737 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700738 status_t status = mAudioTrack->attachAuxEffect(effectId);
739 if (status == NO_ERROR) {
740 mAuxEffectId = effectId;
741 }
742 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700743}
744
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800745// -------------------------------------------------------------------------
746
Eric Laurent1703cdf2011-03-07 14:52:59 -0800747// must be called with mLock held
748status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800749 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800750 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800751 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700752 uint32_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800753 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700754 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800755 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700756 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800757{
758 status_t status;
759 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
760 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700761 ALOGE("Could not get audioflinger");
762 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800763 }
764
Eric Laurentd1b449a2010-05-14 03:26:45 -0700765 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700766 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700767 return NO_INIT;
768 }
769
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700770 // Client decides whether the track is TIMED (see below), but can only express a preference
771 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700772 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700773 // either of these use cases:
774 // use case 1: shared buffer
775 (sharedBuffer != 0) ||
776 // use case 2: callback handler
777 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800778 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700779 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700780 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700781 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700782 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700783 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700784
Eric Laurentd1b449a2010-05-14 03:26:45 -0700785 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700786
Dima Zavinfce7a472011-04-19 22:30:36 -0700787 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700788
Eric Laurentd1b449a2010-05-14 03:26:45 -0700789 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700790 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700791 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700792 } else if (frameCount == 0) {
793 int afFrameCount;
794 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
795 return NO_INIT;
796 }
797 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700798 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700799
800 } else if (sharedBuffer != 0) {
801
802 // Ensure that buffer alignment matches channelCount
803 int channelCount = popcount(channelMask);
804 // 8-bit data in shared memory is not currently supported by AudioFlinger
805 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
806 if (channelCount > 1) {
807 // More than 2 channels does not require stronger alignment than stereo
808 alignment <<= 1;
809 }
810 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
811 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
812 sharedBuffer->pointer(), channelCount);
813 return BAD_VALUE;
814 }
815
816 // When initializing a shared buffer AudioTrack via constructors,
817 // there's no frameCount parameter.
818 // But when initializing a shared buffer AudioTrack via set(),
819 // there _is_ a frameCount parameter. We silently ignore it.
820 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
821
822 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
823
824 // FIXME move these calculations and associated checks to server
825 int afSampleRate;
826 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
827 return NO_INIT;
828 }
829 int afFrameCount;
830 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
831 return NO_INIT;
832 }
833
Eric Laurentd1b449a2010-05-14 03:26:45 -0700834 // Ensure that buffer depth covers at least audio hardware latency
835 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
836 if (minBufCount < 2) minBufCount = 2;
837
838 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800839 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
840 ", afLatency=%d",
841 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700842
843 if (frameCount == 0) {
844 frameCount = minFrameCount;
845 }
846 if (mNotificationFramesAct == 0) {
847 mNotificationFramesAct = frameCount/2;
848 }
849 // Make sure that application is notified with sufficient margin
850 // before underrun
851 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
852 mNotificationFramesAct = frameCount/2;
853 }
854 if (frameCount < minFrameCount) {
855 // not ALOGW because it happens all the time when playing key clicks over A2DP
856 ALOGV("Minimum buffer size corrected from %d to %d",
857 frameCount, minFrameCount);
858 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800859 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700860
Glenn Kastene0fa4672012-04-24 14:35:14 -0700861 } else {
862 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700863 }
864
Glenn Kastena075db42012-03-06 11:22:44 -0800865 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
866 if (mIsTimed) {
867 trackFlags |= IAudioFlinger::TRACK_TIMED;
868 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800869
870 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700871 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700872 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800873 if (mAudioTrackThread != 0) {
874 tid = mAudioTrackThread->getTid();
875 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700876 }
877
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800878 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
879 streamType,
880 sampleRate,
881 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700882 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800883 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800884 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800885 sharedBuffer,
886 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800887 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700888 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800889 &status);
890
891 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000892 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800893 return status;
894 }
895 sp<IMemory> cblk = track->getCblk();
896 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000897 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800898 return NO_INIT;
899 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800900 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800901 mCblkMemory = cblk;
902 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -0800903 // old has the previous value of mCblk->flags before the "or" operation
904 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
905 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
906 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700907 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800908 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700909 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700910 // once denied, do not request again if IAudioTrack is re-created
911 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
912 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800913 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914 if (sharedBuffer == 0) {
915 mNotificationFramesAct = mCblk->frameCount/2;
916 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800917 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800918 if (sharedBuffer == 0) {
919 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
920 } else {
921 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700922 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700923 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800924 }
925
Glenn Kasten83d86532012-01-17 14:39:34 -0800926 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800927 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700928 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800929 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
930 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700931 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700932 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -0700933 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700934 // If IAudioTrack is re-created, don't let the requested frameCount
935 // decrease. This can confuse clients that cache frameCount().
936 if (mCblk->frameCount > mFrameCount) {
937 mFrameCount = mCblk->frameCount;
938 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800939 return NO_ERROR;
940}
941
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
943{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800944 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800945 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700946 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 audio_track_cblk_t* cblk = mCblk;
948 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700949 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950
951 audioBuffer->frameCount = 0;
952 audioBuffer->size = 0;
953
954 uint32_t framesAvail = cblk->framesAvailable();
955
Eric Laurent9b7d9502011-03-21 11:49:00 -0700956 cblk->lock.lock();
957 if (cblk->flags & CBLK_INVALID_MSK) {
958 goto create_new_track;
959 }
960 cblk->lock.unlock();
961
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800963 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 goto start_loop_here;
965 while (framesAvail == 0) {
966 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800967 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100968 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800969 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 return NO_MORE_BUFFERS;
971 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800972 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800973 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800975 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700976 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800977 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700978 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700979 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800980 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800981 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800982 return status_t(STOPPED);
983 }
984 cblk->lock.lock();
985 }
986
987 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700988 goto create_new_track;
989 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800990 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700991 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800992 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
993 // timing out when a loop has been set and we have already written upto loop end
994 // is a normal condition: no need to wake AudioFlinger up.
995 if (cblk->user < cblk->loopEnd) {
Glenn Kasten0c9d26d2012-05-31 14:35:01 -0700996 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p name=%#x"
997 "user=%08x, server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700998 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800999 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001000 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001002 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001003 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001004create_new_track:
1005 result = restoreTrack_l(cblk, false);
1006 }
1007 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001008 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001009 cblk->lock.unlock();
1010 return result;
1011 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001012 }
1013 cblk->waitTimeMs = 0;
1014 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001015
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001016 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001017 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001018 return TIMED_OUT;
1019 }
1020 }
1021 // read the server count again
1022 start_loop_here:
1023 framesAvail = cblk->framesAvailable_l();
1024 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001025 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026 }
1027
1028 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001029
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001030 if (framesReq > framesAvail) {
1031 framesReq = framesAvail;
1032 }
1033
1034 uint32_t u = cblk->user;
1035 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1036
Marco Nelissena1472d92012-03-30 14:36:54 -07001037 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038 framesReq = bufferEnd - u;
1039 }
1040
Eric Laurentc2f1f072009-07-17 12:17:14 -07001041 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1042 audioBuffer->channelCount = mChannelCount;
1043 audioBuffer->frameCount = framesReq;
1044 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001045 if (audio_is_linear_pcm(mFormat)) {
1046 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001047 } else {
1048 audioBuffer->format = mFormat;
1049 }
1050 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001051 active = mActive;
1052 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1053}
1054
1055void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1056{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001057 AutoMutex lock(mLock);
1058 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001059 if (audioBuffer->frameCount > 0) {
1060 // restart track if it was disabled by audioflinger due to previous underrun
1061 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1062 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001063 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001064 mAudioTrack->start();
1065 }
1066 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001067}
1068
1069// -------------------------------------------------------------------------
1070
1071ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1072{
1073
1074 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001075 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076
1077 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001078 // Sanity-check: user is most-likely passing an error code, and it would
1079 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001080 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001081 buffer, userSize, userSize);
1082 return BAD_VALUE;
1083 }
1084
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001086
Eric Laurentdf839842012-05-31 14:27:14 -07001087 if (userSize == 0) {
1088 return 0;
1089 }
1090
Eric Laurent1703cdf2011-03-07 14:52:59 -08001091 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1092 // while we are accessing the cblk
1093 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001094 sp<IAudioTrack> audioTrack = mAudioTrack;
1095 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001096 mLock.unlock();
1097
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001098 ssize_t written = 0;
1099 const int8_t *src = (const int8_t *)buffer;
1100 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001101 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001102
1103 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001104 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001105
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001106 status_t err = obtainBuffer(&audioBuffer, -1);
1107 if (err < 0) {
1108 // out of buffers, return #bytes written
1109 if (err == status_t(NO_MORE_BUFFERS))
1110 break;
1111 return ssize_t(err);
1112 }
1113
1114 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001115
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001116 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001117 // Divide capacity by 2 to take expansion into account
1118 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001119 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001120 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001121 toWrite = audioBuffer.size;
1122 memcpy(audioBuffer.i8, src, toWrite);
1123 src += toWrite;
1124 }
1125 userSize -= toWrite;
1126 written += toWrite;
1127
1128 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001129 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001130
1131 return written;
1132}
1133
1134// -------------------------------------------------------------------------
1135
John Grossman4ff14ba2012-02-08 16:37:41 -08001136TimedAudioTrack::TimedAudioTrack() {
1137 mIsTimed = true;
1138}
1139
1140status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1141{
1142 status_t result = UNKNOWN_ERROR;
1143
1144 // If the track is not invalid already, try to allocate a buffer. alloc
1145 // fails indicating that the server is dead, flag the track as invalid so
1146 // we can attempt to restore in in just a bit.
1147 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1148 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1149 if (result == DEAD_OBJECT) {
1150 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1151 }
1152 }
1153
1154 // If the track is invalid at this point, attempt to restore it. and try the
1155 // allocation one more time.
1156 if (mCblk->flags & CBLK_INVALID_MSK) {
1157 mCblk->lock.lock();
1158 result = restoreTrack_l(mCblk, false);
1159 mCblk->lock.unlock();
1160
1161 if (result == OK)
1162 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1163 }
1164
1165 return result;
1166}
1167
1168status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1169 int64_t pts)
1170{
Eric Laurentdf839842012-05-31 14:27:14 -07001171 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1172 {
1173 AutoMutex lock(mLock);
1174 // restart track if it was disabled by audioflinger due to previous underrun
1175 if (buffer->size() != 0 && status == NO_ERROR &&
1176 mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1177 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1178 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1179 mAudioTrack->start();
1180 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001181 }
Eric Laurentdf839842012-05-31 14:27:14 -07001182 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001183}
1184
1185status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1186 TargetTimeline target)
1187{
1188 return mAudioTrack->setMediaTimeTransform(xform, target);
1189}
1190
1191// -------------------------------------------------------------------------
1192
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001193bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1194{
1195 Buffer audioBuffer;
1196 uint32_t frames;
1197 size_t writtenSize;
1198
Eric Laurent1703cdf2011-03-07 14:52:59 -08001199 mLock.lock();
1200 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1201 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001202 sp<IAudioTrack> audioTrack = mAudioTrack;
1203 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001204 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001205 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001206 mLock.unlock();
1207
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001209 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001210 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001211 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001212 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001213 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001214 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 if (mSharedBuffer != 0) return false;
1217 }
1218 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001219
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001220 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001221 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 int loopCount = -1;
1223 mLoopCount--;
1224 if (mLoopCount >= 0) loopCount = mLoopCount;
1225
1226 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1227 }
1228
1229 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001230 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001231 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001232 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001233 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234 }
1235 }
1236
1237 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001238 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001239 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1241 mNewPosition += mUpdatePeriod;
1242 }
1243 }
1244
1245 // If Shared buffer is used, no data is requested from client.
1246 if (mSharedBuffer != 0) {
1247 frames = 0;
1248 } else {
1249 frames = mRemainingFrames;
1250 }
1251
Glenn Kasten99e53b82012-01-19 08:59:58 -08001252 // See description of waitCount parameter at declaration of obtainBuffer().
1253 // The logic below prevents us from being stuck below at obtainBuffer()
1254 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001255 int32_t waitCount = -1;
1256 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1257 waitCount = 1;
1258 }
1259
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 do {
1261
1262 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001263
Eric Laurent2267ba12011-09-07 11:13:23 -07001264 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 if (err < NO_ERROR) {
1266 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001267 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 -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;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001304 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001305 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001306 // 16 bit.
1307 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1308
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
Eric Laurent1703cdf2011-03-07 14:52:59 -08001323// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1324// the IAudioTrack and IMemory in case they are recreated here.
1325// If the IAudioTrack is successfully restored, the cblk pointer is updated
1326status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1327{
1328 status_t result;
1329
Eric Laurent38ccae22011-03-28 18:37:07 -07001330 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001331 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001332 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001333
Eric Laurent1703cdf2011-03-07 14:52:59 -08001334 // signal old cblk condition so that other threads waiting for available buffers stop
1335 // waiting now
1336 cblk->cv.broadcast();
1337 cblk->lock.unlock();
1338
Eric Laurent9f6530f2011-08-30 10:18:54 -07001339 // refresh the audio configuration cache in this process to make sure we get new
1340 // output parameters in getOutput_l() and createTrack_l()
1341 AudioSystem::clearAudioConfigCache();
1342
Eric Laurent1703cdf2011-03-07 14:52:59 -08001343 // if the new IAudioTrack is created, createTrack_l() will modify the
1344 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1345 // It will also delete the strong references on previous IAudioTrack and IMemory
1346 result = createTrack_l(mStreamType,
1347 cblk->sampleRate,
1348 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001349 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001350 mFrameCount,
1351 mFlags,
1352 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001353 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001354
1355 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001356 uint32_t user = cblk->user;
1357 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001358 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001359 mCblk->user = user;
1360 mCblk->server = user;
1361 mCblk->userBase = user;
1362 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001363 // restore loop: this is not guaranteed to succeed if new frame count is not
1364 // compatible with loop length
1365 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001366 if (!fromStart) {
1367 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001368 // Make sure that a client relying on callback events indicating underrun or
1369 // the actual amount of audio frames played (e.g SoundPool) receives them.
1370 if (mSharedBuffer == 0) {
1371 uint32_t frames = 0;
1372 if (user > server) {
1373 frames = ((user - server) > mCblk->frameCount) ?
1374 mCblk->frameCount : (user - server);
1375 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1376 }
1377 // restart playback even if buffer is not completely filled.
1378 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1379 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1380 // the client
1381 mCblk->stepUser(frames);
1382 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001383 }
Eric Laurent29864602012-05-08 18:57:51 -07001384 if (mSharedBuffer != 0) {
1385 mCblk->stepUser(mCblk->frameCount);
1386 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001387 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001388 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001389 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001390 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001391 if (fromStart && result == NO_ERROR) {
1392 mNewPosition = mCblk->server + mUpdatePeriod;
1393 }
1394 }
1395 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001396 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001397 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001398 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001399 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001400 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001401 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001402 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001403 } else {
1404 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001405 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001406 mLock.unlock();
1407 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001408 if (result == NO_ERROR) {
1409 result = mRestoreStatus;
1410 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001411 cblk->lock.unlock();
1412 mLock.lock();
1413 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001414 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001415 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001416 cblk->lock.unlock();
1417 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001418 }
Steve Block3856b092011-10-20 11:56:00 +01001419 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001420 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001421
1422 if (result == NO_ERROR) {
1423 // from now on we switch to the newly created cblk
1424 cblk = mCblk;
1425 }
1426 cblk->lock.lock();
1427
Steve Block5ff1dd52012-01-05 23:22:43 +00001428 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001429
1430 return result;
1431}
1432
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001433status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1434{
1435
1436 const size_t SIZE = 256;
1437 char buffer[SIZE];
1438 String8 result;
1439
1440 result.append(" AudioTrack::dump\n");
1441 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1442 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001443 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 -08001444 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001445 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 -08001446 result.append(buffer);
1447 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1448 result.append(buffer);
1449 ::write(fd, result.string(), result.size());
1450 return NO_ERROR;
1451}
1452
1453// =========================================================================
1454
1455AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001456 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1457{
1458}
1459
1460AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461{
1462}
1463
1464bool AudioTrack::AudioTrackThread::threadLoop()
1465{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001466 {
1467 AutoMutex _l(mMyLock);
1468 if (mPaused) {
1469 mMyCond.wait(mMyLock);
1470 // caller will check for exitPending()
1471 return true;
1472 }
1473 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001474 if (!mReceiver.processAudioBuffer(this)) {
1475 pause();
1476 }
1477 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001478}
1479
Glenn Kasten3acbd052012-02-28 10:39:56 -08001480void AudioTrack::AudioTrackThread::requestExit()
1481{
1482 // must be in this order to avoid a race condition
1483 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001484 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001485}
1486
1487void AudioTrack::AudioTrackThread::pause()
1488{
1489 AutoMutex _l(mMyLock);
1490 mPaused = true;
1491}
1492
1493void AudioTrack::AudioTrackThread::resume()
1494{
1495 AutoMutex _l(mMyLock);
1496 if (mPaused) {
1497 mPaused = false;
1498 mMyCond.signal();
1499 }
1500}
1501
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502// =========================================================================
1503
Eric Laurent38ccae22011-03-28 18:37:07 -07001504
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001506 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001507 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001508 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001509 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001510{
1511}
1512
1513uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1514{
Marco Nelissena1472d92012-03-30 14:36:54 -07001515 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001516
Marco Nelissena1472d92012-03-30 14:36:54 -07001517 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001518 u += frameCount;
1519 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001520 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001521 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1522 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1523 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1524 }
Glenn Kasten90548972011-12-13 11:45:07 -08001525 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001526 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001527 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528 }
1529
Marco Nelissena1472d92012-03-30 14:36:54 -07001530 uint32_t fc = this->frameCount;
1531 if (u >= fc) {
1532 // common case, user didn't just wrap
1533 if (u - fc >= userBase ) {
1534 userBase += fc;
1535 }
1536 } else if (u >= userBase + fc) {
1537 // user just wrapped
1538 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001539 }
1540
Glenn Kasten90548972011-12-13 11:45:07 -08001541 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542
1543 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001544 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001545 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001546 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547
1548 return u;
1549}
1550
1551bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1552{
Marco Nelissena1472d92012-03-30 14:36:54 -07001553 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1554
Eric Laurent38ccae22011-03-28 18:37:07 -07001555 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001556 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001557 return false;
1558 }
1559
Glenn Kasten90548972011-12-13 11:45:07 -08001560 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001561 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001562
1563 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001564 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001565 // Mark that we have read the first buffer so that next time stepUser() is called
1566 // we switch to normal obtainBuffer() timeout period
1567 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001568 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001569 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001570 // It is possible that we receive a flush()
1571 // while the mixer is processing a block: in this case,
1572 // stepServer() is called After the flush() has reset u & s and
1573 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001574 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001575 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001576 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577 }
1578 }
1579
1580 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001581 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 s = loopStart;
1583 if (--loopCount == 0) {
1584 loopEnd = UINT_MAX;
1585 loopStart = UINT_MAX;
1586 }
1587 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001588
1589 uint32_t fc = this->frameCount;
1590 if (s >= fc) {
1591 // common case, server didn't just wrap
1592 if (s - fc >= serverBase ) {
1593 serverBase += fc;
1594 }
1595 } else if (s >= serverBase + fc) {
1596 // server just wrapped
1597 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001598 }
1599
Glenn Kasten90548972011-12-13 11:45:07 -08001600 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001601
Eric Laurent1703cdf2011-03-07 14:52:59 -08001602 if (!(flags & CBLK_INVALID_MSK)) {
1603 cv.signal();
1604 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605 lock.unlock();
1606 return true;
1607}
1608
1609void* audio_track_cblk_t::buffer(uint32_t offset) const
1610{
Glenn Kasten90548972011-12-13 11:45:07 -08001611 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001612}
1613
1614uint32_t audio_track_cblk_t::framesAvailable()
1615{
1616 Mutex::Autolock _l(lock);
1617 return framesAvailable_l();
1618}
1619
1620uint32_t audio_track_cblk_t::framesAvailable_l()
1621{
Glenn Kasten90548972011-12-13 11:45:07 -08001622 uint32_t u = user;
1623 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001624
Eric Laurentd1b449a2010-05-14 03:26:45 -07001625 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001626 uint32_t limit = (s < loopStart) ? s : loopStart;
1627 return limit + frameCount - u;
1628 } else {
1629 return frameCount + u - s;
1630 }
1631}
1632
1633uint32_t audio_track_cblk_t::framesReady()
1634{
Glenn Kasten90548972011-12-13 11:45:07 -08001635 uint32_t u = user;
1636 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001637
Eric Laurentd1b449a2010-05-14 03:26:45 -07001638 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001639 if (u < loopEnd) {
1640 return u - s;
1641 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001642 // do not block on mutex shared with client on AudioFlinger side
1643 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001644 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001645 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001646 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001647 uint32_t frames = UINT_MAX;
1648 if (loopCount >= 0) {
1649 frames = (loopEnd - loopStart)*loopCount + u - s;
1650 }
1651 lock.unlock();
1652 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653 }
1654 } else {
1655 return s - u;
1656 }
1657}
1658
Eric Laurent38ccae22011-03-28 18:37:07 -07001659bool audio_track_cblk_t::tryLock()
1660{
1661 // the code below simulates lock-with-timeout
1662 // we MUST do this to protect the AudioFlinger server
1663 // as this lock is shared with the client.
1664 status_t err;
1665
1666 err = lock.tryLock();
1667 if (err == -EBUSY) { // just wait a bit
1668 usleep(1000);
1669 err = lock.tryLock();
1670 }
1671 if (err != NO_ERROR) {
1672 // probably, the client just died.
1673 return false;
1674 }
1675 return true;
1676}
1677
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001678// -------------------------------------------------------------------------
1679
1680}; // namespace android