blob: 1f4f3d06077f20478de9d7303a841ffd1c2e44d0 [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);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700300 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 return NO_ERROR;
302}
303
304status_t AudioTrack::initCheck() const
305{
306 return mStatus;
307}
308
309// -------------------------------------------------------------------------
310
311uint32_t AudioTrack::latency() const
312{
313 return mLatency;
314}
315
Glenn Kastenfff6d712012-01-12 16:38:12 -0800316audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317{
318 return mStreamType;
319}
320
Glenn Kastene1c39622012-01-04 09:36:37 -0800321audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322{
323 return mFormat;
324}
325
326int AudioTrack::channelCount() const
327{
328 return mChannelCount;
329}
330
331uint32_t AudioTrack::frameCount() const
332{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700333 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334}
335
Glenn Kastenb9980652012-01-11 09:48:27 -0800336size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337{
Dima Zavinfce7a472011-04-19 22:30:36 -0700338 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700339 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700340 } else {
341 return sizeof(uint8_t);
342 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343}
344
345sp<IMemory>& AudioTrack::sharedBuffer()
346{
347 return mSharedBuffer;
348}
349
350// -------------------------------------------------------------------------
351
352void AudioTrack::start()
353{
354 sp<AudioTrackThread> t = mAudioTrackThread;
355
Steve Block3856b092011-10-20 11:56:00 +0100356 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357
Eric Laurentf5aafb22010-11-18 08:40:16 -0800358 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800359 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
360 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700361 sp<IAudioTrack> audioTrack = mAudioTrack;
362 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800363 audio_track_cblk_t* cblk = mCblk;
364
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800365 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700366 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800367 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800368 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700369 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800370 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
371 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800372 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800373 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800374 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800375 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700376 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700377 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700378 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800379 }
380
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700381 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700382 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800383 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800384 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800385 ALOGV("mAudioTrack->start()");
386 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800387 cblk->lock.lock();
388 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800389 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800390 }
Eric Laurent2b584242009-11-09 23:32:22 -0800391 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800392 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700393 audio_track_cblk_t* temp = cblk;
394 status = restoreTrack_l(temp, true);
395 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800396 }
397 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800398 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100399 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800400 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800401 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800402 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800403 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700404 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700405 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800406 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800407 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 }
409
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410}
411
412void AudioTrack::stop()
413{
414 sp<AudioTrackThread> t = mAudioTrackThread;
415
Steve Block3856b092011-10-20 11:56:00 +0100416 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417
Eric Laurentf5aafb22010-11-18 08:40:16 -0800418 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800419 if (mActive) {
420 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700421 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 mAudioTrack->stop();
423 // Cancel loops (If we are in the middle of a loop, playback
424 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800425 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700426 // the playback head position will reset to 0, so if a marker is set, we need
427 // to activate it again
428 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 // Force flush if a shared buffer is used otherwise audioflinger
430 // will not stop before end of buffer is reached.
431 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800432 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433 }
434 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800435 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700437 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700438 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439 }
440 }
441
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442}
443
444bool AudioTrack::stopped() const
445{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800446 AutoMutex lock(mLock);
447 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448}
449
450void AudioTrack::flush()
451{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800452 AutoMutex lock(mLock);
453 flush_l();
454}
455
456// must be called with mLock held
457void AudioTrack::flush_l()
458{
Steve Block3856b092011-10-20 11:56:00 +0100459 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700460
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700461 // clear playback marker and periodic update counter
462 mMarkerPosition = 0;
463 mMarkerReached = false;
464 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700465
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700467 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468 mAudioTrack->flush();
469 // Release AudioTrack callback thread in case it was waiting for new buffers
470 // in AudioTrack::obtainBuffer()
471 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 }
473}
474
475void AudioTrack::pause()
476{
Steve Block3856b092011-10-20 11:56:00 +0100477 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800478 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800479 if (mActive) {
480 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700481 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482 mAudioTrack->pause();
483 }
484}
485
486void AudioTrack::mute(bool e)
487{
488 mAudioTrack->mute(e);
489 mMuted = e;
490}
491
492bool AudioTrack::muted() const
493{
494 return mMuted;
495}
496
Eric Laurentbe916aa2010-06-01 23:49:17 -0700497status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800499 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700500 return BAD_VALUE;
501 }
502
Eric Laurent1703cdf2011-03-07 14:52:59 -0800503 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 mVolume[LEFT] = left;
505 mVolume[RIGHT] = right;
506
Glenn Kasten83d86532012-01-17 14:39:34 -0800507 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700508
509 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510}
511
Glenn Kastena5224f32012-01-04 12:41:44 -0800512void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700514 if (left != NULL) {
515 *left = mVolume[LEFT];
516 }
517 if (right != NULL) {
518 *right = mVolume[RIGHT];
519 }
520}
521
Eric Laurent2beeb502010-07-16 07:43:46 -0700522status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700523{
Steve Block3856b092011-10-20 11:56:00 +0100524 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800525 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700526 return BAD_VALUE;
527 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800528 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700529
530 mSendLevel = level;
531
Glenn Kasten05632a52012-01-03 14:22:33 -0800532 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700533
534 return NO_ERROR;
535}
536
Glenn Kastena5224f32012-01-04 12:41:44 -0800537void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700538{
539 if (level != NULL) {
540 *level = mSendLevel;
541 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542}
543
Eric Laurent57326622009-07-07 07:10:45 -0700544status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545{
546 int afSamplingRate;
547
John Grossman4ff14ba2012-02-08 16:37:41 -0800548 if (mIsTimed) {
549 return INVALID_OPERATION;
550 }
551
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700553 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 }
555 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700556 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557
Eric Laurent1703cdf2011-03-07 14:52:59 -0800558 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700559 mCblk->sampleRate = rate;
560 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561}
562
Glenn Kastena5224f32012-01-04 12:41:44 -0800563uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
John Grossman4ff14ba2012-02-08 16:37:41 -0800565 if (mIsTimed) {
566 return INVALID_OPERATION;
567 }
568
Eric Laurent1703cdf2011-03-07 14:52:59 -0800569 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700570 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571}
572
573status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
574{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800575 AutoMutex lock(mLock);
576 return setLoop_l(loopStart, loopEnd, loopCount);
577}
578
579// must be called with mLock held
580status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
581{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 audio_track_cblk_t* cblk = mCblk;
583
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 Mutex::Autolock _l(cblk->lock);
585
586 if (loopCount == 0) {
587 cblk->loopStart = UINT_MAX;
588 cblk->loopEnd = UINT_MAX;
589 cblk->loopCount = 0;
590 mLoopCount = 0;
591 return NO_ERROR;
592 }
593
John Grossman4ff14ba2012-02-08 16:37:41 -0800594 if (mIsTimed) {
595 return INVALID_OPERATION;
596 }
597
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700599 loopEnd - loopStart > cblk->frameCount ||
600 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700601 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
602 "user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603 return BAD_VALUE;
604 }
605
Eric Laurent9b7d9502011-03-21 11:49:00 -0700606 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700607 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
608 "framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700609 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700611 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612
613 cblk->loopStart = loopStart;
614 cblk->loopEnd = loopEnd;
615 cblk->loopCount = loopCount;
616 mLoopCount = loopCount;
617
618 return NO_ERROR;
619}
620
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621status_t AudioTrack::setMarkerPosition(uint32_t marker)
622{
Glenn Kastena0d68332012-01-27 16:47:15 -0800623 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624
625 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700626 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627
628 return NO_ERROR;
629}
630
Glenn Kastena5224f32012-01-04 12:41:44 -0800631status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632{
Glenn Kastena0d68332012-01-27 16:47:15 -0800633 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634
635 *marker = mMarkerPosition;
636
637 return NO_ERROR;
638}
639
640status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
641{
Glenn Kastena0d68332012-01-27 16:47:15 -0800642 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643
644 uint32_t curPosition;
645 getPosition(&curPosition);
646 mNewPosition = curPosition + updatePeriod;
647 mUpdatePeriod = updatePeriod;
648
649 return NO_ERROR;
650}
651
Glenn Kastena5224f32012-01-04 12:41:44 -0800652status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653{
Glenn Kastena0d68332012-01-27 16:47:15 -0800654 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655
656 *updatePeriod = mUpdatePeriod;
657
658 return NO_ERROR;
659}
660
661status_t AudioTrack::setPosition(uint32_t position)
662{
John Grossman4ff14ba2012-02-08 16:37:41 -0800663 if (mIsTimed) return INVALID_OPERATION;
664
Eric Laurent1703cdf2011-03-07 14:52:59 -0800665 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800667 if (!stopped_l()) return INVALID_OPERATION;
668
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700669 audio_track_cblk_t* cblk = mCblk;
670 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700672 if (position > cblk->user) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700674 cblk->server = position;
675 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700676
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677 return NO_ERROR;
678}
679
680status_t AudioTrack::getPosition(uint32_t *position)
681{
Glenn Kastena0d68332012-01-27 16:47:15 -0800682 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800683 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700684 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685
686 return NO_ERROR;
687}
688
689status_t AudioTrack::reload()
690{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800691 AutoMutex lock(mLock);
692
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800693 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694
Eric Laurent1703cdf2011-03-07 14:52:59 -0800695 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700697 audio_track_cblk_t* cblk = mCblk;
698 cblk->stepUser(cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800699
700 return NO_ERROR;
701}
702
Eric Laurentc2f1f072009-07-17 12:17:14 -0700703audio_io_handle_t AudioTrack::getOutput()
704{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800705 AutoMutex lock(mLock);
706 return getOutput_l();
707}
708
709// must be called with mLock held
710audio_io_handle_t AudioTrack::getOutput_l()
711{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800712 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800713 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700714}
715
Glenn Kastena5224f32012-01-04 12:41:44 -0800716int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700717{
718 return mSessionId;
719}
720
721status_t AudioTrack::attachAuxEffect(int effectId)
722{
Steve Block3856b092011-10-20 11:56:00 +0100723 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700724 status_t status = mAudioTrack->attachAuxEffect(effectId);
725 if (status == NO_ERROR) {
726 mAuxEffectId = effectId;
727 }
728 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700729}
730
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800731// -------------------------------------------------------------------------
732
Eric Laurent1703cdf2011-03-07 14:52:59 -0800733// must be called with mLock held
734status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800735 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800736 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800737 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700738 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800739 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700740 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800741 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700742 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800743{
744 status_t status;
745 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
746 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700747 ALOGE("Could not get audioflinger");
748 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800749 }
750
Eric Laurentd1b449a2010-05-14 03:26:45 -0700751 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700752 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700753 return NO_INIT;
754 }
755
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700756 // Client decides whether the track is TIMED (see below), but can only express a preference
757 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700758 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700759 // either of these use cases:
760 // use case 1: shared buffer
761 (sharedBuffer != 0) ||
762 // use case 2: callback handler
763 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800764 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700765 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700766 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700767 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700768 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700769 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700770
Eric Laurentd1b449a2010-05-14 03:26:45 -0700771 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700772
Dima Zavinfce7a472011-04-19 22:30:36 -0700773 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700774
Eric Laurentd1b449a2010-05-14 03:26:45 -0700775 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700776 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700777 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700778 } else if (frameCount == 0) {
779 int afFrameCount;
780 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
781 return NO_INIT;
782 }
783 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700784 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700785
786 } else if (sharedBuffer != 0) {
787
788 // Ensure that buffer alignment matches channelCount
789 int channelCount = popcount(channelMask);
790 // 8-bit data in shared memory is not currently supported by AudioFlinger
791 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
792 if (channelCount > 1) {
793 // More than 2 channels does not require stronger alignment than stereo
794 alignment <<= 1;
795 }
796 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
797 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
798 sharedBuffer->pointer(), channelCount);
799 return BAD_VALUE;
800 }
801
802 // When initializing a shared buffer AudioTrack via constructors,
803 // there's no frameCount parameter.
804 // But when initializing a shared buffer AudioTrack via set(),
805 // there _is_ a frameCount parameter. We silently ignore it.
806 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
807
808 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
809
810 // FIXME move these calculations and associated checks to server
811 int afSampleRate;
812 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
813 return NO_INIT;
814 }
815 int afFrameCount;
816 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
817 return NO_INIT;
818 }
819
Eric Laurentd1b449a2010-05-14 03:26:45 -0700820 // Ensure that buffer depth covers at least audio hardware latency
821 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
822 if (minBufCount < 2) minBufCount = 2;
823
824 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800825 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
826 ", afLatency=%d",
827 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700828
829 if (frameCount == 0) {
830 frameCount = minFrameCount;
831 }
832 if (mNotificationFramesAct == 0) {
833 mNotificationFramesAct = frameCount/2;
834 }
835 // Make sure that application is notified with sufficient margin
836 // before underrun
837 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
838 mNotificationFramesAct = frameCount/2;
839 }
840 if (frameCount < minFrameCount) {
841 // not ALOGW because it happens all the time when playing key clicks over A2DP
842 ALOGV("Minimum buffer size corrected from %d to %d",
843 frameCount, minFrameCount);
844 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800845 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700846
Glenn Kastene0fa4672012-04-24 14:35:14 -0700847 } else {
848 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700849 }
850
Glenn Kastena075db42012-03-06 11:22:44 -0800851 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
852 if (mIsTimed) {
853 trackFlags |= IAudioFlinger::TRACK_TIMED;
854 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800855
856 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700857 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700858 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800859 if (mAudioTrackThread != 0) {
860 tid = mAudioTrackThread->getTid();
861 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700862 }
863
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800864 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
865 streamType,
866 sampleRate,
867 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700868 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800869 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800870 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800871 sharedBuffer,
872 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800873 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700874 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800875 &status);
876
877 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000878 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800879 return status;
880 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700881 sp<IMemory> iMem = track->getCblk();
882 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000883 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 return NO_INIT;
885 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800886 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700887 mCblkMemory = iMem;
888 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
889 mCblk = cblk;
Glenn Kastene0b07172012-11-06 15:03:34 -0800890 android_atomic_or(CBLK_DIRECTION, &cblk->flags);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800891 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800892 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700893 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", cblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800894 } else {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700895 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", cblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700896 // once denied, do not request again if IAudioTrack is re-created
897 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
898 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800899 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700900 if (sharedBuffer == 0) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700901 mNotificationFramesAct = cblk->frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700902 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800903 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800904 if (sharedBuffer == 0) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700905 cblk->buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800906 } else {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700907 cblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700908 // Force buffer full condition as data is already present in shared memory
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700909 cblk->stepUser(cblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800910 }
911
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700912 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700913 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700914 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700915 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700916 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
917 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700918 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700919 // FIXME don't believe this lie
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700920 mLatency = afLatency + (1000*cblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700921 // If IAudioTrack is re-created, don't let the requested frameCount
922 // decrease. This can confuse clients that cache frameCount().
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700923 if (cblk->frameCount > mFrameCount) {
924 mFrameCount = cblk->frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700925 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800926 return NO_ERROR;
927}
928
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
930{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800931 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800932 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700933 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934 audio_track_cblk_t* cblk = mCblk;
935 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700936 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937
938 audioBuffer->frameCount = 0;
939 audioBuffer->size = 0;
940
941 uint32_t framesAvail = cblk->framesAvailable();
942
Eric Laurent9b7d9502011-03-21 11:49:00 -0700943 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800944 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700945 goto create_new_track;
946 }
947 cblk->lock.unlock();
948
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800949 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800950 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800951 goto start_loop_here;
952 while (framesAvail == 0) {
953 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800954 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100955 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800956 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800957 return NO_MORE_BUFFERS;
958 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800959 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800960 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800961 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800962 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800963 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800964 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700965 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700966 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800967 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800968 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800969 return status_t(STOPPED);
970 }
971 cblk->lock.lock();
972 }
973
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800974 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975 goto create_new_track;
976 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800977 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700978 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
980 // timing out when a loop has been set and we have already written upto loop end
981 // is a normal condition: no need to wake AudioFlinger up.
982 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700983 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
984 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800987 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800989 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800990 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800991create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700992 audio_track_cblk_t* temp = cblk;
993 result = restoreTrack_l(temp, false);
994 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800995 }
996 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000997 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800998 cblk->lock.unlock();
999 return result;
1000 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 }
1002 cblk->waitTimeMs = 0;
1003 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001004
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001005 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001006 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 return TIMED_OUT;
1008 }
1009 }
1010 // read the server count again
1011 start_loop_here:
1012 framesAvail = cblk->framesAvailable_l();
1013 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001014 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001015 }
1016
1017 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001018
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001019 if (framesReq > framesAvail) {
1020 framesReq = framesAvail;
1021 }
1022
1023 uint32_t u = cblk->user;
1024 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1025
Marco Nelissena1472d92012-03-30 14:36:54 -07001026 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001027 framesReq = bufferEnd - u;
1028 }
1029
Eric Laurentc2f1f072009-07-17 12:17:14 -07001030 audioBuffer->frameCount = framesReq;
1031 audioBuffer->size = framesReq * cblk->frameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001032 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 active = mActive;
1034 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1035}
1036
1037void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1038{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001039 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001040 audio_track_cblk_t* cblk = mCblk;
1041 cblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001042 if (audioBuffer->frameCount > 0) {
1043 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001044 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1045 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1046 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001047 mAudioTrack->start();
1048 }
1049 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001050}
1051
1052// -------------------------------------------------------------------------
1053
1054ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1055{
1056
1057 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001058 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001059
1060 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001061 // Sanity-check: user is most-likely passing an error code, and it would
1062 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001063 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001064 buffer, userSize, userSize);
1065 return BAD_VALUE;
1066 }
1067
Steve Block3856b092011-10-20 11:56:00 +01001068 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001069
Eric Laurentdf839842012-05-31 14:27:14 -07001070 if (userSize == 0) {
1071 return 0;
1072 }
1073
Eric Laurent1703cdf2011-03-07 14:52:59 -08001074 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1075 // while we are accessing the cblk
1076 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001077 sp<IAudioTrack> audioTrack = mAudioTrack;
1078 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001079 mLock.unlock();
1080
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001081 ssize_t written = 0;
1082 const int8_t *src = (const int8_t *)buffer;
1083 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001084 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001085
1086 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001087 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001088
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001089 status_t err = obtainBuffer(&audioBuffer, -1);
1090 if (err < 0) {
1091 // out of buffers, return #bytes written
1092 if (err == status_t(NO_MORE_BUFFERS))
1093 break;
1094 return ssize_t(err);
1095 }
1096
1097 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001098
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001099 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001100 // Divide capacity by 2 to take expansion into account
1101 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001102 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001103 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104 toWrite = audioBuffer.size;
1105 memcpy(audioBuffer.i8, src, toWrite);
1106 src += toWrite;
1107 }
1108 userSize -= toWrite;
1109 written += toWrite;
1110
1111 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001112 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001113
1114 return written;
1115}
1116
1117// -------------------------------------------------------------------------
1118
John Grossman4ff14ba2012-02-08 16:37:41 -08001119TimedAudioTrack::TimedAudioTrack() {
1120 mIsTimed = true;
1121}
1122
1123status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1124{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001125 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001126 status_t result = UNKNOWN_ERROR;
1127
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001128 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1129 // while we are accessing the cblk
1130 sp<IAudioTrack> audioTrack = mAudioTrack;
1131 sp<IMemory> iMem = mCblkMemory;
1132
John Grossman4ff14ba2012-02-08 16:37:41 -08001133 // If the track is not invalid already, try to allocate a buffer. alloc
1134 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001135 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001136 audio_track_cblk_t* cblk = mCblk;
1137 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001138 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1139 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001140 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001141 }
1142 }
1143
1144 // If the track is invalid at this point, attempt to restore it. and try the
1145 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001146 if (cblk->flags & CBLK_INVALID) {
1147 cblk->lock.lock();
1148 audio_track_cblk_t* temp = cblk;
1149 result = restoreTrack_l(temp, false);
1150 cblk = temp;
1151 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001152
1153 if (result == OK)
1154 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1155 }
1156
1157 return result;
1158}
1159
1160status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1161 int64_t pts)
1162{
Eric Laurentdf839842012-05-31 14:27:14 -07001163 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1164 {
1165 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001166 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001167 // restart track if it was disabled by audioflinger due to previous underrun
1168 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001169 mActive && (cblk->flags & CBLK_DISABLED)) {
1170 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001171 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1172 mAudioTrack->start();
1173 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001174 }
Eric Laurentdf839842012-05-31 14:27:14 -07001175 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001176}
1177
1178status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1179 TargetTimeline target)
1180{
1181 return mAudioTrack->setMediaTimeTransform(xform, target);
1182}
1183
1184// -------------------------------------------------------------------------
1185
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1187{
1188 Buffer audioBuffer;
1189 uint32_t frames;
1190 size_t writtenSize;
1191
Eric Laurent1703cdf2011-03-07 14:52:59 -08001192 mLock.lock();
1193 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1194 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001195 sp<IAudioTrack> audioTrack = mAudioTrack;
1196 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001197 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001198 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001199 mLock.unlock();
1200
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001202 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001204 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001206 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001207 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209 if (mSharedBuffer != 0) return false;
1210 }
1211 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001212
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001214 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 int loopCount = -1;
1216 mLoopCount--;
1217 if (mLoopCount >= 0) loopCount = mLoopCount;
1218
1219 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1220 }
1221
1222 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001223 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001224 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001226 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 }
1228 }
1229
1230 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001231 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001232 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001233 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1234 mNewPosition += mUpdatePeriod;
1235 }
1236 }
1237
1238 // If Shared buffer is used, no data is requested from client.
1239 if (mSharedBuffer != 0) {
1240 frames = 0;
1241 } else {
1242 frames = mRemainingFrames;
1243 }
1244
Glenn Kasten99e53b82012-01-19 08:59:58 -08001245 // See description of waitCount parameter at declaration of obtainBuffer().
1246 // The logic below prevents us from being stuck below at obtainBuffer()
1247 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001248 int32_t waitCount = -1;
1249 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1250 waitCount = 1;
1251 }
1252
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253 do {
1254
1255 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001256
Eric Laurent2267ba12011-09-07 11:13:23 -07001257 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258 if (err < NO_ERROR) {
1259 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001260 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1261 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 return false;
1263 }
1264 break;
1265 }
1266 if (err == status_t(STOPPED)) return false;
1267
1268 // Divide buffer size by 2 to take into account the expansion
1269 // due to 8 to 16 bit conversion: the callback must fill only half
1270 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001271 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 audioBuffer.size >>= 1;
1273 }
1274
1275 size_t reqSize = audioBuffer.size;
1276 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1277 writtenSize = audioBuffer.size;
1278
1279 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001280 if (ssize_t(writtenSize) <= 0) {
1281 // The callback is done filling buffers
1282 // Keep this thread going to handle timed events and
1283 // still try to get more data in intervals of WAIT_PERIOD_MS
1284 // but don't just loop and block the CPU, so wait
1285 usleep(WAIT_PERIOD_MS*1000);
1286 break;
1287 }
Eric Laurentdf839842012-05-31 14:27:14 -07001288
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001289 if (writtenSize > reqSize) writtenSize = reqSize;
1290
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001291 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001292 // 8 to 16 bit conversion, note that source and destination are the same address
1293 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 writtenSize <<= 1;
1295 }
1296
1297 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001298 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1299 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001300 // 16 bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001301 audioBuffer.frameCount = writtenSize/cblk->frameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001302
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001303 frames -= audioBuffer.frameCount;
1304
1305 releaseBuffer(&audioBuffer);
1306 }
1307 while (frames);
1308
1309 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001310 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001311 } else {
1312 mRemainingFrames = frames;
1313 }
1314 return true;
1315}
1316
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001317// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001318// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001319// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1320// FIXME Don't depend on caller to hold strong references.
1321status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001322{
1323 status_t result;
1324
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001325 audio_track_cblk_t* cblk = refCblk;
1326 audio_track_cblk_t* newCblk = cblk;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001327 if (!(android_atomic_or(CBLK_RESTORING, &cblk->flags) & CBLK_RESTORING)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001328 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001329 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001330
Eric Laurent1703cdf2011-03-07 14:52:59 -08001331 // signal old cblk condition so that other threads waiting for available buffers stop
1332 // waiting now
1333 cblk->cv.broadcast();
1334 cblk->lock.unlock();
1335
Eric Laurent9f6530f2011-08-30 10:18:54 -07001336 // refresh the audio configuration cache in this process to make sure we get new
1337 // output parameters in getOutput_l() and createTrack_l()
1338 AudioSystem::clearAudioConfigCache();
1339
Eric Laurent1703cdf2011-03-07 14:52:59 -08001340 // if the new IAudioTrack is created, createTrack_l() will modify the
1341 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1342 // It will also delete the strong references on previous IAudioTrack and IMemory
1343 result = createTrack_l(mStreamType,
1344 cblk->sampleRate,
1345 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001346 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001347 mFrameCount,
1348 mFlags,
1349 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001350 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001351
1352 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001353 uint32_t user = cblk->user;
1354 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001355 // restore write index and set other indexes to reflect empty buffer status
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001356 newCblk = mCblk;
1357 newCblk->user = user;
1358 newCblk->server = user;
1359 newCblk->userBase = user;
1360 newCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001361 // restore loop: this is not guaranteed to succeed if new frame count is not
1362 // compatible with loop length
1363 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001364 if (!fromStart) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001365 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001366 // Make sure that a client relying on callback events indicating underrun or
1367 // the actual amount of audio frames played (e.g SoundPool) receives them.
1368 if (mSharedBuffer == 0) {
1369 uint32_t frames = 0;
1370 if (user > server) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001371 frames = ((user - server) > newCblk->frameCount) ?
1372 newCblk->frameCount : (user - server);
1373 memset(newCblk->buffers, 0, frames * newCblk->frameSize);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001374 }
1375 // restart playback even if buffer is not completely filled.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001376 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001377 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
Eric Laurent408b8dc2011-09-06 12:36:15 -07001378 // the client
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001379 newCblk->stepUser(frames);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001380 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001381 }
Eric Laurent29864602012-05-08 18:57:51 -07001382 if (mSharedBuffer != 0) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001383 newCblk->stepUser(newCblk->frameCount);
Eric Laurent29864602012-05-08 18:57:51 -07001384 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001385 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001386 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001387 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001388 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001389 if (fromStart && result == NO_ERROR) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001390 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001391 }
1392 }
1393 if (result != NO_ERROR) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001394 android_atomic_and(~CBLK_RESTORING, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001395 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001396 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001397 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001398 // signal old cblk condition for other threads waiting for restore completion
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001399 android_atomic_or(CBLK_RESTORED, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001400 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001401 } else {
Glenn Kasten9a08ebc2012-11-02 09:59:51 -07001402 bool haveLogged = false;
1403 for (;;) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001404 if (cblk->flags & CBLK_RESTORED) {
Glenn Kasten9a08ebc2012-11-02 09:59:51 -07001405 ALOGW("dead IAudioTrack restored");
1406 result = mRestoreStatus;
1407 cblk->lock.unlock();
1408 break;
1409 }
1410 if (!haveLogged) {
1411 ALOGW("dead IAudioTrack, waiting for a new one");
1412 haveLogged = true;
1413 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001414 mLock.unlock();
1415 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
1416 cblk->lock.unlock();
1417 mLock.lock();
Glenn Kasten9a08ebc2012-11-02 09:59:51 -07001418 if (result != NO_ERROR) {
1419 ALOGW("timed out");
1420 break;
1421 }
1422 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001423 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001424 }
Steve Block3856b092011-10-20 11:56:00 +01001425 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001426 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001427
1428 if (result == NO_ERROR) {
1429 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001430 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001431 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001432 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001433
Steve Block5ff1dd52012-01-05 23:22:43 +00001434 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001435
1436 return result;
1437}
1438
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001439status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1440{
1441
1442 const size_t SIZE = 256;
1443 char buffer[SIZE];
1444 String8 result;
1445
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001446 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001448 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1449 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001450 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001451 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001452 mChannelCount, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001453 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001454 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001455 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456 result.append(buffer);
1457 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1458 result.append(buffer);
1459 ::write(fd, result.string(), result.size());
1460 return NO_ERROR;
1461}
1462
1463// =========================================================================
1464
1465AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001466 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1467{
1468}
1469
1470AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001471{
1472}
1473
1474bool AudioTrack::AudioTrackThread::threadLoop()
1475{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001476 {
1477 AutoMutex _l(mMyLock);
1478 if (mPaused) {
1479 mMyCond.wait(mMyLock);
1480 // caller will check for exitPending()
1481 return true;
1482 }
1483 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001484 if (!mReceiver.processAudioBuffer(this)) {
1485 pause();
1486 }
1487 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001488}
1489
Glenn Kasten3acbd052012-02-28 10:39:56 -08001490void AudioTrack::AudioTrackThread::requestExit()
1491{
1492 // must be in this order to avoid a race condition
1493 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001494 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001495}
1496
1497void AudioTrack::AudioTrackThread::pause()
1498{
1499 AutoMutex _l(mMyLock);
1500 mPaused = true;
1501}
1502
1503void AudioTrack::AudioTrackThread::resume()
1504{
1505 AutoMutex _l(mMyLock);
1506 if (mPaused) {
1507 mPaused = false;
1508 mMyCond.signal();
1509 }
1510}
1511
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001512// =========================================================================
1513
Eric Laurent38ccae22011-03-28 18:37:07 -07001514
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001515audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001516 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001517 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001518 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001519 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001520{
1521}
1522
1523uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1524{
Marco Nelissena1472d92012-03-30 14:36:54 -07001525 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001526
Marco Nelissena1472d92012-03-30 14:36:54 -07001527 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528 u += frameCount;
1529 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001530 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001531 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1532 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1533 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1534 }
Glenn Kasten90548972011-12-13 11:45:07 -08001535 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001536 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001537 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001538 }
1539
Marco Nelissena1472d92012-03-30 14:36:54 -07001540 uint32_t fc = this->frameCount;
1541 if (u >= fc) {
1542 // common case, user didn't just wrap
1543 if (u - fc >= userBase ) {
1544 userBase += fc;
1545 }
1546 } else if (u >= userBase + fc) {
1547 // user just wrapped
1548 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001549 }
1550
Glenn Kasten90548972011-12-13 11:45:07 -08001551 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552
1553 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001554 if (flags & CBLK_UNDERRUN) {
1555 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001556 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001557
1558 return u;
1559}
1560
1561bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1562{
Marco Nelissena1472d92012-03-30 14:36:54 -07001563 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1564
Eric Laurent38ccae22011-03-28 18:37:07 -07001565 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001566 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567 return false;
1568 }
1569
Glenn Kasten90548972011-12-13 11:45:07 -08001570 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001571 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001572
1573 s += frameCount;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001574 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001575 // Mark that we have read the first buffer so that next time stepUser() is called
1576 // we switch to normal obtainBuffer() timeout period
1577 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001578 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001579 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001580 // It is possible that we receive a flush()
1581 // while the mixer is processing a block: in this case,
1582 // stepServer() is called After the flush() has reset u & s and
1583 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001584 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001585 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001586 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001587 }
1588 }
1589
1590 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001591 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001592 s = loopStart;
1593 if (--loopCount == 0) {
1594 loopEnd = UINT_MAX;
1595 loopStart = UINT_MAX;
1596 }
1597 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001598
1599 uint32_t fc = this->frameCount;
1600 if (s >= fc) {
1601 // common case, server didn't just wrap
1602 if (s - fc >= serverBase ) {
1603 serverBase += fc;
1604 }
1605 } else if (s >= serverBase + fc) {
1606 // server just wrapped
1607 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608 }
1609
Glenn Kasten90548972011-12-13 11:45:07 -08001610 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001611
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001612 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001613 cv.signal();
1614 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001615 lock.unlock();
1616 return true;
1617}
1618
1619void* audio_track_cblk_t::buffer(uint32_t offset) const
1620{
Glenn Kasten90548972011-12-13 11:45:07 -08001621 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001622}
1623
1624uint32_t audio_track_cblk_t::framesAvailable()
1625{
1626 Mutex::Autolock _l(lock);
1627 return framesAvailable_l();
1628}
1629
1630uint32_t audio_track_cblk_t::framesAvailable_l()
1631{
Glenn Kasten90548972011-12-13 11:45:07 -08001632 uint32_t u = user;
1633 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001634
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001635 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001636 uint32_t limit = (s < loopStart) ? s : loopStart;
1637 return limit + frameCount - u;
1638 } else {
1639 return frameCount + u - s;
1640 }
1641}
1642
1643uint32_t audio_track_cblk_t::framesReady()
1644{
Glenn Kasten90548972011-12-13 11:45:07 -08001645 uint32_t u = user;
1646 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001647
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001648 if (flags & CBLK_DIRECTION) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649 if (u < loopEnd) {
1650 return u - s;
1651 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001652 // do not block on mutex shared with client on AudioFlinger side
1653 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001654 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001655 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001656 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001657 uint32_t frames = UINT_MAX;
1658 if (loopCount >= 0) {
1659 frames = (loopEnd - loopStart)*loopCount + u - s;
1660 }
1661 lock.unlock();
1662 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001663 }
1664 } else {
1665 return s - u;
1666 }
1667}
1668
Eric Laurent38ccae22011-03-28 18:37:07 -07001669bool audio_track_cblk_t::tryLock()
1670{
1671 // the code below simulates lock-with-timeout
1672 // we MUST do this to protect the AudioFlinger server
1673 // as this lock is shared with the client.
1674 status_t err;
1675
1676 err = lock.tryLock();
1677 if (err == -EBUSY) { // just wait a bit
1678 usleep(1000);
1679 err = lock.tryLock();
1680 }
1681 if (err != NO_ERROR) {
1682 // probably, the client just died.
1683 return false;
1684 }
1685 return true;
1686}
1687
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001688// -------------------------------------------------------------------------
1689
1690}; // namespace android