blob: 2c8460548556ac60193c780dffbf3b033ce67189 [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(
Glenn Kasten7da35f22012-11-14 12:54:39 -080053 size_t* 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 Kastena2049222012-06-22 17:21:07 -070057 if (frameCount == NULL) {
58 return BAD_VALUE;
59 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070060
61 // default to 0 in case of error
62 *frameCount = 0;
63
Glenn Kastene0fa4672012-04-24 14:35:14 -070064 // FIXME merge with similar code in createTrack_l(), except we're missing
65 // some information here that is available in createTrack_l():
66 // audio_io_handle_t output
67 // audio_format_t format
68 // audio_channel_mask_t channelMask
69 // audio_output_flags_t flags
Glenn Kasten1127d652012-11-14 08:44:39 -080070 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080071 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
72 return NO_INIT;
73 }
Glenn Kasten7da35f22012-11-14 12:54:39 -080074 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080075 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
76 return NO_INIT;
77 }
78 uint32_t afLatency;
79 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
80 return NO_INIT;
81 }
82
83 // Ensure that buffer depth covers at least audio hardware latency
84 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
85 if (minBufCount < 2) minBufCount = 2;
86
87 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070088 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080089 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
90 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080091 return NO_ERROR;
92}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093
94// ---------------------------------------------------------------------------
95
96AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070097 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080098 mIsTimed(false),
99 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten552f2742012-12-04 12:22:46 -0800100 mPreviousSchedulingGroup(SP_DEFAULT),
101 mProxy(NULL)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102{
103}
104
105AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800106 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800108 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700109 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700111 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800112 callback_t cbf,
113 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700114 int notificationFrames,
115 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700116 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800117 mIsTimed(false),
118 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten552f2742012-12-04 12:22:46 -0800119 mPreviousSchedulingGroup(SP_DEFAULT),
120 mProxy(NULL)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700122 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700123 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800124 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125}
126
Andreas Huberc8139852012-01-18 10:51:55 -0800127AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800128 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800129 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800130 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700131 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700133 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 callback_t cbf,
135 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700136 int notificationFrames,
137 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700138 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mIsTimed(false),
140 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten552f2742012-12-04 12:22:46 -0800141 mPreviousSchedulingGroup(SP_DEFAULT),
142 mProxy(NULL)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143{
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800144 if (sharedBuffer == 0) {
145 ALOGE("sharedBuffer must be non-0");
146 mStatus = BAD_VALUE;
147 return;
148 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700149 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800150 0 /*frameCount*/, flags, cbf, user, notificationFrames,
151 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152}
153
154AudioTrack::~AudioTrack()
155{
Steve Block3856b092011-10-20 11:56:00 +0100156 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157
158 if (mStatus == NO_ERROR) {
159 // Make sure that callback function exits in the case where
160 // it is looping on buffer full condition in obtainBuffer().
161 // Otherwise the callback thread will never exit.
162 stop();
163 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800164 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 mAudioTrackThread->requestExitAndWait();
166 mAudioTrackThread.clear();
167 }
168 mAudioTrack.clear();
169 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700170 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 }
Glenn Kasten552f2742012-12-04 12:22:46 -0800172 delete mProxy;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173}
174
175status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800176 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800178 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700179 audio_channel_mask_t channelMask,
Glenn Kasten7da35f22012-11-14 12:54:39 -0800180 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700181 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 callback_t cbf,
183 void* user,
184 int notificationFrames,
185 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700186 bool threadCanCallJava,
187 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188{
Glenn Kasten7da35f22012-11-14 12:54:39 -0800189 // FIXME "int" here is legacy and will be replaced by size_t later
190 if (frameCountInt < 0) {
191 ALOGE("Invalid frame count %d", frameCountInt);
192 return BAD_VALUE;
193 }
194 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195
Glenn Kasten8af901c2012-11-01 11:11:38 -0700196 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
197 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198
Glenn Kasten7da35f22012-11-14 12:54:39 -0800199 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700200
Eric Laurent1703cdf2011-03-07 14:52:59 -0800201 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700202 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000203 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 return INVALID_OPERATION;
205 }
206
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700208 if (streamType == AUDIO_STREAM_DEFAULT) {
209 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700211
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 if (sampleRate == 0) {
Glenn Kasten1127d652012-11-14 08:44:39 -0800213 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700214 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
215 return NO_INIT;
216 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217 sampleRate = afSampleRate;
218 }
Glenn Kasten552f2742012-12-04 12:22:46 -0800219 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700220
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800222 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700223 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700225 if (channelMask == 0) {
226 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 }
228
229 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700230 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000231 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232 return BAD_VALUE;
233 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234
Glenn Kastene0fa4672012-04-24 14:35:14 -0700235 // AudioFlinger does not currently support 8-bit data in shared memory
236 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
237 ALOGE("8-bit data in shared memory is not supported");
238 return BAD_VALUE;
239 }
240
Eric Laurentc2f1f072009-07-17 12:17:14 -0700241 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700242 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700243 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800244 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700245 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700246 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700247 // only allow deep buffering for music stream type
248 if (streamType != AUDIO_STREAM_MUSIC) {
249 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
250 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700251
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700252 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700253 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254 return BAD_VALUE;
255 }
Glenn Kastenc45128a2012-11-14 12:47:55 -0800256 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700257 uint32_t channelCount = popcount(channelMask);
Glenn Kastenc45128a2012-11-14 12:47:55 -0800258 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259
Glenn Kasten552f2742012-12-04 12:22:46 -0800260 if (audio_is_linear_pcm(format)) {
261 mFrameSize = channelCount * audio_bytes_per_sample(format);
262 mFrameSizeAF = channelCount * sizeof(int16_t);
263 } else {
264 mFrameSize = sizeof(uint8_t);
265 mFrameSizeAF = sizeof(uint8_t);
266 }
267
Dima Zavinfce7a472011-04-19 22:30:36 -0700268 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800269 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800270 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800271 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272
273 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000274 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 return BAD_VALUE;
276 }
277
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800278 mVolume[LEFT] = 1.0f;
279 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800280 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700281 mFrameCount = frameCount;
Glenn Kastend7101432012-11-14 13:42:25 -0800282 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700283 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700284 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700285 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700286 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700287 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700288
Glenn Kastena997e7a2012-08-07 09:44:19 -0700289 if (cbf != NULL) {
Eric Laurentef6be0b2012-09-13 11:18:23 -0700290 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700291 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
292 }
293
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800294 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800295 status_t status = createTrack_l(streamType,
296 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800297 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800298 frameCount,
299 flags,
300 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700301 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800302
Glenn Kastena997e7a2012-08-07 09:44:19 -0700303 if (status != NO_ERROR) {
304 if (mAudioTrackThread != 0) {
305 mAudioTrackThread->requestExit();
306 mAudioTrackThread.clear();
307 }
308 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700309 }
310
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 mStatus = NO_ERROR;
312
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800314 mFormat = format;
Glenn Kasten5aab59a2012-11-12 07:58:20 -0800315
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 mSharedBuffer = sharedBuffer;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800317 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800319 mLoopCount = 0;
320 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700321 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322 mNewPosition = 0;
323 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700324 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700325 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800326 return NO_ERROR;
327}
328
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329// -------------------------------------------------------------------------
330
331void AudioTrack::start()
332{
333 sp<AudioTrackThread> t = mAudioTrackThread;
334
Steve Block3856b092011-10-20 11:56:00 +0100335 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336
Eric Laurentf5aafb22010-11-18 08:40:16 -0800337 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800338 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
339 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700340 sp<IAudioTrack> audioTrack = mAudioTrack;
341 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800342 audio_track_cblk_t* cblk = mCblk;
343
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800344 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700345 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800346 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800347 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700348 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800349 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
350 cblk->waitTimeMs = 0;
Glenn Kastend12b0332012-11-05 13:38:15 -0800351 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800352 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800353 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800354 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700355 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700356 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700357 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800358 }
359
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700360 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700361 status_t status = NO_ERROR;
Glenn Kastend12b0332012-11-05 13:38:15 -0800362 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800363 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800364 ALOGV("mAudioTrack->start()");
365 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800366 cblk->lock.lock();
367 if (status == DEAD_OBJECT) {
Glenn Kastend12b0332012-11-05 13:38:15 -0800368 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800369 }
Eric Laurent2b584242009-11-09 23:32:22 -0800370 }
Glenn Kastend12b0332012-11-05 13:38:15 -0800371 if (cblk->flags & CBLK_INVALID) {
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700372 audio_track_cblk_t* temp = cblk;
Glenn Kasten020f79f2012-11-07 14:03:00 -0800373 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700374 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800375 }
376 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800377 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100378 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800379 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800380 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800381 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800382 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700383 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700384 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800385 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800386 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 }
388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389}
390
391void AudioTrack::stop()
392{
393 sp<AudioTrackThread> t = mAudioTrackThread;
394
Steve Block3856b092011-10-20 11:56:00 +0100395 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396
Eric Laurentf5aafb22010-11-18 08:40:16 -0800397 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800398 if (mActive) {
399 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700400 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 mAudioTrack->stop();
402 // Cancel loops (If we are in the middle of a loop, playback
403 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800404 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700405 // the playback head position will reset to 0, so if a marker is set, we need
406 // to activate it again
407 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 // Force flush if a shared buffer is used otherwise audioflinger
409 // will not stop before end of buffer is reached.
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800410 // It may be needed to make sure that we stop playback, likely in case looping is on.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800412 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 }
414 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800415 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700417 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700418 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800419 }
420 }
421
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422}
423
424bool AudioTrack::stopped() const
425{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800426 AutoMutex lock(mLock);
427 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428}
429
430void AudioTrack::flush()
431{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800432 AutoMutex lock(mLock);
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800433 if (!mActive && mSharedBuffer == 0) {
434 flush_l();
435 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800436}
437
Eric Laurent1703cdf2011-03-07 14:52:59 -0800438void AudioTrack::flush_l()
439{
Steve Block3856b092011-10-20 11:56:00 +0100440 ALOGV("flush");
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800441 ALOG_ASSERT(!mActive);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700442
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700443 // clear playback marker and periodic update counter
444 mMarkerPosition = 0;
445 mMarkerReached = false;
446 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700447
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800448 mFlushed = true;
449 mAudioTrack->flush();
450 // Release AudioTrack callback thread in case it was waiting for new buffers
451 // in AudioTrack::obtainBuffer()
452 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453}
454
455void AudioTrack::pause()
456{
Steve Block3856b092011-10-20 11:56:00 +0100457 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800458 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800459 if (mActive) {
460 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700461 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462 mAudioTrack->pause();
463 }
464}
465
Eric Laurentbe916aa2010-06-01 23:49:17 -0700466status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467{
Glenn Kasten552f2742012-12-04 12:22:46 -0800468 if (mStatus != NO_ERROR) {
469 return mStatus;
470 }
471 ALOG_ASSERT(mProxy != NULL);
472
Glenn Kastenf0c49502011-11-30 09:46:04 -0800473 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700474 return BAD_VALUE;
475 }
476
Eric Laurent1703cdf2011-03-07 14:52:59 -0800477 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478 mVolume[LEFT] = left;
479 mVolume[RIGHT] = right;
480
Glenn Kasten552f2742012-12-04 12:22:46 -0800481 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700482
483 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484}
485
Glenn Kasten164d6532012-02-27 16:21:04 -0800486status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487{
Glenn Kasten164d6532012-02-27 16:21:04 -0800488 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700489}
490
Eric Laurent2beeb502010-07-16 07:43:46 -0700491status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700492{
Steve Block3856b092011-10-20 11:56:00 +0100493 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten552f2742012-12-04 12:22:46 -0800494
495 if (mStatus != NO_ERROR) {
496 return mStatus;
497 }
498 ALOG_ASSERT(mProxy != NULL);
499
Glenn Kasten05632a52012-01-03 14:22:33 -0800500 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700501 return BAD_VALUE;
502 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800503 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700504
505 mSendLevel = level;
Glenn Kasten552f2742012-12-04 12:22:46 -0800506 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700507
508 return NO_ERROR;
509}
510
Glenn Kastena5224f32012-01-04 12:41:44 -0800511void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700512{
513 if (level != NULL) {
514 *level = mSendLevel;
515 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516}
517
Glenn Kasten1127d652012-11-14 08:44:39 -0800518status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519{
Glenn Kasten1127d652012-11-14 08:44:39 -0800520 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521
John Grossman4ff14ba2012-02-08 16:37:41 -0800522 if (mIsTimed) {
523 return INVALID_OPERATION;
524 }
525
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700527 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 }
529 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastena2049222012-06-22 17:21:07 -0700530 if (rate == 0 || rate > afSamplingRate*2 ) {
531 return BAD_VALUE;
532 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800533
Eric Laurent1703cdf2011-03-07 14:52:59 -0800534 AutoMutex lock(mLock);
Glenn Kasten552f2742012-12-04 12:22:46 -0800535 mSampleRate = rate;
536 mProxy->setSampleRate(rate);
537
Eric Laurent57326622009-07-07 07:10:45 -0700538 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539}
540
Glenn Kastena5224f32012-01-04 12:41:44 -0800541uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542{
John Grossman4ff14ba2012-02-08 16:37:41 -0800543 if (mIsTimed) {
Glenn Kasten1127d652012-11-14 08:44:39 -0800544 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800545 }
546
Eric Laurent1703cdf2011-03-07 14:52:59 -0800547 AutoMutex lock(mLock);
Glenn Kasten552f2742012-12-04 12:22:46 -0800548 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549}
550
551status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
552{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800553 AutoMutex lock(mLock);
554 return setLoop_l(loopStart, loopEnd, loopCount);
555}
556
557// must be called with mLock held
558status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
559{
Glenn Kastena0d77cd2013-08-09 14:32:40 -0700560 // SoundPool streaming implementation uses AudioTrack EVENT_MORE_DATA callback mode,
561 // and relies on being able to loop the data provided by the most recent callback.
562 if (/*mSharedBuffer == 0 ||*/ mIsTimed) {
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800563 return INVALID_OPERATION;
564 }
565
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 audio_track_cblk_t* cblk = mCblk;
567
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 Mutex::Autolock _l(cblk->lock);
569
570 if (loopCount == 0) {
571 cblk->loopStart = UINT_MAX;
572 cblk->loopEnd = UINT_MAX;
573 cblk->loopCount = 0;
574 mLoopCount = 0;
575 return NO_ERROR;
576 }
577
578 if (loopStart >= loopEnd ||
Glenn Kastend7101432012-11-14 13:42:25 -0800579 loopEnd - loopStart > mFrameCount ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700580 cblk->server > loopStart) {
Glenn Kasten8af901c2012-11-01 11:11:38 -0700581 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
Glenn Kastend7101432012-11-14 13:42:25 -0800582 "user %d", loopStart, loopEnd, loopCount, mFrameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583 return BAD_VALUE;
584 }
585
Glenn Kastend7101432012-11-14 13:42:25 -0800586 if ((mSharedBuffer != 0) && (loopEnd > mFrameCount)) {
Glenn Kasten8af901c2012-11-01 11:11:38 -0700587 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
588 "framecount %d",
Glenn Kastend7101432012-11-14 13:42:25 -0800589 loopStart, loopEnd, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700591 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800592
593 cblk->loopStart = loopStart;
594 cblk->loopEnd = loopEnd;
595 cblk->loopCount = loopCount;
596 mLoopCount = loopCount;
597
598 return NO_ERROR;
599}
600
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601status_t AudioTrack::setMarkerPosition(uint32_t marker)
602{
Glenn Kastena2049222012-06-22 17:21:07 -0700603 if (mCbf == NULL) {
604 return INVALID_OPERATION;
605 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606
607 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700608 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609
610 return NO_ERROR;
611}
612
Glenn Kastena5224f32012-01-04 12:41:44 -0800613status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614{
Glenn Kastena2049222012-06-22 17:21:07 -0700615 if (marker == NULL) {
616 return BAD_VALUE;
617 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618
619 *marker = mMarkerPosition;
620
621 return NO_ERROR;
622}
623
624status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
625{
Glenn Kastena2049222012-06-22 17:21:07 -0700626 if (mCbf == NULL) {
627 return INVALID_OPERATION;
628 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629
630 uint32_t curPosition;
631 getPosition(&curPosition);
632 mNewPosition = curPosition + updatePeriod;
633 mUpdatePeriod = updatePeriod;
634
635 return NO_ERROR;
636}
637
Glenn Kastena5224f32012-01-04 12:41:44 -0800638status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639{
Glenn Kastena2049222012-06-22 17:21:07 -0700640 if (updatePeriod == NULL) {
641 return BAD_VALUE;
642 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643
644 *updatePeriod = mUpdatePeriod;
645
646 return NO_ERROR;
647}
648
649status_t AudioTrack::setPosition(uint32_t position)
650{
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800651 if (mSharedBuffer == 0 || mIsTimed) {
Glenn Kastena2049222012-06-22 17:21:07 -0700652 return INVALID_OPERATION;
653 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800654
Eric Laurent1703cdf2011-03-07 14:52:59 -0800655 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656
Glenn Kastena2049222012-06-22 17:21:07 -0700657 if (!stopped_l()) {
658 return INVALID_OPERATION;
659 }
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800660
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700661 audio_track_cblk_t* cblk = mCblk;
662 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663
Glenn Kastena2049222012-06-22 17:21:07 -0700664 if (position > cblk->user) {
665 return BAD_VALUE;
666 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700668 cblk->server = position;
669 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700670
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671 return NO_ERROR;
672}
673
674status_t AudioTrack::getPosition(uint32_t *position)
675{
Glenn Kastena2049222012-06-22 17:21:07 -0700676 if (position == NULL) {
677 return BAD_VALUE;
678 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800679 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700680 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681
682 return NO_ERROR;
683}
684
685status_t AudioTrack::reload()
686{
Glenn Kasten552f2742012-12-04 12:22:46 -0800687 if (mStatus != NO_ERROR) {
688 return mStatus;
689 }
690 ALOG_ASSERT(mProxy != NULL);
691
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800692 if (mSharedBuffer == 0 || mIsTimed) {
693 return INVALID_OPERATION;
694 }
695
Eric Laurent1703cdf2011-03-07 14:52:59 -0800696 AutoMutex lock(mLock);
697
Glenn Kastena2049222012-06-22 17:21:07 -0700698 if (!stopped_l()) {
699 return INVALID_OPERATION;
700 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700701
Eric Laurent1703cdf2011-03-07 14:52:59 -0800702 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703
Glenn Kasten552f2742012-12-04 12:22:46 -0800704 (void) mProxy->stepUser(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705
706 return NO_ERROR;
707}
708
Eric Laurentc2f1f072009-07-17 12:17:14 -0700709audio_io_handle_t AudioTrack::getOutput()
710{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800711 AutoMutex lock(mLock);
712 return getOutput_l();
713}
714
715// must be called with mLock held
716audio_io_handle_t AudioTrack::getOutput_l()
717{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800718 return AudioSystem::getOutput(mStreamType,
Glenn Kasten552f2742012-12-04 12:22:46 -0800719 mSampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700720}
721
Eric Laurentbe916aa2010-06-01 23:49:17 -0700722status_t AudioTrack::attachAuxEffect(int effectId)
723{
Steve Block3856b092011-10-20 11:56:00 +0100724 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700725 status_t status = mAudioTrack->attachAuxEffect(effectId);
726 if (status == NO_ERROR) {
727 mAuxEffectId = effectId;
728 }
729 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700730}
731
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800732// -------------------------------------------------------------------------
733
Eric Laurent1703cdf2011-03-07 14:52:59 -0800734// must be called with mLock held
735status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800736 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800737 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800738 audio_format_t format,
Glenn Kasten7da35f22012-11-14 12:54:39 -0800739 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700740 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800741 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700742 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800743{
744 status_t status;
745 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
746 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700747 ALOGE("Could not get audioflinger");
748 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800749 }
750
Eric Laurentd1b449a2010-05-14 03:26:45 -0700751 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700752 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700753 return NO_INIT;
754 }
755
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700756 // Client decides whether the track is TIMED (see below), but can only express a preference
757 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700758 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700759 // either of these use cases:
760 // use case 1: shared buffer
761 (sharedBuffer != 0) ||
762 // use case 2: callback handler
763 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800764 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700765 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700766 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700767 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700768 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700769 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700770
Eric Laurentd1b449a2010-05-14 03:26:45 -0700771 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700772
Dima Zavinfce7a472011-04-19 22:30:36 -0700773 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700774
Eric Laurentd1b449a2010-05-14 03:26:45 -0700775 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700776 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700777 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700778 } else if (frameCount == 0) {
Glenn Kasten7da35f22012-11-14 12:54:39 -0800779 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700780 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
781 return NO_INIT;
782 }
783 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700784 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700785
786 } else if (sharedBuffer != 0) {
787
Glenn Kastenc45128a2012-11-14 12:47:55 -0800788 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700789 // 8-bit data in shared memory is not currently supported by AudioFlinger
790 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastenc45128a2012-11-14 12:47:55 -0800791 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700792 // More than 2 channels does not require stronger alignment than stereo
793 alignment <<= 1;
794 }
Glenn Kastenc45128a2012-11-14 12:47:55 -0800795 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
796 ALOGE("Invalid buffer alignment: address %p, channel count %u",
797 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700798 return BAD_VALUE;
799 }
800
801 // When initializing a shared buffer AudioTrack via constructors,
802 // there's no frameCount parameter.
803 // But when initializing a shared buffer AudioTrack via set(),
804 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastenc45128a2012-11-14 12:47:55 -0800805 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700806
807 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
808
809 // FIXME move these calculations and associated checks to server
Glenn Kasten1127d652012-11-14 08:44:39 -0800810 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700811 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
812 return NO_INIT;
813 }
Glenn Kasten7da35f22012-11-14 12:54:39 -0800814 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700815 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
816 return NO_INIT;
817 }
818
Eric Laurentd1b449a2010-05-14 03:26:45 -0700819 // Ensure that buffer depth covers at least audio hardware latency
820 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
821 if (minBufCount < 2) minBufCount = 2;
822
Glenn Kasten7da35f22012-11-14 12:54:39 -0800823 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
824 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800825 ", afLatency=%d",
826 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700827
828 if (frameCount == 0) {
829 frameCount = minFrameCount;
830 }
831 if (mNotificationFramesAct == 0) {
832 mNotificationFramesAct = frameCount/2;
833 }
834 // Make sure that application is notified with sufficient margin
835 // before underrun
Glenn Kasten7da35f22012-11-14 12:54:39 -0800836 if (mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700837 mNotificationFramesAct = frameCount/2;
838 }
839 if (frameCount < minFrameCount) {
840 // not ALOGW because it happens all the time when playing key clicks over A2DP
841 ALOGV("Minimum buffer size corrected from %d to %d",
842 frameCount, minFrameCount);
843 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800844 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700845
Glenn Kastene0fa4672012-04-24 14:35:14 -0700846 } else {
847 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700848 }
849
Glenn Kastena075db42012-03-06 11:22:44 -0800850 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
851 if (mIsTimed) {
852 trackFlags |= IAudioFlinger::TRACK_TIMED;
853 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800854
855 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700856 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700857 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800858 if (mAudioTrackThread != 0) {
859 tid = mAudioTrackThread->getTid();
860 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700861 }
862
Glenn Kastenf37971f2012-02-03 11:06:53 -0800863 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800864 sampleRate,
Glenn Kasten520a9af2012-06-21 12:56:37 -0700865 // AudioFlinger only sees 16-bit PCM
866 format == AUDIO_FORMAT_PCM_8_BIT ?
867 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastenc45128a2012-11-14 12:47:55 -0800868 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800869 frameCount,
Glenn Kastenc2674152012-11-06 15:03:34 -0800870 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800871 sharedBuffer,
872 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800873 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700874 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800875 &status);
876
877 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000878 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800879 return status;
880 }
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700881 sp<IMemory> iMem = track->getCblk();
882 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000883 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 return NO_INIT;
885 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800886 mAudioTrack = track;
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700887 mCblkMemory = iMem;
888 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
889 mCblk = cblk;
Glenn Kastend7101432012-11-14 13:42:25 -0800890 size_t temp = cblk->frameCount_;
891 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
892 // In current design, AudioTrack client checks and ensures frame count validity before
893 // passing it to AudioFlinger so AudioFlinger should not return a different value except
894 // for fast track as it uses a special method of assigning frame count.
895 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
896 }
897 frameCount = temp;
Glenn Kastenf8197a62013-04-23 12:39:37 -0700898 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800899 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastenc2674152012-11-06 15:03:34 -0800900 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastend7101432012-11-14 13:42:25 -0800901 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastenf8197a62013-04-23 12:39:37 -0700902 mAwaitBoost = true;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800903 } else {
Glenn Kastend7101432012-11-14 13:42:25 -0800904 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700905 // once denied, do not request again if IAudioTrack is re-created
906 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
907 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800908 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700909 if (sharedBuffer == 0) {
Glenn Kastend7101432012-11-14 13:42:25 -0800910 mNotificationFramesAct = frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700911 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800912 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800913 if (sharedBuffer == 0) {
Glenn Kasten2f6226a2012-11-08 12:13:58 -0800914 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800915 } else {
Glenn Kasten2f6226a2012-11-08 12:13:58 -0800916 mBuffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800917 }
918
Eric Laurent2beeb502010-07-16 07:43:46 -0700919 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700920 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
921 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700922 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700923 // FIXME don't believe this lie
Glenn Kastend7101432012-11-14 13:42:25 -0800924 mLatency = afLatency + (1000*frameCount) / sampleRate;
925 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700926 // If IAudioTrack is re-created, don't let the requested frameCount
927 // decrease. This can confuse clients that cache frameCount().
Glenn Kastend7101432012-11-14 13:42:25 -0800928 if (frameCount > mReqFrameCount) {
929 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700930 }
Glenn Kasten552f2742012-12-04 12:22:46 -0800931
932 // update proxy
933 delete mProxy;
934 mProxy = new AudioTrackClientProxy(cblk, mBuffers, frameCount, mFrameSizeAF);
935 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
936 uint16_t(mVolume[LEFT] * 0x1000));
937 mProxy->setSendLevel(mSendLevel);
938 mProxy->setSampleRate(mSampleRate);
939 if (sharedBuffer != 0) {
940 // Force buffer full condition as data is already present in shared memory
941 mProxy->stepUser(frameCount);
942 }
943
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800944 return NO_ERROR;
945}
946
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
948{
Glenn Kasten552f2742012-12-04 12:22:46 -0800949 ALOG_ASSERT(mStatus == NO_ERROR && mProxy != NULL);
950
Eric Laurent1703cdf2011-03-07 14:52:59 -0800951 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800952 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700953 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800954 audio_track_cblk_t* cblk = mCblk;
955 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700956 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800957
958 audioBuffer->frameCount = 0;
959 audioBuffer->size = 0;
960
Glenn Kasten552f2742012-12-04 12:22:46 -0800961 size_t framesAvail = mProxy->framesAvailable();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962
Eric Laurent9b7d9502011-03-21 11:49:00 -0700963 cblk->lock.lock();
Glenn Kastend12b0332012-11-05 13:38:15 -0800964 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700965 goto create_new_track;
966 }
967 cblk->lock.unlock();
968
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800969 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800970 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800971 goto start_loop_here;
972 while (framesAvail == 0) {
973 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800974 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800976 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977 return NO_MORE_BUFFERS;
978 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800979 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800980 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800982 }
Glenn Kastend12b0332012-11-05 13:38:15 -0800983 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800984 mLock.unlock();
Glenn Kastene0461d12012-11-07 10:13:08 -0800985 // this condition is in shared memory, so if IAudioTrack and control block
986 // are replaced due to mediaserver death or IAudioTrack invalidation then
987 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700988 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700989 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800990 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800991 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800992 return status_t(STOPPED);
993 }
Glenn Kastene0461d12012-11-07 10:13:08 -0800994 // IAudioTrack may have been re-created while mLock was unlocked
995 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800996 cblk->lock.lock();
997 }
998
Glenn Kastend12b0332012-11-05 13:38:15 -0800999 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001000 goto create_new_track;
1001 }
Glenn Kastenf6b16782011-12-15 09:51:17 -08001002 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -07001003 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001004 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
1005 // timing out when a loop has been set and we have already written upto loop end
1006 // is a normal condition: no need to wake AudioFlinger up.
1007 if (cblk->user < cblk->loopEnd) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001008 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
1009 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001010 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001011 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001012 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001014 if (result == DEAD_OBJECT) {
Glenn Kastend12b0332012-11-05 13:38:15 -08001015 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001016create_new_track:
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001017 audio_track_cblk_t* temp = cblk;
Glenn Kasten020f79f2012-11-07 14:03:00 -08001018 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001019 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001020 }
1021 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001022 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001023 cblk->lock.unlock();
1024 return result;
1025 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026 }
1027 cblk->waitTimeMs = 0;
1028 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001029
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001030 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001031 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001032 return TIMED_OUT;
1033 }
1034 }
1035 // read the server count again
1036 start_loop_here:
Glenn Kasten552f2742012-12-04 12:22:46 -08001037 framesAvail = mProxy->framesAvailable_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001039 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001040 }
1041
1042 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001043
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001044 if (framesReq > framesAvail) {
1045 framesReq = framesAvail;
1046 }
1047
1048 uint32_t u = cblk->user;
Glenn Kastend7101432012-11-14 13:42:25 -08001049 uint32_t bufferEnd = cblk->userBase + mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001050
Marco Nelissena1472d92012-03-30 14:36:54 -07001051 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001052 framesReq = bufferEnd - u;
1053 }
1054
Eric Laurentc2f1f072009-07-17 12:17:14 -07001055 audioBuffer->frameCount = framesReq;
Glenn Kasten5aab59a2012-11-12 07:58:20 -08001056 audioBuffer->size = framesReq * mFrameSizeAF;
Glenn Kasten552f2742012-12-04 12:22:46 -08001057 audioBuffer->raw = mProxy->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001058 active = mActive;
1059 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1060}
1061
1062void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1063{
Glenn Kasten552f2742012-12-04 12:22:46 -08001064 ALOG_ASSERT(mStatus == NO_ERROR && mProxy != NULL);
1065
Eric Laurent1703cdf2011-03-07 14:52:59 -08001066 AutoMutex lock(mLock);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001067 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten552f2742012-12-04 12:22:46 -08001068 (void) mProxy->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001069 if (audioBuffer->frameCount > 0) {
1070 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001071 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1072 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1073 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001074 mAudioTrack->start();
1075 }
1076 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001077}
1078
1079// -------------------------------------------------------------------------
1080
1081ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1082{
1083
Glenn Kasten8f7453f2012-11-30 15:00:36 -08001084 if (mSharedBuffer != 0 || mIsTimed) {
Glenn Kastena2049222012-06-22 17:21:07 -07001085 return INVALID_OPERATION;
1086 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001087
1088 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001089 // Sanity-check: user is most-likely passing an error code, and it would
1090 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001091 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001092 buffer, userSize, userSize);
1093 return BAD_VALUE;
1094 }
1095
Steve Block3856b092011-10-20 11:56:00 +01001096 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001097
Eric Laurentdf839842012-05-31 14:27:14 -07001098 if (userSize == 0) {
1099 return 0;
1100 }
1101
Eric Laurent1703cdf2011-03-07 14:52:59 -08001102 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1103 // while we are accessing the cblk
1104 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001105 sp<IAudioTrack> audioTrack = mAudioTrack;
1106 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001107 mLock.unlock();
1108
Glenn Kastene0461d12012-11-07 10:13:08 -08001109 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1110 // so all cblk references might still refer to old shared memory, but that should be benign
1111
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001112 ssize_t written = 0;
1113 const int8_t *src = (const int8_t *)buffer;
1114 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001115 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116
1117 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001118 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001119
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001120 status_t err = obtainBuffer(&audioBuffer, -1);
1121 if (err < 0) {
1122 // out of buffers, return #bytes written
Glenn Kastena2049222012-06-22 17:21:07 -07001123 if (err == status_t(NO_MORE_BUFFERS)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001124 break;
Glenn Kastena2049222012-06-22 17:21:07 -07001125 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001126 return ssize_t(err);
1127 }
1128
1129 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001130
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001131 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001132 // Divide capacity by 2 to take expansion into account
1133 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001134 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001135 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001136 toWrite = audioBuffer.size;
1137 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001138 }
Glenn Kasten9c6c02e2012-11-12 14:32:06 -08001139 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001140 userSize -= toWrite;
1141 written += toWrite;
1142
1143 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001144 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001145
1146 return written;
1147}
1148
1149// -------------------------------------------------------------------------
1150
John Grossman4ff14ba2012-02-08 16:37:41 -08001151TimedAudioTrack::TimedAudioTrack() {
1152 mIsTimed = true;
1153}
1154
1155status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1156{
Glenn Kastena96bd952012-11-02 13:05:14 -07001157 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001158 status_t result = UNKNOWN_ERROR;
1159
Glenn Kastena96bd952012-11-02 13:05:14 -07001160 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1161 // while we are accessing the cblk
1162 sp<IAudioTrack> audioTrack = mAudioTrack;
1163 sp<IMemory> iMem = mCblkMemory;
1164
John Grossman4ff14ba2012-02-08 16:37:41 -08001165 // If the track is not invalid already, try to allocate a buffer. alloc
1166 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kasten2662ac92012-07-30 10:59:30 -07001167 // we can attempt to restore in just a bit.
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001168 audio_track_cblk_t* cblk = mCblk;
1169 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001170 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1171 if (result == DEAD_OBJECT) {
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001172 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001173 }
1174 }
1175
1176 // If the track is invalid at this point, attempt to restore it. and try the
1177 // allocation one more time.
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001178 if (cblk->flags & CBLK_INVALID) {
1179 cblk->lock.lock();
1180 audio_track_cblk_t* temp = cblk;
Glenn Kasten020f79f2012-11-07 14:03:00 -08001181 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001182 cblk = temp;
1183 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001184
Glenn Kastena2049222012-06-22 17:21:07 -07001185 if (result == OK) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001186 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastena2049222012-06-22 17:21:07 -07001187 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001188 }
1189
1190 return result;
1191}
1192
1193status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1194 int64_t pts)
1195{
Eric Laurentdf839842012-05-31 14:27:14 -07001196 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1197 {
1198 AutoMutex lock(mLock);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001199 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001200 // restart track if it was disabled by audioflinger due to previous underrun
1201 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001202 mActive && (cblk->flags & CBLK_DISABLED)) {
1203 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001204 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1205 mAudioTrack->start();
1206 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001207 }
Eric Laurentdf839842012-05-31 14:27:14 -07001208 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001209}
1210
1211status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1212 TargetTimeline target)
1213{
1214 return mAudioTrack->setMediaTimeTransform(xform, target);
1215}
1216
1217// -------------------------------------------------------------------------
1218
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1220{
1221 Buffer audioBuffer;
1222 uint32_t frames;
1223 size_t writtenSize;
1224
Eric Laurent1703cdf2011-03-07 14:52:59 -08001225 mLock.lock();
Glenn Kastenf8197a62013-04-23 12:39:37 -07001226 if (mAwaitBoost) {
1227 mAwaitBoost = false;
1228 mLock.unlock();
1229 static const int32_t kMaxTries = 5;
1230 int32_t tryCounter = kMaxTries;
1231 uint32_t pollUs = 10000;
1232 do {
1233 int policy = sched_getscheduler(0);
1234 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1235 break;
1236 }
1237 usleep(pollUs);
1238 pollUs <<= 1;
1239 } while (tryCounter-- > 0);
1240 if (tryCounter < 0) {
1241 ALOGE("did not receive expected priority boost on time");
1242 }
1243 return true;
1244 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001245 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1246 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001247 sp<IAudioTrack> audioTrack = mAudioTrack;
1248 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001249 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001250 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001251 mLock.unlock();
1252
Glenn Kastene0461d12012-11-07 10:13:08 -08001253 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1254 // so all cblk references might still refer to old shared memory, but that should be benign
1255
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256 // Manage underrun callback
Glenn Kasten552f2742012-12-04 12:22:46 -08001257 if (active && (mProxy->framesAvailable() == mFrameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001258 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kastend12b0332012-11-05 13:38:15 -08001259 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 mCbf(EVENT_UNDERRUN, mUserData, 0);
Glenn Kastend7101432012-11-14 13:42:25 -08001261 if (cblk->server == mFrameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001262 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 }
Glenn Kastena2049222012-06-22 17:21:07 -07001264 if (mSharedBuffer != 0) {
1265 return false;
1266 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267 }
1268 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001269
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001271 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 int loopCount = -1;
1273 mLoopCount--;
1274 if (mLoopCount >= 0) loopCount = mLoopCount;
1275
1276 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1277 }
1278
1279 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001280 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001281 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001282 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001283 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 }
1285 }
1286
1287 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001288 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001289 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1291 mNewPosition += mUpdatePeriod;
1292 }
1293 }
1294
1295 // If Shared buffer is used, no data is requested from client.
1296 if (mSharedBuffer != 0) {
1297 frames = 0;
1298 } else {
1299 frames = mRemainingFrames;
1300 }
1301
Glenn Kasten99e53b82012-01-19 08:59:58 -08001302 // See description of waitCount parameter at declaration of obtainBuffer().
1303 // The logic below prevents us from being stuck below at obtainBuffer()
1304 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001305 int32_t waitCount = -1;
1306 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1307 waitCount = 1;
1308 }
1309
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310 do {
1311
1312 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001313
Eric Laurent2267ba12011-09-07 11:13:23 -07001314 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 if (err < NO_ERROR) {
1316 if (err != TIMED_OUT) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001317 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1318 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 return false;
1320 }
1321 break;
1322 }
Glenn Kastena2049222012-06-22 17:21:07 -07001323 if (err == status_t(STOPPED)) {
1324 return false;
1325 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326
1327 // Divide buffer size by 2 to take into account the expansion
1328 // due to 8 to 16 bit conversion: the callback must fill only half
1329 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001330 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001331 audioBuffer.size >>= 1;
1332 }
1333
1334 size_t reqSize = audioBuffer.size;
1335 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1336 writtenSize = audioBuffer.size;
1337
1338 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001339 if (ssize_t(writtenSize) <= 0) {
1340 // The callback is done filling buffers
1341 // Keep this thread going to handle timed events and
1342 // still try to get more data in intervals of WAIT_PERIOD_MS
1343 // but don't just loop and block the CPU, so wait
1344 usleep(WAIT_PERIOD_MS*1000);
1345 break;
1346 }
Eric Laurentdf839842012-05-31 14:27:14 -07001347
Glenn Kastena2049222012-06-22 17:21:07 -07001348 if (writtenSize > reqSize) {
1349 writtenSize = reqSize;
1350 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001351
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001352 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001353 // 8 to 16 bit conversion, note that source and destination are the same address
1354 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001355 writtenSize <<= 1;
1356 }
1357
1358 audioBuffer.size = writtenSize;
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001359 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1360 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001361 // 16 bit.
Glenn Kasten5aab59a2012-11-12 07:58:20 -08001362 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001363
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364 frames -= audioBuffer.frameCount;
1365
1366 releaseBuffer(&audioBuffer);
1367 }
1368 while (frames);
1369
1370 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001371 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001372 } else {
1373 mRemainingFrames = frames;
1374 }
1375 return true;
1376}
1377
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001378// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001379// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001380// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1381// FIXME Don't depend on caller to hold strong references.
1382status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001383{
1384 status_t result;
1385
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001386 audio_track_cblk_t* cblk = refCblk;
1387 audio_track_cblk_t* newCblk = cblk;
Glenn Kasten411e4472012-11-02 10:00:06 -07001388 ALOGW("dead IAudioTrack, creating a new one from %s",
1389 fromStart ? "start()" : "obtainBuffer()");
Eric Laurent1703cdf2011-03-07 14:52:59 -08001390
Glenn Kastene0461d12012-11-07 10:13:08 -08001391 // signal old cblk condition so that other threads waiting for available buffers stop
1392 // waiting now
1393 cblk->cv.broadcast();
1394 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001395
Glenn Kastene0461d12012-11-07 10:13:08 -08001396 // refresh the audio configuration cache in this process to make sure we get new
1397 // output parameters in getOutput_l() and createTrack_l()
1398 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001399
Glenn Kastene0461d12012-11-07 10:13:08 -08001400 // if the new IAudioTrack is created, createTrack_l() will modify the
1401 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1402 // It will also delete the strong references on previous IAudioTrack and IMemory
1403 result = createTrack_l(mStreamType,
Glenn Kasten552f2742012-12-04 12:22:46 -08001404 mSampleRate,
Glenn Kastene0461d12012-11-07 10:13:08 -08001405 mFormat,
Glenn Kastend7101432012-11-14 13:42:25 -08001406 mReqFrameCount, // so that frame count never goes down
Glenn Kastene0461d12012-11-07 10:13:08 -08001407 mFlags,
1408 mSharedBuffer,
1409 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001410
Glenn Kastene0461d12012-11-07 10:13:08 -08001411 if (result == NO_ERROR) {
1412 uint32_t user = cblk->user;
1413 uint32_t server = cblk->server;
1414 // restore write index and set other indexes to reflect empty buffer status
1415 newCblk = mCblk;
1416 newCblk->user = user;
1417 newCblk->server = user;
1418 newCblk->userBase = user;
1419 newCblk->serverBase = user;
1420 // restore loop: this is not guaranteed to succeed if new frame count is not
1421 // compatible with loop length
1422 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Glenn Kasten552f2742012-12-04 12:22:46 -08001423 size_t frames = 0;
Glenn Kastene0461d12012-11-07 10:13:08 -08001424 if (!fromStart) {
1425 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1426 // Make sure that a client relying on callback events indicating underrun or
1427 // the actual amount of audio frames played (e.g SoundPool) receives them.
1428 if (mSharedBuffer == 0) {
Glenn Kastene0461d12012-11-07 10:13:08 -08001429 if (user > server) {
Glenn Kastend7101432012-11-14 13:42:25 -08001430 frames = ((user - server) > mFrameCount) ?
1431 mFrameCount : (user - server);
Glenn Kasten5aab59a2012-11-12 07:58:20 -08001432 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001433 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001434 // restart playback even if buffer is not completely filled.
1435 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001436 }
1437 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001438 if (mSharedBuffer != 0) {
Glenn Kasten552f2742012-12-04 12:22:46 -08001439 frames = mFrameCount;
1440 }
1441 if (frames > 0) {
1442 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1443 // the client
1444 mProxy->stepUser(frames);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001445 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001446 if (mActive) {
1447 result = mAudioTrack->start();
1448 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1449 }
1450 if (fromStart && result == NO_ERROR) {
1451 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001452 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001453 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001454 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001455 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001456 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001457
1458 if (result == NO_ERROR) {
1459 // from now on we switch to the newly created cblk
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001460 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001461 }
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001462 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001463
Glenn Kasten411e4472012-11-02 10:00:06 -07001464 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001465
1466 return result;
1467}
1468
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001469status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1470{
1471
1472 const size_t SIZE = 256;
1473 char buffer[SIZE];
1474 String8 result;
1475
1476 result.append(" AudioTrack::dump\n");
Glenn Kasten8af901c2012-11-01 11:11:38 -07001477 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1478 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001479 result.append(buffer);
Glenn Kasten8af901c2012-11-01 11:11:38 -07001480 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastend7101432012-11-14 13:42:25 -08001481 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001482 result.append(buffer);
Glenn Kasten552f2742012-12-04 12:22:46 -08001483 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001484 result.append(buffer);
1485 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1486 result.append(buffer);
1487 ::write(fd, result.string(), result.size());
1488 return NO_ERROR;
1489}
1490
1491// =========================================================================
1492
1493AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001494 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1495{
1496}
1497
1498AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001499{
1500}
1501
1502bool AudioTrack::AudioTrackThread::threadLoop()
1503{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001504 {
1505 AutoMutex _l(mMyLock);
1506 if (mPaused) {
1507 mMyCond.wait(mMyLock);
1508 // caller will check for exitPending()
1509 return true;
1510 }
1511 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001512 if (!mReceiver.processAudioBuffer(this)) {
1513 pause();
1514 }
1515 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001516}
1517
Glenn Kasten3acbd052012-02-28 10:39:56 -08001518void AudioTrack::AudioTrackThread::requestExit()
1519{
1520 // must be in this order to avoid a race condition
1521 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001522 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001523}
1524
1525void AudioTrack::AudioTrackThread::pause()
1526{
1527 AutoMutex _l(mMyLock);
1528 mPaused = true;
1529}
1530
1531void AudioTrack::AudioTrackThread::resume()
1532{
1533 AutoMutex _l(mMyLock);
1534 if (mPaused) {
1535 mPaused = false;
1536 mMyCond.signal();
1537 }
1538}
1539
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001540}; // namespace android