blob: 468bd295b1a7f58f3b16fc2ed2233d08c1e83241 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
26#include <sched.h>
27#include <sys/resource.h>
28
29#include <private/media/AudioTrackShared.h>
30
31#include <media/AudioSystem.h>
32#include <media/AudioTrack.h>
33
34#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Glenn Kasten511754b2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
53 int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 uint32_t sampleRate)
56{
Glenn Kasten04cd0182012-06-25 11:49:27 -070057 if (frameCount == NULL) return BAD_VALUE;
58
59 // default to 0 in case of error
60 *frameCount = 0;
61
Glenn Kastene0fa4672012-04-24 14:35:14 -070062 // FIXME merge with similar code in createTrack_l(), except we're missing
63 // some information here that is available in createTrack_l():
64 // audio_io_handle_t output
65 // audio_format_t format
66 // audio_channel_mask_t channelMask
67 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 int afSampleRate;
69 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
70 return NO_INIT;
71 }
72 int afFrameCount;
73 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 uint32_t afLatency;
77 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80
81 // Ensure that buffer depth covers at least audio hardware latency
82 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
83 if (minBufCount < 2) minBufCount = 2;
84
85 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070086 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080087 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
88 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080089 return NO_ERROR;
90}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92// ---------------------------------------------------------------------------
93
94AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070095 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080096 mIsTimed(false),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070098 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099{
100}
101
102AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800103 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800105 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700106 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700108 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 callback_t cbf,
110 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700111 int notificationFrames,
112 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700113 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800114 mIsTimed(false),
115 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700116 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700118 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700119 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800120 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121}
122
Andreas Huberc8139852012-01-18 10:51:55 -0800123AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800124 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800126 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700127 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800128 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700129 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 callback_t cbf,
131 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700132 int notificationFrames,
133 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700134 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800135 mIsTimed(false),
136 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700137 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700139 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800140 0 /*frameCount*/, flags, cbf, user, notificationFrames,
141 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142}
143
144AudioTrack::~AudioTrack()
145{
Steve Block3856b092011-10-20 11:56:00 +0100146 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147
148 if (mStatus == NO_ERROR) {
149 // Make sure that callback function exits in the case where
150 // it is looping on buffer full condition in obtainBuffer().
151 // Otherwise the callback thread will never exit.
152 stop();
153 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800154 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 mAudioTrackThread->requestExitAndWait();
156 mAudioTrackThread.clear();
157 }
158 mAudioTrack.clear();
159 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700160 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162}
163
164status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800165 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800167 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700168 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700170 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 callback_t cbf,
172 void* user,
173 int notificationFrames,
174 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700175 bool threadCanCallJava,
176 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
178
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700179 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
180 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181
Eric Laurent1a9ed112012-03-20 18:36:01 -0700182 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
183
Eric Laurent1703cdf2011-03-07 14:52:59 -0800184 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700185 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000186 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 return INVALID_OPERATION;
188 }
189
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700191 if (streamType == AUDIO_STREAM_DEFAULT) {
192 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700194
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700196 int afSampleRate;
197 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
198 return NO_INIT;
199 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 sampleRate = afSampleRate;
201 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800204 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700205 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700207 if (channelMask == 0) {
208 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 }
210
211 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000213 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214 return BAD_VALUE;
215 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700216
Glenn Kastene0fa4672012-04-24 14:35:14 -0700217 // AudioFlinger does not currently support 8-bit data in shared memory
218 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
219 ALOGE("8-bit data in shared memory is not supported");
220 return BAD_VALUE;
221 }
222
Eric Laurentc2f1f072009-07-17 12:17:14 -0700223 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700225 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800226 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700227 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700228 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700229 // only allow deep buffering for music stream type
230 if (streamType != AUDIO_STREAM_MUSIC) {
231 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
232 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700233
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700234 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700235 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 return BAD_VALUE;
237 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700238 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700239
Dima Zavinfce7a472011-04-19 22:30:36 -0700240 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800241 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800242 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800243 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700244
245 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000246 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 return BAD_VALUE;
248 }
249
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800250 mVolume[LEFT] = 1.0f;
251 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800252 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700253 mFrameCount = frameCount;
254 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700255 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700256 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700257 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700258 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700259
Glenn Kastena997e7a2012-08-07 09:44:19 -0700260 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700261 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700262 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
263 }
264
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800265 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800266 status_t status = createTrack_l(streamType,
267 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800268 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700269 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800270 frameCount,
271 flags,
272 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700273 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800274
Glenn Kastena997e7a2012-08-07 09:44:19 -0700275 if (status != NO_ERROR) {
276 if (mAudioTrackThread != 0) {
277 mAudioTrackThread->requestExit();
278 mAudioTrackThread.clear();
279 }
280 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700281 }
282
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283 mStatus = NO_ERROR;
284
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800286 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700287 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 mChannelCount = channelCount;
289 mSharedBuffer = sharedBuffer;
290 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800291 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800293 mLoopCount = 0;
294 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700295 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 mNewPosition = 0;
297 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700298 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700299 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300 return NO_ERROR;
301}
302
303status_t AudioTrack::initCheck() const
304{
305 return mStatus;
306}
307
308// -------------------------------------------------------------------------
309
310uint32_t AudioTrack::latency() const
311{
312 return mLatency;
313}
314
Glenn Kastenfff6d712012-01-12 16:38:12 -0800315audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316{
317 return mStreamType;
318}
319
Glenn Kastene1c39622012-01-04 09:36:37 -0800320audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321{
322 return mFormat;
323}
324
325int AudioTrack::channelCount() const
326{
327 return mChannelCount;
328}
329
330uint32_t AudioTrack::frameCount() const
331{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700332 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333}
334
Glenn Kastenb9980652012-01-11 09:48:27 -0800335size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336{
Dima Zavinfce7a472011-04-19 22:30:36 -0700337 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700338 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700339 } else {
340 return sizeof(uint8_t);
341 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342}
343
344sp<IMemory>& AudioTrack::sharedBuffer()
345{
346 return mSharedBuffer;
347}
348
349// -------------------------------------------------------------------------
350
351void AudioTrack::start()
352{
353 sp<AudioTrackThread> t = mAudioTrackThread;
354
Steve Block3856b092011-10-20 11:56:00 +0100355 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356
Eric Laurentf5aafb22010-11-18 08:40:16 -0800357 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800358 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
359 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700360 sp<IAudioTrack> audioTrack = mAudioTrack;
361 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800362 audio_track_cblk_t* cblk = mCblk;
363
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800364 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700365 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800366 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800367 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700368 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800369 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
370 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800371 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800372 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800373 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800374 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700375 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700376 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700377 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800378 }
379
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700380 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700381 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800382 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800383 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800384 ALOGV("mAudioTrack->start()");
385 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800386 cblk->lock.lock();
387 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800388 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800389 }
Eric Laurent2b584242009-11-09 23:32:22 -0800390 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800391 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700392 audio_track_cblk_t* temp = cblk;
393 status = restoreTrack_l(temp, true);
394 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800395 }
396 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800397 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100398 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800399 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800400 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800401 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800402 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700403 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700404 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800405 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800406 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 }
408
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409}
410
411void AudioTrack::stop()
412{
413 sp<AudioTrackThread> t = mAudioTrackThread;
414
Steve Block3856b092011-10-20 11:56:00 +0100415 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416
Eric Laurentf5aafb22010-11-18 08:40:16 -0800417 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800418 if (mActive) {
419 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700420 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 mAudioTrack->stop();
422 // Cancel loops (If we are in the middle of a loop, playback
423 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800424 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700425 // the playback head position will reset to 0, so if a marker is set, we need
426 // to activate it again
427 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 // Force flush if a shared buffer is used otherwise audioflinger
429 // will not stop before end of buffer is reached.
430 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800431 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 }
433 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800434 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700436 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700437 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 }
439 }
440
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441}
442
443bool AudioTrack::stopped() const
444{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800445 AutoMutex lock(mLock);
446 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447}
448
449void AudioTrack::flush()
450{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800451 AutoMutex lock(mLock);
452 flush_l();
453}
454
455// must be called with mLock held
456void AudioTrack::flush_l()
457{
Steve Block3856b092011-10-20 11:56:00 +0100458 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700459
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700460 // clear playback marker and periodic update counter
461 mMarkerPosition = 0;
462 mMarkerReached = false;
463 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700464
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800465 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700466 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 mAudioTrack->flush();
468 // Release AudioTrack callback thread in case it was waiting for new buffers
469 // in AudioTrack::obtainBuffer()
470 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 }
472}
473
474void AudioTrack::pause()
475{
Steve Block3856b092011-10-20 11:56:00 +0100476 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800477 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800478 if (mActive) {
479 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700480 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800481 mAudioTrack->pause();
482 }
483}
484
485void AudioTrack::mute(bool e)
486{
487 mAudioTrack->mute(e);
488 mMuted = e;
489}
490
491bool AudioTrack::muted() const
492{
493 return mMuted;
494}
495
Eric Laurentbe916aa2010-06-01 23:49:17 -0700496status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800498 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700499 return BAD_VALUE;
500 }
501
Eric Laurent1703cdf2011-03-07 14:52:59 -0800502 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503 mVolume[LEFT] = left;
504 mVolume[RIGHT] = right;
505
Glenn Kasten83d86532012-01-17 14:39:34 -0800506 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700507
508 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509}
510
Glenn Kastenb1c09932012-02-27 16:21:04 -0800511status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800513 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700514}
515
Eric Laurent2beeb502010-07-16 07:43:46 -0700516status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700517{
Steve Block3856b092011-10-20 11:56:00 +0100518 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800519 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700520 return BAD_VALUE;
521 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800522 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700523
524 mSendLevel = level;
525
Glenn Kasten05632a52012-01-03 14:22:33 -0800526 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700527
528 return NO_ERROR;
529}
530
Glenn Kastena5224f32012-01-04 12:41:44 -0800531void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700532{
533 if (level != NULL) {
534 *level = mSendLevel;
535 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536}
537
Eric Laurent57326622009-07-07 07:10:45 -0700538status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539{
540 int afSamplingRate;
541
John Grossman4ff14ba2012-02-08 16:37:41 -0800542 if (mIsTimed) {
543 return INVALID_OPERATION;
544 }
545
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700547 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 }
549 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700550 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551
Eric Laurent1703cdf2011-03-07 14:52:59 -0800552 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700553 mCblk->sampleRate = rate;
554 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555}
556
Glenn Kastena5224f32012-01-04 12:41:44 -0800557uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558{
John Grossman4ff14ba2012-02-08 16:37:41 -0800559 if (mIsTimed) {
560 return INVALID_OPERATION;
561 }
562
Eric Laurent1703cdf2011-03-07 14:52:59 -0800563 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700564 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565}
566
567status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
568{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800569 AutoMutex lock(mLock);
570 return setLoop_l(loopStart, loopEnd, loopCount);
571}
572
573// must be called with mLock held
574status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
575{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 audio_track_cblk_t* cblk = mCblk;
577
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578 Mutex::Autolock _l(cblk->lock);
579
580 if (loopCount == 0) {
581 cblk->loopStart = UINT_MAX;
582 cblk->loopEnd = UINT_MAX;
583 cblk->loopCount = 0;
584 mLoopCount = 0;
585 return NO_ERROR;
586 }
587
John Grossman4ff14ba2012-02-08 16:37:41 -0800588 if (mIsTimed) {
589 return INVALID_OPERATION;
590 }
591
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700593 loopEnd - loopStart > cblk->frameCount ||
594 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700595 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
596 "user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 return BAD_VALUE;
598 }
599
Eric Laurent9b7d9502011-03-21 11:49:00 -0700600 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700601 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
602 "framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700603 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700605 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606
607 cblk->loopStart = loopStart;
608 cblk->loopEnd = loopEnd;
609 cblk->loopCount = loopCount;
610 mLoopCount = loopCount;
611
612 return NO_ERROR;
613}
614
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615status_t AudioTrack::setMarkerPosition(uint32_t marker)
616{
Glenn Kastena0d68332012-01-27 16:47:15 -0800617 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618
619 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700620 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621
622 return NO_ERROR;
623}
624
Glenn Kastena5224f32012-01-04 12:41:44 -0800625status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626{
Glenn Kastena0d68332012-01-27 16:47:15 -0800627 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628
629 *marker = mMarkerPosition;
630
631 return NO_ERROR;
632}
633
634status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
635{
Glenn Kastena0d68332012-01-27 16:47:15 -0800636 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637
638 uint32_t curPosition;
639 getPosition(&curPosition);
640 mNewPosition = curPosition + updatePeriod;
641 mUpdatePeriod = updatePeriod;
642
643 return NO_ERROR;
644}
645
Glenn Kastena5224f32012-01-04 12:41:44 -0800646status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647{
Glenn Kastena0d68332012-01-27 16:47:15 -0800648 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649
650 *updatePeriod = mUpdatePeriod;
651
652 return NO_ERROR;
653}
654
655status_t AudioTrack::setPosition(uint32_t position)
656{
John Grossman4ff14ba2012-02-08 16:37:41 -0800657 if (mIsTimed) return INVALID_OPERATION;
658
Eric Laurent1703cdf2011-03-07 14:52:59 -0800659 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800661 if (!stopped_l()) return INVALID_OPERATION;
662
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700663 audio_track_cblk_t* cblk = mCblk;
664 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700666 if (position > cblk->user) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700668 cblk->server = position;
669 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700670
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671 return NO_ERROR;
672}
673
674status_t AudioTrack::getPosition(uint32_t *position)
675{
Glenn Kastena0d68332012-01-27 16:47:15 -0800676 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800677 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700678 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679
680 return NO_ERROR;
681}
682
683status_t AudioTrack::reload()
684{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800685 AutoMutex lock(mLock);
686
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800687 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700688
Eric Laurent1703cdf2011-03-07 14:52:59 -0800689 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700691 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten864585d2012-11-06 16:15:41 -0800692 cblk->stepUserOut(cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693
694 return NO_ERROR;
695}
696
Eric Laurentc2f1f072009-07-17 12:17:14 -0700697audio_io_handle_t AudioTrack::getOutput()
698{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800699 AutoMutex lock(mLock);
700 return getOutput_l();
701}
702
703// must be called with mLock held
704audio_io_handle_t AudioTrack::getOutput_l()
705{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800706 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800707 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700708}
709
Glenn Kastena5224f32012-01-04 12:41:44 -0800710int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700711{
712 return mSessionId;
713}
714
715status_t AudioTrack::attachAuxEffect(int effectId)
716{
Steve Block3856b092011-10-20 11:56:00 +0100717 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700718 status_t status = mAudioTrack->attachAuxEffect(effectId);
719 if (status == NO_ERROR) {
720 mAuxEffectId = effectId;
721 }
722 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700723}
724
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800725// -------------------------------------------------------------------------
726
Eric Laurent1703cdf2011-03-07 14:52:59 -0800727// must be called with mLock held
728status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800729 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800730 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800731 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700732 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800733 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700734 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800735 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700736 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800737{
738 status_t status;
739 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
740 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700741 ALOGE("Could not get audioflinger");
742 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800743 }
744
Eric Laurentd1b449a2010-05-14 03:26:45 -0700745 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700746 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700747 return NO_INIT;
748 }
749
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700750 // Client decides whether the track is TIMED (see below), but can only express a preference
751 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700752 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700753 // either of these use cases:
754 // use case 1: shared buffer
755 (sharedBuffer != 0) ||
756 // use case 2: callback handler
757 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800758 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700759 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700760 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700761 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700762 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700763 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700764
Eric Laurentd1b449a2010-05-14 03:26:45 -0700765 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700766
Dima Zavinfce7a472011-04-19 22:30:36 -0700767 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700768
Eric Laurentd1b449a2010-05-14 03:26:45 -0700769 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700770 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700771 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700772 } else if (frameCount == 0) {
773 int afFrameCount;
774 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
775 return NO_INIT;
776 }
777 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700778 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700779
780 } else if (sharedBuffer != 0) {
781
782 // Ensure that buffer alignment matches channelCount
783 int channelCount = popcount(channelMask);
784 // 8-bit data in shared memory is not currently supported by AudioFlinger
785 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
786 if (channelCount > 1) {
787 // More than 2 channels does not require stronger alignment than stereo
788 alignment <<= 1;
789 }
790 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
791 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
792 sharedBuffer->pointer(), channelCount);
793 return BAD_VALUE;
794 }
795
796 // When initializing a shared buffer AudioTrack via constructors,
797 // there's no frameCount parameter.
798 // But when initializing a shared buffer AudioTrack via set(),
799 // there _is_ a frameCount parameter. We silently ignore it.
800 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
801
802 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
803
804 // FIXME move these calculations and associated checks to server
805 int afSampleRate;
806 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
807 return NO_INIT;
808 }
809 int afFrameCount;
810 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
811 return NO_INIT;
812 }
813
Eric Laurentd1b449a2010-05-14 03:26:45 -0700814 // Ensure that buffer depth covers at least audio hardware latency
815 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
816 if (minBufCount < 2) minBufCount = 2;
817
818 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800819 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
820 ", afLatency=%d",
821 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700822
823 if (frameCount == 0) {
824 frameCount = minFrameCount;
825 }
826 if (mNotificationFramesAct == 0) {
827 mNotificationFramesAct = frameCount/2;
828 }
829 // Make sure that application is notified with sufficient margin
830 // before underrun
831 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
832 mNotificationFramesAct = frameCount/2;
833 }
834 if (frameCount < minFrameCount) {
835 // not ALOGW because it happens all the time when playing key clicks over A2DP
836 ALOGV("Minimum buffer size corrected from %d to %d",
837 frameCount, minFrameCount);
838 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800839 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700840
Glenn Kastene0fa4672012-04-24 14:35:14 -0700841 } else {
842 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700843 }
844
Glenn Kastena075db42012-03-06 11:22:44 -0800845 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
846 if (mIsTimed) {
847 trackFlags |= IAudioFlinger::TRACK_TIMED;
848 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800849
850 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700851 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700852 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800853 if (mAudioTrackThread != 0) {
854 tid = mAudioTrackThread->getTid();
855 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700856 }
857
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800858 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
859 streamType,
860 sampleRate,
861 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700862 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800863 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800864 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800865 sharedBuffer,
866 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800867 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700868 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800869 &status);
870
871 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000872 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800873 return status;
874 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700875 sp<IMemory> iMem = track->getCblk();
876 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000877 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800878 return NO_INIT;
879 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800880 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700881 mCblkMemory = iMem;
882 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
883 mCblk = cblk;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800884 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800885 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700886 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", cblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800887 } else {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700888 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", cblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700889 // once denied, do not request again if IAudioTrack is re-created
890 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
891 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800892 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700893 if (sharedBuffer == 0) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700894 mNotificationFramesAct = cblk->frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700895 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800896 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800897 if (sharedBuffer == 0) {
Glenn Kastenb929e412012-11-08 12:13:58 -0800898 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800899 } else {
Glenn Kastenb929e412012-11-08 12:13:58 -0800900 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700901 // Force buffer full condition as data is already present in shared memory
Glenn Kasten864585d2012-11-06 16:15:41 -0800902 cblk->stepUserOut(cblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800903 }
904
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700905 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700906 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700907 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700908 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700909 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
910 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700911 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912 // FIXME don't believe this lie
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700913 mLatency = afLatency + (1000*cblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700914 // If IAudioTrack is re-created, don't let the requested frameCount
915 // decrease. This can confuse clients that cache frameCount().
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700916 if (cblk->frameCount > mFrameCount) {
917 mFrameCount = cblk->frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700918 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800919 return NO_ERROR;
920}
921
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800922status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
923{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800924 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800925 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700926 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800927 audio_track_cblk_t* cblk = mCblk;
928 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700929 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800930
931 audioBuffer->frameCount = 0;
932 audioBuffer->size = 0;
933
Glenn Kasten864585d2012-11-06 16:15:41 -0800934 uint32_t framesAvail = cblk->framesAvailableOut();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935
Eric Laurent9b7d9502011-03-21 11:49:00 -0700936 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800937 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700938 goto create_new_track;
939 }
940 cblk->lock.unlock();
941
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800943 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 goto start_loop_here;
945 while (framesAvail == 0) {
946 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800947 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100948 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800949 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950 return NO_MORE_BUFFERS;
951 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800952 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800953 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800954 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800955 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800956 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800957 mLock.unlock();
Glenn Kastena47f3162012-11-07 10:13:08 -0800958 // this condition is in shared memory, so if IAudioTrack and control block
959 // are replaced due to mediaserver death or IAudioTrack invalidation then
960 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700961 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700962 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800963 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800964 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800965 return status_t(STOPPED);
966 }
Glenn Kastena47f3162012-11-07 10:13:08 -0800967 // IAudioTrack may have been re-created while mLock was unlocked
968 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800969 cblk->lock.lock();
970 }
971
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800972 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700973 goto create_new_track;
974 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800975 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700976 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
978 // timing out when a loop has been set and we have already written upto loop end
979 // is a normal condition: no need to wake AudioFlinger up.
980 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700981 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
982 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700983 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800984 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800985 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800987 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800988 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800989create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700990 audio_track_cblk_t* temp = cblk;
991 result = restoreTrack_l(temp, false);
992 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800993 }
994 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000995 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800996 cblk->lock.unlock();
997 return result;
998 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800999 }
1000 cblk->waitTimeMs = 0;
1001 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001002
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001005 return TIMED_OUT;
1006 }
1007 }
1008 // read the server count again
1009 start_loop_here:
Glenn Kasten864585d2012-11-06 16:15:41 -08001010 framesAvail = cblk->framesAvailableOut_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001011 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001012 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 }
1014
1015 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001016
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001017 if (framesReq > framesAvail) {
1018 framesReq = framesAvail;
1019 }
1020
1021 uint32_t u = cblk->user;
1022 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1023
Marco Nelissena1472d92012-03-30 14:36:54 -07001024 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001025 framesReq = bufferEnd - u;
1026 }
1027
Eric Laurentc2f1f072009-07-17 12:17:14 -07001028 audioBuffer->frameCount = framesReq;
1029 audioBuffer->size = framesReq * cblk->frameSize;
Glenn Kastenb929e412012-11-08 12:13:58 -08001030 audioBuffer->raw = cblk->buffer(mBuffers, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001031 active = mActive;
1032 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1033}
1034
1035void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1036{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001037 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001038 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten864585d2012-11-06 16:15:41 -08001039 cblk->stepUserOut(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001040 if (audioBuffer->frameCount > 0) {
1041 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001042 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1043 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1044 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001045 mAudioTrack->start();
1046 }
1047 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001048}
1049
1050// -------------------------------------------------------------------------
1051
1052ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1053{
1054
1055 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001056 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001057
1058 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001059 // Sanity-check: user is most-likely passing an error code, and it would
1060 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001061 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001062 buffer, userSize, userSize);
1063 return BAD_VALUE;
1064 }
1065
Steve Block3856b092011-10-20 11:56:00 +01001066 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001067
Eric Laurentdf839842012-05-31 14:27:14 -07001068 if (userSize == 0) {
1069 return 0;
1070 }
1071
Eric Laurent1703cdf2011-03-07 14:52:59 -08001072 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1073 // while we are accessing the cblk
1074 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001075 sp<IAudioTrack> audioTrack = mAudioTrack;
1076 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001077 mLock.unlock();
1078
Glenn Kastena47f3162012-11-07 10:13:08 -08001079 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1080 // so all cblk references might still refer to old shared memory, but that should be benign
1081
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001082 ssize_t written = 0;
1083 const int8_t *src = (const int8_t *)buffer;
1084 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001085 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001086
1087 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001088 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001089
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001090 status_t err = obtainBuffer(&audioBuffer, -1);
1091 if (err < 0) {
1092 // out of buffers, return #bytes written
1093 if (err == status_t(NO_MORE_BUFFERS))
1094 break;
1095 return ssize_t(err);
1096 }
1097
1098 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001099
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001100 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001101 // Divide capacity by 2 to take expansion into account
1102 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001103 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001104 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001105 toWrite = audioBuffer.size;
1106 memcpy(audioBuffer.i8, src, toWrite);
1107 src += toWrite;
1108 }
1109 userSize -= toWrite;
1110 written += toWrite;
1111
1112 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001113 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001114
1115 return written;
1116}
1117
1118// -------------------------------------------------------------------------
1119
John Grossman4ff14ba2012-02-08 16:37:41 -08001120TimedAudioTrack::TimedAudioTrack() {
1121 mIsTimed = true;
1122}
1123
1124status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1125{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001126 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001127 status_t result = UNKNOWN_ERROR;
1128
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001129 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1130 // while we are accessing the cblk
1131 sp<IAudioTrack> audioTrack = mAudioTrack;
1132 sp<IMemory> iMem = mCblkMemory;
1133
John Grossman4ff14ba2012-02-08 16:37:41 -08001134 // If the track is not invalid already, try to allocate a buffer. alloc
1135 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001136 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001137 audio_track_cblk_t* cblk = mCblk;
1138 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001139 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1140 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001141 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001142 }
1143 }
1144
1145 // If the track is invalid at this point, attempt to restore it. and try the
1146 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001147 if (cblk->flags & CBLK_INVALID) {
1148 cblk->lock.lock();
1149 audio_track_cblk_t* temp = cblk;
1150 result = restoreTrack_l(temp, false);
1151 cblk = temp;
1152 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001153
1154 if (result == OK)
1155 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1156 }
1157
1158 return result;
1159}
1160
1161status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1162 int64_t pts)
1163{
Eric Laurentdf839842012-05-31 14:27:14 -07001164 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1165 {
1166 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001167 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001168 // restart track if it was disabled by audioflinger due to previous underrun
1169 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001170 mActive && (cblk->flags & CBLK_DISABLED)) {
1171 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001172 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1173 mAudioTrack->start();
1174 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001175 }
Eric Laurentdf839842012-05-31 14:27:14 -07001176 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001177}
1178
1179status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1180 TargetTimeline target)
1181{
1182 return mAudioTrack->setMediaTimeTransform(xform, target);
1183}
1184
1185// -------------------------------------------------------------------------
1186
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001187bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1188{
1189 Buffer audioBuffer;
1190 uint32_t frames;
1191 size_t writtenSize;
1192
Eric Laurent1703cdf2011-03-07 14:52:59 -08001193 mLock.lock();
1194 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1195 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001196 sp<IAudioTrack> audioTrack = mAudioTrack;
1197 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001198 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001199 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001200 mLock.unlock();
1201
Glenn Kastena47f3162012-11-07 10:13:08 -08001202 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1203 // so all cblk references might still refer to old shared memory, but that should be benign
1204
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 // Manage underrun callback
Glenn Kasten864585d2012-11-06 16:15:41 -08001206 if (active && (cblk->framesAvailableOut() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001207 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001208 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001210 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001211 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001212 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 if (mSharedBuffer != 0) return false;
1214 }
1215 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001216
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001218 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219 int loopCount = -1;
1220 mLoopCount--;
1221 if (mLoopCount >= 0) loopCount = mLoopCount;
1222
1223 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1224 }
1225
1226 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001227 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001228 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001230 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001231 }
1232 }
1233
1234 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001235 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001236 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001237 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1238 mNewPosition += mUpdatePeriod;
1239 }
1240 }
1241
1242 // If Shared buffer is used, no data is requested from client.
1243 if (mSharedBuffer != 0) {
1244 frames = 0;
1245 } else {
1246 frames = mRemainingFrames;
1247 }
1248
Glenn Kasten99e53b82012-01-19 08:59:58 -08001249 // See description of waitCount parameter at declaration of obtainBuffer().
1250 // The logic below prevents us from being stuck below at obtainBuffer()
1251 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001252 int32_t waitCount = -1;
1253 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1254 waitCount = 1;
1255 }
1256
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001257 do {
1258
1259 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001260
Eric Laurent2267ba12011-09-07 11:13:23 -07001261 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 if (err < NO_ERROR) {
1263 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001264 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1265 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 return false;
1267 }
1268 break;
1269 }
1270 if (err == status_t(STOPPED)) return false;
1271
1272 // Divide buffer size by 2 to take into account the expansion
1273 // due to 8 to 16 bit conversion: the callback must fill only half
1274 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001275 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276 audioBuffer.size >>= 1;
1277 }
1278
1279 size_t reqSize = audioBuffer.size;
1280 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1281 writtenSize = audioBuffer.size;
1282
1283 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001284 if (ssize_t(writtenSize) <= 0) {
1285 // The callback is done filling buffers
1286 // Keep this thread going to handle timed events and
1287 // still try to get more data in intervals of WAIT_PERIOD_MS
1288 // but don't just loop and block the CPU, so wait
1289 usleep(WAIT_PERIOD_MS*1000);
1290 break;
1291 }
Eric Laurentdf839842012-05-31 14:27:14 -07001292
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 if (writtenSize > reqSize) writtenSize = reqSize;
1294
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001295 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001296 // 8 to 16 bit conversion, note that source and destination are the same address
1297 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298 writtenSize <<= 1;
1299 }
1300
1301 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001302 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1303 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001304 // 16 bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001305 audioBuffer.frameCount = writtenSize/cblk->frameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001306
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307 frames -= audioBuffer.frameCount;
1308
1309 releaseBuffer(&audioBuffer);
1310 }
1311 while (frames);
1312
1313 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001314 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 } else {
1316 mRemainingFrames = frames;
1317 }
1318 return true;
1319}
1320
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001321// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001322// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001323// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1324// FIXME Don't depend on caller to hold strong references.
1325status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001326{
1327 status_t result;
1328
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001329 audio_track_cblk_t* cblk = refCblk;
1330 audio_track_cblk_t* newCblk = cblk;
Glenn Kastena47f3162012-11-07 10:13:08 -08001331 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
1332 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001333
Glenn Kastena47f3162012-11-07 10:13:08 -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();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001338
Glenn Kastena47f3162012-11-07 10:13:08 -08001339 // 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();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001342
Glenn Kastena47f3162012-11-07 10:13:08 -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,
1349 mChannelMask,
1350 mFrameCount,
1351 mFlags,
1352 mSharedBuffer,
1353 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001354
Glenn Kastena47f3162012-11-07 10:13:08 -08001355 if (result == NO_ERROR) {
1356 uint32_t user = cblk->user;
1357 uint32_t server = cblk->server;
1358 // restore write index and set other indexes to reflect empty buffer status
1359 newCblk = mCblk;
1360 newCblk->user = user;
1361 newCblk->server = user;
1362 newCblk->userBase = user;
1363 newCblk->serverBase = user;
1364 // restore loop: this is not guaranteed to succeed if new frame count is not
1365 // compatible with loop length
1366 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1367 if (!fromStart) {
1368 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1369 // Make sure that a client relying on callback events indicating underrun or
1370 // the actual amount of audio frames played (e.g SoundPool) receives them.
1371 if (mSharedBuffer == 0) {
1372 uint32_t frames = 0;
1373 if (user > server) {
1374 frames = ((user - server) > newCblk->frameCount) ?
1375 newCblk->frameCount : (user - server);
Glenn Kastenb929e412012-11-08 12:13:58 -08001376 memset(mBuffers, 0, frames * newCblk->frameSize);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001377 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001378 // restart playback even if buffer is not completely filled.
1379 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1380 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1381 // the client
1382 newCblk->stepUserOut(frames);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001383 }
1384 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001385 if (mSharedBuffer != 0) {
1386 newCblk->stepUserOut(newCblk->frameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001387 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001388 if (mActive) {
1389 result = mAudioTrack->start();
1390 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1391 }
1392 if (fromStart && result == NO_ERROR) {
1393 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001394 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001395 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001396 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001397 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001398 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001399
1400 if (result == NO_ERROR) {
1401 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001402 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001403 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001404 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001405
Steve Block5ff1dd52012-01-05 23:22:43 +00001406 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001407
1408 return result;
1409}
1410
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001411status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1412{
1413
1414 const size_t SIZE = 256;
1415 char buffer[SIZE];
1416 String8 result;
1417
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001418 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001420 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1421 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001422 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001423 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001424 mChannelCount, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001426 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001427 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001428 result.append(buffer);
1429 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1430 result.append(buffer);
1431 ::write(fd, result.string(), result.size());
1432 return NO_ERROR;
1433}
1434
1435// =========================================================================
1436
1437AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001438 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1439{
1440}
1441
1442AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443{
1444}
1445
1446bool AudioTrack::AudioTrackThread::threadLoop()
1447{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001448 {
1449 AutoMutex _l(mMyLock);
1450 if (mPaused) {
1451 mMyCond.wait(mMyLock);
1452 // caller will check for exitPending()
1453 return true;
1454 }
1455 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001456 if (!mReceiver.processAudioBuffer(this)) {
1457 pause();
1458 }
1459 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460}
1461
Glenn Kasten3acbd052012-02-28 10:39:56 -08001462void AudioTrack::AudioTrackThread::requestExit()
1463{
1464 // must be in this order to avoid a race condition
1465 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001466 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001467}
1468
1469void AudioTrack::AudioTrackThread::pause()
1470{
1471 AutoMutex _l(mMyLock);
1472 mPaused = true;
1473}
1474
1475void AudioTrack::AudioTrackThread::resume()
1476{
1477 AutoMutex _l(mMyLock);
1478 if (mPaused) {
1479 mPaused = false;
1480 mMyCond.signal();
1481 }
1482}
1483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001484// =========================================================================
1485
Eric Laurent38ccae22011-03-28 18:37:07 -07001486
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001487audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001488 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastenb929e412012-11-08 12:13:58 -08001489 userBase(0), serverBase(0), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001490 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001491 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492{
1493}
1494
Glenn Kasten864585d2012-11-06 16:15:41 -08001495uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001496{
Marco Nelissena1472d92012-03-30 14:36:54 -07001497 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001498
Marco Nelissena1472d92012-03-30 14:36:54 -07001499 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500 u += frameCount;
1501 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten864585d2012-11-06 16:15:41 -08001502 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001503 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1504 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1505 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1506 }
Glenn Kasten90548972011-12-13 11:45:07 -08001507 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001508 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001509 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001510 }
1511
Marco Nelissena1472d92012-03-30 14:36:54 -07001512 uint32_t fc = this->frameCount;
1513 if (u >= fc) {
1514 // common case, user didn't just wrap
1515 if (u - fc >= userBase ) {
1516 userBase += fc;
1517 }
1518 } else if (u >= userBase + fc) {
1519 // user just wrapped
1520 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001521 }
1522
Glenn Kasten90548972011-12-13 11:45:07 -08001523 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524
1525 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001526 if (flags & CBLK_UNDERRUN) {
1527 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001528 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001529
1530 return u;
1531}
1532
Glenn Kasten864585d2012-11-06 16:15:41 -08001533bool audio_track_cblk_t::stepServer(uint32_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001534{
Marco Nelissena1472d92012-03-30 14:36:54 -07001535 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1536
Eric Laurent38ccae22011-03-28 18:37:07 -07001537 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001538 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001539 return false;
1540 }
1541
Glenn Kasten90548972011-12-13 11:45:07 -08001542 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001543 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001544
1545 s += frameCount;
Glenn Kasten864585d2012-11-06 16:15:41 -08001546 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547 // Mark that we have read the first buffer so that next time stepUser() is called
1548 // we switch to normal obtainBuffer() timeout period
1549 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001550 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001551 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552 // It is possible that we receive a flush()
1553 // while the mixer is processing a block: in this case,
1554 // stepServer() is called After the flush() has reset u & s and
1555 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001556 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001557 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001558 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559 }
1560 }
1561
1562 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001563 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001564 s = loopStart;
1565 if (--loopCount == 0) {
1566 loopEnd = UINT_MAX;
1567 loopStart = UINT_MAX;
1568 }
1569 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001570
1571 uint32_t fc = this->frameCount;
1572 if (s >= fc) {
1573 // common case, server didn't just wrap
1574 if (s - fc >= serverBase ) {
1575 serverBase += fc;
1576 }
1577 } else if (s >= serverBase + fc) {
1578 // server just wrapped
1579 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001580 }
1581
Glenn Kasten90548972011-12-13 11:45:07 -08001582 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001583
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001584 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001585 cv.signal();
1586 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001587 lock.unlock();
1588 return true;
1589}
1590
Glenn Kastenb929e412012-11-08 12:13:58 -08001591void* audio_track_cblk_t::buffer(void *buffers, uint32_t offset) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001592{
Glenn Kasten90548972011-12-13 11:45:07 -08001593 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594}
1595
Glenn Kasten864585d2012-11-06 16:15:41 -08001596uint32_t audio_track_cblk_t::framesAvailable(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001597{
1598 Mutex::Autolock _l(lock);
Glenn Kasten864585d2012-11-06 16:15:41 -08001599 return framesAvailable_l(isOut);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001600}
1601
Glenn Kasten864585d2012-11-06 16:15:41 -08001602uint32_t audio_track_cblk_t::framesAvailable_l(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603{
Glenn Kasten90548972011-12-13 11:45:07 -08001604 uint32_t u = user;
1605 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606
Glenn Kasten864585d2012-11-06 16:15:41 -08001607 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608 uint32_t limit = (s < loopStart) ? s : loopStart;
1609 return limit + frameCount - u;
1610 } else {
1611 return frameCount + u - s;
1612 }
1613}
1614
Glenn Kasten864585d2012-11-06 16:15:41 -08001615uint32_t audio_track_cblk_t::framesReady(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616{
Glenn Kasten90548972011-12-13 11:45:07 -08001617 uint32_t u = user;
1618 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001619
Glenn Kasten864585d2012-11-06 16:15:41 -08001620 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621 if (u < loopEnd) {
1622 return u - s;
1623 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001624 // do not block on mutex shared with client on AudioFlinger side
1625 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001626 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001627 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001628 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001629 uint32_t frames = UINT_MAX;
1630 if (loopCount >= 0) {
1631 frames = (loopEnd - loopStart)*loopCount + u - s;
1632 }
1633 lock.unlock();
1634 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001635 }
1636 } else {
1637 return s - u;
1638 }
1639}
1640
Eric Laurent38ccae22011-03-28 18:37:07 -07001641bool audio_track_cblk_t::tryLock()
1642{
1643 // the code below simulates lock-with-timeout
1644 // we MUST do this to protect the AudioFlinger server
1645 // as this lock is shared with the client.
1646 status_t err;
1647
1648 err = lock.tryLock();
1649 if (err == -EBUSY) { // just wait a bit
1650 usleep(1000);
1651 err = lock.tryLock();
1652 }
1653 if (err != NO_ERROR) {
1654 // probably, the client just died.
1655 return false;
1656 }
1657 return true;
1658}
1659
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001660// -------------------------------------------------------------------------
1661
1662}; // namespace android