blob: faca054e3c7a605ae7c2e8c4b326c69ca60bb1ca [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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080023#include <audio_utils/primitives.h>
24#include <binder/IPCThreadState.h>
25#include <media/AudioTrack.h>
26#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <private/media/AudioTrackShared.h>
28
Glenn Kasten9f80dd22012-12-18 15:57:32 -080029#define WAIT_PERIOD_MS 10
Glenn Kasten511754b2012-01-11 09:52:19 -080030
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080032// ---------------------------------------------------------------------------
33
34// static
35status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080036 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080037 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080038 uint32_t sampleRate)
39{
Glenn Kastend65d73c2012-06-22 17:21:07 -070040 if (frameCount == NULL) {
41 return BAD_VALUE;
42 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070043
44 // default to 0 in case of error
45 *frameCount = 0;
46
Glenn Kastene0fa4672012-04-24 14:35:14 -070047 // FIXME merge with similar code in createTrack_l(), except we're missing
48 // some information here that is available in createTrack_l():
49 // audio_io_handle_t output
50 // audio_format_t format
51 // audio_channel_mask_t channelMask
52 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080053 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080054 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
55 return NO_INIT;
56 }
Glenn Kastene33054e2012-11-14 12:54:39 -080057 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080058 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
59 return NO_INIT;
60 }
61 uint32_t afLatency;
62 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
63 return NO_INIT;
64 }
65
66 // Ensure that buffer depth covers at least audio hardware latency
67 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080068 if (minBufCount < 2) {
69 minBufCount = 2;
70 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080071
72 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070073 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080074 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
75 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080076 return NO_ERROR;
77}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078
79// ---------------------------------------------------------------------------
80
81AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070082 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080083 mIsTimed(false),
84 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -080085 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086{
87}
88
89AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080090 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080092 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -070093 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080094 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070095 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096 callback_t cbf,
97 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -070098 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -080099 int sessionId,
100 transfer_type transferType)
Glenn Kasten87913512011-06-22 16:15:25 -0700101 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800102 mIsTimed(false),
103 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800104 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700106 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700107 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800108 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109}
110
Andreas Huberc8139852012-01-18 10:51:55 -0800111AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800112 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800114 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700115 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700117 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 callback_t cbf,
119 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700120 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800121 int sessionId,
122 transfer_type transferType)
Glenn Kasten87913512011-06-22 16:15:25 -0700123 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800124 mIsTimed(false),
125 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800126 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700128 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800129 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800130 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800131}
132
133AudioTrack::~AudioTrack()
134{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135 if (mStatus == NO_ERROR) {
136 // Make sure that callback function exits in the case where
137 // it is looping on buffer full condition in obtainBuffer().
138 // Otherwise the callback thread will never exit.
139 stop();
140 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800141 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 mAudioTrackThread->requestExitAndWait();
143 mAudioTrackThread.clear();
144 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800145 if (mAudioTrack != 0) {
146 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
147 mAudioTrack.clear();
148 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700150 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 }
152}
153
154status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800155 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800157 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700158 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800159 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700160 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 callback_t cbf,
162 void* user,
163 int notificationFrames,
164 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700165 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800166 int sessionId,
167 transfer_type transferType)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800169 switch (transferType) {
170 case TRANSFER_DEFAULT:
171 if (sharedBuffer != 0) {
172 transferType = TRANSFER_SHARED;
173 } else if (cbf == NULL || threadCanCallJava) {
174 transferType = TRANSFER_SYNC;
175 } else {
176 transferType = TRANSFER_CALLBACK;
177 }
178 break;
179 case TRANSFER_CALLBACK:
180 if (cbf == NULL || sharedBuffer != 0) {
181 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
182 return BAD_VALUE;
183 }
184 break;
185 case TRANSFER_OBTAIN:
186 case TRANSFER_SYNC:
187 if (sharedBuffer != 0) {
188 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
189 return BAD_VALUE;
190 }
191 break;
192 case TRANSFER_SHARED:
193 if (sharedBuffer == 0) {
194 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
195 return BAD_VALUE;
196 }
197 break;
198 default:
199 ALOGE("Invalid transfer type %d", transferType);
200 return BAD_VALUE;
201 }
202 mTransfer = transferType;
203
Glenn Kastene33054e2012-11-14 12:54:39 -0800204 // FIXME "int" here is legacy and will be replaced by size_t later
205 if (frameCountInt < 0) {
206 ALOGE("Invalid frame count %d", frameCountInt);
207 return BAD_VALUE;
208 }
209 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700211 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
212 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213
Glenn Kastene33054e2012-11-14 12:54:39 -0800214 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700215
Eric Laurent1703cdf2011-03-07 14:52:59 -0800216 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800217
Eric Laurent1dd70b92009-04-21 07:56:33 -0700218 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000219 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 return INVALID_OPERATION;
221 }
222
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700224 if (streamType == AUDIO_STREAM_DEFAULT) {
225 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800226 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700227
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800229 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700230 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
231 return NO_INIT;
232 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233 sampleRate = afSampleRate;
234 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800235 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700236
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800238 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700241 if (channelMask == 0) {
242 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243 }
244
245 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700246 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800247 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 return BAD_VALUE;
249 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700250
Glenn Kastene0fa4672012-04-24 14:35:14 -0700251 // AudioFlinger does not currently support 8-bit data in shared memory
252 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
253 ALOGE("8-bit data in shared memory is not supported");
254 return BAD_VALUE;
255 }
256
Eric Laurentc2f1f072009-07-17 12:17:14 -0700257 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700258 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700259 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800260 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700261 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700262 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700263 // only allow deep buffering for music stream type
264 if (streamType != AUDIO_STREAM_MUSIC) {
265 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
266 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700267
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700268 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700269 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700270 return BAD_VALUE;
271 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800272 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700273 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800274 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275
Glenn Kastene3aa6592012-12-04 12:22:46 -0800276 if (audio_is_linear_pcm(format)) {
277 mFrameSize = channelCount * audio_bytes_per_sample(format);
278 mFrameSizeAF = channelCount * sizeof(int16_t);
279 } else {
280 mFrameSize = sizeof(uint8_t);
281 mFrameSizeAF = sizeof(uint8_t);
282 }
283
Dima Zavinfce7a472011-04-19 22:30:36 -0700284 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800285 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800286 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800287 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288
289 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000290 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 return BAD_VALUE;
292 }
293
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800294 mVolume[LEFT] = 1.0f;
295 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800296 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700297 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800298 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700299 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800300 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700301 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700302 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700303 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700304 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700305
Glenn Kastena997e7a2012-08-07 09:44:19 -0700306 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700307 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700308 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
309 }
310
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800311 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800312 status_t status = createTrack_l(streamType,
313 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800314 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800315 frameCount,
316 flags,
317 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800318 output,
319 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800320
Glenn Kastena997e7a2012-08-07 09:44:19 -0700321 if (status != NO_ERROR) {
322 if (mAudioTrackThread != 0) {
323 mAudioTrackThread->requestExit();
324 mAudioTrackThread.clear();
325 }
326 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700327 }
328
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800331 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800333 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800335 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700337 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338 mNewPosition = 0;
339 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700340 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800341 mSequence = 1;
342 mObservedSequence = mSequence;
343 mInUnderrun = false;
344
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800345 return NO_ERROR;
346}
347
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348// -------------------------------------------------------------------------
349
350void AudioTrack::start()
351{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800352 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800353 if (mState == STATE_ACTIVE) {
354 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355 }
356
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800357 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800359 State previousState = mState;
360 mState = STATE_ACTIVE;
361 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
362 // reset current position as seen by client to 0
363 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
364 }
365 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
366 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->flags);
367
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800369 if (t != 0) {
370 t->resume();
371 } else {
372 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
373 get_sched_policy(0, &mPreviousSchedulingGroup);
374 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
375 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800377 status_t status = NO_ERROR;
378 if (!(flags & CBLK_INVALID)) {
379 status = mAudioTrack->start();
380 if (status == DEAD_OBJECT) {
381 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800383 }
384 if (flags & CBLK_INVALID) {
385 status = restoreTrack_l("start");
386 }
387
388 if (status != NO_ERROR) {
389 ALOGE("start() status %d", status);
390 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800392 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700394 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700395 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 }
397 }
398
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800399 // FIXME discarding status
400}
401
402void AudioTrack::stop()
403{
404 AutoMutex lock(mLock);
405 // FIXME pause then stop should not be a nop
406 if (mState != STATE_ACTIVE) {
407 return;
408 }
409
410 mState = STATE_STOPPED;
411 mProxy->interrupt();
412 mAudioTrack->stop();
413 // the playback head position will reset to 0, so if a marker is set, we need
414 // to activate it again
415 mMarkerReached = false;
416#if 0
417 // Force flush if a shared buffer is used otherwise audioflinger
418 // will not stop before end of buffer is reached.
419 // It may be needed to make sure that we stop playback, likely in case looping is on.
420 if (mSharedBuffer != 0) {
421 flush_l();
422 }
423#endif
424 sp<AudioTrackThread> t = mAudioTrackThread;
425 if (t != 0) {
426 t->pause();
427 } else {
428 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
429 set_sched_policy(0, mPreviousSchedulingGroup);
430 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431}
432
433bool AudioTrack::stopped() const
434{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800435 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800436 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437}
438
439void AudioTrack::flush()
440{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800441 if (mSharedBuffer != 0) {
442 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800443 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800444 AutoMutex lock(mLock);
445 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
446 return;
447 }
448 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800449}
450
Eric Laurent1703cdf2011-03-07 14:52:59 -0800451void AudioTrack::flush_l()
452{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800453 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700454
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700455 // clear playback marker and periodic update counter
456 mMarkerPosition = 0;
457 mMarkerReached = false;
458 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700459
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800460 mState = STATE_FLUSHED;
461 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800462 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463}
464
465void AudioTrack::pause()
466{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800467 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800468 if (mState != STATE_ACTIVE) {
469 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800471 mState = STATE_PAUSED;
472 mProxy->interrupt();
473 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474}
475
Eric Laurentbe916aa2010-06-01 23:49:17 -0700476status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800478 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700479 return BAD_VALUE;
480 }
481
Eric Laurent1703cdf2011-03-07 14:52:59 -0800482 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483 mVolume[LEFT] = left;
484 mVolume[RIGHT] = right;
485
Glenn Kastene3aa6592012-12-04 12:22:46 -0800486 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700487
488 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489}
490
Glenn Kastenb1c09932012-02-27 16:21:04 -0800491status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800493 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700494}
495
Eric Laurent2beeb502010-07-16 07:43:46 -0700496status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700497{
Glenn Kasten05632a52012-01-03 14:22:33 -0800498 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700499 return BAD_VALUE;
500 }
501
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800502 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700503 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800504 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700505
506 return NO_ERROR;
507}
508
Glenn Kastena5224f32012-01-04 12:41:44 -0800509void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700510{
511 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800512 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700513 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514}
515
Glenn Kasten3b16c762012-11-14 08:44:39 -0800516status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517{
John Grossman4ff14ba2012-02-08 16:37:41 -0800518 if (mIsTimed) {
519 return INVALID_OPERATION;
520 }
521
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800522 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700524 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525 }
526 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700527 if (rate == 0 || rate > afSamplingRate*2 ) {
528 return BAD_VALUE;
529 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530
Eric Laurent1703cdf2011-03-07 14:52:59 -0800531 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800532 mSampleRate = rate;
533 mProxy->setSampleRate(rate);
534
Eric Laurent57326622009-07-07 07:10:45 -0700535 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536}
537
Glenn Kastena5224f32012-01-04 12:41:44 -0800538uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539{
John Grossman4ff14ba2012-02-08 16:37:41 -0800540 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800541 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800542 }
543
Eric Laurent1703cdf2011-03-07 14:52:59 -0800544 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800545 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546}
547
548status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
549{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800550 if (mSharedBuffer == 0 || mIsTimed) {
551 return INVALID_OPERATION;
552 }
553
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800555 ;
556 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
557 loopEnd - loopStart >= MIN_LOOP) {
558 ;
559 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560 return BAD_VALUE;
561 }
562
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800563 AutoMutex lock(mLock);
564 // See setPosition() regarding setting parameters such as loop points or position while active
565 if (mState == STATE_ACTIVE) {
566 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700567 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800568 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800569 return NO_ERROR;
570}
571
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800572void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
573{
574 // FIXME If setting a loop also sets position to start of loop, then
575 // this is correct. Otherwise it should be removed.
576 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
577 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
578 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
579}
580
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581status_t AudioTrack::setMarkerPosition(uint32_t marker)
582{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700583 if (mCbf == NULL) {
584 return INVALID_OPERATION;
585 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800587 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 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
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800600 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601 *marker = mMarkerPosition;
602
603 return NO_ERROR;
604}
605
606status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
607{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700608 if (mCbf == NULL) {
609 return INVALID_OPERATION;
610 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800612 AutoMutex lock(mLock);
613 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614 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
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800625 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626 *updatePeriod = mUpdatePeriod;
627
628 return NO_ERROR;
629}
630
631status_t AudioTrack::setPosition(uint32_t position)
632{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800633 if (mSharedBuffer == 0 || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700634 return INVALID_OPERATION;
635 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800636 if (position > mFrameCount) {
637 return BAD_VALUE;
638 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800639
Eric Laurent1703cdf2011-03-07 14:52:59 -0800640 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800641 // Currently we require that the player is inactive before setting parameters such as position
642 // or loop points. Otherwise, there could be a race condition: the application could read the
643 // current position, compute a new position or loop parameters, and then set that position or
644 // loop parameters but it would do the "wrong" thing since the position has continued to advance
645 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
646 // to specify how it wants to handle such scenarios.
647 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700648 return INVALID_OPERATION;
649 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800650 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
651 mLoopPeriod = 0;
652 // FIXME Check whether loops and setting position are incompatible in old code.
653 // If we use setLoop for both purposes we lose the capability to set the position while looping.
654 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700655
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 return NO_ERROR;
657}
658
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800659status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700661 if (position == NULL) {
662 return BAD_VALUE;
663 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800664
Eric Laurent1703cdf2011-03-07 14:52:59 -0800665 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800666 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
667 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
668 mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669
670 return NO_ERROR;
671}
672
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800673status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800674{
675 if (mSharedBuffer == 0 || mIsTimed) {
676 return INVALID_OPERATION;
677 }
678 if (position == NULL) {
679 return BAD_VALUE;
680 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800681
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800682 AutoMutex lock(mLock);
683 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800684 return NO_ERROR;
685}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800686
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687status_t AudioTrack::reload()
688{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800689 if (mSharedBuffer == 0 || mIsTimed) {
690 return INVALID_OPERATION;
691 }
692
Eric Laurent1703cdf2011-03-07 14:52:59 -0800693 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800694 // See setPosition() regarding setting parameters such as loop points or position while active
695 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700696 return INVALID_OPERATION;
697 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800698 mNewPosition = mUpdatePeriod;
699 mLoopPeriod = 0;
700 // FIXME The new code cannot reload while keeping a loop specified.
701 // Need to check how the old code handled this, and whether it's a significant change.
702 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703 return NO_ERROR;
704}
705
Eric Laurentc2f1f072009-07-17 12:17:14 -0700706audio_io_handle_t AudioTrack::getOutput()
707{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800708 AutoMutex lock(mLock);
709 return getOutput_l();
710}
711
712// must be called with mLock held
713audio_io_handle_t AudioTrack::getOutput_l()
714{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800715 return AudioSystem::getOutput(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -0800716 mSampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700717}
718
Eric Laurentbe916aa2010-06-01 23:49:17 -0700719status_t AudioTrack::attachAuxEffect(int effectId)
720{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800721 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700722 status_t status = mAudioTrack->attachAuxEffect(effectId);
723 if (status == NO_ERROR) {
724 mAuxEffectId = effectId;
725 }
726 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700727}
728
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729// -------------------------------------------------------------------------
730
Eric Laurent1703cdf2011-03-07 14:52:59 -0800731// must be called with mLock held
732status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800733 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800734 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800735 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800736 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700737 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800738 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800739 audio_io_handle_t output,
740 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800741{
742 status_t status;
743 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
744 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700745 ALOGE("Could not get audioflinger");
746 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800747 }
748
Eric Laurentd1b449a2010-05-14 03:26:45 -0700749 uint32_t afLatency;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800750 if ((status = AudioSystem::getLatency(output, streamType, &afLatency)) != NO_ERROR) {
751 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700752 return NO_INIT;
753 }
754
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700755 // Client decides whether the track is TIMED (see below), but can only express a preference
756 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700757 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700758 // either of these use cases:
759 // use case 1: shared buffer
760 (sharedBuffer != 0) ||
761 // use case 2: callback handler
762 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800763 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700764 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700765 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700766 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700767 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700768 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700769
Eric Laurentd1b449a2010-05-14 03:26:45 -0700770 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700771
Dima Zavinfce7a472011-04-19 22:30:36 -0700772 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700773
Eric Laurentd1b449a2010-05-14 03:26:45 -0700774 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700775 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700776 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700777 } else if (frameCount == 0) {
Glenn Kastene33054e2012-11-14 12:54:39 -0800778 size_t afFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800779 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
780 if (status != NO_ERROR) {
781 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType,
782 status);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700783 return NO_INIT;
784 }
785 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700786 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700787
788 } else if (sharedBuffer != 0) {
789
Glenn Kastena42ff002012-11-14 12:47:55 -0800790 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700791 // 8-bit data in shared memory is not currently supported by AudioFlinger
792 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800793 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700794 // More than 2 channels does not require stronger alignment than stereo
795 alignment <<= 1;
796 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800797 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
798 ALOGE("Invalid buffer alignment: address %p, channel count %u",
799 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700800 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.
Glenn Kastena42ff002012-11-14 12:47:55 -0800807 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700808
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 Kasten9f80dd22012-12-18 15:57:32 -0800813 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
814 if (status != NO_ERROR) {
815 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType,
816 status);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700817 return NO_INIT;
818 }
Glenn Kastene33054e2012-11-14 12:54:39 -0800819 size_t afFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800820 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
821 if (status != NO_ERROR) {
822 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700823 return NO_INIT;
824 }
825
Eric Laurentd1b449a2010-05-14 03:26:45 -0700826 // Ensure that buffer depth covers at least audio hardware latency
827 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700828 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
829 afFrameCount, minBufCount, afSampleRate, afLatency);
830 if (minBufCount <= 2) {
831 minBufCount = sampleRate == afSampleRate ? 2 : 3;
Glenn Kasten7c027242012-12-26 14:43:16 -0800832 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700833
Glenn Kastene33054e2012-11-14 12:54:39 -0800834 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
835 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800836 ", afLatency=%d",
837 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700838
839 if (frameCount == 0) {
840 frameCount = minFrameCount;
841 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700842 // Make sure that application is notified with sufficient margin
843 // before underrun
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800844 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700845 mNotificationFramesAct = frameCount/2;
846 }
847 if (frameCount < minFrameCount) {
848 // not ALOGW because it happens all the time when playing key clicks over A2DP
849 ALOGV("Minimum buffer size corrected from %d to %d",
850 frameCount, minFrameCount);
851 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800852 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700853
Glenn Kastene0fa4672012-04-24 14:35:14 -0700854 } else {
855 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700856 }
857
Glenn Kastena075db42012-03-06 11:22:44 -0800858 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
859 if (mIsTimed) {
860 trackFlags |= IAudioFlinger::TRACK_TIMED;
861 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800862
863 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700864 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700865 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800866 if (mAudioTrackThread != 0) {
867 tid = mAudioTrackThread->getTid();
868 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700869 }
870
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800871 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800872 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700873 // AudioFlinger only sees 16-bit PCM
874 format == AUDIO_FORMAT_PCM_8_BIT ?
875 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800876 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800877 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800878 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800879 sharedBuffer,
880 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800881 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700882 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800883 &status);
884
885 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000886 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800887 return status;
888 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700889 sp<IMemory> iMem = track->getCblk();
890 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000891 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800892 return NO_INIT;
893 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800894 if (mAudioTrack != 0) {
895 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
896 mDeathNotifier.clear();
897 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800898 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700899 mCblkMemory = iMem;
900 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
901 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800902 size_t temp = cblk->frameCount_;
903 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
904 // In current design, AudioTrack client checks and ensures frame count validity before
905 // passing it to AudioFlinger so AudioFlinger should not return a different value except
906 // for fast track as it uses a special method of assigning frame count.
907 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
908 }
909 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -0700910 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800911 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800912 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800913 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -0700914 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800915 if (sharedBuffer == 0) {
916 // double-buffering is not required for fast tracks, due to tighter scheduling
917 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount) {
918 mNotificationFramesAct = frameCount;
919 }
920 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800921 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -0800922 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700923 // once denied, do not request again if IAudioTrack is re-created
924 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
925 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800926 if (sharedBuffer == 0) {
927 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) {
928 mNotificationFramesAct = frameCount/2;
929 }
930 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700931 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800932 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800933 mRefreshRemaining = true;
934
935 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
936 // is the value of pointer() for the shared buffer, otherwise buffers points
937 // immediately after the control block. This address is for the mapping within client
938 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
939 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800940 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800941 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800942 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800943 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800944 }
945
Eric Laurent2beeb502010-07-16 07:43:46 -0700946 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700947 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -0800948 mLatency = afLatency + (1000*frameCount) / sampleRate;
949 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700950 // If IAudioTrack is re-created, don't let the requested frameCount
951 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -0800952 if (frameCount > mReqFrameCount) {
953 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700954 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800955
956 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800957 if (sharedBuffer == 0) {
958 mStaticProxy.clear();
959 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
960 } else {
961 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
962 mProxy = mStaticProxy;
963 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800964 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
965 uint16_t(mVolume[LEFT] * 0x1000));
966 mProxy->setSendLevel(mSendLevel);
967 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800968 mProxy->setEpoch(epoch);
969 mProxy->setMinimum(mNotificationFramesAct);
970
971 mDeathNotifier = new DeathNotifier(this);
972 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800973
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800974 return NO_ERROR;
975}
976
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
978{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800979 if (audioBuffer == NULL) {
980 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -0700981 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800982 if (mTransfer != TRANSFER_OBTAIN) {
983 audioBuffer->frameCount = 0;
984 audioBuffer->size = 0;
985 audioBuffer->raw = NULL;
986 return INVALID_OPERATION;
987 }
Eric Laurent9b7d9502011-03-21 11:49:00 -0700988
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800989 const struct timespec *requested;
990 if (waitCount == -1) {
991 requested = &ClientProxy::kForever;
992 } else if (waitCount == 0) {
993 requested = &ClientProxy::kNonBlocking;
994 } else if (waitCount > 0) {
995 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
996 struct timespec timeout;
997 timeout.tv_sec = ms / 1000;
998 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
999 requested = &timeout;
1000 } else {
1001 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1002 requested = NULL;
1003 }
1004 return obtainBuffer(audioBuffer, requested);
1005}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001006
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001007status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1008 struct timespec *elapsed, size_t *nonContig)
1009{
1010 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1011 uint32_t oldSequence = 0;
1012 uint32_t newSequence;
1013
1014 Proxy::Buffer buffer;
1015 status_t status = NO_ERROR;
1016
1017 static const int32_t kMaxTries = 5;
1018 int32_t tryCounter = kMaxTries;
1019
1020 do {
1021 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1022 // keep them from going away if another thread re-creates the track during obtainBuffer()
1023 sp<AudioTrackClientProxy> proxy;
1024 sp<IMemory> iMem;
1025
1026 { // start of lock scope
1027 AutoMutex lock(mLock);
1028
1029 newSequence = mSequence;
1030 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1031 if (status == DEAD_OBJECT) {
1032 // re-create track, unless someone else has already done so
1033 if (newSequence == oldSequence) {
1034 status = restoreTrack_l("obtainBuffer");
1035 if (status != NO_ERROR) {
1036 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001037 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038 }
1039 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001040 oldSequence = newSequence;
1041
1042 // Keep the extra references
1043 proxy = mProxy;
1044 iMem = mCblkMemory;
1045
1046 // Non-blocking if track is stopped or paused
1047 if (mState != STATE_ACTIVE) {
1048 requested = &ClientProxy::kNonBlocking;
1049 }
1050
1051 } // end of lock scope
1052
1053 buffer.mFrameCount = audioBuffer->frameCount;
1054 // FIXME starts the requested timeout and elapsed over from scratch
1055 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1056
1057 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1058
1059 audioBuffer->frameCount = buffer.mFrameCount;
1060 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1061 audioBuffer->raw = buffer.mRaw;
1062 if (nonContig != NULL) {
1063 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001064 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001065 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001066}
1067
1068void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1069{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001070 if (mTransfer == TRANSFER_SHARED) {
1071 return;
1072 }
1073
1074 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1075 if (stepCount == 0) {
1076 return;
1077 }
1078
1079 Proxy::Buffer buffer;
1080 buffer.mFrameCount = stepCount;
1081 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001082
Eric Laurent1703cdf2011-03-07 14:52:59 -08001083 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001084 mInUnderrun = false;
1085 mProxy->releaseBuffer(&buffer);
1086
1087 // restart track if it was disabled by audioflinger due to previous underrun
1088 if (mState == STATE_ACTIVE) {
1089 audio_track_cblk_t* cblk = mCblk;
1090 if (android_atomic_and(~CBLK_DISABLED, &cblk->flags) & CBLK_DISABLED) {
1091 ALOGW("releaseBuffer() track %p name=%#x disabled due to previous underrun, restarting",
1092 this, cblk->mName);
1093 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001094 mAudioTrack->start();
1095 }
1096 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001097}
1098
1099// -------------------------------------------------------------------------
1100
1101ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1102{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001103 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001104 return INVALID_OPERATION;
1105 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001106
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001107 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001108 // Sanity-check: user is most-likely passing an error code, and it would
1109 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001110 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001111 return BAD_VALUE;
1112 }
1113
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001114 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115 Buffer audioBuffer;
1116
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001117 while (userSize >= mFrameSize) {
1118 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001119
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001120 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001121 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001122 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001123 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001124 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001125 return ssize_t(err);
1126 }
1127
1128 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001129 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001130 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001131 toWrite = audioBuffer.size >> 1;
1132 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001133 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001134 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001135 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001136 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001137 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001138 userSize -= toWrite;
1139 written += toWrite;
1140
1141 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001142 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001143
1144 return written;
1145}
1146
1147// -------------------------------------------------------------------------
1148
John Grossman4ff14ba2012-02-08 16:37:41 -08001149TimedAudioTrack::TimedAudioTrack() {
1150 mIsTimed = true;
1151}
1152
1153status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1154{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001155 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001156 status_t result = UNKNOWN_ERROR;
1157
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001158#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001159 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1160 // while we are accessing the cblk
1161 sp<IAudioTrack> audioTrack = mAudioTrack;
1162 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001163#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001164
John Grossman4ff14ba2012-02-08 16:37:41 -08001165 // If the track is not invalid already, try to allocate a buffer. alloc
1166 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001167 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001168 audio_track_cblk_t* cblk = mCblk;
1169 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001170 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1171 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001172 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001173 }
1174 }
1175
1176 // If the track is invalid at this point, attempt to restore it. and try the
1177 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001178 if (cblk->flags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001179 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001180
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001181 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001182 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001183 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001184 }
1185
1186 return result;
1187}
1188
1189status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1190 int64_t pts)
1191{
Eric Laurentdf839842012-05-31 14:27:14 -07001192 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1193 {
1194 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001195 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001196 // restart track if it was disabled by audioflinger due to previous underrun
1197 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001198 (mState == STATE_ACTIVE) && (cblk->flags & CBLK_DISABLED)) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001199 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001200 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001201 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001202 mAudioTrack->start();
1203 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001204 }
Eric Laurentdf839842012-05-31 14:27:14 -07001205 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001206}
1207
1208status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1209 TargetTimeline target)
1210{
1211 return mAudioTrack->setMediaTimeTransform(xform, target);
1212}
1213
1214// -------------------------------------------------------------------------
1215
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001216nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001218 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001219 if (mAwaitBoost) {
1220 mAwaitBoost = false;
1221 mLock.unlock();
1222 static const int32_t kMaxTries = 5;
1223 int32_t tryCounter = kMaxTries;
1224 uint32_t pollUs = 10000;
1225 do {
1226 int policy = sched_getscheduler(0);
1227 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1228 break;
1229 }
1230 usleep(pollUs);
1231 pollUs <<= 1;
1232 } while (tryCounter-- > 0);
1233 if (tryCounter < 0) {
1234 ALOGE("did not receive expected priority boost on time");
1235 }
1236 return true;
1237 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001238
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001239 // Can only reference mCblk while locked
1240 int32_t flags = android_atomic_and(
1241 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->flags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001242
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001243 // Check for track invalidation
1244 if (flags & CBLK_INVALID) {
1245 (void) restoreTrack_l("processAudioBuffer");
1246 mLock.unlock();
1247 // Run again immediately, but with a new IAudioTrack
1248 return 0;
1249 }
1250
1251 bool active = mState == STATE_ACTIVE;
1252
1253 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1254 bool newUnderrun = false;
1255 if (flags & CBLK_UNDERRUN) {
1256#if 0
1257 // Currently in shared buffer mode, when the server reaches the end of buffer,
1258 // the track stays active in continuous underrun state. It's up to the application
1259 // to pause or stop the track, or set the position to a new offset within buffer.
1260 // This was some experimental code to auto-pause on underrun. Keeping it here
1261 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1262 if (mTransfer == TRANSFER_SHARED) {
1263 mState = STATE_PAUSED;
1264 active = false;
1265 }
1266#endif
1267 if (!mInUnderrun) {
1268 mInUnderrun = true;
1269 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 }
1271 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001272
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001273 // Get current position of server
1274 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275
1276 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001277 bool markerReached = false;
1278 size_t markerPosition = mMarkerPosition;
1279 // FIXME fails for wraparound, need 64 bits
1280 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1281 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001282 }
1283
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001284 // Determine number of new position callback(s) that will be needed, while locked
1285 size_t newPosCount = 0;
1286 size_t newPosition = mNewPosition;
1287 size_t updatePeriod = mUpdatePeriod;
1288 // FIXME fails for wraparound, need 64 bits
1289 if (updatePeriod > 0 && position >= newPosition) {
1290 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1291 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292 }
1293
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001294 // Cache other fields that will be needed soon
1295 uint32_t loopPeriod = mLoopPeriod;
1296 uint32_t sampleRate = mSampleRate;
1297 size_t notificationFrames = mNotificationFramesAct;
1298 if (mRefreshRemaining) {
1299 mRefreshRemaining = false;
1300 mRemainingFrames = notificationFrames;
1301 mRetryOnPartialBuffer = false;
1302 }
1303 size_t misalignment = mProxy->getMisalignment();
1304 int32_t sequence = mSequence;
1305
1306 // These fields don't need to be cached, because they are assigned only by set():
1307 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1308 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1309
1310 mLock.unlock();
1311
1312 // perform callbacks while unlocked
1313 if (newUnderrun) {
1314 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1315 }
1316 // FIXME we will miss loops if loop cycle was signaled several times since last call
1317 // to processAudioBuffer()
1318 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1319 mCbf(EVENT_LOOP_END, mUserData, NULL);
1320 }
1321 if (flags & CBLK_BUFFER_END) {
1322 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1323 }
1324 if (markerReached) {
1325 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1326 }
1327 while (newPosCount > 0) {
1328 size_t temp = newPosition;
1329 mCbf(EVENT_NEW_POS, mUserData, &temp);
1330 newPosition += updatePeriod;
1331 newPosCount--;
1332 }
1333 if (mObservedSequence != sequence) {
1334 mObservedSequence = sequence;
1335 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001336 }
1337
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001338 // if inactive, then don't run me again until re-started
1339 if (!active) {
1340 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001341 }
1342
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001343 // Compute the estimated time until the next timed event (position, markers, loops)
1344 // FIXME only for non-compressed audio
1345 uint32_t minFrames = ~0;
1346 if (!markerReached && position < markerPosition) {
1347 minFrames = markerPosition - position;
1348 }
1349 if (loopPeriod > 0 && loopPeriod < minFrames) {
1350 minFrames = loopPeriod;
1351 }
1352 if (updatePeriod > 0 && updatePeriod < minFrames) {
1353 minFrames = updatePeriod;
1354 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001355
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001356 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1357 static const uint32_t kPoll = 0;
1358 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1359 minFrames = kPoll * notificationFrames;
1360 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001361
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001362 // Convert frame units to time units
1363 nsecs_t ns = NS_WHENEVER;
1364 if (minFrames != (uint32_t) ~0) {
1365 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1366 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1367 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1368 }
1369
1370 // If not supplying data by EVENT_MORE_DATA, then we're done
1371 if (mTransfer != TRANSFER_CALLBACK) {
1372 return ns;
1373 }
1374
1375 struct timespec timeout;
1376 const struct timespec *requested = &ClientProxy::kForever;
1377 if (ns != NS_WHENEVER) {
1378 timeout.tv_sec = ns / 1000000000LL;
1379 timeout.tv_nsec = ns % 1000000000LL;
1380 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1381 requested = &timeout;
1382 }
1383
1384 while (mRemainingFrames > 0) {
1385
1386 Buffer audioBuffer;
1387 audioBuffer.frameCount = mRemainingFrames;
1388 size_t nonContig;
1389 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1390 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1391 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1392 requested = &ClientProxy::kNonBlocking;
1393 size_t avail = audioBuffer.frameCount + nonContig;
1394 ALOGV("obtainBuffer(%u) returned %u = %u + %u",
1395 mRemainingFrames, avail, audioBuffer.frameCount, nonContig);
1396 if (err != NO_ERROR) {
1397 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR) {
1398 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001399 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001400 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1401 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001402 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001403
1404 if (mRetryOnPartialBuffer) {
1405 mRetryOnPartialBuffer = false;
1406 if (avail < mRemainingFrames) {
1407 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1408 if (ns < 0 || myns < ns) {
1409 ns = myns;
1410 }
1411 return ns;
1412 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001413 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001414
1415 // Divide buffer size by 2 to take into account the expansion
1416 // due to 8 to 16 bit conversion: the callback must fill only half
1417 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001418 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419 audioBuffer.size >>= 1;
1420 }
1421
1422 size_t reqSize = audioBuffer.size;
1423 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001424 size_t writtenSize = audioBuffer.size;
1425 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426
1427 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001428 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1429 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1430 reqSize, (int) writtenSize);
1431 return NS_NEVER;
1432 }
1433
1434 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001435 // The callback is done filling buffers
1436 // Keep this thread going to handle timed events and
1437 // still try to get more data in intervals of WAIT_PERIOD_MS
1438 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001439 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001440 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001442 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001443 // 8 to 16 bit conversion, note that source and destination are the same address
1444 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001445 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001446 }
1447
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001448 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1449 audioBuffer.frameCount = releasedFrames;
1450 mRemainingFrames -= releasedFrames;
1451 if (misalignment >= releasedFrames) {
1452 misalignment -= releasedFrames;
1453 } else {
1454 misalignment = 0;
1455 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456
1457 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001459 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1460 // if callback doesn't like to accept the full chunk
1461 if (writtenSize < reqSize) {
1462 continue;
1463 }
1464
1465 // There could be enough non-contiguous frames available to satisfy the remaining request
1466 if (mRemainingFrames <= nonContig) {
1467 continue;
1468 }
1469
1470#if 0
1471 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1472 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1473 // that total to a sum == notificationFrames.
1474 if (0 < misalignment && misalignment <= mRemainingFrames) {
1475 mRemainingFrames = misalignment;
1476 return (mRemainingFrames * 1100000000LL) / sampleRate;
1477 }
1478#endif
1479
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001481 mRemainingFrames = notificationFrames;
1482 mRetryOnPartialBuffer = true;
1483
1484 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1485 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486}
1487
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001488status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001489{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001490 ALOGW("dead IAudioTrack, creating a new one from %s()", from);
1491 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001492 status_t result;
1493
Glenn Kastena47f3162012-11-07 10:13:08 -08001494 // refresh the audio configuration cache in this process to make sure we get new
1495 // output parameters in getOutput_l() and createTrack_l()
1496 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001497
Glenn Kastena47f3162012-11-07 10:13:08 -08001498 // if the new IAudioTrack is created, createTrack_l() will modify the
1499 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1500 // It will also delete the strong references on previous IAudioTrack and IMemory
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001501 size_t position = mProxy->getPosition();
1502 mNewPosition = position + mUpdatePeriod;
1503 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001504 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001505 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001506 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001507 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001508 mFlags,
1509 mSharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001510 getOutput_l(),
1511 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001512
Glenn Kastena47f3162012-11-07 10:13:08 -08001513 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001514 // continue playback from last known position, but
1515 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1516 if (mStaticProxy != NULL) {
1517 mLoopPeriod = 0;
1518 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1519 }
1520 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1521 // track destruction have been played? This is critical for SoundPool implementation
1522 // This must be broken, and needs to be tested/debugged.
1523#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001524 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001525 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001526 // Make sure that a client relying on callback events indicating underrun or
1527 // the actual amount of audio frames played (e.g SoundPool) receives them.
1528 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001529 // restart playback even if buffer is not completely filled.
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001530 android_atomic_or(CBLK_FORCEREADY, &mCblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001531 }
1532 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001533#endif
1534 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001535 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001536 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001537 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001538 if (result != NO_ERROR) {
1539 ALOGW("restoreTrack_l() failed status %d", result);
1540 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001541 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001542
1543 return result;
1544}
1545
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001546status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1547{
1548
1549 const size_t SIZE = 256;
1550 char buffer[SIZE];
1551 String8 result;
1552
1553 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001554 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1555 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001556 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001557 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001558 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001560 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001562 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001563 result.append(buffer);
1564 ::write(fd, result.string(), result.size());
1565 return NO_ERROR;
1566}
1567
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001568uint32_t AudioTrack::getUnderrunFrames() const
1569{
1570 AutoMutex lock(mLock);
1571 return mProxy->getUnderrunFrames();
1572}
1573
1574// =========================================================================
1575
1576void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1577{
1578 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1579 if (audioTrack != 0) {
1580 AutoMutex lock(audioTrack->mLock);
1581 audioTrack->mProxy->binderDied();
1582 }
1583}
1584
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001585// =========================================================================
1586
1587AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001588 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mResumeLatch(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001589{
1590}
1591
1592AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001593{
1594}
1595
1596bool AudioTrack::AudioTrackThread::threadLoop()
1597{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001598 {
1599 AutoMutex _l(mMyLock);
1600 if (mPaused) {
1601 mMyCond.wait(mMyLock);
1602 // caller will check for exitPending()
1603 return true;
1604 }
1605 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001606 nsecs_t ns = mReceiver.processAudioBuffer(this);
1607 switch (ns) {
1608 case 0:
1609 return true;
1610 case NS_WHENEVER:
1611 sleep(1);
1612 return true;
1613 case NS_INACTIVE:
1614 pauseConditional();
1615 return true;
1616 case NS_NEVER:
1617 return false;
1618 default:
1619 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
1620 struct timespec req;
1621 req.tv_sec = ns / 1000000000LL;
1622 req.tv_nsec = ns % 1000000000LL;
1623 nanosleep(&req, NULL /*rem*/);
1624 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001625 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001626}
1627
Glenn Kasten3acbd052012-02-28 10:39:56 -08001628void AudioTrack::AudioTrackThread::requestExit()
1629{
1630 // must be in this order to avoid a race condition
1631 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001632 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001633}
1634
1635void AudioTrack::AudioTrackThread::pause()
1636{
1637 AutoMutex _l(mMyLock);
1638 mPaused = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001639 mResumeLatch = false;
1640}
1641
1642void AudioTrack::AudioTrackThread::pauseConditional()
1643{
1644 AutoMutex _l(mMyLock);
1645 if (mResumeLatch) {
1646 mResumeLatch = false;
1647 } else {
1648 mPaused = true;
1649 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001650}
1651
1652void AudioTrack::AudioTrackThread::resume()
1653{
1654 AutoMutex _l(mMyLock);
1655 if (mPaused) {
1656 mPaused = false;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001657 mResumeLatch = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001658 mMyCond.signal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001659 } else {
1660 mResumeLatch = true;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001661 }
1662}
1663
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001664}; // namespace android