blob: d73eabdab7b3f447224cbe2257b9826de990186c [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,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800107 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108}
109
Glenn Kasten17a736c2012-02-14 08:52:15 -0800110// DEPRECATED
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800112 int streamType,
113 uint32_t sampleRate,
114 int format,
115 int channelMask,
116 int frameCount,
117 uint32_t flags,
118 callback_t cbf,
119 void* user,
120 int notificationFrames,
121 int sessionId)
122 : mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800123 mIsTimed(false),
Andreas Huberc8139852012-01-18 10:51:55 -0800124 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
125{
126 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800127 frameCount, (audio_policy_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800128 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800129}
130
131AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800132 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800133 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800134 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700135 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 const sp<IMemory>& sharedBuffer,
Glenn Kasten18868c52012-03-07 09:15:37 -0800137 audio_policy_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 callback_t cbf,
139 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700140 int notificationFrames,
141 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700142 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800143 mIsTimed(false),
144 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
145 mPreviousSchedulingGroup(ANDROID_TGROUP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700147 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800148 0 /*frameCount*/, flags, cbf, user, notificationFrames,
149 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150}
151
152AudioTrack::~AudioTrack()
153{
Steve Block3856b092011-10-20 11:56:00 +0100154 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155
156 if (mStatus == NO_ERROR) {
157 // Make sure that callback function exits in the case where
158 // it is looping on buffer full condition in obtainBuffer().
159 // Otherwise the callback thread will never exit.
160 stop();
161 if (mAudioTrackThread != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162 mAudioTrackThread->requestExitAndWait();
163 mAudioTrackThread.clear();
164 }
165 mAudioTrack.clear();
166 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700167 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 }
169}
170
171status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800172 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800174 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700175 int channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 int frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -0800177 audio_policy_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 callback_t cbf,
179 void* user,
180 int notificationFrames,
181 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700182 bool threadCanCallJava,
183 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184{
185
Steve Block3856b092011-10-20 11:56:00 +0100186 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187
Eric Laurent1703cdf2011-03-07 14:52:59 -0800188 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700189 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000190 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191 return INVALID_OPERATION;
192 }
193
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 int afSampleRate;
195 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
196 return NO_INIT;
197 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700198
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199 uint32_t afLatency;
200 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
201 return NO_INIT;
202 }
203
204 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700205 if (streamType == AUDIO_STREAM_DEFAULT) {
206 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700208
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 if (sampleRate == 0) {
210 sampleRate = afSampleRate;
211 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700212
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800214 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700215 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700217 if (channelMask == 0) {
218 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 }
220
221 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700222 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000223 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 return BAD_VALUE;
225 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700226
227 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700228 if (!audio_is_linear_pcm(format)) {
Glenn Kasten18868c52012-03-07 09:15:37 -0800229 flags = (audio_policy_output_flags_t) (flags | AUDIO_POLICY_OUTPUT_FLAG_DIRECT);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700230 }
231
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700232 if (!audio_is_output_channel(channelMask)) {
Steve Block29357bc2012-01-06 19:20:56 +0000233 ALOGE("Invalid channel mask");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234 return BAD_VALUE;
235 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700236 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700237
Dima Zavinfce7a472011-04-19 22:30:36 -0700238 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800239 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800240 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800241 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700242
243 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000244 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 return BAD_VALUE;
246 }
247
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800248 mVolume[LEFT] = 1.0f;
249 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800250 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700251 mFrameCount = frameCount;
252 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700253 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700254 mAuxEffectId = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700255
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800256 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800257 status_t status = createTrack_l(streamType,
258 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800259 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700260 (uint32_t)channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800261 frameCount,
262 flags,
263 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700264 output);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800266 if (status != NO_ERROR) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267 return status;
268 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800269
Glenn Kastena0d68332012-01-27 16:47:15 -0800270 if (cbf != NULL) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 }
273
274 mStatus = NO_ERROR;
275
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800277 mFormat = format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700278 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 mChannelCount = channelCount;
280 mSharedBuffer = sharedBuffer;
281 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800282 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283 mCbf = cbf;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 mLoopCount = 0;
286 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700287 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 mNewPosition = 0;
289 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700290 mFlushed = false;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291 mFlags = flags;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700292 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700293 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294 return NO_ERROR;
295}
296
297status_t AudioTrack::initCheck() const
298{
299 return mStatus;
300}
301
302// -------------------------------------------------------------------------
303
304uint32_t AudioTrack::latency() const
305{
306 return mLatency;
307}
308
Glenn Kastenfff6d712012-01-12 16:38:12 -0800309audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310{
311 return mStreamType;
312}
313
Glenn Kastene1c39622012-01-04 09:36:37 -0800314audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315{
316 return mFormat;
317}
318
319int AudioTrack::channelCount() const
320{
321 return mChannelCount;
322}
323
324uint32_t AudioTrack::frameCount() const
325{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700326 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327}
328
Glenn Kastenb9980652012-01-11 09:48:27 -0800329size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330{
Dima Zavinfce7a472011-04-19 22:30:36 -0700331 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700332 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700333 } else {
334 return sizeof(uint8_t);
335 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336}
337
338sp<IMemory>& AudioTrack::sharedBuffer()
339{
340 return mSharedBuffer;
341}
342
343// -------------------------------------------------------------------------
344
345void AudioTrack::start()
346{
347 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700348 status_t status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349
Steve Block3856b092011-10-20 11:56:00 +0100350 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 if (t != 0) {
352 if (t->exitPending()) {
353 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block29357bc2012-01-06 19:20:56 +0000354 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355 return;
356 }
357 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700358 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359
Eric Laurentf5aafb22010-11-18 08:40:16 -0800360 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800361 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
362 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700363 sp<IAudioTrack> audioTrack = mAudioTrack;
364 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800365 audio_track_cblk_t* cblk = mCblk;
366
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800367 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700368 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800369 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800370 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700371 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800372 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
373 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700374 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800375 pid_t tid;
Eric Laurent2b584242009-11-09 23:32:22 -0800376 if (t != 0) {
Glenn Kasten480b4682012-02-28 12:30:08 -0800377 t->run("AudioTrack", ANDROID_PRIORITY_AUDIO);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800378 tid = t->getTid(); // pid_t is unknown until run()
379 ALOGV("getTid=%d", tid);
380 if (tid == -1) {
381 tid = 0;
382 }
Eric Laurent2b584242009-11-09 23:32:22 -0800383 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700384 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
385 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
386 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800387 tid = 0; // not gettid()
Eric Laurent2b584242009-11-09 23:32:22 -0800388 }
389
Steve Block3856b092011-10-20 11:56:00 +0100390 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800391 if (!(cblk->flags & CBLK_INVALID_MSK)) {
392 cblk->lock.unlock();
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800393 ALOGV("mAudioTrack->start(tid=%d)", tid);
394 status = mAudioTrack->start(tid);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800395 cblk->lock.lock();
396 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700397 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800398 }
Eric Laurent2b584242009-11-09 23:32:22 -0800399 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800400 if (cblk->flags & CBLK_INVALID_MSK) {
401 status = restoreTrack_l(cblk, true);
402 }
403 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800404 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100405 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800406 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800407 if (t != 0) {
408 t->requestExit();
409 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700410 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
411 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800412 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800413 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 }
415
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416}
417
418void AudioTrack::stop()
419{
420 sp<AudioTrackThread> t = mAudioTrackThread;
421
Steve Block3856b092011-10-20 11:56:00 +0100422 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423
Eric Laurentf5aafb22010-11-18 08:40:16 -0800424 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800425 if (mActive) {
426 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700427 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 mAudioTrack->stop();
429 // Cancel loops (If we are in the middle of a loop, playback
430 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800431 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700432 // the playback head position will reset to 0, so if a marker is set, we need
433 // to activate it again
434 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 // Force flush if a shared buffer is used otherwise audioflinger
436 // will not stop before end of buffer is reached.
437 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800438 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439 }
440 if (t != 0) {
441 t->requestExit();
442 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700443 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
444 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445 }
446 }
447
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448}
449
450bool AudioTrack::stopped() const
451{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800452 AutoMutex lock(mLock);
453 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454}
455
456void AudioTrack::flush()
457{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800458 AutoMutex lock(mLock);
459 flush_l();
460}
461
462// must be called with mLock held
463void AudioTrack::flush_l()
464{
Steve Block3856b092011-10-20 11:56:00 +0100465 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700466
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700467 // clear playback marker and periodic update counter
468 mMarkerPosition = 0;
469 mMarkerReached = false;
470 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700471
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700473 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474 mAudioTrack->flush();
475 // Release AudioTrack callback thread in case it was waiting for new buffers
476 // in AudioTrack::obtainBuffer()
477 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478 }
479}
480
481void AudioTrack::pause()
482{
Steve Block3856b092011-10-20 11:56:00 +0100483 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800484 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800485 if (mActive) {
486 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 mAudioTrack->pause();
488 }
489}
490
491void AudioTrack::mute(bool e)
492{
493 mAudioTrack->mute(e);
494 mMuted = e;
495}
496
497bool AudioTrack::muted() const
498{
499 return mMuted;
500}
501
Eric Laurentbe916aa2010-06-01 23:49:17 -0700502status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800504 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700505 return BAD_VALUE;
506 }
507
Eric Laurent1703cdf2011-03-07 14:52:59 -0800508 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509 mVolume[LEFT] = left;
510 mVolume[RIGHT] = right;
511
Glenn Kasten83d86532012-01-17 14:39:34 -0800512 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700513
514 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515}
516
Glenn Kastena5224f32012-01-04 12:41:44 -0800517void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700519 if (left != NULL) {
520 *left = mVolume[LEFT];
521 }
522 if (right != NULL) {
523 *right = mVolume[RIGHT];
524 }
525}
526
Eric Laurent2beeb502010-07-16 07:43:46 -0700527status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700528{
Steve Block3856b092011-10-20 11:56:00 +0100529 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800530 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700531 return BAD_VALUE;
532 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800533 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700534
535 mSendLevel = level;
536
Glenn Kasten05632a52012-01-03 14:22:33 -0800537 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700538
539 return NO_ERROR;
540}
541
Glenn Kastena5224f32012-01-04 12:41:44 -0800542void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700543{
544 if (level != NULL) {
545 *level = mSendLevel;
546 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547}
548
Eric Laurent57326622009-07-07 07:10:45 -0700549status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550{
551 int afSamplingRate;
552
John Grossman4ff14ba2012-02-08 16:37:41 -0800553 if (mIsTimed) {
554 return INVALID_OPERATION;
555 }
556
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700558 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559 }
560 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700561 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562
Eric Laurent1703cdf2011-03-07 14:52:59 -0800563 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700564 mCblk->sampleRate = rate;
565 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566}
567
Glenn Kastena5224f32012-01-04 12:41:44 -0800568uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800569{
John Grossman4ff14ba2012-02-08 16:37:41 -0800570 if (mIsTimed) {
571 return INVALID_OPERATION;
572 }
573
Eric Laurent1703cdf2011-03-07 14:52:59 -0800574 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700575 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576}
577
578status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
579{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800580 AutoMutex lock(mLock);
581 return setLoop_l(loopStart, loopEnd, loopCount);
582}
583
584// must be called with mLock held
585status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
586{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 audio_track_cblk_t* cblk = mCblk;
588
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 Mutex::Autolock _l(cblk->lock);
590
591 if (loopCount == 0) {
592 cblk->loopStart = UINT_MAX;
593 cblk->loopEnd = UINT_MAX;
594 cblk->loopCount = 0;
595 mLoopCount = 0;
596 return NO_ERROR;
597 }
598
John Grossman4ff14ba2012-02-08 16:37:41 -0800599 if (mIsTimed) {
600 return INVALID_OPERATION;
601 }
602
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700604 loopEnd - loopStart > cblk->frameCount ||
605 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000606 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 -0800607 return BAD_VALUE;
608 }
609
Eric Laurent9b7d9502011-03-21 11:49:00 -0700610 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000611 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700612 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700614 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615
616 cblk->loopStart = loopStart;
617 cblk->loopEnd = loopEnd;
618 cblk->loopCount = loopCount;
619 mLoopCount = loopCount;
620
621 return NO_ERROR;
622}
623
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624status_t AudioTrack::setMarkerPosition(uint32_t marker)
625{
Glenn Kastena0d68332012-01-27 16:47:15 -0800626 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627
628 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700629 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630
631 return NO_ERROR;
632}
633
Glenn Kastena5224f32012-01-04 12:41:44 -0800634status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635{
Glenn Kastena0d68332012-01-27 16:47:15 -0800636 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637
638 *marker = mMarkerPosition;
639
640 return NO_ERROR;
641}
642
643status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
644{
Glenn Kastena0d68332012-01-27 16:47:15 -0800645 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646
647 uint32_t curPosition;
648 getPosition(&curPosition);
649 mNewPosition = curPosition + updatePeriod;
650 mUpdatePeriod = updatePeriod;
651
652 return NO_ERROR;
653}
654
Glenn Kastena5224f32012-01-04 12:41:44 -0800655status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656{
Glenn Kastena0d68332012-01-27 16:47:15 -0800657 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800658
659 *updatePeriod = mUpdatePeriod;
660
661 return NO_ERROR;
662}
663
664status_t AudioTrack::setPosition(uint32_t position)
665{
John Grossman4ff14ba2012-02-08 16:37:41 -0800666 if (mIsTimed) return INVALID_OPERATION;
667
Eric Laurent1703cdf2011-03-07 14:52:59 -0800668 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800670 if (!stopped_l()) return INVALID_OPERATION;
671
672 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673
674 if (position > mCblk->user) return BAD_VALUE;
675
676 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700677 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700678
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679 return NO_ERROR;
680}
681
682status_t AudioTrack::getPosition(uint32_t *position)
683{
Glenn Kastena0d68332012-01-27 16:47:15 -0800684 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800685 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700686 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687
688 return NO_ERROR;
689}
690
691status_t AudioTrack::reload()
692{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800693 AutoMutex lock(mLock);
694
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800695 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700696
Eric Laurent1703cdf2011-03-07 14:52:59 -0800697 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698
Eric Laurentd1b449a2010-05-14 03:26:45 -0700699 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700
701 return NO_ERROR;
702}
703
Eric Laurentc2f1f072009-07-17 12:17:14 -0700704audio_io_handle_t AudioTrack::getOutput()
705{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800706 AutoMutex lock(mLock);
707 return getOutput_l();
708}
709
710// must be called with mLock held
711audio_io_handle_t AudioTrack::getOutput_l()
712{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800713 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800714 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700715}
716
Glenn Kastena5224f32012-01-04 12:41:44 -0800717int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700718{
719 return mSessionId;
720}
721
722status_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,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700739 uint32_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800740 int frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -0800741 audio_policy_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800742 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700743 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800744{
745 status_t status;
746 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
747 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700748 ALOGE("Could not get audioflinger");
749 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800750 }
751
Eric Laurentd1b449a2010-05-14 03:26:45 -0700752 int afSampleRate;
753 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
754 return NO_INIT;
755 }
756 int afFrameCount;
757 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
758 return NO_INIT;
759 }
760 uint32_t afLatency;
761 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
762 return NO_INIT;
763 }
764
765 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavinfce7a472011-04-19 22:30:36 -0700766 if (!audio_is_linear_pcm(format)) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700767 if (sharedBuffer != 0) {
768 frameCount = sharedBuffer->size();
769 }
770 } else {
771 // Ensure that buffer depth covers at least audio hardware latency
772 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
773 if (minBufCount < 2) minBufCount = 2;
774
775 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
776
777 if (sharedBuffer == 0) {
778 if (frameCount == 0) {
779 frameCount = minFrameCount;
780 }
781 if (mNotificationFramesAct == 0) {
782 mNotificationFramesAct = frameCount/2;
783 }
784 // Make sure that application is notified with sufficient margin
785 // before underrun
786 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
787 mNotificationFramesAct = frameCount/2;
788 }
789 if (frameCount < minFrameCount) {
Glenn Kasten291f4d52012-03-19 12:16:56 -0700790 // not ALOGW because it happens all the time when playing key clicks over A2DP
791 ALOGV("Minimum buffer size corrected from %d to %d",
Eric Laurentd8d61852012-03-05 17:06:40 -0800792 frameCount, minFrameCount);
793 frameCount = minFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700794 }
795 } else {
Glenn Kasten99e53b82012-01-19 08:59:58 -0800796 // Ensure that buffer alignment matches channelCount
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700797 int channelCount = popcount(channelMask);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700798 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000799 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700800 return BAD_VALUE;
801 }
802 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
803 }
804 }
805
Glenn Kastena075db42012-03-06 11:22:44 -0800806 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
807 if (mIsTimed) {
808 trackFlags |= IAudioFlinger::TRACK_TIMED;
809 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800810 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
811 streamType,
812 sampleRate,
813 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700814 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800815 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -0800816 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800817 sharedBuffer,
818 output,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700819 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800820 &status);
821
822 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000823 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800824 return status;
825 }
826 sp<IMemory> cblk = track->getCblk();
827 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000828 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800829 return NO_INIT;
830 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800831 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800832 mCblkMemory = cblk;
833 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurent38ccae22011-03-28 18:37:07 -0700834 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800835 if (sharedBuffer == 0) {
836 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
837 } else {
838 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700839 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700840 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800841 }
842
Glenn Kasten83d86532012-01-17 14:39:34 -0800843 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800844 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700845 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800846 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
847 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700848 mRemainingFrames = mNotificationFramesAct;
849 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800850 return NO_ERROR;
851}
852
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
854{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800855 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800856 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700857 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800858 audio_track_cblk_t* cblk = mCblk;
859 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700860 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861
862 audioBuffer->frameCount = 0;
863 audioBuffer->size = 0;
864
865 uint32_t framesAvail = cblk->framesAvailable();
866
Eric Laurent9b7d9502011-03-21 11:49:00 -0700867 cblk->lock.lock();
868 if (cblk->flags & CBLK_INVALID_MSK) {
869 goto create_new_track;
870 }
871 cblk->lock.unlock();
872
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800873 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800874 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800875 goto start_loop_here;
876 while (framesAvail == 0) {
877 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800878 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800880 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 return NO_MORE_BUFFERS;
882 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800883 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800885 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800886 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700887 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800888 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700889 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700890 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800891 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800892 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800893 return status_t(STOPPED);
894 }
895 cblk->lock.lock();
896 }
897
898 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700899 goto create_new_track;
900 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800901 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700902 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
904 // timing out when a loop has been set and we have already written upto loop end
905 // is a normal condition: no need to wake AudioFlinger up.
906 if (cblk->user < cblk->loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000907 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700909 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800910 cblk->lock.unlock();
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800911 result = mAudioTrack->start(0); // callback thread hasn't changed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800912 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800913 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700914 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800915create_new_track:
916 result = restoreTrack_l(cblk, false);
917 }
918 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000919 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800920 cblk->lock.unlock();
921 return result;
922 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800923 }
924 cblk->waitTimeMs = 0;
925 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700926
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800927 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800928 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929 return TIMED_OUT;
930 }
931 }
932 // read the server count again
933 start_loop_here:
934 framesAvail = cblk->framesAvailable_l();
935 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800936 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937 }
938
Eric Laurent44d98482010-09-30 16:12:31 -0700939 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent1703cdf2011-03-07 14:52:59 -0800940 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700941 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +0000942 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800943 mAudioTrack->start(0); // callback thread hasn't changed
Eric Laurent44d98482010-09-30 16:12:31 -0700944 }
945
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700947
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800948 if (framesReq > framesAvail) {
949 framesReq = framesAvail;
950 }
951
952 uint32_t u = cblk->user;
953 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
954
Marco Nelissena1472d92012-03-30 14:36:54 -0700955 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 framesReq = bufferEnd - u;
957 }
958
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
960 audioBuffer->channelCount = mChannelCount;
961 audioBuffer->frameCount = framesReq;
962 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -0700963 if (audio_is_linear_pcm(mFormat)) {
964 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700965 } else {
966 audioBuffer->format = mFormat;
967 }
968 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800969 active = mActive;
970 return active ? status_t(NO_ERROR) : status_t(STOPPED);
971}
972
973void AudioTrack::releaseBuffer(Buffer* audioBuffer)
974{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800975 AutoMutex lock(mLock);
976 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977}
978
979// -------------------------------------------------------------------------
980
981ssize_t AudioTrack::write(const void* buffer, size_t userSize)
982{
983
984 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -0800985 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986
987 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -0800988 // Sanity-check: user is most-likely passing an error code, and it would
989 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +0000990 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991 buffer, userSize, userSize);
992 return BAD_VALUE;
993 }
994
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800996
Eric Laurent1703cdf2011-03-07 14:52:59 -0800997 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
998 // while we are accessing the cblk
999 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001000 sp<IAudioTrack> audioTrack = mAudioTrack;
1001 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001002 mLock.unlock();
1003
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001004 ssize_t written = 0;
1005 const int8_t *src = (const int8_t *)buffer;
1006 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001007 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001008
1009 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001010 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001011
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001012 status_t err = obtainBuffer(&audioBuffer, -1);
1013 if (err < 0) {
1014 // out of buffers, return #bytes written
1015 if (err == status_t(NO_MORE_BUFFERS))
1016 break;
1017 return ssize_t(err);
1018 }
1019
1020 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001021
Dima Zavinfce7a472011-04-19 22:30:36 -07001022 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001023 // Divide capacity by 2 to take expansion into account
1024 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001025 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001026 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001027 toWrite = audioBuffer.size;
1028 memcpy(audioBuffer.i8, src, toWrite);
1029 src += toWrite;
1030 }
1031 userSize -= toWrite;
1032 written += toWrite;
1033
1034 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001035 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001036
1037 return written;
1038}
1039
1040// -------------------------------------------------------------------------
1041
John Grossman4ff14ba2012-02-08 16:37:41 -08001042TimedAudioTrack::TimedAudioTrack() {
1043 mIsTimed = true;
1044}
1045
1046status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1047{
1048 status_t result = UNKNOWN_ERROR;
1049
1050 // If the track is not invalid already, try to allocate a buffer. alloc
1051 // fails indicating that the server is dead, flag the track as invalid so
1052 // we can attempt to restore in in just a bit.
1053 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1054 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1055 if (result == DEAD_OBJECT) {
1056 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1057 }
1058 }
1059
1060 // If the track is invalid at this point, attempt to restore it. and try the
1061 // allocation one more time.
1062 if (mCblk->flags & CBLK_INVALID_MSK) {
1063 mCblk->lock.lock();
1064 result = restoreTrack_l(mCblk, false);
1065 mCblk->lock.unlock();
1066
1067 if (result == OK)
1068 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1069 }
1070
1071 return result;
1072}
1073
1074status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1075 int64_t pts)
1076{
1077 // restart track if it was disabled by audioflinger due to previous underrun
1078 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1079 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1080 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1081 mAudioTrack->start(0);
1082 }
1083
1084 return mAudioTrack->queueTimedBuffer(buffer, pts);
1085}
1086
1087status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1088 TargetTimeline target)
1089{
1090 return mAudioTrack->setMediaTimeTransform(xform, target);
1091}
1092
1093// -------------------------------------------------------------------------
1094
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001095bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1096{
1097 Buffer audioBuffer;
1098 uint32_t frames;
1099 size_t writtenSize;
1100
Eric Laurent1703cdf2011-03-07 14:52:59 -08001101 mLock.lock();
1102 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1103 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001104 sp<IAudioTrack> audioTrack = mAudioTrack;
1105 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001106 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001107 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001108 mLock.unlock();
1109
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001110 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001111 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001112 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001113 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001114 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001115 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001116 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001117 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001118 if (mSharedBuffer != 0) return false;
1119 }
1120 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001121
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001123 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001124 int loopCount = -1;
1125 mLoopCount--;
1126 if (mLoopCount >= 0) loopCount = mLoopCount;
1127
1128 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1129 }
1130
1131 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001132 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001133 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001134 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001135 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001136 }
1137 }
1138
1139 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001140 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001141 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001142 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1143 mNewPosition += mUpdatePeriod;
1144 }
1145 }
1146
1147 // If Shared buffer is used, no data is requested from client.
1148 if (mSharedBuffer != 0) {
1149 frames = 0;
1150 } else {
1151 frames = mRemainingFrames;
1152 }
1153
Glenn Kasten99e53b82012-01-19 08:59:58 -08001154 // See description of waitCount parameter at declaration of obtainBuffer().
1155 // The logic below prevents us from being stuck below at obtainBuffer()
1156 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001157 int32_t waitCount = -1;
1158 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1159 waitCount = 1;
1160 }
1161
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001162 do {
1163
1164 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001165
Eric Laurent2267ba12011-09-07 11:13:23 -07001166 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 if (err < NO_ERROR) {
1168 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001169 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 -08001170 return false;
1171 }
1172 break;
1173 }
1174 if (err == status_t(STOPPED)) return false;
1175
1176 // Divide buffer size by 2 to take into account the expansion
1177 // due to 8 to 16 bit conversion: the callback must fill only half
1178 // of the destination buffer
Dima Zavinfce7a472011-04-19 22:30:36 -07001179 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001180 audioBuffer.size >>= 1;
1181 }
1182
1183 size_t reqSize = audioBuffer.size;
1184 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1185 writtenSize = audioBuffer.size;
1186
1187 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001188 if (ssize_t(writtenSize) <= 0) {
1189 // The callback is done filling buffers
1190 // Keep this thread going to handle timed events and
1191 // still try to get more data in intervals of WAIT_PERIOD_MS
1192 // but don't just loop and block the CPU, so wait
1193 usleep(WAIT_PERIOD_MS*1000);
1194 break;
1195 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001196 if (writtenSize > reqSize) writtenSize = reqSize;
1197
Dima Zavinfce7a472011-04-19 22:30:36 -07001198 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001199 // 8 to 16 bit conversion, note that source and destination are the same address
1200 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 writtenSize <<= 1;
1202 }
1203
1204 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001205 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001206 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001207 // 16 bit.
1208 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1209
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001210 frames -= audioBuffer.frameCount;
1211
1212 releaseBuffer(&audioBuffer);
1213 }
1214 while (frames);
1215
1216 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001217 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001218 } else {
1219 mRemainingFrames = frames;
1220 }
1221 return true;
1222}
1223
Eric Laurent1703cdf2011-03-07 14:52:59 -08001224// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1225// the IAudioTrack and IMemory in case they are recreated here.
1226// If the IAudioTrack is successfully restored, the cblk pointer is updated
1227status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1228{
1229 status_t result;
1230
Eric Laurent38ccae22011-03-28 18:37:07 -07001231 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001232 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001233 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001234
Eric Laurent1703cdf2011-03-07 14:52:59 -08001235 // signal old cblk condition so that other threads waiting for available buffers stop
1236 // waiting now
1237 cblk->cv.broadcast();
1238 cblk->lock.unlock();
1239
Eric Laurent9f6530f2011-08-30 10:18:54 -07001240 // refresh the audio configuration cache in this process to make sure we get new
1241 // output parameters in getOutput_l() and createTrack_l()
1242 AudioSystem::clearAudioConfigCache();
1243
Eric Laurent1703cdf2011-03-07 14:52:59 -08001244 // if the new IAudioTrack is created, createTrack_l() will modify the
1245 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1246 // It will also delete the strong references on previous IAudioTrack and IMemory
1247 result = createTrack_l(mStreamType,
1248 cblk->sampleRate,
1249 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001250 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001251 mFrameCount,
1252 mFlags,
1253 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001254 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001255
1256 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001257 uint32_t user = cblk->user;
1258 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001259 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001260 mCblk->user = user;
1261 mCblk->server = user;
1262 mCblk->userBase = user;
1263 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001264 // restore loop: this is not guaranteed to succeed if new frame count is not
1265 // compatible with loop length
1266 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001267 if (!fromStart) {
1268 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001269 // Make sure that a client relying on callback events indicating underrun or
1270 // the actual amount of audio frames played (e.g SoundPool) receives them.
1271 if (mSharedBuffer == 0) {
1272 uint32_t frames = 0;
1273 if (user > server) {
1274 frames = ((user - server) > mCblk->frameCount) ?
1275 mCblk->frameCount : (user - server);
1276 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1277 }
1278 // restart playback even if buffer is not completely filled.
1279 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1280 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1281 // the client
1282 mCblk->stepUser(frames);
1283 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001284 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001285 if (mActive) {
Glenn Kasten6dbc1352012-02-02 10:56:47 -08001286 result = mAudioTrack->start(0); // callback thread hasn't changed
Steve Block5ff1dd52012-01-05 23:22:43 +00001287 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001288 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001289 if (fromStart && result == NO_ERROR) {
1290 mNewPosition = mCblk->server + mUpdatePeriod;
1291 }
1292 }
1293 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001294 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001295 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001296 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001297 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001298 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001299 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001300 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001301 } else {
1302 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001303 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001304 mLock.unlock();
1305 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001306 if (result == NO_ERROR) {
1307 result = mRestoreStatus;
1308 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001309 cblk->lock.unlock();
1310 mLock.lock();
1311 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001312 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001313 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001314 cblk->lock.unlock();
1315 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001316 }
Steve Block3856b092011-10-20 11:56:00 +01001317 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001318 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001319
1320 if (result == NO_ERROR) {
1321 // from now on we switch to the newly created cblk
1322 cblk = mCblk;
1323 }
1324 cblk->lock.lock();
1325
Steve Block5ff1dd52012-01-05 23:22:43 +00001326 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001327
1328 return result;
1329}
1330
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001331status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1332{
1333
1334 const size_t SIZE = 256;
1335 char buffer[SIZE];
1336 String8 result;
1337
1338 result.append(" AudioTrack::dump\n");
1339 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1340 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001341 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 -08001342 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001343 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 -08001344 result.append(buffer);
1345 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1346 result.append(buffer);
1347 ::write(fd, result.string(), result.size());
1348 return NO_ERROR;
1349}
1350
1351// =========================================================================
1352
1353AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1354 : Thread(bCanCallJava), mReceiver(receiver)
1355{
1356}
1357
1358bool AudioTrack::AudioTrackThread::threadLoop()
1359{
1360 return mReceiver.processAudioBuffer(this);
1361}
1362
1363status_t AudioTrack::AudioTrackThread::readyToRun()
1364{
1365 return NO_ERROR;
1366}
1367
1368void AudioTrack::AudioTrackThread::onFirstRef()
1369{
1370}
1371
1372// =========================================================================
1373
Eric Laurent38ccae22011-03-28 18:37:07 -07001374
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001375audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001376 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001377 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001378 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001379 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001380{
1381}
1382
1383uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1384{
Marco Nelissena1472d92012-03-30 14:36:54 -07001385 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001386
Marco Nelissena1472d92012-03-30 14:36:54 -07001387 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001388 u += frameCount;
1389 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001390 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001391 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1392 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1393 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1394 }
Glenn Kasten90548972011-12-13 11:45:07 -08001395 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001396 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001397 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398 }
1399
Marco Nelissena1472d92012-03-30 14:36:54 -07001400 uint32_t fc = this->frameCount;
1401 if (u >= fc) {
1402 // common case, user didn't just wrap
1403 if (u - fc >= userBase ) {
1404 userBase += fc;
1405 }
1406 } else if (u >= userBase + fc) {
1407 // user just wrapped
1408 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001409 }
1410
Glenn Kasten90548972011-12-13 11:45:07 -08001411 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001412
1413 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001414 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001415 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001416 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001417
1418 return u;
1419}
1420
1421bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1422{
Marco Nelissena1472d92012-03-30 14:36:54 -07001423 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1424
Eric Laurent38ccae22011-03-28 18:37:07 -07001425 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001426 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427 return false;
1428 }
1429
Glenn Kasten90548972011-12-13 11:45:07 -08001430 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001431 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432
1433 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001434 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001435 // Mark that we have read the first buffer so that next time stepUser() is called
1436 // we switch to normal obtainBuffer() timeout period
1437 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001438 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001439 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001440 // It is possible that we receive a flush()
1441 // while the mixer is processing a block: in this case,
1442 // stepServer() is called After the flush() has reset u & s and
1443 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001444 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001445 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001446 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447 }
1448 }
1449
1450 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001451 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 s = loopStart;
1453 if (--loopCount == 0) {
1454 loopEnd = UINT_MAX;
1455 loopStart = UINT_MAX;
1456 }
1457 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001458
1459 uint32_t fc = this->frameCount;
1460 if (s >= fc) {
1461 // common case, server didn't just wrap
1462 if (s - fc >= serverBase ) {
1463 serverBase += fc;
1464 }
1465 } else if (s >= serverBase + fc) {
1466 // server just wrapped
1467 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001468 }
1469
Glenn Kasten90548972011-12-13 11:45:07 -08001470 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001471
Eric Laurent1703cdf2011-03-07 14:52:59 -08001472 if (!(flags & CBLK_INVALID_MSK)) {
1473 cv.signal();
1474 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001475 lock.unlock();
1476 return true;
1477}
1478
1479void* audio_track_cblk_t::buffer(uint32_t offset) const
1480{
Glenn Kasten90548972011-12-13 11:45:07 -08001481 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001482}
1483
1484uint32_t audio_track_cblk_t::framesAvailable()
1485{
1486 Mutex::Autolock _l(lock);
1487 return framesAvailable_l();
1488}
1489
1490uint32_t audio_track_cblk_t::framesAvailable_l()
1491{
Glenn Kasten90548972011-12-13 11:45:07 -08001492 uint32_t u = user;
1493 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001494
Eric Laurentd1b449a2010-05-14 03:26:45 -07001495 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001496 uint32_t limit = (s < loopStart) ? s : loopStart;
1497 return limit + frameCount - u;
1498 } else {
1499 return frameCount + u - s;
1500 }
1501}
1502
1503uint32_t audio_track_cblk_t::framesReady()
1504{
Glenn Kasten90548972011-12-13 11:45:07 -08001505 uint32_t u = user;
1506 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001507
Eric Laurentd1b449a2010-05-14 03:26:45 -07001508 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001509 if (u < loopEnd) {
1510 return u - s;
1511 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001512 // do not block on mutex shared with client on AudioFlinger side
1513 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001514 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001515 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001516 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001517 uint32_t frames = UINT_MAX;
1518 if (loopCount >= 0) {
1519 frames = (loopEnd - loopStart)*loopCount + u - s;
1520 }
1521 lock.unlock();
1522 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001523 }
1524 } else {
1525 return s - u;
1526 }
1527}
1528
Eric Laurent38ccae22011-03-28 18:37:07 -07001529bool audio_track_cblk_t::tryLock()
1530{
1531 // the code below simulates lock-with-timeout
1532 // we MUST do this to protect the AudioFlinger server
1533 // as this lock is shared with the client.
1534 status_t err;
1535
1536 err = lock.tryLock();
1537 if (err == -EBUSY) { // just wait a bit
1538 usleep(1000);
1539 err = lock.tryLock();
1540 }
1541 if (err != NO_ERROR) {
1542 // probably, the client just died.
1543 return false;
1544 }
1545 return true;
1546}
1547
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001548// -------------------------------------------------------------------------
1549
1550}; // namespace android