blob: e0e1a7354cbffa5cea6a30e8c69e76d4094ac1d5 [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
47 // default to 0 in case of error
48 *frameCount = 0;
49
Glenn Kastene0fa4672012-04-24 14:35:14 -070050 // FIXME merge with similar code in createTrack_l(), except we're missing
51 // some information here that is available in createTrack_l():
52 // audio_io_handle_t output
53 // audio_format_t format
54 // audio_channel_mask_t channelMask
55 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080056 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080057 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
58 return NO_INIT;
59 }
Glenn Kastene33054e2012-11-14 12:54:39 -080060 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080061 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
62 return NO_INIT;
63 }
64 uint32_t afLatency;
65 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
66 return NO_INIT;
67 }
68
69 // Ensure that buffer depth covers at least audio hardware latency
70 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080071 if (minBufCount < 2) {
72 minBufCount = 2;
73 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080074
75 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070076 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080077 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
78 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080079 return NO_ERROR;
80}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080081
82// ---------------------------------------------------------------------------
83
84AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070085 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080086 mIsTimed(false),
87 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George2048c222014-01-08 13:59:53 -080088 mPreviousSchedulingGroup(SP_DEFAULT),
89 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090{
91}
92
93AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080094 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080096 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -070097 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070099 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100 callback_t cbf,
101 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700102 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800103 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000104 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800105 const audio_offload_info_t *offloadInfo,
106 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700107 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800108 mIsTimed(false),
109 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George2048c222014-01-08 13:59:53 -0800110 mPreviousSchedulingGroup(SP_DEFAULT),
111 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800112{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700113 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700114 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800115 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
116 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117}
118
Andreas Huberc8139852012-01-18 10:51:55 -0800119AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800120 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800122 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700123 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700125 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800126 callback_t cbf,
127 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700128 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800129 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000130 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800131 const audio_offload_info_t *offloadInfo,
132 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700133 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800134 mIsTimed(false),
135 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George2048c222014-01-08 13:59:53 -0800136 mPreviousSchedulingGroup(SP_DEFAULT),
137 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700139 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800140 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800141 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142}
143
144AudioTrack::~AudioTrack()
145{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 if (mStatus == NO_ERROR) {
147 // Make sure that callback function exits in the case where
148 // it is looping on buffer full condition in obtainBuffer().
149 // Otherwise the callback thread will never exit.
150 stop();
151 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100152 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800153 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 mAudioTrackThread->requestExitAndWait();
155 mAudioTrackThread.clear();
156 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700157 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
158 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700160 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162}
163
164status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800165 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800167 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700168 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800169 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700170 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 callback_t cbf,
172 void* user,
173 int notificationFrames,
174 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700175 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800176 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000177 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800178 const audio_offload_info_t *offloadInfo,
179 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800181 switch (transferType) {
182 case TRANSFER_DEFAULT:
183 if (sharedBuffer != 0) {
184 transferType = TRANSFER_SHARED;
185 } else if (cbf == NULL || threadCanCallJava) {
186 transferType = TRANSFER_SYNC;
187 } else {
188 transferType = TRANSFER_CALLBACK;
189 }
190 break;
191 case TRANSFER_CALLBACK:
192 if (cbf == NULL || sharedBuffer != 0) {
193 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
194 return BAD_VALUE;
195 }
196 break;
197 case TRANSFER_OBTAIN:
198 case TRANSFER_SYNC:
199 if (sharedBuffer != 0) {
200 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
201 return BAD_VALUE;
202 }
203 break;
204 case TRANSFER_SHARED:
205 if (sharedBuffer == 0) {
206 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
207 return BAD_VALUE;
208 }
209 break;
210 default:
211 ALOGE("Invalid transfer type %d", transferType);
212 return BAD_VALUE;
213 }
214 mTransfer = transferType;
215
Glenn Kastene33054e2012-11-14 12:54:39 -0800216 // FIXME "int" here is legacy and will be replaced by size_t later
217 if (frameCountInt < 0) {
218 ALOGE("Invalid frame count %d", frameCountInt);
219 return BAD_VALUE;
220 }
221 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700223 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
224 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225
Glenn Kastene33054e2012-11-14 12:54:39 -0800226 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700227
Eric Laurent1703cdf2011-03-07 14:52:59 -0800228 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800229
Glenn Kasten53cec222013-08-29 09:01:02 -0700230 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700231 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000232 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233 return INVALID_OPERATION;
234 }
235
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100236 mOutput = 0;
237
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 if (streamType == AUDIO_STREAM_DEFAULT) {
240 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800244 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700245 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
246 return NO_INIT;
247 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 sampleRate = afSampleRate;
249 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800250 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700251
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800253 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700254 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700256 if (channelMask == 0) {
257 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 }
259
260 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700261 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800262 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263 return BAD_VALUE;
264 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265
Glenn Kastene0fa4672012-04-24 14:35:14 -0700266 // AudioFlinger does not currently support 8-bit data in shared memory
267 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
268 ALOGE("8-bit data in shared memory is not supported");
269 return BAD_VALUE;
270 }
271
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100273 // or offload was requested
274 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
275 || !audio_is_linear_pcm(format)) {
276 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
277 ? "Offload request, forcing to Direct Output"
278 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700279 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800280 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700281 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700282 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700283 // only allow deep buffering for music stream type
284 if (streamType != AUDIO_STREAM_MUSIC) {
285 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
286 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700288 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700289 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290 return BAD_VALUE;
291 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800292 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700293 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800294 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700295
Glenn Kastene3aa6592012-12-04 12:22:46 -0800296 if (audio_is_linear_pcm(format)) {
297 mFrameSize = channelCount * audio_bytes_per_sample(format);
298 mFrameSizeAF = channelCount * sizeof(int16_t);
299 } else {
300 mFrameSize = sizeof(uint8_t);
301 mFrameSizeAF = sizeof(uint8_t);
302 }
303
Dima Zavinfce7a472011-04-19 22:30:36 -0700304 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800305 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800306 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000307 flags,
308 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309
310 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000311 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 return BAD_VALUE;
313 }
314
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800315 mVolume[LEFT] = 1.0f;
316 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800317 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700318 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800319 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700320 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800321 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700322 mSessionId = sessionId;
Marco Nelissen9cae2172013-01-14 14:12:05 -0800323 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
324 mClientUid = IPCThreadState::self()->getCallingUid();
325 } else {
326 mClientUid = uid;
327 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700328 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700329 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700330 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700331
Glenn Kastena997e7a2012-08-07 09:44:19 -0700332 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700333 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700334 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
335 }
336
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800337 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800338 status_t status = createTrack_l(streamType,
339 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800340 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800341 frameCount,
342 flags,
343 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800344 output,
345 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800346
Glenn Kastena997e7a2012-08-07 09:44:19 -0700347 if (status != NO_ERROR) {
348 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100349 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
350 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700351 mAudioTrackThread.clear();
352 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100353 //Use of direct and offloaded output streams is ref counted by audio policy manager.
354 // As getOutput was called above and resulted in an output stream to be opened,
355 // we need to release it.
356 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700357 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700358 }
359
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800362 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800364 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800366 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700368 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800369 mNewPosition = 0;
370 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700371 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800372 mSequence = 1;
373 mObservedSequence = mSequence;
374 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100375 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800376
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377 return NO_ERROR;
378}
379
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380// -------------------------------------------------------------------------
381
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100382status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800384 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100385
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800386 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100387 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 }
389
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800390 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800392 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100393 if (previousState == STATE_PAUSED_STOPPING) {
394 mState = STATE_STOPPING;
395 } else {
396 mState = STATE_ACTIVE;
397 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800398 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
399 // reset current position as seen by client to 0
400 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700401 // force refresh of remaining frames by processAudioBuffer() as last
402 // write before stop could be partial.
403 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800404 }
405 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700406 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800409 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100410 if (previousState == STATE_STOPPING) {
411 mProxy->interrupt();
412 } else {
413 t->resume();
414 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800415 } else {
416 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
417 get_sched_policy(0, &mPreviousSchedulingGroup);
418 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
419 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800420
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800421 status_t status = NO_ERROR;
422 if (!(flags & CBLK_INVALID)) {
423 status = mAudioTrack->start();
424 if (status == DEAD_OBJECT) {
425 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800427 }
428 if (flags & CBLK_INVALID) {
429 status = restoreTrack_l("start");
430 }
431
432 if (status != NO_ERROR) {
433 ALOGE("start() status %d", status);
434 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100436 if (previousState != STATE_STOPPING) {
437 t->pause();
438 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700440 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700441 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 }
443 }
444
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100445 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800446}
447
448void AudioTrack::stop()
449{
450 AutoMutex lock(mLock);
451 // FIXME pause then stop should not be a nop
452 if (mState != STATE_ACTIVE) {
453 return;
454 }
455
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100456 if (isOffloaded()) {
457 mState = STATE_STOPPING;
458 } else {
459 mState = STATE_STOPPED;
460 }
461
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800462 mProxy->interrupt();
463 mAudioTrack->stop();
464 // the playback head position will reset to 0, so if a marker is set, we need
465 // to activate it again
466 mMarkerReached = false;
467#if 0
468 // Force flush if a shared buffer is used otherwise audioflinger
469 // will not stop before end of buffer is reached.
470 // It may be needed to make sure that we stop playback, likely in case looping is on.
471 if (mSharedBuffer != 0) {
472 flush_l();
473 }
474#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100475
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800476 sp<AudioTrackThread> t = mAudioTrackThread;
477 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100478 if (!isOffloaded()) {
479 t->pause();
480 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800481 } else {
482 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
483 set_sched_policy(0, mPreviousSchedulingGroup);
484 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485}
486
487bool AudioTrack::stopped() const
488{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800489 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800490 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491}
492
493void AudioTrack::flush()
494{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800495 if (mSharedBuffer != 0) {
496 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800497 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800498 AutoMutex lock(mLock);
499 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
500 return;
501 }
502 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800503}
504
Eric Laurent1703cdf2011-03-07 14:52:59 -0800505void AudioTrack::flush_l()
506{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800507 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700508
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700509 // clear playback marker and periodic update counter
510 mMarkerPosition = 0;
511 mMarkerReached = false;
512 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100513 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700514
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800515 mState = STATE_FLUSHED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100516 if (isOffloaded()) {
517 mProxy->interrupt();
518 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800520 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521}
522
523void AudioTrack::pause()
524{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800525 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100526 if (mState == STATE_ACTIVE) {
527 mState = STATE_PAUSED;
528 } else if (mState == STATE_STOPPING) {
529 mState = STATE_PAUSED_STOPPING;
530 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800531 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800533 mProxy->interrupt();
534 mAudioTrack->pause();
Haynes Mathew George2048c222014-01-08 13:59:53 -0800535
536 if (isOffloaded()) {
537 if (mOutput != 0) {
538 uint32_t halFrames;
539 // OffloadThread sends HAL pause in its threadLoop.. time saved
540 // here can be slightly off
541 AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition);
542 ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition);
543 }
544 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545}
546
Eric Laurentbe916aa2010-06-01 23:49:17 -0700547status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800549 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700550 return BAD_VALUE;
551 }
552
Eric Laurent1703cdf2011-03-07 14:52:59 -0800553 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 mVolume[LEFT] = left;
555 mVolume[RIGHT] = right;
556
Glenn Kastene3aa6592012-12-04 12:22:46 -0800557 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700558
Eric Laurent59fe0102013-09-27 18:48:26 -0700559 if (isOffloaded()) {
560 mAudioTrack->signal();
561 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700562 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563}
564
Glenn Kastenb1c09932012-02-27 16:21:04 -0800565status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800567 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700568}
569
Eric Laurent2beeb502010-07-16 07:43:46 -0700570status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571{
Glenn Kasten05632a52012-01-03 14:22:33 -0800572 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700573 return BAD_VALUE;
574 }
575
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800576 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700577 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800578 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700579
580 return NO_ERROR;
581}
582
Glenn Kastena5224f32012-01-04 12:41:44 -0800583void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700584{
585 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800586 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588}
589
Glenn Kasten3b16c762012-11-14 08:44:39 -0800590status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100592 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800593 return INVALID_OPERATION;
594 }
595
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800596 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700598 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 }
600 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700601 if (rate == 0 || rate > afSamplingRate*2 ) {
602 return BAD_VALUE;
603 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604
Eric Laurent1703cdf2011-03-07 14:52:59 -0800605 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800606 mSampleRate = rate;
607 mProxy->setSampleRate(rate);
608
Eric Laurent57326622009-07-07 07:10:45 -0700609 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610}
611
Glenn Kastena5224f32012-01-04 12:41:44 -0800612uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613{
John Grossman4ff14ba2012-02-08 16:37:41 -0800614 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800615 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800616 }
617
Eric Laurent1703cdf2011-03-07 14:52:59 -0800618 AutoMutex lock(mLock);
Eric Laurentd0115d82013-07-26 17:16:50 -0700619
620 // sample rate can be updated during playback by the offloaded decoder so we need to
621 // query the HAL and update if needed.
622// FIXME use Proxy return channel to update the rate from server and avoid polling here
623 if (isOffloaded()) {
624 if (mOutput != 0) {
625 uint32_t sampleRate = 0;
626 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
627 if (status == NO_ERROR) {
628 mSampleRate = sampleRate;
629 }
630 }
631 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800632 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633}
634
635status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
636{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100637 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800638 return INVALID_OPERATION;
639 }
640
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800642 ;
643 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
644 loopEnd - loopStart >= MIN_LOOP) {
645 ;
646 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647 return BAD_VALUE;
648 }
649
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800650 AutoMutex lock(mLock);
651 // See setPosition() regarding setting parameters such as loop points or position while active
652 if (mState == STATE_ACTIVE) {
653 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700654 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800655 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 return NO_ERROR;
657}
658
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800659void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
660{
661 // FIXME If setting a loop also sets position to start of loop, then
662 // this is correct. Otherwise it should be removed.
663 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
664 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
665 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
666}
667
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668status_t AudioTrack::setMarkerPosition(uint32_t marker)
669{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700670 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100671 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700672 return INVALID_OPERATION;
673 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800675 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700677 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678
679 return NO_ERROR;
680}
681
Glenn Kastena5224f32012-01-04 12:41:44 -0800682status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100684 if (isOffloaded()) {
685 return INVALID_OPERATION;
686 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700687 if (marker == NULL) {
688 return BAD_VALUE;
689 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800691 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692 *marker = mMarkerPosition;
693
694 return NO_ERROR;
695}
696
697status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
698{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700699 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100700 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700701 return INVALID_OPERATION;
702 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800704 AutoMutex lock(mLock);
705 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 return NO_ERROR;
708}
709
Glenn Kastena5224f32012-01-04 12:41:44 -0800710status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100712 if (isOffloaded()) {
713 return INVALID_OPERATION;
714 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700715 if (updatePeriod == NULL) {
716 return BAD_VALUE;
717 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800719 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 *updatePeriod = mUpdatePeriod;
721
722 return NO_ERROR;
723}
724
725status_t AudioTrack::setPosition(uint32_t position)
726{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100727 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700728 return INVALID_OPERATION;
729 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800730 if (position > mFrameCount) {
731 return BAD_VALUE;
732 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800733
Eric Laurent1703cdf2011-03-07 14:52:59 -0800734 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800735 // Currently we require that the player is inactive before setting parameters such as position
736 // or loop points. Otherwise, there could be a race condition: the application could read the
737 // current position, compute a new position or loop parameters, and then set that position or
738 // loop parameters but it would do the "wrong" thing since the position has continued to advance
739 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
740 // to specify how it wants to handle such scenarios.
741 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700742 return INVALID_OPERATION;
743 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800744 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
745 mLoopPeriod = 0;
746 // FIXME Check whether loops and setting position are incompatible in old code.
747 // If we use setLoop for both purposes we lose the capability to set the position while looping.
748 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700749
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 return NO_ERROR;
751}
752
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800753status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700755 if (position == NULL) {
756 return BAD_VALUE;
757 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800758
Eric Laurent1703cdf2011-03-07 14:52:59 -0800759 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100760 if (isOffloaded()) {
761 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762
Haynes Mathew George2048c222014-01-08 13:59:53 -0800763 if ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING)) {
764 ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition);
765 *position = mPausedPosition;
766 return NO_ERROR;
767 }
768
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100769 if (mOutput != 0) {
770 uint32_t halFrames;
771 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
772 }
773 *position = dspFrames;
774 } else {
775 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
776 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
777 mProxy->getPosition();
778 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 return NO_ERROR;
780}
781
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800782status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800783{
784 if (mSharedBuffer == 0 || mIsTimed) {
785 return INVALID_OPERATION;
786 }
787 if (position == NULL) {
788 return BAD_VALUE;
789 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800790
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800791 AutoMutex lock(mLock);
792 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800793 return NO_ERROR;
794}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800795
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800796status_t AudioTrack::reload()
797{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100798 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800799 return INVALID_OPERATION;
800 }
801
Eric Laurent1703cdf2011-03-07 14:52:59 -0800802 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800803 // See setPosition() regarding setting parameters such as loop points or position while active
804 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700805 return INVALID_OPERATION;
806 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800807 mNewPosition = mUpdatePeriod;
808 mLoopPeriod = 0;
809 // FIXME The new code cannot reload while keeping a loop specified.
810 // Need to check how the old code handled this, and whether it's a significant change.
811 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812 return NO_ERROR;
813}
814
Eric Laurentc2f1f072009-07-17 12:17:14 -0700815audio_io_handle_t AudioTrack::getOutput()
816{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800817 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100818 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800819}
820
821// must be called with mLock held
822audio_io_handle_t AudioTrack::getOutput_l()
823{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100824 if (mOutput) {
825 return mOutput;
826 } else {
827 return AudioSystem::getOutput(mStreamType,
828 mSampleRate, mFormat, mChannelMask, mFlags);
829 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700830}
831
Eric Laurentbe916aa2010-06-01 23:49:17 -0700832status_t AudioTrack::attachAuxEffect(int effectId)
833{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800834 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700835 status_t status = mAudioTrack->attachAuxEffect(effectId);
836 if (status == NO_ERROR) {
837 mAuxEffectId = effectId;
838 }
839 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700840}
841
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800842// -------------------------------------------------------------------------
843
Eric Laurent1703cdf2011-03-07 14:52:59 -0800844// must be called with mLock held
845status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800846 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800847 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800848 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800849 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700850 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800851 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800852 audio_io_handle_t output,
853 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800854{
855 status_t status;
856 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
857 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700858 ALOGE("Could not get audioflinger");
859 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800860 }
861
Glenn Kastence8828a2013-09-16 18:07:38 -0700862 // Not all of these values are needed under all conditions, but it is easier to get them all
863
Eric Laurentd1b449a2010-05-14 03:26:45 -0700864 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700865 status = AudioSystem::getLatency(output, streamType, &afLatency);
866 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800867 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700868 return NO_INIT;
869 }
870
Glenn Kastence8828a2013-09-16 18:07:38 -0700871 size_t afFrameCount;
872 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
873 if (status != NO_ERROR) {
874 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
875 return NO_INIT;
876 }
877
878 uint32_t afSampleRate;
879 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
880 if (status != NO_ERROR) {
881 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
882 return NO_INIT;
883 }
884
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885 // Client decides whether the track is TIMED (see below), but can only express a preference
886 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700887 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700888 // either of these use cases:
889 // use case 1: shared buffer
890 (sharedBuffer != 0) ||
891 // use case 2: callback handler
892 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800893 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700894 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700895 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700896 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700897 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700898 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700899
Glenn Kastence8828a2013-09-16 18:07:38 -0700900 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastend812fc02013-12-03 09:06:43 -0800901 // n = 1 fast track with single buffering; nBuffering is ignored
902 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700903 // n = 2 normal track, no sample rate conversion
904 // n = 3 normal track, with sample rate conversion
905 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
906 // n > 3 very high latency or very small notification interval; nBuffering is ignored
907 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
908
Eric Laurentd1b449a2010-05-14 03:26:45 -0700909 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910
Dima Zavinfce7a472011-04-19 22:30:36 -0700911 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912
Eric Laurentd1b449a2010-05-14 03:26:45 -0700913 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700915 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700917 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700918 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100919 if (mNotificationFramesAct != frameCount) {
920 mNotificationFramesAct = frameCount;
921 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922 } else if (sharedBuffer != 0) {
923
Glenn Kastena42ff002012-11-14 12:47:55 -0800924 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700925 // 8-bit data in shared memory is not currently supported by AudioFlinger
926 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800927 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700928 // More than 2 channels does not require stronger alignment than stereo
929 alignment <<= 1;
930 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800931 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
932 ALOGE("Invalid buffer alignment: address %p, channel count %u",
933 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934 return BAD_VALUE;
935 }
936
937 // When initializing a shared buffer AudioTrack via constructors,
938 // there's no frameCount parameter.
939 // But when initializing a shared buffer AudioTrack via set(),
940 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800941 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700942
943 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
944
945 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700946
Eric Laurentd1b449a2010-05-14 03:26:45 -0700947 // Ensure that buffer depth covers at least audio hardware latency
948 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700949 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
950 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700951 if (minBufCount <= nBuffering) {
952 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800953 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700954
Glenn Kastene33054e2012-11-14 12:54:39 -0800955 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
956 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800957 ", afLatency=%d",
958 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700959
960 if (frameCount == 0) {
961 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700962 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700963 // not ALOGW because it happens all the time when playing key clicks over A2DP
964 ALOGV("Minimum buffer size corrected from %d to %d",
965 frameCount, minFrameCount);
966 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800967 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700968 // Make sure that application is notified with sufficient margin before underrun
969 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
970 mNotificationFramesAct = frameCount/nBuffering;
971 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700972
Glenn Kastene0fa4672012-04-24 14:35:14 -0700973 } else {
974 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975 }
976
Glenn Kastena075db42012-03-06 11:22:44 -0800977 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
978 if (mIsTimed) {
979 trackFlags |= IAudioFlinger::TRACK_TIMED;
980 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800981
982 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700983 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700984 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800985 if (mAudioTrackThread != 0) {
986 tid = mAudioTrackThread->getTid();
987 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700988 }
989
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100990 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
991 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
992 }
993
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800994 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800995 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700996 // AudioFlinger only sees 16-bit PCM
997 format == AUDIO_FORMAT_PCM_8_BIT ?
998 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800999 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001000 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -08001001 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001002 sharedBuffer,
1003 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001004 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001005 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001006 mName,
Marco Nelissen9cae2172013-01-14 14:12:05 -08001007 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001008 &status);
1009
1010 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001011 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001012 return status;
1013 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001014 sp<IMemory> iMem = track->getCblk();
1015 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001016 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001017 return NO_INIT;
1018 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001019 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001020 if (mAudioTrack != 0) {
1021 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1022 mDeathNotifier.clear();
1023 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001024 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001025 mCblkMemory = iMem;
1026 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1027 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001028 size_t temp = cblk->frameCount_;
1029 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1030 // In current design, AudioTrack client checks and ensures frame count validity before
1031 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1032 // for fast track as it uses a special method of assigning frame count.
1033 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1034 }
1035 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001036 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001037 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001038 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001039 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001040 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001041 if (sharedBuffer == 0) {
Glenn Kastend812fc02013-12-03 09:06:43 -08001042 // Theoretically double-buffering is not required for fast tracks,
1043 // due to tighter scheduling. But in practice, to accommodate kernels with
1044 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1045 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1046 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001047 }
1048 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001049 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001050 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001051 // once denied, do not request again if IAudioTrack is re-created
1052 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1053 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001054 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001055 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1056 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001057 }
1058 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001059 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001060 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001061 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1062 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1063 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1064 } else {
1065 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1066 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1067 mFlags = flags;
1068 return NO_INIT;
1069 }
1070 }
1071
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001072 mRefreshRemaining = true;
1073
1074 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1075 // is the value of pointer() for the shared buffer, otherwise buffers points
1076 // immediately after the control block. This address is for the mapping within client
1077 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1078 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001079 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001080 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001081 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001082 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001083 }
1084
Eric Laurent2beeb502010-07-16 07:43:46 -07001085 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001086 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001087 mLatency = afLatency + (1000*frameCount) / sampleRate;
1088 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001089 // If IAudioTrack is re-created, don't let the requested frameCount
1090 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001091 if (frameCount > mReqFrameCount) {
1092 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001093 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001094
1095 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001096 if (sharedBuffer == 0) {
1097 mStaticProxy.clear();
1098 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1099 } else {
1100 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1101 mProxy = mStaticProxy;
1102 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001103 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1104 uint16_t(mVolume[LEFT] * 0x1000));
1105 mProxy->setSendLevel(mSendLevel);
1106 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001107 mProxy->setEpoch(epoch);
1108 mProxy->setMinimum(mNotificationFramesAct);
1109
1110 mDeathNotifier = new DeathNotifier(this);
1111 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001112
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001113 return NO_ERROR;
1114}
1115
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1117{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001118 if (audioBuffer == NULL) {
1119 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001120 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001121 if (mTransfer != TRANSFER_OBTAIN) {
1122 audioBuffer->frameCount = 0;
1123 audioBuffer->size = 0;
1124 audioBuffer->raw = NULL;
1125 return INVALID_OPERATION;
1126 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001127
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001128 const struct timespec *requested;
Eric Laurent88876fb2014-01-27 18:13:39 -08001129 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001130 if (waitCount == -1) {
1131 requested = &ClientProxy::kForever;
1132 } else if (waitCount == 0) {
1133 requested = &ClientProxy::kNonBlocking;
1134 } else if (waitCount > 0) {
1135 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001136 timeout.tv_sec = ms / 1000;
1137 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1138 requested = &timeout;
1139 } else {
1140 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1141 requested = NULL;
1142 }
1143 return obtainBuffer(audioBuffer, requested);
1144}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001145
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001146status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1147 struct timespec *elapsed, size_t *nonContig)
1148{
1149 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1150 uint32_t oldSequence = 0;
1151 uint32_t newSequence;
1152
1153 Proxy::Buffer buffer;
1154 status_t status = NO_ERROR;
1155
1156 static const int32_t kMaxTries = 5;
1157 int32_t tryCounter = kMaxTries;
1158
1159 do {
1160 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1161 // keep them from going away if another thread re-creates the track during obtainBuffer()
1162 sp<AudioTrackClientProxy> proxy;
1163 sp<IMemory> iMem;
1164
1165 { // start of lock scope
1166 AutoMutex lock(mLock);
1167
1168 newSequence = mSequence;
1169 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1170 if (status == DEAD_OBJECT) {
1171 // re-create track, unless someone else has already done so
1172 if (newSequence == oldSequence) {
1173 status = restoreTrack_l("obtainBuffer");
1174 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001175 buffer.mFrameCount = 0;
1176 buffer.mRaw = NULL;
1177 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001178 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001179 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001180 }
1181 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001182 oldSequence = newSequence;
1183
1184 // Keep the extra references
1185 proxy = mProxy;
1186 iMem = mCblkMemory;
1187
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001188 if (mState == STATE_STOPPING) {
1189 status = -EINTR;
1190 buffer.mFrameCount = 0;
1191 buffer.mRaw = NULL;
1192 buffer.mNonContig = 0;
1193 break;
1194 }
1195
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001196 // Non-blocking if track is stopped or paused
1197 if (mState != STATE_ACTIVE) {
1198 requested = &ClientProxy::kNonBlocking;
1199 }
1200
1201 } // end of lock scope
1202
1203 buffer.mFrameCount = audioBuffer->frameCount;
1204 // FIXME starts the requested timeout and elapsed over from scratch
1205 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1206
1207 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1208
1209 audioBuffer->frameCount = buffer.mFrameCount;
1210 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1211 audioBuffer->raw = buffer.mRaw;
1212 if (nonContig != NULL) {
1213 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001215 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216}
1217
1218void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1219{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001220 if (mTransfer == TRANSFER_SHARED) {
1221 return;
1222 }
1223
1224 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1225 if (stepCount == 0) {
1226 return;
1227 }
1228
1229 Proxy::Buffer buffer;
1230 buffer.mFrameCount = stepCount;
1231 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001232
Eric Laurent1703cdf2011-03-07 14:52:59 -08001233 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001234 mInUnderrun = false;
1235 mProxy->releaseBuffer(&buffer);
1236
1237 // restart track if it was disabled by audioflinger due to previous underrun
1238 if (mState == STATE_ACTIVE) {
1239 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001240 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001241 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1242 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001243 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001244 mAudioTrack->start();
1245 }
1246 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001247}
1248
1249// -------------------------------------------------------------------------
1250
1251ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1252{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001253 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001254 return INVALID_OPERATION;
1255 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001257 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001258 // Sanity-check: user is most-likely passing an error code, and it would
1259 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001260 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261 return BAD_VALUE;
1262 }
1263
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001264 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 Buffer audioBuffer;
1266
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 while (userSize >= mFrameSize) {
1268 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001269
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001270 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001272 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001274 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 return ssize_t(err);
1276 }
1277
1278 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001279 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001281 toWrite = audioBuffer.size >> 1;
1282 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001283 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001285 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001287 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288 userSize -= toWrite;
1289 written += toWrite;
1290
1291 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001292 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293
1294 return written;
1295}
1296
1297// -------------------------------------------------------------------------
1298
John Grossman4ff14ba2012-02-08 16:37:41 -08001299TimedAudioTrack::TimedAudioTrack() {
1300 mIsTimed = true;
1301}
1302
1303status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1304{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001305 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001306 status_t result = UNKNOWN_ERROR;
1307
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001308#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001309 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1310 // while we are accessing the cblk
1311 sp<IAudioTrack> audioTrack = mAudioTrack;
1312 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001313#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001314
John Grossman4ff14ba2012-02-08 16:37:41 -08001315 // If the track is not invalid already, try to allocate a buffer. alloc
1316 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001317 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001318 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001319 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001320 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1321 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001322 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001323 }
1324 }
1325
1326 // If the track is invalid at this point, attempt to restore it. and try the
1327 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001328 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001329 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001330
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001331 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001332 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001333 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001334 }
1335
1336 return result;
1337}
1338
1339status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1340 int64_t pts)
1341{
Eric Laurentdf839842012-05-31 14:27:14 -07001342 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1343 {
1344 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001345 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001346 // restart track if it was disabled by audioflinger due to previous underrun
1347 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001348 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1349 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001350 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001351 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001352 mAudioTrack->start();
1353 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001354 }
Eric Laurentdf839842012-05-31 14:27:14 -07001355 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001356}
1357
1358status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1359 TargetTimeline target)
1360{
1361 return mAudioTrack->setMediaTimeTransform(xform, target);
1362}
1363
1364// -------------------------------------------------------------------------
1365
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001366nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001367{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001368 // Currently the AudioTrack thread is not created if there are no callbacks.
1369 // Would it ever make sense to run the thread, even without callbacks?
1370 // If so, then replace this by checks at each use for mCbf != NULL.
1371 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1372
Eric Laurent1703cdf2011-03-07 14:52:59 -08001373 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001374 if (mAwaitBoost) {
1375 mAwaitBoost = false;
1376 mLock.unlock();
1377 static const int32_t kMaxTries = 5;
1378 int32_t tryCounter = kMaxTries;
1379 uint32_t pollUs = 10000;
1380 do {
1381 int policy = sched_getscheduler(0);
1382 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1383 break;
1384 }
1385 usleep(pollUs);
1386 pollUs <<= 1;
1387 } while (tryCounter-- > 0);
1388 if (tryCounter < 0) {
1389 ALOGE("did not receive expected priority boost on time");
1390 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001391 // Run again immediately
1392 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001393 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001394
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001395 // Can only reference mCblk while locked
1396 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001397 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001398
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001399 // Check for track invalidation
1400 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001401 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1402 // AudioSystem cache. We should not exit here but after calling the callback so
1403 // that the upper layers can recreate the track
1404 if (!isOffloaded() || (mSequence == mObservedSequence)) {
1405 status_t status = restoreTrack_l("processAudioBuffer");
1406 mLock.unlock();
1407 // Run again immediately, but with a new IAudioTrack
1408 return 0;
1409 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001410 }
1411
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001412 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001413 bool active = mState == STATE_ACTIVE;
1414
1415 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1416 bool newUnderrun = false;
1417 if (flags & CBLK_UNDERRUN) {
1418#if 0
1419 // Currently in shared buffer mode, when the server reaches the end of buffer,
1420 // the track stays active in continuous underrun state. It's up to the application
1421 // to pause or stop the track, or set the position to a new offset within buffer.
1422 // This was some experimental code to auto-pause on underrun. Keeping it here
1423 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1424 if (mTransfer == TRANSFER_SHARED) {
1425 mState = STATE_PAUSED;
1426 active = false;
1427 }
1428#endif
1429 if (!mInUnderrun) {
1430 mInUnderrun = true;
1431 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432 }
1433 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001434
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001435 // Get current position of server
1436 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437
1438 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001439 bool markerReached = false;
1440 size_t markerPosition = mMarkerPosition;
1441 // FIXME fails for wraparound, need 64 bits
1442 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1443 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444 }
1445
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001446 // Determine number of new position callback(s) that will be needed, while locked
1447 size_t newPosCount = 0;
1448 size_t newPosition = mNewPosition;
1449 size_t updatePeriod = mUpdatePeriod;
1450 // FIXME fails for wraparound, need 64 bits
1451 if (updatePeriod > 0 && position >= newPosition) {
1452 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1453 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 }
1455
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001456 // Cache other fields that will be needed soon
1457 uint32_t loopPeriod = mLoopPeriod;
1458 uint32_t sampleRate = mSampleRate;
1459 size_t notificationFrames = mNotificationFramesAct;
1460 if (mRefreshRemaining) {
1461 mRefreshRemaining = false;
1462 mRemainingFrames = notificationFrames;
1463 mRetryOnPartialBuffer = false;
1464 }
1465 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001466 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001467
1468 // These fields don't need to be cached, because they are assigned only by set():
1469 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1470 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1471
1472 mLock.unlock();
1473
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001474 if (waitStreamEnd) {
1475 AutoMutex lock(mLock);
1476
1477 sp<AudioTrackClientProxy> proxy = mProxy;
1478 sp<IMemory> iMem = mCblkMemory;
1479
1480 struct timespec timeout;
1481 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1482 timeout.tv_nsec = 0;
1483
1484 mLock.unlock();
1485 status_t status = mProxy->waitStreamEndDone(&timeout);
1486 mLock.lock();
1487 switch (status) {
1488 case NO_ERROR:
1489 case DEAD_OBJECT:
1490 case TIMED_OUT:
1491 mLock.unlock();
1492 mCbf(EVENT_STREAM_END, mUserData, NULL);
1493 mLock.lock();
1494 if (mState == STATE_STOPPING) {
1495 mState = STATE_STOPPED;
1496 if (status != DEAD_OBJECT) {
1497 return NS_INACTIVE;
1498 }
1499 }
1500 return 0;
1501 default:
1502 return 0;
1503 }
1504 }
1505
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001506 // perform callbacks while unlocked
1507 if (newUnderrun) {
1508 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1509 }
1510 // FIXME we will miss loops if loop cycle was signaled several times since last call
1511 // to processAudioBuffer()
1512 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1513 mCbf(EVENT_LOOP_END, mUserData, NULL);
1514 }
1515 if (flags & CBLK_BUFFER_END) {
1516 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1517 }
1518 if (markerReached) {
1519 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1520 }
1521 while (newPosCount > 0) {
1522 size_t temp = newPosition;
1523 mCbf(EVENT_NEW_POS, mUserData, &temp);
1524 newPosition += updatePeriod;
1525 newPosCount--;
1526 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001527
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001528 if (mObservedSequence != sequence) {
1529 mObservedSequence = sequence;
1530 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001531 // for offloaded tracks, just wait for the upper layers to recreate the track
1532 if (isOffloaded()) {
1533 return NS_INACTIVE;
1534 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001535 }
1536
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001537 // if inactive, then don't run me again until re-started
1538 if (!active) {
1539 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001540 }
1541
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001542 // Compute the estimated time until the next timed event (position, markers, loops)
1543 // FIXME only for non-compressed audio
1544 uint32_t minFrames = ~0;
1545 if (!markerReached && position < markerPosition) {
1546 minFrames = markerPosition - position;
1547 }
1548 if (loopPeriod > 0 && loopPeriod < minFrames) {
1549 minFrames = loopPeriod;
1550 }
1551 if (updatePeriod > 0 && updatePeriod < minFrames) {
1552 minFrames = updatePeriod;
1553 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001554
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001555 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1556 static const uint32_t kPoll = 0;
1557 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1558 minFrames = kPoll * notificationFrames;
1559 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001560
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001561 // Convert frame units to time units
1562 nsecs_t ns = NS_WHENEVER;
1563 if (minFrames != (uint32_t) ~0) {
1564 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1565 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1566 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1567 }
1568
1569 // If not supplying data by EVENT_MORE_DATA, then we're done
1570 if (mTransfer != TRANSFER_CALLBACK) {
1571 return ns;
1572 }
1573
1574 struct timespec timeout;
1575 const struct timespec *requested = &ClientProxy::kForever;
1576 if (ns != NS_WHENEVER) {
1577 timeout.tv_sec = ns / 1000000000LL;
1578 timeout.tv_nsec = ns % 1000000000LL;
1579 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1580 requested = &timeout;
1581 }
1582
1583 while (mRemainingFrames > 0) {
1584
1585 Buffer audioBuffer;
1586 audioBuffer.frameCount = mRemainingFrames;
1587 size_t nonContig;
1588 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1589 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1590 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1591 requested = &ClientProxy::kNonBlocking;
1592 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001593 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1594 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001595 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001596 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1597 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001598 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001600 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1601 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001602 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001603
Eric Laurent42a6f422013-08-29 14:35:05 -07001604 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001605 mRetryOnPartialBuffer = false;
1606 if (avail < mRemainingFrames) {
1607 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1608 if (ns < 0 || myns < ns) {
1609 ns = myns;
1610 }
1611 return ns;
1612 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001613 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001614
1615 // Divide buffer size by 2 to take into account the expansion
1616 // due to 8 to 16 bit conversion: the callback must fill only half
1617 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001618 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001619 audioBuffer.size >>= 1;
1620 }
1621
1622 size_t reqSize = audioBuffer.size;
1623 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001624 size_t writtenSize = audioBuffer.size;
1625 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001626
1627 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001628 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1629 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1630 reqSize, (int) writtenSize);
1631 return NS_NEVER;
1632 }
1633
1634 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001635 // The callback is done filling buffers
1636 // Keep this thread going to handle timed events and
1637 // still try to get more data in intervals of WAIT_PERIOD_MS
1638 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001639 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001640 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001641
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001642 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001643 // 8 to 16 bit conversion, note that source and destination are the same address
1644 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001645 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001646 }
1647
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001648 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1649 audioBuffer.frameCount = releasedFrames;
1650 mRemainingFrames -= releasedFrames;
1651 if (misalignment >= releasedFrames) {
1652 misalignment -= releasedFrames;
1653 } else {
1654 misalignment = 0;
1655 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001656
1657 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001658
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001659 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1660 // if callback doesn't like to accept the full chunk
1661 if (writtenSize < reqSize) {
1662 continue;
1663 }
1664
1665 // There could be enough non-contiguous frames available to satisfy the remaining request
1666 if (mRemainingFrames <= nonContig) {
1667 continue;
1668 }
1669
1670#if 0
1671 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1672 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1673 // that total to a sum == notificationFrames.
1674 if (0 < misalignment && misalignment <= mRemainingFrames) {
1675 mRemainingFrames = misalignment;
1676 return (mRemainingFrames * 1100000000LL) / sampleRate;
1677 }
1678#endif
1679
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001680 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001681 mRemainingFrames = notificationFrames;
1682 mRetryOnPartialBuffer = true;
1683
1684 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1685 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001686}
1687
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001688status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001689{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001690 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
1691 isOffloaded() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001692 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001693 status_t result;
1694
Glenn Kastena47f3162012-11-07 10:13:08 -08001695 // refresh the audio configuration cache in this process to make sure we get new
1696 // output parameters in getOutput_l() and createTrack_l()
1697 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001698
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001699 if (isOffloaded()) {
1700 return DEAD_OBJECT;
1701 }
1702
1703 // force new output query from audio policy manager;
1704 mOutput = 0;
1705 audio_io_handle_t output = getOutput_l();
1706
Glenn Kastena47f3162012-11-07 10:13:08 -08001707 // if the new IAudioTrack is created, createTrack_l() will modify the
1708 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1709 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001710
1711 // take the frames that will be lost by track recreation into account in saved position
1712 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001713 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001714 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001715 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001716 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001717 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001718 mFlags,
1719 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001720 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001721 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001722
Glenn Kastena47f3162012-11-07 10:13:08 -08001723 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001724 // continue playback from last known position, but
1725 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1726 if (mStaticProxy != NULL) {
1727 mLoopPeriod = 0;
1728 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1729 }
1730 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1731 // track destruction have been played? This is critical for SoundPool implementation
1732 // This must be broken, and needs to be tested/debugged.
1733#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001734 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001735 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001736 // Make sure that a client relying on callback events indicating underrun or
1737 // the actual amount of audio frames played (e.g SoundPool) receives them.
1738 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001739 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001740 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001741 }
1742 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001743#endif
1744 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001745 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001746 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001747 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001748 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001749 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1750 // As getOutput was called above and resulted in an output stream to be opened,
1751 // we need to release it.
1752 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001753 ALOGW("restoreTrack_l() failed status %d", result);
1754 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001755 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001756
1757 return result;
1758}
1759
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001760status_t AudioTrack::setParameters(const String8& keyValuePairs)
1761{
1762 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001763 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001764}
1765
Glenn Kastence703742013-07-19 16:33:58 -07001766status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1767{
Glenn Kasten53cec222013-08-29 09:01:02 -07001768 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001769 // FIXME not implemented for fast tracks; should use proxy and SSQ
1770 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1771 return INVALID_OPERATION;
1772 }
1773 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1774 return INVALID_OPERATION;
1775 }
1776 status_t status = mAudioTrack->getTimestamp(timestamp);
1777 if (status == NO_ERROR) {
1778 timestamp.mPosition += mProxy->getEpoch();
1779 }
1780 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001781}
1782
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001783String8 AudioTrack::getParameters(const String8& keys)
1784{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001785 if (mOutput) {
1786 return AudioSystem::getParameters(mOutput, keys);
1787 } else {
1788 return String8::empty();
1789 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001790}
1791
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001792status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1793{
1794
1795 const size_t SIZE = 256;
1796 char buffer[SIZE];
1797 String8 result;
1798
1799 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001800 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1801 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001802 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001803 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001804 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001805 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001806 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001807 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001808 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809 result.append(buffer);
1810 ::write(fd, result.string(), result.size());
1811 return NO_ERROR;
1812}
1813
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001814uint32_t AudioTrack::getUnderrunFrames() const
1815{
1816 AutoMutex lock(mLock);
1817 return mProxy->getUnderrunFrames();
1818}
1819
1820// =========================================================================
1821
1822void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1823{
1824 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1825 if (audioTrack != 0) {
1826 AutoMutex lock(audioTrack->mLock);
1827 audioTrack->mProxy->binderDied();
1828 }
1829}
1830
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001831// =========================================================================
1832
1833AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001834 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1835 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001836{
1837}
1838
1839AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001840{
1841}
1842
1843bool AudioTrack::AudioTrackThread::threadLoop()
1844{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001845 {
1846 AutoMutex _l(mMyLock);
1847 if (mPaused) {
1848 mMyCond.wait(mMyLock);
1849 // caller will check for exitPending()
1850 return true;
1851 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001852 if (mIgnoreNextPausedInt) {
1853 mIgnoreNextPausedInt = false;
1854 mPausedInt = false;
1855 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001856 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001857 if (mPausedNs > 0) {
1858 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1859 } else {
1860 mMyCond.wait(mMyLock);
1861 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001862 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001863 return true;
1864 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001865 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001866 nsecs_t ns = mReceiver.processAudioBuffer(this);
1867 switch (ns) {
1868 case 0:
1869 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001870 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001871 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001872 return true;
1873 case NS_NEVER:
1874 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001875 case NS_WHENEVER:
1876 // FIXME increase poll interval, or make event-driven
1877 ns = 1000000000LL;
1878 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001879 default:
1880 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001881 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001882 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001883 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001884}
1885
Glenn Kasten3acbd052012-02-28 10:39:56 -08001886void AudioTrack::AudioTrackThread::requestExit()
1887{
1888 // must be in this order to avoid a race condition
1889 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001890 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001891}
1892
1893void AudioTrack::AudioTrackThread::pause()
1894{
1895 AutoMutex _l(mMyLock);
1896 mPaused = true;
1897}
1898
1899void AudioTrack::AudioTrackThread::resume()
1900{
1901 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001902 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001903 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001904 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001905 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001906 mMyCond.signal();
1907 }
1908}
1909
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001910void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1911{
1912 AutoMutex _l(mMyLock);
1913 mPausedInt = true;
1914 mPausedNs = ns;
1915}
1916
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001917}; // namespace android