blob: 596523dcc19c39c93ed99f07f67355a762317a5d [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
Glenn Kasten8af901c2012-11-01 11:11:38 -0700201 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
202 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203
Eric Laurent1a9ed112012-03-20 18:36:01 -0700204 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
205
Eric Laurent1703cdf2011-03-07 14:52:59 -0800206 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700207 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000208 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 return INVALID_OPERATION;
210 }
211
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700213 if (streamType == AUDIO_STREAM_DEFAULT) {
214 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700216
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700218 int afSampleRate;
219 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
220 return NO_INIT;
221 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222 sampleRate = afSampleRate;
223 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700224
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800226 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700227 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700229 if (channelMask == 0) {
230 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231 }
232
233 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700234 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000235 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236 return BAD_VALUE;
237 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700238
Glenn Kastene0fa4672012-04-24 14:35:14 -0700239 // AudioFlinger does not currently support 8-bit data in shared memory
240 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
241 ALOGE("8-bit data in shared memory is not supported");
242 return BAD_VALUE;
243 }
244
Eric Laurentc2f1f072009-07-17 12:17:14 -0700245 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700246 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700247 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800248 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700249 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700250 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700251 // only allow deep buffering for music stream type
252 if (streamType != AUDIO_STREAM_MUSIC) {
253 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
254 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700255
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700256 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700257 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700258 return BAD_VALUE;
259 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700260 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700261
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800263 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800264 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800265 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700266
267 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000268 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 return BAD_VALUE;
270 }
271
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800272 mVolume[LEFT] = 1.0f;
273 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800274 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700275 mFrameCount = frameCount;
276 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700277 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700278 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700279 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700280 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700281
Glenn Kastena997e7a2012-08-07 09:44:19 -0700282 if (cbf != NULL) {
Eric Laurentef6be0b2012-09-13 11:18:23 -0700283 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700284 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
285 }
286
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800287 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800288 status_t status = createTrack_l(streamType,
289 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800290 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700291 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800292 frameCount,
293 flags,
294 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700295 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800296
Glenn Kastena997e7a2012-08-07 09:44:19 -0700297 if (status != NO_ERROR) {
298 if (mAudioTrackThread != 0) {
299 mAudioTrackThread->requestExit();
300 mAudioTrackThread.clear();
301 }
302 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700303 }
304
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 mStatus = NO_ERROR;
306
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800308 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700309 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 mChannelCount = channelCount;
311 mSharedBuffer = sharedBuffer;
312 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800313 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315 mLoopCount = 0;
316 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700317 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 mNewPosition = 0;
319 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700320 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700321 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700322 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 return NO_ERROR;
324}
325
326status_t AudioTrack::initCheck() const
327{
328 return mStatus;
329}
330
331// -------------------------------------------------------------------------
332
333uint32_t AudioTrack::latency() const
334{
335 return mLatency;
336}
337
Glenn Kastenfff6d712012-01-12 16:38:12 -0800338audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339{
340 return mStreamType;
341}
342
Glenn Kastene1c39622012-01-04 09:36:37 -0800343audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344{
345 return mFormat;
346}
347
348int AudioTrack::channelCount() const
349{
350 return mChannelCount;
351}
352
353uint32_t AudioTrack::frameCount() const
354{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700355 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356}
357
Glenn Kastenb9980652012-01-11 09:48:27 -0800358size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359{
Dima Zavinfce7a472011-04-19 22:30:36 -0700360 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700361 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700362 } else {
363 return sizeof(uint8_t);
364 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365}
366
367sp<IMemory>& AudioTrack::sharedBuffer()
368{
369 return mSharedBuffer;
370}
371
372// -------------------------------------------------------------------------
373
374void AudioTrack::start()
375{
376 sp<AudioTrackThread> t = mAudioTrackThread;
377
Steve Block3856b092011-10-20 11:56:00 +0100378 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379
Eric Laurentf5aafb22010-11-18 08:40:16 -0800380 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800381 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
382 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700383 sp<IAudioTrack> audioTrack = mAudioTrack;
384 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800385 audio_track_cblk_t* cblk = mCblk;
386
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800387 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700388 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800389 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800390 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700391 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800392 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
393 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700394 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800395 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800396 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800397 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700398 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700399 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700400 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800401 }
402
Steve Block3856b092011-10-20 11:56:00 +0100403 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700404 status_t status = NO_ERROR;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800405 if (!(cblk->flags & CBLK_INVALID_MSK)) {
406 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800407 ALOGV("mAudioTrack->start()");
408 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800409 cblk->lock.lock();
410 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700411 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800412 }
Eric Laurent2b584242009-11-09 23:32:22 -0800413 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800414 if (cblk->flags & CBLK_INVALID_MSK) {
415 status = restoreTrack_l(cblk, true);
416 }
417 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800418 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800420 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800421 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800422 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800423 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700424 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700425 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800426 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800427 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 }
429
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430}
431
432void AudioTrack::stop()
433{
434 sp<AudioTrackThread> t = mAudioTrackThread;
435
Steve Block3856b092011-10-20 11:56:00 +0100436 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437
Eric Laurentf5aafb22010-11-18 08:40:16 -0800438 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800439 if (mActive) {
440 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700441 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 mAudioTrack->stop();
443 // Cancel loops (If we are in the middle of a loop, playback
444 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800445 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700446 // the playback head position will reset to 0, so if a marker is set, we need
447 // to activate it again
448 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 // Force flush if a shared buffer is used otherwise audioflinger
450 // will not stop before end of buffer is reached.
451 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800452 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 }
454 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800455 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800456 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700457 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700458 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 }
460 }
461
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462}
463
464bool AudioTrack::stopped() const
465{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800466 AutoMutex lock(mLock);
467 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468}
469
470void AudioTrack::flush()
471{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800472 AutoMutex lock(mLock);
473 flush_l();
474}
475
476// must be called with mLock held
477void AudioTrack::flush_l()
478{
Steve Block3856b092011-10-20 11:56:00 +0100479 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700480
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700481 // clear playback marker and periodic update counter
482 mMarkerPosition = 0;
483 mMarkerReached = false;
484 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700485
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700487 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 mAudioTrack->flush();
489 // Release AudioTrack callback thread in case it was waiting for new buffers
490 // in AudioTrack::obtainBuffer()
491 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 }
493}
494
495void AudioTrack::pause()
496{
Steve Block3856b092011-10-20 11:56:00 +0100497 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800498 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800499 if (mActive) {
500 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700501 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 mAudioTrack->pause();
503 }
504}
505
506void AudioTrack::mute(bool e)
507{
508 mAudioTrack->mute(e);
509 mMuted = e;
510}
511
512bool AudioTrack::muted() const
513{
514 return mMuted;
515}
516
Eric Laurentbe916aa2010-06-01 23:49:17 -0700517status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800519 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700520 return BAD_VALUE;
521 }
522
Eric Laurent1703cdf2011-03-07 14:52:59 -0800523 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524 mVolume[LEFT] = left;
525 mVolume[RIGHT] = right;
526
Glenn Kasten83d86532012-01-17 14:39:34 -0800527 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700528
529 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530}
531
Glenn Kasten164d6532012-02-27 16:21:04 -0800532status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800533{
Glenn Kasten164d6532012-02-27 16:21:04 -0800534 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700535}
536
Eric Laurent2beeb502010-07-16 07:43:46 -0700537status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700538{
Steve Block3856b092011-10-20 11:56:00 +0100539 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800540 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700541 return BAD_VALUE;
542 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800543 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700544
545 mSendLevel = level;
546
Glenn Kasten05632a52012-01-03 14:22:33 -0800547 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700548
549 return NO_ERROR;
550}
551
Glenn Kastena5224f32012-01-04 12:41:44 -0800552void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700553{
554 if (level != NULL) {
555 *level = mSendLevel;
556 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557}
558
Eric Laurent57326622009-07-07 07:10:45 -0700559status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560{
561 int afSamplingRate;
562
John Grossman4ff14ba2012-02-08 16:37:41 -0800563 if (mIsTimed) {
564 return INVALID_OPERATION;
565 }
566
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700568 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800569 }
570 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700571 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572
Eric Laurent1703cdf2011-03-07 14:52:59 -0800573 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700574 mCblk->sampleRate = rate;
575 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576}
577
Glenn Kastena5224f32012-01-04 12:41:44 -0800578uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579{
John Grossman4ff14ba2012-02-08 16:37:41 -0800580 if (mIsTimed) {
581 return INVALID_OPERATION;
582 }
583
Eric Laurent1703cdf2011-03-07 14:52:59 -0800584 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700585 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586}
587
588status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
589{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800590 AutoMutex lock(mLock);
591 return setLoop_l(loopStart, loopEnd, loopCount);
592}
593
594// must be called with mLock held
595status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
596{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 audio_track_cblk_t* cblk = mCblk;
598
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 Mutex::Autolock _l(cblk->lock);
600
601 if (loopCount == 0) {
602 cblk->loopStart = UINT_MAX;
603 cblk->loopEnd = UINT_MAX;
604 cblk->loopCount = 0;
605 mLoopCount = 0;
606 return NO_ERROR;
607 }
608
John Grossman4ff14ba2012-02-08 16:37:41 -0800609 if (mIsTimed) {
610 return INVALID_OPERATION;
611 }
612
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700614 loopEnd - loopStart > cblk->frameCount ||
615 cblk->server > loopStart) {
Glenn Kasten8af901c2012-11-01 11:11:38 -0700616 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
617 "user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 return BAD_VALUE;
619 }
620
Eric Laurent9b7d9502011-03-21 11:49:00 -0700621 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Glenn Kasten8af901c2012-11-01 11:11:38 -0700622 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
623 "framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700624 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700626 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627
628 cblk->loopStart = loopStart;
629 cblk->loopEnd = loopEnd;
630 cblk->loopCount = loopCount;
631 mLoopCount = loopCount;
632
633 return NO_ERROR;
634}
635
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636status_t AudioTrack::setMarkerPosition(uint32_t marker)
637{
Glenn Kastena0d68332012-01-27 16:47:15 -0800638 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639
640 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700641 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642
643 return NO_ERROR;
644}
645
Glenn Kastena5224f32012-01-04 12:41:44 -0800646status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647{
Glenn Kastena0d68332012-01-27 16:47:15 -0800648 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649
650 *marker = mMarkerPosition;
651
652 return NO_ERROR;
653}
654
655status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
656{
Glenn Kastena0d68332012-01-27 16:47:15 -0800657 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800658
659 uint32_t curPosition;
660 getPosition(&curPosition);
661 mNewPosition = curPosition + updatePeriod;
662 mUpdatePeriod = updatePeriod;
663
664 return NO_ERROR;
665}
666
Glenn Kastena5224f32012-01-04 12:41:44 -0800667status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668{
Glenn Kastena0d68332012-01-27 16:47:15 -0800669 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670
671 *updatePeriod = mUpdatePeriod;
672
673 return NO_ERROR;
674}
675
676status_t AudioTrack::setPosition(uint32_t position)
677{
John Grossman4ff14ba2012-02-08 16:37:41 -0800678 if (mIsTimed) return INVALID_OPERATION;
679
Eric Laurent1703cdf2011-03-07 14:52:59 -0800680 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800682 if (!stopped_l()) return INVALID_OPERATION;
683
684 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685
686 if (position > mCblk->user) return BAD_VALUE;
687
688 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700689 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700690
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691 return NO_ERROR;
692}
693
694status_t AudioTrack::getPosition(uint32_t *position)
695{
Glenn Kastena0d68332012-01-27 16:47:15 -0800696 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800697 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700698 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800699
700 return NO_ERROR;
701}
702
703status_t AudioTrack::reload()
704{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800705 AutoMutex lock(mLock);
706
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800707 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700708
Eric Laurent1703cdf2011-03-07 14:52:59 -0800709 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800710
Eric Laurentd1b449a2010-05-14 03:26:45 -0700711 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712
713 return NO_ERROR;
714}
715
Eric Laurentc2f1f072009-07-17 12:17:14 -0700716audio_io_handle_t AudioTrack::getOutput()
717{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800718 AutoMutex lock(mLock);
719 return getOutput_l();
720}
721
722// must be called with mLock held
723audio_io_handle_t AudioTrack::getOutput_l()
724{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800725 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800726 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700727}
728
Glenn Kastena5224f32012-01-04 12:41:44 -0800729int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700730{
731 return mSessionId;
732}
733
734status_t AudioTrack::attachAuxEffect(int effectId)
735{
Steve Block3856b092011-10-20 11:56:00 +0100736 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700737 status_t status = mAudioTrack->attachAuxEffect(effectId);
738 if (status == NO_ERROR) {
739 mAuxEffectId = effectId;
740 }
741 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700742}
743
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800744// -------------------------------------------------------------------------
745
Eric Laurent1703cdf2011-03-07 14:52:59 -0800746// must be called with mLock held
747status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800748 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800749 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800750 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700751 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800752 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700753 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800754 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700755 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800756{
757 status_t status;
758 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
759 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700760 ALOGE("Could not get audioflinger");
761 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800762 }
763
Eric Laurentd1b449a2010-05-14 03:26:45 -0700764 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700765 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700766 return NO_INIT;
767 }
768
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700769 // Client decides whether the track is TIMED (see below), but can only express a preference
770 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700771 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700772 // either of these use cases:
773 // use case 1: shared buffer
774 (sharedBuffer != 0) ||
775 // use case 2: callback handler
776 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800777 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700778 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700779 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700780 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700781 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700782 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700783
Eric Laurentd1b449a2010-05-14 03:26:45 -0700784 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700785
Dima Zavinfce7a472011-04-19 22:30:36 -0700786 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700787
Eric Laurentd1b449a2010-05-14 03:26:45 -0700788 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700789 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700790 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700791 } else if (frameCount == 0) {
792 int afFrameCount;
793 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
794 return NO_INIT;
795 }
796 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700797 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700798
799 } else if (sharedBuffer != 0) {
800
801 // Ensure that buffer alignment matches channelCount
802 int channelCount = popcount(channelMask);
803 // 8-bit data in shared memory is not currently supported by AudioFlinger
804 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
805 if (channelCount > 1) {
806 // More than 2 channels does not require stronger alignment than stereo
807 alignment <<= 1;
808 }
809 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
810 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
811 sharedBuffer->pointer(), channelCount);
812 return BAD_VALUE;
813 }
814
815 // When initializing a shared buffer AudioTrack via constructors,
816 // there's no frameCount parameter.
817 // But when initializing a shared buffer AudioTrack via set(),
818 // there _is_ a frameCount parameter. We silently ignore it.
819 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
820
821 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
822
823 // FIXME move these calculations and associated checks to server
824 int afSampleRate;
825 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
826 return NO_INIT;
827 }
828 int afFrameCount;
829 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
830 return NO_INIT;
831 }
832
Eric Laurentd1b449a2010-05-14 03:26:45 -0700833 // Ensure that buffer depth covers at least audio hardware latency
834 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
835 if (minBufCount < 2) minBufCount = 2;
836
837 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800838 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
839 ", afLatency=%d",
840 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700841
842 if (frameCount == 0) {
843 frameCount = minFrameCount;
844 }
845 if (mNotificationFramesAct == 0) {
846 mNotificationFramesAct = frameCount/2;
847 }
848 // Make sure that application is notified with sufficient margin
849 // before underrun
850 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
851 mNotificationFramesAct = frameCount/2;
852 }
853 if (frameCount < minFrameCount) {
854 // not ALOGW because it happens all the time when playing key clicks over A2DP
855 ALOGV("Minimum buffer size corrected from %d to %d",
856 frameCount, minFrameCount);
857 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800858 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700859
Glenn Kastene0fa4672012-04-24 14:35:14 -0700860 } else {
861 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700862 }
863
Glenn Kastena075db42012-03-06 11:22:44 -0800864 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
865 if (mIsTimed) {
866 trackFlags |= IAudioFlinger::TRACK_TIMED;
867 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800868
869 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700870 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700871 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800872 if (mAudioTrackThread != 0) {
873 tid = mAudioTrackThread->getTid();
874 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700875 }
876
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800877 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
878 streamType,
879 sampleRate,
880 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700881 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800882 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800883 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 sharedBuffer,
885 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800886 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700887 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800888 &status);
889
890 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000891 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800892 return status;
893 }
894 sp<IMemory> cblk = track->getCblk();
895 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000896 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800897 return NO_INIT;
898 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800899 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800900 mCblkMemory = cblk;
901 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -0800902 // old has the previous value of mCblk->flags before the "or" operation
903 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
904 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
905 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700906 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800907 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -0700908 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700909 // once denied, do not request again if IAudioTrack is re-created
910 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
911 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800912 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700913 if (sharedBuffer == 0) {
914 mNotificationFramesAct = mCblk->frameCount/2;
915 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800916 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800917 if (sharedBuffer == 0) {
918 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
919 } else {
920 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700921 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700922 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800923 }
924
Glenn Kasten8af901c2012-11-01 11:11:38 -0700925 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
926 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800927 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700928 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800929 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
930 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700931 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700932 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -0700933 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700934 // If IAudioTrack is re-created, don't let the requested frameCount
935 // decrease. This can confuse clients that cache frameCount().
936 if (mCblk->frameCount > mFrameCount) {
937 mFrameCount = mCblk->frameCount;
938 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800939 return NO_ERROR;
940}
941
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
943{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800944 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800945 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700946 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 audio_track_cblk_t* cblk = mCblk;
948 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700949 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950
951 audioBuffer->frameCount = 0;
952 audioBuffer->size = 0;
953
954 uint32_t framesAvail = cblk->framesAvailable();
955
Eric Laurent9b7d9502011-03-21 11:49:00 -0700956 cblk->lock.lock();
957 if (cblk->flags & CBLK_INVALID_MSK) {
958 goto create_new_track;
959 }
960 cblk->lock.unlock();
961
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800963 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 goto start_loop_here;
965 while (framesAvail == 0) {
966 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800967 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100968 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800969 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 return NO_MORE_BUFFERS;
971 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800972 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800973 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800975 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700976 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800977 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700978 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700979 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800980 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800981 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800982 return status_t(STOPPED);
983 }
984 cblk->lock.lock();
985 }
986
987 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700988 goto create_new_track;
989 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800990 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700991 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800992 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
993 // timing out when a loop has been set and we have already written upto loop end
994 // is a normal condition: no need to wake AudioFlinger up.
995 if (cblk->user < cblk->loopEnd) {
Glenn Kasten8af901c2012-11-01 11:11:38 -0700996 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
997 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700998 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800999 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001000 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001002 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001003 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001004create_new_track:
1005 result = restoreTrack_l(cblk, false);
1006 }
1007 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001008 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001009 cblk->lock.unlock();
1010 return result;
1011 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001012 }
1013 cblk->waitTimeMs = 0;
1014 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001015
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001016 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001017 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001018 return TIMED_OUT;
1019 }
1020 }
1021 // read the server count again
1022 start_loop_here:
1023 framesAvail = cblk->framesAvailable_l();
1024 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001025 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026 }
1027
1028 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001029
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001030 if (framesReq > framesAvail) {
1031 framesReq = framesAvail;
1032 }
1033
1034 uint32_t u = cblk->user;
1035 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1036
Marco Nelissena1472d92012-03-30 14:36:54 -07001037 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038 framesReq = bufferEnd - u;
1039 }
1040
Eric Laurentc2f1f072009-07-17 12:17:14 -07001041 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1042 audioBuffer->channelCount = mChannelCount;
1043 audioBuffer->frameCount = framesReq;
1044 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001045 if (audio_is_linear_pcm(mFormat)) {
1046 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001047 } else {
1048 audioBuffer->format = mFormat;
1049 }
1050 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001051 active = mActive;
1052 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1053}
1054
1055void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1056{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001057 AutoMutex lock(mLock);
1058 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001059 if (audioBuffer->frameCount > 0) {
1060 // restart track if it was disabled by audioflinger due to previous underrun
1061 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1062 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001063 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001064 mAudioTrack->start();
1065 }
1066 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001067}
1068
1069// -------------------------------------------------------------------------
1070
1071ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1072{
1073
1074 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001075 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076
1077 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001078 // Sanity-check: user is most-likely passing an error code, and it would
1079 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001080 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001081 buffer, userSize, userSize);
1082 return BAD_VALUE;
1083 }
1084
Steve Block3856b092011-10-20 11:56:00 +01001085 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001086
Eric Laurentdf839842012-05-31 14:27:14 -07001087 if (userSize == 0) {
1088 return 0;
1089 }
1090
Eric Laurent1703cdf2011-03-07 14:52:59 -08001091 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1092 // while we are accessing the cblk
1093 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001094 sp<IAudioTrack> audioTrack = mAudioTrack;
1095 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001096 mLock.unlock();
1097
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001098 ssize_t written = 0;
1099 const int8_t *src = (const int8_t *)buffer;
1100 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001101 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001102
1103 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001104 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001105
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001106 status_t err = obtainBuffer(&audioBuffer, -1);
1107 if (err < 0) {
1108 // out of buffers, return #bytes written
1109 if (err == status_t(NO_MORE_BUFFERS))
1110 break;
1111 return ssize_t(err);
1112 }
1113
1114 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001115
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001116 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001117 // Divide capacity by 2 to take expansion into account
1118 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001119 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001120 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001121 toWrite = audioBuffer.size;
1122 memcpy(audioBuffer.i8, src, toWrite);
1123 src += toWrite;
1124 }
1125 userSize -= toWrite;
1126 written += toWrite;
1127
1128 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001129 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001130
1131 return written;
1132}
1133
1134// -------------------------------------------------------------------------
1135
John Grossman4ff14ba2012-02-08 16:37:41 -08001136TimedAudioTrack::TimedAudioTrack() {
1137 mIsTimed = true;
1138}
1139
1140status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1141{
1142 status_t result = UNKNOWN_ERROR;
1143
1144 // If the track is not invalid already, try to allocate a buffer. alloc
1145 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kasten2662ac92012-07-30 10:59:30 -07001146 // we can attempt to restore in just a bit.
John Grossman4ff14ba2012-02-08 16:37:41 -08001147 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1148 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1149 if (result == DEAD_OBJECT) {
1150 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1151 }
1152 }
1153
1154 // If the track is invalid at this point, attempt to restore it. and try the
1155 // allocation one more time.
1156 if (mCblk->flags & CBLK_INVALID_MSK) {
1157 mCblk->lock.lock();
1158 result = restoreTrack_l(mCblk, false);
1159 mCblk->lock.unlock();
1160
1161 if (result == OK)
1162 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1163 }
1164
1165 return result;
1166}
1167
1168status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1169 int64_t pts)
1170{
Eric Laurentdf839842012-05-31 14:27:14 -07001171 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1172 {
1173 AutoMutex lock(mLock);
1174 // restart track if it was disabled by audioflinger due to previous underrun
1175 if (buffer->size() != 0 && status == NO_ERROR &&
1176 mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1177 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1178 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1179 mAudioTrack->start();
1180 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001181 }
Eric Laurentdf839842012-05-31 14:27:14 -07001182 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001183}
1184
1185status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1186 TargetTimeline target)
1187{
1188 return mAudioTrack->setMediaTimeTransform(xform, target);
1189}
1190
1191// -------------------------------------------------------------------------
1192
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001193bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1194{
1195 Buffer audioBuffer;
1196 uint32_t frames;
1197 size_t writtenSize;
1198
Eric Laurent1703cdf2011-03-07 14:52:59 -08001199 mLock.lock();
1200 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1201 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001202 sp<IAudioTrack> audioTrack = mAudioTrack;
1203 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001204 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001205 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001206 mLock.unlock();
1207
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001209 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001210 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001211 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001212 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001213 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001214 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 if (mSharedBuffer != 0) return false;
1217 }
1218 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001219
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001220 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001221 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 int loopCount = -1;
1223 mLoopCount--;
1224 if (mLoopCount >= 0) loopCount = mLoopCount;
1225
1226 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1227 }
1228
1229 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001230 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001231 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001232 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001233 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234 }
1235 }
1236
1237 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001238 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001239 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1241 mNewPosition += mUpdatePeriod;
1242 }
1243 }
1244
1245 // If Shared buffer is used, no data is requested from client.
1246 if (mSharedBuffer != 0) {
1247 frames = 0;
1248 } else {
1249 frames = mRemainingFrames;
1250 }
1251
Glenn Kasten99e53b82012-01-19 08:59:58 -08001252 // See description of waitCount parameter at declaration of obtainBuffer().
1253 // The logic below prevents us from being stuck below at obtainBuffer()
1254 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001255 int32_t waitCount = -1;
1256 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1257 waitCount = 1;
1258 }
1259
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 do {
1261
1262 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001263
Eric Laurent2267ba12011-09-07 11:13:23 -07001264 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 if (err < NO_ERROR) {
1266 if (err != TIMED_OUT) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001267 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1268 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269 return false;
1270 }
1271 break;
1272 }
1273 if (err == status_t(STOPPED)) return false;
1274
1275 // Divide buffer size by 2 to take into account the expansion
1276 // due to 8 to 16 bit conversion: the callback must fill only half
1277 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001278 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279 audioBuffer.size >>= 1;
1280 }
1281
1282 size_t reqSize = audioBuffer.size;
1283 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1284 writtenSize = audioBuffer.size;
1285
1286 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001287 if (ssize_t(writtenSize) <= 0) {
1288 // The callback is done filling buffers
1289 // Keep this thread going to handle timed events and
1290 // still try to get more data in intervals of WAIT_PERIOD_MS
1291 // but don't just loop and block the CPU, so wait
1292 usleep(WAIT_PERIOD_MS*1000);
1293 break;
1294 }
Eric Laurentdf839842012-05-31 14:27:14 -07001295
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001296 if (writtenSize > reqSize) writtenSize = reqSize;
1297
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001298 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001299 // 8 to 16 bit conversion, note that source and destination are the same address
1300 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301 writtenSize <<= 1;
1302 }
1303
1304 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001305 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001306 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001307 // 16 bit.
1308 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1309
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310 frames -= audioBuffer.frameCount;
1311
1312 releaseBuffer(&audioBuffer);
1313 }
1314 while (frames);
1315
1316 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001317 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001318 } else {
1319 mRemainingFrames = frames;
1320 }
1321 return true;
1322}
1323
Eric Laurent1703cdf2011-03-07 14:52:59 -08001324// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1325// the IAudioTrack and IMemory in case they are recreated here.
1326// If the IAudioTrack is successfully restored, the cblk pointer is updated
1327status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1328{
1329 status_t result;
1330
Eric Laurent38ccae22011-03-28 18:37:07 -07001331 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001332 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001333 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001334
Eric Laurent1703cdf2011-03-07 14:52:59 -08001335 // signal old cblk condition so that other threads waiting for available buffers stop
1336 // waiting now
1337 cblk->cv.broadcast();
1338 cblk->lock.unlock();
1339
Eric Laurent9f6530f2011-08-30 10:18:54 -07001340 // refresh the audio configuration cache in this process to make sure we get new
1341 // output parameters in getOutput_l() and createTrack_l()
1342 AudioSystem::clearAudioConfigCache();
1343
Eric Laurent1703cdf2011-03-07 14:52:59 -08001344 // if the new IAudioTrack is created, createTrack_l() will modify the
1345 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1346 // It will also delete the strong references on previous IAudioTrack and IMemory
1347 result = createTrack_l(mStreamType,
1348 cblk->sampleRate,
1349 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001350 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001351 mFrameCount,
1352 mFlags,
1353 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001354 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001355
1356 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001357 uint32_t user = cblk->user;
1358 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001359 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001360 mCblk->user = user;
1361 mCblk->server = user;
1362 mCblk->userBase = user;
1363 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001364 // restore loop: this is not guaranteed to succeed if new frame count is not
1365 // compatible with loop length
1366 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001367 if (!fromStart) {
1368 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001369 // Make sure that a client relying on callback events indicating underrun or
1370 // the actual amount of audio frames played (e.g SoundPool) receives them.
1371 if (mSharedBuffer == 0) {
1372 uint32_t frames = 0;
1373 if (user > server) {
1374 frames = ((user - server) > mCblk->frameCount) ?
1375 mCblk->frameCount : (user - server);
1376 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1377 }
1378 // restart playback even if buffer is not completely filled.
1379 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1380 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1381 // the client
1382 mCblk->stepUser(frames);
1383 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001384 }
Eric Laurent29864602012-05-08 18:57:51 -07001385 if (mSharedBuffer != 0) {
1386 mCblk->stepUser(mCblk->frameCount);
1387 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001388 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001389 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001390 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001391 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001392 if (fromStart && result == NO_ERROR) {
1393 mNewPosition = mCblk->server + mUpdatePeriod;
1394 }
1395 }
1396 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001397 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001398 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001399 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001400 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001401 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001402 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001403 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001404 } else {
Glenn Kasten36665ac2012-11-02 09:59:51 -07001405 bool haveLogged = false;
1406 for (;;) {
1407 if (cblk->flags & CBLK_RESTORED_MSK) {
1408 ALOGW("dead IAudioTrack restored");
1409 result = mRestoreStatus;
1410 cblk->lock.unlock();
1411 break;
1412 }
1413 if (!haveLogged) {
1414 ALOGW("dead IAudioTrack, waiting for a new one");
1415 haveLogged = true;
1416 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001417 mLock.unlock();
1418 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
1419 cblk->lock.unlock();
1420 mLock.lock();
Glenn Kasten36665ac2012-11-02 09:59:51 -07001421 if (result != NO_ERROR) {
1422 ALOGW("timed out");
1423 break;
1424 }
1425 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001426 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001427 }
Steve Block3856b092011-10-20 11:56:00 +01001428 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001429 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001430
1431 if (result == NO_ERROR) {
1432 // from now on we switch to the newly created cblk
1433 cblk = mCblk;
1434 }
1435 cblk->lock.lock();
1436
Steve Block5ff1dd52012-01-05 23:22:43 +00001437 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001438
1439 return result;
1440}
1441
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001442status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1443{
1444
1445 const size_t SIZE = 256;
1446 char buffer[SIZE];
1447 String8 result;
1448
1449 result.append(" AudioTrack::dump\n");
Glenn Kasten8af901c2012-11-01 11:11:38 -07001450 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1451 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 result.append(buffer);
Glenn Kasten8af901c2012-11-01 11:11:38 -07001453 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
1454 mChannelCount, mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001455 result.append(buffer);
Glenn Kasten8af901c2012-11-01 11:11:38 -07001456 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n",
1457 (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458 result.append(buffer);
1459 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1460 result.append(buffer);
1461 ::write(fd, result.string(), result.size());
1462 return NO_ERROR;
1463}
1464
1465// =========================================================================
1466
1467AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001468 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1469{
1470}
1471
1472AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473{
1474}
1475
1476bool AudioTrack::AudioTrackThread::threadLoop()
1477{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001478 {
1479 AutoMutex _l(mMyLock);
1480 if (mPaused) {
1481 mMyCond.wait(mMyLock);
1482 // caller will check for exitPending()
1483 return true;
1484 }
1485 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001486 if (!mReceiver.processAudioBuffer(this)) {
1487 pause();
1488 }
1489 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001490}
1491
Glenn Kasten3acbd052012-02-28 10:39:56 -08001492void AudioTrack::AudioTrackThread::requestExit()
1493{
1494 // must be in this order to avoid a race condition
1495 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001496 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001497}
1498
1499void AudioTrack::AudioTrackThread::pause()
1500{
1501 AutoMutex _l(mMyLock);
1502 mPaused = true;
1503}
1504
1505void AudioTrack::AudioTrackThread::resume()
1506{
1507 AutoMutex _l(mMyLock);
1508 if (mPaused) {
1509 mPaused = false;
1510 mMyCond.signal();
1511 }
1512}
1513
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001514// =========================================================================
1515
Eric Laurent38ccae22011-03-28 18:37:07 -07001516
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001517audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001518 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001519 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001520 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001521 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001522{
1523}
1524
1525uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1526{
Marco Nelissena1472d92012-03-30 14:36:54 -07001527 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528
Marco Nelissena1472d92012-03-30 14:36:54 -07001529 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001530 u += frameCount;
1531 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001532 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001533 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1534 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1535 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1536 }
Glenn Kasten90548972011-12-13 11:45:07 -08001537 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001538 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001539 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001540 }
1541
Marco Nelissena1472d92012-03-30 14:36:54 -07001542 uint32_t fc = this->frameCount;
1543 if (u >= fc) {
1544 // common case, user didn't just wrap
1545 if (u - fc >= userBase ) {
1546 userBase += fc;
1547 }
1548 } else if (u >= userBase + fc) {
1549 // user just wrapped
1550 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551 }
1552
Glenn Kasten90548972011-12-13 11:45:07 -08001553 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001554
1555 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001556 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001557 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001558 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559
1560 return u;
1561}
1562
1563bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1564{
Marco Nelissena1472d92012-03-30 14:36:54 -07001565 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1566
Eric Laurent38ccae22011-03-28 18:37:07 -07001567 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001568 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001569 return false;
1570 }
1571
Glenn Kasten90548972011-12-13 11:45:07 -08001572 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001573 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001574
1575 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001576 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577 // Mark that we have read the first buffer so that next time stepUser() is called
1578 // we switch to normal obtainBuffer() timeout period
1579 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001580 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001581 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 // It is possible that we receive a flush()
1583 // while the mixer is processing a block: in this case,
1584 // stepServer() is called After the flush() has reset u & s and
1585 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001586 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001587 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001588 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001589 }
1590 }
1591
1592 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001593 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594 s = loopStart;
1595 if (--loopCount == 0) {
1596 loopEnd = UINT_MAX;
1597 loopStart = UINT_MAX;
1598 }
1599 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001600
1601 uint32_t fc = this->frameCount;
1602 if (s >= fc) {
1603 // common case, server didn't just wrap
1604 if (s - fc >= serverBase ) {
1605 serverBase += fc;
1606 }
1607 } else if (s >= serverBase + fc) {
1608 // server just wrapped
1609 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001610 }
1611
Glenn Kasten90548972011-12-13 11:45:07 -08001612 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001613
Eric Laurent1703cdf2011-03-07 14:52:59 -08001614 if (!(flags & CBLK_INVALID_MSK)) {
1615 cv.signal();
1616 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001617 lock.unlock();
1618 return true;
1619}
1620
1621void* audio_track_cblk_t::buffer(uint32_t offset) const
1622{
Glenn Kasten90548972011-12-13 11:45:07 -08001623 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001624}
1625
1626uint32_t audio_track_cblk_t::framesAvailable()
1627{
1628 Mutex::Autolock _l(lock);
1629 return framesAvailable_l();
1630}
1631
1632uint32_t audio_track_cblk_t::framesAvailable_l()
1633{
Glenn Kasten90548972011-12-13 11:45:07 -08001634 uint32_t u = user;
1635 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001636
Eric Laurentd1b449a2010-05-14 03:26:45 -07001637 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001638 uint32_t limit = (s < loopStart) ? s : loopStart;
1639 return limit + frameCount - u;
1640 } else {
1641 return frameCount + u - s;
1642 }
1643}
1644
1645uint32_t audio_track_cblk_t::framesReady()
1646{
Glenn Kasten90548972011-12-13 11:45:07 -08001647 uint32_t u = user;
1648 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649
Eric Laurentd1b449a2010-05-14 03:26:45 -07001650 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001651 if (u < loopEnd) {
1652 return u - s;
1653 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001654 // do not block on mutex shared with client on AudioFlinger side
1655 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001656 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001657 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001658 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001659 uint32_t frames = UINT_MAX;
1660 if (loopCount >= 0) {
1661 frames = (loopEnd - loopStart)*loopCount + u - s;
1662 }
1663 lock.unlock();
1664 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001665 }
1666 } else {
1667 return s - u;
1668 }
1669}
1670
Eric Laurent38ccae22011-03-28 18:37:07 -07001671bool audio_track_cblk_t::tryLock()
1672{
1673 // the code below simulates lock-with-timeout
1674 // we MUST do this to protect the AudioFlinger server
1675 // as this lock is shared with the client.
1676 status_t err;
1677
1678 err = lock.tryLock();
1679 if (err == -EBUSY) { // just wait a bit
1680 usleep(1000);
1681 err = lock.tryLock();
1682 }
1683 if (err != NO_ERROR) {
1684 // probably, the client just died.
1685 return false;
1686 }
1687 return true;
1688}
1689
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001690// -------------------------------------------------------------------------
1691
1692}; // namespace android