blob: fa52fa95865892f5a8de207f336f5b55b84fa419 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
26#include <sched.h>
27#include <sys/resource.h>
28
29#include <private/media/AudioTrackShared.h>
30
31#include <media/AudioSystem.h>
32#include <media/AudioTrack.h>
33
34#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Glenn Kasten511754b2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
53 int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 uint32_t sampleRate)
56{
Glenn Kasten04cd0182012-06-25 11:49:27 -070057 if (frameCount == NULL) return BAD_VALUE;
58
59 // default to 0 in case of error
60 *frameCount = 0;
61
Glenn Kastene0fa4672012-04-24 14:35:14 -070062 // FIXME merge with similar code in createTrack_l(), except we're missing
63 // some information here that is available in createTrack_l():
64 // audio_io_handle_t output
65 // audio_format_t format
66 // audio_channel_mask_t channelMask
67 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 int afSampleRate;
69 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
70 return NO_INIT;
71 }
72 int afFrameCount;
73 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 uint32_t afLatency;
77 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80
81 // Ensure that buffer depth covers at least audio hardware latency
82 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
83 if (minBufCount < 2) minBufCount = 2;
84
85 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070086 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080087 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
88 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080089 return NO_ERROR;
90}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92// ---------------------------------------------------------------------------
93
94AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070095 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080096 mIsTimed(false),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070098 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099{
100}
101
102AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800103 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800105 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700106 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700108 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 callback_t cbf,
110 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700111 int notificationFrames,
112 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700113 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800114 mIsTimed(false),
115 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700116 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700118 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700119 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800120 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121}
122
Glenn Kasten17a736c2012-02-14 08:52:15 -0800123// DEPRECATED
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800125 int streamType,
126 uint32_t sampleRate,
127 int format,
128 int channelMask,
129 int frameCount,
130 uint32_t flags,
131 callback_t cbf,
132 void* user,
133 int notificationFrames,
134 int sessionId)
135 : mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800136 mIsTimed(false),
Glenn Kastena6364332012-04-19 09:35:04 -0700137 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
Andreas Huberc8139852012-01-18 10:51:55 -0800138{
Glenn Kasten28b76b32012-07-03 17:24:41 -0700139 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format,
140 (audio_channel_mask_t) channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700141 frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800142 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800143}
144
145AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800146 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800148 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700149 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700151 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 callback_t cbf,
153 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700154 int notificationFrames,
155 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700156 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800157 mIsTimed(false),
158 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700159 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700161 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800162 0 /*frameCount*/, flags, cbf, user, notificationFrames,
163 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164}
165
166AudioTrack::~AudioTrack()
167{
Steve Block3856b092011-10-20 11:56:00 +0100168 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169
170 if (mStatus == NO_ERROR) {
171 // Make sure that callback function exits in the case where
172 // it is looping on buffer full condition in obtainBuffer().
173 // Otherwise the callback thread will never exit.
174 stop();
175 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800176 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 mAudioTrackThread->requestExitAndWait();
178 mAudioTrackThread.clear();
179 }
180 mAudioTrack.clear();
181 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700182 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 }
184}
185
186status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800187 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800189 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700190 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700192 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 callback_t cbf,
194 void* user,
195 int notificationFrames,
196 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700197 bool threadCanCallJava,
198 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199{
200
Steve Block3856b092011-10-20 11:56:00 +0100201 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202
Eric Laurent1a9ed112012-03-20 18:36:01 -0700203 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
204
Eric Laurent1703cdf2011-03-07 14:52:59 -0800205 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700206 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000207 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 return INVALID_OPERATION;
209 }
210
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 if (streamType == AUDIO_STREAM_DEFAULT) {
213 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700215
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700217 int afSampleRate;
218 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
219 return NO_INIT;
220 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 sampleRate = afSampleRate;
222 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700223
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800225 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700226 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700228 if (channelMask == 0) {
229 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 }
231
232 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700233 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000234 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235 return BAD_VALUE;
236 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700237
Glenn Kastene0fa4672012-04-24 14:35:14 -0700238 // AudioFlinger does not currently support 8-bit data in shared memory
239 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
240 ALOGE("8-bit data in shared memory is not supported");
241 return BAD_VALUE;
242 }
243
Eric Laurentc2f1f072009-07-17 12:17:14 -0700244 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700246 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800247 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700248 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700250 // only allow deep buffering for music stream type
251 if (streamType != AUDIO_STREAM_MUSIC) {
252 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
253 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700255 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700256 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700257 return BAD_VALUE;
258 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700259 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700260
Dima Zavinfce7a472011-04-19 22:30:36 -0700261 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800262 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800263 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800264 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265
266 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000267 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 return BAD_VALUE;
269 }
270
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800271 mVolume[LEFT] = 1.0f;
272 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800273 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700274 mFrameCount = frameCount;
275 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700276 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700277 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700278 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700279 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700280
Glenn Kastena997e7a2012-08-07 09:44:19 -0700281 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700282 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700283 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
284 }
285
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800286 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800287 status_t status = createTrack_l(streamType,
288 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800289 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700290 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800291 frameCount,
292 flags,
293 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700294 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800295
Glenn Kastena997e7a2012-08-07 09:44:19 -0700296 if (status != NO_ERROR) {
297 if (mAudioTrackThread != 0) {
298 mAudioTrackThread->requestExit();
299 mAudioTrackThread.clear();
300 }
301 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700302 }
303
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304 mStatus = NO_ERROR;
305
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800307 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700308 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 mChannelCount = channelCount;
310 mSharedBuffer = sharedBuffer;
311 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800312 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 mLoopCount = 0;
315 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700316 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 mNewPosition = 0;
318 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700319 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700320 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700321 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322 return NO_ERROR;
323}
324
325status_t AudioTrack::initCheck() const
326{
327 return mStatus;
328}
329
330// -------------------------------------------------------------------------
331
332uint32_t AudioTrack::latency() const
333{
334 return mLatency;
335}
336
Glenn Kastenfff6d712012-01-12 16:38:12 -0800337audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338{
339 return mStreamType;
340}
341
Glenn Kastene1c39622012-01-04 09:36:37 -0800342audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343{
344 return mFormat;
345}
346
347int AudioTrack::channelCount() const
348{
349 return mChannelCount;
350}
351
352uint32_t AudioTrack::frameCount() const
353{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700354 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355}
356
Glenn Kastenb9980652012-01-11 09:48:27 -0800357size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358{
Dima Zavinfce7a472011-04-19 22:30:36 -0700359 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700360 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 } else {
362 return sizeof(uint8_t);
363 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364}
365
366sp<IMemory>& AudioTrack::sharedBuffer()
367{
368 return mSharedBuffer;
369}
370
371// -------------------------------------------------------------------------
372
373void AudioTrack::start()
374{
375 sp<AudioTrackThread> t = mAudioTrackThread;
376
Steve Block3856b092011-10-20 11:56:00 +0100377 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378
Eric Laurentf5aafb22010-11-18 08:40:16 -0800379 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800380 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
381 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700382 sp<IAudioTrack> audioTrack = mAudioTrack;
383 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800384 audio_track_cblk_t* cblk = mCblk;
385
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800386 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700387 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800388 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800389 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700390 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800391 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
392 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700393 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800394 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800395 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800396 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700397 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700398 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700399 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800400 }
401
Steve Block3856b092011-10-20 11:56:00 +0100402 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700403 status_t status = NO_ERROR;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800404 if (!(cblk->flags & CBLK_INVALID_MSK)) {
405 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800406 ALOGV("mAudioTrack->start()");
407 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800408 cblk->lock.lock();
409 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700410 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800411 }
Eric Laurent2b584242009-11-09 23:32:22 -0800412 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800413 if (cblk->flags & CBLK_INVALID_MSK) {
414 status = restoreTrack_l(cblk, true);
415 }
416 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800417 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100418 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800419 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800420 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800421 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800422 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700423 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700424 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800425 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800426 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427 }
428
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429}
430
431void AudioTrack::stop()
432{
433 sp<AudioTrackThread> t = mAudioTrackThread;
434
Steve Block3856b092011-10-20 11:56:00 +0100435 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436
Eric Laurentf5aafb22010-11-18 08:40:16 -0800437 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800438 if (mActive) {
439 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700440 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441 mAudioTrack->stop();
442 // Cancel loops (If we are in the middle of a loop, playback
443 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800444 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700445 // the playback head position will reset to 0, so if a marker is set, we need
446 // to activate it again
447 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448 // Force flush if a shared buffer is used otherwise audioflinger
449 // will not stop before end of buffer is reached.
450 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800451 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 }
453 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800454 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700456 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700457 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458 }
459 }
460
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461}
462
463bool AudioTrack::stopped() const
464{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800465 AutoMutex lock(mLock);
466 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467}
468
469void AudioTrack::flush()
470{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800471 AutoMutex lock(mLock);
472 flush_l();
473}
474
475// must be called with mLock held
476void AudioTrack::flush_l()
477{
Steve Block3856b092011-10-20 11:56:00 +0100478 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700479
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700480 // clear playback marker and periodic update counter
481 mMarkerPosition = 0;
482 mMarkerReached = false;
483 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700484
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700486 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 mAudioTrack->flush();
488 // Release AudioTrack callback thread in case it was waiting for new buffers
489 // in AudioTrack::obtainBuffer()
490 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491 }
492}
493
494void AudioTrack::pause()
495{
Steve Block3856b092011-10-20 11:56:00 +0100496 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800497 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800498 if (mActive) {
499 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700500 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501 mAudioTrack->pause();
502 }
503}
504
505void AudioTrack::mute(bool e)
506{
507 mAudioTrack->mute(e);
508 mMuted = e;
509}
510
511bool AudioTrack::muted() const
512{
513 return mMuted;
514}
515
Eric Laurentbe916aa2010-06-01 23:49:17 -0700516status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800518 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700519 return BAD_VALUE;
520 }
521
Eric Laurent1703cdf2011-03-07 14:52:59 -0800522 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 mVolume[LEFT] = left;
524 mVolume[RIGHT] = right;
525
Glenn Kasten83d86532012-01-17 14:39:34 -0800526 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700527
528 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529}
530
Glenn Kastena5224f32012-01-04 12:41:44 -0800531void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700533 if (left != NULL) {
534 *left = mVolume[LEFT];
535 }
536 if (right != NULL) {
537 *right = mVolume[RIGHT];
538 }
539}
540
Eric Laurent2beeb502010-07-16 07:43:46 -0700541status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700542{
Steve Block3856b092011-10-20 11:56:00 +0100543 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800544 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700545 return BAD_VALUE;
546 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800547 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700548
549 mSendLevel = level;
550
Glenn Kasten05632a52012-01-03 14:22:33 -0800551 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700552
553 return NO_ERROR;
554}
555
Glenn Kastena5224f32012-01-04 12:41:44 -0800556void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700557{
558 if (level != NULL) {
559 *level = mSendLevel;
560 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561}
562
Eric Laurent57326622009-07-07 07:10:45 -0700563status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
565 int afSamplingRate;
566
John Grossman4ff14ba2012-02-08 16:37:41 -0800567 if (mIsTimed) {
568 return INVALID_OPERATION;
569 }
570
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700572 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573 }
574 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700575 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576
Eric Laurent1703cdf2011-03-07 14:52:59 -0800577 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700578 mCblk->sampleRate = rate;
579 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800580}
581
Glenn Kastena5224f32012-01-04 12:41:44 -0800582uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583{
John Grossman4ff14ba2012-02-08 16:37:41 -0800584 if (mIsTimed) {
585 return INVALID_OPERATION;
586 }
587
Eric Laurent1703cdf2011-03-07 14:52:59 -0800588 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700589 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590}
591
592status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
593{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800594 AutoMutex lock(mLock);
595 return setLoop_l(loopStart, loopEnd, loopCount);
596}
597
598// must be called with mLock held
599status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
600{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601 audio_track_cblk_t* cblk = mCblk;
602
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603 Mutex::Autolock _l(cblk->lock);
604
605 if (loopCount == 0) {
606 cblk->loopStart = UINT_MAX;
607 cblk->loopEnd = UINT_MAX;
608 cblk->loopCount = 0;
609 mLoopCount = 0;
610 return NO_ERROR;
611 }
612
John Grossman4ff14ba2012-02-08 16:37:41 -0800613 if (mIsTimed) {
614 return INVALID_OPERATION;
615 }
616
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700618 loopEnd - loopStart > cblk->frameCount ||
619 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000620 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 -0800621 return BAD_VALUE;
622 }
623
Eric Laurent9b7d9502011-03-21 11:49:00 -0700624 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000625 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700626 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700628 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629
630 cblk->loopStart = loopStart;
631 cblk->loopEnd = loopEnd;
632 cblk->loopCount = loopCount;
633 mLoopCount = loopCount;
634
635 return NO_ERROR;
636}
637
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638status_t AudioTrack::setMarkerPosition(uint32_t marker)
639{
Glenn Kastena0d68332012-01-27 16:47:15 -0800640 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641
642 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700643 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644
645 return NO_ERROR;
646}
647
Glenn Kastena5224f32012-01-04 12:41:44 -0800648status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649{
Glenn Kastena0d68332012-01-27 16:47:15 -0800650 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800651
652 *marker = mMarkerPosition;
653
654 return NO_ERROR;
655}
656
657status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
658{
Glenn Kastena0d68332012-01-27 16:47:15 -0800659 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660
661 uint32_t curPosition;
662 getPosition(&curPosition);
663 mNewPosition = curPosition + updatePeriod;
664 mUpdatePeriod = updatePeriod;
665
666 return NO_ERROR;
667}
668
Glenn Kastena5224f32012-01-04 12:41:44 -0800669status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670{
Glenn Kastena0d68332012-01-27 16:47:15 -0800671 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672
673 *updatePeriod = mUpdatePeriod;
674
675 return NO_ERROR;
676}
677
678status_t AudioTrack::setPosition(uint32_t position)
679{
John Grossman4ff14ba2012-02-08 16:37:41 -0800680 if (mIsTimed) return INVALID_OPERATION;
681
Eric Laurent1703cdf2011-03-07 14:52:59 -0800682 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800684 if (!stopped_l()) return INVALID_OPERATION;
685
686 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687
688 if (position > mCblk->user) return BAD_VALUE;
689
690 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700691 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700692
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 return NO_ERROR;
694}
695
696status_t AudioTrack::getPosition(uint32_t *position)
697{
Glenn Kastena0d68332012-01-27 16:47:15 -0800698 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800699 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700700 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701
702 return NO_ERROR;
703}
704
705status_t AudioTrack::reload()
706{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800707 AutoMutex lock(mLock);
708
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800709 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700710
Eric Laurent1703cdf2011-03-07 14:52:59 -0800711 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712
Eric Laurentd1b449a2010-05-14 03:26:45 -0700713 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714
715 return NO_ERROR;
716}
717
Eric Laurentc2f1f072009-07-17 12:17:14 -0700718audio_io_handle_t AudioTrack::getOutput()
719{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800720 AutoMutex lock(mLock);
721 return getOutput_l();
722}
723
724// must be called with mLock held
725audio_io_handle_t AudioTrack::getOutput_l()
726{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800727 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800728 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700729}
730
Glenn Kastena5224f32012-01-04 12:41:44 -0800731int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700732{
733 return mSessionId;
734}
735
Ricardo Cerqueira54d90af2012-09-02 04:33:40 +0100736extern "C" int _ZNK7android10AudioTrack12getSessionIdEv();
737extern "C" int _ZN7android10AudioTrack12getSessionIdEv() {
738 return _ZNK7android10AudioTrack12getSessionIdEv();
739}
740
Eric Laurentbe916aa2010-06-01 23:49:17 -0700741status_t AudioTrack::attachAuxEffect(int effectId)
742{
Steve Block3856b092011-10-20 11:56:00 +0100743 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700744 status_t status = mAudioTrack->attachAuxEffect(effectId);
745 if (status == NO_ERROR) {
746 mAuxEffectId = effectId;
747 }
748 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700749}
750
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751// -------------------------------------------------------------------------
752
Eric Laurent1703cdf2011-03-07 14:52:59 -0800753// must be called with mLock held
754status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800755 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800756 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800757 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700758 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800759 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700760 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800761 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700762 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800763{
764 status_t status;
765 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
766 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700767 ALOGE("Could not get audioflinger");
768 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800769 }
770
Eric Laurentd1b449a2010-05-14 03:26:45 -0700771 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700772 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700773 return NO_INIT;
774 }
775
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700776 // Client decides whether the track is TIMED (see below), but can only express a preference
777 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700778 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700779 // either of these use cases:
780 // use case 1: shared buffer
781 (sharedBuffer != 0) ||
782 // use case 2: callback handler
783 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800784 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700785 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700786 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700787 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700788 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700789 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700790
Eric Laurentd1b449a2010-05-14 03:26:45 -0700791 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700792
Dima Zavinfce7a472011-04-19 22:30:36 -0700793 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700794
Eric Laurentd1b449a2010-05-14 03:26:45 -0700795 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700796 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700797 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700798 } else if (frameCount == 0) {
799 int afFrameCount;
800 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
801 return NO_INIT;
802 }
803 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700804 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700805
806 } else if (sharedBuffer != 0) {
807
808 // Ensure that buffer alignment matches channelCount
809 int channelCount = popcount(channelMask);
810 // 8-bit data in shared memory is not currently supported by AudioFlinger
811 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
812 if (channelCount > 1) {
813 // More than 2 channels does not require stronger alignment than stereo
814 alignment <<= 1;
815 }
816 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
817 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
818 sharedBuffer->pointer(), channelCount);
819 return BAD_VALUE;
820 }
821
822 // When initializing a shared buffer AudioTrack via constructors,
823 // there's no frameCount parameter.
824 // But when initializing a shared buffer AudioTrack via set(),
825 // there _is_ a frameCount parameter. We silently ignore it.
826 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
827
828 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
829
830 // FIXME move these calculations and associated checks to server
831 int afSampleRate;
832 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
833 return NO_INIT;
834 }
835 int afFrameCount;
836 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
837 return NO_INIT;
838 }
839
Eric Laurentd1b449a2010-05-14 03:26:45 -0700840 // Ensure that buffer depth covers at least audio hardware latency
841 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
842 if (minBufCount < 2) minBufCount = 2;
843
844 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800845 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
846 ", afLatency=%d",
847 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700848
849 if (frameCount == 0) {
850 frameCount = minFrameCount;
851 }
852 if (mNotificationFramesAct == 0) {
853 mNotificationFramesAct = frameCount/2;
854 }
855 // Make sure that application is notified with sufficient margin
856 // before underrun
857 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
858 mNotificationFramesAct = frameCount/2;
859 }
860 if (frameCount < minFrameCount) {
861 // not ALOGW because it happens all the time when playing key clicks over A2DP
862 ALOGV("Minimum buffer size corrected from %d to %d",
863 frameCount, minFrameCount);
864 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800865 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700866
Glenn Kastene0fa4672012-04-24 14:35:14 -0700867 } else {
868 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700869 }
870
Glenn Kastena075db42012-03-06 11:22:44 -0800871 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
872 if (mIsTimed) {
873 trackFlags |= IAudioFlinger::TRACK_TIMED;
874 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800875
876 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700877 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700878 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800879 if (mAudioTrackThread != 0) {
880 tid = mAudioTrackThread->getTid();
881 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700882 }
883
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
885 streamType,
886 sampleRate,
887 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700888 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800889 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800890 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800891 sharedBuffer,
892 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800893 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700894 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800895 &status);
896
897 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000898 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800899 return status;
900 }
901 sp<IMemory> cblk = track->getCblk();
902 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000903 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800904 return NO_INIT;
905 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800906 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800907 mCblkMemory = cblk;
908 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -0800909 // old has the previous value of mCblk->flags before the "or" operation
910 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
911 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
912 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700913 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800914 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700915 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700916 // once denied, do not request again if IAudioTrack is re-created
917 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
918 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800919 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920 if (sharedBuffer == 0) {
921 mNotificationFramesAct = mCblk->frameCount/2;
922 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800923 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800924 if (sharedBuffer == 0) {
925 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
926 } else {
927 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700928 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700929 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800930 }
931
Glenn Kasten83d86532012-01-17 14:39:34 -0800932 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800933 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700934 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800935 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
936 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700937 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700938 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -0700939 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700940 // If IAudioTrack is re-created, don't let the requested frameCount
941 // decrease. This can confuse clients that cache frameCount().
942 if (mCblk->frameCount > mFrameCount) {
943 mFrameCount = mCblk->frameCount;
944 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800945 return NO_ERROR;
946}
947
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800948status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
949{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800950 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800951 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700952 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953 audio_track_cblk_t* cblk = mCblk;
954 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700955 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956
957 audioBuffer->frameCount = 0;
958 audioBuffer->size = 0;
959
960 uint32_t framesAvail = cblk->framesAvailable();
961
Eric Laurent9b7d9502011-03-21 11:49:00 -0700962 cblk->lock.lock();
963 if (cblk->flags & CBLK_INVALID_MSK) {
964 goto create_new_track;
965 }
966 cblk->lock.unlock();
967
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800968 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800969 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 goto start_loop_here;
971 while (framesAvail == 0) {
972 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800973 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100974 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800975 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 return NO_MORE_BUFFERS;
977 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800978 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800979 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800980 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800981 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700982 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800983 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700984 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700985 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800986 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800987 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800988 return status_t(STOPPED);
989 }
990 cblk->lock.lock();
991 }
992
993 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700994 goto create_new_track;
995 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800996 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700997 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800998 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
999 // timing out when a loop has been set and we have already written upto loop end
1000 // is a normal condition: no need to wake AudioFlinger up.
1001 if (cblk->user < cblk->loopEnd) {
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001002 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p name=%#x"
1003 "user=%08x, server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001004 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001005 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001006 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001008 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001009 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001010create_new_track:
1011 result = restoreTrack_l(cblk, false);
1012 }
1013 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001014 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001015 cblk->lock.unlock();
1016 return result;
1017 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001018 }
1019 cblk->waitTimeMs = 0;
1020 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001021
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001022 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001023 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024 return TIMED_OUT;
1025 }
1026 }
1027 // read the server count again
1028 start_loop_here:
1029 framesAvail = cblk->framesAvailable_l();
1030 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001031 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001032 }
1033
1034 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001035
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001036 if (framesReq > framesAvail) {
1037 framesReq = framesAvail;
1038 }
1039
1040 uint32_t u = cblk->user;
1041 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1042
Marco Nelissena1472d92012-03-30 14:36:54 -07001043 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001044 framesReq = bufferEnd - u;
1045 }
1046
Eric Laurentc2f1f072009-07-17 12:17:14 -07001047 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1048 audioBuffer->channelCount = mChannelCount;
1049 audioBuffer->frameCount = framesReq;
1050 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001051 if (audio_is_linear_pcm(mFormat)) {
1052 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001053 } else {
1054 audioBuffer->format = mFormat;
1055 }
1056 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001057 active = mActive;
1058 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1059}
1060
1061void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1062{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001063 AutoMutex lock(mLock);
1064 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001065 if (audioBuffer->frameCount > 0) {
1066 // restart track if it was disabled by audioflinger due to previous underrun
1067 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1068 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001069 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001070 mAudioTrack->start();
1071 }
1072 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001073}
1074
1075// -------------------------------------------------------------------------
1076
1077ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1078{
1079
1080 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001081 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001082
1083 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001084 // Sanity-check: user is most-likely passing an error code, and it would
1085 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001086 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001087 buffer, userSize, userSize);
1088 return BAD_VALUE;
1089 }
1090
Steve Block3856b092011-10-20 11:56:00 +01001091 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001092
Eric Laurentdf839842012-05-31 14:27:14 -07001093 if (userSize == 0) {
1094 return 0;
1095 }
1096
Eric Laurent1703cdf2011-03-07 14:52:59 -08001097 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1098 // while we are accessing the cblk
1099 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001100 sp<IAudioTrack> audioTrack = mAudioTrack;
1101 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001102 mLock.unlock();
1103
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104 ssize_t written = 0;
1105 const int8_t *src = (const int8_t *)buffer;
1106 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001107 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001108
1109 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001110 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001111
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001112 status_t err = obtainBuffer(&audioBuffer, -1);
1113 if (err < 0) {
1114 // out of buffers, return #bytes written
1115 if (err == status_t(NO_MORE_BUFFERS))
1116 break;
1117 return ssize_t(err);
1118 }
1119
1120 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001121
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001122 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001123 // Divide capacity by 2 to take expansion into account
1124 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001125 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001126 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001127 toWrite = audioBuffer.size;
1128 memcpy(audioBuffer.i8, src, toWrite);
1129 src += toWrite;
1130 }
1131 userSize -= toWrite;
1132 written += toWrite;
1133
1134 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001135 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001136
1137 return written;
1138}
1139
1140// -------------------------------------------------------------------------
1141
John Grossman4ff14ba2012-02-08 16:37:41 -08001142TimedAudioTrack::TimedAudioTrack() {
1143 mIsTimed = true;
1144}
1145
1146status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1147{
1148 status_t result = UNKNOWN_ERROR;
1149
1150 // If the track is not invalid already, try to allocate a buffer. alloc
1151 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001152 // we can attempt to restore in just a bit.
John Grossman4ff14ba2012-02-08 16:37:41 -08001153 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1154 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1155 if (result == DEAD_OBJECT) {
1156 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1157 }
1158 }
1159
1160 // If the track is invalid at this point, attempt to restore it. and try the
1161 // allocation one more time.
1162 if (mCblk->flags & CBLK_INVALID_MSK) {
1163 mCblk->lock.lock();
1164 result = restoreTrack_l(mCblk, false);
1165 mCblk->lock.unlock();
1166
1167 if (result == OK)
1168 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1169 }
1170
1171 return result;
1172}
1173
1174status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1175 int64_t pts)
1176{
Eric Laurentdf839842012-05-31 14:27:14 -07001177 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1178 {
1179 AutoMutex lock(mLock);
1180 // restart track if it was disabled by audioflinger due to previous underrun
1181 if (buffer->size() != 0 && status == NO_ERROR &&
1182 mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1183 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1184 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1185 mAudioTrack->start();
1186 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001187 }
Eric Laurentdf839842012-05-31 14:27:14 -07001188 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001189}
1190
1191status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1192 TargetTimeline target)
1193{
1194 return mAudioTrack->setMediaTimeTransform(xform, target);
1195}
1196
1197// -------------------------------------------------------------------------
1198
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001199bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1200{
1201 Buffer audioBuffer;
1202 uint32_t frames;
1203 size_t writtenSize;
1204
Eric Laurent1703cdf2011-03-07 14:52:59 -08001205 mLock.lock();
1206 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1207 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001208 sp<IAudioTrack> audioTrack = mAudioTrack;
1209 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001210 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001211 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001212 mLock.unlock();
1213
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001215 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001216 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001217 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001218 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001219 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001220 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001221 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 if (mSharedBuffer != 0) return false;
1223 }
1224 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001225
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001226 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001227 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 int loopCount = -1;
1229 mLoopCount--;
1230 if (mLoopCount >= 0) loopCount = mLoopCount;
1231
1232 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1233 }
1234
1235 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001236 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001237 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001239 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240 }
1241 }
1242
1243 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001244 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001245 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001246 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1247 mNewPosition += mUpdatePeriod;
1248 }
1249 }
1250
1251 // If Shared buffer is used, no data is requested from client.
1252 if (mSharedBuffer != 0) {
1253 frames = 0;
1254 } else {
1255 frames = mRemainingFrames;
1256 }
1257
Glenn Kasten99e53b82012-01-19 08:59:58 -08001258 // See description of waitCount parameter at declaration of obtainBuffer().
1259 // The logic below prevents us from being stuck below at obtainBuffer()
1260 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001261 int32_t waitCount = -1;
1262 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1263 waitCount = 1;
1264 }
1265
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 do {
1267
1268 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001269
Eric Laurent2267ba12011-09-07 11:13:23 -07001270 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 if (err < NO_ERROR) {
1272 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001273 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 -08001274 return false;
1275 }
1276 break;
1277 }
1278 if (err == status_t(STOPPED)) return false;
1279
1280 // Divide buffer size by 2 to take into account the expansion
1281 // due to 8 to 16 bit conversion: the callback must fill only half
1282 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001283 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 audioBuffer.size >>= 1;
1285 }
1286
1287 size_t reqSize = audioBuffer.size;
1288 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1289 writtenSize = audioBuffer.size;
1290
1291 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001292 if (ssize_t(writtenSize) <= 0) {
1293 // The callback is done filling buffers
1294 // Keep this thread going to handle timed events and
1295 // still try to get more data in intervals of WAIT_PERIOD_MS
1296 // but don't just loop and block the CPU, so wait
1297 usleep(WAIT_PERIOD_MS*1000);
1298 break;
1299 }
Eric Laurentdf839842012-05-31 14:27:14 -07001300
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301 if (writtenSize > reqSize) writtenSize = reqSize;
1302
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001303 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001304 // 8 to 16 bit conversion, note that source and destination are the same address
1305 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306 writtenSize <<= 1;
1307 }
1308
1309 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001310 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001311 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001312 // 16 bit.
1313 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1314
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 frames -= audioBuffer.frameCount;
1316
1317 releaseBuffer(&audioBuffer);
1318 }
1319 while (frames);
1320
1321 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001322 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001323 } else {
1324 mRemainingFrames = frames;
1325 }
1326 return true;
1327}
1328
Eric Laurent1703cdf2011-03-07 14:52:59 -08001329// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1330// the IAudioTrack and IMemory in case they are recreated here.
1331// If the IAudioTrack is successfully restored, the cblk pointer is updated
1332status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1333{
1334 status_t result;
1335
Eric Laurent38ccae22011-03-28 18:37:07 -07001336 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001337 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001338 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001339
Eric Laurent1703cdf2011-03-07 14:52:59 -08001340 // signal old cblk condition so that other threads waiting for available buffers stop
1341 // waiting now
1342 cblk->cv.broadcast();
1343 cblk->lock.unlock();
1344
Eric Laurent9f6530f2011-08-30 10:18:54 -07001345 // refresh the audio configuration cache in this process to make sure we get new
1346 // output parameters in getOutput_l() and createTrack_l()
1347 AudioSystem::clearAudioConfigCache();
1348
Eric Laurent1703cdf2011-03-07 14:52:59 -08001349 // if the new IAudioTrack is created, createTrack_l() will modify the
1350 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1351 // It will also delete the strong references on previous IAudioTrack and IMemory
1352 result = createTrack_l(mStreamType,
1353 cblk->sampleRate,
1354 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001355 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001356 mFrameCount,
1357 mFlags,
1358 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001359 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001360
1361 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001362 uint32_t user = cblk->user;
1363 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001364 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001365 mCblk->user = user;
1366 mCblk->server = user;
1367 mCblk->userBase = user;
1368 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001369 // restore loop: this is not guaranteed to succeed if new frame count is not
1370 // compatible with loop length
1371 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001372 if (!fromStart) {
1373 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001374 // Make sure that a client relying on callback events indicating underrun or
1375 // the actual amount of audio frames played (e.g SoundPool) receives them.
1376 if (mSharedBuffer == 0) {
1377 uint32_t frames = 0;
1378 if (user > server) {
1379 frames = ((user - server) > mCblk->frameCount) ?
1380 mCblk->frameCount : (user - server);
1381 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1382 }
1383 // restart playback even if buffer is not completely filled.
1384 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1385 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1386 // the client
1387 mCblk->stepUser(frames);
1388 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001389 }
Eric Laurent29864602012-05-08 18:57:51 -07001390 if (mSharedBuffer != 0) {
1391 mCblk->stepUser(mCblk->frameCount);
1392 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001393 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001394 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001395 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001396 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001397 if (fromStart && result == NO_ERROR) {
1398 mNewPosition = mCblk->server + mUpdatePeriod;
1399 }
1400 }
1401 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001402 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001403 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001404 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001405 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001406 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001407 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001408 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001409 } else {
1410 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001411 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001412 mLock.unlock();
1413 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001414 if (result == NO_ERROR) {
1415 result = mRestoreStatus;
1416 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001417 cblk->lock.unlock();
1418 mLock.lock();
1419 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001420 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001421 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001422 cblk->lock.unlock();
1423 }
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 Kastene53b9ea2012-03-12 16:29:55 -07001426 result, mActive, mCblk, cblk, mCblk->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
1430 cblk = mCblk;
1431 }
1432 cblk->lock.lock();
1433
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
1446 result.append(" AudioTrack::dump\n");
1447 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1448 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001449 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 -08001450 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001451 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 -08001452 result.append(buffer);
1453 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1454 result.append(buffer);
1455 ::write(fd, result.string(), result.size());
1456 return NO_ERROR;
1457}
1458
1459// =========================================================================
1460
1461AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001462 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1463{
1464}
1465
1466AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001467{
1468}
1469
1470bool AudioTrack::AudioTrackThread::threadLoop()
1471{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001472 {
1473 AutoMutex _l(mMyLock);
1474 if (mPaused) {
1475 mMyCond.wait(mMyLock);
1476 // caller will check for exitPending()
1477 return true;
1478 }
1479 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001480 if (!mReceiver.processAudioBuffer(this)) {
1481 pause();
1482 }
1483 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001484}
1485
Glenn Kasten3acbd052012-02-28 10:39:56 -08001486void AudioTrack::AudioTrackThread::requestExit()
1487{
1488 // must be in this order to avoid a race condition
1489 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001490 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001491}
1492
1493void AudioTrack::AudioTrackThread::pause()
1494{
1495 AutoMutex _l(mMyLock);
1496 mPaused = true;
1497}
1498
1499void AudioTrack::AudioTrackThread::resume()
1500{
1501 AutoMutex _l(mMyLock);
1502 if (mPaused) {
1503 mPaused = false;
1504 mMyCond.signal();
1505 }
1506}
1507
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001508// =========================================================================
1509
Eric Laurent38ccae22011-03-28 18:37:07 -07001510
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001511audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001512 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001513 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001514 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001515 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001516{
1517}
1518
1519uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1520{
Marco Nelissena1472d92012-03-30 14:36:54 -07001521 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001522
Marco Nelissena1472d92012-03-30 14:36:54 -07001523 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524 u += frameCount;
1525 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001526 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001527 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1528 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1529 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1530 }
Glenn Kasten90548972011-12-13 11:45:07 -08001531 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001532 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001533 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001534 }
1535
Marco Nelissena1472d92012-03-30 14:36:54 -07001536 uint32_t fc = this->frameCount;
1537 if (u >= fc) {
1538 // common case, user didn't just wrap
1539 if (u - fc >= userBase ) {
1540 userBase += fc;
1541 }
1542 } else if (u >= userBase + fc) {
1543 // user just wrapped
1544 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001545 }
1546
Glenn Kasten90548972011-12-13 11:45:07 -08001547 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001548
1549 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001550 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001551 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001552 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001553
1554 return u;
1555}
1556
1557bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1558{
Marco Nelissena1472d92012-03-30 14:36:54 -07001559 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1560
Eric Laurent38ccae22011-03-28 18:37:07 -07001561 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001562 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001563 return false;
1564 }
1565
Glenn Kasten90548972011-12-13 11:45:07 -08001566 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001567 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001568
1569 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001570 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001571 // Mark that we have read the first buffer so that next time stepUser() is called
1572 // we switch to normal obtainBuffer() timeout period
1573 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001574 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001575 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001576 // It is possible that we receive a flush()
1577 // while the mixer is processing a block: in this case,
1578 // stepServer() is called After the flush() has reset u & s and
1579 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001580 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001581 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001582 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001583 }
1584 }
1585
1586 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001587 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001588 s = loopStart;
1589 if (--loopCount == 0) {
1590 loopEnd = UINT_MAX;
1591 loopStart = UINT_MAX;
1592 }
1593 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001594
1595 uint32_t fc = this->frameCount;
1596 if (s >= fc) {
1597 // common case, server didn't just wrap
1598 if (s - fc >= serverBase ) {
1599 serverBase += fc;
1600 }
1601 } else if (s >= serverBase + fc) {
1602 // server just wrapped
1603 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001604 }
1605
Glenn Kasten90548972011-12-13 11:45:07 -08001606 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001607
Eric Laurent1703cdf2011-03-07 14:52:59 -08001608 if (!(flags & CBLK_INVALID_MSK)) {
1609 cv.signal();
1610 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001611 lock.unlock();
1612 return true;
1613}
1614
1615void* audio_track_cblk_t::buffer(uint32_t offset) const
1616{
Glenn Kasten90548972011-12-13 11:45:07 -08001617 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001618}
1619
1620uint32_t audio_track_cblk_t::framesAvailable()
1621{
1622 Mutex::Autolock _l(lock);
1623 return framesAvailable_l();
1624}
1625
1626uint32_t audio_track_cblk_t::framesAvailable_l()
1627{
Glenn Kasten90548972011-12-13 11:45:07 -08001628 uint32_t u = user;
1629 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630
Eric Laurentd1b449a2010-05-14 03:26:45 -07001631 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001632 uint32_t limit = (s < loopStart) ? s : loopStart;
1633 return limit + frameCount - u;
1634 } else {
1635 return frameCount + u - s;
1636 }
1637}
1638
1639uint32_t audio_track_cblk_t::framesReady()
1640{
Glenn Kasten90548972011-12-13 11:45:07 -08001641 uint32_t u = user;
1642 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643
Eric Laurentd1b449a2010-05-14 03:26:45 -07001644 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645 if (u < loopEnd) {
1646 return u - s;
1647 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001648 // do not block on mutex shared with client on AudioFlinger side
1649 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001650 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001651 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001652 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001653 uint32_t frames = UINT_MAX;
1654 if (loopCount >= 0) {
1655 frames = (loopEnd - loopStart)*loopCount + u - s;
1656 }
1657 lock.unlock();
1658 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001659 }
1660 } else {
1661 return s - u;
1662 }
1663}
1664
Eric Laurent38ccae22011-03-28 18:37:07 -07001665bool audio_track_cblk_t::tryLock()
1666{
1667 // the code below simulates lock-with-timeout
1668 // we MUST do this to protect the AudioFlinger server
1669 // as this lock is shared with the client.
1670 status_t err;
1671
1672 err = lock.tryLock();
1673 if (err == -EBUSY) { // just wait a bit
1674 usleep(1000);
1675 err = lock.tryLock();
1676 }
1677 if (err != NO_ERROR) {
1678 // probably, the client just died.
1679 return false;
1680 }
1681 return true;
1682}
1683
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001684// -------------------------------------------------------------------------
1685
1686}; // namespace android