blob: d0872f0cfc797dcf00150f532301c5fad9a7665c [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 Kastend65d73c2012-06-22 17:21:07 -070057 if (frameCount == NULL) {
58 return BAD_VALUE;
59 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070060
61 // default to 0 in case of error
62 *frameCount = 0;
63
Glenn Kastene0fa4672012-04-24 14:35:14 -070064 // FIXME merge with similar code in createTrack_l(), except we're missing
65 // some information here that is available in createTrack_l():
66 // audio_io_handle_t output
67 // audio_format_t format
68 // audio_channel_mask_t channelMask
69 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080070 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080071 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
72 return NO_INIT;
73 }
Glenn Kastene33054e2012-11-14 12:54:39 -080074 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080075 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
76 return NO_INIT;
77 }
78 uint32_t afLatency;
79 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
80 return NO_INIT;
81 }
82
83 // Ensure that buffer depth covers at least audio hardware latency
84 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
85 if (minBufCount < 2) minBufCount = 2;
86
87 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070088 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080089 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
90 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080091 return NO_ERROR;
92}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093
94// ---------------------------------------------------------------------------
95
96AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070097 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080098 mIsTimed(false),
99 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700100 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101{
102}
103
104AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800105 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800106 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800107 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700108 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700110 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 callback_t cbf,
112 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700113 int notificationFrames,
114 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700115 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800116 mIsTimed(false),
117 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700118 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700120 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700121 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800122 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123}
124
Andreas Huberc8139852012-01-18 10:51:55 -0800125AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800126 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800128 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700129 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700131 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 callback_t cbf,
133 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700134 int notificationFrames,
135 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700136 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800137 mIsTimed(false),
138 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700139 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800141 if (sharedBuffer == 0) {
142 ALOGE("sharedBuffer must be non-0");
143 mStatus = BAD_VALUE;
144 return;
145 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700146 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800147 0 /*frameCount*/, flags, cbf, user, notificationFrames,
148 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149}
150
151AudioTrack::~AudioTrack()
152{
Steve Block3856b092011-10-20 11:56:00 +0100153 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154
155 if (mStatus == NO_ERROR) {
156 // Make sure that callback function exits in the case where
157 // it is looping on buffer full condition in obtainBuffer().
158 // Otherwise the callback thread will never exit.
159 stop();
160 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800161 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
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,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700175 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800176 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700177 audio_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{
Glenn Kastene33054e2012-11-14 12:54:39 -0800185 // FIXME "int" here is legacy and will be replaced by size_t later
186 if (frameCountInt < 0) {
187 ALOGE("Invalid frame count %d", frameCountInt);
188 return BAD_VALUE;
189 }
190 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700192 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
193 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194
Glenn Kastene33054e2012-11-14 12:54:39 -0800195 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700196
Eric Laurent1703cdf2011-03-07 14:52:59 -0800197 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700198 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000199 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 return INVALID_OPERATION;
201 }
202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 // 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 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700207
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800209 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700210 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
211 return NO_INIT;
212 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 sampleRate = afSampleRate;
214 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700215
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800217 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700220 if (channelMask == 0) {
221 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222 }
223
224 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700225 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000226 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 return BAD_VALUE;
228 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700229
Glenn Kastene0fa4672012-04-24 14:35:14 -0700230 // AudioFlinger does not currently support 8-bit data in shared memory
231 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
232 ALOGE("8-bit data in shared memory is not supported");
233 return BAD_VALUE;
234 }
235
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700237 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700238 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800239 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700240 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700241 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700242 // only allow deep buffering for music stream type
243 if (streamType != AUDIO_STREAM_MUSIC) {
244 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
245 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700246
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700247 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700248 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 return BAD_VALUE;
250 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800251 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700252 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800253 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800256 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800257 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800258 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259
260 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000261 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 return BAD_VALUE;
263 }
264
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800265 mVolume[LEFT] = 1.0f;
266 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800267 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700268 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800269 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700270 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700271 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700272 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700273 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700274 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700275
Glenn Kastena997e7a2012-08-07 09:44:19 -0700276 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700277 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700278 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
279 }
280
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800281 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800282 status_t status = createTrack_l(streamType,
283 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800284 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800285 frameCount,
286 flags,
287 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700288 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800289
Glenn Kastena997e7a2012-08-07 09:44:19 -0700290 if (status != NO_ERROR) {
291 if (mAudioTrackThread != 0) {
292 mAudioTrackThread->requestExit();
293 mAudioTrackThread.clear();
294 }
295 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700296 }
297
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800298 mStatus = NO_ERROR;
299
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800301 mFormat = format;
Glenn Kasten83a03822012-11-12 07:58:20 -0800302
303 if (audio_is_linear_pcm(format)) {
304 mFrameSize = channelCount * audio_bytes_per_sample(format);
305 mFrameSizeAF = channelCount * sizeof(int16_t);
306 } else {
307 mFrameSize = sizeof(uint8_t);
308 mFrameSizeAF = sizeof(uint8_t);
309 }
310
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 mSharedBuffer = sharedBuffer;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800312 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 mLoopCount = 0;
315 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700316 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 mNewPosition = 0;
318 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700319 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700320 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321 return NO_ERROR;
322}
323
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324// -------------------------------------------------------------------------
325
326void AudioTrack::start()
327{
328 sp<AudioTrackThread> t = mAudioTrackThread;
329
Steve Block3856b092011-10-20 11:56:00 +0100330 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331
Eric Laurentf5aafb22010-11-18 08:40:16 -0800332 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800333 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
334 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700335 sp<IAudioTrack> audioTrack = mAudioTrack;
336 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800337 audio_track_cblk_t* cblk = mCblk;
338
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800339 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700340 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800341 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800342 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700343 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800344 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
345 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800346 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800347 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800348 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800349 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700350 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700351 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700352 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800353 }
354
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700355 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700356 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800357 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800358 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800359 ALOGV("mAudioTrack->start()");
360 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800361 cblk->lock.lock();
362 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800363 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800364 }
Eric Laurent2b584242009-11-09 23:32:22 -0800365 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800366 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700367 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800368 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700369 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800370 }
371 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800372 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100373 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800374 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800375 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800376 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800377 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700378 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700379 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800380 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800381 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 }
383
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384}
385
386void AudioTrack::stop()
387{
388 sp<AudioTrackThread> t = mAudioTrackThread;
389
Steve Block3856b092011-10-20 11:56:00 +0100390 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391
Eric Laurentf5aafb22010-11-18 08:40:16 -0800392 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800393 if (mActive) {
394 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700395 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 mAudioTrack->stop();
397 // Cancel loops (If we are in the middle of a loop, playback
398 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800399 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700400 // the playback head position will reset to 0, so if a marker is set, we need
401 // to activate it again
402 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800403 // Force flush if a shared buffer is used otherwise audioflinger
404 // will not stop before end of buffer is reached.
Glenn Kasten4bae3642012-11-30 13:41:12 -0800405 // It may be needed to make sure that we stop playback, likely in case looping is on.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800407 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 }
409 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800410 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700412 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700413 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 }
415 }
416
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417}
418
419bool AudioTrack::stopped() const
420{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800421 AutoMutex lock(mLock);
422 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423}
424
425void AudioTrack::flush()
426{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800427 AutoMutex lock(mLock);
Glenn Kasten4bae3642012-11-30 13:41:12 -0800428 if (!mActive && mSharedBuffer == 0) {
429 flush_l();
430 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800431}
432
Eric Laurent1703cdf2011-03-07 14:52:59 -0800433void AudioTrack::flush_l()
434{
Steve Block3856b092011-10-20 11:56:00 +0100435 ALOGV("flush");
Glenn Kasten4bae3642012-11-30 13:41:12 -0800436 ALOG_ASSERT(!mActive);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700437
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700438 // clear playback marker and periodic update counter
439 mMarkerPosition = 0;
440 mMarkerReached = false;
441 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700442
Glenn Kasten4bae3642012-11-30 13:41:12 -0800443 mFlushed = true;
444 mAudioTrack->flush();
445 // Release AudioTrack callback thread in case it was waiting for new buffers
446 // in AudioTrack::obtainBuffer()
447 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448}
449
450void AudioTrack::pause()
451{
Steve Block3856b092011-10-20 11:56:00 +0100452 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800453 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800454 if (mActive) {
455 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700456 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 mAudioTrack->pause();
458 }
459}
460
Eric Laurentbe916aa2010-06-01 23:49:17 -0700461status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800463 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700464 return BAD_VALUE;
465 }
466
Eric Laurent1703cdf2011-03-07 14:52:59 -0800467 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468 mVolume[LEFT] = left;
469 mVolume[RIGHT] = right;
470
Glenn Kasten83d86532012-01-17 14:39:34 -0800471 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700472
473 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474}
475
Glenn Kastenb1c09932012-02-27 16:21:04 -0800476status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800478 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700479}
480
Eric Laurent2beeb502010-07-16 07:43:46 -0700481status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700482{
Steve Block3856b092011-10-20 11:56:00 +0100483 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800484 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700485 return BAD_VALUE;
486 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800487 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700488
489 mSendLevel = level;
490
Glenn Kasten05632a52012-01-03 14:22:33 -0800491 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700492
493 return NO_ERROR;
494}
495
Glenn Kastena5224f32012-01-04 12:41:44 -0800496void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700497{
498 if (level != NULL) {
499 *level = mSendLevel;
500 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501}
502
Glenn Kasten3b16c762012-11-14 08:44:39 -0800503status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504{
Glenn Kasten3b16c762012-11-14 08:44:39 -0800505 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506
John Grossman4ff14ba2012-02-08 16:37:41 -0800507 if (mIsTimed) {
508 return INVALID_OPERATION;
509 }
510
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700512 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 }
514 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700515 if (rate == 0 || rate > afSamplingRate*2 ) {
516 return BAD_VALUE;
517 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518
Eric Laurent1703cdf2011-03-07 14:52:59 -0800519 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700520 mCblk->sampleRate = rate;
521 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522}
523
Glenn Kastena5224f32012-01-04 12:41:44 -0800524uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525{
John Grossman4ff14ba2012-02-08 16:37:41 -0800526 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800527 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800528 }
529
Eric Laurent1703cdf2011-03-07 14:52:59 -0800530 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700531 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532}
533
534status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
535{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800536 AutoMutex lock(mLock);
537 return setLoop_l(loopStart, loopEnd, loopCount);
538}
539
540// must be called with mLock held
541status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
542{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800543 if (mSharedBuffer == 0 || mIsTimed) {
544 return INVALID_OPERATION;
545 }
546
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 audio_track_cblk_t* cblk = mCblk;
548
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 Mutex::Autolock _l(cblk->lock);
550
551 if (loopCount == 0) {
552 cblk->loopStart = UINT_MAX;
553 cblk->loopEnd = UINT_MAX;
554 cblk->loopCount = 0;
555 mLoopCount = 0;
556 return NO_ERROR;
557 }
558
559 if (loopStart >= loopEnd ||
Glenn Kastenb6037442012-11-14 13:42:25 -0800560 loopEnd - loopStart > mFrameCount ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700561 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700562 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
Glenn Kastenb6037442012-11-14 13:42:25 -0800563 "user %d", loopStart, loopEnd, loopCount, mFrameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 return BAD_VALUE;
565 }
566
Glenn Kastenb6037442012-11-14 13:42:25 -0800567 if ((mSharedBuffer != 0) && (loopEnd > mFrameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700568 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
569 "framecount %d",
Glenn Kastenb6037442012-11-14 13:42:25 -0800570 loopStart, loopEnd, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700572 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573
574 cblk->loopStart = loopStart;
575 cblk->loopEnd = loopEnd;
576 cblk->loopCount = loopCount;
577 mLoopCount = loopCount;
578
579 return NO_ERROR;
580}
581
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582status_t AudioTrack::setMarkerPosition(uint32_t marker)
583{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700584 if (mCbf == NULL) {
585 return INVALID_OPERATION;
586 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587
588 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700589 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590
591 return NO_ERROR;
592}
593
Glenn Kastena5224f32012-01-04 12:41:44 -0800594status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700596 if (marker == NULL) {
597 return BAD_VALUE;
598 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599
600 *marker = mMarkerPosition;
601
602 return NO_ERROR;
603}
604
605status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
606{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700607 if (mCbf == NULL) {
608 return INVALID_OPERATION;
609 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610
611 uint32_t curPosition;
612 getPosition(&curPosition);
613 mNewPosition = curPosition + updatePeriod;
614 mUpdatePeriod = updatePeriod;
615
616 return NO_ERROR;
617}
618
Glenn Kastena5224f32012-01-04 12:41:44 -0800619status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700621 if (updatePeriod == NULL) {
622 return BAD_VALUE;
623 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624
625 *updatePeriod = mUpdatePeriod;
626
627 return NO_ERROR;
628}
629
630status_t AudioTrack::setPosition(uint32_t position)
631{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800632 if (mSharedBuffer == 0 || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700633 return INVALID_OPERATION;
634 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800635
Eric Laurent1703cdf2011-03-07 14:52:59 -0800636 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637
Glenn Kastend65d73c2012-06-22 17:21:07 -0700638 if (!stopped_l()) {
639 return INVALID_OPERATION;
640 }
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800641
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700642 audio_track_cblk_t* cblk = mCblk;
643 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644
Glenn Kastend65d73c2012-06-22 17:21:07 -0700645 if (position > cblk->user) {
646 return BAD_VALUE;
647 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700649 cblk->server = position;
650 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700651
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return NO_ERROR;
653}
654
655status_t AudioTrack::getPosition(uint32_t *position)
656{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700657 if (position == NULL) {
658 return BAD_VALUE;
659 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800660 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700661 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662
663 return NO_ERROR;
664}
665
666status_t AudioTrack::reload()
667{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800668 if (mSharedBuffer == 0 || mIsTimed) {
669 return INVALID_OPERATION;
670 }
671
Eric Laurent1703cdf2011-03-07 14:52:59 -0800672 AutoMutex lock(mLock);
673
Glenn Kastend65d73c2012-06-22 17:21:07 -0700674 if (!stopped_l()) {
675 return INVALID_OPERATION;
676 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700677
Eric Laurent1703cdf2011-03-07 14:52:59 -0800678 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700680 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800681 cblk->stepUserOut(mFrameCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682
683 return NO_ERROR;
684}
685
Eric Laurentc2f1f072009-07-17 12:17:14 -0700686audio_io_handle_t AudioTrack::getOutput()
687{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800688 AutoMutex lock(mLock);
689 return getOutput_l();
690}
691
692// must be called with mLock held
693audio_io_handle_t AudioTrack::getOutput_l()
694{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800695 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800696 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700697}
698
Eric Laurentbe916aa2010-06-01 23:49:17 -0700699status_t AudioTrack::attachAuxEffect(int effectId)
700{
Steve Block3856b092011-10-20 11:56:00 +0100701 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700702 status_t status = mAudioTrack->attachAuxEffect(effectId);
703 if (status == NO_ERROR) {
704 mAuxEffectId = effectId;
705 }
706 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700707}
708
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709// -------------------------------------------------------------------------
710
Eric Laurent1703cdf2011-03-07 14:52:59 -0800711// must be called with mLock held
712status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800713 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800714 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800715 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800716 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700717 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800718 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700719 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800720{
721 status_t status;
722 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
723 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700724 ALOGE("Could not get audioflinger");
725 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800726 }
727
Eric Laurentd1b449a2010-05-14 03:26:45 -0700728 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700729 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700730 return NO_INIT;
731 }
732
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700733 // Client decides whether the track is TIMED (see below), but can only express a preference
734 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700735 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700736 // either of these use cases:
737 // use case 1: shared buffer
738 (sharedBuffer != 0) ||
739 // use case 2: callback handler
740 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800741 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700742 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700743 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700744 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700745 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700746 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700747
Eric Laurentd1b449a2010-05-14 03:26:45 -0700748 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700749
Dima Zavinfce7a472011-04-19 22:30:36 -0700750 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700751
Eric Laurentd1b449a2010-05-14 03:26:45 -0700752 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700753 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700754 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700755 } else if (frameCount == 0) {
Glenn Kastene33054e2012-11-14 12:54:39 -0800756 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700757 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
758 return NO_INIT;
759 }
760 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700761 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700762
763 } else if (sharedBuffer != 0) {
764
Glenn Kastena42ff002012-11-14 12:47:55 -0800765 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700766 // 8-bit data in shared memory is not currently supported by AudioFlinger
767 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800768 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700769 // More than 2 channels does not require stronger alignment than stereo
770 alignment <<= 1;
771 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800772 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
773 ALOGE("Invalid buffer alignment: address %p, channel count %u",
774 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700775 return BAD_VALUE;
776 }
777
778 // When initializing a shared buffer AudioTrack via constructors,
779 // there's no frameCount parameter.
780 // But when initializing a shared buffer AudioTrack via set(),
781 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800782 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700783
784 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
785
786 // FIXME move these calculations and associated checks to server
Glenn Kasten3b16c762012-11-14 08:44:39 -0800787 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700788 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
789 return NO_INIT;
790 }
Glenn Kastene33054e2012-11-14 12:54:39 -0800791 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700792 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
793 return NO_INIT;
794 }
795
Eric Laurentd1b449a2010-05-14 03:26:45 -0700796 // Ensure that buffer depth covers at least audio hardware latency
797 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
798 if (minBufCount < 2) minBufCount = 2;
799
Glenn Kastene33054e2012-11-14 12:54:39 -0800800 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
801 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800802 ", afLatency=%d",
803 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700804
805 if (frameCount == 0) {
806 frameCount = minFrameCount;
807 }
808 if (mNotificationFramesAct == 0) {
809 mNotificationFramesAct = frameCount/2;
810 }
811 // Make sure that application is notified with sufficient margin
812 // before underrun
Glenn Kastene33054e2012-11-14 12:54:39 -0800813 if (mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700814 mNotificationFramesAct = frameCount/2;
815 }
816 if (frameCount < minFrameCount) {
817 // not ALOGW because it happens all the time when playing key clicks over A2DP
818 ALOGV("Minimum buffer size corrected from %d to %d",
819 frameCount, minFrameCount);
820 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800821 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700822
Glenn Kastene0fa4672012-04-24 14:35:14 -0700823 } else {
824 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700825 }
826
Glenn Kastena075db42012-03-06 11:22:44 -0800827 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
828 if (mIsTimed) {
829 trackFlags |= IAudioFlinger::TRACK_TIMED;
830 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800831
832 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700833 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700834 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800835 if (mAudioTrackThread != 0) {
836 tid = mAudioTrackThread->getTid();
837 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700838 }
839
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800840 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800841 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700842 // AudioFlinger only sees 16-bit PCM
843 format == AUDIO_FORMAT_PCM_8_BIT ?
844 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800845 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800846 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800847 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800848 sharedBuffer,
849 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800850 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700851 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800852 &status);
853
854 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000855 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800856 return status;
857 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700858 sp<IMemory> iMem = track->getCblk();
859 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000860 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800861 return NO_INIT;
862 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800863 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700864 mCblkMemory = iMem;
865 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
866 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800867 size_t temp = cblk->frameCount_;
868 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
869 // In current design, AudioTrack client checks and ensures frame count validity before
870 // passing it to AudioFlinger so AudioFlinger should not return a different value except
871 // for fast track as it uses a special method of assigning frame count.
872 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
873 }
874 frameCount = temp;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800875 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800876 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800877 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800878 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -0800879 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700880 // once denied, do not request again if IAudioTrack is re-created
881 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
882 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800883 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700884 if (sharedBuffer == 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800885 mNotificationFramesAct = frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700886 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800887 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800888 if (sharedBuffer == 0) {
Glenn Kastenb929e412012-11-08 12:13:58 -0800889 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800890 } else {
Glenn Kastenb929e412012-11-08 12:13:58 -0800891 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700892 // Force buffer full condition as data is already present in shared memory
Glenn Kastenb6037442012-11-14 13:42:25 -0800893 cblk->stepUserOut(frameCount, frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800894 }
895
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700896 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700897 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700898 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700899 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700900 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
901 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700902 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700903 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -0800904 mLatency = afLatency + (1000*frameCount) / sampleRate;
905 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700906 // If IAudioTrack is re-created, don't let the requested frameCount
907 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -0800908 if (frameCount > mReqFrameCount) {
909 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700910 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800911 return NO_ERROR;
912}
913
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
915{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800916 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800917 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700918 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800919 audio_track_cblk_t* cblk = mCblk;
920 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700921 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800922
923 audioBuffer->frameCount = 0;
924 audioBuffer->size = 0;
925
Glenn Kastenb6037442012-11-14 13:42:25 -0800926 uint32_t framesAvail = cblk->framesAvailableOut(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800927
Eric Laurent9b7d9502011-03-21 11:49:00 -0700928 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800929 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700930 goto create_new_track;
931 }
932 cblk->lock.unlock();
933
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800935 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936 goto start_loop_here;
937 while (framesAvail == 0) {
938 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800939 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100940 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800941 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942 return NO_MORE_BUFFERS;
943 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800944 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800945 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800947 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800948 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800949 mLock.unlock();
Glenn Kastena47f3162012-11-07 10:13:08 -0800950 // this condition is in shared memory, so if IAudioTrack and control block
951 // are replaced due to mediaserver death or IAudioTrack invalidation then
952 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700953 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700954 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800955 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800956 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800957 return status_t(STOPPED);
958 }
Glenn Kastena47f3162012-11-07 10:13:08 -0800959 // IAudioTrack may have been re-created while mLock was unlocked
960 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800961 cblk->lock.lock();
962 }
963
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800964 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700965 goto create_new_track;
966 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800967 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700968 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800969 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
970 // timing out when a loop has been set and we have already written upto loop end
971 // is a normal condition: no need to wake AudioFlinger up.
972 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700973 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
974 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700975 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800977 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800978 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800979 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800980 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800981create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700982 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800983 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700984 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800985 }
986 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000987 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800988 cblk->lock.unlock();
989 return result;
990 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991 }
992 cblk->waitTimeMs = 0;
993 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700994
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800996 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800997 return TIMED_OUT;
998 }
999 }
1000 // read the server count again
1001 start_loop_here:
Glenn Kastenb6037442012-11-14 13:42:25 -08001002 framesAvail = cblk->framesAvailableOut_l(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001005 }
1006
1007 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001008
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001009 if (framesReq > framesAvail) {
1010 framesReq = framesAvail;
1011 }
1012
1013 uint32_t u = cblk->user;
Glenn Kastenb6037442012-11-14 13:42:25 -08001014 uint32_t bufferEnd = cblk->userBase + mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001015
Marco Nelissena1472d92012-03-30 14:36:54 -07001016 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001017 framesReq = bufferEnd - u;
1018 }
1019
Eric Laurentc2f1f072009-07-17 12:17:14 -07001020 audioBuffer->frameCount = framesReq;
Glenn Kasten83a03822012-11-12 07:58:20 -08001021 audioBuffer->size = framesReq * mFrameSizeAF;
1022 audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001023 active = mActive;
1024 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1025}
1026
1027void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1028{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001029 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001030 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001031 cblk->stepUserOut(audioBuffer->frameCount, mFrameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001032 if (audioBuffer->frameCount > 0) {
1033 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001034 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1035 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1036 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001037 mAudioTrack->start();
1038 }
1039 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001040}
1041
1042// -------------------------------------------------------------------------
1043
1044ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1045{
1046
Glenn Kasten083d1c12012-11-30 15:00:36 -08001047 if (mSharedBuffer != 0 || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001048 return INVALID_OPERATION;
1049 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001050
1051 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001052 // Sanity-check: user is most-likely passing an error code, and it would
1053 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001054 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001055 buffer, userSize, userSize);
1056 return BAD_VALUE;
1057 }
1058
Steve Block3856b092011-10-20 11:56:00 +01001059 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001060
Eric Laurentdf839842012-05-31 14:27:14 -07001061 if (userSize == 0) {
1062 return 0;
1063 }
1064
Eric Laurent1703cdf2011-03-07 14:52:59 -08001065 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1066 // while we are accessing the cblk
1067 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001068 sp<IAudioTrack> audioTrack = mAudioTrack;
1069 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001070 mLock.unlock();
1071
Glenn Kastena47f3162012-11-07 10:13:08 -08001072 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1073 // so all cblk references might still refer to old shared memory, but that should be benign
1074
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001075 ssize_t written = 0;
1076 const int8_t *src = (const int8_t *)buffer;
1077 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001078 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001079
1080 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001081 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001082
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001083 status_t err = obtainBuffer(&audioBuffer, -1);
1084 if (err < 0) {
1085 // out of buffers, return #bytes written
Glenn Kastend65d73c2012-06-22 17:21:07 -07001086 if (err == status_t(NO_MORE_BUFFERS)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001087 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001088 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001089 return ssize_t(err);
1090 }
1091
1092 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001093
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001094 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001095 // Divide capacity by 2 to take expansion into account
1096 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001097 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001098 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001099 toWrite = audioBuffer.size;
1100 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001101 }
Glenn Kastenbc0f6b92012-11-12 14:32:06 -08001102 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001103 userSize -= toWrite;
1104 written += toWrite;
1105
1106 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001107 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001108
1109 return written;
1110}
1111
1112// -------------------------------------------------------------------------
1113
John Grossman4ff14ba2012-02-08 16:37:41 -08001114TimedAudioTrack::TimedAudioTrack() {
1115 mIsTimed = true;
1116}
1117
1118status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1119{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001120 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001121 status_t result = UNKNOWN_ERROR;
1122
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001123 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1124 // while we are accessing the cblk
1125 sp<IAudioTrack> audioTrack = mAudioTrack;
1126 sp<IMemory> iMem = mCblkMemory;
1127
John Grossman4ff14ba2012-02-08 16:37:41 -08001128 // If the track is not invalid already, try to allocate a buffer. alloc
1129 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001130 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001131 audio_track_cblk_t* cblk = mCblk;
1132 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001133 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1134 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001135 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001136 }
1137 }
1138
1139 // If the track is invalid at this point, attempt to restore it. and try the
1140 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001141 if (cblk->flags & CBLK_INVALID) {
1142 cblk->lock.lock();
1143 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001144 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001145 cblk = temp;
1146 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001147
Glenn Kastend65d73c2012-06-22 17:21:07 -07001148 if (result == OK) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001149 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001150 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001151 }
1152
1153 return result;
1154}
1155
1156status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1157 int64_t pts)
1158{
Eric Laurentdf839842012-05-31 14:27:14 -07001159 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1160 {
1161 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001162 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001163 // restart track if it was disabled by audioflinger due to previous underrun
1164 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001165 mActive && (cblk->flags & CBLK_DISABLED)) {
1166 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001167 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1168 mAudioTrack->start();
1169 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001170 }
Eric Laurentdf839842012-05-31 14:27:14 -07001171 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001172}
1173
1174status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1175 TargetTimeline target)
1176{
1177 return mAudioTrack->setMediaTimeTransform(xform, target);
1178}
1179
1180// -------------------------------------------------------------------------
1181
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001182bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1183{
1184 Buffer audioBuffer;
1185 uint32_t frames;
1186 size_t writtenSize;
1187
Eric Laurent1703cdf2011-03-07 14:52:59 -08001188 mLock.lock();
1189 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1190 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001191 sp<IAudioTrack> audioTrack = mAudioTrack;
1192 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001193 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001194 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001195 mLock.unlock();
1196
Glenn Kastena47f3162012-11-07 10:13:08 -08001197 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1198 // so all cblk references might still refer to old shared memory, but that should be benign
1199
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001200 // Manage underrun callback
Glenn Kastenb6037442012-11-14 13:42:25 -08001201 if (active && (cblk->framesAvailableOut(mFrameCount) == mFrameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001202 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001203 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204 mCbf(EVENT_UNDERRUN, mUserData, 0);
Glenn Kastenb6037442012-11-14 13:42:25 -08001205 if (cblk->server == mFrameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001206 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001208 if (mSharedBuffer != 0) {
1209 return false;
1210 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001211 }
1212 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001213
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001215 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 int loopCount = -1;
1217 mLoopCount--;
1218 if (mLoopCount >= 0) loopCount = mLoopCount;
1219
1220 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1221 }
1222
1223 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001224 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001225 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001226 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001227 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 }
1229 }
1230
1231 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001232 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001233 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1235 mNewPosition += mUpdatePeriod;
1236 }
1237 }
1238
1239 // If Shared buffer is used, no data is requested from client.
1240 if (mSharedBuffer != 0) {
1241 frames = 0;
1242 } else {
1243 frames = mRemainingFrames;
1244 }
1245
Glenn Kasten99e53b82012-01-19 08:59:58 -08001246 // See description of waitCount parameter at declaration of obtainBuffer().
1247 // The logic below prevents us from being stuck below at obtainBuffer()
1248 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001249 int32_t waitCount = -1;
1250 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1251 waitCount = 1;
1252 }
1253
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254 do {
1255
1256 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001257
Eric Laurent2267ba12011-09-07 11:13:23 -07001258 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 if (err < NO_ERROR) {
1260 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001261 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1262 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 return false;
1264 }
1265 break;
1266 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001267 if (err == status_t(STOPPED)) {
1268 return false;
1269 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270
1271 // Divide buffer size by 2 to take into account the expansion
1272 // due to 8 to 16 bit conversion: the callback must fill only half
1273 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001274 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 audioBuffer.size >>= 1;
1276 }
1277
1278 size_t reqSize = audioBuffer.size;
1279 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1280 writtenSize = audioBuffer.size;
1281
1282 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001283 if (ssize_t(writtenSize) <= 0) {
1284 // The callback is done filling buffers
1285 // Keep this thread going to handle timed events and
1286 // still try to get more data in intervals of WAIT_PERIOD_MS
1287 // but don't just loop and block the CPU, so wait
1288 usleep(WAIT_PERIOD_MS*1000);
1289 break;
1290 }
Eric Laurentdf839842012-05-31 14:27:14 -07001291
Glenn Kastend65d73c2012-06-22 17:21:07 -07001292 if (writtenSize > reqSize) {
1293 writtenSize = reqSize;
1294 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001296 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001297 // 8 to 16 bit conversion, note that source and destination are the same address
1298 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 writtenSize <<= 1;
1300 }
1301
1302 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001303 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1304 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001305 // 16 bit.
Glenn Kasten83a03822012-11-12 07:58:20 -08001306 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001307
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001308 frames -= audioBuffer.frameCount;
1309
1310 releaseBuffer(&audioBuffer);
1311 }
1312 while (frames);
1313
1314 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001315 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 } else {
1317 mRemainingFrames = frames;
1318 }
1319 return true;
1320}
1321
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001322// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001323// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001324// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1325// FIXME Don't depend on caller to hold strong references.
1326status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001327{
1328 status_t result;
1329
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001330 audio_track_cblk_t* cblk = refCblk;
1331 audio_track_cblk_t* newCblk = cblk;
Glenn Kasten827e5f12012-11-02 10:00:06 -07001332 ALOGW("dead IAudioTrack, creating a new one from %s",
1333 fromStart ? "start()" : "obtainBuffer()");
Eric Laurent1703cdf2011-03-07 14:52:59 -08001334
Glenn Kastena47f3162012-11-07 10:13:08 -08001335 // signal old cblk condition so that other threads waiting for available buffers stop
1336 // waiting now
1337 cblk->cv.broadcast();
1338 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001339
Glenn Kastena47f3162012-11-07 10:13:08 -08001340 // refresh the audio configuration cache in this process to make sure we get new
1341 // output parameters in getOutput_l() and createTrack_l()
1342 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001343
Glenn Kastena47f3162012-11-07 10:13:08 -08001344 // if the new IAudioTrack is created, createTrack_l() will modify the
1345 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1346 // It will also delete the strong references on previous IAudioTrack and IMemory
1347 result = createTrack_l(mStreamType,
1348 cblk->sampleRate,
1349 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001350 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001351 mFlags,
1352 mSharedBuffer,
1353 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001354
Glenn Kastena47f3162012-11-07 10:13:08 -08001355 if (result == NO_ERROR) {
1356 uint32_t user = cblk->user;
1357 uint32_t server = cblk->server;
1358 // restore write index and set other indexes to reflect empty buffer status
1359 newCblk = mCblk;
1360 newCblk->user = user;
1361 newCblk->server = user;
1362 newCblk->userBase = user;
1363 newCblk->serverBase = user;
1364 // restore loop: this is not guaranteed to succeed if new frame count is not
1365 // compatible with loop length
1366 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1367 if (!fromStart) {
1368 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1369 // Make sure that a client relying on callback events indicating underrun or
1370 // the actual amount of audio frames played (e.g SoundPool) receives them.
1371 if (mSharedBuffer == 0) {
1372 uint32_t frames = 0;
1373 if (user > server) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001374 frames = ((user - server) > mFrameCount) ?
1375 mFrameCount : (user - server);
Glenn Kasten83a03822012-11-12 07:58:20 -08001376 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001377 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001378 // restart playback even if buffer is not completely filled.
1379 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1380 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1381 // the client
Glenn Kastenb6037442012-11-14 13:42:25 -08001382 newCblk->stepUserOut(frames, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001383 }
1384 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001385 if (mSharedBuffer != 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001386 newCblk->stepUserOut(mFrameCount, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001387 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001388 if (mActive) {
1389 result = mAudioTrack->start();
1390 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1391 }
1392 if (fromStart && result == NO_ERROR) {
1393 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001394 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001395 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001396 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001397 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001398 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001399
1400 if (result == NO_ERROR) {
1401 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001402 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001403 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001404 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001405
Glenn Kasten827e5f12012-11-02 10:00:06 -07001406 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001407
1408 return result;
1409}
1410
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001411status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1412{
1413
1414 const size_t SIZE = 256;
1415 char buffer[SIZE];
1416 String8 result;
1417
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001418 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001420 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1421 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001422 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001423 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001424 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425 result.append(buffer);
Glenn Kastene4756fe2012-11-29 13:38:14 -08001426 snprintf(buffer, 255, " sample rate(%u), status(%d)\n",
1427 (cblk == 0) ? 0 : cblk->sampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001428 result.append(buffer);
1429 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1430 result.append(buffer);
1431 ::write(fd, result.string(), result.size());
1432 return NO_ERROR;
1433}
1434
1435// =========================================================================
1436
1437AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001438 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1439{
1440}
1441
1442AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443{
1444}
1445
1446bool AudioTrack::AudioTrackThread::threadLoop()
1447{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001448 {
1449 AutoMutex _l(mMyLock);
1450 if (mPaused) {
1451 mMyCond.wait(mMyLock);
1452 // caller will check for exitPending()
1453 return true;
1454 }
1455 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001456 if (!mReceiver.processAudioBuffer(this)) {
1457 pause();
1458 }
1459 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460}
1461
Glenn Kasten3acbd052012-02-28 10:39:56 -08001462void AudioTrack::AudioTrackThread::requestExit()
1463{
1464 // must be in this order to avoid a race condition
1465 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001466 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001467}
1468
1469void AudioTrack::AudioTrackThread::pause()
1470{
1471 AutoMutex _l(mMyLock);
1472 mPaused = true;
1473}
1474
1475void AudioTrack::AudioTrackThread::resume()
1476{
1477 AutoMutex _l(mMyLock);
1478 if (mPaused) {
1479 mPaused = false;
1480 mMyCond.signal();
1481 }
1482}
1483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001484}; // namespace android