blob: 8954d9fe6f5a3380ec1a35b6fa3d5d465a00af70 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080023#include <audio_utils/primitives.h>
24#include <binder/IPCThreadState.h>
25#include <media/AudioTrack.h>
26#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010030#define WAIT_PERIOD_MS 10
31#define WAIT_STREAM_END_TIMEOUT_SEC 120
32
Glenn Kasten511754b2012-01-11 09:52:19 -080033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080035// ---------------------------------------------------------------------------
36
37// static
38status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080039 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080040 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080041 uint32_t sampleRate)
42{
Glenn Kastend65d73c2012-06-22 17:21:07 -070043 if (frameCount == NULL) {
44 return BAD_VALUE;
45 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070046
Glenn Kastene0fa4672012-04-24 14:35:14 -070047 // FIXME merge with similar code in createTrack_l(), except we're missing
48 // some information here that is available in createTrack_l():
49 // audio_io_handle_t output
50 // audio_format_t format
51 // audio_channel_mask_t channelMask
52 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080053 uint32_t afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080054 status_t status;
55 status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
56 if (status != NO_ERROR) {
57 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080058 }
Glenn Kastene33054e2012-11-14 12:54:39 -080059 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080060 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
61 if (status != NO_ERROR) {
62 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080063 }
64 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080065 status = AudioSystem::getOutputLatency(&afLatency, streamType);
66 if (status != NO_ERROR) {
67 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 }
69
70 // Ensure that buffer depth covers at least audio hardware latency
71 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080072 if (minBufCount < 2) {
73 minBufCount = 2;
74 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080075
76 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070077 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080078 // The formula above should always produce a non-zero value, but return an error
79 // in the unlikely event that it does not, as that's part of the API contract.
80 if (*frameCount == 0) {
81 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d",
82 streamType, sampleRate);
83 return BAD_VALUE;
84 }
Glenn Kasten3acbd052012-02-28 10:39:56 -080085 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
86 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080087 return NO_ERROR;
88}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089
90// ---------------------------------------------------------------------------
91
92AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070093 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080094 mIsTimed(false),
95 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -080096 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097{
98}
99
100AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800101 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800103 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700104 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700106 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 callback_t cbf,
108 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700109 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800110 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000111 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800112 const audio_offload_info_t *offloadInfo,
113 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700114 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800115 mIsTimed(false),
116 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800117 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700119 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700120 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800121 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
122 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123}
124
Andreas Huberc8139852012-01-18 10:51:55 -0800125AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800126 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800128 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700129 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700131 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 callback_t cbf,
133 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700134 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800135 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000136 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800137 const audio_offload_info_t *offloadInfo,
138 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700139 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mIsTimed(false),
141 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800142 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700144 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800145 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800146 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147}
148
149AudioTrack::~AudioTrack()
150{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 if (mStatus == NO_ERROR) {
152 // Make sure that callback function exits in the case where
153 // it is looping on buffer full condition in obtainBuffer().
154 // Otherwise the callback thread will never exit.
155 stop();
156 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100157 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800158 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 mAudioTrackThread->requestExitAndWait();
160 mAudioTrackThread.clear();
161 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700162 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
163 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700165 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 }
167}
168
169status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800170 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800172 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700173 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800174 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700175 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 callback_t cbf,
177 void* user,
178 int notificationFrames,
179 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700180 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800181 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000182 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800183 const audio_offload_info_t *offloadInfo,
184 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800186 switch (transferType) {
187 case TRANSFER_DEFAULT:
188 if (sharedBuffer != 0) {
189 transferType = TRANSFER_SHARED;
190 } else if (cbf == NULL || threadCanCallJava) {
191 transferType = TRANSFER_SYNC;
192 } else {
193 transferType = TRANSFER_CALLBACK;
194 }
195 break;
196 case TRANSFER_CALLBACK:
197 if (cbf == NULL || sharedBuffer != 0) {
198 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
199 return BAD_VALUE;
200 }
201 break;
202 case TRANSFER_OBTAIN:
203 case TRANSFER_SYNC:
204 if (sharedBuffer != 0) {
205 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
206 return BAD_VALUE;
207 }
208 break;
209 case TRANSFER_SHARED:
210 if (sharedBuffer == 0) {
211 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
212 return BAD_VALUE;
213 }
214 break;
215 default:
216 ALOGE("Invalid transfer type %d", transferType);
217 return BAD_VALUE;
218 }
219 mTransfer = transferType;
220
Glenn Kastene33054e2012-11-14 12:54:39 -0800221 // FIXME "int" here is legacy and will be replaced by size_t later
222 if (frameCountInt < 0) {
223 ALOGE("Invalid frame count %d", frameCountInt);
224 return BAD_VALUE;
225 }
226 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700228 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
229 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230
Glenn Kastene33054e2012-11-14 12:54:39 -0800231 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700232
Eric Laurent1703cdf2011-03-07 14:52:59 -0800233 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800234
Glenn Kasten53cec222013-08-29 09:01:02 -0700235 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700236 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000237 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238 return INVALID_OPERATION;
239 }
240
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100241 mOutput = 0;
242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700244 if (streamType == AUDIO_STREAM_DEFAULT) {
245 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800246 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700247
Glenn Kastenb1bef512014-01-13 10:25:53 -0800248 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800250 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
251 if (status != NO_ERROR) {
252 ALOGE("Could not get output sample rate for stream type %d; status %d",
253 streamType, status);
254 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700255 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800257 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700258
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800260 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700261 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263
264 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700265 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800266 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267 return BAD_VALUE;
268 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700269
Glenn Kasten8ba90322013-10-30 11:29:27 -0700270 if (!audio_is_output_channel(channelMask)) {
271 ALOGE("Invalid channel mask %#x", channelMask);
272 return BAD_VALUE;
273 }
274
Glenn Kastene0fa4672012-04-24 14:35:14 -0700275 // AudioFlinger does not currently support 8-bit data in shared memory
276 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
277 ALOGE("8-bit data in shared memory is not supported");
278 return BAD_VALUE;
279 }
280
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100282 // or offload was requested
283 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
284 || !audio_is_linear_pcm(format)) {
285 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
286 ? "Offload request, forcing to Direct Output"
287 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700288 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800289 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700290 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700292 // only allow deep buffering for music stream type
293 if (streamType != AUDIO_STREAM_MUSIC) {
294 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
295 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700296
Glenn Kastena42ff002012-11-14 12:47:55 -0800297 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700298 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800299 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700300
Glenn Kastene3aa6592012-12-04 12:22:46 -0800301 if (audio_is_linear_pcm(format)) {
302 mFrameSize = channelCount * audio_bytes_per_sample(format);
303 mFrameSizeAF = channelCount * sizeof(int16_t);
304 } else {
305 mFrameSize = sizeof(uint8_t);
306 mFrameSizeAF = sizeof(uint8_t);
307 }
308
Dima Zavinfce7a472011-04-19 22:30:36 -0700309 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800310 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800311 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000312 flags,
313 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314
315 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000316 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 return BAD_VALUE;
318 }
319
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800320 mVolume[LEFT] = 1.0f;
321 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800322 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700323 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800324 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700325 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800326 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700327 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800328 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
329 mClientUid = IPCThreadState::self()->getCallingUid();
330 } else {
331 mClientUid = uid;
332 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700333 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700334 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700335 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700336
Glenn Kastena997e7a2012-08-07 09:44:19 -0700337 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700338 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700339 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
340 }
341
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800342 // create the IAudioTrack
Glenn Kastenb1bef512014-01-13 10:25:53 -0800343 status = createTrack_l(streamType,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800344 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800345 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800346 frameCount,
347 flags,
348 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800349 output,
350 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800351
Glenn Kastena997e7a2012-08-07 09:44:19 -0700352 if (status != NO_ERROR) {
353 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100354 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
355 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700356 mAudioTrackThread.clear();
357 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100358 //Use of direct and offloaded output streams is ref counted by audio policy manager.
359 // As getOutput was called above and resulted in an output stream to be opened,
360 // we need to release it.
361 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700362 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700363 }
364
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800367 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800369 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800371 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700373 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 mNewPosition = 0;
375 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700376 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800377 mSequence = 1;
378 mObservedSequence = mSequence;
379 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100380 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800381
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 return NO_ERROR;
383}
384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385// -------------------------------------------------------------------------
386
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100387status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800389 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100390
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800391 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100392 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 }
394
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800397 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100398 if (previousState == STATE_PAUSED_STOPPING) {
399 mState = STATE_STOPPING;
400 } else {
401 mState = STATE_ACTIVE;
402 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800403 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
404 // reset current position as seen by client to 0
405 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700406 // force refresh of remaining frames by processAudioBuffer() as last
407 // write before stop could be partial.
408 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800409 }
410 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700411 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800412
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800414 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100415 if (previousState == STATE_STOPPING) {
416 mProxy->interrupt();
417 } else {
418 t->resume();
419 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800420 } else {
421 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
422 get_sched_policy(0, &mPreviousSchedulingGroup);
423 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
424 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800426 status_t status = NO_ERROR;
427 if (!(flags & CBLK_INVALID)) {
428 status = mAudioTrack->start();
429 if (status == DEAD_OBJECT) {
430 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800432 }
433 if (flags & CBLK_INVALID) {
434 status = restoreTrack_l("start");
435 }
436
437 if (status != NO_ERROR) {
438 ALOGE("start() status %d", status);
439 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100441 if (previousState != STATE_STOPPING) {
442 t->pause();
443 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700445 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700446 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 }
448 }
449
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100450 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800451}
452
453void AudioTrack::stop()
454{
455 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700456 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800457 return;
458 }
459
Glenn Kasten23a75452014-01-13 10:37:17 -0800460 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100461 mState = STATE_STOPPING;
462 } else {
463 mState = STATE_STOPPED;
464 }
465
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800466 mProxy->interrupt();
467 mAudioTrack->stop();
468 // the playback head position will reset to 0, so if a marker is set, we need
469 // to activate it again
470 mMarkerReached = false;
471#if 0
472 // Force flush if a shared buffer is used otherwise audioflinger
473 // will not stop before end of buffer is reached.
474 // It may be needed to make sure that we stop playback, likely in case looping is on.
475 if (mSharedBuffer != 0) {
476 flush_l();
477 }
478#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100479
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800480 sp<AudioTrackThread> t = mAudioTrackThread;
481 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800482 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100483 t->pause();
484 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800485 } else {
486 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
487 set_sched_policy(0, mPreviousSchedulingGroup);
488 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489}
490
491bool AudioTrack::stopped() const
492{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800493 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800494 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495}
496
497void AudioTrack::flush()
498{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800499 if (mSharedBuffer != 0) {
500 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800501 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800502 AutoMutex lock(mLock);
503 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
504 return;
505 }
506 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800507}
508
Eric Laurent1703cdf2011-03-07 14:52:59 -0800509void AudioTrack::flush_l()
510{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800511 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700512
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700513 // clear playback marker and periodic update counter
514 mMarkerPosition = 0;
515 mMarkerReached = false;
516 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100517 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800520 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100521 mProxy->interrupt();
522 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800523 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800524 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525}
526
527void AudioTrack::pause()
528{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800529 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100530 if (mState == STATE_ACTIVE) {
531 mState = STATE_PAUSED;
532 } else if (mState == STATE_STOPPING) {
533 mState = STATE_PAUSED_STOPPING;
534 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800535 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800537 mProxy->interrupt();
538 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539}
540
Eric Laurentbe916aa2010-06-01 23:49:17 -0700541status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800543 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700544 return BAD_VALUE;
545 }
546
Eric Laurent1703cdf2011-03-07 14:52:59 -0800547 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 mVolume[LEFT] = left;
549 mVolume[RIGHT] = right;
550
Glenn Kastene3aa6592012-12-04 12:22:46 -0800551 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700552
Glenn Kasten23a75452014-01-13 10:37:17 -0800553 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700554 mAudioTrack->signal();
555 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700556 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557}
558
Glenn Kastenb1c09932012-02-27 16:21:04 -0800559status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800561 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700562}
563
Eric Laurent2beeb502010-07-16 07:43:46 -0700564status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700565{
Glenn Kasten05632a52012-01-03 14:22:33 -0800566 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700567 return BAD_VALUE;
568 }
569
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800570 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800572 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700573
574 return NO_ERROR;
575}
576
Glenn Kastena5224f32012-01-04 12:41:44 -0800577void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700578{
579 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800580 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700581 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582}
583
Glenn Kasten3b16c762012-11-14 08:44:39 -0800584status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100586 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800587 return INVALID_OPERATION;
588 }
589
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800590 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700592 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593 }
594 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700595 if (rate == 0 || rate > afSamplingRate*2 ) {
596 return BAD_VALUE;
597 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598
Eric Laurent1703cdf2011-03-07 14:52:59 -0800599 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800600 mSampleRate = rate;
601 mProxy->setSampleRate(rate);
602
Eric Laurent57326622009-07-07 07:10:45 -0700603 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604}
605
Glenn Kastena5224f32012-01-04 12:41:44 -0800606uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607{
John Grossman4ff14ba2012-02-08 16:37:41 -0800608 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800609 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800610 }
611
Eric Laurent1703cdf2011-03-07 14:52:59 -0800612 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700613
614 // sample rate can be updated during playback by the offloaded decoder so we need to
615 // query the HAL and update if needed.
616// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800617 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700618 if (mOutput != 0) {
619 uint32_t sampleRate = 0;
620 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
621 if (status == NO_ERROR) {
622 mSampleRate = sampleRate;
623 }
624 }
625 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800626 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627}
628
629status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
630{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100631 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800632 return INVALID_OPERATION;
633 }
634
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800636 ;
637 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
638 loopEnd - loopStart >= MIN_LOOP) {
639 ;
640 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 return BAD_VALUE;
642 }
643
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800644 AutoMutex lock(mLock);
645 // See setPosition() regarding setting parameters such as loop points or position while active
646 if (mState == STATE_ACTIVE) {
647 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700648 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800649 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 return NO_ERROR;
651}
652
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800653void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
654{
655 // FIXME If setting a loop also sets position to start of loop, then
656 // this is correct. Otherwise it should be removed.
657 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
658 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
659 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
660}
661
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662status_t AudioTrack::setMarkerPosition(uint32_t marker)
663{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700664 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100665 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700666 return INVALID_OPERATION;
667 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800669 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700671 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672
673 return NO_ERROR;
674}
675
Glenn Kastena5224f32012-01-04 12:41:44 -0800676status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100678 if (isOffloaded()) {
679 return INVALID_OPERATION;
680 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700681 if (marker == NULL) {
682 return BAD_VALUE;
683 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800685 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686 *marker = mMarkerPosition;
687
688 return NO_ERROR;
689}
690
691status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
692{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700693 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100694 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700695 return INVALID_OPERATION;
696 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800698 AutoMutex lock(mLock);
699 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701 return NO_ERROR;
702}
703
Glenn Kastena5224f32012-01-04 12:41:44 -0800704status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100706 if (isOffloaded()) {
707 return INVALID_OPERATION;
708 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700709 if (updatePeriod == NULL) {
710 return BAD_VALUE;
711 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800713 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714 *updatePeriod = mUpdatePeriod;
715
716 return NO_ERROR;
717}
718
719status_t AudioTrack::setPosition(uint32_t position)
720{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100721 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700722 return INVALID_OPERATION;
723 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800724 if (position > mFrameCount) {
725 return BAD_VALUE;
726 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800727
Eric Laurent1703cdf2011-03-07 14:52:59 -0800728 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800729 // Currently we require that the player is inactive before setting parameters such as position
730 // or loop points. Otherwise, there could be a race condition: the application could read the
731 // current position, compute a new position or loop parameters, and then set that position or
732 // loop parameters but it would do the "wrong" thing since the position has continued to advance
733 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
734 // to specify how it wants to handle such scenarios.
735 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700736 return INVALID_OPERATION;
737 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800738 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
739 mLoopPeriod = 0;
740 // FIXME Check whether loops and setting position are incompatible in old code.
741 // If we use setLoop for both purposes we lose the capability to set the position while looping.
742 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700743
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800744 return NO_ERROR;
745}
746
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800747status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800748{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700749 if (position == NULL) {
750 return BAD_VALUE;
751 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800752
Eric Laurent1703cdf2011-03-07 14:52:59 -0800753 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800754 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100755 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100757 if (mOutput != 0) {
758 uint32_t halFrames;
759 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
760 }
761 *position = dspFrames;
762 } else {
763 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
764 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
765 mProxy->getPosition();
766 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800767 return NO_ERROR;
768}
769
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800770status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800771{
772 if (mSharedBuffer == 0 || mIsTimed) {
773 return INVALID_OPERATION;
774 }
775 if (position == NULL) {
776 return BAD_VALUE;
777 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800778
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800779 AutoMutex lock(mLock);
780 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800781 return NO_ERROR;
782}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800783
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800784status_t AudioTrack::reload()
785{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100786 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800787 return INVALID_OPERATION;
788 }
789
Eric Laurent1703cdf2011-03-07 14:52:59 -0800790 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800791 // See setPosition() regarding setting parameters such as loop points or position while active
792 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700793 return INVALID_OPERATION;
794 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800795 mNewPosition = mUpdatePeriod;
796 mLoopPeriod = 0;
797 // FIXME The new code cannot reload while keeping a loop specified.
798 // Need to check how the old code handled this, and whether it's a significant change.
799 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800800 return NO_ERROR;
801}
802
Eric Laurentc2f1f072009-07-17 12:17:14 -0700803audio_io_handle_t AudioTrack::getOutput()
804{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800805 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100806 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800807}
808
809// must be called with mLock held
810audio_io_handle_t AudioTrack::getOutput_l()
811{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100812 if (mOutput) {
813 return mOutput;
814 } else {
815 return AudioSystem::getOutput(mStreamType,
816 mSampleRate, mFormat, mChannelMask, mFlags);
817 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700818}
819
Eric Laurentbe916aa2010-06-01 23:49:17 -0700820status_t AudioTrack::attachAuxEffect(int effectId)
821{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800822 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700823 status_t status = mAudioTrack->attachAuxEffect(effectId);
824 if (status == NO_ERROR) {
825 mAuxEffectId = effectId;
826 }
827 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700828}
829
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800830// -------------------------------------------------------------------------
831
Eric Laurent1703cdf2011-03-07 14:52:59 -0800832// must be called with mLock held
833status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800834 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800835 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800836 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800837 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700838 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800839 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800840 audio_io_handle_t output,
841 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800842{
843 status_t status;
844 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
845 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700846 ALOGE("Could not get audioflinger");
847 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800848 }
849
Glenn Kastence8828a2013-09-16 18:07:38 -0700850 // Not all of these values are needed under all conditions, but it is easier to get them all
851
Eric Laurentd1b449a2010-05-14 03:26:45 -0700852 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700853 status = AudioSystem::getLatency(output, streamType, &afLatency);
854 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800855 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700856 return NO_INIT;
857 }
858
Glenn Kastence8828a2013-09-16 18:07:38 -0700859 size_t afFrameCount;
860 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
861 if (status != NO_ERROR) {
862 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
863 return NO_INIT;
864 }
865
866 uint32_t afSampleRate;
867 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
868 if (status != NO_ERROR) {
869 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
870 return NO_INIT;
871 }
872
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700873 // Client decides whether the track is TIMED (see below), but can only express a preference
874 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700875 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700876 // either of these use cases:
877 // use case 1: shared buffer
878 (sharedBuffer != 0) ||
879 // use case 2: callback handler
880 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800881 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700882 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700883 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700884 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700886 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700887
Glenn Kastence8828a2013-09-16 18:07:38 -0700888 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800889 // n = 1 fast track with single buffering; nBuffering is ignored
890 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700891 // n = 2 normal track, no sample rate conversion
892 // n = 3 normal track, with sample rate conversion
893 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
894 // n > 3 very high latency or very small notification interval; nBuffering is ignored
895 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
896
Eric Laurentd1b449a2010-05-14 03:26:45 -0700897 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700898
Dima Zavinfce7a472011-04-19 22:30:36 -0700899 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700900
Eric Laurentd1b449a2010-05-14 03:26:45 -0700901 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700902 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700903 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700904 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700905 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700906 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100907 if (mNotificationFramesAct != frameCount) {
908 mNotificationFramesAct = frameCount;
909 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910 } else if (sharedBuffer != 0) {
911
Glenn Kastena42ff002012-11-14 12:47:55 -0800912 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700913 // 8-bit data in shared memory is not currently supported by AudioFlinger
914 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800915 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916 // More than 2 channels does not require stronger alignment than stereo
917 alignment <<= 1;
918 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800919 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
920 ALOGE("Invalid buffer alignment: address %p, channel count %u",
921 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922 return BAD_VALUE;
923 }
924
925 // When initializing a shared buffer AudioTrack via constructors,
926 // there's no frameCount parameter.
927 // But when initializing a shared buffer AudioTrack via set(),
928 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800929 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700930
931 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
932
933 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934
Eric Laurentd1b449a2010-05-14 03:26:45 -0700935 // Ensure that buffer depth covers at least audio hardware latency
936 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700937 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
938 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700939 if (minBufCount <= nBuffering) {
940 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800941 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700942
Glenn Kastene33054e2012-11-14 12:54:39 -0800943 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
944 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800945 ", afLatency=%d",
946 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700947
948 if (frameCount == 0) {
949 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700950 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700951 // not ALOGW because it happens all the time when playing key clicks over A2DP
952 ALOGV("Minimum buffer size corrected from %d to %d",
953 frameCount, minFrameCount);
954 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800955 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700956 // Make sure that application is notified with sufficient margin before underrun
957 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
958 mNotificationFramesAct = frameCount/nBuffering;
959 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700960
Glenn Kastene0fa4672012-04-24 14:35:14 -0700961 } else {
962 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700963 }
964
Glenn Kastena075db42012-03-06 11:22:44 -0800965 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
966 if (mIsTimed) {
967 trackFlags |= IAudioFlinger::TRACK_TIMED;
968 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800969
970 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700971 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700972 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800973 if (mAudioTrackThread != 0) {
974 tid = mAudioTrackThread->getTid();
975 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700976 }
977
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100978 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
979 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
980 }
981
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800982 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800983 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700984 // AudioFlinger only sees 16-bit PCM
985 format == AUDIO_FORMAT_PCM_8_BIT ?
986 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800987 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800988 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800989 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800990 sharedBuffer,
991 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800992 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700993 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700994 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800995 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800996 &status);
997
998 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000999 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001000 return status;
1001 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001002 sp<IMemory> iMem = track->getCblk();
1003 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001004 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001005 return NO_INIT;
1006 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001007 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001008 if (mAudioTrack != 0) {
1009 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1010 mDeathNotifier.clear();
1011 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001012 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001013 mCblkMemory = iMem;
1014 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1015 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001016 size_t temp = cblk->frameCount_;
1017 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1018 // In current design, AudioTrack client checks and ensures frame count validity before
1019 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1020 // for fast track as it uses a special method of assigning frame count.
1021 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1022 }
1023 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001024 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001025 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001026 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001027 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001028 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001029 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001030 // Theoretically double-buffering is not required for fast tracks,
1031 // due to tighter scheduling. But in practice, to accommodate kernels with
1032 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1033 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1034 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001035 }
1036 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001037 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001038 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001039 // once denied, do not request again if IAudioTrack is re-created
1040 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1041 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001042 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001043 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1044 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001045 }
1046 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001047 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001048 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001049 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1050 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1051 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1052 } else {
1053 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1054 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1055 mFlags = flags;
1056 return NO_INIT;
1057 }
1058 }
1059
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001060 mRefreshRemaining = true;
1061
1062 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1063 // is the value of pointer() for the shared buffer, otherwise buffers points
1064 // immediately after the control block. This address is for the mapping within client
1065 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1066 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001067 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001068 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001069 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001070 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001071 }
1072
Eric Laurent2beeb502010-07-16 07:43:46 -07001073 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001074 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001075 mLatency = afLatency + (1000*frameCount) / sampleRate;
1076 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001077 // If IAudioTrack is re-created, don't let the requested frameCount
1078 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001079 if (frameCount > mReqFrameCount) {
1080 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001081 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001082
1083 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001084 if (sharedBuffer == 0) {
1085 mStaticProxy.clear();
1086 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1087 } else {
1088 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1089 mProxy = mStaticProxy;
1090 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001091 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1092 uint16_t(mVolume[LEFT] * 0x1000));
1093 mProxy->setSendLevel(mSendLevel);
1094 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001095 mProxy->setEpoch(epoch);
1096 mProxy->setMinimum(mNotificationFramesAct);
1097
1098 mDeathNotifier = new DeathNotifier(this);
1099 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001100
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001101 return NO_ERROR;
1102}
1103
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1105{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001106 if (audioBuffer == NULL) {
1107 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001108 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001109 if (mTransfer != TRANSFER_OBTAIN) {
1110 audioBuffer->frameCount = 0;
1111 audioBuffer->size = 0;
1112 audioBuffer->raw = NULL;
1113 return INVALID_OPERATION;
1114 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001115
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001116 const struct timespec *requested;
1117 if (waitCount == -1) {
1118 requested = &ClientProxy::kForever;
1119 } else if (waitCount == 0) {
1120 requested = &ClientProxy::kNonBlocking;
1121 } else if (waitCount > 0) {
1122 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1123 struct timespec timeout;
1124 timeout.tv_sec = ms / 1000;
1125 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1126 requested = &timeout;
1127 } else {
1128 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1129 requested = NULL;
1130 }
1131 return obtainBuffer(audioBuffer, requested);
1132}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001133
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001134status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1135 struct timespec *elapsed, size_t *nonContig)
1136{
1137 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1138 uint32_t oldSequence = 0;
1139 uint32_t newSequence;
1140
1141 Proxy::Buffer buffer;
1142 status_t status = NO_ERROR;
1143
1144 static const int32_t kMaxTries = 5;
1145 int32_t tryCounter = kMaxTries;
1146
1147 do {
1148 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1149 // keep them from going away if another thread re-creates the track during obtainBuffer()
1150 sp<AudioTrackClientProxy> proxy;
1151 sp<IMemory> iMem;
1152
1153 { // start of lock scope
1154 AutoMutex lock(mLock);
1155
1156 newSequence = mSequence;
1157 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1158 if (status == DEAD_OBJECT) {
1159 // re-create track, unless someone else has already done so
1160 if (newSequence == oldSequence) {
1161 status = restoreTrack_l("obtainBuffer");
1162 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001163 buffer.mFrameCount = 0;
1164 buffer.mRaw = NULL;
1165 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001166 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001168 }
1169 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001170 oldSequence = newSequence;
1171
1172 // Keep the extra references
1173 proxy = mProxy;
1174 iMem = mCblkMemory;
1175
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001176 if (mState == STATE_STOPPING) {
1177 status = -EINTR;
1178 buffer.mFrameCount = 0;
1179 buffer.mRaw = NULL;
1180 buffer.mNonContig = 0;
1181 break;
1182 }
1183
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001184 // Non-blocking if track is stopped or paused
1185 if (mState != STATE_ACTIVE) {
1186 requested = &ClientProxy::kNonBlocking;
1187 }
1188
1189 } // end of lock scope
1190
1191 buffer.mFrameCount = audioBuffer->frameCount;
1192 // FIXME starts the requested timeout and elapsed over from scratch
1193 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1194
1195 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1196
1197 audioBuffer->frameCount = buffer.mFrameCount;
1198 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1199 audioBuffer->raw = buffer.mRaw;
1200 if (nonContig != NULL) {
1201 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001202 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001203 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204}
1205
1206void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1207{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001208 if (mTransfer == TRANSFER_SHARED) {
1209 return;
1210 }
1211
1212 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1213 if (stepCount == 0) {
1214 return;
1215 }
1216
1217 Proxy::Buffer buffer;
1218 buffer.mFrameCount = stepCount;
1219 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001220
Eric Laurent1703cdf2011-03-07 14:52:59 -08001221 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001222 mInUnderrun = false;
1223 mProxy->releaseBuffer(&buffer);
1224
1225 // restart track if it was disabled by audioflinger due to previous underrun
1226 if (mState == STATE_ACTIVE) {
1227 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001228 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001229 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1230 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001231 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001232 mAudioTrack->start();
1233 }
1234 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235}
1236
1237// -------------------------------------------------------------------------
1238
1239ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1240{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001241 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001242 return INVALID_OPERATION;
1243 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001244
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001245 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001246 // Sanity-check: user is most-likely passing an error code, and it would
1247 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001248 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249 return BAD_VALUE;
1250 }
1251
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001252 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253 Buffer audioBuffer;
1254
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001255 while (userSize >= mFrameSize) {
1256 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001257
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001258 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001260 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001262 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 return ssize_t(err);
1264 }
1265
1266 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001267 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001269 toWrite = audioBuffer.size >> 1;
1270 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001271 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001273 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001275 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276 userSize -= toWrite;
1277 written += toWrite;
1278
1279 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001280 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281
1282 return written;
1283}
1284
1285// -------------------------------------------------------------------------
1286
John Grossman4ff14ba2012-02-08 16:37:41 -08001287TimedAudioTrack::TimedAudioTrack() {
1288 mIsTimed = true;
1289}
1290
1291status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1292{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001293 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001294 status_t result = UNKNOWN_ERROR;
1295
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001296#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001297 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1298 // while we are accessing the cblk
1299 sp<IAudioTrack> audioTrack = mAudioTrack;
1300 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001301#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001302
John Grossman4ff14ba2012-02-08 16:37:41 -08001303 // If the track is not invalid already, try to allocate a buffer. alloc
1304 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001305 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001306 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001307 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001308 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1309 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001310 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001311 }
1312 }
1313
1314 // If the track is invalid at this point, attempt to restore it. and try the
1315 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001316 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001317 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001318
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001319 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001320 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001321 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001322 }
1323
1324 return result;
1325}
1326
1327status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1328 int64_t pts)
1329{
Eric Laurentdf839842012-05-31 14:27:14 -07001330 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1331 {
1332 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001333 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001334 // restart track if it was disabled by audioflinger due to previous underrun
1335 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001336 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1337 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001338 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001339 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001340 mAudioTrack->start();
1341 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001342 }
Eric Laurentdf839842012-05-31 14:27:14 -07001343 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001344}
1345
1346status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1347 TargetTimeline target)
1348{
1349 return mAudioTrack->setMediaTimeTransform(xform, target);
1350}
1351
1352// -------------------------------------------------------------------------
1353
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001354nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001355{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001356 // Currently the AudioTrack thread is not created if there are no callbacks.
1357 // Would it ever make sense to run the thread, even without callbacks?
1358 // If so, then replace this by checks at each use for mCbf != NULL.
1359 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1360
Eric Laurent1703cdf2011-03-07 14:52:59 -08001361 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001362 if (mAwaitBoost) {
1363 mAwaitBoost = false;
1364 mLock.unlock();
1365 static const int32_t kMaxTries = 5;
1366 int32_t tryCounter = kMaxTries;
1367 uint32_t pollUs = 10000;
1368 do {
1369 int policy = sched_getscheduler(0);
1370 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1371 break;
1372 }
1373 usleep(pollUs);
1374 pollUs <<= 1;
1375 } while (tryCounter-- > 0);
1376 if (tryCounter < 0) {
1377 ALOGE("did not receive expected priority boost on time");
1378 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001379 // Run again immediately
1380 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001381 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001382
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001383 // Can only reference mCblk while locked
1384 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001385 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001386
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001387 // Check for track invalidation
1388 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001389 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1390 // AudioSystem cache. We should not exit here but after calling the callback so
1391 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001392 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001393 status_t status = restoreTrack_l("processAudioBuffer");
1394 mLock.unlock();
1395 // Run again immediately, but with a new IAudioTrack
1396 return 0;
1397 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001398 }
1399
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001400 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001401 bool active = mState == STATE_ACTIVE;
1402
1403 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1404 bool newUnderrun = false;
1405 if (flags & CBLK_UNDERRUN) {
1406#if 0
1407 // Currently in shared buffer mode, when the server reaches the end of buffer,
1408 // the track stays active in continuous underrun state. It's up to the application
1409 // to pause or stop the track, or set the position to a new offset within buffer.
1410 // This was some experimental code to auto-pause on underrun. Keeping it here
1411 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1412 if (mTransfer == TRANSFER_SHARED) {
1413 mState = STATE_PAUSED;
1414 active = false;
1415 }
1416#endif
1417 if (!mInUnderrun) {
1418 mInUnderrun = true;
1419 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 }
1421 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001422
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001423 // Get current position of server
1424 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425
1426 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001427 bool markerReached = false;
1428 size_t markerPosition = mMarkerPosition;
1429 // FIXME fails for wraparound, need 64 bits
1430 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1431 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432 }
1433
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001434 // Determine number of new position callback(s) that will be needed, while locked
1435 size_t newPosCount = 0;
1436 size_t newPosition = mNewPosition;
1437 size_t updatePeriod = mUpdatePeriod;
1438 // FIXME fails for wraparound, need 64 bits
1439 if (updatePeriod > 0 && position >= newPosition) {
1440 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1441 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001442 }
1443
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001444 // Cache other fields that will be needed soon
1445 uint32_t loopPeriod = mLoopPeriod;
1446 uint32_t sampleRate = mSampleRate;
1447 size_t notificationFrames = mNotificationFramesAct;
1448 if (mRefreshRemaining) {
1449 mRefreshRemaining = false;
1450 mRemainingFrames = notificationFrames;
1451 mRetryOnPartialBuffer = false;
1452 }
1453 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001454 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001455
1456 // These fields don't need to be cached, because they are assigned only by set():
1457 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1458 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1459
1460 mLock.unlock();
1461
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001462 if (waitStreamEnd) {
1463 AutoMutex lock(mLock);
1464
1465 sp<AudioTrackClientProxy> proxy = mProxy;
1466 sp<IMemory> iMem = mCblkMemory;
1467
1468 struct timespec timeout;
1469 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1470 timeout.tv_nsec = 0;
1471
1472 mLock.unlock();
1473 status_t status = mProxy->waitStreamEndDone(&timeout);
1474 mLock.lock();
1475 switch (status) {
1476 case NO_ERROR:
1477 case DEAD_OBJECT:
1478 case TIMED_OUT:
1479 mLock.unlock();
1480 mCbf(EVENT_STREAM_END, mUserData, NULL);
1481 mLock.lock();
1482 if (mState == STATE_STOPPING) {
1483 mState = STATE_STOPPED;
1484 if (status != DEAD_OBJECT) {
1485 return NS_INACTIVE;
1486 }
1487 }
1488 return 0;
1489 default:
1490 return 0;
1491 }
1492 }
1493
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001494 // perform callbacks while unlocked
1495 if (newUnderrun) {
1496 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1497 }
1498 // FIXME we will miss loops if loop cycle was signaled several times since last call
1499 // to processAudioBuffer()
1500 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1501 mCbf(EVENT_LOOP_END, mUserData, NULL);
1502 }
1503 if (flags & CBLK_BUFFER_END) {
1504 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1505 }
1506 if (markerReached) {
1507 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1508 }
1509 while (newPosCount > 0) {
1510 size_t temp = newPosition;
1511 mCbf(EVENT_NEW_POS, mUserData, &temp);
1512 newPosition += updatePeriod;
1513 newPosCount--;
1514 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001515
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001516 if (mObservedSequence != sequence) {
1517 mObservedSequence = sequence;
1518 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001519 // for offloaded tracks, just wait for the upper layers to recreate the track
1520 if (isOffloaded()) {
1521 return NS_INACTIVE;
1522 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001523 }
1524
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001525 // if inactive, then don't run me again until re-started
1526 if (!active) {
1527 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001528 }
1529
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001530 // Compute the estimated time until the next timed event (position, markers, loops)
1531 // FIXME only for non-compressed audio
1532 uint32_t minFrames = ~0;
1533 if (!markerReached && position < markerPosition) {
1534 minFrames = markerPosition - position;
1535 }
1536 if (loopPeriod > 0 && loopPeriod < minFrames) {
1537 minFrames = loopPeriod;
1538 }
1539 if (updatePeriod > 0 && updatePeriod < minFrames) {
1540 minFrames = updatePeriod;
1541 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001543 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1544 static const uint32_t kPoll = 0;
1545 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1546 minFrames = kPoll * notificationFrames;
1547 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001548
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001549 // Convert frame units to time units
1550 nsecs_t ns = NS_WHENEVER;
1551 if (minFrames != (uint32_t) ~0) {
1552 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1553 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1554 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1555 }
1556
1557 // If not supplying data by EVENT_MORE_DATA, then we're done
1558 if (mTransfer != TRANSFER_CALLBACK) {
1559 return ns;
1560 }
1561
1562 struct timespec timeout;
1563 const struct timespec *requested = &ClientProxy::kForever;
1564 if (ns != NS_WHENEVER) {
1565 timeout.tv_sec = ns / 1000000000LL;
1566 timeout.tv_nsec = ns % 1000000000LL;
1567 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1568 requested = &timeout;
1569 }
1570
1571 while (mRemainingFrames > 0) {
1572
1573 Buffer audioBuffer;
1574 audioBuffer.frameCount = mRemainingFrames;
1575 size_t nonContig;
1576 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1577 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1578 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1579 requested = &ClientProxy::kNonBlocking;
1580 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001581 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1582 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001583 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001584 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1585 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001586 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001587 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001588 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1589 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001590 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001591
Eric Laurent42a6f422013-08-29 14:35:05 -07001592 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001593 mRetryOnPartialBuffer = false;
1594 if (avail < mRemainingFrames) {
1595 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1596 if (ns < 0 || myns < ns) {
1597 ns = myns;
1598 }
1599 return ns;
1600 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001601 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001602
1603 // Divide buffer size by 2 to take into account the expansion
1604 // due to 8 to 16 bit conversion: the callback must fill only half
1605 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001606 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001607 audioBuffer.size >>= 1;
1608 }
1609
1610 size_t reqSize = audioBuffer.size;
1611 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001612 size_t writtenSize = audioBuffer.size;
1613 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001614
1615 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001616 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1617 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1618 reqSize, (int) writtenSize);
1619 return NS_NEVER;
1620 }
1621
1622 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001623 // The callback is done filling buffers
1624 // Keep this thread going to handle timed events and
1625 // still try to get more data in intervals of WAIT_PERIOD_MS
1626 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001627 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001628 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001629
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001630 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001631 // 8 to 16 bit conversion, note that source and destination are the same address
1632 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001633 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001634 }
1635
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001636 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1637 audioBuffer.frameCount = releasedFrames;
1638 mRemainingFrames -= releasedFrames;
1639 if (misalignment >= releasedFrames) {
1640 misalignment -= releasedFrames;
1641 } else {
1642 misalignment = 0;
1643 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001644
1645 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001646
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001647 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1648 // if callback doesn't like to accept the full chunk
1649 if (writtenSize < reqSize) {
1650 continue;
1651 }
1652
1653 // There could be enough non-contiguous frames available to satisfy the remaining request
1654 if (mRemainingFrames <= nonContig) {
1655 continue;
1656 }
1657
1658#if 0
1659 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1660 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1661 // that total to a sum == notificationFrames.
1662 if (0 < misalignment && misalignment <= mRemainingFrames) {
1663 mRemainingFrames = misalignment;
1664 return (mRemainingFrames * 1100000000LL) / sampleRate;
1665 }
1666#endif
1667
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001668 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001669 mRemainingFrames = notificationFrames;
1670 mRetryOnPartialBuffer = true;
1671
1672 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1673 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001674}
1675
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001676status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001677{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001678 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001679 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001680 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001681 status_t result;
1682
Glenn Kastena47f3162012-11-07 10:13:08 -08001683 // refresh the audio configuration cache in this process to make sure we get new
1684 // output parameters in getOutput_l() and createTrack_l()
1685 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001686
Glenn Kasten23a75452014-01-13 10:37:17 -08001687 if (isOffloaded_l()) {
1688 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001689 return DEAD_OBJECT;
1690 }
1691
1692 // force new output query from audio policy manager;
1693 mOutput = 0;
1694 audio_io_handle_t output = getOutput_l();
1695
Glenn Kastena47f3162012-11-07 10:13:08 -08001696 // if the new IAudioTrack is created, createTrack_l() will modify the
1697 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1698 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001699
1700 // take the frames that will be lost by track recreation into account in saved position
1701 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001702 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001703 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001704 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001705 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001706 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001707 mFlags,
1708 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001709 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001710 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001711
Glenn Kastena47f3162012-11-07 10:13:08 -08001712 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001713 // continue playback from last known position, but
1714 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1715 if (mStaticProxy != NULL) {
1716 mLoopPeriod = 0;
1717 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1718 }
1719 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1720 // track destruction have been played? This is critical for SoundPool implementation
1721 // This must be broken, and needs to be tested/debugged.
1722#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001723 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001724 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001725 // Make sure that a client relying on callback events indicating underrun or
1726 // the actual amount of audio frames played (e.g SoundPool) receives them.
1727 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001728 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001729 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001730 }
1731 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001732#endif
1733 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001734 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001735 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001736 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001737 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001738 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1739 // As getOutput was called above and resulted in an output stream to be opened,
1740 // we need to release it.
1741 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001742 ALOGW("restoreTrack_l() failed status %d", result);
1743 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001744 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001745
1746 return result;
1747}
1748
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001749status_t AudioTrack::setParameters(const String8& keyValuePairs)
1750{
1751 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001752 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001753}
1754
Glenn Kastence703742013-07-19 16:33:58 -07001755status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1756{
Glenn Kasten53cec222013-08-29 09:01:02 -07001757 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001758 // FIXME not implemented for fast tracks; should use proxy and SSQ
1759 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1760 return INVALID_OPERATION;
1761 }
1762 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1763 return INVALID_OPERATION;
1764 }
1765 status_t status = mAudioTrack->getTimestamp(timestamp);
1766 if (status == NO_ERROR) {
1767 timestamp.mPosition += mProxy->getEpoch();
1768 }
1769 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001770}
1771
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001772String8 AudioTrack::getParameters(const String8& keys)
1773{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001774 audio_io_handle_t output = getOutput();
1775 if (output != 0) {
1776 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001777 } else {
1778 return String8::empty();
1779 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001780}
1781
Glenn Kasten23a75452014-01-13 10:37:17 -08001782bool AudioTrack::isOffloaded() const
1783{
1784 AutoMutex lock(mLock);
1785 return isOffloaded_l();
1786}
1787
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001788status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001789{
1790
1791 const size_t SIZE = 256;
1792 char buffer[SIZE];
1793 String8 result;
1794
1795 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001796 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1797 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001798 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001799 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001800 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001801 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001802 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001803 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001804 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001805 result.append(buffer);
1806 ::write(fd, result.string(), result.size());
1807 return NO_ERROR;
1808}
1809
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001810uint32_t AudioTrack::getUnderrunFrames() const
1811{
1812 AutoMutex lock(mLock);
1813 return mProxy->getUnderrunFrames();
1814}
1815
1816// =========================================================================
1817
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001818void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001819{
1820 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1821 if (audioTrack != 0) {
1822 AutoMutex lock(audioTrack->mLock);
1823 audioTrack->mProxy->binderDied();
1824 }
1825}
1826
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001827// =========================================================================
1828
1829AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001830 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1831 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001832{
1833}
1834
1835AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001836{
1837}
1838
1839bool AudioTrack::AudioTrackThread::threadLoop()
1840{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001841 {
1842 AutoMutex _l(mMyLock);
1843 if (mPaused) {
1844 mMyCond.wait(mMyLock);
1845 // caller will check for exitPending()
1846 return true;
1847 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001848 if (mIgnoreNextPausedInt) {
1849 mIgnoreNextPausedInt = false;
1850 mPausedInt = false;
1851 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001852 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001853 if (mPausedNs > 0) {
1854 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1855 } else {
1856 mMyCond.wait(mMyLock);
1857 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001858 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001859 return true;
1860 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001861 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001862 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001863 switch (ns) {
1864 case 0:
1865 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001866 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001867 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001868 return true;
1869 case NS_NEVER:
1870 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001871 case NS_WHENEVER:
1872 // FIXME increase poll interval, or make event-driven
1873 ns = 1000000000LL;
1874 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001875 default:
1876 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001877 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001878 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001879 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001880}
1881
Glenn Kasten3acbd052012-02-28 10:39:56 -08001882void AudioTrack::AudioTrackThread::requestExit()
1883{
1884 // must be in this order to avoid a race condition
1885 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001886 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001887}
1888
1889void AudioTrack::AudioTrackThread::pause()
1890{
1891 AutoMutex _l(mMyLock);
1892 mPaused = true;
1893}
1894
1895void AudioTrack::AudioTrackThread::resume()
1896{
1897 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001898 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001899 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001900 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001901 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001902 mMyCond.signal();
1903 }
1904}
1905
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001906void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1907{
1908 AutoMutex _l(mMyLock);
1909 mPausedInt = true;
1910 mPausedNs = ns;
1911}
1912
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001913}; // namespace android