blob: 60143c600f8d382e740ae599b018229a883f1d87 [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 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198 uint32_t afLatency;
199 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
200 return NO_INIT;
201 }
202
203 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700204 if (streamType == AUDIO_STREAM_DEFAULT) {
205 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 }
207 if (sampleRate == 0) {
208 sampleRate = afSampleRate;
209 }
210 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800211 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700212 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700214 if (channelMask == 0) {
215 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 }
217
218 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700219 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000220 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 return BAD_VALUE;
222 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700223
224 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700225 if (!audio_is_linear_pcm(format)) {
Glenn Kasten18868c52012-03-07 09:15:37 -0800226 flags = (audio_policy_output_flags_t) (flags | AUDIO_POLICY_OUTPUT_FLAG_DIRECT);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700227 }
228
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700229 if (!audio_is_output_channel(channelMask)) {
Steve Block29357bc2012-01-06 19:20:56 +0000230 ALOGE("Invalid channel mask");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700231 return BAD_VALUE;
232 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700233 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234
Dima Zavinfce7a472011-04-19 22:30:36 -0700235 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800236 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800237 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800238 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700239
240 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000241 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800242 return BAD_VALUE;
243 }
244
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800245 mVolume[LEFT] = 1.0f;
246 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800247 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700248 mFrameCount = frameCount;
249 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700250 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700251 mAuxEffectId = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700252
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800253 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800254 status_t status = createTrack_l(streamType,
255 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800256 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700257 (uint32_t)channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800258 frameCount,
259 flags,
260 sharedBuffer,
261 output,
262 true);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800264 if (status != NO_ERROR) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 return status;
266 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800267
Glenn Kastena0d68332012-01-27 16:47:15 -0800268 if (cbf != NULL) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270 }
271
272 mStatus = NO_ERROR;
273
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800275 mFormat = format;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700276 mChannelMask = (uint32_t)channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 mChannelCount = channelCount;
278 mSharedBuffer = sharedBuffer;
279 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800280 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 mCbf = cbf;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283 mLoopCount = 0;
284 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700285 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800286 mNewPosition = 0;
287 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700288 mFlushed = false;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700289 mFlags = flags;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700290 AudioSystem::acquireAudioSessionId(mSessionId);
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700291 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 return NO_ERROR;
293}
294
295status_t AudioTrack::initCheck() const
296{
297 return mStatus;
298}
299
300// -------------------------------------------------------------------------
301
302uint32_t AudioTrack::latency() const
303{
304 return mLatency;
305}
306
Glenn Kastenfff6d712012-01-12 16:38:12 -0800307audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308{
309 return mStreamType;
310}
311
Glenn Kastene1c39622012-01-04 09:36:37 -0800312audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313{
314 return mFormat;
315}
316
317int AudioTrack::channelCount() const
318{
319 return mChannelCount;
320}
321
322uint32_t AudioTrack::frameCount() const
323{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700324 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325}
326
Glenn Kastenb9980652012-01-11 09:48:27 -0800327size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328{
Dima Zavinfce7a472011-04-19 22:30:36 -0700329 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700330 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700331 } else {
332 return sizeof(uint8_t);
333 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334}
335
336sp<IMemory>& AudioTrack::sharedBuffer()
337{
338 return mSharedBuffer;
339}
340
341// -------------------------------------------------------------------------
342
343void AudioTrack::start()
344{
345 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700346 status_t status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347
Steve Block3856b092011-10-20 11:56:00 +0100348 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349 if (t != 0) {
350 if (t->exitPending()) {
351 if (t->requestExitAndWait() == WOULD_BLOCK) {
Steve Block29357bc2012-01-06 19:20:56 +0000352 ALOGE("AudioTrack::start called from thread");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353 return;
354 }
355 }
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700356 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357
Eric Laurentf5aafb22010-11-18 08:40:16 -0800358 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800359 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
360 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700361 sp<IAudioTrack> audioTrack = mAudioTrack;
362 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800363 audio_track_cblk_t* cblk = mCblk;
364
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800365 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700366 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800367 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800368 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700369 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800370 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
371 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700372 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800373 pid_t tid;
Eric Laurent2b584242009-11-09 23:32:22 -0800374 if (t != 0) {
Glenn Kasten480b4682012-02-28 12:30:08 -0800375 t->run("AudioTrack", ANDROID_PRIORITY_AUDIO);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800376 tid = t->getTid(); // pid_t is unknown until run()
377 ALOGV("getTid=%d", tid);
378 if (tid == -1) {
379 tid = 0;
380 }
Eric Laurent2b584242009-11-09 23:32:22 -0800381 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700382 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
383 mPreviousSchedulingGroup = androidGetThreadSchedulingGroup(0);
384 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800385 tid = 0; // not gettid()
Eric Laurent2b584242009-11-09 23:32:22 -0800386 }
387
Steve Block3856b092011-10-20 11:56:00 +0100388 ALOGV("start %p before lock cblk %p", this, mCblk);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800389 if (!(cblk->flags & CBLK_INVALID_MSK)) {
390 cblk->lock.unlock();
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800391 ALOGV("mAudioTrack->start(tid=%d)", tid);
392 status = mAudioTrack->start(tid);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800393 cblk->lock.lock();
394 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700395 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800396 }
Eric Laurent2b584242009-11-09 23:32:22 -0800397 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800398 if (cblk->flags & CBLK_INVALID_MSK) {
399 status = restoreTrack_l(cblk, true);
400 }
401 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800402 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100403 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800404 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800405 if (t != 0) {
406 t->requestExit();
407 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700408 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
409 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800410 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800411 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 }
413
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414}
415
416void AudioTrack::stop()
417{
418 sp<AudioTrackThread> t = mAudioTrackThread;
419
Steve Block3856b092011-10-20 11:56:00 +0100420 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421
Eric Laurentf5aafb22010-11-18 08:40:16 -0800422 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800423 if (mActive) {
424 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700425 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 mAudioTrack->stop();
427 // Cancel loops (If we are in the middle of a loop, playback
428 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800429 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700430 // the playback head position will reset to 0, so if a marker is set, we need
431 // to activate it again
432 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433 // Force flush if a shared buffer is used otherwise audioflinger
434 // will not stop before end of buffer is reached.
435 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800436 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437 }
438 if (t != 0) {
439 t->requestExit();
440 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700441 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
442 androidSetThreadSchedulingGroup(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443 }
444 }
445
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446}
447
448bool AudioTrack::stopped() const
449{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800450 AutoMutex lock(mLock);
451 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452}
453
454void AudioTrack::flush()
455{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800456 AutoMutex lock(mLock);
457 flush_l();
458}
459
460// must be called with mLock held
461void AudioTrack::flush_l()
462{
Steve Block3856b092011-10-20 11:56:00 +0100463 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700464
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700465 // clear playback marker and periodic update counter
466 mMarkerPosition = 0;
467 mMarkerReached = false;
468 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700469
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700471 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 mAudioTrack->flush();
473 // Release AudioTrack callback thread in case it was waiting for new buffers
474 // in AudioTrack::obtainBuffer()
475 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 }
477}
478
479void AudioTrack::pause()
480{
Steve Block3856b092011-10-20 11:56:00 +0100481 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800482 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800483 if (mActive) {
484 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 mAudioTrack->pause();
486 }
487}
488
489void AudioTrack::mute(bool e)
490{
491 mAudioTrack->mute(e);
492 mMuted = e;
493}
494
495bool AudioTrack::muted() const
496{
497 return mMuted;
498}
499
Eric Laurentbe916aa2010-06-01 23:49:17 -0700500status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800502 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700503 return BAD_VALUE;
504 }
505
Eric Laurent1703cdf2011-03-07 14:52:59 -0800506 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507 mVolume[LEFT] = left;
508 mVolume[RIGHT] = right;
509
Glenn Kasten83d86532012-01-17 14:39:34 -0800510 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700511
512 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513}
514
Glenn Kastena5224f32012-01-04 12:41:44 -0800515void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700517 if (left != NULL) {
518 *left = mVolume[LEFT];
519 }
520 if (right != NULL) {
521 *right = mVolume[RIGHT];
522 }
523}
524
Eric Laurent2beeb502010-07-16 07:43:46 -0700525status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700526{
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800528 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700529 return BAD_VALUE;
530 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800531 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700532
533 mSendLevel = level;
534
Glenn Kasten05632a52012-01-03 14:22:33 -0800535 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700536
537 return NO_ERROR;
538}
539
Glenn Kastena5224f32012-01-04 12:41:44 -0800540void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700541{
542 if (level != NULL) {
543 *level = mSendLevel;
544 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545}
546
Eric Laurent57326622009-07-07 07:10:45 -0700547status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548{
549 int afSamplingRate;
550
John Grossman4ff14ba2012-02-08 16:37:41 -0800551 if (mIsTimed) {
552 return INVALID_OPERATION;
553 }
554
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700556 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 }
558 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700559 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560
Eric Laurent1703cdf2011-03-07 14:52:59 -0800561 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700562 mCblk->sampleRate = rate;
563 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564}
565
Glenn Kastena5224f32012-01-04 12:41:44 -0800566uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567{
John Grossman4ff14ba2012-02-08 16:37:41 -0800568 if (mIsTimed) {
569 return INVALID_OPERATION;
570 }
571
Eric Laurent1703cdf2011-03-07 14:52:59 -0800572 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700573 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574}
575
576status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
577{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800578 AutoMutex lock(mLock);
579 return setLoop_l(loopStart, loopEnd, loopCount);
580}
581
582// must be called with mLock held
583status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
584{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 audio_track_cblk_t* cblk = mCblk;
586
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 Mutex::Autolock _l(cblk->lock);
588
589 if (loopCount == 0) {
590 cblk->loopStart = UINT_MAX;
591 cblk->loopEnd = UINT_MAX;
592 cblk->loopCount = 0;
593 mLoopCount = 0;
594 return NO_ERROR;
595 }
596
John Grossman4ff14ba2012-02-08 16:37:41 -0800597 if (mIsTimed) {
598 return INVALID_OPERATION;
599 }
600
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700602 loopEnd - loopStart > cblk->frameCount ||
603 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000604 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 -0800605 return BAD_VALUE;
606 }
607
Eric Laurent9b7d9502011-03-21 11:49:00 -0700608 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000609 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700610 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700612 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613
614 cblk->loopStart = loopStart;
615 cblk->loopEnd = loopEnd;
616 cblk->loopCount = loopCount;
617 mLoopCount = loopCount;
618
619 return NO_ERROR;
620}
621
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622status_t AudioTrack::setMarkerPosition(uint32_t marker)
623{
Glenn Kastena0d68332012-01-27 16:47:15 -0800624 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625
626 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700627 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628
629 return NO_ERROR;
630}
631
Glenn Kastena5224f32012-01-04 12:41:44 -0800632status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633{
Glenn Kastena0d68332012-01-27 16:47:15 -0800634 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635
636 *marker = mMarkerPosition;
637
638 return NO_ERROR;
639}
640
641status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
642{
Glenn Kastena0d68332012-01-27 16:47:15 -0800643 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644
645 uint32_t curPosition;
646 getPosition(&curPosition);
647 mNewPosition = curPosition + updatePeriod;
648 mUpdatePeriod = updatePeriod;
649
650 return NO_ERROR;
651}
652
Glenn Kastena5224f32012-01-04 12:41:44 -0800653status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654{
Glenn Kastena0d68332012-01-27 16:47:15 -0800655 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656
657 *updatePeriod = mUpdatePeriod;
658
659 return NO_ERROR;
660}
661
662status_t AudioTrack::setPosition(uint32_t position)
663{
John Grossman4ff14ba2012-02-08 16:37:41 -0800664 if (mIsTimed) return INVALID_OPERATION;
665
Eric Laurent1703cdf2011-03-07 14:52:59 -0800666 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800668 if (!stopped_l()) return INVALID_OPERATION;
669
670 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671
672 if (position > mCblk->user) return BAD_VALUE;
673
674 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700675 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700676
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677 return NO_ERROR;
678}
679
680status_t AudioTrack::getPosition(uint32_t *position)
681{
Glenn Kastena0d68332012-01-27 16:47:15 -0800682 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800683 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700684 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685
686 return NO_ERROR;
687}
688
689status_t AudioTrack::reload()
690{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800691 AutoMutex lock(mLock);
692
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800693 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694
Eric Laurent1703cdf2011-03-07 14:52:59 -0800695 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696
Eric Laurentd1b449a2010-05-14 03:26:45 -0700697 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698
699 return NO_ERROR;
700}
701
Eric Laurentc2f1f072009-07-17 12:17:14 -0700702audio_io_handle_t AudioTrack::getOutput()
703{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800704 AutoMutex lock(mLock);
705 return getOutput_l();
706}
707
708// must be called with mLock held
709audio_io_handle_t AudioTrack::getOutput_l()
710{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800711 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800712 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700713}
714
Glenn Kastena5224f32012-01-04 12:41:44 -0800715int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700716{
717 return mSessionId;
718}
719
720status_t AudioTrack::attachAuxEffect(int effectId)
721{
Steve Block3856b092011-10-20 11:56:00 +0100722 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700723 status_t status = mAudioTrack->attachAuxEffect(effectId);
724 if (status == NO_ERROR) {
725 mAuxEffectId = effectId;
726 }
727 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700728}
729
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730// -------------------------------------------------------------------------
731
Eric Laurent1703cdf2011-03-07 14:52:59 -0800732// must be called with mLock held
733status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800734 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800735 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800736 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700737 uint32_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800738 int frameCount,
Glenn Kasten18868c52012-03-07 09:15:37 -0800739 audio_policy_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800740 const sp<IMemory>& sharedBuffer,
Eric Laurentd1b449a2010-05-14 03:26:45 -0700741 audio_io_handle_t output,
742 bool enforceFrameCount)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800743{
744 status_t status;
745 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
746 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700747 ALOGE("Could not get audioflinger");
748 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800749 }
750
Eric Laurentd1b449a2010-05-14 03:26:45 -0700751 int afSampleRate;
752 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
753 return NO_INIT;
754 }
755 int afFrameCount;
756 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
757 return NO_INIT;
758 }
759 uint32_t afLatency;
760 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
761 return NO_INIT;
762 }
763
764 mNotificationFramesAct = mNotificationFramesReq;
Dima Zavinfce7a472011-04-19 22:30:36 -0700765 if (!audio_is_linear_pcm(format)) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700766 if (sharedBuffer != 0) {
767 frameCount = sharedBuffer->size();
768 }
769 } else {
770 // Ensure that buffer depth covers at least audio hardware latency
771 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
772 if (minBufCount < 2) minBufCount = 2;
773
774 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
775
776 if (sharedBuffer == 0) {
777 if (frameCount == 0) {
778 frameCount = minFrameCount;
779 }
780 if (mNotificationFramesAct == 0) {
781 mNotificationFramesAct = frameCount/2;
782 }
783 // Make sure that application is notified with sufficient margin
784 // before underrun
785 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
786 mNotificationFramesAct = frameCount/2;
787 }
788 if (frameCount < minFrameCount) {
Eric Laurentd8d61852012-03-05 17:06:40 -0800789 ALOGW_IF(enforceFrameCount, "Minimum buffer size corrected from %d to %d",
790 frameCount, minFrameCount);
791 frameCount = minFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700792 }
793 } else {
Glenn Kasten99e53b82012-01-19 08:59:58 -0800794 // Ensure that buffer alignment matches channelCount
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700795 int channelCount = popcount(channelMask);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700796 if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000797 ALOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700798 return BAD_VALUE;
799 }
800 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
801 }
802 }
803
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800804 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
805 streamType,
806 sampleRate,
807 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700808 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800809 frameCount,
810 ((uint16_t)flags) << 16,
811 sharedBuffer,
812 output,
John Grossman4ff14ba2012-02-08 16:37:41 -0800813 mIsTimed,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700814 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800815 &status);
816
817 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000818 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800819 return status;
820 }
821 sp<IMemory> cblk = track->getCblk();
822 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000823 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800824 return NO_INIT;
825 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800826 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800827 mCblkMemory = cblk;
828 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Eric Laurent38ccae22011-03-28 18:37:07 -0700829 android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800830 if (sharedBuffer == 0) {
831 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
832 } else {
833 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700834 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -0700835 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800836 }
837
Glenn Kasten83d86532012-01-17 14:39:34 -0800838 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -0800839 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700840 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800841 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
842 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700843 mRemainingFrames = mNotificationFramesAct;
844 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800845 return NO_ERROR;
846}
847
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800848status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
849{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800850 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800851 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700852 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853 audio_track_cblk_t* cblk = mCblk;
854 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700855 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856
857 audioBuffer->frameCount = 0;
858 audioBuffer->size = 0;
859
860 uint32_t framesAvail = cblk->framesAvailable();
861
Eric Laurent9b7d9502011-03-21 11:49:00 -0700862 cblk->lock.lock();
863 if (cblk->flags & CBLK_INVALID_MSK) {
864 goto create_new_track;
865 }
866 cblk->lock.unlock();
867
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800868 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800869 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870 goto start_loop_here;
871 while (framesAvail == 0) {
872 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800873 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100874 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800875 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800876 return NO_MORE_BUFFERS;
877 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800878 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800879 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800880 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800881 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700882 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800883 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -0700884 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700885 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800886 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800887 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800888 return status_t(STOPPED);
889 }
890 cblk->lock.lock();
891 }
892
893 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700894 goto create_new_track;
895 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800896 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700897 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
899 // timing out when a loop has been set and we have already written upto loop end
900 // is a normal condition: no need to wake AudioFlinger up.
901 if (cblk->user < cblk->loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000902 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p "
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903 "user=%08x, server=%08x", this, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700904 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800905 cblk->lock.unlock();
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800906 result = mAudioTrack->start(0); // callback thread hasn't changed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800907 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800908 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700909 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800910create_new_track:
911 result = restoreTrack_l(cblk, false);
912 }
913 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000914 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800915 cblk->lock.unlock();
916 return result;
917 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 }
919 cblk->waitTimeMs = 0;
920 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700921
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800922 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800923 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800924 return TIMED_OUT;
925 }
926 }
927 // read the server count again
928 start_loop_here:
929 framesAvail = cblk->framesAvailable_l();
930 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800931 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932 }
933
Eric Laurent44d98482010-09-30 16:12:31 -0700934 // restart track if it was disabled by audioflinger due to previous underrun
Eric Laurent1703cdf2011-03-07 14:52:59 -0800935 if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700936 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +0000937 ALOGW("obtainBuffer() track %p disabled, restarting", this);
Glenn Kasten6dbc1352012-02-02 10:56:47 -0800938 mAudioTrack->start(0); // callback thread hasn't changed
Eric Laurent44d98482010-09-30 16:12:31 -0700939 }
940
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800941 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700942
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943 if (framesReq > framesAvail) {
944 framesReq = framesAvail;
945 }
946
947 uint32_t u = cblk->user;
948 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
949
950 if (u + framesReq > bufferEnd) {
951 framesReq = bufferEnd - u;
952 }
953
Eric Laurentc2f1f072009-07-17 12:17:14 -0700954 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
955 audioBuffer->channelCount = mChannelCount;
956 audioBuffer->frameCount = framesReq;
957 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -0700958 if (audio_is_linear_pcm(mFormat)) {
959 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960 } else {
961 audioBuffer->format = mFormat;
962 }
963 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 active = mActive;
965 return active ? status_t(NO_ERROR) : status_t(STOPPED);
966}
967
968void AudioTrack::releaseBuffer(Buffer* audioBuffer)
969{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800970 AutoMutex lock(mLock);
971 mCblk->stepUser(audioBuffer->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972}
973
974// -------------------------------------------------------------------------
975
976ssize_t AudioTrack::write(const void* buffer, size_t userSize)
977{
978
979 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -0800980 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981
982 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -0800983 // Sanity-check: user is most-likely passing an error code, and it would
984 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +0000985 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 buffer, userSize, userSize);
987 return BAD_VALUE;
988 }
989
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991
Eric Laurent1703cdf2011-03-07 14:52:59 -0800992 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
993 // while we are accessing the cblk
994 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700995 sp<IAudioTrack> audioTrack = mAudioTrack;
996 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800997 mLock.unlock();
998
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800999 ssize_t written = 0;
1000 const int8_t *src = (const int8_t *)buffer;
1001 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001002 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003
1004 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001005 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001006
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 status_t err = obtainBuffer(&audioBuffer, -1);
1008 if (err < 0) {
1009 // out of buffers, return #bytes written
1010 if (err == status_t(NO_MORE_BUFFERS))
1011 break;
1012 return ssize_t(err);
1013 }
1014
1015 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001016
Dima Zavinfce7a472011-04-19 22:30:36 -07001017 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001018 // Divide capacity by 2 to take expansion into account
1019 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001020 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001021 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001022 toWrite = audioBuffer.size;
1023 memcpy(audioBuffer.i8, src, toWrite);
1024 src += toWrite;
1025 }
1026 userSize -= toWrite;
1027 written += toWrite;
1028
1029 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001030 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001031
1032 return written;
1033}
1034
1035// -------------------------------------------------------------------------
1036
John Grossman4ff14ba2012-02-08 16:37:41 -08001037TimedAudioTrack::TimedAudioTrack() {
1038 mIsTimed = true;
1039}
1040
1041status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1042{
1043 status_t result = UNKNOWN_ERROR;
1044
1045 // If the track is not invalid already, try to allocate a buffer. alloc
1046 // fails indicating that the server is dead, flag the track as invalid so
1047 // we can attempt to restore in in just a bit.
1048 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1049 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1050 if (result == DEAD_OBJECT) {
1051 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1052 }
1053 }
1054
1055 // If the track is invalid at this point, attempt to restore it. and try the
1056 // allocation one more time.
1057 if (mCblk->flags & CBLK_INVALID_MSK) {
1058 mCblk->lock.lock();
1059 result = restoreTrack_l(mCblk, false);
1060 mCblk->lock.unlock();
1061
1062 if (result == OK)
1063 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1064 }
1065
1066 return result;
1067}
1068
1069status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1070 int64_t pts)
1071{
1072 // restart track if it was disabled by audioflinger due to previous underrun
1073 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1074 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1075 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1076 mAudioTrack->start(0);
1077 }
1078
1079 return mAudioTrack->queueTimedBuffer(buffer, pts);
1080}
1081
1082status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1083 TargetTimeline target)
1084{
1085 return mAudioTrack->setMediaTimeTransform(xform, target);
1086}
1087
1088// -------------------------------------------------------------------------
1089
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001090bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1091{
1092 Buffer audioBuffer;
1093 uint32_t frames;
1094 size_t writtenSize;
1095
Eric Laurent1703cdf2011-03-07 14:52:59 -08001096 mLock.lock();
1097 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1098 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001099 sp<IAudioTrack> audioTrack = mAudioTrack;
1100 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001101 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001102 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001103 mLock.unlock();
1104
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001105 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001106 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001108 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001109 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001110 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001111 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001112 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001113 if (mSharedBuffer != 0) return false;
1114 }
1115 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001116
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001117 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001118 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001119 int loopCount = -1;
1120 mLoopCount--;
1121 if (mLoopCount >= 0) loopCount = mLoopCount;
1122
1123 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1124 }
1125
1126 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001127 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001128 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001129 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001130 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001131 }
1132 }
1133
1134 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001135 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001136 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001137 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1138 mNewPosition += mUpdatePeriod;
1139 }
1140 }
1141
1142 // If Shared buffer is used, no data is requested from client.
1143 if (mSharedBuffer != 0) {
1144 frames = 0;
1145 } else {
1146 frames = mRemainingFrames;
1147 }
1148
Glenn Kasten99e53b82012-01-19 08:59:58 -08001149 // See description of waitCount parameter at declaration of obtainBuffer().
1150 // The logic below prevents us from being stuck below at obtainBuffer()
1151 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001152 int32_t waitCount = -1;
1153 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1154 waitCount = 1;
1155 }
1156
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001157 do {
1158
1159 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001160
Eric Laurent2267ba12011-09-07 11:13:23 -07001161 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001162 if (err < NO_ERROR) {
1163 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001164 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 -08001165 return false;
1166 }
1167 break;
1168 }
1169 if (err == status_t(STOPPED)) return false;
1170
1171 // Divide buffer size by 2 to take into account the expansion
1172 // due to 8 to 16 bit conversion: the callback must fill only half
1173 // of the destination buffer
Dima Zavinfce7a472011-04-19 22:30:36 -07001174 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001175 audioBuffer.size >>= 1;
1176 }
1177
1178 size_t reqSize = audioBuffer.size;
1179 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1180 writtenSize = audioBuffer.size;
1181
1182 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001183 if (ssize_t(writtenSize) <= 0) {
1184 // The callback is done filling buffers
1185 // Keep this thread going to handle timed events and
1186 // still try to get more data in intervals of WAIT_PERIOD_MS
1187 // but don't just loop and block the CPU, so wait
1188 usleep(WAIT_PERIOD_MS*1000);
1189 break;
1190 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001191 if (writtenSize > reqSize) writtenSize = reqSize;
1192
Dima Zavinfce7a472011-04-19 22:30:36 -07001193 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001194 // 8 to 16 bit conversion, note that source and destination are the same address
1195 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001196 writtenSize <<= 1;
1197 }
1198
1199 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001200 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001201 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001202 // 16 bit.
1203 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1204
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 frames -= audioBuffer.frameCount;
1206
1207 releaseBuffer(&audioBuffer);
1208 }
1209 while (frames);
1210
1211 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001212 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 } else {
1214 mRemainingFrames = frames;
1215 }
1216 return true;
1217}
1218
Eric Laurent1703cdf2011-03-07 14:52:59 -08001219// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1220// the IAudioTrack and IMemory in case they are recreated here.
1221// If the IAudioTrack is successfully restored, the cblk pointer is updated
1222status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1223{
1224 status_t result;
1225
Eric Laurent38ccae22011-03-28 18:37:07 -07001226 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001227 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001228 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001229
Eric Laurent1703cdf2011-03-07 14:52:59 -08001230 // signal old cblk condition so that other threads waiting for available buffers stop
1231 // waiting now
1232 cblk->cv.broadcast();
1233 cblk->lock.unlock();
1234
Eric Laurent9f6530f2011-08-30 10:18:54 -07001235 // refresh the audio configuration cache in this process to make sure we get new
1236 // output parameters in getOutput_l() and createTrack_l()
1237 AudioSystem::clearAudioConfigCache();
1238
Eric Laurent1703cdf2011-03-07 14:52:59 -08001239 // if the new IAudioTrack is created, createTrack_l() will modify the
1240 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1241 // It will also delete the strong references on previous IAudioTrack and IMemory
1242 result = createTrack_l(mStreamType,
1243 cblk->sampleRate,
1244 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001245 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001246 mFrameCount,
1247 mFlags,
1248 mSharedBuffer,
1249 getOutput_l(),
1250 false);
1251
1252 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001253 uint32_t user = cblk->user;
1254 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001255 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001256 mCblk->user = user;
1257 mCblk->server = user;
1258 mCblk->userBase = user;
1259 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001260 // restore loop: this is not guaranteed to succeed if new frame count is not
1261 // compatible with loop length
1262 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001263 if (!fromStart) {
1264 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001265 // Make sure that a client relying on callback events indicating underrun or
1266 // the actual amount of audio frames played (e.g SoundPool) receives them.
1267 if (mSharedBuffer == 0) {
1268 uint32_t frames = 0;
1269 if (user > server) {
1270 frames = ((user - server) > mCblk->frameCount) ?
1271 mCblk->frameCount : (user - server);
1272 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1273 }
1274 // restart playback even if buffer is not completely filled.
1275 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1276 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1277 // the client
1278 mCblk->stepUser(frames);
1279 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001280 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001281 if (mActive) {
Glenn Kasten6dbc1352012-02-02 10:56:47 -08001282 result = mAudioTrack->start(0); // callback thread hasn't changed
Steve Block5ff1dd52012-01-05 23:22:43 +00001283 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001284 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001285 if (fromStart && result == NO_ERROR) {
1286 mNewPosition = mCblk->server + mUpdatePeriod;
1287 }
1288 }
1289 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001290 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001291 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001292 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001293 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001294 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001295 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001296 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001297 } else {
1298 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001299 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001300 mLock.unlock();
1301 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001302 if (result == NO_ERROR) {
1303 result = mRestoreStatus;
1304 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001305 cblk->lock.unlock();
1306 mLock.lock();
1307 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001308 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001309 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001310 cblk->lock.unlock();
1311 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001312 }
Steve Block3856b092011-10-20 11:56:00 +01001313 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001314 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001315
1316 if (result == NO_ERROR) {
1317 // from now on we switch to the newly created cblk
1318 cblk = mCblk;
1319 }
1320 cblk->lock.lock();
1321
Steve Block5ff1dd52012-01-05 23:22:43 +00001322 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001323
1324 return result;
1325}
1326
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001327status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1328{
1329
1330 const size_t SIZE = 256;
1331 char buffer[SIZE];
1332 String8 result;
1333
1334 result.append(" AudioTrack::dump\n");
1335 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1336 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001337 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 -08001338 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001339 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 -08001340 result.append(buffer);
1341 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1342 result.append(buffer);
1343 ::write(fd, result.string(), result.size());
1344 return NO_ERROR;
1345}
1346
1347// =========================================================================
1348
1349AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
1350 : Thread(bCanCallJava), mReceiver(receiver)
1351{
1352}
1353
1354bool AudioTrack::AudioTrackThread::threadLoop()
1355{
1356 return mReceiver.processAudioBuffer(this);
1357}
1358
1359status_t AudioTrack::AudioTrackThread::readyToRun()
1360{
1361 return NO_ERROR;
1362}
1363
1364void AudioTrack::AudioTrackThread::onFirstRef()
1365{
1366}
1367
1368// =========================================================================
1369
Eric Laurent38ccae22011-03-28 18:37:07 -07001370
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001371audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001372 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001373 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001374 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001375 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001376{
1377}
1378
1379uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1380{
Glenn Kasten90548972011-12-13 11:45:07 -08001381 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001382
1383 u += frameCount;
1384 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001385 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001386 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1387 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1388 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1389 }
Glenn Kasten90548972011-12-13 11:45:07 -08001390 } else if (u > server) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001391 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001392 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001393 }
1394
1395 if (u >= userBase + this->frameCount) {
1396 userBase += this->frameCount;
1397 }
1398
Glenn Kasten90548972011-12-13 11:45:07 -08001399 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001400
1401 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001402 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001403 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001404 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405
1406 return u;
1407}
1408
1409bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1410{
Eric Laurent38ccae22011-03-28 18:37:07 -07001411 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001412 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001413 return false;
1414 }
1415
Glenn Kasten90548972011-12-13 11:45:07 -08001416 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001417
1418 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001419 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 // Mark that we have read the first buffer so that next time stepUser() is called
1421 // we switch to normal obtainBuffer() timeout period
1422 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001423 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001424 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425 // It is possible that we receive a flush()
1426 // while the mixer is processing a block: in this case,
1427 // stepServer() is called After the flush() has reset u & s and
1428 // we have s > u
Glenn Kasten90548972011-12-13 11:45:07 -08001429 if (s > user) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001430 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001431 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432 }
1433 }
1434
1435 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001436 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437 s = loopStart;
1438 if (--loopCount == 0) {
1439 loopEnd = UINT_MAX;
1440 loopStart = UINT_MAX;
1441 }
1442 }
1443 if (s >= serverBase + this->frameCount) {
1444 serverBase += this->frameCount;
1445 }
1446
Glenn Kasten90548972011-12-13 11:45:07 -08001447 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001448
Eric Laurent1703cdf2011-03-07 14:52:59 -08001449 if (!(flags & CBLK_INVALID_MSK)) {
1450 cv.signal();
1451 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 lock.unlock();
1453 return true;
1454}
1455
1456void* audio_track_cblk_t::buffer(uint32_t offset) const
1457{
Glenn Kasten90548972011-12-13 11:45:07 -08001458 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001459}
1460
1461uint32_t audio_track_cblk_t::framesAvailable()
1462{
1463 Mutex::Autolock _l(lock);
1464 return framesAvailable_l();
1465}
1466
1467uint32_t audio_track_cblk_t::framesAvailable_l()
1468{
Glenn Kasten90548972011-12-13 11:45:07 -08001469 uint32_t u = user;
1470 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001471
Eric Laurentd1b449a2010-05-14 03:26:45 -07001472 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473 uint32_t limit = (s < loopStart) ? s : loopStart;
1474 return limit + frameCount - u;
1475 } else {
1476 return frameCount + u - s;
1477 }
1478}
1479
1480uint32_t audio_track_cblk_t::framesReady()
1481{
Glenn Kasten90548972011-12-13 11:45:07 -08001482 uint32_t u = user;
1483 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001484
Eric Laurentd1b449a2010-05-14 03:26:45 -07001485 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486 if (u < loopEnd) {
1487 return u - s;
1488 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001489 // do not block on mutex shared with client on AudioFlinger side
1490 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001491 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001492 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001493 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001494 uint32_t frames = UINT_MAX;
1495 if (loopCount >= 0) {
1496 frames = (loopEnd - loopStart)*loopCount + u - s;
1497 }
1498 lock.unlock();
1499 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500 }
1501 } else {
1502 return s - u;
1503 }
1504}
1505
Eric Laurent38ccae22011-03-28 18:37:07 -07001506bool audio_track_cblk_t::tryLock()
1507{
1508 // the code below simulates lock-with-timeout
1509 // we MUST do this to protect the AudioFlinger server
1510 // as this lock is shared with the client.
1511 status_t err;
1512
1513 err = lock.tryLock();
1514 if (err == -EBUSY) { // just wait a bit
1515 usleep(1000);
1516 err = lock.tryLock();
1517 }
1518 if (err != NO_ERROR) {
1519 // probably, the client just died.
1520 return false;
1521 }
1522 return true;
1523}
1524
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001525// -------------------------------------------------------------------------
1526
1527}; // namespace android