blob: 0ca035f92200a9eed1234e872e8d2df06026c2a0 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
26#include <sched.h>
27#include <sys/resource.h>
28
29#include <private/media/AudioTrackShared.h>
30
31#include <media/AudioSystem.h>
32#include <media/AudioTrack.h>
33
34#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Glenn Kasten511754b2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
53 int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 uint32_t sampleRate)
56{
Glenn Kasten04cd0182012-06-25 11:49:27 -070057 if (frameCount == NULL) return BAD_VALUE;
58
59 // default to 0 in case of error
60 *frameCount = 0;
61
Glenn Kastene0fa4672012-04-24 14:35:14 -070062 // FIXME merge with similar code in createTrack_l(), except we're missing
63 // some information here that is available in createTrack_l():
64 // audio_io_handle_t output
65 // audio_format_t format
66 // audio_channel_mask_t channelMask
67 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 int afSampleRate;
69 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
70 return NO_INIT;
71 }
72 int afFrameCount;
73 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 uint32_t afLatency;
77 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80
81 // Ensure that buffer depth covers at least audio hardware latency
82 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
83 if (minBufCount < 2) minBufCount = 2;
84
85 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070086 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080087 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
88 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080089 return NO_ERROR;
90}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92// ---------------------------------------------------------------------------
93
94AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070095 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080096 mIsTimed(false),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070098 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099{
100}
101
102AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800103 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800105 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700106 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700108 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 callback_t cbf,
110 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700111 int notificationFrames,
112 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700113 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800114 mIsTimed(false),
115 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700116 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700118 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700119 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800120 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121}
122
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{
Glenn Kasten28b76b32012-07-03 17:24:41 -0700139 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format,
140 (audio_channel_mask_t) channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700141 frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800142 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800143}
144
145AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800146 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800148 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700149 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700151 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 callback_t cbf,
153 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700154 int notificationFrames,
155 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700156 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800157 mIsTimed(false),
158 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700159 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700161 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800162 0 /*frameCount*/, flags, cbf, user, notificationFrames,
163 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164}
165
166AudioTrack::~AudioTrack()
167{
Steve Block3856b092011-10-20 11:56:00 +0100168 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169
170 if (mStatus == NO_ERROR) {
171 // Make sure that callback function exits in the case where
172 // it is looping on buffer full condition in obtainBuffer().
173 // Otherwise the callback thread will never exit.
174 stop();
175 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800176 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 mAudioTrackThread->requestExitAndWait();
178 mAudioTrackThread.clear();
179 }
180 mAudioTrack.clear();
181 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700182 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 }
184}
185
186status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800187 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800189 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700190 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700192 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 callback_t cbf,
194 void* user,
195 int notificationFrames,
196 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700197 bool threadCanCallJava,
198 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199{
200
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202
Eric Laurent1a9ed112012-03-20 18:36:01 -0700203 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
204
Eric Laurent1703cdf2011-03-07 14:52:59 -0800205 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700206 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000207 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 return INVALID_OPERATION;
209 }
210
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 if (streamType == AUDIO_STREAM_DEFAULT) {
213 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700215
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700217 int afSampleRate;
218 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
219 return NO_INIT;
220 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 sampleRate = afSampleRate;
222 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700223
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800225 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700226 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700228 if (channelMask == 0) {
229 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 }
231
232 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700233 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000234 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235 return BAD_VALUE;
236 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700237
Glenn Kastene0fa4672012-04-24 14:35:14 -0700238 // AudioFlinger does not currently support 8-bit data in shared memory
239 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
240 ALOGE("8-bit data in shared memory is not supported");
241 return BAD_VALUE;
242 }
243
Eric Laurentc2f1f072009-07-17 12:17:14 -0700244 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700246 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800247 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700248 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700250 // only allow deep buffering for music stream type
251 if (streamType != AUDIO_STREAM_MUSIC) {
252 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
253 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700255 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700256 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700257 return BAD_VALUE;
258 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700259 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700260
Dima Zavinfce7a472011-04-19 22:30:36 -0700261 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800262 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800263 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800264 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265
266 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000267 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 return BAD_VALUE;
269 }
270
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800271 mVolume[LEFT] = 1.0f;
272 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800273 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700274 mFrameCount = frameCount;
275 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700276 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700277 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700278 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700279 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700280
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800281 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800282 status_t status = createTrack_l(streamType,
283 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800284 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700285 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800286 frameCount,
287 flags,
288 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700289 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800290 if (status != NO_ERROR) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 return status;
292 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800293
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700294 if (cbf != NULL) {
295 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
296 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO);
297 }
298
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 mStatus = NO_ERROR;
300
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800302 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700303 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304 mChannelCount = channelCount;
305 mSharedBuffer = sharedBuffer;
306 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800307 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 mLoopCount = 0;
310 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700311 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 mNewPosition = 0;
313 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700314 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700315 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700316 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 return NO_ERROR;
318}
319
320status_t AudioTrack::initCheck() const
321{
322 return mStatus;
323}
324
325// -------------------------------------------------------------------------
326
327uint32_t AudioTrack::latency() const
328{
329 return mLatency;
330}
331
Glenn Kastenfff6d712012-01-12 16:38:12 -0800332audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333{
334 return mStreamType;
335}
336
Glenn Kastene1c39622012-01-04 09:36:37 -0800337audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338{
339 return mFormat;
340}
341
342int AudioTrack::channelCount() const
343{
344 return mChannelCount;
345}
346
347uint32_t AudioTrack::frameCount() const
348{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700349 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350}
351
Glenn Kastenb9980652012-01-11 09:48:27 -0800352size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353{
Dima Zavinfce7a472011-04-19 22:30:36 -0700354 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700355 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700356 } else {
357 return sizeof(uint8_t);
358 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359}
360
361sp<IMemory>& AudioTrack::sharedBuffer()
362{
363 return mSharedBuffer;
364}
365
366// -------------------------------------------------------------------------
367
368void AudioTrack::start()
369{
370 sp<AudioTrackThread> t = mAudioTrackThread;
371
Steve Block3856b092011-10-20 11:56:00 +0100372 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373
Eric Laurentf5aafb22010-11-18 08:40:16 -0800374 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800375 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
376 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700377 sp<IAudioTrack> audioTrack = mAudioTrack;
378 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800379 audio_track_cblk_t* cblk = mCblk;
380
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800381 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700382 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800383 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800384 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700385 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800386 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
387 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700388 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800389 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800390 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800391 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700392 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700393 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700394 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800395 }
396
Steve Block3856b092011-10-20 11:56:00 +0100397 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700398 status_t status = NO_ERROR;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800399 if (!(cblk->flags & CBLK_INVALID_MSK)) {
400 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800401 ALOGV("mAudioTrack->start()");
402 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800403 cblk->lock.lock();
404 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700405 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800406 }
Eric Laurent2b584242009-11-09 23:32:22 -0800407 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800408 if (cblk->flags & CBLK_INVALID_MSK) {
409 status = restoreTrack_l(cblk, true);
410 }
411 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800412 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100413 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800414 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800415 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800416 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800417 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700418 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700419 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800420 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800421 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 }
423
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424}
425
426void AudioTrack::stop()
427{
428 sp<AudioTrackThread> t = mAudioTrackThread;
429
Steve Block3856b092011-10-20 11:56:00 +0100430 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431
Eric Laurentf5aafb22010-11-18 08:40:16 -0800432 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800433 if (mActive) {
434 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700435 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 mAudioTrack->stop();
437 // Cancel loops (If we are in the middle of a loop, playback
438 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800439 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700440 // the playback head position will reset to 0, so if a marker is set, we need
441 // to activate it again
442 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443 // Force flush if a shared buffer is used otherwise audioflinger
444 // will not stop before end of buffer is reached.
445 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800446 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 }
448 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800449 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700451 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700452 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 }
454 }
455
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800456}
457
458bool AudioTrack::stopped() const
459{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800460 AutoMutex lock(mLock);
461 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462}
463
464void AudioTrack::flush()
465{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800466 AutoMutex lock(mLock);
467 flush_l();
468}
469
470// must be called with mLock held
471void AudioTrack::flush_l()
472{
Steve Block3856b092011-10-20 11:56:00 +0100473 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700474
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700475 // clear playback marker and periodic update counter
476 mMarkerPosition = 0;
477 mMarkerReached = false;
478 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700479
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700481 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482 mAudioTrack->flush();
483 // Release AudioTrack callback thread in case it was waiting for new buffers
484 // in AudioTrack::obtainBuffer()
485 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 }
487}
488
489void AudioTrack::pause()
490{
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800492 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800493 if (mActive) {
494 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700495 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496 mAudioTrack->pause();
497 }
498}
499
500void AudioTrack::mute(bool e)
501{
502 mAudioTrack->mute(e);
503 mMuted = e;
504}
505
506bool AudioTrack::muted() const
507{
508 return mMuted;
509}
510
Eric Laurentbe916aa2010-06-01 23:49:17 -0700511status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800513 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700514 return BAD_VALUE;
515 }
516
Eric Laurent1703cdf2011-03-07 14:52:59 -0800517 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 mVolume[LEFT] = left;
519 mVolume[RIGHT] = right;
520
Glenn Kasten83d86532012-01-17 14:39:34 -0800521 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700522
523 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524}
525
Glenn Kastena5224f32012-01-04 12:41:44 -0800526void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700528 if (left != NULL) {
529 *left = mVolume[LEFT];
530 }
531 if (right != NULL) {
532 *right = mVolume[RIGHT];
533 }
534}
535
Eric Laurent2beeb502010-07-16 07:43:46 -0700536status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700537{
Steve Block3856b092011-10-20 11:56:00 +0100538 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800539 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700540 return BAD_VALUE;
541 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800542 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700543
544 mSendLevel = level;
545
Glenn Kasten05632a52012-01-03 14:22:33 -0800546 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700547
548 return NO_ERROR;
549}
550
Glenn Kastena5224f32012-01-04 12:41:44 -0800551void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700552{
553 if (level != NULL) {
554 *level = mSendLevel;
555 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556}
557
Eric Laurent57326622009-07-07 07:10:45 -0700558status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559{
560 int afSamplingRate;
561
John Grossman4ff14ba2012-02-08 16:37:41 -0800562 if (mIsTimed) {
563 return INVALID_OPERATION;
564 }
565
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700567 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 }
569 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700570 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571
Eric Laurent1703cdf2011-03-07 14:52:59 -0800572 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700573 mCblk->sampleRate = rate;
574 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575}
576
Glenn Kastena5224f32012-01-04 12:41:44 -0800577uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578{
John Grossman4ff14ba2012-02-08 16:37:41 -0800579 if (mIsTimed) {
580 return INVALID_OPERATION;
581 }
582
Eric Laurent1703cdf2011-03-07 14:52:59 -0800583 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700584 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585}
586
587status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
588{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800589 AutoMutex lock(mLock);
590 return setLoop_l(loopStart, loopEnd, loopCount);
591}
592
593// must be called with mLock held
594status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
595{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 audio_track_cblk_t* cblk = mCblk;
597
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 Mutex::Autolock _l(cblk->lock);
599
600 if (loopCount == 0) {
601 cblk->loopStart = UINT_MAX;
602 cblk->loopEnd = UINT_MAX;
603 cblk->loopCount = 0;
604 mLoopCount = 0;
605 return NO_ERROR;
606 }
607
John Grossman4ff14ba2012-02-08 16:37:41 -0800608 if (mIsTimed) {
609 return INVALID_OPERATION;
610 }
611
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700613 loopEnd - loopStart > cblk->frameCount ||
614 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000615 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 -0800616 return BAD_VALUE;
617 }
618
Eric Laurent9b7d9502011-03-21 11:49:00 -0700619 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000620 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700621 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700623 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624
625 cblk->loopStart = loopStart;
626 cblk->loopEnd = loopEnd;
627 cblk->loopCount = loopCount;
628 mLoopCount = loopCount;
629
630 return NO_ERROR;
631}
632
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633status_t AudioTrack::setMarkerPosition(uint32_t marker)
634{
Glenn Kastena0d68332012-01-27 16:47:15 -0800635 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636
637 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700638 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639
640 return NO_ERROR;
641}
642
Glenn Kastena5224f32012-01-04 12:41:44 -0800643status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644{
Glenn Kastena0d68332012-01-27 16:47:15 -0800645 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646
647 *marker = mMarkerPosition;
648
649 return NO_ERROR;
650}
651
652status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
653{
Glenn Kastena0d68332012-01-27 16:47:15 -0800654 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655
656 uint32_t curPosition;
657 getPosition(&curPosition);
658 mNewPosition = curPosition + updatePeriod;
659 mUpdatePeriod = updatePeriod;
660
661 return NO_ERROR;
662}
663
Glenn Kastena5224f32012-01-04 12:41:44 -0800664status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665{
Glenn Kastena0d68332012-01-27 16:47:15 -0800666 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667
668 *updatePeriod = mUpdatePeriod;
669
670 return NO_ERROR;
671}
672
673status_t AudioTrack::setPosition(uint32_t position)
674{
John Grossman4ff14ba2012-02-08 16:37:41 -0800675 if (mIsTimed) return INVALID_OPERATION;
676
Eric Laurent1703cdf2011-03-07 14:52:59 -0800677 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800679 if (!stopped_l()) return INVALID_OPERATION;
680
681 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682
683 if (position > mCblk->user) return BAD_VALUE;
684
685 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700686 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700687
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688 return NO_ERROR;
689}
690
691status_t AudioTrack::getPosition(uint32_t *position)
692{
Glenn Kastena0d68332012-01-27 16:47:15 -0800693 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800694 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700695 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696
697 return NO_ERROR;
698}
699
700status_t AudioTrack::reload()
701{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800702 AutoMutex lock(mLock);
703
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800704 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700705
Eric Laurent1703cdf2011-03-07 14:52:59 -0800706 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707
Eric Laurentd1b449a2010-05-14 03:26:45 -0700708 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709
710 return NO_ERROR;
711}
712
Eric Laurentc2f1f072009-07-17 12:17:14 -0700713audio_io_handle_t AudioTrack::getOutput()
714{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800715 AutoMutex lock(mLock);
716 return getOutput_l();
717}
718
719// must be called with mLock held
720audio_io_handle_t AudioTrack::getOutput_l()
721{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800722 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800723 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700724}
725
Glenn Kastena5224f32012-01-04 12:41:44 -0800726int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700727{
728 return mSessionId;
729}
730
731status_t AudioTrack::attachAuxEffect(int effectId)
732{
Steve Block3856b092011-10-20 11:56:00 +0100733 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700734 status_t status = mAudioTrack->attachAuxEffect(effectId);
735 if (status == NO_ERROR) {
736 mAuxEffectId = effectId;
737 }
738 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700739}
740
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741// -------------------------------------------------------------------------
742
Eric Laurent1703cdf2011-03-07 14:52:59 -0800743// must be called with mLock held
744status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800745 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800746 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800747 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700748 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800749 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700750 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800751 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700752 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800753{
754 status_t status;
755 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
756 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700757 ALOGE("Could not get audioflinger");
758 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800759 }
760
Eric Laurentd1b449a2010-05-14 03:26:45 -0700761 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700762 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700763 return NO_INIT;
764 }
765
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700766 // Client decides whether the track is TIMED (see below), but can only express a preference
767 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700768 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700769 // either of these use cases:
770 // use case 1: shared buffer
771 (sharedBuffer != 0) ||
772 // use case 2: callback handler
773 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800774 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700775 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700776 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700777 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700778 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700779 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700780
Eric Laurentd1b449a2010-05-14 03:26:45 -0700781 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700782
Dima Zavinfce7a472011-04-19 22:30:36 -0700783 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700784
Eric Laurentd1b449a2010-05-14 03:26:45 -0700785 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700786 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700787 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700788 } else if (frameCount == 0) {
789 int afFrameCount;
790 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
791 return NO_INIT;
792 }
793 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700794 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700795
796 } else if (sharedBuffer != 0) {
797
798 // Ensure that buffer alignment matches channelCount
799 int channelCount = popcount(channelMask);
800 // 8-bit data in shared memory is not currently supported by AudioFlinger
801 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
802 if (channelCount > 1) {
803 // More than 2 channels does not require stronger alignment than stereo
804 alignment <<= 1;
805 }
806 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
807 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
808 sharedBuffer->pointer(), channelCount);
809 return BAD_VALUE;
810 }
811
812 // When initializing a shared buffer AudioTrack via constructors,
813 // there's no frameCount parameter.
814 // But when initializing a shared buffer AudioTrack via set(),
815 // there _is_ a frameCount parameter. We silently ignore it.
816 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
817
818 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
819
820 // FIXME move these calculations and associated checks to server
821 int afSampleRate;
822 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
823 return NO_INIT;
824 }
825 int afFrameCount;
826 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
827 return NO_INIT;
828 }
829
Eric Laurentd1b449a2010-05-14 03:26:45 -0700830 // Ensure that buffer depth covers at least audio hardware latency
831 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
832 if (minBufCount < 2) minBufCount = 2;
833
834 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800835 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
836 ", afLatency=%d",
837 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700838
839 if (frameCount == 0) {
840 frameCount = minFrameCount;
841 }
842 if (mNotificationFramesAct == 0) {
843 mNotificationFramesAct = frameCount/2;
844 }
845 // Make sure that application is notified with sufficient margin
846 // before underrun
847 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
848 mNotificationFramesAct = frameCount/2;
849 }
850 if (frameCount < minFrameCount) {
851 // not ALOGW because it happens all the time when playing key clicks over A2DP
852 ALOGV("Minimum buffer size corrected from %d to %d",
853 frameCount, minFrameCount);
854 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800855 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700856
Glenn Kastene0fa4672012-04-24 14:35:14 -0700857 } else {
858 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700859 }
860
Glenn Kastena075db42012-03-06 11:22:44 -0800861 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
862 if (mIsTimed) {
863 trackFlags |= IAudioFlinger::TRACK_TIMED;
864 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800865
866 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700867 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700868 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800869 if (mAudioTrackThread != 0) {
870 tid = mAudioTrackThread->getTid();
871 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700872 }
873
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800874 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
875 streamType,
876 sampleRate,
877 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700878 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800879 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800880 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800881 sharedBuffer,
882 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800883 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700884 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800885 &status);
886
887 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000888 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800889 return status;
890 }
891 sp<IMemory> cblk = track->getCblk();
892 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000893 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800894 return NO_INIT;
895 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800896 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800897 mCblkMemory = cblk;
898 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -0800899 // old has the previous value of mCblk->flags before the "or" operation
900 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
901 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
902 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700903 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800904 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700905 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700906 // once denied, do not request again if IAudioTrack is re-created
907 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
908 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800909 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910 if (sharedBuffer == 0) {
911 mNotificationFramesAct = mCblk->frameCount/2;
912 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800913 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800914 if (sharedBuffer == 0) {
915 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
916 } else {
917 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700918 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700919 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800920 }
921
Glenn Kasten83d86532012-01-17 14:39:34 -0800922 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800923 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700924 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800925 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
926 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700927 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700928 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -0700929 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700930 // If IAudioTrack is re-created, don't let the requested frameCount
931 // decrease. This can confuse clients that cache frameCount().
932 if (mCblk->frameCount > mFrameCount) {
933 mFrameCount = mCblk->frameCount;
934 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800935 return NO_ERROR;
936}
937
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
939{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800940 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800941 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700942 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943 audio_track_cblk_t* cblk = mCblk;
944 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700945 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946
947 audioBuffer->frameCount = 0;
948 audioBuffer->size = 0;
949
950 uint32_t framesAvail = cblk->framesAvailable();
951
Eric Laurent9b7d9502011-03-21 11:49:00 -0700952 cblk->lock.lock();
953 if (cblk->flags & CBLK_INVALID_MSK) {
954 goto create_new_track;
955 }
956 cblk->lock.unlock();
957
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800958 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800959 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800960 goto start_loop_here;
961 while (framesAvail == 0) {
962 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800963 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100964 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800965 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800966 return NO_MORE_BUFFERS;
967 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800968 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800969 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800971 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700972 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800973 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700974 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800976 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800977 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800978 return status_t(STOPPED);
979 }
980 cblk->lock.lock();
981 }
982
983 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700984 goto create_new_track;
985 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800986 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700987 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
989 // timing out when a loop has been set and we have already written upto loop end
990 // is a normal condition: no need to wake AudioFlinger up.
991 if (cblk->user < cblk->loopEnd) {
Glenn Kasten0c9d26d2012-05-31 14:35:01 -0700992 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p name=%#x"
993 "user=%08x, server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700994 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800996 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800997 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800998 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700999 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001000create_new_track:
1001 result = restoreTrack_l(cblk, false);
1002 }
1003 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001004 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001005 cblk->lock.unlock();
1006 return result;
1007 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001008 }
1009 cblk->waitTimeMs = 0;
1010 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001011
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001012 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001013 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001014 return TIMED_OUT;
1015 }
1016 }
1017 // read the server count again
1018 start_loop_here:
1019 framesAvail = cblk->framesAvailable_l();
1020 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001021 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001022 }
1023
1024 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001025
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026 if (framesReq > framesAvail) {
1027 framesReq = framesAvail;
1028 }
1029
1030 uint32_t u = cblk->user;
1031 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1032
Marco Nelissena1472d92012-03-30 14:36:54 -07001033 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001034 framesReq = bufferEnd - u;
1035 }
1036
Eric Laurentc2f1f072009-07-17 12:17:14 -07001037 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1038 audioBuffer->channelCount = mChannelCount;
1039 audioBuffer->frameCount = framesReq;
1040 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001041 if (audio_is_linear_pcm(mFormat)) {
1042 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001043 } else {
1044 audioBuffer->format = mFormat;
1045 }
1046 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001047 active = mActive;
1048 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1049}
1050
1051void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1052{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001053 AutoMutex lock(mLock);
1054 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001055 if (audioBuffer->frameCount > 0) {
1056 // restart track if it was disabled by audioflinger due to previous underrun
1057 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1058 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001059 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001060 mAudioTrack->start();
1061 }
1062 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001063}
1064
1065// -------------------------------------------------------------------------
1066
1067ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1068{
1069
1070 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001071 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001072
1073 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001074 // Sanity-check: user is most-likely passing an error code, and it would
1075 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001076 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001077 buffer, userSize, userSize);
1078 return BAD_VALUE;
1079 }
1080
Steve Block3856b092011-10-20 11:56:00 +01001081 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001082
Eric Laurentdf839842012-05-31 14:27:14 -07001083 if (userSize == 0) {
1084 return 0;
1085 }
1086
Eric Laurent1703cdf2011-03-07 14:52:59 -08001087 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1088 // while we are accessing the cblk
1089 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001090 sp<IAudioTrack> audioTrack = mAudioTrack;
1091 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001092 mLock.unlock();
1093
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001094 ssize_t written = 0;
1095 const int8_t *src = (const int8_t *)buffer;
1096 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001097 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001098
1099 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001100 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001101
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001102 status_t err = obtainBuffer(&audioBuffer, -1);
1103 if (err < 0) {
1104 // out of buffers, return #bytes written
1105 if (err == status_t(NO_MORE_BUFFERS))
1106 break;
1107 return ssize_t(err);
1108 }
1109
1110 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001111
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001112 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001113 // Divide capacity by 2 to take expansion into account
1114 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001115 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001116 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001117 toWrite = audioBuffer.size;
1118 memcpy(audioBuffer.i8, src, toWrite);
1119 src += toWrite;
1120 }
1121 userSize -= toWrite;
1122 written += toWrite;
1123
1124 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001125 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001126
1127 return written;
1128}
1129
1130// -------------------------------------------------------------------------
1131
John Grossman4ff14ba2012-02-08 16:37:41 -08001132TimedAudioTrack::TimedAudioTrack() {
1133 mIsTimed = true;
1134}
1135
1136status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1137{
1138 status_t result = UNKNOWN_ERROR;
1139
1140 // If the track is not invalid already, try to allocate a buffer. alloc
1141 // fails indicating that the server is dead, flag the track as invalid so
1142 // we can attempt to restore in in just a bit.
1143 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1144 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1145 if (result == DEAD_OBJECT) {
1146 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1147 }
1148 }
1149
1150 // If the track is invalid at this point, attempt to restore it. and try the
1151 // allocation one more time.
1152 if (mCblk->flags & CBLK_INVALID_MSK) {
1153 mCblk->lock.lock();
1154 result = restoreTrack_l(mCblk, false);
1155 mCblk->lock.unlock();
1156
1157 if (result == OK)
1158 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1159 }
1160
1161 return result;
1162}
1163
1164status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1165 int64_t pts)
1166{
Eric Laurentdf839842012-05-31 14:27:14 -07001167 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1168 {
1169 AutoMutex lock(mLock);
1170 // restart track if it was disabled by audioflinger due to previous underrun
1171 if (buffer->size() != 0 && status == NO_ERROR &&
1172 mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1173 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1174 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1175 mAudioTrack->start();
1176 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001177 }
Eric Laurentdf839842012-05-31 14:27:14 -07001178 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001179}
1180
1181status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1182 TargetTimeline target)
1183{
1184 return mAudioTrack->setMediaTimeTransform(xform, target);
1185}
1186
1187// -------------------------------------------------------------------------
1188
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001189bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1190{
1191 Buffer audioBuffer;
1192 uint32_t frames;
1193 size_t writtenSize;
1194
Eric Laurent1703cdf2011-03-07 14:52:59 -08001195 mLock.lock();
1196 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1197 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001198 sp<IAudioTrack> audioTrack = mAudioTrack;
1199 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001200 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001201 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001202 mLock.unlock();
1203
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001205 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001207 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001209 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001210 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001211 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001212 if (mSharedBuffer != 0) return false;
1213 }
1214 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001215
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001217 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001218 int loopCount = -1;
1219 mLoopCount--;
1220 if (mLoopCount >= 0) loopCount = mLoopCount;
1221
1222 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1223 }
1224
1225 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001226 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001227 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001229 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230 }
1231 }
1232
1233 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001234 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001235 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1237 mNewPosition += mUpdatePeriod;
1238 }
1239 }
1240
1241 // If Shared buffer is used, no data is requested from client.
1242 if (mSharedBuffer != 0) {
1243 frames = 0;
1244 } else {
1245 frames = mRemainingFrames;
1246 }
1247
Glenn Kasten99e53b82012-01-19 08:59:58 -08001248 // See description of waitCount parameter at declaration of obtainBuffer().
1249 // The logic below prevents us from being stuck below at obtainBuffer()
1250 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001251 int32_t waitCount = -1;
1252 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1253 waitCount = 1;
1254 }
1255
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256 do {
1257
1258 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001259
Eric Laurent2267ba12011-09-07 11:13:23 -07001260 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261 if (err < NO_ERROR) {
1262 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001263 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 -08001264 return false;
1265 }
1266 break;
1267 }
1268 if (err == status_t(STOPPED)) return false;
1269
1270 // Divide buffer size by 2 to take into account the expansion
1271 // due to 8 to 16 bit conversion: the callback must fill only half
1272 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001273 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 audioBuffer.size >>= 1;
1275 }
1276
1277 size_t reqSize = audioBuffer.size;
1278 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1279 writtenSize = audioBuffer.size;
1280
1281 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001282 if (ssize_t(writtenSize) <= 0) {
1283 // The callback is done filling buffers
1284 // Keep this thread going to handle timed events and
1285 // still try to get more data in intervals of WAIT_PERIOD_MS
1286 // but don't just loop and block the CPU, so wait
1287 usleep(WAIT_PERIOD_MS*1000);
1288 break;
1289 }
Eric Laurentdf839842012-05-31 14:27:14 -07001290
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291 if (writtenSize > reqSize) writtenSize = reqSize;
1292
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001293 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001294 // 8 to 16 bit conversion, note that source and destination are the same address
1295 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001296 writtenSize <<= 1;
1297 }
1298
1299 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001300 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001301 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001302 // 16 bit.
1303 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1304
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 frames -= audioBuffer.frameCount;
1306
1307 releaseBuffer(&audioBuffer);
1308 }
1309 while (frames);
1310
1311 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001312 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001313 } else {
1314 mRemainingFrames = frames;
1315 }
1316 return true;
1317}
1318
Eric Laurent1703cdf2011-03-07 14:52:59 -08001319// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1320// the IAudioTrack and IMemory in case they are recreated here.
1321// If the IAudioTrack is successfully restored, the cblk pointer is updated
1322status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1323{
1324 status_t result;
1325
Eric Laurent38ccae22011-03-28 18:37:07 -07001326 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001327 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001328 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001329
Eric Laurent1703cdf2011-03-07 14:52:59 -08001330 // signal old cblk condition so that other threads waiting for available buffers stop
1331 // waiting now
1332 cblk->cv.broadcast();
1333 cblk->lock.unlock();
1334
Eric Laurent9f6530f2011-08-30 10:18:54 -07001335 // refresh the audio configuration cache in this process to make sure we get new
1336 // output parameters in getOutput_l() and createTrack_l()
1337 AudioSystem::clearAudioConfigCache();
1338
Eric Laurent1703cdf2011-03-07 14:52:59 -08001339 // if the new IAudioTrack is created, createTrack_l() will modify the
1340 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1341 // It will also delete the strong references on previous IAudioTrack and IMemory
1342 result = createTrack_l(mStreamType,
1343 cblk->sampleRate,
1344 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001345 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001346 mFrameCount,
1347 mFlags,
1348 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001349 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001350
1351 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001352 uint32_t user = cblk->user;
1353 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001354 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001355 mCblk->user = user;
1356 mCblk->server = user;
1357 mCblk->userBase = user;
1358 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001359 // restore loop: this is not guaranteed to succeed if new frame count is not
1360 // compatible with loop length
1361 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001362 if (!fromStart) {
1363 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001364 // Make sure that a client relying on callback events indicating underrun or
1365 // the actual amount of audio frames played (e.g SoundPool) receives them.
1366 if (mSharedBuffer == 0) {
1367 uint32_t frames = 0;
1368 if (user > server) {
1369 frames = ((user - server) > mCblk->frameCount) ?
1370 mCblk->frameCount : (user - server);
1371 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1372 }
1373 // restart playback even if buffer is not completely filled.
1374 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1375 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1376 // the client
1377 mCblk->stepUser(frames);
1378 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001379 }
Eric Laurent29864602012-05-08 18:57:51 -07001380 if (mSharedBuffer != 0) {
1381 mCblk->stepUser(mCblk->frameCount);
1382 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001383 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001384 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001385 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001386 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001387 if (fromStart && result == NO_ERROR) {
1388 mNewPosition = mCblk->server + mUpdatePeriod;
1389 }
1390 }
1391 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001392 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001393 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001394 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001395 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001396 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001397 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001398 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001399 } else {
1400 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001401 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001402 mLock.unlock();
1403 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001404 if (result == NO_ERROR) {
1405 result = mRestoreStatus;
1406 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001407 cblk->lock.unlock();
1408 mLock.lock();
1409 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001410 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001411 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001412 cblk->lock.unlock();
1413 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001414 }
Steve Block3856b092011-10-20 11:56:00 +01001415 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001416 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001417
1418 if (result == NO_ERROR) {
1419 // from now on we switch to the newly created cblk
1420 cblk = mCblk;
1421 }
1422 cblk->lock.lock();
1423
Steve Block5ff1dd52012-01-05 23:22:43 +00001424 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001425
1426 return result;
1427}
1428
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1430{
1431
1432 const size_t SIZE = 256;
1433 char buffer[SIZE];
1434 String8 result;
1435
1436 result.append(" AudioTrack::dump\n");
1437 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1438 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001439 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 -08001440 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001441 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 -08001442 result.append(buffer);
1443 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1444 result.append(buffer);
1445 ::write(fd, result.string(), result.size());
1446 return NO_ERROR;
1447}
1448
1449// =========================================================================
1450
1451AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001452 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1453{
1454}
1455
1456AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001457{
1458}
1459
1460bool AudioTrack::AudioTrackThread::threadLoop()
1461{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001462 {
1463 AutoMutex _l(mMyLock);
1464 if (mPaused) {
1465 mMyCond.wait(mMyLock);
1466 // caller will check for exitPending()
1467 return true;
1468 }
1469 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001470 if (!mReceiver.processAudioBuffer(this)) {
1471 pause();
1472 }
1473 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001474}
1475
Glenn Kasten3acbd052012-02-28 10:39:56 -08001476void AudioTrack::AudioTrackThread::requestExit()
1477{
1478 // must be in this order to avoid a race condition
1479 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001480 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001481}
1482
1483void AudioTrack::AudioTrackThread::pause()
1484{
1485 AutoMutex _l(mMyLock);
1486 mPaused = true;
1487}
1488
1489void AudioTrack::AudioTrackThread::resume()
1490{
1491 AutoMutex _l(mMyLock);
1492 if (mPaused) {
1493 mPaused = false;
1494 mMyCond.signal();
1495 }
1496}
1497
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001498// =========================================================================
1499
Eric Laurent38ccae22011-03-28 18:37:07 -07001500
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001501audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001502 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001503 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001504 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001505 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001506{
1507}
1508
1509uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1510{
Marco Nelissena1472d92012-03-30 14:36:54 -07001511 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001512
Marco Nelissena1472d92012-03-30 14:36:54 -07001513 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001514 u += frameCount;
1515 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001516 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001517 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1518 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1519 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1520 }
Glenn Kasten90548972011-12-13 11:45:07 -08001521 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001522 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001523 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524 }
1525
Marco Nelissena1472d92012-03-30 14:36:54 -07001526 uint32_t fc = this->frameCount;
1527 if (u >= fc) {
1528 // common case, user didn't just wrap
1529 if (u - fc >= userBase ) {
1530 userBase += fc;
1531 }
1532 } else if (u >= userBase + fc) {
1533 // user just wrapped
1534 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001535 }
1536
Glenn Kasten90548972011-12-13 11:45:07 -08001537 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001538
1539 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001540 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001541 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001542 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001543
1544 return u;
1545}
1546
1547bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1548{
Marco Nelissena1472d92012-03-30 14:36:54 -07001549 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1550
Eric Laurent38ccae22011-03-28 18:37:07 -07001551 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001552 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001553 return false;
1554 }
1555
Glenn Kasten90548972011-12-13 11:45:07 -08001556 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001557 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001558
1559 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001560 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561 // Mark that we have read the first buffer so that next time stepUser() is called
1562 // we switch to normal obtainBuffer() timeout period
1563 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001564 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001565 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001566 // It is possible that we receive a flush()
1567 // while the mixer is processing a block: in this case,
1568 // stepServer() is called After the flush() has reset u & s and
1569 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001570 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001571 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001572 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001573 }
1574 }
1575
1576 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001577 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001578 s = loopStart;
1579 if (--loopCount == 0) {
1580 loopEnd = UINT_MAX;
1581 loopStart = UINT_MAX;
1582 }
1583 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001584
1585 uint32_t fc = this->frameCount;
1586 if (s >= fc) {
1587 // common case, server didn't just wrap
1588 if (s - fc >= serverBase ) {
1589 serverBase += fc;
1590 }
1591 } else if (s >= serverBase + fc) {
1592 // server just wrapped
1593 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594 }
1595
Glenn Kasten90548972011-12-13 11:45:07 -08001596 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001597
Eric Laurent1703cdf2011-03-07 14:52:59 -08001598 if (!(flags & CBLK_INVALID_MSK)) {
1599 cv.signal();
1600 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001601 lock.unlock();
1602 return true;
1603}
1604
1605void* audio_track_cblk_t::buffer(uint32_t offset) const
1606{
Glenn Kasten90548972011-12-13 11:45:07 -08001607 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608}
1609
1610uint32_t audio_track_cblk_t::framesAvailable()
1611{
1612 Mutex::Autolock _l(lock);
1613 return framesAvailable_l();
1614}
1615
1616uint32_t audio_track_cblk_t::framesAvailable_l()
1617{
Glenn Kasten90548972011-12-13 11:45:07 -08001618 uint32_t u = user;
1619 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001620
Eric Laurentd1b449a2010-05-14 03:26:45 -07001621 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001622 uint32_t limit = (s < loopStart) ? s : loopStart;
1623 return limit + frameCount - u;
1624 } else {
1625 return frameCount + u - s;
1626 }
1627}
1628
1629uint32_t audio_track_cblk_t::framesReady()
1630{
Glenn Kasten90548972011-12-13 11:45:07 -08001631 uint32_t u = user;
1632 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001633
Eric Laurentd1b449a2010-05-14 03:26:45 -07001634 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001635 if (u < loopEnd) {
1636 return u - s;
1637 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001638 // do not block on mutex shared with client on AudioFlinger side
1639 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001640 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001641 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001643 uint32_t frames = UINT_MAX;
1644 if (loopCount >= 0) {
1645 frames = (loopEnd - loopStart)*loopCount + u - s;
1646 }
1647 lock.unlock();
1648 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649 }
1650 } else {
1651 return s - u;
1652 }
1653}
1654
Eric Laurent38ccae22011-03-28 18:37:07 -07001655bool audio_track_cblk_t::tryLock()
1656{
1657 // the code below simulates lock-with-timeout
1658 // we MUST do this to protect the AudioFlinger server
1659 // as this lock is shared with the client.
1660 status_t err;
1661
1662 err = lock.tryLock();
1663 if (err == -EBUSY) { // just wait a bit
1664 usleep(1000);
1665 err = lock.tryLock();
1666 }
1667 if (err != NO_ERROR) {
1668 // probably, the client just died.
1669 return false;
1670 }
1671 return true;
1672}
1673
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001674// -------------------------------------------------------------------------
1675
1676}; // namespace android