blob: 50fbf3617eb90544881492f586d1e843be3841d9 [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
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800806 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
807 streamType,
808 sampleRate,
809 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700810 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800811 frameCount,
812 ((uint16_t)flags) << 16,
813 sharedBuffer,
814 output,
John Grossman4ff14ba2012-02-08 16:37:41 -0800815 mIsTimed,
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,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001251 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001252
1253 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001254 uint32_t user = cblk->user;
1255 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001256 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001257 mCblk->user = user;
1258 mCblk->server = user;
1259 mCblk->userBase = user;
1260 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001261 // restore loop: this is not guaranteed to succeed if new frame count is not
1262 // compatible with loop length
1263 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001264 if (!fromStart) {
1265 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001266 // Make sure that a client relying on callback events indicating underrun or
1267 // the actual amount of audio frames played (e.g SoundPool) receives them.
1268 if (mSharedBuffer == 0) {
1269 uint32_t frames = 0;
1270 if (user > server) {
1271 frames = ((user - server) > mCblk->frameCount) ?
1272 mCblk->frameCount : (user - server);
1273 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1274 }
1275 // restart playback even if buffer is not completely filled.
1276 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1277 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1278 // the client
1279 mCblk->stepUser(frames);
1280 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001281 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001282 if (mActive) {
Glenn Kasten6dbc1352012-02-02 10:56:47 -08001283 result = mAudioTrack->start(0); // callback thread hasn't changed
Steve Block5ff1dd52012-01-05 23:22:43 +00001284 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001285 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001286 if (fromStart && result == NO_ERROR) {
1287 mNewPosition = mCblk->server + mUpdatePeriod;
1288 }
1289 }
1290 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001291 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001292 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001293 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001294 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001295 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001296 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001297 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001298 } else {
1299 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001300 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001301 mLock.unlock();
1302 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001303 if (result == NO_ERROR) {
1304 result = mRestoreStatus;
1305 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001306 cblk->lock.unlock();
1307 mLock.lock();
1308 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001309 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001310 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001311 cblk->lock.unlock();
1312 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001313 }
Steve Block3856b092011-10-20 11:56:00 +01001314 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001315 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001316
1317 if (result == NO_ERROR) {
1318 // from now on we switch to the newly created cblk
1319 cblk = mCblk;
1320 }
1321 cblk->lock.lock();
1322
Steve Block5ff1dd52012-01-05 23:22:43 +00001323 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001324
1325 return result;
1326}
1327
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001328status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1329{
1330
1331 const size_t SIZE = 256;
1332 char buffer[SIZE];
1333 String8 result;
1334
1335 result.append(" AudioTrack::dump\n");
1336 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1337 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001338 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 -08001339 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001340 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 -08001341 result.append(buffer);
1342 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1343 result.append(buffer);
1344 ::write(fd, result.string(), result.size());
1345 return NO_ERROR;
1346}
1347
1348// =========================================================================
1349
1350AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1351 : Thread(bCanCallJava), mReceiver(receiver)
1352{
1353}
1354
1355bool AudioTrack::AudioTrackThread::threadLoop()
1356{
1357 return mReceiver.processAudioBuffer(this);
1358}
1359
1360status_t AudioTrack::AudioTrackThread::readyToRun()
1361{
1362 return NO_ERROR;
1363}
1364
1365void AudioTrack::AudioTrackThread::onFirstRef()
1366{
1367}
1368
1369// =========================================================================
1370
Eric Laurent38ccae22011-03-28 18:37:07 -07001371
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001372audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001373 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001374 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001375 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001376 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001377{
1378}
1379
1380uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1381{
Glenn Kasten90548972011-12-13 11:45:07 -08001382 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001383
1384 u += frameCount;
1385 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001386 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001387 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1388 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1389 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1390 }
Glenn Kasten90548972011-12-13 11:45:07 -08001391 } else if (u > server) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001392 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001393 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001394 }
1395
1396 if (u >= userBase + this->frameCount) {
1397 userBase += this->frameCount;
1398 }
1399
Glenn Kasten90548972011-12-13 11:45:07 -08001400 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001401
1402 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001403 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001404 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001405 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001406
1407 return u;
1408}
1409
1410bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1411{
Eric Laurent38ccae22011-03-28 18:37:07 -07001412 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001413 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001414 return false;
1415 }
1416
Glenn Kasten90548972011-12-13 11:45:07 -08001417 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001418
1419 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001420 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001421 // Mark that we have read the first buffer so that next time stepUser() is called
1422 // we switch to normal obtainBuffer() timeout period
1423 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001424 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001425 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426 // It is possible that we receive a flush()
1427 // while the mixer is processing a block: in this case,
1428 // stepServer() is called After the flush() has reset u & s and
1429 // we have s > u
Glenn Kasten90548972011-12-13 11:45:07 -08001430 if (s > user) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001431 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001432 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001433 }
1434 }
1435
1436 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001437 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438 s = loopStart;
1439 if (--loopCount == 0) {
1440 loopEnd = UINT_MAX;
1441 loopStart = UINT_MAX;
1442 }
1443 }
1444 if (s >= serverBase + this->frameCount) {
1445 serverBase += this->frameCount;
1446 }
1447
Glenn Kasten90548972011-12-13 11:45:07 -08001448 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001449
Eric Laurent1703cdf2011-03-07 14:52:59 -08001450 if (!(flags & CBLK_INVALID_MSK)) {
1451 cv.signal();
1452 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001453 lock.unlock();
1454 return true;
1455}
1456
1457void* audio_track_cblk_t::buffer(uint32_t offset) const
1458{
Glenn Kasten90548972011-12-13 11:45:07 -08001459 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460}
1461
1462uint32_t audio_track_cblk_t::framesAvailable()
1463{
1464 Mutex::Autolock _l(lock);
1465 return framesAvailable_l();
1466}
1467
1468uint32_t audio_track_cblk_t::framesAvailable_l()
1469{
Glenn Kasten90548972011-12-13 11:45:07 -08001470 uint32_t u = user;
1471 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001472
Eric Laurentd1b449a2010-05-14 03:26:45 -07001473 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001474 uint32_t limit = (s < loopStart) ? s : loopStart;
1475 return limit + frameCount - u;
1476 } else {
1477 return frameCount + u - s;
1478 }
1479}
1480
1481uint32_t audio_track_cblk_t::framesReady()
1482{
Glenn Kasten90548972011-12-13 11:45:07 -08001483 uint32_t u = user;
1484 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001485
Eric Laurentd1b449a2010-05-14 03:26:45 -07001486 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001487 if (u < loopEnd) {
1488 return u - s;
1489 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001490 // do not block on mutex shared with client on AudioFlinger side
1491 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001492 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001493 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001494 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001495 uint32_t frames = UINT_MAX;
1496 if (loopCount >= 0) {
1497 frames = (loopEnd - loopStart)*loopCount + u - s;
1498 }
1499 lock.unlock();
1500 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001501 }
1502 } else {
1503 return s - u;
1504 }
1505}
1506
Eric Laurent38ccae22011-03-28 18:37:07 -07001507bool audio_track_cblk_t::tryLock()
1508{
1509 // the code below simulates lock-with-timeout
1510 // we MUST do this to protect the AudioFlinger server
1511 // as this lock is shared with the client.
1512 status_t err;
1513
1514 err = lock.tryLock();
1515 if (err == -EBUSY) { // just wait a bit
1516 usleep(1000);
1517 err = lock.tryLock();
1518 }
1519 if (err != NO_ERROR) {
1520 // probably, the client just died.
1521 return false;
1522 }
1523 return true;
1524}
1525
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001526// -------------------------------------------------------------------------
1527
1528}; // namespace android