blob: 74438eee2b470c86f57340a3e7171cc985f13568 [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 Kastene0fa4672012-04-24 14:35:14 -070057 // FIXME merge with similar code in createTrack_l(), except we're missing
58 // some information here that is available in createTrack_l():
59 // audio_io_handle_t output
60 // audio_format_t format
61 // audio_channel_mask_t channelMask
62 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080063 int afSampleRate;
64 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
65 return NO_INIT;
66 }
67 int afFrameCount;
68 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
69 return NO_INIT;
70 }
71 uint32_t afLatency;
72 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
73 return NO_INIT;
74 }
75
76 // Ensure that buffer depth covers at least audio hardware latency
77 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
78 if (minBufCount < 2) minBufCount = 2;
79
80 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070081 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080082 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
83 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080084 return NO_ERROR;
85}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086
87// ---------------------------------------------------------------------------
88
89AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070090 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080091 mIsTimed(false),
92 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070093 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080094{
95}
96
97AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080098 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800100 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700101 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700103 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 callback_t cbf,
105 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700106 int notificationFrames,
107 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700108 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800109 mIsTimed(false),
110 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700111 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800112{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700113 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700114 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800115 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116}
117
Glenn Kasten17a736c2012-02-14 08:52:15 -0800118// DEPRECATED
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800120 int streamType,
121 uint32_t sampleRate,
122 int format,
123 int channelMask,
124 int frameCount,
125 uint32_t flags,
126 callback_t cbf,
127 void* user,
128 int notificationFrames,
129 int sessionId)
130 : mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800131 mIsTimed(false),
Glenn Kastena6364332012-04-19 09:35:04 -0700132 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
Andreas Huberc8139852012-01-18 10:51:55 -0800133{
134 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700135 frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800136 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800137}
138
139AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800140 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800142 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700143 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700145 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 callback_t cbf,
147 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700148 int notificationFrames,
149 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700150 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800151 mIsTimed(false),
152 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700153 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700155 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800156 0 /*frameCount*/, flags, cbf, user, notificationFrames,
157 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158}
159
160AudioTrack::~AudioTrack()
161{
Steve Block3856b092011-10-20 11:56:00 +0100162 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163
164 if (mStatus == NO_ERROR) {
165 // Make sure that callback function exits in the case where
166 // it is looping on buffer full condition in obtainBuffer().
167 // Otherwise the callback thread will never exit.
168 stop();
169 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800170 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 mAudioTrackThread->requestExitAndWait();
172 mAudioTrackThread.clear();
173 }
174 mAudioTrack.clear();
175 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700176 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 }
178}
179
180status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800181 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800183 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700184 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700186 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 callback_t cbf,
188 void* user,
189 int notificationFrames,
190 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700191 bool threadCanCallJava,
192 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193{
194
Steve Block3856b092011-10-20 11:56:00 +0100195 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196
Eric Laurent1a9ed112012-03-20 18:36:01 -0700197 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
198
Eric Laurent1703cdf2011-03-07 14:52:59 -0800199 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700200 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000201 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202 return INVALID_OPERATION;
203 }
204
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700206 if (streamType == AUDIO_STREAM_DEFAULT) {
207 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700209
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700211 int afSampleRate;
212 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
213 return NO_INIT;
214 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 sampleRate = afSampleRate;
216 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700217
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800218 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800219 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700220 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700222 if (channelMask == 0) {
223 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 }
225
226 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700227 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000228 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229 return BAD_VALUE;
230 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700231
Glenn Kastene0fa4672012-04-24 14:35:14 -0700232 // AudioFlinger does not currently support 8-bit data in shared memory
233 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
234 ALOGE("8-bit data in shared memory is not supported");
235 return BAD_VALUE;
236 }
237
Eric Laurentc2f1f072009-07-17 12:17:14 -0700238 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700240 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800241 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700242 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700243 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700244 // only allow deep buffering for music stream type
245 if (streamType != AUDIO_STREAM_MUSIC) {
246 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
247 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700248
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700249 if (!audio_is_output_channel(channelMask)) {
Steve Block29357bc2012-01-06 19:20:56 +0000250 ALOGE("Invalid channel mask");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700251 return BAD_VALUE;
252 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700253 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800256 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800257 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800258 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259
260 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000261 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 return BAD_VALUE;
263 }
264
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800265 mVolume[LEFT] = 1.0f;
266 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800267 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700268 mFrameCount = frameCount;
269 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700270 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700271 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700272 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700273 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700274
Glenn Kasten3acbd052012-02-28 10:39:56 -0800275 if (cbf != NULL) {
276 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
277 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
278 }
279
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800280 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800281 status_t status = createTrack_l(streamType,
282 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800283 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700284 (uint32_t)channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800285 frameCount,
286 flags,
287 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700288 output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800290 if (status != NO_ERROR) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800291 if (mAudioTrackThread != 0) {
292 mAudioTrackThread->requestExit();
293 mAudioTrackThread.clear();
294 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 return status;
296 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800297
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 mStatus = NO_ERROR;
299
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800301 mFormat = format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700302 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303 mChannelCount = channelCount;
304 mSharedBuffer = sharedBuffer;
305 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800306 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 mLoopCount = 0;
309 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700310 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 mNewPosition = 0;
312 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700313 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700314 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700315 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 return NO_ERROR;
317}
318
319status_t AudioTrack::initCheck() const
320{
321 return mStatus;
322}
323
324// -------------------------------------------------------------------------
325
326uint32_t AudioTrack::latency() const
327{
328 return mLatency;
329}
330
Glenn Kastenfff6d712012-01-12 16:38:12 -0800331audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332{
333 return mStreamType;
334}
335
Glenn Kastene1c39622012-01-04 09:36:37 -0800336audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337{
338 return mFormat;
339}
340
341int AudioTrack::channelCount() const
342{
343 return mChannelCount;
344}
345
346uint32_t AudioTrack::frameCount() const
347{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700348 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349}
350
Glenn Kastenb9980652012-01-11 09:48:27 -0800351size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352{
Dima Zavinfce7a472011-04-19 22:30:36 -0700353 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700354 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 } else {
356 return sizeof(uint8_t);
357 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358}
359
360sp<IMemory>& AudioTrack::sharedBuffer()
361{
362 return mSharedBuffer;
363}
364
365// -------------------------------------------------------------------------
366
367void AudioTrack::start()
368{
369 sp<AudioTrackThread> t = mAudioTrackThread;
370
Steve Block3856b092011-10-20 11:56:00 +0100371 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372
Eric Laurentf5aafb22010-11-18 08:40:16 -0800373 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800374 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
375 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700376 sp<IAudioTrack> audioTrack = mAudioTrack;
377 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800378 audio_track_cblk_t* cblk = mCblk;
379
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800380 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700381 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800382 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800383 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700384 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800385 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
386 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700387 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800388 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800389 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800390 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700391 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700392 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700393 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800394 }
395
Steve Block3856b092011-10-20 11:56:00 +0100396 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700397 status_t status = NO_ERROR;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800398 if (!(cblk->flags & CBLK_INVALID_MSK)) {
399 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800400 ALOGV("mAudioTrack->start()");
401 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800402 cblk->lock.lock();
403 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700404 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800405 }
Eric Laurent2b584242009-11-09 23:32:22 -0800406 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800407 if (cblk->flags & CBLK_INVALID_MSK) {
408 status = restoreTrack_l(cblk, true);
409 }
410 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800411 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100412 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800413 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800414 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800415 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800416 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700417 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700418 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800419 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800420 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 }
422
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423}
424
425void AudioTrack::stop()
426{
427 sp<AudioTrackThread> t = mAudioTrackThread;
428
Steve Block3856b092011-10-20 11:56:00 +0100429 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430
Eric Laurentf5aafb22010-11-18 08:40:16 -0800431 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800432 if (mActive) {
433 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700434 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 mAudioTrack->stop();
436 // Cancel loops (If we are in the middle of a loop, playback
437 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800438 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700439 // the playback head position will reset to 0, so if a marker is set, we need
440 // to activate it again
441 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 // Force flush if a shared buffer is used otherwise audioflinger
443 // will not stop before end of buffer is reached.
444 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800445 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446 }
447 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800448 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700450 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700451 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 }
453 }
454
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455}
456
457bool AudioTrack::stopped() const
458{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800459 AutoMutex lock(mLock);
460 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461}
462
463void AudioTrack::flush()
464{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800465 AutoMutex lock(mLock);
466 flush_l();
467}
468
469// must be called with mLock held
470void AudioTrack::flush_l()
471{
Steve Block3856b092011-10-20 11:56:00 +0100472 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700473
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700474 // clear playback marker and periodic update counter
475 mMarkerPosition = 0;
476 mMarkerReached = false;
477 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700478
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700480 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800481 mAudioTrack->flush();
482 // Release AudioTrack callback thread in case it was waiting for new buffers
483 // in AudioTrack::obtainBuffer()
484 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 }
486}
487
488void AudioTrack::pause()
489{
Steve Block3856b092011-10-20 11:56:00 +0100490 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800491 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800492 if (mActive) {
493 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700494 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495 mAudioTrack->pause();
496 }
497}
498
499void AudioTrack::mute(bool e)
500{
501 mAudioTrack->mute(e);
502 mMuted = e;
503}
504
505bool AudioTrack::muted() const
506{
507 return mMuted;
508}
509
Eric Laurentbe916aa2010-06-01 23:49:17 -0700510status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800512 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700513 return BAD_VALUE;
514 }
515
Eric Laurent1703cdf2011-03-07 14:52:59 -0800516 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517 mVolume[LEFT] = left;
518 mVolume[RIGHT] = right;
519
Glenn Kasten83d86532012-01-17 14:39:34 -0800520 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700521
522 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523}
524
Glenn Kastena5224f32012-01-04 12:41:44 -0800525void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700527 if (left != NULL) {
528 *left = mVolume[LEFT];
529 }
530 if (right != NULL) {
531 *right = mVolume[RIGHT];
532 }
533}
534
Eric Laurent2beeb502010-07-16 07:43:46 -0700535status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700536{
Steve Block3856b092011-10-20 11:56:00 +0100537 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800538 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700539 return BAD_VALUE;
540 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800541 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700542
543 mSendLevel = level;
544
Glenn Kasten05632a52012-01-03 14:22:33 -0800545 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700546
547 return NO_ERROR;
548}
549
Glenn Kastena5224f32012-01-04 12:41:44 -0800550void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700551{
552 if (level != NULL) {
553 *level = mSendLevel;
554 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555}
556
Eric Laurent57326622009-07-07 07:10:45 -0700557status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558{
559 int afSamplingRate;
560
John Grossman4ff14ba2012-02-08 16:37:41 -0800561 if (mIsTimed) {
562 return INVALID_OPERATION;
563 }
564
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700566 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567 }
568 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700569 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800570
Eric Laurent1703cdf2011-03-07 14:52:59 -0800571 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700572 mCblk->sampleRate = rate;
573 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574}
575
Glenn Kastena5224f32012-01-04 12:41:44 -0800576uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577{
John Grossman4ff14ba2012-02-08 16:37:41 -0800578 if (mIsTimed) {
579 return INVALID_OPERATION;
580 }
581
Eric Laurent1703cdf2011-03-07 14:52:59 -0800582 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700583 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584}
585
586status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
587{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800588 AutoMutex lock(mLock);
589 return setLoop_l(loopStart, loopEnd, loopCount);
590}
591
592// must be called with mLock held
593status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
594{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595 audio_track_cblk_t* cblk = mCblk;
596
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 Mutex::Autolock _l(cblk->lock);
598
599 if (loopCount == 0) {
600 cblk->loopStart = UINT_MAX;
601 cblk->loopEnd = UINT_MAX;
602 cblk->loopCount = 0;
603 mLoopCount = 0;
604 return NO_ERROR;
605 }
606
John Grossman4ff14ba2012-02-08 16:37:41 -0800607 if (mIsTimed) {
608 return INVALID_OPERATION;
609 }
610
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700612 loopEnd - loopStart > cblk->frameCount ||
613 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000614 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 return BAD_VALUE;
616 }
617
Eric Laurent9b7d9502011-03-21 11:49:00 -0700618 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000619 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700620 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700622 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623
624 cblk->loopStart = loopStart;
625 cblk->loopEnd = loopEnd;
626 cblk->loopCount = loopCount;
627 mLoopCount = loopCount;
628
629 return NO_ERROR;
630}
631
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632status_t AudioTrack::setMarkerPosition(uint32_t marker)
633{
Glenn Kastena0d68332012-01-27 16:47:15 -0800634 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635
636 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700637 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638
639 return NO_ERROR;
640}
641
Glenn Kastena5224f32012-01-04 12:41:44 -0800642status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643{
Glenn Kastena0d68332012-01-27 16:47:15 -0800644 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645
646 *marker = mMarkerPosition;
647
648 return NO_ERROR;
649}
650
651status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
652{
Glenn Kastena0d68332012-01-27 16:47:15 -0800653 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654
655 uint32_t curPosition;
656 getPosition(&curPosition);
657 mNewPosition = curPosition + updatePeriod;
658 mUpdatePeriod = updatePeriod;
659
660 return NO_ERROR;
661}
662
Glenn Kastena5224f32012-01-04 12:41:44 -0800663status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664{
Glenn Kastena0d68332012-01-27 16:47:15 -0800665 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666
667 *updatePeriod = mUpdatePeriod;
668
669 return NO_ERROR;
670}
671
672status_t AudioTrack::setPosition(uint32_t position)
673{
John Grossman4ff14ba2012-02-08 16:37:41 -0800674 if (mIsTimed) return INVALID_OPERATION;
675
Eric Laurent1703cdf2011-03-07 14:52:59 -0800676 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800678 if (!stopped_l()) return INVALID_OPERATION;
679
680 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681
682 if (position > mCblk->user) return BAD_VALUE;
683
684 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700685 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700686
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687 return NO_ERROR;
688}
689
690status_t AudioTrack::getPosition(uint32_t *position)
691{
Glenn Kastena0d68332012-01-27 16:47:15 -0800692 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800693 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700694 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695
696 return NO_ERROR;
697}
698
699status_t AudioTrack::reload()
700{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800701 AutoMutex lock(mLock);
702
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800703 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700704
Eric Laurent1703cdf2011-03-07 14:52:59 -0800705 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706
Eric Laurentd1b449a2010-05-14 03:26:45 -0700707 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708
709 return NO_ERROR;
710}
711
Eric Laurentc2f1f072009-07-17 12:17:14 -0700712audio_io_handle_t AudioTrack::getOutput()
713{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800714 AutoMutex lock(mLock);
715 return getOutput_l();
716}
717
718// must be called with mLock held
719audio_io_handle_t AudioTrack::getOutput_l()
720{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800721 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800722 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700723}
724
Glenn Kastena5224f32012-01-04 12:41:44 -0800725int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700726{
727 return mSessionId;
728}
729
730status_t AudioTrack::attachAuxEffect(int effectId)
731{
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700733 status_t status = mAudioTrack->attachAuxEffect(effectId);
734 if (status == NO_ERROR) {
735 mAuxEffectId = effectId;
736 }
737 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700738}
739
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800740// -------------------------------------------------------------------------
741
Eric Laurent1703cdf2011-03-07 14:52:59 -0800742// must be called with mLock held
743status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800744 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800745 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800746 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700747 uint32_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800748 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700749 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800750 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700751 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800752{
753 status_t status;
754 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
755 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700756 ALOGE("Could not get audioflinger");
757 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800758 }
759
Eric Laurentd1b449a2010-05-14 03:26:45 -0700760 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700761 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700762 return NO_INIT;
763 }
764
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700765 // Client decides whether the track is TIMED (see below), but can only express a preference
766 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700767 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700768 // either of these use cases:
769 // use case 1: shared buffer
770 (sharedBuffer != 0) ||
771 // use case 2: callback handler
772 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800773 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700774 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700775 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700776 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700777 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700778 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700779
Eric Laurentd1b449a2010-05-14 03:26:45 -0700780 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700781
Dima Zavinfce7a472011-04-19 22:30:36 -0700782 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700783
Eric Laurentd1b449a2010-05-14 03:26:45 -0700784 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700785 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700786 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700787 } else if (frameCount == 0) {
788 int afFrameCount;
789 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
790 return NO_INIT;
791 }
792 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700793 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700794
795 } else if (sharedBuffer != 0) {
796
797 // Ensure that buffer alignment matches channelCount
798 int channelCount = popcount(channelMask);
799 // 8-bit data in shared memory is not currently supported by AudioFlinger
800 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
801 if (channelCount > 1) {
802 // More than 2 channels does not require stronger alignment than stereo
803 alignment <<= 1;
804 }
805 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
806 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
807 sharedBuffer->pointer(), channelCount);
808 return BAD_VALUE;
809 }
810
811 // When initializing a shared buffer AudioTrack via constructors,
812 // there's no frameCount parameter.
813 // But when initializing a shared buffer AudioTrack via set(),
814 // there _is_ a frameCount parameter. We silently ignore it.
815 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
816
817 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
818
819 // FIXME move these calculations and associated checks to server
820 int afSampleRate;
821 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
822 return NO_INIT;
823 }
824 int afFrameCount;
825 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
826 return NO_INIT;
827 }
828
Eric Laurentd1b449a2010-05-14 03:26:45 -0700829 // Ensure that buffer depth covers at least audio hardware latency
830 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
831 if (minBufCount < 2) minBufCount = 2;
832
833 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800834 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
835 ", afLatency=%d",
836 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700837
838 if (frameCount == 0) {
839 frameCount = minFrameCount;
840 }
841 if (mNotificationFramesAct == 0) {
842 mNotificationFramesAct = frameCount/2;
843 }
844 // Make sure that application is notified with sufficient margin
845 // before underrun
846 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
847 mNotificationFramesAct = frameCount/2;
848 }
849 if (frameCount < minFrameCount) {
850 // not ALOGW because it happens all the time when playing key clicks over A2DP
851 ALOGV("Minimum buffer size corrected from %d to %d",
852 frameCount, minFrameCount);
853 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800854 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700855
Glenn Kastene0fa4672012-04-24 14:35:14 -0700856 } else {
857 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700858 }
859
Glenn Kastena075db42012-03-06 11:22:44 -0800860 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
861 if (mIsTimed) {
862 trackFlags |= IAudioFlinger::TRACK_TIMED;
863 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800864
865 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700866 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700867 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800868 if (mAudioTrackThread != 0) {
869 tid = mAudioTrackThread->getTid();
870 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700871 }
872
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800873 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
874 streamType,
875 sampleRate,
876 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700877 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800878 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800879 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800880 sharedBuffer,
881 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800882 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700883 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 &status);
885
886 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000887 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800888 return status;
889 }
890 sp<IMemory> cblk = track->getCblk();
891 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000892 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800893 return NO_INIT;
894 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800895 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800896 mCblkMemory = cblk;
897 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -0800898 // old has the previous value of mCblk->flags before the "or" operation
899 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
900 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
901 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700902 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800903 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700904 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700905 // once denied, do not request again if IAudioTrack is re-created
906 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
907 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800908 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700909 if (sharedBuffer == 0) {
910 mNotificationFramesAct = mCblk->frameCount/2;
911 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800912 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800913 if (sharedBuffer == 0) {
914 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
915 } else {
916 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700917 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700918 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800919 }
920
Glenn Kasten83d86532012-01-17 14:39:34 -0800921 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800922 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700923 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800924 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
925 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700926 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700927 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -0700928 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700929 // If IAudioTrack is re-created, don't let the requested frameCount
930 // decrease. This can confuse clients that cache frameCount().
931 if (mCblk->frameCount > mFrameCount) {
932 mFrameCount = mCblk->frameCount;
933 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800934 return NO_ERROR;
935}
936
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
938{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800939 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800940 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700941 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942 audio_track_cblk_t* cblk = mCblk;
943 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700944 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945
946 audioBuffer->frameCount = 0;
947 audioBuffer->size = 0;
948
949 uint32_t framesAvail = cblk->framesAvailable();
950
Eric Laurent9b7d9502011-03-21 11:49:00 -0700951 cblk->lock.lock();
952 if (cblk->flags & CBLK_INVALID_MSK) {
953 goto create_new_track;
954 }
955 cblk->lock.unlock();
956
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800957 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800958 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800959 goto start_loop_here;
960 while (framesAvail == 0) {
961 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800962 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800964 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800965 return NO_MORE_BUFFERS;
966 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800967 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800968 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800969 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800970 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700971 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800972 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700973 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700974 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800975 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800976 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800977 return status_t(STOPPED);
978 }
979 cblk->lock.lock();
980 }
981
982 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700983 goto create_new_track;
984 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800985 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700986 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800987 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
988 // timing out when a loop has been set and we have already written upto loop end
989 // is a normal condition: no need to wake AudioFlinger up.
990 if (cblk->user < cblk->loopEnd) {
Glenn Kasten0c9d26d2012-05-31 14:35:01 -0700991 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p name=%#x"
992 "user=%08x, server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700993 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800994 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800995 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800996 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800997 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700998 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800999create_new_track:
1000 result = restoreTrack_l(cblk, false);
1001 }
1002 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001003 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001004 cblk->lock.unlock();
1005 return result;
1006 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 }
1008 cblk->waitTimeMs = 0;
1009 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001010
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001011 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001012 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 return TIMED_OUT;
1014 }
1015 }
1016 // read the server count again
1017 start_loop_here:
1018 framesAvail = cblk->framesAvailable_l();
1019 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001020 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 }
1022
1023 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001024
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001025 if (framesReq > framesAvail) {
1026 framesReq = framesAvail;
1027 }
1028
1029 uint32_t u = cblk->user;
1030 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1031
Marco Nelissena1472d92012-03-30 14:36:54 -07001032 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 framesReq = bufferEnd - u;
1034 }
1035
Eric Laurentc2f1f072009-07-17 12:17:14 -07001036 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1037 audioBuffer->channelCount = mChannelCount;
1038 audioBuffer->frameCount = framesReq;
1039 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001040 if (audio_is_linear_pcm(mFormat)) {
1041 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001042 } else {
1043 audioBuffer->format = mFormat;
1044 }
1045 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001046 active = mActive;
1047 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1048}
1049
1050void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1051{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001052 AutoMutex lock(mLock);
1053 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001054 if (audioBuffer->frameCount > 0) {
1055 // restart track if it was disabled by audioflinger due to previous underrun
1056 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1057 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001058 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001059 mAudioTrack->start();
1060 }
1061 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001062}
1063
1064// -------------------------------------------------------------------------
1065
1066ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1067{
1068
1069 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001070 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001071
1072 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001073 // Sanity-check: user is most-likely passing an error code, and it would
1074 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001075 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076 buffer, userSize, userSize);
1077 return BAD_VALUE;
1078 }
1079
Steve Block3856b092011-10-20 11:56:00 +01001080 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001081
Eric Laurentdf839842012-05-31 14:27:14 -07001082 if (userSize == 0) {
1083 return 0;
1084 }
1085
Eric Laurent1703cdf2011-03-07 14:52:59 -08001086 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1087 // while we are accessing the cblk
1088 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001089 sp<IAudioTrack> audioTrack = mAudioTrack;
1090 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001091 mLock.unlock();
1092
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001093 ssize_t written = 0;
1094 const int8_t *src = (const int8_t *)buffer;
1095 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001096 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001097
1098 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001099 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001100
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001101 status_t err = obtainBuffer(&audioBuffer, -1);
1102 if (err < 0) {
1103 // out of buffers, return #bytes written
1104 if (err == status_t(NO_MORE_BUFFERS))
1105 break;
1106 return ssize_t(err);
1107 }
1108
1109 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001110
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001111 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001112 // Divide capacity by 2 to take expansion into account
1113 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001114 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001115 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116 toWrite = audioBuffer.size;
1117 memcpy(audioBuffer.i8, src, toWrite);
1118 src += toWrite;
1119 }
1120 userSize -= toWrite;
1121 written += toWrite;
1122
1123 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001124 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001125
1126 return written;
1127}
1128
1129// -------------------------------------------------------------------------
1130
John Grossman4ff14ba2012-02-08 16:37:41 -08001131TimedAudioTrack::TimedAudioTrack() {
1132 mIsTimed = true;
1133}
1134
1135status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1136{
1137 status_t result = UNKNOWN_ERROR;
1138
1139 // If the track is not invalid already, try to allocate a buffer. alloc
1140 // fails indicating that the server is dead, flag the track as invalid so
1141 // we can attempt to restore in in just a bit.
1142 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1143 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1144 if (result == DEAD_OBJECT) {
1145 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1146 }
1147 }
1148
1149 // If the track is invalid at this point, attempt to restore it. and try the
1150 // allocation one more time.
1151 if (mCblk->flags & CBLK_INVALID_MSK) {
1152 mCblk->lock.lock();
1153 result = restoreTrack_l(mCblk, false);
1154 mCblk->lock.unlock();
1155
1156 if (result == OK)
1157 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1158 }
1159
1160 return result;
1161}
1162
1163status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1164 int64_t pts)
1165{
Eric Laurentdf839842012-05-31 14:27:14 -07001166 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1167 {
1168 AutoMutex lock(mLock);
1169 // restart track if it was disabled by audioflinger due to previous underrun
1170 if (buffer->size() != 0 && status == NO_ERROR &&
1171 mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1172 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1173 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1174 mAudioTrack->start();
1175 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001176 }
Eric Laurentdf839842012-05-31 14:27:14 -07001177 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001178}
1179
1180status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1181 TargetTimeline target)
1182{
1183 return mAudioTrack->setMediaTimeTransform(xform, target);
1184}
1185
1186// -------------------------------------------------------------------------
1187
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1189{
1190 Buffer audioBuffer;
1191 uint32_t frames;
1192 size_t writtenSize;
1193
Eric Laurent1703cdf2011-03-07 14:52:59 -08001194 mLock.lock();
1195 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1196 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001197 sp<IAudioTrack> audioTrack = mAudioTrack;
1198 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001199 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001200 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001201 mLock.unlock();
1202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001204 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001205 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001206 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001208 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001209 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001210 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001211 if (mSharedBuffer != 0) return false;
1212 }
1213 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001214
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001216 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217 int loopCount = -1;
1218 mLoopCount--;
1219 if (mLoopCount >= 0) loopCount = mLoopCount;
1220
1221 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1222 }
1223
1224 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001225 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001226 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001228 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229 }
1230 }
1231
1232 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001233 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001234 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1236 mNewPosition += mUpdatePeriod;
1237 }
1238 }
1239
1240 // If Shared buffer is used, no data is requested from client.
1241 if (mSharedBuffer != 0) {
1242 frames = 0;
1243 } else {
1244 frames = mRemainingFrames;
1245 }
1246
Glenn Kasten99e53b82012-01-19 08:59:58 -08001247 // See description of waitCount parameter at declaration of obtainBuffer().
1248 // The logic below prevents us from being stuck below at obtainBuffer()
1249 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001250 int32_t waitCount = -1;
1251 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1252 waitCount = 1;
1253 }
1254
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 do {
1256
1257 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001258
Eric Laurent2267ba12011-09-07 11:13:23 -07001259 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 if (err < NO_ERROR) {
1261 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001262 ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 return false;
1264 }
1265 break;
1266 }
1267 if (err == status_t(STOPPED)) return false;
1268
1269 // Divide buffer size by 2 to take into account the expansion
1270 // due to 8 to 16 bit conversion: the callback must fill only half
1271 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001272 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273 audioBuffer.size >>= 1;
1274 }
1275
1276 size_t reqSize = audioBuffer.size;
1277 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1278 writtenSize = audioBuffer.size;
1279
1280 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001281 if (ssize_t(writtenSize) <= 0) {
1282 // The callback is done filling buffers
1283 // Keep this thread going to handle timed events and
1284 // still try to get more data in intervals of WAIT_PERIOD_MS
1285 // but don't just loop and block the CPU, so wait
1286 usleep(WAIT_PERIOD_MS*1000);
1287 break;
1288 }
Eric Laurentdf839842012-05-31 14:27:14 -07001289
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 if (writtenSize > reqSize) writtenSize = reqSize;
1291
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001292 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001293 // 8 to 16 bit conversion, note that source and destination are the same address
1294 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 writtenSize <<= 1;
1296 }
1297
1298 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001299 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001300 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001301 // 16 bit.
1302 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1303
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304 frames -= audioBuffer.frameCount;
1305
1306 releaseBuffer(&audioBuffer);
1307 }
1308 while (frames);
1309
1310 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001311 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001312 } else {
1313 mRemainingFrames = frames;
1314 }
1315 return true;
1316}
1317
Eric Laurent1703cdf2011-03-07 14:52:59 -08001318// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1319// the IAudioTrack and IMemory in case they are recreated here.
1320// If the IAudioTrack is successfully restored, the cblk pointer is updated
1321status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1322{
1323 status_t result;
1324
Eric Laurent38ccae22011-03-28 18:37:07 -07001325 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001326 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001327 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001328
Eric Laurent1703cdf2011-03-07 14:52:59 -08001329 // signal old cblk condition so that other threads waiting for available buffers stop
1330 // waiting now
1331 cblk->cv.broadcast();
1332 cblk->lock.unlock();
1333
Eric Laurent9f6530f2011-08-30 10:18:54 -07001334 // refresh the audio configuration cache in this process to make sure we get new
1335 // output parameters in getOutput_l() and createTrack_l()
1336 AudioSystem::clearAudioConfigCache();
1337
Eric Laurent1703cdf2011-03-07 14:52:59 -08001338 // if the new IAudioTrack is created, createTrack_l() will modify the
1339 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1340 // It will also delete the strong references on previous IAudioTrack and IMemory
1341 result = createTrack_l(mStreamType,
1342 cblk->sampleRate,
1343 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001344 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001345 mFrameCount,
1346 mFlags,
1347 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001348 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001349
1350 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001351 uint32_t user = cblk->user;
1352 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001353 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001354 mCblk->user = user;
1355 mCblk->server = user;
1356 mCblk->userBase = user;
1357 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001358 // restore loop: this is not guaranteed to succeed if new frame count is not
1359 // compatible with loop length
1360 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001361 if (!fromStart) {
1362 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001363 // Make sure that a client relying on callback events indicating underrun or
1364 // the actual amount of audio frames played (e.g SoundPool) receives them.
1365 if (mSharedBuffer == 0) {
1366 uint32_t frames = 0;
1367 if (user > server) {
1368 frames = ((user - server) > mCblk->frameCount) ?
1369 mCblk->frameCount : (user - server);
1370 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1371 }
1372 // restart playback even if buffer is not completely filled.
1373 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1374 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1375 // the client
1376 mCblk->stepUser(frames);
1377 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001378 }
Eric Laurent29864602012-05-08 18:57:51 -07001379 if (mSharedBuffer != 0) {
1380 mCblk->stepUser(mCblk->frameCount);
1381 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001382 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001383 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001384 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001385 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001386 if (fromStart && result == NO_ERROR) {
1387 mNewPosition = mCblk->server + mUpdatePeriod;
1388 }
1389 }
1390 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001391 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001392 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001393 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001394 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001395 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001396 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001397 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001398 } else {
1399 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001400 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001401 mLock.unlock();
1402 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001403 if (result == NO_ERROR) {
1404 result = mRestoreStatus;
1405 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001406 cblk->lock.unlock();
1407 mLock.lock();
1408 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001409 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001410 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001411 cblk->lock.unlock();
1412 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001413 }
Steve Block3856b092011-10-20 11:56:00 +01001414 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001415 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001416
1417 if (result == NO_ERROR) {
1418 // from now on we switch to the newly created cblk
1419 cblk = mCblk;
1420 }
1421 cblk->lock.lock();
1422
Steve Block5ff1dd52012-01-05 23:22:43 +00001423 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001424
1425 return result;
1426}
1427
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001428status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1429{
1430
1431 const size_t SIZE = 256;
1432 char buffer[SIZE];
1433 String8 result;
1434
1435 result.append(" AudioTrack::dump\n");
1436 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1437 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001438 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001439 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001440 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 result.append(buffer);
1442 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1443 result.append(buffer);
1444 ::write(fd, result.string(), result.size());
1445 return NO_ERROR;
1446}
1447
1448// =========================================================================
1449
1450AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001451 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1452{
1453}
1454
1455AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456{
1457}
1458
1459bool AudioTrack::AudioTrackThread::threadLoop()
1460{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001461 {
1462 AutoMutex _l(mMyLock);
1463 if (mPaused) {
1464 mMyCond.wait(mMyLock);
1465 // caller will check for exitPending()
1466 return true;
1467 }
1468 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001469 if (!mReceiver.processAudioBuffer(this)) {
1470 pause();
1471 }
1472 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473}
1474
1475status_t AudioTrack::AudioTrackThread::readyToRun()
1476{
1477 return NO_ERROR;
1478}
1479
1480void AudioTrack::AudioTrackThread::onFirstRef()
1481{
1482}
1483
Glenn Kasten3acbd052012-02-28 10:39:56 -08001484void AudioTrack::AudioTrackThread::requestExit()
1485{
1486 // must be in this order to avoid a race condition
1487 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001488 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001489}
1490
1491void AudioTrack::AudioTrackThread::pause()
1492{
1493 AutoMutex _l(mMyLock);
1494 mPaused = true;
1495}
1496
1497void AudioTrack::AudioTrackThread::resume()
1498{
1499 AutoMutex _l(mMyLock);
1500 if (mPaused) {
1501 mPaused = false;
1502 mMyCond.signal();
1503 }
1504}
1505
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001506// =========================================================================
1507
Eric Laurent38ccae22011-03-28 18:37:07 -07001508
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001509audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001510 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001511 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001512 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001513 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001514{
1515}
1516
1517uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1518{
Marco Nelissena1472d92012-03-30 14:36:54 -07001519 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001520
Marco Nelissena1472d92012-03-30 14:36:54 -07001521 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001522 u += frameCount;
1523 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001524 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001525 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1526 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1527 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1528 }
Glenn Kasten90548972011-12-13 11:45:07 -08001529 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001530 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001531 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001532 }
1533
Marco Nelissena1472d92012-03-30 14:36:54 -07001534 uint32_t fc = this->frameCount;
1535 if (u >= fc) {
1536 // common case, user didn't just wrap
1537 if (u - fc >= userBase ) {
1538 userBase += fc;
1539 }
1540 } else if (u >= userBase + fc) {
1541 // user just wrapped
1542 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001543 }
1544
Glenn Kasten90548972011-12-13 11:45:07 -08001545 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001546
1547 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001548 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001549 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001550 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551
1552 return u;
1553}
1554
1555bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1556{
Marco Nelissena1472d92012-03-30 14:36:54 -07001557 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1558
Eric Laurent38ccae22011-03-28 18:37:07 -07001559 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001560 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561 return false;
1562 }
1563
Glenn Kasten90548972011-12-13 11:45:07 -08001564 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001565 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001566
1567 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001568 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001569 // Mark that we have read the first buffer so that next time stepUser() is called
1570 // we switch to normal obtainBuffer() timeout period
1571 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001572 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001573 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001574 // It is possible that we receive a flush()
1575 // while the mixer is processing a block: in this case,
1576 // stepServer() is called After the flush() has reset u & s and
1577 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001578 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001579 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001580 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001581 }
1582 }
1583
1584 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001585 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001586 s = loopStart;
1587 if (--loopCount == 0) {
1588 loopEnd = UINT_MAX;
1589 loopStart = UINT_MAX;
1590 }
1591 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001592
1593 uint32_t fc = this->frameCount;
1594 if (s >= fc) {
1595 // common case, server didn't just wrap
1596 if (s - fc >= serverBase ) {
1597 serverBase += fc;
1598 }
1599 } else if (s >= serverBase + fc) {
1600 // server just wrapped
1601 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001602 }
1603
Glenn Kasten90548972011-12-13 11:45:07 -08001604 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605
Eric Laurent1703cdf2011-03-07 14:52:59 -08001606 if (!(flags & CBLK_INVALID_MSK)) {
1607 cv.signal();
1608 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001609 lock.unlock();
1610 return true;
1611}
1612
1613void* audio_track_cblk_t::buffer(uint32_t offset) const
1614{
Glenn Kasten90548972011-12-13 11:45:07 -08001615 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616}
1617
1618uint32_t audio_track_cblk_t::framesAvailable()
1619{
1620 Mutex::Autolock _l(lock);
1621 return framesAvailable_l();
1622}
1623
1624uint32_t audio_track_cblk_t::framesAvailable_l()
1625{
Glenn Kasten90548972011-12-13 11:45:07 -08001626 uint32_t u = user;
1627 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001628
Eric Laurentd1b449a2010-05-14 03:26:45 -07001629 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630 uint32_t limit = (s < loopStart) ? s : loopStart;
1631 return limit + frameCount - u;
1632 } else {
1633 return frameCount + u - s;
1634 }
1635}
1636
1637uint32_t audio_track_cblk_t::framesReady()
1638{
Glenn Kasten90548972011-12-13 11:45:07 -08001639 uint32_t u = user;
1640 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001641
Eric Laurentd1b449a2010-05-14 03:26:45 -07001642 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643 if (u < loopEnd) {
1644 return u - s;
1645 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001646 // do not block on mutex shared with client on AudioFlinger side
1647 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001648 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001649 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001650 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001651 uint32_t frames = UINT_MAX;
1652 if (loopCount >= 0) {
1653 frames = (loopEnd - loopStart)*loopCount + u - s;
1654 }
1655 lock.unlock();
1656 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001657 }
1658 } else {
1659 return s - u;
1660 }
1661}
1662
Eric Laurent38ccae22011-03-28 18:37:07 -07001663bool audio_track_cblk_t::tryLock()
1664{
1665 // the code below simulates lock-with-timeout
1666 // we MUST do this to protect the AudioFlinger server
1667 // as this lock is shared with the client.
1668 status_t err;
1669
1670 err = lock.tryLock();
1671 if (err == -EBUSY) { // just wait a bit
1672 usleep(1000);
1673 err = lock.tryLock();
1674 }
1675 if (err != NO_ERROR) {
1676 // probably, the client just died.
1677 return false;
1678 }
1679 return true;
1680}
1681
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001682// -------------------------------------------------------------------------
1683
1684}; // namespace android