blob: a9d6993f42099877e8eae711640639da5220f04d [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),
Glenn Kasten9f80dd22012-12-18 15:57:32 -080088 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089{
90}
91
92AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080093 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080094 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080095 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -070096 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070098 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099 callback_t cbf,
100 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700101 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800102 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000103 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800104 const audio_offload_info_t *offloadInfo,
105 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700106 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800107 mIsTimed(false),
108 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800109 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700111 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700112 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800113 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
114 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115}
116
Andreas Huberc8139852012-01-18 10:51:55 -0800117AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800118 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800120 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700121 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800122 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700123 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 callback_t cbf,
125 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700126 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800127 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000128 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800129 const audio_offload_info_t *offloadInfo,
130 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700131 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800132 mIsTimed(false),
133 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800134 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700136 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800137 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800138 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139}
140
141AudioTrack::~AudioTrack()
142{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 if (mStatus == NO_ERROR) {
144 // Make sure that callback function exits in the case where
145 // it is looping on buffer full condition in obtainBuffer().
146 // Otherwise the callback thread will never exit.
147 stop();
148 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100149 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800150 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 mAudioTrackThread->requestExitAndWait();
152 mAudioTrackThread.clear();
153 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700154 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
155 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700157 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158 }
159}
160
161status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800162 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800164 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700165 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800166 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700167 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 callback_t cbf,
169 void* user,
170 int notificationFrames,
171 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700172 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800173 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000174 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800175 const audio_offload_info_t *offloadInfo,
176 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800178 switch (transferType) {
179 case TRANSFER_DEFAULT:
180 if (sharedBuffer != 0) {
181 transferType = TRANSFER_SHARED;
182 } else if (cbf == NULL || threadCanCallJava) {
183 transferType = TRANSFER_SYNC;
184 } else {
185 transferType = TRANSFER_CALLBACK;
186 }
187 break;
188 case TRANSFER_CALLBACK:
189 if (cbf == NULL || sharedBuffer != 0) {
190 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
191 return BAD_VALUE;
192 }
193 break;
194 case TRANSFER_OBTAIN:
195 case TRANSFER_SYNC:
196 if (sharedBuffer != 0) {
197 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
198 return BAD_VALUE;
199 }
200 break;
201 case TRANSFER_SHARED:
202 if (sharedBuffer == 0) {
203 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
204 return BAD_VALUE;
205 }
206 break;
207 default:
208 ALOGE("Invalid transfer type %d", transferType);
209 return BAD_VALUE;
210 }
211 mTransfer = transferType;
212
Glenn Kastene33054e2012-11-14 12:54:39 -0800213 // FIXME "int" here is legacy and will be replaced by size_t later
214 if (frameCountInt < 0) {
215 ALOGE("Invalid frame count %d", frameCountInt);
216 return BAD_VALUE;
217 }
218 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700220 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
221 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222
Glenn Kastene33054e2012-11-14 12:54:39 -0800223 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700224
Eric Laurent1703cdf2011-03-07 14:52:59 -0800225 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800226
Glenn Kasten53cec222013-08-29 09:01:02 -0700227 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700228 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000229 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 return INVALID_OPERATION;
231 }
232
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100233 mOutput = 0;
234
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700236 if (streamType == AUDIO_STREAM_DEFAULT) {
237 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700239
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800241 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700242 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
243 return NO_INIT;
244 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 sampleRate = afSampleRate;
246 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800247 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800250 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700251 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700253 if (channelMask == 0) {
254 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 }
256
257 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700258 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800259 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260 return BAD_VALUE;
261 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700262
Glenn Kastene0fa4672012-04-24 14:35:14 -0700263 // AudioFlinger does not currently support 8-bit data in shared memory
264 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
265 ALOGE("8-bit data in shared memory is not supported");
266 return BAD_VALUE;
267 }
268
Eric Laurentc2f1f072009-07-17 12:17:14 -0700269 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100270 // or offload was requested
271 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
272 || !audio_is_linear_pcm(format)) {
273 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
274 ? "Offload request, forcing to Direct Output"
275 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700276 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800277 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700278 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700279 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700280 // only allow deep buffering for music stream type
281 if (streamType != AUDIO_STREAM_MUSIC) {
282 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
283 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700284
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700285 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700286 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287 return BAD_VALUE;
288 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800289 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700290 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800291 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700292
Glenn Kastene3aa6592012-12-04 12:22:46 -0800293 if (audio_is_linear_pcm(format)) {
294 mFrameSize = channelCount * audio_bytes_per_sample(format);
295 mFrameSizeAF = channelCount * sizeof(int16_t);
296 } else {
297 mFrameSize = sizeof(uint8_t);
298 mFrameSizeAF = sizeof(uint8_t);
299 }
300
Dima Zavinfce7a472011-04-19 22:30:36 -0700301 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800302 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800303 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000304 flags,
305 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700306
307 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000308 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 return BAD_VALUE;
310 }
311
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800312 mVolume[LEFT] = 1.0f;
313 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800314 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700315 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800316 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700317 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800318 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700319 mSessionId = sessionId;
Marco Nelissen9cae2172013-01-14 14:12:05 -0800320 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
321 mClientUid = IPCThreadState::self()->getCallingUid();
322 } else {
323 mClientUid = uid;
324 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700325 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700326 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700327 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700328
Glenn Kastena997e7a2012-08-07 09:44:19 -0700329 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700330 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700331 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
332 }
333
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800334 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800335 status_t status = createTrack_l(streamType,
336 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800337 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800338 frameCount,
339 flags,
340 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800341 output,
342 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800343
Glenn Kastena997e7a2012-08-07 09:44:19 -0700344 if (status != NO_ERROR) {
345 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100346 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
347 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700348 mAudioTrackThread.clear();
349 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100350 //Use of direct and offloaded output streams is ref counted by audio policy manager.
351 // As getOutput was called above and resulted in an output stream to be opened,
352 // we need to release it.
353 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700354 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700355 }
356
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800359 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800361 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800363 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700365 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 mNewPosition = 0;
367 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700368 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800369 mSequence = 1;
370 mObservedSequence = mSequence;
371 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100372 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800373
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 return NO_ERROR;
375}
376
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377// -------------------------------------------------------------------------
378
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100379status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800381 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100382
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800383 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100384 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 }
386
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800387 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100390 if (previousState == STATE_PAUSED_STOPPING) {
391 mState = STATE_STOPPING;
392 } else {
393 mState = STATE_ACTIVE;
394 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
396 // reset current position as seen by client to 0
397 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700398 // force refresh of remaining frames by processAudioBuffer() as last
399 // write before stop could be partial.
400 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800401 }
402 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700403 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800404
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800406 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100407 if (previousState == STATE_STOPPING) {
408 mProxy->interrupt();
409 } else {
410 t->resume();
411 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800412 } else {
413 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
414 get_sched_policy(0, &mPreviousSchedulingGroup);
415 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
416 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800418 status_t status = NO_ERROR;
419 if (!(flags & CBLK_INVALID)) {
420 status = mAudioTrack->start();
421 if (status == DEAD_OBJECT) {
422 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800424 }
425 if (flags & CBLK_INVALID) {
426 status = restoreTrack_l("start");
427 }
428
429 if (status != NO_ERROR) {
430 ALOGE("start() status %d", status);
431 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100433 if (previousState != STATE_STOPPING) {
434 t->pause();
435 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700437 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700438 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439 }
440 }
441
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100442 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800443}
444
445void AudioTrack::stop()
446{
447 AutoMutex lock(mLock);
448 // FIXME pause then stop should not be a nop
449 if (mState != STATE_ACTIVE) {
450 return;
451 }
452
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100453 if (isOffloaded()) {
454 mState = STATE_STOPPING;
455 } else {
456 mState = STATE_STOPPED;
457 }
458
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800459 mProxy->interrupt();
460 mAudioTrack->stop();
461 // the playback head position will reset to 0, so if a marker is set, we need
462 // to activate it again
463 mMarkerReached = false;
464#if 0
465 // Force flush if a shared buffer is used otherwise audioflinger
466 // will not stop before end of buffer is reached.
467 // It may be needed to make sure that we stop playback, likely in case looping is on.
468 if (mSharedBuffer != 0) {
469 flush_l();
470 }
471#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100472
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800473 sp<AudioTrackThread> t = mAudioTrackThread;
474 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100475 if (!isOffloaded()) {
476 t->pause();
477 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800478 } else {
479 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
480 set_sched_policy(0, mPreviousSchedulingGroup);
481 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482}
483
484bool AudioTrack::stopped() const
485{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800486 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800487 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488}
489
490void AudioTrack::flush()
491{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800492 if (mSharedBuffer != 0) {
493 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800494 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800495 AutoMutex lock(mLock);
496 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
497 return;
498 }
499 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800500}
501
Eric Laurent1703cdf2011-03-07 14:52:59 -0800502void AudioTrack::flush_l()
503{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800504 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700505
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700506 // clear playback marker and periodic update counter
507 mMarkerPosition = 0;
508 mMarkerReached = false;
509 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100510 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700511
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800512 mState = STATE_FLUSHED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100513 if (isOffloaded()) {
514 mProxy->interrupt();
515 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800516 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800517 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518}
519
520void AudioTrack::pause()
521{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800522 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100523 if (mState == STATE_ACTIVE) {
524 mState = STATE_PAUSED;
525 } else if (mState == STATE_STOPPING) {
526 mState = STATE_PAUSED_STOPPING;
527 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800528 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800530 mProxy->interrupt();
531 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532}
533
Eric Laurentbe916aa2010-06-01 23:49:17 -0700534status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800536 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700537 return BAD_VALUE;
538 }
539
Eric Laurent1703cdf2011-03-07 14:52:59 -0800540 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541 mVolume[LEFT] = left;
542 mVolume[RIGHT] = right;
543
Glenn Kastene3aa6592012-12-04 12:22:46 -0800544 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700545
Eric Laurent59fe0102013-09-27 18:48:26 -0700546 if (isOffloaded()) {
547 mAudioTrack->signal();
548 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700549 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550}
551
Glenn Kastenb1c09932012-02-27 16:21:04 -0800552status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800554 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700555}
556
Eric Laurent2beeb502010-07-16 07:43:46 -0700557status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700558{
Glenn Kasten05632a52012-01-03 14:22:33 -0800559 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700560 return BAD_VALUE;
561 }
562
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800563 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700564 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800565 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700566
567 return NO_ERROR;
568}
569
Glenn Kastena5224f32012-01-04 12:41:44 -0800570void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571{
572 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800573 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700574 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575}
576
Glenn Kasten3b16c762012-11-14 08:44:39 -0800577status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100579 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800580 return INVALID_OPERATION;
581 }
582
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800583 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700585 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 }
587 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700588 if (rate == 0 || rate > afSamplingRate*2 ) {
589 return BAD_VALUE;
590 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591
Eric Laurent1703cdf2011-03-07 14:52:59 -0800592 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800593 mSampleRate = rate;
594 mProxy->setSampleRate(rate);
595
Eric Laurent57326622009-07-07 07:10:45 -0700596 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597}
598
Glenn Kastena5224f32012-01-04 12:41:44 -0800599uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600{
John Grossman4ff14ba2012-02-08 16:37:41 -0800601 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800602 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800603 }
604
Eric Laurent1703cdf2011-03-07 14:52:59 -0800605 AutoMutex lock(mLock);
Eric Laurent5e49afd2013-07-26 17:16:50 -0700606
607 // sample rate can be updated during playback by the offloaded decoder so we need to
608 // query the HAL and update if needed.
609// FIXME use Proxy return channel to update the rate from server and avoid polling here
610 if (isOffloaded()) {
611 if (mOutput != 0) {
612 uint32_t sampleRate = 0;
613 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
614 if (status == NO_ERROR) {
615 mSampleRate = sampleRate;
616 }
617 }
618 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800619 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620}
621
622status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
623{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100624 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800625 return INVALID_OPERATION;
626 }
627
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800629 ;
630 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
631 loopEnd - loopStart >= MIN_LOOP) {
632 ;
633 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 return BAD_VALUE;
635 }
636
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800637 AutoMutex lock(mLock);
638 // See setPosition() regarding setting parameters such as loop points or position while active
639 if (mState == STATE_ACTIVE) {
640 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700641 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800642 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643 return NO_ERROR;
644}
645
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800646void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
647{
648 // FIXME If setting a loop also sets position to start of loop, then
649 // this is correct. Otherwise it should be removed.
650 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
651 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
652 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
653}
654
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655status_t AudioTrack::setMarkerPosition(uint32_t marker)
656{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700657 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100658 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700659 return INVALID_OPERATION;
660 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800662 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700664 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665
666 return NO_ERROR;
667}
668
Glenn Kastena5224f32012-01-04 12:41:44 -0800669status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100671 if (isOffloaded()) {
672 return INVALID_OPERATION;
673 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700674 if (marker == NULL) {
675 return BAD_VALUE;
676 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800678 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679 *marker = mMarkerPosition;
680
681 return NO_ERROR;
682}
683
684status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
685{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700686 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100687 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700688 return INVALID_OPERATION;
689 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800691 AutoMutex lock(mLock);
692 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694 return NO_ERROR;
695}
696
Glenn Kastena5224f32012-01-04 12:41:44 -0800697status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100699 if (isOffloaded()) {
700 return INVALID_OPERATION;
701 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700702 if (updatePeriod == NULL) {
703 return BAD_VALUE;
704 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800706 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 *updatePeriod = mUpdatePeriod;
708
709 return NO_ERROR;
710}
711
712status_t AudioTrack::setPosition(uint32_t position)
713{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100714 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700715 return INVALID_OPERATION;
716 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800717 if (position > mFrameCount) {
718 return BAD_VALUE;
719 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800720
Eric Laurent1703cdf2011-03-07 14:52:59 -0800721 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800722 // Currently we require that the player is inactive before setting parameters such as position
723 // or loop points. Otherwise, there could be a race condition: the application could read the
724 // current position, compute a new position or loop parameters, and then set that position or
725 // loop parameters but it would do the "wrong" thing since the position has continued to advance
726 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
727 // to specify how it wants to handle such scenarios.
728 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700729 return INVALID_OPERATION;
730 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800731 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
732 mLoopPeriod = 0;
733 // FIXME Check whether loops and setting position are incompatible in old code.
734 // If we use setLoop for both purposes we lose the capability to set the position while looping.
735 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700736
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800737 return NO_ERROR;
738}
739
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800740status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700742 if (position == NULL) {
743 return BAD_VALUE;
744 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800745
Eric Laurent1703cdf2011-03-07 14:52:59 -0800746 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100747 if (isOffloaded()) {
748 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100750 if (mOutput != 0) {
751 uint32_t halFrames;
752 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
753 }
754 *position = dspFrames;
755 } else {
756 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
757 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
758 mProxy->getPosition();
759 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760 return NO_ERROR;
761}
762
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000763status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800764{
765 if (mSharedBuffer == 0 || mIsTimed) {
766 return INVALID_OPERATION;
767 }
768 if (position == NULL) {
769 return BAD_VALUE;
770 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800771
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800772 AutoMutex lock(mLock);
773 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800774 return NO_ERROR;
775}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800776
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800777status_t AudioTrack::reload()
778{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100779 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800780 return INVALID_OPERATION;
781 }
782
Eric Laurent1703cdf2011-03-07 14:52:59 -0800783 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800784 // See setPosition() regarding setting parameters such as loop points or position while active
785 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700786 return INVALID_OPERATION;
787 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800788 mNewPosition = mUpdatePeriod;
789 mLoopPeriod = 0;
790 // FIXME The new code cannot reload while keeping a loop specified.
791 // Need to check how the old code handled this, and whether it's a significant change.
792 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800793 return NO_ERROR;
794}
795
Eric Laurentc2f1f072009-07-17 12:17:14 -0700796audio_io_handle_t AudioTrack::getOutput()
797{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800798 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100799 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800800}
801
802// must be called with mLock held
803audio_io_handle_t AudioTrack::getOutput_l()
804{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100805 if (mOutput) {
806 return mOutput;
807 } else {
808 return AudioSystem::getOutput(mStreamType,
809 mSampleRate, mFormat, mChannelMask, mFlags);
810 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700811}
812
Eric Laurentbe916aa2010-06-01 23:49:17 -0700813status_t AudioTrack::attachAuxEffect(int effectId)
814{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800815 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700816 status_t status = mAudioTrack->attachAuxEffect(effectId);
817 if (status == NO_ERROR) {
818 mAuxEffectId = effectId;
819 }
820 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700821}
822
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823// -------------------------------------------------------------------------
824
Eric Laurent1703cdf2011-03-07 14:52:59 -0800825// must be called with mLock held
826status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800827 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800828 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800829 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800830 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700831 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800832 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800833 audio_io_handle_t output,
834 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800835{
836 status_t status;
837 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
838 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700839 ALOGE("Could not get audioflinger");
840 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800841 }
842
Glenn Kastence8828a2013-09-16 18:07:38 -0700843 // Not all of these values are needed under all conditions, but it is easier to get them all
844
Eric Laurentd1b449a2010-05-14 03:26:45 -0700845 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700846 status = AudioSystem::getLatency(output, streamType, &afLatency);
847 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800848 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700849 return NO_INIT;
850 }
851
Glenn Kastence8828a2013-09-16 18:07:38 -0700852 size_t afFrameCount;
853 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
854 if (status != NO_ERROR) {
855 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
856 return NO_INIT;
857 }
858
859 uint32_t afSampleRate;
860 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
861 if (status != NO_ERROR) {
862 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
863 return NO_INIT;
864 }
865
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700866 // Client decides whether the track is TIMED (see below), but can only express a preference
867 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700868 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700869 // either of these use cases:
870 // use case 1: shared buffer
871 (sharedBuffer != 0) ||
872 // use case 2: callback handler
873 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800874 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700875 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700876 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700877 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700878 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700879 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700880
Martin Storsjo398f2132014-01-31 13:30:15 +0200881 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && sampleRate != afSampleRate) {
882 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client due to mismatching sample rate (%d vs %d)",
883 sampleRate, afSampleRate);
884 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
885 }
886
Glenn Kastence8828a2013-09-16 18:07:38 -0700887 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800888 // n = 1 fast track with single buffering; nBuffering is ignored
889 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700890 // n = 2 normal track, no sample rate conversion
891 // n = 3 normal track, with sample rate conversion
892 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
893 // n > 3 very high latency or very small notification interval; nBuffering is ignored
894 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
895
Eric Laurentd1b449a2010-05-14 03:26:45 -0700896 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700897
Dima Zavinfce7a472011-04-19 22:30:36 -0700898 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700899
Eric Laurentd1b449a2010-05-14 03:26:45 -0700900 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700901 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700902 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700903 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700904 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700905 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100906 if (mNotificationFramesAct != frameCount) {
907 mNotificationFramesAct = frameCount;
908 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700909 } else if (sharedBuffer != 0) {
910
Glenn Kastena42ff002012-11-14 12:47:55 -0800911 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912 // 8-bit data in shared memory is not currently supported by AudioFlinger
913 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800914 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700915 // More than 2 channels does not require stronger alignment than stereo
916 alignment <<= 1;
917 }
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000918 if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800919 ALOGE("Invalid buffer alignment: address %p, channel count %u",
920 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700921 return BAD_VALUE;
922 }
923
924 // When initializing a shared buffer AudioTrack via constructors,
925 // there's no frameCount parameter.
926 // But when initializing a shared buffer AudioTrack via set(),
927 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800928 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700929
930 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
931
932 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700933
Eric Laurentd1b449a2010-05-14 03:26:45 -0700934 // Ensure that buffer depth covers at least audio hardware latency
935 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700936 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
937 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700938 if (minBufCount <= nBuffering) {
939 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800940 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700941
Glenn Kastene33054e2012-11-14 12:54:39 -0800942 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
943 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800944 ", afLatency=%d",
945 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700946
947 if (frameCount == 0) {
948 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700949 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700950 // not ALOGW because it happens all the time when playing key clicks over A2DP
951 ALOGV("Minimum buffer size corrected from %d to %d",
952 frameCount, minFrameCount);
953 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800954 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700955 // Make sure that application is notified with sufficient margin before underrun
956 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
957 mNotificationFramesAct = frameCount/nBuffering;
958 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700959
Glenn Kastene0fa4672012-04-24 14:35:14 -0700960 } else {
961 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700962 }
963
Glenn Kastena075db42012-03-06 11:22:44 -0800964 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
965 if (mIsTimed) {
966 trackFlags |= IAudioFlinger::TRACK_TIMED;
967 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800968
969 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700970 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700971 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800972 if (mAudioTrackThread != 0) {
973 tid = mAudioTrackThread->getTid();
974 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700975 }
976
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100977 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
978 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
979 }
980
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800981 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800982 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700983 // AudioFlinger only sees 16-bit PCM
984 format == AUDIO_FORMAT_PCM_8_BIT ?
985 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800986 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800987 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800988 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800989 sharedBuffer,
990 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800991 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700992 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700993 mName,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800994 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800995 &status);
996
997 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000998 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800999 return status;
1000 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001001 sp<IMemory> iMem = track->getCblk();
1002 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001003 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 return NO_INIT;
1005 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001006 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001007 if (mAudioTrack != 0) {
1008 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1009 mDeathNotifier.clear();
1010 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001011 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001012 mCblkMemory = iMem;
1013 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1014 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001015 size_t temp = cblk->frameCount_;
1016 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1017 // In current design, AudioTrack client checks and ensures frame count validity before
1018 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1019 // for fast track as it uses a special method of assigning frame count.
1020 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1021 }
1022 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001023 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001024 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001025 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001026 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001027 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001028 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001029 // Theoretically double-buffering is not required for fast tracks,
1030 // due to tighter scheduling. But in practice, to accommodate kernels with
1031 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1032 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1033 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001034 }
1035 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001036 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001037 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001038 // once denied, do not request again if IAudioTrack is re-created
1039 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1040 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001041 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001042 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1043 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001044 }
1045 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001046 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001047 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001048 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1049 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1050 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1051 } else {
1052 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1053 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1054 mFlags = flags;
1055 return NO_INIT;
1056 }
1057 }
1058
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001059 mRefreshRemaining = true;
1060
1061 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1062 // is the value of pointer() for the shared buffer, otherwise buffers points
1063 // immediately after the control block. This address is for the mapping within client
1064 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1065 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001066 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001067 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001068 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001069 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001070 }
1071
Eric Laurent2beeb502010-07-16 07:43:46 -07001072 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001073 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001074 mLatency = afLatency + (1000*frameCount) / sampleRate;
1075 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001076 // If IAudioTrack is re-created, don't let the requested frameCount
1077 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001078 if (frameCount > mReqFrameCount) {
1079 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001080 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001081
1082 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001083 if (sharedBuffer == 0) {
1084 mStaticProxy.clear();
1085 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1086 } else {
1087 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1088 mProxy = mStaticProxy;
1089 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001090 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1091 uint16_t(mVolume[LEFT] * 0x1000));
1092 mProxy->setSendLevel(mSendLevel);
1093 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001094 mProxy->setEpoch(epoch);
1095 mProxy->setMinimum(mNotificationFramesAct);
1096
1097 mDeathNotifier = new DeathNotifier(this);
1098 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001099
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001100 return NO_ERROR;
1101}
1102
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001103status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1104{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001105 if (audioBuffer == NULL) {
1106 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001107 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001108 if (mTransfer != TRANSFER_OBTAIN) {
1109 audioBuffer->frameCount = 0;
1110 audioBuffer->size = 0;
1111 audioBuffer->raw = NULL;
1112 return INVALID_OPERATION;
1113 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001114
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001115 const struct timespec *requested;
1116 if (waitCount == -1) {
1117 requested = &ClientProxy::kForever;
1118 } else if (waitCount == 0) {
1119 requested = &ClientProxy::kNonBlocking;
1120 } else if (waitCount > 0) {
1121 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1122 struct timespec timeout;
1123 timeout.tv_sec = ms / 1000;
1124 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1125 requested = &timeout;
1126 } else {
1127 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1128 requested = NULL;
1129 }
1130 return obtainBuffer(audioBuffer, requested);
1131}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001132
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001133status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1134 struct timespec *elapsed, size_t *nonContig)
1135{
1136 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1137 uint32_t oldSequence = 0;
1138 uint32_t newSequence;
1139
1140 Proxy::Buffer buffer;
1141 status_t status = NO_ERROR;
1142
1143 static const int32_t kMaxTries = 5;
1144 int32_t tryCounter = kMaxTries;
1145
1146 do {
1147 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1148 // keep them from going away if another thread re-creates the track during obtainBuffer()
1149 sp<AudioTrackClientProxy> proxy;
1150 sp<IMemory> iMem;
1151
1152 { // start of lock scope
1153 AutoMutex lock(mLock);
1154
1155 newSequence = mSequence;
1156 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1157 if (status == DEAD_OBJECT) {
1158 // re-create track, unless someone else has already done so
1159 if (newSequence == oldSequence) {
1160 status = restoreTrack_l("obtainBuffer");
1161 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001162 buffer.mFrameCount = 0;
1163 buffer.mRaw = NULL;
1164 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001165 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001166 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 }
1168 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001169 oldSequence = newSequence;
1170
1171 // Keep the extra references
1172 proxy = mProxy;
1173 iMem = mCblkMemory;
1174
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001175 if (mState == STATE_STOPPING) {
1176 status = -EINTR;
1177 buffer.mFrameCount = 0;
1178 buffer.mRaw = NULL;
1179 buffer.mNonContig = 0;
1180 break;
1181 }
1182
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001183 // Non-blocking if track is stopped or paused
1184 if (mState != STATE_ACTIVE) {
1185 requested = &ClientProxy::kNonBlocking;
1186 }
1187
1188 } // end of lock scope
1189
1190 buffer.mFrameCount = audioBuffer->frameCount;
1191 // FIXME starts the requested timeout and elapsed over from scratch
1192 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1193
1194 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1195
1196 audioBuffer->frameCount = buffer.mFrameCount;
1197 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1198 audioBuffer->raw = buffer.mRaw;
1199 if (nonContig != NULL) {
1200 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001202 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203}
1204
1205void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1206{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001207 if (mTransfer == TRANSFER_SHARED) {
1208 return;
1209 }
1210
1211 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1212 if (stepCount == 0) {
1213 return;
1214 }
1215
1216 Proxy::Buffer buffer;
1217 buffer.mFrameCount = stepCount;
1218 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001219
Eric Laurent1703cdf2011-03-07 14:52:59 -08001220 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001221 mInUnderrun = false;
1222 mProxy->releaseBuffer(&buffer);
1223
1224 // restart track if it was disabled by audioflinger due to previous underrun
1225 if (mState == STATE_ACTIVE) {
1226 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001227 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001228 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1229 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001230 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001231 mAudioTrack->start();
1232 }
1233 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001234}
1235
1236// -------------------------------------------------------------------------
1237
1238ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1239{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001240 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001241 return INVALID_OPERATION;
1242 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001243
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001244 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001245 // Sanity-check: user is most-likely passing an error code, and it would
1246 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001247 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248 return BAD_VALUE;
1249 }
1250
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001251 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252 Buffer audioBuffer;
1253
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001254 while (userSize >= mFrameSize) {
1255 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001256
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001257 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001259 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001261 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 return ssize_t(err);
1263 }
1264
1265 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001266 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001268 toWrite = audioBuffer.size >> 1;
1269 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001270 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001272 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001274 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 userSize -= toWrite;
1276 written += toWrite;
1277
1278 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001279 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280
1281 return written;
1282}
1283
1284// -------------------------------------------------------------------------
1285
John Grossman4ff14ba2012-02-08 16:37:41 -08001286TimedAudioTrack::TimedAudioTrack() {
1287 mIsTimed = true;
1288}
1289
1290status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1291{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001292 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001293 status_t result = UNKNOWN_ERROR;
1294
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001295#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001296 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1297 // while we are accessing the cblk
1298 sp<IAudioTrack> audioTrack = mAudioTrack;
1299 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001300#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001301
John Grossman4ff14ba2012-02-08 16:37:41 -08001302 // If the track is not invalid already, try to allocate a buffer. alloc
1303 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001304 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001305 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001306 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001307 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1308 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001309 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001310 }
1311 }
1312
1313 // If the track is invalid at this point, attempt to restore it. and try the
1314 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001315 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001316 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001317
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001318 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001319 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001320 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001321 }
1322
1323 return result;
1324}
1325
1326status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1327 int64_t pts)
1328{
Eric Laurentdf839842012-05-31 14:27:14 -07001329 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1330 {
1331 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001332 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001333 // restart track if it was disabled by audioflinger due to previous underrun
1334 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001335 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1336 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001337 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001338 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001339 mAudioTrack->start();
1340 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001341 }
Eric Laurentdf839842012-05-31 14:27:14 -07001342 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001343}
1344
1345status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1346 TargetTimeline target)
1347{
1348 return mAudioTrack->setMediaTimeTransform(xform, target);
1349}
1350
1351// -------------------------------------------------------------------------
1352
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001353nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001354{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001355 // Currently the AudioTrack thread is not created if there are no callbacks.
1356 // Would it ever make sense to run the thread, even without callbacks?
1357 // If so, then replace this by checks at each use for mCbf != NULL.
1358 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1359
Eric Laurent1703cdf2011-03-07 14:52:59 -08001360 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001361 if (mAwaitBoost) {
1362 mAwaitBoost = false;
1363 mLock.unlock();
1364 static const int32_t kMaxTries = 5;
1365 int32_t tryCounter = kMaxTries;
1366 uint32_t pollUs = 10000;
1367 do {
1368 int policy = sched_getscheduler(0);
1369 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1370 break;
1371 }
1372 usleep(pollUs);
1373 pollUs <<= 1;
1374 } while (tryCounter-- > 0);
1375 if (tryCounter < 0) {
1376 ALOGE("did not receive expected priority boost on time");
1377 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001378 // Run again immediately
1379 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001380 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001381
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001382 // Can only reference mCblk while locked
1383 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001384 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001385
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001386 // Check for track invalidation
1387 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001388 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1389 // AudioSystem cache. We should not exit here but after calling the callback so
1390 // that the upper layers can recreate the track
1391 if (!isOffloaded() || (mSequence == mObservedSequence)) {
1392 status_t status = restoreTrack_l("processAudioBuffer");
1393 mLock.unlock();
1394 // Run again immediately, but with a new IAudioTrack
1395 return 0;
1396 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001397 }
1398
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001399 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001400 bool active = mState == STATE_ACTIVE;
1401
1402 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1403 bool newUnderrun = false;
1404 if (flags & CBLK_UNDERRUN) {
1405#if 0
1406 // Currently in shared buffer mode, when the server reaches the end of buffer,
1407 // the track stays active in continuous underrun state. It's up to the application
1408 // to pause or stop the track, or set the position to a new offset within buffer.
1409 // This was some experimental code to auto-pause on underrun. Keeping it here
1410 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1411 if (mTransfer == TRANSFER_SHARED) {
1412 mState = STATE_PAUSED;
1413 active = false;
1414 }
1415#endif
1416 if (!mInUnderrun) {
1417 mInUnderrun = true;
1418 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419 }
1420 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001421
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001422 // Get current position of server
1423 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424
1425 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001426 bool markerReached = false;
1427 size_t markerPosition = mMarkerPosition;
1428 // FIXME fails for wraparound, need 64 bits
1429 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1430 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001431 }
1432
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001433 // Determine number of new position callback(s) that will be needed, while locked
1434 size_t newPosCount = 0;
1435 size_t newPosition = mNewPosition;
1436 size_t updatePeriod = mUpdatePeriod;
1437 // FIXME fails for wraparound, need 64 bits
1438 if (updatePeriod > 0 && position >= newPosition) {
1439 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1440 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 }
1442
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001443 // Cache other fields that will be needed soon
1444 uint32_t loopPeriod = mLoopPeriod;
1445 uint32_t sampleRate = mSampleRate;
1446 size_t notificationFrames = mNotificationFramesAct;
1447 if (mRefreshRemaining) {
1448 mRefreshRemaining = false;
1449 mRemainingFrames = notificationFrames;
1450 mRetryOnPartialBuffer = false;
1451 }
1452 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001453 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001454
1455 // These fields don't need to be cached, because they are assigned only by set():
1456 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1457 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1458
1459 mLock.unlock();
1460
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001461 if (waitStreamEnd) {
1462 AutoMutex lock(mLock);
1463
1464 sp<AudioTrackClientProxy> proxy = mProxy;
1465 sp<IMemory> iMem = mCblkMemory;
1466
1467 struct timespec timeout;
1468 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1469 timeout.tv_nsec = 0;
1470
1471 mLock.unlock();
1472 status_t status = mProxy->waitStreamEndDone(&timeout);
1473 mLock.lock();
1474 switch (status) {
1475 case NO_ERROR:
1476 case DEAD_OBJECT:
1477 case TIMED_OUT:
1478 mLock.unlock();
1479 mCbf(EVENT_STREAM_END, mUserData, NULL);
1480 mLock.lock();
1481 if (mState == STATE_STOPPING) {
1482 mState = STATE_STOPPED;
1483 if (status != DEAD_OBJECT) {
1484 return NS_INACTIVE;
1485 }
1486 }
1487 return 0;
1488 default:
1489 return 0;
1490 }
1491 }
1492
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001493 // perform callbacks while unlocked
1494 if (newUnderrun) {
1495 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1496 }
1497 // FIXME we will miss loops if loop cycle was signaled several times since last call
1498 // to processAudioBuffer()
1499 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1500 mCbf(EVENT_LOOP_END, mUserData, NULL);
1501 }
1502 if (flags & CBLK_BUFFER_END) {
1503 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1504 }
1505 if (markerReached) {
1506 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1507 }
1508 while (newPosCount > 0) {
1509 size_t temp = newPosition;
1510 mCbf(EVENT_NEW_POS, mUserData, &temp);
1511 newPosition += updatePeriod;
1512 newPosCount--;
1513 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001514
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001515 if (mObservedSequence != sequence) {
1516 mObservedSequence = sequence;
1517 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001518 // for offloaded tracks, just wait for the upper layers to recreate the track
1519 if (isOffloaded()) {
1520 return NS_INACTIVE;
1521 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001522 }
1523
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001524 // if inactive, then don't run me again until re-started
1525 if (!active) {
1526 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001527 }
1528
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001529 // Compute the estimated time until the next timed event (position, markers, loops)
1530 // FIXME only for non-compressed audio
1531 uint32_t minFrames = ~0;
1532 if (!markerReached && position < markerPosition) {
1533 minFrames = markerPosition - position;
1534 }
1535 if (loopPeriod > 0 && loopPeriod < minFrames) {
1536 minFrames = loopPeriod;
1537 }
1538 if (updatePeriod > 0 && updatePeriod < minFrames) {
1539 minFrames = updatePeriod;
1540 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001541
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001542 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1543 static const uint32_t kPoll = 0;
1544 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1545 minFrames = kPoll * notificationFrames;
1546 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001547
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001548 // Convert frame units to time units
1549 nsecs_t ns = NS_WHENEVER;
1550 if (minFrames != (uint32_t) ~0) {
1551 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1552 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1553 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1554 }
1555
1556 // If not supplying data by EVENT_MORE_DATA, then we're done
1557 if (mTransfer != TRANSFER_CALLBACK) {
1558 return ns;
1559 }
1560
1561 struct timespec timeout;
1562 const struct timespec *requested = &ClientProxy::kForever;
1563 if (ns != NS_WHENEVER) {
1564 timeout.tv_sec = ns / 1000000000LL;
1565 timeout.tv_nsec = ns % 1000000000LL;
1566 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1567 requested = &timeout;
1568 }
1569
1570 while (mRemainingFrames > 0) {
1571
1572 Buffer audioBuffer;
1573 audioBuffer.frameCount = mRemainingFrames;
1574 size_t nonContig;
1575 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1576 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1577 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1578 requested = &ClientProxy::kNonBlocking;
1579 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001580 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1581 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001582 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001583 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1584 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001585 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001586 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001587 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1588 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001589 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001590
Eric Laurent42a6f422013-08-29 14:35:05 -07001591 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001592 mRetryOnPartialBuffer = false;
1593 if (avail < mRemainingFrames) {
1594 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1595 if (ns < 0 || myns < ns) {
1596 ns = myns;
1597 }
1598 return ns;
1599 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001600 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001601
1602 // Divide buffer size by 2 to take into account the expansion
1603 // due to 8 to 16 bit conversion: the callback must fill only half
1604 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001605 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 audioBuffer.size >>= 1;
1607 }
1608
1609 size_t reqSize = audioBuffer.size;
1610 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001611 size_t writtenSize = audioBuffer.size;
1612 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001613
1614 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001615 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1616 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1617 reqSize, (int) writtenSize);
1618 return NS_NEVER;
1619 }
1620
1621 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001622 // The callback is done filling buffers
1623 // Keep this thread going to handle timed events and
1624 // still try to get more data in intervals of WAIT_PERIOD_MS
1625 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001626 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001627 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001628
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001629 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001630 // 8 to 16 bit conversion, note that source and destination are the same address
1631 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001632 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001633 }
1634
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001635 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1636 audioBuffer.frameCount = releasedFrames;
1637 mRemainingFrames -= releasedFrames;
1638 if (misalignment >= releasedFrames) {
1639 misalignment -= releasedFrames;
1640 } else {
1641 misalignment = 0;
1642 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643
1644 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001646 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1647 // if callback doesn't like to accept the full chunk
1648 if (writtenSize < reqSize) {
1649 continue;
1650 }
1651
1652 // There could be enough non-contiguous frames available to satisfy the remaining request
1653 if (mRemainingFrames <= nonContig) {
1654 continue;
1655 }
1656
1657#if 0
1658 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1659 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1660 // that total to a sum == notificationFrames.
1661 if (0 < misalignment && misalignment <= mRemainingFrames) {
1662 mRemainingFrames = misalignment;
1663 return (mRemainingFrames * 1100000000LL) / sampleRate;
1664 }
1665#endif
1666
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001667 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001668 mRemainingFrames = notificationFrames;
1669 mRetryOnPartialBuffer = true;
1670
1671 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1672 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001673}
1674
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001675status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001676{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001677 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
1678 isOffloaded() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001679 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001680 status_t result;
1681
Glenn Kastena47f3162012-11-07 10:13:08 -08001682 // refresh the audio configuration cache in this process to make sure we get new
1683 // output parameters in getOutput_l() and createTrack_l()
1684 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001685
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001686 if (isOffloaded()) {
1687 return DEAD_OBJECT;
1688 }
1689
1690 // force new output query from audio policy manager;
1691 mOutput = 0;
1692 audio_io_handle_t output = getOutput_l();
1693
Glenn Kastena47f3162012-11-07 10:13:08 -08001694 // if the new IAudioTrack is created, createTrack_l() will modify the
1695 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1696 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001697
1698 // take the frames that will be lost by track recreation into account in saved position
1699 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001700 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001701 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001702 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001703 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001704 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001705 mFlags,
1706 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001707 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001708 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001709
Glenn Kastena47f3162012-11-07 10:13:08 -08001710 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001711 // continue playback from last known position, but
1712 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1713 if (mStaticProxy != NULL) {
1714 mLoopPeriod = 0;
1715 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1716 }
1717 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1718 // track destruction have been played? This is critical for SoundPool implementation
1719 // This must be broken, and needs to be tested/debugged.
1720#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001721 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001722 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001723 // Make sure that a client relying on callback events indicating underrun or
1724 // the actual amount of audio frames played (e.g SoundPool) receives them.
1725 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001726 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001727 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001728 }
1729 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001730#endif
1731 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001732 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001733 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001734 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001735 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001736 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1737 // As getOutput was called above and resulted in an output stream to be opened,
1738 // we need to release it.
1739 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001740 ALOGW("restoreTrack_l() failed status %d", result);
1741 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001742 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001743
1744 return result;
1745}
1746
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001747status_t AudioTrack::setParameters(const String8& keyValuePairs)
1748{
1749 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001750 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001751}
1752
Glenn Kastence703742013-07-19 16:33:58 -07001753status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1754{
Glenn Kasten53cec222013-08-29 09:01:02 -07001755 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001756 // FIXME not implemented for fast tracks; should use proxy and SSQ
1757 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1758 return INVALID_OPERATION;
1759 }
1760 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1761 return INVALID_OPERATION;
1762 }
1763 status_t status = mAudioTrack->getTimestamp(timestamp);
1764 if (status == NO_ERROR) {
1765 timestamp.mPosition += mProxy->getEpoch();
1766 }
1767 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001768}
1769
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001770String8 AudioTrack::getParameters(const String8& keys)
1771{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001772 if (mOutput) {
1773 return AudioSystem::getParameters(mOutput, keys);
1774 } else {
1775 return String8::empty();
1776 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001777}
1778
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001779status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1780{
1781
1782 const size_t SIZE = 256;
1783 char buffer[SIZE];
1784 String8 result;
1785
1786 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001787 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1788 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001789 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001790 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001791 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001792 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001793 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001794 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001795 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001796 result.append(buffer);
1797 ::write(fd, result.string(), result.size());
1798 return NO_ERROR;
1799}
1800
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001801uint32_t AudioTrack::getUnderrunFrames() const
1802{
1803 AutoMutex lock(mLock);
1804 return mProxy->getUnderrunFrames();
1805}
1806
1807// =========================================================================
1808
1809void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1810{
1811 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1812 if (audioTrack != 0) {
1813 AutoMutex lock(audioTrack->mLock);
1814 audioTrack->mProxy->binderDied();
1815 }
1816}
1817
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001818// =========================================================================
1819
1820AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001821 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1822 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001823{
1824}
1825
1826AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001827{
1828}
1829
1830bool AudioTrack::AudioTrackThread::threadLoop()
1831{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001832 {
1833 AutoMutex _l(mMyLock);
1834 if (mPaused) {
1835 mMyCond.wait(mMyLock);
1836 // caller will check for exitPending()
1837 return true;
1838 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001839 if (mIgnoreNextPausedInt) {
1840 mIgnoreNextPausedInt = false;
1841 mPausedInt = false;
1842 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001843 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001844 if (mPausedNs > 0) {
1845 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1846 } else {
1847 mMyCond.wait(mMyLock);
1848 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001849 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001850 return true;
1851 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001852 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001853 nsecs_t ns = mReceiver.processAudioBuffer(this);
1854 switch (ns) {
1855 case 0:
1856 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001857 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001858 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001859 return true;
1860 case NS_NEVER:
1861 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001862 case NS_WHENEVER:
1863 // FIXME increase poll interval, or make event-driven
1864 ns = 1000000000LL;
1865 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001866 default:
1867 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001868 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001869 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001870 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001871}
1872
Glenn Kasten3acbd052012-02-28 10:39:56 -08001873void AudioTrack::AudioTrackThread::requestExit()
1874{
1875 // must be in this order to avoid a race condition
1876 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001877 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001878}
1879
1880void AudioTrack::AudioTrackThread::pause()
1881{
1882 AutoMutex _l(mMyLock);
1883 mPaused = true;
1884}
1885
1886void AudioTrack::AudioTrackThread::resume()
1887{
1888 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001889 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001890 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001891 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001892 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001893 mMyCond.signal();
1894 }
1895}
1896
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001897void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1898{
1899 AutoMutex _l(mMyLock);
1900 mPausedInt = true;
1901 mPausedNs = ns;
1902}
1903
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001904}; // namespace android