blob: 0be553482ca7bed1d6e28496222e1a2c203a86d8 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
26#include <sched.h>
27#include <sys/resource.h>
28
29#include <private/media/AudioTrackShared.h>
30
31#include <media/AudioSystem.h>
32#include <media/AudioTrack.h>
33
34#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Glenn Kasten511754b2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080053 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 uint32_t sampleRate)
56{
Glenn Kasten04cd0182012-06-25 11:49:27 -070057 if (frameCount == NULL) return BAD_VALUE;
58
59 // default to 0 in case of error
60 *frameCount = 0;
61
Glenn Kastene0fa4672012-04-24 14:35:14 -070062 // FIXME merge with similar code in createTrack_l(), except we're missing
63 // some information here that is available in createTrack_l():
64 // audio_io_handle_t output
65 // audio_format_t format
66 // audio_channel_mask_t channelMask
67 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080068 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080069 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
70 return NO_INIT;
71 }
Glenn Kastene33054e2012-11-14 12:54:39 -080072 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080073 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 uint32_t afLatency;
77 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80
81 // Ensure that buffer depth covers at least audio hardware latency
82 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
83 if (minBufCount < 2) minBufCount = 2;
84
85 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070086 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080087 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
88 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080089 return NO_ERROR;
90}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92// ---------------------------------------------------------------------------
93
94AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070095 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080096 mIsTimed(false),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070098 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099{
100}
101
102AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800103 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800105 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700106 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700108 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 callback_t cbf,
110 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700111 int notificationFrames,
112 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700113 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800114 mIsTimed(false),
115 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700116 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700118 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700119 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800120 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121}
122
Andreas Huberc8139852012-01-18 10:51:55 -0800123AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800124 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800126 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700127 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800128 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700129 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 callback_t cbf,
131 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700132 int notificationFrames,
133 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700134 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800135 mIsTimed(false),
136 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700137 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700139 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800140 0 /*frameCount*/, flags, cbf, user, notificationFrames,
141 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142}
143
144AudioTrack::~AudioTrack()
145{
Steve Block3856b092011-10-20 11:56:00 +0100146 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147
148 if (mStatus == NO_ERROR) {
149 // Make sure that callback function exits in the case where
150 // it is looping on buffer full condition in obtainBuffer().
151 // Otherwise the callback thread will never exit.
152 stop();
153 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800154 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 mAudioTrackThread->requestExitAndWait();
156 mAudioTrackThread.clear();
157 }
158 mAudioTrack.clear();
159 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700160 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162}
163
164status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800165 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800167 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700168 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800169 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700170 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 callback_t cbf,
172 void* user,
173 int notificationFrames,
174 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700175 bool threadCanCallJava,
176 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
Glenn Kastene33054e2012-11-14 12:54:39 -0800178 // FIXME "int" here is legacy and will be replaced by size_t later
179 if (frameCountInt < 0) {
180 ALOGE("Invalid frame count %d", frameCountInt);
181 return BAD_VALUE;
182 }
183 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700185 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
186 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187
Glenn Kastene33054e2012-11-14 12:54:39 -0800188 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700189
Eric Laurent1703cdf2011-03-07 14:52:59 -0800190 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700191 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000192 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 return INVALID_OPERATION;
194 }
195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700197 if (streamType == AUDIO_STREAM_DEFAULT) {
198 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700200
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800202 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700203 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
204 return NO_INIT;
205 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 sampleRate = afSampleRate;
207 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700208
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800210 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700213 if (channelMask == 0) {
214 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 }
216
217 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000219 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 return BAD_VALUE;
221 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700222
Glenn Kastene0fa4672012-04-24 14:35:14 -0700223 // AudioFlinger does not currently support 8-bit data in shared memory
224 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
225 ALOGE("8-bit data in shared memory is not supported");
226 return BAD_VALUE;
227 }
228
Eric Laurentc2f1f072009-07-17 12:17:14 -0700229 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700230 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700231 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800232 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700233 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700235 // only allow deep buffering for music stream type
236 if (streamType != AUDIO_STREAM_MUSIC) {
237 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
238 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700239
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700240 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700241 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700242 return BAD_VALUE;
243 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700244 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700245
Dima Zavinfce7a472011-04-19 22:30:36 -0700246 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800247 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800248 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800249 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700250
251 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000252 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 return BAD_VALUE;
254 }
255
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800256 mVolume[LEFT] = 1.0f;
257 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800258 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700259 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800260 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700261 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700262 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700263 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700264 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700265 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700266
Glenn Kastena997e7a2012-08-07 09:44:19 -0700267 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700268 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700269 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
270 }
271
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800272 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800273 status_t status = createTrack_l(streamType,
274 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800275 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700276 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800277 frameCount,
278 flags,
279 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700280 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800281
Glenn Kastena997e7a2012-08-07 09:44:19 -0700282 if (status != NO_ERROR) {
283 if (mAudioTrackThread != 0) {
284 mAudioTrackThread->requestExit();
285 mAudioTrackThread.clear();
286 }
287 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700288 }
289
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290 mStatus = NO_ERROR;
291
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800293 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700294 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 mChannelCount = channelCount;
Glenn Kasten83a03822012-11-12 07:58:20 -0800296
297 if (audio_is_linear_pcm(format)) {
298 mFrameSize = channelCount * audio_bytes_per_sample(format);
299 mFrameSizeAF = channelCount * sizeof(int16_t);
300 } else {
301 mFrameSize = sizeof(uint8_t);
302 mFrameSizeAF = sizeof(uint8_t);
303 }
304
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 mSharedBuffer = sharedBuffer;
306 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800307 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 mLoopCount = 0;
310 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700311 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 mNewPosition = 0;
313 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700314 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700315 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 return NO_ERROR;
317}
318
319status_t AudioTrack::initCheck() const
320{
321 return mStatus;
322}
323
324// -------------------------------------------------------------------------
325
326uint32_t AudioTrack::latency() const
327{
328 return mLatency;
329}
330
Glenn Kastenfff6d712012-01-12 16:38:12 -0800331audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332{
333 return mStreamType;
334}
335
Glenn Kastene1c39622012-01-04 09:36:37 -0800336audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337{
338 return mFormat;
339}
340
341int AudioTrack::channelCount() const
342{
343 return mChannelCount;
344}
345
Glenn Kastene33054e2012-11-14 12:54:39 -0800346size_t AudioTrack::frameCount() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347{
Glenn Kastenb6037442012-11-14 13:42:25 -0800348 return mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349}
350
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351sp<IMemory>& AudioTrack::sharedBuffer()
352{
353 return mSharedBuffer;
354}
355
356// -------------------------------------------------------------------------
357
358void AudioTrack::start()
359{
360 sp<AudioTrackThread> t = mAudioTrackThread;
361
Steve Block3856b092011-10-20 11:56:00 +0100362 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363
Eric Laurentf5aafb22010-11-18 08:40:16 -0800364 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800365 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
366 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700367 sp<IAudioTrack> audioTrack = mAudioTrack;
368 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800369 audio_track_cblk_t* cblk = mCblk;
370
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800371 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700372 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800373 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800374 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700375 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800376 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
377 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800378 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800379 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800380 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800381 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700382 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700383 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700384 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800385 }
386
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700387 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700388 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800389 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800390 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800391 ALOGV("mAudioTrack->start()");
392 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800393 cblk->lock.lock();
394 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800395 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800396 }
Eric Laurent2b584242009-11-09 23:32:22 -0800397 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800398 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700399 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800400 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700401 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800402 }
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) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800408 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800409 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700410 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700411 set_sched_policy(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) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800441 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700443 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700444 set_sched_policy(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;
Eric Laurent192cbba2012-06-13 08:38:36 -0700487 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 mAudioTrack->pause();
489 }
490}
491
492void AudioTrack::mute(bool e)
493{
494 mAudioTrack->mute(e);
495 mMuted = e;
496}
497
498bool AudioTrack::muted() const
499{
500 return mMuted;
501}
502
Eric Laurentbe916aa2010-06-01 23:49:17 -0700503status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800505 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700506 return BAD_VALUE;
507 }
508
Eric Laurent1703cdf2011-03-07 14:52:59 -0800509 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 mVolume[LEFT] = left;
511 mVolume[RIGHT] = right;
512
Glenn Kasten83d86532012-01-17 14:39:34 -0800513 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700514
515 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516}
517
Glenn Kastenb1c09932012-02-27 16:21:04 -0800518status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800520 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700521}
522
Eric Laurent2beeb502010-07-16 07:43:46 -0700523status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700524{
Steve Block3856b092011-10-20 11:56:00 +0100525 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800526 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700527 return BAD_VALUE;
528 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800529 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700530
531 mSendLevel = level;
532
Glenn Kasten05632a52012-01-03 14:22:33 -0800533 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700534
535 return NO_ERROR;
536}
537
Glenn Kastena5224f32012-01-04 12:41:44 -0800538void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700539{
540 if (level != NULL) {
541 *level = mSendLevel;
542 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543}
544
Glenn Kasten3b16c762012-11-14 08:44:39 -0800545status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546{
Glenn Kasten3b16c762012-11-14 08:44:39 -0800547 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548
John Grossman4ff14ba2012-02-08 16:37:41 -0800549 if (mIsTimed) {
550 return INVALID_OPERATION;
551 }
552
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700554 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 }
556 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kasten3b16c762012-11-14 08:44:39 -0800557 if (rate == 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558
Eric Laurent1703cdf2011-03-07 14:52:59 -0800559 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700560 mCblk->sampleRate = rate;
561 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562}
563
Glenn Kastena5224f32012-01-04 12:41:44 -0800564uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565{
John Grossman4ff14ba2012-02-08 16:37:41 -0800566 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800567 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800568 }
569
Eric Laurent1703cdf2011-03-07 14:52:59 -0800570 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700571 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572}
573
574status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
575{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800576 AutoMutex lock(mLock);
577 return setLoop_l(loopStart, loopEnd, loopCount);
578}
579
580// must be called with mLock held
581status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
582{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583 audio_track_cblk_t* cblk = mCblk;
584
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585 Mutex::Autolock _l(cblk->lock);
586
587 if (loopCount == 0) {
588 cblk->loopStart = UINT_MAX;
589 cblk->loopEnd = UINT_MAX;
590 cblk->loopCount = 0;
591 mLoopCount = 0;
592 return NO_ERROR;
593 }
594
John Grossman4ff14ba2012-02-08 16:37:41 -0800595 if (mIsTimed) {
596 return INVALID_OPERATION;
597 }
598
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 if (loopStart >= loopEnd ||
Glenn Kastenb6037442012-11-14 13:42:25 -0800600 loopEnd - loopStart > mFrameCount ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700601 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700602 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
Glenn Kastenb6037442012-11-14 13:42:25 -0800603 "user %d", loopStart, loopEnd, loopCount, mFrameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 return BAD_VALUE;
605 }
606
Glenn Kastenb6037442012-11-14 13:42:25 -0800607 if ((mSharedBuffer != 0) && (loopEnd > mFrameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700608 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
609 "framecount %d",
Glenn Kastenb6037442012-11-14 13:42:25 -0800610 loopStart, loopEnd, mFrameCount);
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
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700670 audio_track_cblk_t* cblk = mCblk;
671 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700673 if (position > cblk->user) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700675 cblk->server = position;
676 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700677
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678 return NO_ERROR;
679}
680
681status_t AudioTrack::getPosition(uint32_t *position)
682{
Glenn Kastena0d68332012-01-27 16:47:15 -0800683 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800684 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700685 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686
687 return NO_ERROR;
688}
689
690status_t AudioTrack::reload()
691{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800692 AutoMutex lock(mLock);
693
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800694 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700695
Eric Laurent1703cdf2011-03-07 14:52:59 -0800696 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700698 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800699 cblk->stepUserOut(mFrameCount, mFrameCount);
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,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700739 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800740 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700741 audio_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 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700753 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700754 return NO_INIT;
755 }
756
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700757 // Client decides whether the track is TIMED (see below), but can only express a preference
758 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700759 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700760 // either of these use cases:
761 // use case 1: shared buffer
762 (sharedBuffer != 0) ||
763 // use case 2: callback handler
764 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800765 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700766 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700767 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700768 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700769 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700770 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700771
Eric Laurentd1b449a2010-05-14 03:26:45 -0700772 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700773
Dima Zavinfce7a472011-04-19 22:30:36 -0700774 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700775
Eric Laurentd1b449a2010-05-14 03:26:45 -0700776 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700777 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700778 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700779 } else if (frameCount == 0) {
Glenn Kastene33054e2012-11-14 12:54:39 -0800780 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700781 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
782 return NO_INIT;
783 }
784 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700785 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700786
787 } else if (sharedBuffer != 0) {
788
789 // Ensure that buffer alignment matches channelCount
790 int channelCount = popcount(channelMask);
791 // 8-bit data in shared memory is not currently supported by AudioFlinger
792 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
793 if (channelCount > 1) {
794 // More than 2 channels does not require stronger alignment than stereo
795 alignment <<= 1;
796 }
797 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
798 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
799 sharedBuffer->pointer(), channelCount);
800 return BAD_VALUE;
801 }
802
803 // When initializing a shared buffer AudioTrack via constructors,
804 // there's no frameCount parameter.
805 // But when initializing a shared buffer AudioTrack via set(),
806 // there _is_ a frameCount parameter. We silently ignore it.
807 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
808
809 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
810
811 // FIXME move these calculations and associated checks to server
Glenn Kasten3b16c762012-11-14 08:44:39 -0800812 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700813 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
814 return NO_INIT;
815 }
Glenn Kastene33054e2012-11-14 12:54:39 -0800816 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700817 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
818 return NO_INIT;
819 }
820
Eric Laurentd1b449a2010-05-14 03:26:45 -0700821 // Ensure that buffer depth covers at least audio hardware latency
822 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
823 if (minBufCount < 2) minBufCount = 2;
824
Glenn Kastene33054e2012-11-14 12:54:39 -0800825 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
826 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800827 ", afLatency=%d",
828 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700829
830 if (frameCount == 0) {
831 frameCount = minFrameCount;
832 }
833 if (mNotificationFramesAct == 0) {
834 mNotificationFramesAct = frameCount/2;
835 }
836 // Make sure that application is notified with sufficient margin
837 // before underrun
Glenn Kastene33054e2012-11-14 12:54:39 -0800838 if (mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700839 mNotificationFramesAct = frameCount/2;
840 }
841 if (frameCount < minFrameCount) {
842 // not ALOGW because it happens all the time when playing key clicks over A2DP
843 ALOGV("Minimum buffer size corrected from %d to %d",
844 frameCount, minFrameCount);
845 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800846 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700847
Glenn Kastene0fa4672012-04-24 14:35:14 -0700848 } else {
849 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700850 }
851
Glenn Kastena075db42012-03-06 11:22:44 -0800852 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
853 if (mIsTimed) {
854 trackFlags |= IAudioFlinger::TRACK_TIMED;
855 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800856
857 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700858 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700859 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800860 if (mAudioTrackThread != 0) {
861 tid = mAudioTrackThread->getTid();
862 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700863 }
864
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800865 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
866 streamType,
867 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700868 // AudioFlinger only sees 16-bit PCM
869 format == AUDIO_FORMAT_PCM_8_BIT ?
870 AUDIO_FORMAT_PCM_16_BIT : format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700871 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800872 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800873 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800874 sharedBuffer,
875 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800876 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700877 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800878 &status);
879
880 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000881 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800882 return status;
883 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700884 sp<IMemory> iMem = track->getCblk();
885 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000886 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800887 return NO_INIT;
888 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800889 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700890 mCblkMemory = iMem;
891 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
892 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800893 size_t temp = cblk->frameCount_;
894 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
895 // In current design, AudioTrack client checks and ensures frame count validity before
896 // passing it to AudioFlinger so AudioFlinger should not return a different value except
897 // for fast track as it uses a special method of assigning frame count.
898 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
899 }
900 frameCount = temp;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800901 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800902 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800903 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800904 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -0800905 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700906 // once denied, do not request again if IAudioTrack is re-created
907 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
908 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800909 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910 if (sharedBuffer == 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800911 mNotificationFramesAct = frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800913 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800914 if (sharedBuffer == 0) {
Glenn Kastenb929e412012-11-08 12:13:58 -0800915 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800916 } else {
Glenn Kastenb929e412012-11-08 12:13:58 -0800917 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700918 // Force buffer full condition as data is already present in shared memory
Glenn Kastenb6037442012-11-14 13:42:25 -0800919 cblk->stepUserOut(frameCount, frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800920 }
921
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700922 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700923 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700924 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700925 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700926 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
927 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700928 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700929 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -0800930 mLatency = afLatency + (1000*frameCount) / sampleRate;
931 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700932 // If IAudioTrack is re-created, don't let the requested frameCount
933 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -0800934 if (frameCount > mReqFrameCount) {
935 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700936 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800937 return NO_ERROR;
938}
939
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800940status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
941{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800942 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800943 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700944 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 audio_track_cblk_t* cblk = mCblk;
946 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700947 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800948
949 audioBuffer->frameCount = 0;
950 audioBuffer->size = 0;
951
Glenn Kastenb6037442012-11-14 13:42:25 -0800952 uint32_t framesAvail = cblk->framesAvailableOut(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953
Eric Laurent9b7d9502011-03-21 11:49:00 -0700954 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800955 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700956 goto create_new_track;
957 }
958 cblk->lock.unlock();
959
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800960 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800961 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 goto start_loop_here;
963 while (framesAvail == 0) {
964 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800965 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800967 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800968 return NO_MORE_BUFFERS;
969 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800970 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800971 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800973 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800974 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800975 mLock.unlock();
Glenn Kastena47f3162012-11-07 10:13:08 -0800976 // this condition is in shared memory, so if IAudioTrack and control block
977 // are replaced due to mediaserver death or IAudioTrack invalidation then
978 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700979 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700980 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800981 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800982 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800983 return status_t(STOPPED);
984 }
Glenn Kastena47f3162012-11-07 10:13:08 -0800985 // IAudioTrack may have been re-created while mLock was unlocked
986 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800987 cblk->lock.lock();
988 }
989
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800990 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700991 goto create_new_track;
992 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800993 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700994 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
996 // timing out when a loop has been set and we have already written upto loop end
997 // is a normal condition: no need to wake AudioFlinger up.
998 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700999 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
1000 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001001 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001002 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001003 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001004 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001005 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001006 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001007create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001008 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001009 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001010 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001011 }
1012 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001013 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001014 cblk->lock.unlock();
1015 return result;
1016 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001017 }
1018 cblk->waitTimeMs = 0;
1019 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001020
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001022 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001023 return TIMED_OUT;
1024 }
1025 }
1026 // read the server count again
1027 start_loop_here:
Glenn Kastenb6037442012-11-14 13:42:25 -08001028 framesAvail = cblk->framesAvailableOut_l(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001029 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001030 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001031 }
1032
1033 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001034
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001035 if (framesReq > framesAvail) {
1036 framesReq = framesAvail;
1037 }
1038
1039 uint32_t u = cblk->user;
Glenn Kastenb6037442012-11-14 13:42:25 -08001040 uint32_t bufferEnd = cblk->userBase + mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001041
Marco Nelissena1472d92012-03-30 14:36:54 -07001042 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001043 framesReq = bufferEnd - u;
1044 }
1045
Eric Laurentc2f1f072009-07-17 12:17:14 -07001046 audioBuffer->frameCount = framesReq;
Glenn Kasten83a03822012-11-12 07:58:20 -08001047 audioBuffer->size = framesReq * mFrameSizeAF;
1048 audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001049 active = mActive;
1050 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1051}
1052
1053void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1054{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001055 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001056 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001057 cblk->stepUserOut(audioBuffer->frameCount, mFrameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001058 if (audioBuffer->frameCount > 0) {
1059 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001060 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1061 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1062 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001063 mAudioTrack->start();
1064 }
1065 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001066}
1067
1068// -------------------------------------------------------------------------
1069
1070ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1071{
1072
1073 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001074 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001075
1076 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001077 // Sanity-check: user is most-likely passing an error code, and it would
1078 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001079 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001080 buffer, userSize, userSize);
1081 return BAD_VALUE;
1082 }
1083
Steve Block3856b092011-10-20 11:56:00 +01001084 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001085
Eric Laurentdf839842012-05-31 14:27:14 -07001086 if (userSize == 0) {
1087 return 0;
1088 }
1089
Eric Laurent1703cdf2011-03-07 14:52:59 -08001090 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1091 // while we are accessing the cblk
1092 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001093 sp<IAudioTrack> audioTrack = mAudioTrack;
1094 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001095 mLock.unlock();
1096
Glenn Kastena47f3162012-11-07 10:13:08 -08001097 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1098 // so all cblk references might still refer to old shared memory, but that should be benign
1099
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001100 ssize_t written = 0;
1101 const int8_t *src = (const int8_t *)buffer;
1102 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001103 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104
1105 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001106 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001107
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001108 status_t err = obtainBuffer(&audioBuffer, -1);
1109 if (err < 0) {
1110 // out of buffers, return #bytes written
1111 if (err == status_t(NO_MORE_BUFFERS))
1112 break;
1113 return ssize_t(err);
1114 }
1115
1116 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001117
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001118 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001119 // Divide capacity by 2 to take expansion into account
1120 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001121 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001122 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001123 toWrite = audioBuffer.size;
1124 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001125 }
Glenn Kastenbc0f6b92012-11-12 14:32:06 -08001126 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001127 userSize -= toWrite;
1128 written += toWrite;
1129
1130 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001131 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001132
1133 return written;
1134}
1135
1136// -------------------------------------------------------------------------
1137
John Grossman4ff14ba2012-02-08 16:37:41 -08001138TimedAudioTrack::TimedAudioTrack() {
1139 mIsTimed = true;
1140}
1141
1142status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1143{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001144 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001145 status_t result = UNKNOWN_ERROR;
1146
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001147 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1148 // while we are accessing the cblk
1149 sp<IAudioTrack> audioTrack = mAudioTrack;
1150 sp<IMemory> iMem = mCblkMemory;
1151
John Grossman4ff14ba2012-02-08 16:37:41 -08001152 // If the track is not invalid already, try to allocate a buffer. alloc
1153 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001154 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001155 audio_track_cblk_t* cblk = mCblk;
1156 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001157 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1158 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001159 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001160 }
1161 }
1162
1163 // If the track is invalid at this point, attempt to restore it. and try the
1164 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001165 if (cblk->flags & CBLK_INVALID) {
1166 cblk->lock.lock();
1167 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001168 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001169 cblk = temp;
1170 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001171
1172 if (result == OK)
1173 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1174 }
1175
1176 return result;
1177}
1178
1179status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1180 int64_t pts)
1181{
Eric Laurentdf839842012-05-31 14:27:14 -07001182 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1183 {
1184 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001185 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001186 // restart track if it was disabled by audioflinger due to previous underrun
1187 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001188 mActive && (cblk->flags & CBLK_DISABLED)) {
1189 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001190 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1191 mAudioTrack->start();
1192 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001193 }
Eric Laurentdf839842012-05-31 14:27:14 -07001194 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001195}
1196
1197status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1198 TargetTimeline target)
1199{
1200 return mAudioTrack->setMediaTimeTransform(xform, target);
1201}
1202
1203// -------------------------------------------------------------------------
1204
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1206{
1207 Buffer audioBuffer;
1208 uint32_t frames;
1209 size_t writtenSize;
1210
Eric Laurent1703cdf2011-03-07 14:52:59 -08001211 mLock.lock();
1212 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1213 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001214 sp<IAudioTrack> audioTrack = mAudioTrack;
1215 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001216 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001217 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001218 mLock.unlock();
1219
Glenn Kastena47f3162012-11-07 10:13:08 -08001220 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1221 // so all cblk references might still refer to old shared memory, but that should be benign
1222
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001223 // Manage underrun callback
Glenn Kastenb6037442012-11-14 13:42:25 -08001224 if (active && (cblk->framesAvailableOut(mFrameCount) == mFrameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001225 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001226 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 mCbf(EVENT_UNDERRUN, mUserData, 0);
Glenn Kastenb6037442012-11-14 13:42:25 -08001228 if (cblk->server == mFrameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001229 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001231 if (mSharedBuffer != 0) return false;
1232 }
1233 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001234
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001236 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001237 int loopCount = -1;
1238 mLoopCount--;
1239 if (mLoopCount >= 0) loopCount = mLoopCount;
1240
1241 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1242 }
1243
1244 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001245 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001246 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001247 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001248 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249 }
1250 }
1251
1252 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001253 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001254 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1256 mNewPosition += mUpdatePeriod;
1257 }
1258 }
1259
1260 // If Shared buffer is used, no data is requested from client.
1261 if (mSharedBuffer != 0) {
1262 frames = 0;
1263 } else {
1264 frames = mRemainingFrames;
1265 }
1266
Glenn Kasten99e53b82012-01-19 08:59:58 -08001267 // See description of waitCount parameter at declaration of obtainBuffer().
1268 // The logic below prevents us from being stuck below at obtainBuffer()
1269 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001270 int32_t waitCount = -1;
1271 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1272 waitCount = 1;
1273 }
1274
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 do {
1276
1277 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001278
Eric Laurent2267ba12011-09-07 11:13:23 -07001279 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 if (err < NO_ERROR) {
1281 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001282 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1283 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 return false;
1285 }
1286 break;
1287 }
1288 if (err == status_t(STOPPED)) return false;
1289
1290 // Divide buffer size by 2 to take into account the expansion
1291 // due to 8 to 16 bit conversion: the callback must fill only half
1292 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001293 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 audioBuffer.size >>= 1;
1295 }
1296
1297 size_t reqSize = audioBuffer.size;
1298 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1299 writtenSize = audioBuffer.size;
1300
1301 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001302 if (ssize_t(writtenSize) <= 0) {
1303 // The callback is done filling buffers
1304 // Keep this thread going to handle timed events and
1305 // still try to get more data in intervals of WAIT_PERIOD_MS
1306 // but don't just loop and block the CPU, so wait
1307 usleep(WAIT_PERIOD_MS*1000);
1308 break;
1309 }
Eric Laurentdf839842012-05-31 14:27:14 -07001310
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001311 if (writtenSize > reqSize) writtenSize = reqSize;
1312
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001313 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001314 // 8 to 16 bit conversion, note that source and destination are the same address
1315 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 writtenSize <<= 1;
1317 }
1318
1319 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001320 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1321 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001322 // 16 bit.
Glenn Kasten83a03822012-11-12 07:58:20 -08001323 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001324
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001325 frames -= audioBuffer.frameCount;
1326
1327 releaseBuffer(&audioBuffer);
1328 }
1329 while (frames);
1330
1331 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001332 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001333 } else {
1334 mRemainingFrames = frames;
1335 }
1336 return true;
1337}
1338
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001339// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001340// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001341// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1342// FIXME Don't depend on caller to hold strong references.
1343status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001344{
1345 status_t result;
1346
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001347 audio_track_cblk_t* cblk = refCblk;
1348 audio_track_cblk_t* newCblk = cblk;
Glenn Kastena47f3162012-11-07 10:13:08 -08001349 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
1350 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001351
Glenn Kastena47f3162012-11-07 10:13:08 -08001352 // signal old cblk condition so that other threads waiting for available buffers stop
1353 // waiting now
1354 cblk->cv.broadcast();
1355 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001356
Glenn Kastena47f3162012-11-07 10:13:08 -08001357 // refresh the audio configuration cache in this process to make sure we get new
1358 // output parameters in getOutput_l() and createTrack_l()
1359 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001360
Glenn Kastena47f3162012-11-07 10:13:08 -08001361 // if the new IAudioTrack is created, createTrack_l() will modify the
1362 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1363 // It will also delete the strong references on previous IAudioTrack and IMemory
1364 result = createTrack_l(mStreamType,
1365 cblk->sampleRate,
1366 mFormat,
1367 mChannelMask,
Glenn Kastenb6037442012-11-14 13:42:25 -08001368 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001369 mFlags,
1370 mSharedBuffer,
1371 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001372
Glenn Kastena47f3162012-11-07 10:13:08 -08001373 if (result == NO_ERROR) {
1374 uint32_t user = cblk->user;
1375 uint32_t server = cblk->server;
1376 // restore write index and set other indexes to reflect empty buffer status
1377 newCblk = mCblk;
1378 newCblk->user = user;
1379 newCblk->server = user;
1380 newCblk->userBase = user;
1381 newCblk->serverBase = user;
1382 // restore loop: this is not guaranteed to succeed if new frame count is not
1383 // compatible with loop length
1384 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1385 if (!fromStart) {
1386 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1387 // Make sure that a client relying on callback events indicating underrun or
1388 // the actual amount of audio frames played (e.g SoundPool) receives them.
1389 if (mSharedBuffer == 0) {
1390 uint32_t frames = 0;
1391 if (user > server) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001392 frames = ((user - server) > mFrameCount) ?
1393 mFrameCount : (user - server);
Glenn Kasten83a03822012-11-12 07:58:20 -08001394 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001395 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001396 // restart playback even if buffer is not completely filled.
1397 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1398 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1399 // the client
Glenn Kastenb6037442012-11-14 13:42:25 -08001400 newCblk->stepUserOut(frames, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001401 }
1402 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001403 if (mSharedBuffer != 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001404 newCblk->stepUserOut(mFrameCount, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001405 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001406 if (mActive) {
1407 result = mAudioTrack->start();
1408 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1409 }
1410 if (fromStart && result == NO_ERROR) {
1411 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001412 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001413 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001414 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001415 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001416 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001417
1418 if (result == NO_ERROR) {
1419 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001420 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001421 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001422 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001423
Steve Block5ff1dd52012-01-05 23:22:43 +00001424 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001425
1426 return result;
1427}
1428
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1430{
1431
1432 const size_t SIZE = 256;
1433 char buffer[SIZE];
1434 String8 result;
1435
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001436 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001438 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1439 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001440 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001441 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001442 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443 result.append(buffer);
Glenn Kasten3b16c762012-11-14 08:44:39 -08001444 snprintf(buffer, 255, " sample rate(%u), status(%d), muted(%d)\n",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001445 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001446 result.append(buffer);
1447 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1448 result.append(buffer);
1449 ::write(fd, result.string(), result.size());
1450 return NO_ERROR;
1451}
1452
1453// =========================================================================
1454
1455AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001456 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1457{
1458}
1459
1460AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461{
1462}
1463
1464bool AudioTrack::AudioTrackThread::threadLoop()
1465{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001466 {
1467 AutoMutex _l(mMyLock);
1468 if (mPaused) {
1469 mMyCond.wait(mMyLock);
1470 // caller will check for exitPending()
1471 return true;
1472 }
1473 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001474 if (!mReceiver.processAudioBuffer(this)) {
1475 pause();
1476 }
1477 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001478}
1479
Glenn Kasten3acbd052012-02-28 10:39:56 -08001480void AudioTrack::AudioTrackThread::requestExit()
1481{
1482 // must be in this order to avoid a race condition
1483 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001484 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001485}
1486
1487void AudioTrack::AudioTrackThread::pause()
1488{
1489 AutoMutex _l(mMyLock);
1490 mPaused = true;
1491}
1492
1493void AudioTrack::AudioTrackThread::resume()
1494{
1495 AutoMutex _l(mMyLock);
1496 if (mPaused) {
1497 mPaused = false;
1498 mMyCond.signal();
1499 }
1500}
1501
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502// =========================================================================
1503
Eric Laurent38ccae22011-03-28 18:37:07 -07001504
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001506 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastenb6037442012-11-14 13:42:25 -08001507 userBase(0), serverBase(0), frameCount_(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001508 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001509 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001510{
1511}
1512
Glenn Kastenb6037442012-11-14 13:42:25 -08001513uint32_t audio_track_cblk_t::stepUser(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001514{
Glenn Kastenb6037442012-11-14 13:42:25 -08001515 ALOGV("stepuser %08x %08x %d", user, server, stepCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001516
Marco Nelissena1472d92012-03-30 14:36:54 -07001517 uint32_t u = user;
Glenn Kastenb6037442012-11-14 13:42:25 -08001518 u += stepCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001519 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten864585d2012-11-06 16:15:41 -08001520 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001521 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1522 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1523 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1524 }
Glenn Kasten90548972011-12-13 11:45:07 -08001525 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001526 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001527 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528 }
1529
Glenn Kastenb6037442012-11-14 13:42:25 -08001530 if (u >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001531 // common case, user didn't just wrap
Glenn Kastenb6037442012-11-14 13:42:25 -08001532 if (u - frameCount >= userBase ) {
1533 userBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001534 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001535 } else if (u >= userBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001536 // user just wrapped
Glenn Kastenb6037442012-11-14 13:42:25 -08001537 userBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001538 }
1539
Glenn Kasten90548972011-12-13 11:45:07 -08001540 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001541
1542 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001543 if (flags & CBLK_UNDERRUN) {
1544 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001545 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001546
1547 return u;
1548}
1549
Glenn Kastenb6037442012-11-14 13:42:25 -08001550bool audio_track_cblk_t::stepServer(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551{
Glenn Kastenb6037442012-11-14 13:42:25 -08001552 ALOGV("stepserver %08x %08x %d", user, server, stepCount);
Marco Nelissena1472d92012-03-30 14:36:54 -07001553
Eric Laurent38ccae22011-03-28 18:37:07 -07001554 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001555 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001556 return false;
1557 }
1558
Glenn Kasten90548972011-12-13 11:45:07 -08001559 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001560 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561
Glenn Kastenb6037442012-11-14 13:42:25 -08001562 s += stepCount;
Glenn Kasten864585d2012-11-06 16:15:41 -08001563 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001564 // Mark that we have read the first buffer so that next time stepUser() is called
1565 // we switch to normal obtainBuffer() timeout period
1566 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001567 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001568 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001569 // It is possible that we receive a flush()
1570 // while the mixer is processing a block: in this case,
1571 // stepServer() is called After the flush() has reset u & s and
1572 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001573 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001574 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001575 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001576 }
1577 }
1578
1579 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001580 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001581 s = loopStart;
1582 if (--loopCount == 0) {
1583 loopEnd = UINT_MAX;
1584 loopStart = UINT_MAX;
1585 }
1586 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001587
Glenn Kastenb6037442012-11-14 13:42:25 -08001588 if (s >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001589 // common case, server didn't just wrap
Glenn Kastenb6037442012-11-14 13:42:25 -08001590 if (s - frameCount >= serverBase ) {
1591 serverBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001592 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001593 } else if (s >= serverBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001594 // server just wrapped
Glenn Kastenb6037442012-11-14 13:42:25 -08001595 serverBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596 }
1597
Glenn Kasten90548972011-12-13 11:45:07 -08001598 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001600 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001601 cv.signal();
1602 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603 lock.unlock();
1604 return true;
1605}
1606
Glenn Kasten83a03822012-11-12 07:58:20 -08001607void* audio_track_cblk_t::buffer(void *buffers, size_t frameSize, uint32_t offset) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608{
Glenn Kasten90548972011-12-13 11:45:07 -08001609 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001610}
1611
Glenn Kastenb6037442012-11-14 13:42:25 -08001612uint32_t audio_track_cblk_t::framesAvailable(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001613{
1614 Mutex::Autolock _l(lock);
Glenn Kastenb6037442012-11-14 13:42:25 -08001615 return framesAvailable_l(frameCount, isOut);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616}
1617
Glenn Kastenb6037442012-11-14 13:42:25 -08001618uint32_t audio_track_cblk_t::framesAvailable_l(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001619{
Glenn Kasten90548972011-12-13 11:45:07 -08001620 uint32_t u = user;
1621 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001622
Glenn Kasten864585d2012-11-06 16:15:41 -08001623 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001624 uint32_t limit = (s < loopStart) ? s : loopStart;
1625 return limit + frameCount - u;
1626 } else {
1627 return frameCount + u - s;
1628 }
1629}
1630
Glenn Kasten864585d2012-11-06 16:15:41 -08001631uint32_t audio_track_cblk_t::framesReady(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001632{
Glenn Kasten90548972011-12-13 11:45:07 -08001633 uint32_t u = user;
1634 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001635
Glenn Kasten864585d2012-11-06 16:15:41 -08001636 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001637 if (u < loopEnd) {
1638 return u - s;
1639 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001640 // do not block on mutex shared with client on AudioFlinger side
1641 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001642 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001643 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001644 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001645 uint32_t frames = UINT_MAX;
1646 if (loopCount >= 0) {
1647 frames = (loopEnd - loopStart)*loopCount + u - s;
1648 }
1649 lock.unlock();
1650 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001651 }
1652 } else {
1653 return s - u;
1654 }
1655}
1656
Eric Laurent38ccae22011-03-28 18:37:07 -07001657bool audio_track_cblk_t::tryLock()
1658{
1659 // the code below simulates lock-with-timeout
1660 // we MUST do this to protect the AudioFlinger server
1661 // as this lock is shared with the client.
1662 status_t err;
1663
1664 err = lock.tryLock();
1665 if (err == -EBUSY) { // just wait a bit
1666 usleep(1000);
1667 err = lock.tryLock();
1668 }
1669 if (err != NO_ERROR) {
1670 // probably, the client just died.
1671 return false;
1672 }
1673 return true;
1674}
1675
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001676// -------------------------------------------------------------------------
1677
1678}; // namespace android