blob: a73f035107d29295f2e0d71c014f3ecaebccce94 [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{
57 int afSampleRate;
58 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
59 return NO_INIT;
60 }
61 int afFrameCount;
62 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
63 return NO_INIT;
64 }
65 uint32_t afLatency;
66 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
67 return NO_INIT;
68 }
69
70 // Ensure that buffer depth covers at least audio hardware latency
71 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
72 if (minBufCount < 2) minBufCount = 2;
73
74 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070075 afFrameCount * minBufCount * sampleRate / afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080076 return NO_ERROR;
77}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078
79// ---------------------------------------------------------------------------
80
81AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070082 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080083 mIsTimed(false),
84 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
85 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086{
87}
88
89AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080090 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080092 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070093 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080094 int frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -080095 audio_policy_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096 callback_t cbf,
97 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -070098 int notificationFrames,
99 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700100 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800101 mIsTimed(false),
102 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
103 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700105 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700106 frameCount, flags, cbf, user, notificationFrames,
107 0, false, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108}
109
110AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800111 int streamType,
112 uint32_t sampleRate,
113 int format,
114 int channelMask,
115 int frameCount,
116 uint32_t flags,
117 callback_t cbf,
118 void* user,
119 int notificationFrames,
120 int sessionId)
121 : mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800122 mIsTimed(false),
Andreas Huberc8139852012-01-18 10:51:55 -0800123 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
124{
125 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800126 frameCount, (audio_policy_output_flags_t)flags, cbf, user, notificationFrames,
Andreas Huberc8139852012-01-18 10:51:55 -0800127 0, false, sessionId);
128}
129
130AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800131 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800133 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700134 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135 const sp<IMemory>& sharedBuffer,
Glenn Kasten18868c52012-03-07 09:15:37 -0800136 audio_policy_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137 callback_t cbf,
138 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700139 int notificationFrames,
140 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700141 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800142 mIsTimed(false),
143 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
144 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700146 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700147 0, flags, cbf, user, notificationFrames,
148 sharedBuffer, false, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149}
150
151AudioTrack::~AudioTrack()
152{
Steve Block3856b092011-10-20 11:56:00 +0100153 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154
155 if (mStatus == NO_ERROR) {
156 // Make sure that callback function exits in the case where
157 // it is looping on buffer full condition in obtainBuffer().
158 // Otherwise the callback thread will never exit.
159 stop();
160 if (mAudioTrackThread != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 mAudioTrackThread->requestExitAndWait();
162 mAudioTrackThread.clear();
163 }
164 mAudioTrack.clear();
165 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700166 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167 }
168}
169
170status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800171 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800173 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700174 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175 int frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -0800176 audio_policy_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 callback_t cbf,
178 void* user,
179 int notificationFrames,
180 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700181 bool threadCanCallJava,
182 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183{
184
Steve Block3856b092011-10-20 11:56:00 +0100185 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186
Eric Laurent1703cdf2011-03-07 14:52:59 -0800187 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700188 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000189 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 return INVALID_OPERATION;
191 }
192
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 int afSampleRate;
194 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
195 return NO_INIT;
196 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197 uint32_t afLatency;
198 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
199 return NO_INIT;
200 }
201
202 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700203 if (streamType == AUDIO_STREAM_DEFAULT) {
204 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 }
206 if (sampleRate == 0) {
207 sampleRate = afSampleRate;
208 }
209 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800210 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700213 if (channelMask == 0) {
214 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 }
216
217 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000219 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 return BAD_VALUE;
221 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700222
223 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 if (!audio_is_linear_pcm(format)) {
Glenn Kasten18868c52012-03-07 09:15:37 -0800225 flags = (audio_policy_output_flags_t) (flags | AUDIO_POLICY_OUTPUT_FLAG_DIRECT);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700226 }
227
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700228 if (!audio_is_output_channel(channelMask)) {
Steve Block29357bc2012-01-06 19:20:56 +0000229 ALOGE("Invalid channel mask");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700230 return BAD_VALUE;
231 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700232 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700233
Dima Zavinfce7a472011-04-19 22:30:36 -0700234 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800235 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800236 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800237 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700238
239 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000240 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241 return BAD_VALUE;
242 }
243
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800244 mVolume[LEFT] = 1.0f;
245 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800246 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700247 mFrameCount = frameCount;
248 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700249 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700250 mAuxEffectId = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700251
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800252 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800253 status_t status = createTrack_l(streamType,
254 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800255 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700256 (uint32_t)channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800257 frameCount,
258 flags,
259 sharedBuffer,
260 output,
261 true);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800263 if (status != NO_ERROR) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264 return status;
265 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800266
Glenn Kastena0d68332012-01-27 16:47:15 -0800267 if (cbf != NULL) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 }
270
271 mStatus = NO_ERROR;
272
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800274 mFormat = format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700275 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 mChannelCount = channelCount;
277 mSharedBuffer = sharedBuffer;
278 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800279 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280 mCbf = cbf;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 mLoopCount = 0;
283 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700284 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 mNewPosition = 0;
286 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700287 mFlushed = false;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288 mFlags = flags;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700289 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700290 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 return NO_ERROR;
292}
293
294status_t AudioTrack::initCheck() const
295{
296 return mStatus;
297}
298
299// -------------------------------------------------------------------------
300
301uint32_t AudioTrack::latency() const
302{
303 return mLatency;
304}
305
Glenn Kastenfff6d712012-01-12 16:38:12 -0800306audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307{
308 return mStreamType;
309}
310
Glenn Kastene1c39622012-01-04 09:36:37 -0800311audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312{
313 return mFormat;
314}
315
316int AudioTrack::channelCount() const
317{
318 return mChannelCount;
319}
320
321uint32_t AudioTrack::frameCount() const
322{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700323 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324}
325
Glenn Kastenb9980652012-01-11 09:48:27 -0800326size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327{
Dima Zavinfce7a472011-04-19 22:30:36 -0700328 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700329 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700330 } else {
331 return sizeof(uint8_t);
332 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333}
334
335sp<IMemory>& AudioTrack::sharedBuffer()
336{
337 return mSharedBuffer;
338}
339
340// -------------------------------------------------------------------------
341
342void AudioTrack::start()
343{
344 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700345 status_t status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346
Steve Block3856b092011-10-20 11:56:00 +0100347 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 if (t != 0) {
349 if (t->exitPending()) {
350 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block29357bc2012-01-06 19:20:56 +0000351 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 return;
353 }
354 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700355 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356
Eric Laurentf5aafb22010-11-18 08:40:16 -0800357 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800358 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
359 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700360 sp<IAudioTrack> audioTrack = mAudioTrack;
361 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800362 audio_track_cblk_t* cblk = mCblk;
363
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800364 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700365 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800366 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800367 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700368 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800369 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
370 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700371 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800372 pid_t tid;
Eric Laurent2b584242009-11-09 23:32:22 -0800373 if (t != 0) {
Glenn Kasten480b4682012-02-28 12:30:08 -0800374 t->run("AudioTrack", ANDROID_PRIORITY_AUDIO);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800375 tid = t->getTid(); // pid_t is unknown until run()
376 ALOGV("getTid=%d", tid);
377 if (tid == -1) {
378 tid = 0;
379 }
Eric Laurent2b584242009-11-09 23:32:22 -0800380 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700381 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
382 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
383 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800384 tid = 0; // not gettid()
Eric Laurent2b584242009-11-09 23:32:22 -0800385 }
386
Steve Block3856b092011-10-20 11:56:00 +0100387 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800388 if (!(cblk->flags & CBLK_INVALID_MSK)) {
389 cblk->lock.unlock();
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800390 ALOGV("mAudioTrack->start(tid=%d)", tid);
391 status = mAudioTrack->start(tid);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800392 cblk->lock.lock();
393 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700394 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800395 }
Eric Laurent2b584242009-11-09 23:32:22 -0800396 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800397 if (cblk->flags & CBLK_INVALID_MSK) {
398 status = restoreTrack_l(cblk, true);
399 }
400 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800401 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100402 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800403 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800404 if (t != 0) {
405 t->requestExit();
406 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700407 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
408 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800409 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800410 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 }
412
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413}
414
415void AudioTrack::stop()
416{
417 sp<AudioTrackThread> t = mAudioTrackThread;
418
Steve Block3856b092011-10-20 11:56:00 +0100419 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800420
Eric Laurentf5aafb22010-11-18 08:40:16 -0800421 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800422 if (mActive) {
423 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700424 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425 mAudioTrack->stop();
426 // Cancel loops (If we are in the middle of a loop, playback
427 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800428 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700429 // the playback head position will reset to 0, so if a marker is set, we need
430 // to activate it again
431 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 // Force flush if a shared buffer is used otherwise audioflinger
433 // will not stop before end of buffer is reached.
434 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800435 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 }
437 if (t != 0) {
438 t->requestExit();
439 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700440 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
441 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 }
443 }
444
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445}
446
447bool AudioTrack::stopped() const
448{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800449 AutoMutex lock(mLock);
450 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451}
452
453void AudioTrack::flush()
454{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800455 AutoMutex lock(mLock);
456 flush_l();
457}
458
459// must be called with mLock held
460void AudioTrack::flush_l()
461{
Steve Block3856b092011-10-20 11:56:00 +0100462 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700463
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700464 // clear playback marker and periodic update counter
465 mMarkerPosition = 0;
466 mMarkerReached = false;
467 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700468
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700470 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 mAudioTrack->flush();
472 // Release AudioTrack callback thread in case it was waiting for new buffers
473 // in AudioTrack::obtainBuffer()
474 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475 }
476}
477
478void AudioTrack::pause()
479{
Steve Block3856b092011-10-20 11:56:00 +0100480 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800481 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800482 if (mActive) {
483 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 mAudioTrack->pause();
485 }
486}
487
488void AudioTrack::mute(bool e)
489{
490 mAudioTrack->mute(e);
491 mMuted = e;
492}
493
494bool AudioTrack::muted() const
495{
496 return mMuted;
497}
498
Eric Laurentbe916aa2010-06-01 23:49:17 -0700499status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800501 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700502 return BAD_VALUE;
503 }
504
Eric Laurent1703cdf2011-03-07 14:52:59 -0800505 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 mVolume[LEFT] = left;
507 mVolume[RIGHT] = right;
508
Glenn Kasten83d86532012-01-17 14:39:34 -0800509 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700510
511 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512}
513
Glenn Kastena5224f32012-01-04 12:41:44 -0800514void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700516 if (left != NULL) {
517 *left = mVolume[LEFT];
518 }
519 if (right != NULL) {
520 *right = mVolume[RIGHT];
521 }
522}
523
Eric Laurent2beeb502010-07-16 07:43:46 -0700524status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700525{
Steve Block3856b092011-10-20 11:56:00 +0100526 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800527 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700528 return BAD_VALUE;
529 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800530 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700531
532 mSendLevel = level;
533
Glenn Kasten05632a52012-01-03 14:22:33 -0800534 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700535
536 return NO_ERROR;
537}
538
Glenn Kastena5224f32012-01-04 12:41:44 -0800539void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700540{
541 if (level != NULL) {
542 *level = mSendLevel;
543 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544}
545
Eric Laurent57326622009-07-07 07:10:45 -0700546status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547{
548 int afSamplingRate;
549
John Grossman4ff14ba2012-02-08 16:37:41 -0800550 if (mIsTimed) {
551 return INVALID_OPERATION;
552 }
553
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700555 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 }
557 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700558 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559
Eric Laurent1703cdf2011-03-07 14:52:59 -0800560 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700561 mCblk->sampleRate = rate;
562 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563}
564
Glenn Kastena5224f32012-01-04 12:41:44 -0800565uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566{
John Grossman4ff14ba2012-02-08 16:37:41 -0800567 if (mIsTimed) {
568 return INVALID_OPERATION;
569 }
570
Eric Laurent1703cdf2011-03-07 14:52:59 -0800571 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700572 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573}
574
575status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
576{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800577 AutoMutex lock(mLock);
578 return setLoop_l(loopStart, loopEnd, loopCount);
579}
580
581// must be called with mLock held
582status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
583{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 audio_track_cblk_t* cblk = mCblk;
585
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 Mutex::Autolock _l(cblk->lock);
587
588 if (loopCount == 0) {
589 cblk->loopStart = UINT_MAX;
590 cblk->loopEnd = UINT_MAX;
591 cblk->loopCount = 0;
592 mLoopCount = 0;
593 return NO_ERROR;
594 }
595
John Grossman4ff14ba2012-02-08 16:37:41 -0800596 if (mIsTimed) {
597 return INVALID_OPERATION;
598 }
599
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700601 loopEnd - loopStart > cblk->frameCount ||
602 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000603 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 return BAD_VALUE;
605 }
606
Eric Laurent9b7d9502011-03-21 11:49:00 -0700607 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000608 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700609 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700611 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612
613 cblk->loopStart = loopStart;
614 cblk->loopEnd = loopEnd;
615 cblk->loopCount = loopCount;
616 mLoopCount = loopCount;
617
618 return NO_ERROR;
619}
620
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621status_t AudioTrack::setMarkerPosition(uint32_t marker)
622{
Glenn Kastena0d68332012-01-27 16:47:15 -0800623 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624
625 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700626 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627
628 return NO_ERROR;
629}
630
Glenn Kastena5224f32012-01-04 12:41:44 -0800631status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632{
Glenn Kastena0d68332012-01-27 16:47:15 -0800633 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634
635 *marker = mMarkerPosition;
636
637 return NO_ERROR;
638}
639
640status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
641{
Glenn Kastena0d68332012-01-27 16:47:15 -0800642 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643
644 uint32_t curPosition;
645 getPosition(&curPosition);
646 mNewPosition = curPosition + updatePeriod;
647 mUpdatePeriod = updatePeriod;
648
649 return NO_ERROR;
650}
651
Glenn Kastena5224f32012-01-04 12:41:44 -0800652status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653{
Glenn Kastena0d68332012-01-27 16:47:15 -0800654 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655
656 *updatePeriod = mUpdatePeriod;
657
658 return NO_ERROR;
659}
660
661status_t AudioTrack::setPosition(uint32_t position)
662{
John Grossman4ff14ba2012-02-08 16:37:41 -0800663 if (mIsTimed) return INVALID_OPERATION;
664
Eric Laurent1703cdf2011-03-07 14:52:59 -0800665 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800667 if (!stopped_l()) return INVALID_OPERATION;
668
669 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670
671 if (position > mCblk->user) return BAD_VALUE;
672
673 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700674 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700675
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 return NO_ERROR;
677}
678
679status_t AudioTrack::getPosition(uint32_t *position)
680{
Glenn Kastena0d68332012-01-27 16:47:15 -0800681 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800682 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700683 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684
685 return NO_ERROR;
686}
687
688status_t AudioTrack::reload()
689{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800690 AutoMutex lock(mLock);
691
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800692 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700693
Eric Laurent1703cdf2011-03-07 14:52:59 -0800694 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695
Eric Laurentd1b449a2010-05-14 03:26:45 -0700696 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697
698 return NO_ERROR;
699}
700
Eric Laurentc2f1f072009-07-17 12:17:14 -0700701audio_io_handle_t AudioTrack::getOutput()
702{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800703 AutoMutex lock(mLock);
704 return getOutput_l();
705}
706
707// must be called with mLock held
708audio_io_handle_t AudioTrack::getOutput_l()
709{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800710 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800711 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700712}
713
Glenn Kastena5224f32012-01-04 12:41:44 -0800714int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700715{
716 return mSessionId;
717}
718
719status_t AudioTrack::attachAuxEffect(int effectId)
720{
Steve Block3856b092011-10-20 11:56:00 +0100721 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700722 status_t status = mAudioTrack->attachAuxEffect(effectId);
723 if (status == NO_ERROR) {
724 mAuxEffectId = effectId;
725 }
726 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700727}
728
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729// -------------------------------------------------------------------------
730
Eric Laurent1703cdf2011-03-07 14:52:59 -0800731// must be called with mLock held
732status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800733 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800734 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800735 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700736 uint32_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800737 int frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -0800738 audio_policy_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800739 const sp<IMemory>& sharedBuffer,
Eric Laurentd1b449a2010-05-14 03:26:45 -0700740 audio_io_handle_t output,
741 bool enforceFrameCount)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800742{
743 status_t status;
744 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
745 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700746 ALOGE("Could not get audioflinger");
747 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800748 }
749
Eric Laurentd1b449a2010-05-14 03:26:45 -0700750 int afSampleRate;
751 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
752 return NO_INIT;
753 }
754 int afFrameCount;
755 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
756 return NO_INIT;
757 }
758 uint32_t afLatency;
759 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
760 return NO_INIT;
761 }
762
763 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavinfce7a472011-04-19 22:30:36 -0700764 if (!audio_is_linear_pcm(format)) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700765 if (sharedBuffer != 0) {
766 frameCount = sharedBuffer->size();
767 }
768 } else {
769 // Ensure that buffer depth covers at least audio hardware latency
770 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
771 if (minBufCount < 2) minBufCount = 2;
772
773 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
774
775 if (sharedBuffer == 0) {
776 if (frameCount == 0) {
777 frameCount = minFrameCount;
778 }
779 if (mNotificationFramesAct == 0) {
780 mNotificationFramesAct = frameCount/2;
781 }
782 // Make sure that application is notified with sufficient margin
783 // before underrun
784 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
785 mNotificationFramesAct = frameCount/2;
786 }
787 if (frameCount < minFrameCount) {
Eric Laurentd8d61852012-03-05 17:06:40 -0800788 ALOGW_IF(enforceFrameCount, "Minimum buffer size corrected from %d to %d",
789 frameCount, minFrameCount);
790 frameCount = minFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700791 }
792 } else {
Glenn Kasten99e53b82012-01-19 08:59:58 -0800793 // Ensure that buffer alignment matches channelCount
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700794 int channelCount = popcount(channelMask);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700795 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000796 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700797 return BAD_VALUE;
798 }
799 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
800 }
801 }
802
Glenn Kastena075db42012-03-06 11:22:44 -0800803 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
804 if (mIsTimed) {
805 trackFlags |= IAudioFlinger::TRACK_TIMED;
806 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800807 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
808 streamType,
809 sampleRate,
810 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700811 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800812 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800813 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800814 sharedBuffer,
815 output,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700816 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800817 &status);
818
819 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000820 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800821 return status;
822 }
823 sp<IMemory> cblk = track->getCblk();
824 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000825 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800826 return NO_INIT;
827 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800828 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800829 mCblkMemory = cblk;
830 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurent38ccae22011-03-28 18:37:07 -0700831 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800832 if (sharedBuffer == 0) {
833 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
834 } else {
835 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700836 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700837 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800838 }
839
Glenn Kasten83d86532012-01-17 14:39:34 -0800840 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800841 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700842 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800843 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
844 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700845 mRemainingFrames = mNotificationFramesAct;
846 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800847 return NO_ERROR;
848}
849
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800850status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
851{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800852 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800853 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700854 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 audio_track_cblk_t* cblk = mCblk;
856 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700857 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800858
859 audioBuffer->frameCount = 0;
860 audioBuffer->size = 0;
861
862 uint32_t framesAvail = cblk->framesAvailable();
863
Eric Laurent9b7d9502011-03-21 11:49:00 -0700864 cblk->lock.lock();
865 if (cblk->flags & CBLK_INVALID_MSK) {
866 goto create_new_track;
867 }
868 cblk->lock.unlock();
869
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800871 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872 goto start_loop_here;
873 while (framesAvail == 0) {
874 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800875 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100876 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800877 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800878 return NO_MORE_BUFFERS;
879 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800880 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800881 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800882 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800883 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700884 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800885 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700886 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700887 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800888 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800889 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800890 return status_t(STOPPED);
891 }
892 cblk->lock.lock();
893 }
894
895 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700896 goto create_new_track;
897 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800898 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700899 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800900 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
901 // timing out when a loop has been set and we have already written upto loop end
902 // is a normal condition: no need to wake AudioFlinger up.
903 if (cblk->user < cblk->loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000904 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800905 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700906 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800907 cblk->lock.unlock();
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800908 result = mAudioTrack->start(0); // callback thread hasn't changed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800910 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700911 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800912create_new_track:
913 result = restoreTrack_l(cblk, false);
914 }
915 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000916 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800917 cblk->lock.unlock();
918 return result;
919 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800920 }
921 cblk->waitTimeMs = 0;
922 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700923
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800924 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800925 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800926 return TIMED_OUT;
927 }
928 }
929 // read the server count again
930 start_loop_here:
931 framesAvail = cblk->framesAvailable_l();
932 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800933 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934 }
935
Eric Laurent44d98482010-09-30 16:12:31 -0700936 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent1703cdf2011-03-07 14:52:59 -0800937 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700938 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +0000939 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800940 mAudioTrack->start(0); // callback thread hasn't changed
Eric Laurent44d98482010-09-30 16:12:31 -0700941 }
942
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 if (framesReq > framesAvail) {
946 framesReq = framesAvail;
947 }
948
949 uint32_t u = cblk->user;
950 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
951
952 if (u + framesReq > bufferEnd) {
953 framesReq = bufferEnd - u;
954 }
955
Eric Laurentc2f1f072009-07-17 12:17:14 -0700956 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
957 audioBuffer->channelCount = mChannelCount;
958 audioBuffer->frameCount = framesReq;
959 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -0700960 if (audio_is_linear_pcm(mFormat)) {
961 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962 } else {
963 audioBuffer->format = mFormat;
964 }
965 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800966 active = mActive;
967 return active ? status_t(NO_ERROR) : status_t(STOPPED);
968}
969
970void AudioTrack::releaseBuffer(Buffer* audioBuffer)
971{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800972 AutoMutex lock(mLock);
973 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974}
975
976// -------------------------------------------------------------------------
977
978ssize_t AudioTrack::write(const void* buffer, size_t userSize)
979{
980
981 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -0800982 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800983
984 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -0800985 // Sanity-check: user is most-likely passing an error code, and it would
986 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +0000987 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 buffer, userSize, userSize);
989 return BAD_VALUE;
990 }
991
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800993
Eric Laurent1703cdf2011-03-07 14:52:59 -0800994 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
995 // while we are accessing the cblk
996 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700997 sp<IAudioTrack> audioTrack = mAudioTrack;
998 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800999 mLock.unlock();
1000
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 ssize_t written = 0;
1002 const int8_t *src = (const int8_t *)buffer;
1003 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001004 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001005
1006 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001007 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001008
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001009 status_t err = obtainBuffer(&audioBuffer, -1);
1010 if (err < 0) {
1011 // out of buffers, return #bytes written
1012 if (err == status_t(NO_MORE_BUFFERS))
1013 break;
1014 return ssize_t(err);
1015 }
1016
1017 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001018
Dima Zavinfce7a472011-04-19 22:30:36 -07001019 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001020 // Divide capacity by 2 to take expansion into account
1021 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001022 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001023 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024 toWrite = audioBuffer.size;
1025 memcpy(audioBuffer.i8, src, toWrite);
1026 src += toWrite;
1027 }
1028 userSize -= toWrite;
1029 written += toWrite;
1030
1031 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001032 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033
1034 return written;
1035}
1036
1037// -------------------------------------------------------------------------
1038
John Grossman4ff14ba2012-02-08 16:37:41 -08001039TimedAudioTrack::TimedAudioTrack() {
1040 mIsTimed = true;
1041}
1042
1043status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1044{
1045 status_t result = UNKNOWN_ERROR;
1046
1047 // If the track is not invalid already, try to allocate a buffer. alloc
1048 // fails indicating that the server is dead, flag the track as invalid so
1049 // we can attempt to restore in in just a bit.
1050 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1051 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1052 if (result == DEAD_OBJECT) {
1053 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1054 }
1055 }
1056
1057 // If the track is invalid at this point, attempt to restore it. and try the
1058 // allocation one more time.
1059 if (mCblk->flags & CBLK_INVALID_MSK) {
1060 mCblk->lock.lock();
1061 result = restoreTrack_l(mCblk, false);
1062 mCblk->lock.unlock();
1063
1064 if (result == OK)
1065 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1066 }
1067
1068 return result;
1069}
1070
1071status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1072 int64_t pts)
1073{
1074 // restart track if it was disabled by audioflinger due to previous underrun
1075 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1076 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1077 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1078 mAudioTrack->start(0);
1079 }
1080
1081 return mAudioTrack->queueTimedBuffer(buffer, pts);
1082}
1083
1084status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1085 TargetTimeline target)
1086{
1087 return mAudioTrack->setMediaTimeTransform(xform, target);
1088}
1089
1090// -------------------------------------------------------------------------
1091
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001092bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1093{
1094 Buffer audioBuffer;
1095 uint32_t frames;
1096 size_t writtenSize;
1097
Eric Laurent1703cdf2011-03-07 14:52:59 -08001098 mLock.lock();
1099 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1100 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001101 sp<IAudioTrack> audioTrack = mAudioTrack;
1102 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001103 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001104 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001105 mLock.unlock();
1106
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001107 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001108 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001109 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001110 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001111 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001112 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001113 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001114 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115 if (mSharedBuffer != 0) return false;
1116 }
1117 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001118
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001119 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001120 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001121 int loopCount = -1;
1122 mLoopCount--;
1123 if (mLoopCount >= 0) loopCount = mLoopCount;
1124
1125 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1126 }
1127
1128 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001129 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001130 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001131 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001132 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001133 }
1134 }
1135
1136 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001137 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001138 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001139 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1140 mNewPosition += mUpdatePeriod;
1141 }
1142 }
1143
1144 // If Shared buffer is used, no data is requested from client.
1145 if (mSharedBuffer != 0) {
1146 frames = 0;
1147 } else {
1148 frames = mRemainingFrames;
1149 }
1150
Glenn Kasten99e53b82012-01-19 08:59:58 -08001151 // See description of waitCount parameter at declaration of obtainBuffer().
1152 // The logic below prevents us from being stuck below at obtainBuffer()
1153 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001154 int32_t waitCount = -1;
1155 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1156 waitCount = 1;
1157 }
1158
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001159 do {
1160
1161 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001162
Eric Laurent2267ba12011-09-07 11:13:23 -07001163 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001164 if (err < NO_ERROR) {
1165 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001166 ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 return false;
1168 }
1169 break;
1170 }
1171 if (err == status_t(STOPPED)) return false;
1172
1173 // Divide buffer size by 2 to take into account the expansion
1174 // due to 8 to 16 bit conversion: the callback must fill only half
1175 // of the destination buffer
Dima Zavinfce7a472011-04-19 22:30:36 -07001176 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001177 audioBuffer.size >>= 1;
1178 }
1179
1180 size_t reqSize = audioBuffer.size;
1181 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1182 writtenSize = audioBuffer.size;
1183
1184 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001185 if (ssize_t(writtenSize) <= 0) {
1186 // The callback is done filling buffers
1187 // Keep this thread going to handle timed events and
1188 // still try to get more data in intervals of WAIT_PERIOD_MS
1189 // but don't just loop and block the CPU, so wait
1190 usleep(WAIT_PERIOD_MS*1000);
1191 break;
1192 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001193 if (writtenSize > reqSize) writtenSize = reqSize;
1194
Dima Zavinfce7a472011-04-19 22:30:36 -07001195 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001196 // 8 to 16 bit conversion, note that source and destination are the same address
1197 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001198 writtenSize <<= 1;
1199 }
1200
1201 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001202 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001203 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001204 // 16 bit.
1205 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1206
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 frames -= audioBuffer.frameCount;
1208
1209 releaseBuffer(&audioBuffer);
1210 }
1211 while (frames);
1212
1213 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001214 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 } else {
1216 mRemainingFrames = frames;
1217 }
1218 return true;
1219}
1220
Eric Laurent1703cdf2011-03-07 14:52:59 -08001221// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1222// the IAudioTrack and IMemory in case they are recreated here.
1223// If the IAudioTrack is successfully restored, the cblk pointer is updated
1224status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1225{
1226 status_t result;
1227
Eric Laurent38ccae22011-03-28 18:37:07 -07001228 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001229 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001230 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001231
Eric Laurent1703cdf2011-03-07 14:52:59 -08001232 // signal old cblk condition so that other threads waiting for available buffers stop
1233 // waiting now
1234 cblk->cv.broadcast();
1235 cblk->lock.unlock();
1236
Eric Laurent9f6530f2011-08-30 10:18:54 -07001237 // refresh the audio configuration cache in this process to make sure we get new
1238 // output parameters in getOutput_l() and createTrack_l()
1239 AudioSystem::clearAudioConfigCache();
1240
Eric Laurent1703cdf2011-03-07 14:52:59 -08001241 // if the new IAudioTrack is created, createTrack_l() will modify the
1242 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1243 // It will also delete the strong references on previous IAudioTrack and IMemory
1244 result = createTrack_l(mStreamType,
1245 cblk->sampleRate,
1246 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001247 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001248 mFrameCount,
1249 mFlags,
1250 mSharedBuffer,
1251 getOutput_l(),
1252 false);
1253
1254 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001255 uint32_t user = cblk->user;
1256 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001257 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001258 mCblk->user = user;
1259 mCblk->server = user;
1260 mCblk->userBase = user;
1261 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001262 // restore loop: this is not guaranteed to succeed if new frame count is not
1263 // compatible with loop length
1264 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001265 if (!fromStart) {
1266 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001267 // Make sure that a client relying on callback events indicating underrun or
1268 // the actual amount of audio frames played (e.g SoundPool) receives them.
1269 if (mSharedBuffer == 0) {
1270 uint32_t frames = 0;
1271 if (user > server) {
1272 frames = ((user - server) > mCblk->frameCount) ?
1273 mCblk->frameCount : (user - server);
1274 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1275 }
1276 // restart playback even if buffer is not completely filled.
1277 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1278 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1279 // the client
1280 mCblk->stepUser(frames);
1281 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001282 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001283 if (mActive) {
Glenn Kasten6dbc1352012-02-02 10:56:47 -08001284 result = mAudioTrack->start(0); // callback thread hasn't changed
Steve Block5ff1dd52012-01-05 23:22:43 +00001285 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001286 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001287 if (fromStart && result == NO_ERROR) {
1288 mNewPosition = mCblk->server + mUpdatePeriod;
1289 }
1290 }
1291 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001292 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001293 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001294 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001295 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001296 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001297 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001298 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001299 } else {
1300 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001301 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001302 mLock.unlock();
1303 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001304 if (result == NO_ERROR) {
1305 result = mRestoreStatus;
1306 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001307 cblk->lock.unlock();
1308 mLock.lock();
1309 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001310 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001311 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001312 cblk->lock.unlock();
1313 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001314 }
Steve Block3856b092011-10-20 11:56:00 +01001315 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001316 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001317
1318 if (result == NO_ERROR) {
1319 // from now on we switch to the newly created cblk
1320 cblk = mCblk;
1321 }
1322 cblk->lock.lock();
1323
Steve Block5ff1dd52012-01-05 23:22:43 +00001324 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001325
1326 return result;
1327}
1328
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001329status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1330{
1331
1332 const size_t SIZE = 256;
1333 char buffer[SIZE];
1334 String8 result;
1335
1336 result.append(" AudioTrack::dump\n");
1337 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1338 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001339 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001340 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001341 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001342 result.append(buffer);
1343 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1344 result.append(buffer);
1345 ::write(fd, result.string(), result.size());
1346 return NO_ERROR;
1347}
1348
1349// =========================================================================
1350
1351AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1352 : Thread(bCanCallJava), mReceiver(receiver)
1353{
1354}
1355
1356bool AudioTrack::AudioTrackThread::threadLoop()
1357{
1358 return mReceiver.processAudioBuffer(this);
1359}
1360
1361status_t AudioTrack::AudioTrackThread::readyToRun()
1362{
1363 return NO_ERROR;
1364}
1365
1366void AudioTrack::AudioTrackThread::onFirstRef()
1367{
1368}
1369
1370// =========================================================================
1371
Eric Laurent38ccae22011-03-28 18:37:07 -07001372
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001373audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001374 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001375 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001376 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001377 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001378{
1379}
1380
1381uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1382{
Glenn Kasten90548972011-12-13 11:45:07 -08001383 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001384
1385 u += frameCount;
1386 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001387 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001388 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1389 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1390 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1391 }
Glenn Kasten90548972011-12-13 11:45:07 -08001392 } else if (u > server) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001393 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001394 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395 }
1396
1397 if (u >= userBase + this->frameCount) {
1398 userBase += this->frameCount;
1399 }
1400
Glenn Kasten90548972011-12-13 11:45:07 -08001401 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001402
1403 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001404 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001405 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001406 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001407
1408 return u;
1409}
1410
1411bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1412{
Eric Laurent38ccae22011-03-28 18:37:07 -07001413 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001414 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001415 return false;
1416 }
1417
Glenn Kasten90548972011-12-13 11:45:07 -08001418 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419
1420 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001421 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001422 // Mark that we have read the first buffer so that next time stepUser() is called
1423 // we switch to normal obtainBuffer() timeout period
1424 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001425 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001426 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427 // It is possible that we receive a flush()
1428 // while the mixer is processing a block: in this case,
1429 // stepServer() is called After the flush() has reset u & s and
1430 // we have s > u
Glenn Kasten90548972011-12-13 11:45:07 -08001431 if (s > user) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001432 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001433 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001434 }
1435 }
1436
1437 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001438 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001439 s = loopStart;
1440 if (--loopCount == 0) {
1441 loopEnd = UINT_MAX;
1442 loopStart = UINT_MAX;
1443 }
1444 }
1445 if (s >= serverBase + this->frameCount) {
1446 serverBase += this->frameCount;
1447 }
1448
Glenn Kasten90548972011-12-13 11:45:07 -08001449 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001450
Eric Laurent1703cdf2011-03-07 14:52:59 -08001451 if (!(flags & CBLK_INVALID_MSK)) {
1452 cv.signal();
1453 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 lock.unlock();
1455 return true;
1456}
1457
1458void* audio_track_cblk_t::buffer(uint32_t offset) const
1459{
Glenn Kasten90548972011-12-13 11:45:07 -08001460 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461}
1462
1463uint32_t audio_track_cblk_t::framesAvailable()
1464{
1465 Mutex::Autolock _l(lock);
1466 return framesAvailable_l();
1467}
1468
1469uint32_t audio_track_cblk_t::framesAvailable_l()
1470{
Glenn Kasten90548972011-12-13 11:45:07 -08001471 uint32_t u = user;
1472 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473
Eric Laurentd1b449a2010-05-14 03:26:45 -07001474 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001475 uint32_t limit = (s < loopStart) ? s : loopStart;
1476 return limit + frameCount - u;
1477 } else {
1478 return frameCount + u - s;
1479 }
1480}
1481
1482uint32_t audio_track_cblk_t::framesReady()
1483{
Glenn Kasten90548972011-12-13 11:45:07 -08001484 uint32_t u = user;
1485 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486
Eric Laurentd1b449a2010-05-14 03:26:45 -07001487 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001488 if (u < loopEnd) {
1489 return u - s;
1490 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001491 // do not block on mutex shared with client on AudioFlinger side
1492 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001493 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001494 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001495 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001496 uint32_t frames = UINT_MAX;
1497 if (loopCount >= 0) {
1498 frames = (loopEnd - loopStart)*loopCount + u - s;
1499 }
1500 lock.unlock();
1501 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502 }
1503 } else {
1504 return s - u;
1505 }
1506}
1507
Eric Laurent38ccae22011-03-28 18:37:07 -07001508bool audio_track_cblk_t::tryLock()
1509{
1510 // the code below simulates lock-with-timeout
1511 // we MUST do this to protect the AudioFlinger server
1512 // as this lock is shared with the client.
1513 status_t err;
1514
1515 err = lock.tryLock();
1516 if (err == -EBUSY) { // just wait a bit
1517 usleep(1000);
1518 err = lock.tryLock();
1519 }
1520 if (err != NO_ERROR) {
1521 // probably, the client just died.
1522 return false;
1523 }
1524 return true;
1525}
1526
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001527// -------------------------------------------------------------------------
1528
1529}; // namespace android