blob: 8aa012661f40d162020ae5ca776483379765d2cf [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 }
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800358 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100359 // 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;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800701
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 return NO_ERROR;
703}
704
Glenn Kastena5224f32012-01-04 12:41:44 -0800705status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100707 if (isOffloaded()) {
708 return INVALID_OPERATION;
709 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700710 if (updatePeriod == NULL) {
711 return BAD_VALUE;
712 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800713
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800714 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800715 *updatePeriod = mUpdatePeriod;
716
717 return NO_ERROR;
718}
719
720status_t AudioTrack::setPosition(uint32_t position)
721{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100722 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700723 return INVALID_OPERATION;
724 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800725 if (position > mFrameCount) {
726 return BAD_VALUE;
727 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800728
Eric Laurent1703cdf2011-03-07 14:52:59 -0800729 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800730 // Currently we require that the player is inactive before setting parameters such as position
731 // or loop points. Otherwise, there could be a race condition: the application could read the
732 // current position, compute a new position or loop parameters, and then set that position or
733 // loop parameters but it would do the "wrong" thing since the position has continued to advance
734 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
735 // to specify how it wants to handle such scenarios.
736 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700737 return INVALID_OPERATION;
738 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800739 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
740 mLoopPeriod = 0;
741 // FIXME Check whether loops and setting position are incompatible in old code.
742 // If we use setLoop for both purposes we lose the capability to set the position while looping.
743 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700744
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800745 return NO_ERROR;
746}
747
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800748status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700750 if (position == NULL) {
751 return BAD_VALUE;
752 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800753
Eric Laurent1703cdf2011-03-07 14:52:59 -0800754 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800755 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100756 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800757
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100758 if (mOutput != 0) {
759 uint32_t halFrames;
760 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
761 }
762 *position = dspFrames;
763 } else {
764 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
765 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
766 mProxy->getPosition();
767 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800768 return NO_ERROR;
769}
770
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800771status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800772{
773 if (mSharedBuffer == 0 || mIsTimed) {
774 return INVALID_OPERATION;
775 }
776 if (position == NULL) {
777 return BAD_VALUE;
778 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800779
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800780 AutoMutex lock(mLock);
781 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800782 return NO_ERROR;
783}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800784
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800785status_t AudioTrack::reload()
786{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100787 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800788 return INVALID_OPERATION;
789 }
790
Eric Laurent1703cdf2011-03-07 14:52:59 -0800791 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800792 // See setPosition() regarding setting parameters such as loop points or position while active
793 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700794 return INVALID_OPERATION;
795 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800796 mNewPosition = mUpdatePeriod;
797 mLoopPeriod = 0;
798 // FIXME The new code cannot reload while keeping a loop specified.
799 // Need to check how the old code handled this, and whether it's a significant change.
800 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800801 return NO_ERROR;
802}
803
Eric Laurentc2f1f072009-07-17 12:17:14 -0700804audio_io_handle_t AudioTrack::getOutput()
805{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800806 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100807 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800808}
809
810// must be called with mLock held
811audio_io_handle_t AudioTrack::getOutput_l()
812{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100813 if (mOutput) {
814 return mOutput;
815 } else {
816 return AudioSystem::getOutput(mStreamType,
817 mSampleRate, mFormat, mChannelMask, mFlags);
818 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700819}
820
Eric Laurentbe916aa2010-06-01 23:49:17 -0700821status_t AudioTrack::attachAuxEffect(int effectId)
822{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800823 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700824 status_t status = mAudioTrack->attachAuxEffect(effectId);
825 if (status == NO_ERROR) {
826 mAuxEffectId = effectId;
827 }
828 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700829}
830
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831// -------------------------------------------------------------------------
832
Eric Laurent1703cdf2011-03-07 14:52:59 -0800833// must be called with mLock held
834status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800835 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800836 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800837 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800838 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700839 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800840 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800841 audio_io_handle_t output,
842 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800843{
844 status_t status;
845 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
846 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700847 ALOGE("Could not get audioflinger");
848 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800849 }
850
Glenn Kastence8828a2013-09-16 18:07:38 -0700851 // Not all of these values are needed under all conditions, but it is easier to get them all
852
Eric Laurentd1b449a2010-05-14 03:26:45 -0700853 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700854 status = AudioSystem::getLatency(output, streamType, &afLatency);
855 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800856 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700857 return NO_INIT;
858 }
859
Glenn Kastence8828a2013-09-16 18:07:38 -0700860 size_t afFrameCount;
861 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
862 if (status != NO_ERROR) {
863 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
864 return NO_INIT;
865 }
866
867 uint32_t afSampleRate;
868 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
869 if (status != NO_ERROR) {
870 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
871 return NO_INIT;
872 }
873
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700874 // Client decides whether the track is TIMED (see below), but can only express a preference
875 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700876 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700877 // either of these use cases:
878 // use case 1: shared buffer
879 (sharedBuffer != 0) ||
880 // use case 2: callback handler
881 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800882 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700883 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700884 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700885 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700886 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700887 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700888
Glenn Kastence8828a2013-09-16 18:07:38 -0700889 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800890 // n = 1 fast track with single buffering; nBuffering is ignored
891 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700892 // n = 2 normal track, no sample rate conversion
893 // n = 3 normal track, with sample rate conversion
894 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
895 // n > 3 very high latency or very small notification interval; nBuffering is ignored
896 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
897
Eric Laurentd1b449a2010-05-14 03:26:45 -0700898 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700899
Dima Zavinfce7a472011-04-19 22:30:36 -0700900 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700901
Eric Laurentd1b449a2010-05-14 03:26:45 -0700902 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700903 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700904 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700905 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700906 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700907 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100908 if (mNotificationFramesAct != frameCount) {
909 mNotificationFramesAct = frameCount;
910 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700911 } else if (sharedBuffer != 0) {
912
Glenn Kastena42ff002012-11-14 12:47:55 -0800913 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914 // 8-bit data in shared memory is not currently supported by AudioFlinger
915 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800916 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700917 // More than 2 channels does not require stronger alignment than stereo
918 alignment <<= 1;
919 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800920 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
921 ALOGE("Invalid buffer alignment: address %p, channel count %u",
922 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700923 return BAD_VALUE;
924 }
925
926 // When initializing a shared buffer AudioTrack via constructors,
927 // there's no frameCount parameter.
928 // But when initializing a shared buffer AudioTrack via set(),
929 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800930 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700931
932 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
933
934 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700935
Eric Laurentd1b449a2010-05-14 03:26:45 -0700936 // Ensure that buffer depth covers at least audio hardware latency
937 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700938 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
939 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700940 if (minBufCount <= nBuffering) {
941 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800942 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700943
Glenn Kastene33054e2012-11-14 12:54:39 -0800944 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
945 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800946 ", afLatency=%d",
947 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700948
949 if (frameCount == 0) {
950 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700951 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700952 // not ALOGW because it happens all the time when playing key clicks over A2DP
953 ALOGV("Minimum buffer size corrected from %d to %d",
954 frameCount, minFrameCount);
955 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800956 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700957 // Make sure that application is notified with sufficient margin before underrun
958 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
959 mNotificationFramesAct = frameCount/nBuffering;
960 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700961
Glenn Kastene0fa4672012-04-24 14:35:14 -0700962 } else {
963 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700964 }
965
Glenn Kastena075db42012-03-06 11:22:44 -0800966 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
967 if (mIsTimed) {
968 trackFlags |= IAudioFlinger::TRACK_TIMED;
969 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800970
971 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700972 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700973 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800974 if (mAudioTrackThread != 0) {
975 tid = mAudioTrackThread->getTid();
976 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700977 }
978
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100979 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
980 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
981 }
982
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800983 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800984 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700985 // AudioFlinger only sees 16-bit PCM
986 format == AUDIO_FORMAT_PCM_8_BIT ?
987 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800988 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800989 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800990 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800991 sharedBuffer,
992 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800993 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700994 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700995 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800996 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800997 &status);
998
999 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001000 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001001 return status;
1002 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001003 sp<IMemory> iMem = track->getCblk();
1004 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001005 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001006 return NO_INIT;
1007 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001008 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001009 if (mAudioTrack != 0) {
1010 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1011 mDeathNotifier.clear();
1012 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001013 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001014 mCblkMemory = iMem;
1015 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1016 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001017 size_t temp = cblk->frameCount_;
1018 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1019 // In current design, AudioTrack client checks and ensures frame count validity before
1020 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1021 // for fast track as it uses a special method of assigning frame count.
1022 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1023 }
1024 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001025 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001026 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001027 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001028 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001029 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001030 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001031 // Theoretically double-buffering is not required for fast tracks,
1032 // due to tighter scheduling. But in practice, to accommodate kernels with
1033 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1034 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1035 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001036 }
1037 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001038 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001039 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001040 // once denied, do not request again if IAudioTrack is re-created
1041 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1042 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001043 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001044 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1045 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001046 }
1047 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001048 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001049 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001050 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1051 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1052 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1053 } else {
1054 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1055 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1056 mFlags = flags;
1057 return NO_INIT;
1058 }
1059 }
1060
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001061 mRefreshRemaining = true;
1062
1063 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1064 // is the value of pointer() for the shared buffer, otherwise buffers points
1065 // immediately after the control block. This address is for the mapping within client
1066 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1067 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001068 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001069 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001070 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001071 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001072 }
1073
Eric Laurent2beeb502010-07-16 07:43:46 -07001074 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001075 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001076 mLatency = afLatency + (1000*frameCount) / sampleRate;
1077 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001078 // If IAudioTrack is re-created, don't let the requested frameCount
1079 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001080 if (frameCount > mReqFrameCount) {
1081 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001082 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001083
1084 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001085 if (sharedBuffer == 0) {
1086 mStaticProxy.clear();
1087 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1088 } else {
1089 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1090 mProxy = mStaticProxy;
1091 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001092 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1093 uint16_t(mVolume[LEFT] * 0x1000));
1094 mProxy->setSendLevel(mSendLevel);
1095 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001096 mProxy->setEpoch(epoch);
1097 mProxy->setMinimum(mNotificationFramesAct);
1098
1099 mDeathNotifier = new DeathNotifier(this);
1100 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001101
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001102 return NO_ERROR;
1103}
1104
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001105status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1106{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001107 if (audioBuffer == NULL) {
1108 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001109 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001110 if (mTransfer != TRANSFER_OBTAIN) {
1111 audioBuffer->frameCount = 0;
1112 audioBuffer->size = 0;
1113 audioBuffer->raw = NULL;
1114 return INVALID_OPERATION;
1115 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001116
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001117 const struct timespec *requested;
1118 if (waitCount == -1) {
1119 requested = &ClientProxy::kForever;
1120 } else if (waitCount == 0) {
1121 requested = &ClientProxy::kNonBlocking;
1122 } else if (waitCount > 0) {
1123 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1124 struct timespec timeout;
1125 timeout.tv_sec = ms / 1000;
1126 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1127 requested = &timeout;
1128 } else {
1129 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1130 requested = NULL;
1131 }
1132 return obtainBuffer(audioBuffer, requested);
1133}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001134
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001135status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1136 struct timespec *elapsed, size_t *nonContig)
1137{
1138 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1139 uint32_t oldSequence = 0;
1140 uint32_t newSequence;
1141
1142 Proxy::Buffer buffer;
1143 status_t status = NO_ERROR;
1144
1145 static const int32_t kMaxTries = 5;
1146 int32_t tryCounter = kMaxTries;
1147
1148 do {
1149 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1150 // keep them from going away if another thread re-creates the track during obtainBuffer()
1151 sp<AudioTrackClientProxy> proxy;
1152 sp<IMemory> iMem;
1153
1154 { // start of lock scope
1155 AutoMutex lock(mLock);
1156
1157 newSequence = mSequence;
1158 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1159 if (status == DEAD_OBJECT) {
1160 // re-create track, unless someone else has already done so
1161 if (newSequence == oldSequence) {
1162 status = restoreTrack_l("obtainBuffer");
1163 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001164 buffer.mFrameCount = 0;
1165 buffer.mRaw = NULL;
1166 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001167 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001168 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001169 }
1170 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001171 oldSequence = newSequence;
1172
1173 // Keep the extra references
1174 proxy = mProxy;
1175 iMem = mCblkMemory;
1176
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001177 if (mState == STATE_STOPPING) {
1178 status = -EINTR;
1179 buffer.mFrameCount = 0;
1180 buffer.mRaw = NULL;
1181 buffer.mNonContig = 0;
1182 break;
1183 }
1184
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001185 // Non-blocking if track is stopped or paused
1186 if (mState != STATE_ACTIVE) {
1187 requested = &ClientProxy::kNonBlocking;
1188 }
1189
1190 } // end of lock scope
1191
1192 buffer.mFrameCount = audioBuffer->frameCount;
1193 // FIXME starts the requested timeout and elapsed over from scratch
1194 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1195
1196 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1197
1198 audioBuffer->frameCount = buffer.mFrameCount;
1199 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1200 audioBuffer->raw = buffer.mRaw;
1201 if (nonContig != NULL) {
1202 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001204 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205}
1206
1207void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1208{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001209 if (mTransfer == TRANSFER_SHARED) {
1210 return;
1211 }
1212
1213 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1214 if (stepCount == 0) {
1215 return;
1216 }
1217
1218 Proxy::Buffer buffer;
1219 buffer.mFrameCount = stepCount;
1220 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001221
Eric Laurent1703cdf2011-03-07 14:52:59 -08001222 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001223 mInUnderrun = false;
1224 mProxy->releaseBuffer(&buffer);
1225
1226 // restart track if it was disabled by audioflinger due to previous underrun
1227 if (mState == STATE_ACTIVE) {
1228 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001229 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001230 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1231 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001232 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001233 mAudioTrack->start();
1234 }
1235 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236}
1237
1238// -------------------------------------------------------------------------
1239
1240ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1241{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001242 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001243 return INVALID_OPERATION;
1244 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001246 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001247 // Sanity-check: user is most-likely passing an error code, and it would
1248 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001249 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250 return BAD_VALUE;
1251 }
1252
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001253 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254 Buffer audioBuffer;
1255
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001256 while (userSize >= mFrameSize) {
1257 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001258
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001259 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001261 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001263 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 return ssize_t(err);
1265 }
1266
1267 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001268 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001270 toWrite = audioBuffer.size >> 1;
1271 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001272 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001274 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001276 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 userSize -= toWrite;
1278 written += toWrite;
1279
1280 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001281 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001282
1283 return written;
1284}
1285
1286// -------------------------------------------------------------------------
1287
John Grossman4ff14ba2012-02-08 16:37:41 -08001288TimedAudioTrack::TimedAudioTrack() {
1289 mIsTimed = true;
1290}
1291
1292status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1293{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001294 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001295 status_t result = UNKNOWN_ERROR;
1296
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001297#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001298 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1299 // while we are accessing the cblk
1300 sp<IAudioTrack> audioTrack = mAudioTrack;
1301 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001302#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001303
John Grossman4ff14ba2012-02-08 16:37:41 -08001304 // If the track is not invalid already, try to allocate a buffer. alloc
1305 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001306 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001307 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001308 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001309 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1310 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001311 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001312 }
1313 }
1314
1315 // If the track is invalid at this point, attempt to restore it. and try the
1316 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001317 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001318 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001319
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001320 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001321 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001322 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001323 }
1324
1325 return result;
1326}
1327
1328status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1329 int64_t pts)
1330{
Eric Laurentdf839842012-05-31 14:27:14 -07001331 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1332 {
1333 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001334 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001335 // restart track if it was disabled by audioflinger due to previous underrun
1336 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001337 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1338 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001339 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001340 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001341 mAudioTrack->start();
1342 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001343 }
Eric Laurentdf839842012-05-31 14:27:14 -07001344 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001345}
1346
1347status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1348 TargetTimeline target)
1349{
1350 return mAudioTrack->setMediaTimeTransform(xform, target);
1351}
1352
1353// -------------------------------------------------------------------------
1354
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001355nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001356{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001357 // Currently the AudioTrack thread is not created if there are no callbacks.
1358 // Would it ever make sense to run the thread, even without callbacks?
1359 // If so, then replace this by checks at each use for mCbf != NULL.
1360 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1361
Eric Laurent1703cdf2011-03-07 14:52:59 -08001362 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001363 if (mAwaitBoost) {
1364 mAwaitBoost = false;
1365 mLock.unlock();
1366 static const int32_t kMaxTries = 5;
1367 int32_t tryCounter = kMaxTries;
1368 uint32_t pollUs = 10000;
1369 do {
1370 int policy = sched_getscheduler(0);
1371 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1372 break;
1373 }
1374 usleep(pollUs);
1375 pollUs <<= 1;
1376 } while (tryCounter-- > 0);
1377 if (tryCounter < 0) {
1378 ALOGE("did not receive expected priority boost on time");
1379 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001380 // Run again immediately
1381 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001382 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001383
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001384 // Can only reference mCblk while locked
1385 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001386 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001387
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001388 // Check for track invalidation
1389 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001390 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1391 // AudioSystem cache. We should not exit here but after calling the callback so
1392 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001393 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001394 status_t status = restoreTrack_l("processAudioBuffer");
1395 mLock.unlock();
1396 // Run again immediately, but with a new IAudioTrack
1397 return 0;
1398 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001399 }
1400
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001401 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001402 bool active = mState == STATE_ACTIVE;
1403
1404 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1405 bool newUnderrun = false;
1406 if (flags & CBLK_UNDERRUN) {
1407#if 0
1408 // Currently in shared buffer mode, when the server reaches the end of buffer,
1409 // the track stays active in continuous underrun state. It's up to the application
1410 // to pause or stop the track, or set the position to a new offset within buffer.
1411 // This was some experimental code to auto-pause on underrun. Keeping it here
1412 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1413 if (mTransfer == TRANSFER_SHARED) {
1414 mState = STATE_PAUSED;
1415 active = false;
1416 }
1417#endif
1418 if (!mInUnderrun) {
1419 mInUnderrun = true;
1420 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001421 }
1422 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001423
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001424 // Get current position of server
1425 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426
1427 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001428 bool markerReached = false;
1429 size_t markerPosition = mMarkerPosition;
1430 // FIXME fails for wraparound, need 64 bits
1431 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1432 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001433 }
1434
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001435 // Determine number of new position callback(s) that will be needed, while locked
1436 size_t newPosCount = 0;
1437 size_t newPosition = mNewPosition;
1438 size_t updatePeriod = mUpdatePeriod;
1439 // FIXME fails for wraparound, need 64 bits
1440 if (updatePeriod > 0 && position >= newPosition) {
1441 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1442 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443 }
1444
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001445 // Cache other fields that will be needed soon
1446 uint32_t loopPeriod = mLoopPeriod;
1447 uint32_t sampleRate = mSampleRate;
1448 size_t notificationFrames = mNotificationFramesAct;
1449 if (mRefreshRemaining) {
1450 mRefreshRemaining = false;
1451 mRemainingFrames = notificationFrames;
1452 mRetryOnPartialBuffer = false;
1453 }
1454 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001455 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001456
1457 // These fields don't need to be cached, because they are assigned only by set():
1458 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1459 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1460
1461 mLock.unlock();
1462
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001463 if (waitStreamEnd) {
1464 AutoMutex lock(mLock);
1465
1466 sp<AudioTrackClientProxy> proxy = mProxy;
1467 sp<IMemory> iMem = mCblkMemory;
1468
1469 struct timespec timeout;
1470 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1471 timeout.tv_nsec = 0;
1472
1473 mLock.unlock();
1474 status_t status = mProxy->waitStreamEndDone(&timeout);
1475 mLock.lock();
1476 switch (status) {
1477 case NO_ERROR:
1478 case DEAD_OBJECT:
1479 case TIMED_OUT:
1480 mLock.unlock();
1481 mCbf(EVENT_STREAM_END, mUserData, NULL);
1482 mLock.lock();
1483 if (mState == STATE_STOPPING) {
1484 mState = STATE_STOPPED;
1485 if (status != DEAD_OBJECT) {
1486 return NS_INACTIVE;
1487 }
1488 }
1489 return 0;
1490 default:
1491 return 0;
1492 }
1493 }
1494
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001495 // perform callbacks while unlocked
1496 if (newUnderrun) {
1497 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1498 }
1499 // FIXME we will miss loops if loop cycle was signaled several times since last call
1500 // to processAudioBuffer()
1501 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1502 mCbf(EVENT_LOOP_END, mUserData, NULL);
1503 }
1504 if (flags & CBLK_BUFFER_END) {
1505 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1506 }
1507 if (markerReached) {
1508 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1509 }
1510 while (newPosCount > 0) {
1511 size_t temp = newPosition;
1512 mCbf(EVENT_NEW_POS, mUserData, &temp);
1513 newPosition += updatePeriod;
1514 newPosCount--;
1515 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001516
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001517 if (mObservedSequence != sequence) {
1518 mObservedSequence = sequence;
1519 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001520 // for offloaded tracks, just wait for the upper layers to recreate the track
1521 if (isOffloaded()) {
1522 return NS_INACTIVE;
1523 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524 }
1525
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001526 // if inactive, then don't run me again until re-started
1527 if (!active) {
1528 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001529 }
1530
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001531 // Compute the estimated time until the next timed event (position, markers, loops)
1532 // FIXME only for non-compressed audio
1533 uint32_t minFrames = ~0;
1534 if (!markerReached && position < markerPosition) {
1535 minFrames = markerPosition - position;
1536 }
1537 if (loopPeriod > 0 && loopPeriod < minFrames) {
1538 minFrames = loopPeriod;
1539 }
1540 if (updatePeriod > 0 && updatePeriod < minFrames) {
1541 minFrames = updatePeriod;
1542 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001543
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001544 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1545 static const uint32_t kPoll = 0;
1546 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1547 minFrames = kPoll * notificationFrames;
1548 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001549
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001550 // Convert frame units to time units
1551 nsecs_t ns = NS_WHENEVER;
1552 if (minFrames != (uint32_t) ~0) {
1553 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1554 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1555 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1556 }
1557
1558 // If not supplying data by EVENT_MORE_DATA, then we're done
1559 if (mTransfer != TRANSFER_CALLBACK) {
1560 return ns;
1561 }
1562
1563 struct timespec timeout;
1564 const struct timespec *requested = &ClientProxy::kForever;
1565 if (ns != NS_WHENEVER) {
1566 timeout.tv_sec = ns / 1000000000LL;
1567 timeout.tv_nsec = ns % 1000000000LL;
1568 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1569 requested = &timeout;
1570 }
1571
1572 while (mRemainingFrames > 0) {
1573
1574 Buffer audioBuffer;
1575 audioBuffer.frameCount = mRemainingFrames;
1576 size_t nonContig;
1577 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1578 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1579 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1580 requested = &ClientProxy::kNonBlocking;
1581 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001582 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1583 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001584 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001585 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1586 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001587 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001588 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001589 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1590 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001591 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001592
Eric Laurent42a6f422013-08-29 14:35:05 -07001593 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001594 mRetryOnPartialBuffer = false;
1595 if (avail < mRemainingFrames) {
1596 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1597 if (ns < 0 || myns < ns) {
1598 ns = myns;
1599 }
1600 return ns;
1601 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001602 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603
1604 // Divide buffer size by 2 to take into account the expansion
1605 // due to 8 to 16 bit conversion: the callback must fill only half
1606 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001607 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608 audioBuffer.size >>= 1;
1609 }
1610
1611 size_t reqSize = audioBuffer.size;
1612 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001613 size_t writtenSize = audioBuffer.size;
1614 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001615
1616 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001617 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1618 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1619 reqSize, (int) writtenSize);
1620 return NS_NEVER;
1621 }
1622
1623 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001624 // The callback is done filling buffers
1625 // Keep this thread going to handle timed events and
1626 // still try to get more data in intervals of WAIT_PERIOD_MS
1627 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001628 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001629 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001631 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001632 // 8 to 16 bit conversion, note that source and destination are the same address
1633 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001634 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001635 }
1636
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001637 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1638 audioBuffer.frameCount = releasedFrames;
1639 mRemainingFrames -= releasedFrames;
1640 if (misalignment >= releasedFrames) {
1641 misalignment -= releasedFrames;
1642 } else {
1643 misalignment = 0;
1644 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645
1646 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001647
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001648 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1649 // if callback doesn't like to accept the full chunk
1650 if (writtenSize < reqSize) {
1651 continue;
1652 }
1653
1654 // There could be enough non-contiguous frames available to satisfy the remaining request
1655 if (mRemainingFrames <= nonContig) {
1656 continue;
1657 }
1658
1659#if 0
1660 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1661 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1662 // that total to a sum == notificationFrames.
1663 if (0 < misalignment && misalignment <= mRemainingFrames) {
1664 mRemainingFrames = misalignment;
1665 return (mRemainingFrames * 1100000000LL) / sampleRate;
1666 }
1667#endif
1668
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001669 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001670 mRemainingFrames = notificationFrames;
1671 mRetryOnPartialBuffer = true;
1672
1673 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1674 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001675}
1676
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001677status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001678{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001679 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001680 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001681 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001682 status_t result;
1683
Glenn Kastena47f3162012-11-07 10:13:08 -08001684 // refresh the audio configuration cache in this process to make sure we get new
1685 // output parameters in getOutput_l() and createTrack_l()
1686 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001687
Glenn Kasten23a75452014-01-13 10:37:17 -08001688 if (isOffloaded_l()) {
1689 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001690 return DEAD_OBJECT;
1691 }
1692
1693 // force new output query from audio policy manager;
1694 mOutput = 0;
1695 audio_io_handle_t output = getOutput_l();
1696
Glenn Kastena47f3162012-11-07 10:13:08 -08001697 // if the new IAudioTrack is created, createTrack_l() will modify the
1698 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1699 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001700
1701 // take the frames that will be lost by track recreation into account in saved position
1702 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001703 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001704 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001705 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001706 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001707 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001708 mFlags,
1709 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001710 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001711 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001712
Glenn Kastena47f3162012-11-07 10:13:08 -08001713 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001714 // continue playback from last known position, but
1715 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1716 if (mStaticProxy != NULL) {
1717 mLoopPeriod = 0;
1718 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1719 }
1720 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1721 // track destruction have been played? This is critical for SoundPool implementation
1722 // This must be broken, and needs to be tested/debugged.
1723#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001724 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001725 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001726 // Make sure that a client relying on callback events indicating underrun or
1727 // the actual amount of audio frames played (e.g SoundPool) receives them.
1728 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001729 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001730 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001731 }
1732 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001733#endif
1734 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001735 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001736 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001737 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001738 if (result != NO_ERROR) {
Glenn Kasten2b2165c2014-01-13 08:53:36 -08001739 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001740 // As getOutput was called above and resulted in an output stream to be opened,
1741 // we need to release it.
1742 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001743 ALOGW("restoreTrack_l() failed status %d", result);
1744 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001745 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001746
1747 return result;
1748}
1749
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001750status_t AudioTrack::setParameters(const String8& keyValuePairs)
1751{
1752 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001753 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001754}
1755
Glenn Kastence703742013-07-19 16:33:58 -07001756status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1757{
Glenn Kasten53cec222013-08-29 09:01:02 -07001758 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001759 // FIXME not implemented for fast tracks; should use proxy and SSQ
1760 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1761 return INVALID_OPERATION;
1762 }
1763 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1764 return INVALID_OPERATION;
1765 }
1766 status_t status = mAudioTrack->getTimestamp(timestamp);
1767 if (status == NO_ERROR) {
1768 timestamp.mPosition += mProxy->getEpoch();
1769 }
1770 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001771}
1772
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001773String8 AudioTrack::getParameters(const String8& keys)
1774{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001775 audio_io_handle_t output = getOutput();
1776 if (output != 0) {
1777 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001778 } else {
1779 return String8::empty();
1780 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001781}
1782
Glenn Kasten23a75452014-01-13 10:37:17 -08001783bool AudioTrack::isOffloaded() const
1784{
1785 AutoMutex lock(mLock);
1786 return isOffloaded_l();
1787}
1788
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001789status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001790{
1791
1792 const size_t SIZE = 256;
1793 char buffer[SIZE];
1794 String8 result;
1795
1796 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001797 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1798 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001799 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001800 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001801 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001802 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001803 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001804 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001805 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806 result.append(buffer);
1807 ::write(fd, result.string(), result.size());
1808 return NO_ERROR;
1809}
1810
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001811uint32_t AudioTrack::getUnderrunFrames() const
1812{
1813 AutoMutex lock(mLock);
1814 return mProxy->getUnderrunFrames();
1815}
1816
1817// =========================================================================
1818
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001819void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001820{
1821 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1822 if (audioTrack != 0) {
1823 AutoMutex lock(audioTrack->mLock);
1824 audioTrack->mProxy->binderDied();
1825 }
1826}
1827
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001828// =========================================================================
1829
1830AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001831 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1832 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001833{
1834}
1835
1836AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001837{
1838}
1839
1840bool AudioTrack::AudioTrackThread::threadLoop()
1841{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001842 {
1843 AutoMutex _l(mMyLock);
1844 if (mPaused) {
1845 mMyCond.wait(mMyLock);
1846 // caller will check for exitPending()
1847 return true;
1848 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001849 if (mIgnoreNextPausedInt) {
1850 mIgnoreNextPausedInt = false;
1851 mPausedInt = false;
1852 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001853 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001854 if (mPausedNs > 0) {
1855 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1856 } else {
1857 mMyCond.wait(mMyLock);
1858 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001859 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001860 return true;
1861 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001862 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001863 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001864 switch (ns) {
1865 case 0:
1866 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001867 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001868 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001869 return true;
1870 case NS_NEVER:
1871 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001872 case NS_WHENEVER:
1873 // FIXME increase poll interval, or make event-driven
1874 ns = 1000000000LL;
1875 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001876 default:
1877 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001878 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001879 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001880 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001881}
1882
Glenn Kasten3acbd052012-02-28 10:39:56 -08001883void AudioTrack::AudioTrackThread::requestExit()
1884{
1885 // must be in this order to avoid a race condition
1886 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001887 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001888}
1889
1890void AudioTrack::AudioTrackThread::pause()
1891{
1892 AutoMutex _l(mMyLock);
1893 mPaused = true;
1894}
1895
1896void AudioTrack::AudioTrackThread::resume()
1897{
1898 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001899 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001900 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001901 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001902 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001903 mMyCond.signal();
1904 }
1905}
1906
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001907void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1908{
1909 AutoMutex _l(mMyLock);
1910 mPausedInt = true;
1911 mPausedNs = ns;
1912}
1913
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001914}; // namespace android