blob: a6ffc62e0a94f0e4c18057668eeffbe3ed0b52e2 [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>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010030#define WAIT_PERIOD_MS 10
31#define WAIT_STREAM_END_TIMEOUT_SEC 120
32
Glenn Kasten511754b2012-01-11 09:52:19 -080033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080035// ---------------------------------------------------------------------------
36
37// static
38status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080039 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080040 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080041 uint32_t sampleRate)
42{
Glenn Kastend65d73c2012-06-22 17:21:07 -070043 if (frameCount == NULL) {
44 return BAD_VALUE;
45 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070046
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;
Glenn Kasten66a04672014-01-08 08:53:44 -080054 status_t status;
55 status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
56 if (status != NO_ERROR) {
57 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080058 }
Glenn Kastene33054e2012-11-14 12:54:39 -080059 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080060 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
61 if (status != NO_ERROR) {
62 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080063 }
64 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080065 status = AudioSystem::getOutputLatency(&afLatency, streamType);
66 if (status != NO_ERROR) {
67 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 }
69
70 // Ensure that buffer depth covers at least audio hardware latency
71 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080072 if (minBufCount < 2) {
73 minBufCount = 2;
74 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080075
76 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070077 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080078 // The formula above should always produce a non-zero value, but return an error
79 // in the unlikely event that it does not, as that's part of the API contract.
80 if (*frameCount == 0) {
81 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d",
82 streamType, sampleRate);
83 return BAD_VALUE;
84 }
Glenn Kasten3acbd052012-02-28 10:39:56 -080085 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
86 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080087 return NO_ERROR;
88}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089
90// ---------------------------------------------------------------------------
91
92AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070093 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080094 mIsTimed(false),
95 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -080096 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097{
98}
99
100AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800101 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800103 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700104 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700106 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 callback_t cbf,
108 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700109 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800110 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000111 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800112 const audio_offload_info_t *offloadInfo,
113 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700114 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800115 mIsTimed(false),
116 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800117 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700119 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700120 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800121 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
122 offloadInfo, uid);
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,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800135 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000136 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800137 const audio_offload_info_t *offloadInfo,
138 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700139 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mIsTimed(false),
141 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800142 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700144 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800145 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800146 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147}
148
149AudioTrack::~AudioTrack()
150{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 if (mStatus == NO_ERROR) {
152 // Make sure that callback function exits in the case where
153 // it is looping on buffer full condition in obtainBuffer().
154 // Otherwise the callback thread will never exit.
155 stop();
156 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100157 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800158 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 mAudioTrackThread->requestExitAndWait();
160 mAudioTrackThread.clear();
161 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700162 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
163 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700165 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 }
167}
168
169status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800170 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800172 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700173 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800174 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700175 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 callback_t cbf,
177 void* user,
178 int notificationFrames,
179 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700180 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800181 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000182 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800183 const audio_offload_info_t *offloadInfo,
184 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800186 switch (transferType) {
187 case TRANSFER_DEFAULT:
188 if (sharedBuffer != 0) {
189 transferType = TRANSFER_SHARED;
190 } else if (cbf == NULL || threadCanCallJava) {
191 transferType = TRANSFER_SYNC;
192 } else {
193 transferType = TRANSFER_CALLBACK;
194 }
195 break;
196 case TRANSFER_CALLBACK:
197 if (cbf == NULL || sharedBuffer != 0) {
198 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
199 return BAD_VALUE;
200 }
201 break;
202 case TRANSFER_OBTAIN:
203 case TRANSFER_SYNC:
204 if (sharedBuffer != 0) {
205 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
206 return BAD_VALUE;
207 }
208 break;
209 case TRANSFER_SHARED:
210 if (sharedBuffer == 0) {
211 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
212 return BAD_VALUE;
213 }
214 break;
215 default:
216 ALOGE("Invalid transfer type %d", transferType);
217 return BAD_VALUE;
218 }
219 mTransfer = transferType;
220
Glenn Kastene33054e2012-11-14 12:54:39 -0800221 // FIXME "int" here is legacy and will be replaced by size_t later
222 if (frameCountInt < 0) {
223 ALOGE("Invalid frame count %d", frameCountInt);
224 return BAD_VALUE;
225 }
226 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700228 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
229 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230
Glenn Kastene33054e2012-11-14 12:54:39 -0800231 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700232
Eric Laurent1703cdf2011-03-07 14:52:59 -0800233 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800234
Glenn Kasten53cec222013-08-29 09:01:02 -0700235 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700236 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000237 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238 return INVALID_OPERATION;
239 }
240
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100241 mOutput = 0;
242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 if (streamType == AUDIO_STREAM_DEFAULT) {
245 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800246 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800249 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700250 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
251 return NO_INIT;
252 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 sampleRate = afSampleRate;
254 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800255 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700256
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800258 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261
262 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700263 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800264 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 return BAD_VALUE;
266 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700267
Glenn Kasten8ba90322013-10-30 11:29:27 -0700268 if (!audio_is_output_channel(channelMask)) {
269 ALOGE("Invalid channel mask %#x", channelMask);
270 return BAD_VALUE;
271 }
272
Glenn Kastene0fa4672012-04-24 14:35:14 -0700273 // AudioFlinger does not currently support 8-bit data in shared memory
274 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
275 ALOGE("8-bit data in shared memory is not supported");
276 return BAD_VALUE;
277 }
278
Eric Laurentc2f1f072009-07-17 12:17:14 -0700279 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100280 // or offload was requested
281 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
282 || !audio_is_linear_pcm(format)) {
283 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
284 ? "Offload request, forcing to Direct Output"
285 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700286 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800287 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700288 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700289 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700290 // only allow deep buffering for music stream type
291 if (streamType != AUDIO_STREAM_MUSIC) {
292 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
293 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294
Glenn Kastena42ff002012-11-14 12:47:55 -0800295 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700296 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800297 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700298
Glenn Kastene3aa6592012-12-04 12:22:46 -0800299 if (audio_is_linear_pcm(format)) {
300 mFrameSize = channelCount * audio_bytes_per_sample(format);
301 mFrameSizeAF = channelCount * sizeof(int16_t);
302 } else {
303 mFrameSize = sizeof(uint8_t);
304 mFrameSizeAF = sizeof(uint8_t);
305 }
306
Dima Zavinfce7a472011-04-19 22:30:36 -0700307 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800308 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800309 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000310 flags,
311 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312
313 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000314 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315 return BAD_VALUE;
316 }
317
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800318 mVolume[LEFT] = 1.0f;
319 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800320 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700321 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800322 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700323 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800324 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700325 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800326 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
327 mClientUid = IPCThreadState::self()->getCallingUid();
328 } else {
329 mClientUid = uid;
330 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700331 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700332 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700333 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700334
Glenn Kastena997e7a2012-08-07 09:44:19 -0700335 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700336 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700337 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
338 }
339
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800340 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800341 status_t status = createTrack_l(streamType,
342 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800343 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800344 frameCount,
345 flags,
346 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800347 output,
348 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800349
Glenn Kastena997e7a2012-08-07 09:44:19 -0700350 if (status != NO_ERROR) {
351 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100352 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
353 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700354 mAudioTrackThread.clear();
355 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100356 //Use of direct and offloaded output streams is ref counted by audio policy manager.
357 // As getOutput was called above and resulted in an output stream to be opened,
358 // we need to release it.
359 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700360 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700361 }
362
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800365 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800367 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800369 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700371 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 mNewPosition = 0;
373 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700374 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800375 mSequence = 1;
376 mObservedSequence = mSequence;
377 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100378 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800379
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380 return NO_ERROR;
381}
382
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383// -------------------------------------------------------------------------
384
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100385status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800387 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100388
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100390 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 }
392
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800393 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100396 if (previousState == STATE_PAUSED_STOPPING) {
397 mState = STATE_STOPPING;
398 } else {
399 mState = STATE_ACTIVE;
400 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800401 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
402 // reset current position as seen by client to 0
403 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700404 // force refresh of remaining frames by processAudioBuffer() as last
405 // write before stop could be partial.
406 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800407 }
408 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700409 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800410
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800412 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100413 if (previousState == STATE_STOPPING) {
414 mProxy->interrupt();
415 } else {
416 t->resume();
417 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800418 } else {
419 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
420 get_sched_policy(0, &mPreviousSchedulingGroup);
421 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
422 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800424 status_t status = NO_ERROR;
425 if (!(flags & CBLK_INVALID)) {
426 status = mAudioTrack->start();
427 if (status == DEAD_OBJECT) {
428 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800430 }
431 if (flags & CBLK_INVALID) {
432 status = restoreTrack_l("start");
433 }
434
435 if (status != NO_ERROR) {
436 ALOGE("start() status %d", status);
437 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100439 if (previousState != STATE_STOPPING) {
440 t->pause();
441 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700443 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700444 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445 }
446 }
447
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100448 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800449}
450
451void AudioTrack::stop()
452{
453 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700454 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800455 return;
456 }
457
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100458 if (isOffloaded()) {
459 mState = STATE_STOPPING;
460 } else {
461 mState = STATE_STOPPED;
462 }
463
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800464 mProxy->interrupt();
465 mAudioTrack->stop();
466 // the playback head position will reset to 0, so if a marker is set, we need
467 // to activate it again
468 mMarkerReached = false;
469#if 0
470 // Force flush if a shared buffer is used otherwise audioflinger
471 // will not stop before end of buffer is reached.
472 // It may be needed to make sure that we stop playback, likely in case looping is on.
473 if (mSharedBuffer != 0) {
474 flush_l();
475 }
476#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100477
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800478 sp<AudioTrackThread> t = mAudioTrackThread;
479 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100480 if (!isOffloaded()) {
481 t->pause();
482 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800483 } else {
484 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
485 set_sched_policy(0, mPreviousSchedulingGroup);
486 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487}
488
489bool AudioTrack::stopped() const
490{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800491 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800492 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493}
494
495void AudioTrack::flush()
496{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800497 if (mSharedBuffer != 0) {
498 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800499 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800500 AutoMutex lock(mLock);
501 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
502 return;
503 }
504 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800505}
506
Eric Laurent1703cdf2011-03-07 14:52:59 -0800507void AudioTrack::flush_l()
508{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800509 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700510
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700511 // clear playback marker and periodic update counter
512 mMarkerPosition = 0;
513 mMarkerReached = false;
514 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100515 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700516
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800517 mState = STATE_FLUSHED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100518 if (isOffloaded()) {
519 mProxy->interrupt();
520 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800521 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800522 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523}
524
525void AudioTrack::pause()
526{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800527 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100528 if (mState == STATE_ACTIVE) {
529 mState = STATE_PAUSED;
530 } else if (mState == STATE_STOPPING) {
531 mState = STATE_PAUSED_STOPPING;
532 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800533 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800535 mProxy->interrupt();
536 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800537}
538
Eric Laurentbe916aa2010-06-01 23:49:17 -0700539status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800541 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700542 return BAD_VALUE;
543 }
544
Eric Laurent1703cdf2011-03-07 14:52:59 -0800545 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 mVolume[LEFT] = left;
547 mVolume[RIGHT] = right;
548
Glenn Kastene3aa6592012-12-04 12:22:46 -0800549 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700550
Eric Laurent59fe0102013-09-27 18:48:26 -0700551 if (isOffloaded()) {
552 mAudioTrack->signal();
553 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700554 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555}
556
Glenn Kastenb1c09932012-02-27 16:21:04 -0800557status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800559 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700560}
561
Eric Laurent2beeb502010-07-16 07:43:46 -0700562status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700563{
Glenn Kasten05632a52012-01-03 14:22:33 -0800564 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700565 return BAD_VALUE;
566 }
567
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800568 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700569 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800570 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571
572 return NO_ERROR;
573}
574
Glenn Kastena5224f32012-01-04 12:41:44 -0800575void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700576{
577 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800578 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700579 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800580}
581
Glenn Kasten3b16c762012-11-14 08:44:39 -0800582status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100584 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800585 return INVALID_OPERATION;
586 }
587
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800588 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700590 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591 }
592 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700593 if (rate == 0 || rate > afSamplingRate*2 ) {
594 return BAD_VALUE;
595 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596
Eric Laurent1703cdf2011-03-07 14:52:59 -0800597 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800598 mSampleRate = rate;
599 mProxy->setSampleRate(rate);
600
Eric Laurent57326622009-07-07 07:10:45 -0700601 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602}
603
Glenn Kastena5224f32012-01-04 12:41:44 -0800604uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800605{
John Grossman4ff14ba2012-02-08 16:37:41 -0800606 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800607 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800608 }
609
Eric Laurent1703cdf2011-03-07 14:52:59 -0800610 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700611
612 // sample rate can be updated during playback by the offloaded decoder so we need to
613 // query the HAL and update if needed.
614// FIXME use Proxy return channel to update the rate from server and avoid polling here
615 if (isOffloaded()) {
616 if (mOutput != 0) {
617 uint32_t sampleRate = 0;
618 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
619 if (status == NO_ERROR) {
620 mSampleRate = sampleRate;
621 }
622 }
623 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800624 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625}
626
627status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
628{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100629 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800630 return INVALID_OPERATION;
631 }
632
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800634 ;
635 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
636 loopEnd - loopStart >= MIN_LOOP) {
637 ;
638 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639 return BAD_VALUE;
640 }
641
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800642 AutoMutex lock(mLock);
643 // See setPosition() regarding setting parameters such as loop points or position while active
644 if (mState == STATE_ACTIVE) {
645 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700646 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800647 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648 return NO_ERROR;
649}
650
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800651void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
652{
653 // FIXME If setting a loop also sets position to start of loop, then
654 // this is correct. Otherwise it should be removed.
655 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
656 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
657 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
658}
659
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660status_t AudioTrack::setMarkerPosition(uint32_t marker)
661{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700662 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100663 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700664 return INVALID_OPERATION;
665 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800667 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700669 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670
671 return NO_ERROR;
672}
673
Glenn Kastena5224f32012-01-04 12:41:44 -0800674status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800675{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100676 if (isOffloaded()) {
677 return INVALID_OPERATION;
678 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700679 if (marker == NULL) {
680 return BAD_VALUE;
681 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800683 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684 *marker = mMarkerPosition;
685
686 return NO_ERROR;
687}
688
689status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
690{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700691 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100692 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700693 return INVALID_OPERATION;
694 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800696 AutoMutex lock(mLock);
697 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800699 return NO_ERROR;
700}
701
Glenn Kastena5224f32012-01-04 12:41:44 -0800702status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100704 if (isOffloaded()) {
705 return INVALID_OPERATION;
706 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700707 if (updatePeriod == NULL) {
708 return BAD_VALUE;
709 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800710
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800711 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712 *updatePeriod = mUpdatePeriod;
713
714 return NO_ERROR;
715}
716
717status_t AudioTrack::setPosition(uint32_t position)
718{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100719 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700720 return INVALID_OPERATION;
721 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800722 if (position > mFrameCount) {
723 return BAD_VALUE;
724 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800725
Eric Laurent1703cdf2011-03-07 14:52:59 -0800726 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800727 // Currently we require that the player is inactive before setting parameters such as position
728 // or loop points. Otherwise, there could be a race condition: the application could read the
729 // current position, compute a new position or loop parameters, and then set that position or
730 // loop parameters but it would do the "wrong" thing since the position has continued to advance
731 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
732 // to specify how it wants to handle such scenarios.
733 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700734 return INVALID_OPERATION;
735 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800736 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
737 mLoopPeriod = 0;
738 // FIXME Check whether loops and setting position are incompatible in old code.
739 // If we use setLoop for both purposes we lose the capability to set the position while looping.
740 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700741
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742 return NO_ERROR;
743}
744
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800745status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700747 if (position == NULL) {
748 return BAD_VALUE;
749 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800750
Eric Laurent1703cdf2011-03-07 14:52:59 -0800751 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100752 if (isOffloaded()) {
753 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100755 if (mOutput != 0) {
756 uint32_t halFrames;
757 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
758 }
759 *position = dspFrames;
760 } else {
761 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
762 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
763 mProxy->getPosition();
764 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 return NO_ERROR;
766}
767
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800768status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800769{
770 if (mSharedBuffer == 0 || mIsTimed) {
771 return INVALID_OPERATION;
772 }
773 if (position == NULL) {
774 return BAD_VALUE;
775 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800776
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800777 AutoMutex lock(mLock);
778 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800779 return NO_ERROR;
780}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800781
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800782status_t AudioTrack::reload()
783{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100784 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800785 return INVALID_OPERATION;
786 }
787
Eric Laurent1703cdf2011-03-07 14:52:59 -0800788 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800789 // See setPosition() regarding setting parameters such as loop points or position while active
790 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700791 return INVALID_OPERATION;
792 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800793 mNewPosition = mUpdatePeriod;
794 mLoopPeriod = 0;
795 // FIXME The new code cannot reload while keeping a loop specified.
796 // Need to check how the old code handled this, and whether it's a significant change.
797 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800798 return NO_ERROR;
799}
800
Eric Laurentc2f1f072009-07-17 12:17:14 -0700801audio_io_handle_t AudioTrack::getOutput()
802{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800803 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100804 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800805}
806
807// must be called with mLock held
808audio_io_handle_t AudioTrack::getOutput_l()
809{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100810 if (mOutput) {
811 return mOutput;
812 } else {
813 return AudioSystem::getOutput(mStreamType,
814 mSampleRate, mFormat, mChannelMask, mFlags);
815 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700816}
817
Eric Laurentbe916aa2010-06-01 23:49:17 -0700818status_t AudioTrack::attachAuxEffect(int effectId)
819{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800820 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700821 status_t status = mAudioTrack->attachAuxEffect(effectId);
822 if (status == NO_ERROR) {
823 mAuxEffectId = effectId;
824 }
825 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700826}
827
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800828// -------------------------------------------------------------------------
829
Eric Laurent1703cdf2011-03-07 14:52:59 -0800830// must be called with mLock held
831status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800832 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800833 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800834 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800835 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700836 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800837 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800838 audio_io_handle_t output,
839 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800840{
841 status_t status;
842 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
843 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700844 ALOGE("Could not get audioflinger");
845 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800846 }
847
Glenn Kastence8828a2013-09-16 18:07:38 -0700848 // Not all of these values are needed under all conditions, but it is easier to get them all
849
Eric Laurentd1b449a2010-05-14 03:26:45 -0700850 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700851 status = AudioSystem::getLatency(output, streamType, &afLatency);
852 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800853 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700854 return NO_INIT;
855 }
856
Glenn Kastence8828a2013-09-16 18:07:38 -0700857 size_t afFrameCount;
858 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
859 if (status != NO_ERROR) {
860 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
861 return NO_INIT;
862 }
863
864 uint32_t afSampleRate;
865 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
866 if (status != NO_ERROR) {
867 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
868 return NO_INIT;
869 }
870
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700871 // Client decides whether the track is TIMED (see below), but can only express a preference
872 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700873 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700874 // either of these use cases:
875 // use case 1: shared buffer
876 (sharedBuffer != 0) ||
877 // use case 2: callback handler
878 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800879 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700880 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700881 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700882 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700883 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700884 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885
Glenn Kastence8828a2013-09-16 18:07:38 -0700886 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800887 // n = 1 fast track with single buffering; nBuffering is ignored
888 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700889 // n = 2 normal track, no sample rate conversion
890 // n = 3 normal track, with sample rate conversion
891 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
892 // n > 3 very high latency or very small notification interval; nBuffering is ignored
893 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
894
Eric Laurentd1b449a2010-05-14 03:26:45 -0700895 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700896
Dima Zavinfce7a472011-04-19 22:30:36 -0700897 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700898
Eric Laurentd1b449a2010-05-14 03:26:45 -0700899 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700900 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700901 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700902 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700903 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700904 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100905 if (mNotificationFramesAct != frameCount) {
906 mNotificationFramesAct = frameCount;
907 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700908 } else if (sharedBuffer != 0) {
909
Glenn Kastena42ff002012-11-14 12:47:55 -0800910 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700911 // 8-bit data in shared memory is not currently supported by AudioFlinger
912 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800913 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914 // More than 2 channels does not require stronger alignment than stereo
915 alignment <<= 1;
916 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800917 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
918 ALOGE("Invalid buffer alignment: address %p, channel count %u",
919 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920 return BAD_VALUE;
921 }
922
923 // When initializing a shared buffer AudioTrack via constructors,
924 // there's no frameCount parameter.
925 // But when initializing a shared buffer AudioTrack via set(),
926 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800927 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700928
929 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
930
931 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700932
Eric Laurentd1b449a2010-05-14 03:26:45 -0700933 // Ensure that buffer depth covers at least audio hardware latency
934 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700935 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
936 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700937 if (minBufCount <= nBuffering) {
938 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800939 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700940
Glenn Kastene33054e2012-11-14 12:54:39 -0800941 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
942 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800943 ", afLatency=%d",
944 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700945
946 if (frameCount == 0) {
947 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700948 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700949 // not ALOGW because it happens all the time when playing key clicks over A2DP
950 ALOGV("Minimum buffer size corrected from %d to %d",
951 frameCount, minFrameCount);
952 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800953 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700954 // Make sure that application is notified with sufficient margin before underrun
955 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
956 mNotificationFramesAct = frameCount/nBuffering;
957 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700958
Glenn Kastene0fa4672012-04-24 14:35:14 -0700959 } else {
960 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700961 }
962
Glenn Kastena075db42012-03-06 11:22:44 -0800963 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
964 if (mIsTimed) {
965 trackFlags |= IAudioFlinger::TRACK_TIMED;
966 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800967
968 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700969 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700970 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800971 if (mAudioTrackThread != 0) {
972 tid = mAudioTrackThread->getTid();
973 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700974 }
975
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100976 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
977 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
978 }
979
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800980 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800981 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700982 // AudioFlinger only sees 16-bit PCM
983 format == AUDIO_FORMAT_PCM_8_BIT ?
984 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800985 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800986 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800987 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800988 sharedBuffer,
989 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800990 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700991 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700992 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800993 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800994 &status);
995
996 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000997 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800998 return status;
999 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001000 sp<IMemory> iMem = track->getCblk();
1001 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001002 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001003 return NO_INIT;
1004 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001005 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001006 if (mAudioTrack != 0) {
1007 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1008 mDeathNotifier.clear();
1009 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001010 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001011 mCblkMemory = iMem;
1012 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1013 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001014 size_t temp = cblk->frameCount_;
1015 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1016 // In current design, AudioTrack client checks and ensures frame count validity before
1017 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1018 // for fast track as it uses a special method of assigning frame count.
1019 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1020 }
1021 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001022 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001023 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001024 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001025 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001026 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001027 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001028 // Theoretically double-buffering is not required for fast tracks,
1029 // due to tighter scheduling. But in practice, to accommodate kernels with
1030 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1031 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1032 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001033 }
1034 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001035 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001036 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001037 // once denied, do not request again if IAudioTrack is re-created
1038 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1039 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001040 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001041 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1042 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001043 }
1044 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001045 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001046 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001047 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1048 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1049 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1050 } else {
1051 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1052 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1053 mFlags = flags;
1054 return NO_INIT;
1055 }
1056 }
1057
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001058 mRefreshRemaining = true;
1059
1060 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1061 // is the value of pointer() for the shared buffer, otherwise buffers points
1062 // immediately after the control block. This address is for the mapping within client
1063 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1064 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001065 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001066 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001067 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001068 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001069 }
1070
Eric Laurent2beeb502010-07-16 07:43:46 -07001071 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001072 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001073 mLatency = afLatency + (1000*frameCount) / sampleRate;
1074 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001075 // If IAudioTrack is re-created, don't let the requested frameCount
1076 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001077 if (frameCount > mReqFrameCount) {
1078 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001079 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001080
1081 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001082 if (sharedBuffer == 0) {
1083 mStaticProxy.clear();
1084 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1085 } else {
1086 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1087 mProxy = mStaticProxy;
1088 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001089 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1090 uint16_t(mVolume[LEFT] * 0x1000));
1091 mProxy->setSendLevel(mSendLevel);
1092 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001093 mProxy->setEpoch(epoch);
1094 mProxy->setMinimum(mNotificationFramesAct);
1095
1096 mDeathNotifier = new DeathNotifier(this);
1097 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001098
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001099 return NO_ERROR;
1100}
1101
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001102status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1103{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001104 if (audioBuffer == NULL) {
1105 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001106 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001107 if (mTransfer != TRANSFER_OBTAIN) {
1108 audioBuffer->frameCount = 0;
1109 audioBuffer->size = 0;
1110 audioBuffer->raw = NULL;
1111 return INVALID_OPERATION;
1112 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001113
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001114 const struct timespec *requested;
1115 if (waitCount == -1) {
1116 requested = &ClientProxy::kForever;
1117 } else if (waitCount == 0) {
1118 requested = &ClientProxy::kNonBlocking;
1119 } else if (waitCount > 0) {
1120 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1121 struct timespec timeout;
1122 timeout.tv_sec = ms / 1000;
1123 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1124 requested = &timeout;
1125 } else {
1126 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1127 requested = NULL;
1128 }
1129 return obtainBuffer(audioBuffer, requested);
1130}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001131
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001132status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1133 struct timespec *elapsed, size_t *nonContig)
1134{
1135 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1136 uint32_t oldSequence = 0;
1137 uint32_t newSequence;
1138
1139 Proxy::Buffer buffer;
1140 status_t status = NO_ERROR;
1141
1142 static const int32_t kMaxTries = 5;
1143 int32_t tryCounter = kMaxTries;
1144
1145 do {
1146 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1147 // keep them from going away if another thread re-creates the track during obtainBuffer()
1148 sp<AudioTrackClientProxy> proxy;
1149 sp<IMemory> iMem;
1150
1151 { // start of lock scope
1152 AutoMutex lock(mLock);
1153
1154 newSequence = mSequence;
1155 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1156 if (status == DEAD_OBJECT) {
1157 // re-create track, unless someone else has already done so
1158 if (newSequence == oldSequence) {
1159 status = restoreTrack_l("obtainBuffer");
1160 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001161 buffer.mFrameCount = 0;
1162 buffer.mRaw = NULL;
1163 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001164 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001165 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001166 }
1167 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001168 oldSequence = newSequence;
1169
1170 // Keep the extra references
1171 proxy = mProxy;
1172 iMem = mCblkMemory;
1173
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001174 if (mState == STATE_STOPPING) {
1175 status = -EINTR;
1176 buffer.mFrameCount = 0;
1177 buffer.mRaw = NULL;
1178 buffer.mNonContig = 0;
1179 break;
1180 }
1181
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001182 // Non-blocking if track is stopped or paused
1183 if (mState != STATE_ACTIVE) {
1184 requested = &ClientProxy::kNonBlocking;
1185 }
1186
1187 } // end of lock scope
1188
1189 buffer.mFrameCount = audioBuffer->frameCount;
1190 // FIXME starts the requested timeout and elapsed over from scratch
1191 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1192
1193 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1194
1195 audioBuffer->frameCount = buffer.mFrameCount;
1196 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1197 audioBuffer->raw = buffer.mRaw;
1198 if (nonContig != NULL) {
1199 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001200 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001201 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001202}
1203
1204void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1205{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001206 if (mTransfer == TRANSFER_SHARED) {
1207 return;
1208 }
1209
1210 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1211 if (stepCount == 0) {
1212 return;
1213 }
1214
1215 Proxy::Buffer buffer;
1216 buffer.mFrameCount = stepCount;
1217 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001218
Eric Laurent1703cdf2011-03-07 14:52:59 -08001219 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001220 mInUnderrun = false;
1221 mProxy->releaseBuffer(&buffer);
1222
1223 // restart track if it was disabled by audioflinger due to previous underrun
1224 if (mState == STATE_ACTIVE) {
1225 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001226 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001227 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1228 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001229 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001230 mAudioTrack->start();
1231 }
1232 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001233}
1234
1235// -------------------------------------------------------------------------
1236
1237ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1238{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001239 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001240 return INVALID_OPERATION;
1241 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001242
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001243 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001244 // Sanity-check: user is most-likely passing an error code, and it would
1245 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001246 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001247 return BAD_VALUE;
1248 }
1249
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001250 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 Buffer audioBuffer;
1252
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001253 while (userSize >= mFrameSize) {
1254 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001255
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001256 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001257 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001258 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001260 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261 return ssize_t(err);
1262 }
1263
1264 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001265 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 toWrite = audioBuffer.size >> 1;
1268 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001269 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001271 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001273 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 userSize -= toWrite;
1275 written += toWrite;
1276
1277 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001278 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279
1280 return written;
1281}
1282
1283// -------------------------------------------------------------------------
1284
John Grossman4ff14ba2012-02-08 16:37:41 -08001285TimedAudioTrack::TimedAudioTrack() {
1286 mIsTimed = true;
1287}
1288
1289status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1290{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001291 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001292 status_t result = UNKNOWN_ERROR;
1293
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001294#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001295 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1296 // while we are accessing the cblk
1297 sp<IAudioTrack> audioTrack = mAudioTrack;
1298 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001299#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001300
John Grossman4ff14ba2012-02-08 16:37:41 -08001301 // If the track is not invalid already, try to allocate a buffer. alloc
1302 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001303 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001304 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001305 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001306 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1307 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001308 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001309 }
1310 }
1311
1312 // If the track is invalid at this point, attempt to restore it. and try the
1313 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001314 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001315 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001316
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001317 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001318 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001319 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001320 }
1321
1322 return result;
1323}
1324
1325status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1326 int64_t pts)
1327{
Eric Laurentdf839842012-05-31 14:27:14 -07001328 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1329 {
1330 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001331 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001332 // restart track if it was disabled by audioflinger due to previous underrun
1333 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001334 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1335 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001336 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001337 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001338 mAudioTrack->start();
1339 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001340 }
Eric Laurentdf839842012-05-31 14:27:14 -07001341 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001342}
1343
1344status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1345 TargetTimeline target)
1346{
1347 return mAudioTrack->setMediaTimeTransform(xform, target);
1348}
1349
1350// -------------------------------------------------------------------------
1351
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001352nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001353{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001354 // Currently the AudioTrack thread is not created if there are no callbacks.
1355 // Would it ever make sense to run the thread, even without callbacks?
1356 // If so, then replace this by checks at each use for mCbf != NULL.
1357 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1358
Eric Laurent1703cdf2011-03-07 14:52:59 -08001359 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001360 if (mAwaitBoost) {
1361 mAwaitBoost = false;
1362 mLock.unlock();
1363 static const int32_t kMaxTries = 5;
1364 int32_t tryCounter = kMaxTries;
1365 uint32_t pollUs = 10000;
1366 do {
1367 int policy = sched_getscheduler(0);
1368 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1369 break;
1370 }
1371 usleep(pollUs);
1372 pollUs <<= 1;
1373 } while (tryCounter-- > 0);
1374 if (tryCounter < 0) {
1375 ALOGE("did not receive expected priority boost on time");
1376 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001377 // Run again immediately
1378 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001379 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001380
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001381 // Can only reference mCblk while locked
1382 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001383 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001384
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001385 // Check for track invalidation
1386 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001387 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1388 // AudioSystem cache. We should not exit here but after calling the callback so
1389 // that the upper layers can recreate the track
1390 if (!isOffloaded() || (mSequence == mObservedSequence)) {
1391 status_t status = restoreTrack_l("processAudioBuffer");
1392 mLock.unlock();
1393 // Run again immediately, but with a new IAudioTrack
1394 return 0;
1395 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001396 }
1397
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001398 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001399 bool active = mState == STATE_ACTIVE;
1400
1401 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1402 bool newUnderrun = false;
1403 if (flags & CBLK_UNDERRUN) {
1404#if 0
1405 // Currently in shared buffer mode, when the server reaches the end of buffer,
1406 // the track stays active in continuous underrun state. It's up to the application
1407 // to pause or stop the track, or set the position to a new offset within buffer.
1408 // This was some experimental code to auto-pause on underrun. Keeping it here
1409 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1410 if (mTransfer == TRANSFER_SHARED) {
1411 mState = STATE_PAUSED;
1412 active = false;
1413 }
1414#endif
1415 if (!mInUnderrun) {
1416 mInUnderrun = true;
1417 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001418 }
1419 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001420
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001421 // Get current position of server
1422 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001423
1424 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001425 bool markerReached = false;
1426 size_t markerPosition = mMarkerPosition;
1427 // FIXME fails for wraparound, need 64 bits
1428 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1429 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001430 }
1431
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001432 // Determine number of new position callback(s) that will be needed, while locked
1433 size_t newPosCount = 0;
1434 size_t newPosition = mNewPosition;
1435 size_t updatePeriod = mUpdatePeriod;
1436 // FIXME fails for wraparound, need 64 bits
1437 if (updatePeriod > 0 && position >= newPosition) {
1438 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1439 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001440 }
1441
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001442 // Cache other fields that will be needed soon
1443 uint32_t loopPeriod = mLoopPeriod;
1444 uint32_t sampleRate = mSampleRate;
1445 size_t notificationFrames = mNotificationFramesAct;
1446 if (mRefreshRemaining) {
1447 mRefreshRemaining = false;
1448 mRemainingFrames = notificationFrames;
1449 mRetryOnPartialBuffer = false;
1450 }
1451 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001452 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001453
1454 // These fields don't need to be cached, because they are assigned only by set():
1455 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1456 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1457
1458 mLock.unlock();
1459
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001460 if (waitStreamEnd) {
1461 AutoMutex lock(mLock);
1462
1463 sp<AudioTrackClientProxy> proxy = mProxy;
1464 sp<IMemory> iMem = mCblkMemory;
1465
1466 struct timespec timeout;
1467 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1468 timeout.tv_nsec = 0;
1469
1470 mLock.unlock();
1471 status_t status = mProxy->waitStreamEndDone(&timeout);
1472 mLock.lock();
1473 switch (status) {
1474 case NO_ERROR:
1475 case DEAD_OBJECT:
1476 case TIMED_OUT:
1477 mLock.unlock();
1478 mCbf(EVENT_STREAM_END, mUserData, NULL);
1479 mLock.lock();
1480 if (mState == STATE_STOPPING) {
1481 mState = STATE_STOPPED;
1482 if (status != DEAD_OBJECT) {
1483 return NS_INACTIVE;
1484 }
1485 }
1486 return 0;
1487 default:
1488 return 0;
1489 }
1490 }
1491
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001492 // perform callbacks while unlocked
1493 if (newUnderrun) {
1494 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1495 }
1496 // FIXME we will miss loops if loop cycle was signaled several times since last call
1497 // to processAudioBuffer()
1498 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1499 mCbf(EVENT_LOOP_END, mUserData, NULL);
1500 }
1501 if (flags & CBLK_BUFFER_END) {
1502 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1503 }
1504 if (markerReached) {
1505 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1506 }
1507 while (newPosCount > 0) {
1508 size_t temp = newPosition;
1509 mCbf(EVENT_NEW_POS, mUserData, &temp);
1510 newPosition += updatePeriod;
1511 newPosCount--;
1512 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001513
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001514 if (mObservedSequence != sequence) {
1515 mObservedSequence = sequence;
1516 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001517 // for offloaded tracks, just wait for the upper layers to recreate the track
1518 if (isOffloaded()) {
1519 return NS_INACTIVE;
1520 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001521 }
1522
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001523 // if inactive, then don't run me again until re-started
1524 if (!active) {
1525 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001526 }
1527
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001528 // Compute the estimated time until the next timed event (position, markers, loops)
1529 // FIXME only for non-compressed audio
1530 uint32_t minFrames = ~0;
1531 if (!markerReached && position < markerPosition) {
1532 minFrames = markerPosition - position;
1533 }
1534 if (loopPeriod > 0 && loopPeriod < minFrames) {
1535 minFrames = loopPeriod;
1536 }
1537 if (updatePeriod > 0 && updatePeriod < minFrames) {
1538 minFrames = updatePeriod;
1539 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001540
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001541 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1542 static const uint32_t kPoll = 0;
1543 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1544 minFrames = kPoll * notificationFrames;
1545 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001546
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001547 // Convert frame units to time units
1548 nsecs_t ns = NS_WHENEVER;
1549 if (minFrames != (uint32_t) ~0) {
1550 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1551 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1552 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1553 }
1554
1555 // If not supplying data by EVENT_MORE_DATA, then we're done
1556 if (mTransfer != TRANSFER_CALLBACK) {
1557 return ns;
1558 }
1559
1560 struct timespec timeout;
1561 const struct timespec *requested = &ClientProxy::kForever;
1562 if (ns != NS_WHENEVER) {
1563 timeout.tv_sec = ns / 1000000000LL;
1564 timeout.tv_nsec = ns % 1000000000LL;
1565 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1566 requested = &timeout;
1567 }
1568
1569 while (mRemainingFrames > 0) {
1570
1571 Buffer audioBuffer;
1572 audioBuffer.frameCount = mRemainingFrames;
1573 size_t nonContig;
1574 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1575 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1576 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1577 requested = &ClientProxy::kNonBlocking;
1578 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001579 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1580 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001581 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001582 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1583 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001584 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001585 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001586 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1587 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001588 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001589
Eric Laurent42a6f422013-08-29 14:35:05 -07001590 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001591 mRetryOnPartialBuffer = false;
1592 if (avail < mRemainingFrames) {
1593 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1594 if (ns < 0 || myns < ns) {
1595 ns = myns;
1596 }
1597 return ns;
1598 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001599 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001600
1601 // Divide buffer size by 2 to take into account the expansion
1602 // due to 8 to 16 bit conversion: the callback must fill only half
1603 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001604 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605 audioBuffer.size >>= 1;
1606 }
1607
1608 size_t reqSize = audioBuffer.size;
1609 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001610 size_t writtenSize = audioBuffer.size;
1611 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001612
1613 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001614 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1615 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1616 reqSize, (int) writtenSize);
1617 return NS_NEVER;
1618 }
1619
1620 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001621 // The callback is done filling buffers
1622 // Keep this thread going to handle timed events and
1623 // still try to get more data in intervals of WAIT_PERIOD_MS
1624 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001625 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001626 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001627
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001628 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001629 // 8 to 16 bit conversion, note that source and destination are the same address
1630 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001631 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001632 }
1633
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001634 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1635 audioBuffer.frameCount = releasedFrames;
1636 mRemainingFrames -= releasedFrames;
1637 if (misalignment >= releasedFrames) {
1638 misalignment -= releasedFrames;
1639 } else {
1640 misalignment = 0;
1641 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642
1643 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001644
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001645 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1646 // if callback doesn't like to accept the full chunk
1647 if (writtenSize < reqSize) {
1648 continue;
1649 }
1650
1651 // There could be enough non-contiguous frames available to satisfy the remaining request
1652 if (mRemainingFrames <= nonContig) {
1653 continue;
1654 }
1655
1656#if 0
1657 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1658 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1659 // that total to a sum == notificationFrames.
1660 if (0 < misalignment && misalignment <= mRemainingFrames) {
1661 mRemainingFrames = misalignment;
1662 return (mRemainingFrames * 1100000000LL) / sampleRate;
1663 }
1664#endif
1665
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001666 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001667 mRemainingFrames = notificationFrames;
1668 mRetryOnPartialBuffer = true;
1669
1670 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1671 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001672}
1673
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001674status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001675{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001676 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
1677 isOffloaded() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001678 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001679 status_t result;
1680
Glenn Kastena47f3162012-11-07 10:13:08 -08001681 // refresh the audio configuration cache in this process to make sure we get new
1682 // output parameters in getOutput_l() and createTrack_l()
1683 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001684
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001685 if (isOffloaded()) {
1686 return DEAD_OBJECT;
1687 }
1688
1689 // force new output query from audio policy manager;
1690 mOutput = 0;
1691 audio_io_handle_t output = getOutput_l();
1692
Glenn Kastena47f3162012-11-07 10:13:08 -08001693 // if the new IAudioTrack is created, createTrack_l() will modify the
1694 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1695 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001696
1697 // take the frames that will be lost by track recreation into account in saved position
1698 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001699 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001700 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001701 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001702 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001703 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001704 mFlags,
1705 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001706 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001707 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001708
Glenn Kastena47f3162012-11-07 10:13:08 -08001709 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001710 // continue playback from last known position, but
1711 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1712 if (mStaticProxy != NULL) {
1713 mLoopPeriod = 0;
1714 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1715 }
1716 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1717 // track destruction have been played? This is critical for SoundPool implementation
1718 // This must be broken, and needs to be tested/debugged.
1719#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001720 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001721 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001722 // Make sure that a client relying on callback events indicating underrun or
1723 // the actual amount of audio frames played (e.g SoundPool) receives them.
1724 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001725 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001726 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001727 }
1728 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001729#endif
1730 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001731 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001732 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001733 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001734 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001735 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1736 // As getOutput was called above and resulted in an output stream to be opened,
1737 // we need to release it.
1738 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001739 ALOGW("restoreTrack_l() failed status %d", result);
1740 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001741 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001742
1743 return result;
1744}
1745
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001746status_t AudioTrack::setParameters(const String8& keyValuePairs)
1747{
1748 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001749 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001750}
1751
Glenn Kastence703742013-07-19 16:33:58 -07001752status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1753{
Glenn Kasten53cec222013-08-29 09:01:02 -07001754 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001755 // FIXME not implemented for fast tracks; should use proxy and SSQ
1756 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1757 return INVALID_OPERATION;
1758 }
1759 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1760 return INVALID_OPERATION;
1761 }
1762 status_t status = mAudioTrack->getTimestamp(timestamp);
1763 if (status == NO_ERROR) {
1764 timestamp.mPosition += mProxy->getEpoch();
1765 }
1766 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001767}
1768
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001769String8 AudioTrack::getParameters(const String8& keys)
1770{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001771 if (mOutput) {
1772 return AudioSystem::getParameters(mOutput, keys);
1773 } else {
1774 return String8::empty();
1775 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001776}
1777
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001778status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001779{
1780
1781 const size_t SIZE = 256;
1782 char buffer[SIZE];
1783 String8 result;
1784
1785 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001786 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1787 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001788 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001789 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001790 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001791 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001792 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001793 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001794 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001795 result.append(buffer);
1796 ::write(fd, result.string(), result.size());
1797 return NO_ERROR;
1798}
1799
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001800uint32_t AudioTrack::getUnderrunFrames() const
1801{
1802 AutoMutex lock(mLock);
1803 return mProxy->getUnderrunFrames();
1804}
1805
1806// =========================================================================
1807
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001808void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001809{
1810 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1811 if (audioTrack != 0) {
1812 AutoMutex lock(audioTrack->mLock);
1813 audioTrack->mProxy->binderDied();
1814 }
1815}
1816
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001817// =========================================================================
1818
1819AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001820 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1821 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001822{
1823}
1824
1825AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001826{
1827}
1828
1829bool AudioTrack::AudioTrackThread::threadLoop()
1830{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001831 {
1832 AutoMutex _l(mMyLock);
1833 if (mPaused) {
1834 mMyCond.wait(mMyLock);
1835 // caller will check for exitPending()
1836 return true;
1837 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001838 if (mIgnoreNextPausedInt) {
1839 mIgnoreNextPausedInt = false;
1840 mPausedInt = false;
1841 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001842 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001843 if (mPausedNs > 0) {
1844 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1845 } else {
1846 mMyCond.wait(mMyLock);
1847 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001848 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001849 return true;
1850 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001851 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001852 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001853 switch (ns) {
1854 case 0:
1855 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001856 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001857 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001858 return true;
1859 case NS_NEVER:
1860 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001861 case NS_WHENEVER:
1862 // FIXME increase poll interval, or make event-driven
1863 ns = 1000000000LL;
1864 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001865 default:
1866 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001867 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001868 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001869 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001870}
1871
Glenn Kasten3acbd052012-02-28 10:39:56 -08001872void AudioTrack::AudioTrackThread::requestExit()
1873{
1874 // must be in this order to avoid a race condition
1875 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001876 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001877}
1878
1879void AudioTrack::AudioTrackThread::pause()
1880{
1881 AutoMutex _l(mMyLock);
1882 mPaused = true;
1883}
1884
1885void AudioTrack::AudioTrackThread::resume()
1886{
1887 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001888 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001889 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001890 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001891 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001892 mMyCond.signal();
1893 }
1894}
1895
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001896void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1897{
1898 AutoMutex _l(mMyLock);
1899 mPausedInt = true;
1900 mPausedNs = ns;
1901}
1902
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001903}; // namespace android