blob: a113f77ff142b094fcde7ac591c8b73ec835f561 [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;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800323 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800324 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700325 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800326 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700327 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800328 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
329 mClientUid = IPCThreadState::self()->getCallingUid();
330 } else {
331 mClientUid = uid;
332 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700333 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700334 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700335 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700336
Glenn Kastena997e7a2012-08-07 09:44:19 -0700337 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700338 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700339 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
340 }
341
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800342 // create the IAudioTrack
Glenn Kastenb1bef512014-01-13 10:25:53 -0800343 status = createTrack_l(streamType,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800344 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800345 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800346 frameCount,
347 flags,
348 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800349 output,
350 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800351
Glenn Kastena997e7a2012-08-07 09:44:19 -0700352 if (status != NO_ERROR) {
353 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100354 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
355 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700356 mAudioTrackThread.clear();
357 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100358 //Use of direct and offloaded output streams is ref counted by audio policy manager.
359 // As getOutput was called above and resulted in an output stream to be opened,
360 // we need to release it.
361 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700362 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700363 }
364
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800366 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800367 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800369 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800370 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800371 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700373 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 mNewPosition = 0;
375 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700376 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800377 mSequence = 1;
378 mObservedSequence = mSequence;
379 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100380 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800381
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 return NO_ERROR;
383}
384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385// -------------------------------------------------------------------------
386
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100387status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800389 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100390
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800391 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100392 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 }
394
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800397 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100398 if (previousState == STATE_PAUSED_STOPPING) {
399 mState = STATE_STOPPING;
400 } else {
401 mState = STATE_ACTIVE;
402 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800403 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
404 // reset current position as seen by client to 0
405 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700406 // force refresh of remaining frames by processAudioBuffer() as last
407 // write before stop could be partial.
408 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800409 }
410 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700411 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800412
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800414 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100415 if (previousState == STATE_STOPPING) {
416 mProxy->interrupt();
417 } else {
418 t->resume();
419 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800420 } else {
421 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
422 get_sched_policy(0, &mPreviousSchedulingGroup);
423 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
424 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800426 status_t status = NO_ERROR;
427 if (!(flags & CBLK_INVALID)) {
428 status = mAudioTrack->start();
429 if (status == DEAD_OBJECT) {
430 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800432 }
433 if (flags & CBLK_INVALID) {
434 status = restoreTrack_l("start");
435 }
436
437 if (status != NO_ERROR) {
438 ALOGE("start() status %d", status);
439 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100441 if (previousState != STATE_STOPPING) {
442 t->pause();
443 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700445 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700446 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 }
448 }
449
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100450 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800451}
452
453void AudioTrack::stop()
454{
455 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700456 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800457 return;
458 }
459
Glenn Kasten23a75452014-01-13 10:37:17 -0800460 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100461 mState = STATE_STOPPING;
462 } else {
463 mState = STATE_STOPPED;
464 }
465
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800466 mProxy->interrupt();
467 mAudioTrack->stop();
468 // the playback head position will reset to 0, so if a marker is set, we need
469 // to activate it again
470 mMarkerReached = false;
471#if 0
472 // Force flush if a shared buffer is used otherwise audioflinger
473 // will not stop before end of buffer is reached.
474 // It may be needed to make sure that we stop playback, likely in case looping is on.
475 if (mSharedBuffer != 0) {
476 flush_l();
477 }
478#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100479
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800480 sp<AudioTrackThread> t = mAudioTrackThread;
481 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800482 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100483 t->pause();
484 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800485 } else {
486 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
487 set_sched_policy(0, mPreviousSchedulingGroup);
488 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489}
490
491bool AudioTrack::stopped() const
492{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800493 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800494 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495}
496
497void AudioTrack::flush()
498{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800499 if (mSharedBuffer != 0) {
500 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800501 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800502 AutoMutex lock(mLock);
503 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
504 return;
505 }
506 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800507}
508
Eric Laurent1703cdf2011-03-07 14:52:59 -0800509void AudioTrack::flush_l()
510{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800511 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700512
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700513 // clear playback marker and periodic update counter
514 mMarkerPosition = 0;
515 mMarkerReached = false;
516 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100517 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800520 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100521 mProxy->interrupt();
522 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800523 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800524 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525}
526
527void AudioTrack::pause()
528{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800529 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100530 if (mState == STATE_ACTIVE) {
531 mState = STATE_PAUSED;
532 } else if (mState == STATE_STOPPING) {
533 mState = STATE_PAUSED_STOPPING;
534 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800535 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800537 mProxy->interrupt();
538 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539}
540
Eric Laurentbe916aa2010-06-01 23:49:17 -0700541status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800543 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700544 return BAD_VALUE;
545 }
546
Eric Laurent1703cdf2011-03-07 14:52:59 -0800547 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 mVolume[LEFT] = left;
549 mVolume[RIGHT] = right;
550
Glenn Kastene3aa6592012-12-04 12:22:46 -0800551 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700552
Glenn Kasten23a75452014-01-13 10:37:17 -0800553 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700554 mAudioTrack->signal();
555 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700556 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557}
558
Glenn Kastenb1c09932012-02-27 16:21:04 -0800559status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800561 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700562}
563
Eric Laurent2beeb502010-07-16 07:43:46 -0700564status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700565{
Glenn Kasten05632a52012-01-03 14:22:33 -0800566 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700567 return BAD_VALUE;
568 }
569
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800570 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800572 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700573
574 return NO_ERROR;
575}
576
Glenn Kastena5224f32012-01-04 12:41:44 -0800577void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700578{
579 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800580 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700581 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582}
583
Glenn Kasten3b16c762012-11-14 08:44:39 -0800584status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100586 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800587 return INVALID_OPERATION;
588 }
589
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800590 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700592 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593 }
594 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700595 if (rate == 0 || rate > afSamplingRate*2 ) {
596 return BAD_VALUE;
597 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598
Eric Laurent1703cdf2011-03-07 14:52:59 -0800599 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800600 mSampleRate = rate;
601 mProxy->setSampleRate(rate);
602
Eric Laurent57326622009-07-07 07:10:45 -0700603 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604}
605
Glenn Kastena5224f32012-01-04 12:41:44 -0800606uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607{
John Grossman4ff14ba2012-02-08 16:37:41 -0800608 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800609 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800610 }
611
Eric Laurent1703cdf2011-03-07 14:52:59 -0800612 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700613
614 // sample rate can be updated during playback by the offloaded decoder so we need to
615 // query the HAL and update if needed.
616// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800617 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700618 if (mOutput != 0) {
619 uint32_t sampleRate = 0;
620 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
621 if (status == NO_ERROR) {
622 mSampleRate = sampleRate;
623 }
624 }
625 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800626 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627}
628
629status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
630{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100631 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800632 return INVALID_OPERATION;
633 }
634
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800636 ;
637 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
638 loopEnd - loopStart >= MIN_LOOP) {
639 ;
640 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 return BAD_VALUE;
642 }
643
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800644 AutoMutex lock(mLock);
645 // See setPosition() regarding setting parameters such as loop points or position while active
646 if (mState == STATE_ACTIVE) {
647 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700648 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800649 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 return NO_ERROR;
651}
652
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800653void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
654{
655 // FIXME If setting a loop also sets position to start of loop, then
656 // this is correct. Otherwise it should be removed.
657 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
658 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
659 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
660}
661
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662status_t AudioTrack::setMarkerPosition(uint32_t marker)
663{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700664 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100665 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700666 return INVALID_OPERATION;
667 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800669 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700671 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672
673 return NO_ERROR;
674}
675
Glenn Kastena5224f32012-01-04 12:41:44 -0800676status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100678 if (isOffloaded()) {
679 return INVALID_OPERATION;
680 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700681 if (marker == NULL) {
682 return BAD_VALUE;
683 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800685 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686 *marker = mMarkerPosition;
687
688 return NO_ERROR;
689}
690
691status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
692{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700693 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100694 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700695 return INVALID_OPERATION;
696 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800698 AutoMutex lock(mLock);
699 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701 return NO_ERROR;
702}
703
Glenn Kastena5224f32012-01-04 12:41:44 -0800704status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100706 if (isOffloaded()) {
707 return INVALID_OPERATION;
708 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700709 if (updatePeriod == NULL) {
710 return BAD_VALUE;
711 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800713 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714 *updatePeriod = mUpdatePeriod;
715
716 return NO_ERROR;
717}
718
719status_t AudioTrack::setPosition(uint32_t position)
720{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100721 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700722 return INVALID_OPERATION;
723 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800724 if (position > mFrameCount) {
725 return BAD_VALUE;
726 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800727
Eric Laurent1703cdf2011-03-07 14:52:59 -0800728 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800729 // Currently we require that the player is inactive before setting parameters such as position
730 // or loop points. Otherwise, there could be a race condition: the application could read the
731 // current position, compute a new position or loop parameters, and then set that position or
732 // loop parameters but it would do the "wrong" thing since the position has continued to advance
733 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
734 // to specify how it wants to handle such scenarios.
735 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700736 return INVALID_OPERATION;
737 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800738 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
739 mLoopPeriod = 0;
740 // FIXME Check whether loops and setting position are incompatible in old code.
741 // If we use setLoop for both purposes we lose the capability to set the position while looping.
742 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700743
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800744 return NO_ERROR;
745}
746
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800747status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800748{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700749 if (position == NULL) {
750 return BAD_VALUE;
751 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800752
Eric Laurent1703cdf2011-03-07 14:52:59 -0800753 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800754 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100755 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100757 if (mOutput != 0) {
758 uint32_t halFrames;
759 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
760 }
761 *position = dspFrames;
762 } else {
763 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
764 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
765 mProxy->getPosition();
766 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800767 return NO_ERROR;
768}
769
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800770status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800771{
772 if (mSharedBuffer == 0 || mIsTimed) {
773 return INVALID_OPERATION;
774 }
775 if (position == NULL) {
776 return BAD_VALUE;
777 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800778
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800779 AutoMutex lock(mLock);
780 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800781 return NO_ERROR;
782}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800783
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800784status_t AudioTrack::reload()
785{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100786 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800787 return INVALID_OPERATION;
788 }
789
Eric Laurent1703cdf2011-03-07 14:52:59 -0800790 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800791 // See setPosition() regarding setting parameters such as loop points or position while active
792 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700793 return INVALID_OPERATION;
794 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800795 mNewPosition = mUpdatePeriod;
796 mLoopPeriod = 0;
797 // FIXME The new code cannot reload while keeping a loop specified.
798 // Need to check how the old code handled this, and whether it's a significant change.
799 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800800 return NO_ERROR;
801}
802
Eric Laurentc2f1f072009-07-17 12:17:14 -0700803audio_io_handle_t AudioTrack::getOutput()
804{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800805 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100806 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800807}
808
809// must be called with mLock held
810audio_io_handle_t AudioTrack::getOutput_l()
811{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100812 if (mOutput) {
813 return mOutput;
814 } else {
815 return AudioSystem::getOutput(mStreamType,
816 mSampleRate, mFormat, mChannelMask, mFlags);
817 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700818}
819
Eric Laurentbe916aa2010-06-01 23:49:17 -0700820status_t AudioTrack::attachAuxEffect(int effectId)
821{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800822 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700823 status_t status = mAudioTrack->attachAuxEffect(effectId);
824 if (status == NO_ERROR) {
825 mAuxEffectId = effectId;
826 }
827 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700828}
829
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800830// -------------------------------------------------------------------------
831
Eric Laurent1703cdf2011-03-07 14:52:59 -0800832// must be called with mLock held
833status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800834 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800835 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800836 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800837 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700838 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800839 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800840 audio_io_handle_t output,
841 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800842{
843 status_t status;
844 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
845 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700846 ALOGE("Could not get audioflinger");
847 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800848 }
849
Glenn Kastence8828a2013-09-16 18:07:38 -0700850 // Not all of these values are needed under all conditions, but it is easier to get them all
851
Eric Laurentd1b449a2010-05-14 03:26:45 -0700852 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700853 status = AudioSystem::getLatency(output, streamType, &afLatency);
854 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800855 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700856 return NO_INIT;
857 }
858
Glenn Kastence8828a2013-09-16 18:07:38 -0700859 size_t afFrameCount;
860 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
861 if (status != NO_ERROR) {
862 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
863 return NO_INIT;
864 }
865
866 uint32_t afSampleRate;
867 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
868 if (status != NO_ERROR) {
869 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
870 return NO_INIT;
871 }
872
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700873 // Client decides whether the track is TIMED (see below), but can only express a preference
874 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700875 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700876 // either of these use cases:
877 // use case 1: shared buffer
878 (sharedBuffer != 0) ||
879 // use case 2: callback handler
880 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800881 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700882 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700883 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700884 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700886 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700887
Glenn Kastence8828a2013-09-16 18:07:38 -0700888 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800889 // n = 1 fast track with single buffering; nBuffering is ignored
890 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700891 // n = 2 normal track, no sample rate conversion
892 // n = 3 normal track, with sample rate conversion
893 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
894 // n > 3 very high latency or very small notification interval; nBuffering is ignored
895 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
896
Eric Laurentd1b449a2010-05-14 03:26:45 -0700897 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700898
Dima Zavinfce7a472011-04-19 22:30:36 -0700899 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700900
Eric Laurentd1b449a2010-05-14 03:26:45 -0700901 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700902 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700903 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700904 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700905 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700906 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100907 if (mNotificationFramesAct != frameCount) {
908 mNotificationFramesAct = frameCount;
909 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910 } else if (sharedBuffer != 0) {
911
Glenn Kastena42ff002012-11-14 12:47:55 -0800912 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700913 // 8-bit data in shared memory is not currently supported by AudioFlinger
914 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800915 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916 // More than 2 channels does not require stronger alignment than stereo
917 alignment <<= 1;
918 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800919 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
920 ALOGE("Invalid buffer alignment: address %p, channel count %u",
921 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922 return BAD_VALUE;
923 }
924
925 // When initializing a shared buffer AudioTrack via constructors,
926 // there's no frameCount parameter.
927 // But when initializing a shared buffer AudioTrack via set(),
928 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800929 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700930
931 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
932
933 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934
Eric Laurentd1b449a2010-05-14 03:26:45 -0700935 // Ensure that buffer depth covers at least audio hardware latency
936 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700937 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
938 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700939 if (minBufCount <= nBuffering) {
940 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800941 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700942
Glenn Kastene33054e2012-11-14 12:54:39 -0800943 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
944 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800945 ", afLatency=%d",
946 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700947
948 if (frameCount == 0) {
949 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700950 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700951 // not ALOGW because it happens all the time when playing key clicks over A2DP
952 ALOGV("Minimum buffer size corrected from %d to %d",
953 frameCount, minFrameCount);
954 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800955 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700956 // Make sure that application is notified with sufficient margin before underrun
957 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
958 mNotificationFramesAct = frameCount/nBuffering;
959 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700960
Glenn Kastene0fa4672012-04-24 14:35:14 -0700961 } else {
962 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700963 }
964
Glenn Kastena075db42012-03-06 11:22:44 -0800965 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
966 if (mIsTimed) {
967 trackFlags |= IAudioFlinger::TRACK_TIMED;
968 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800969
970 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700971 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700972 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800973 if (mAudioTrackThread != 0) {
974 tid = mAudioTrackThread->getTid();
975 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700976 }
977
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100978 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
979 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
980 }
981
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800982 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800983 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700984 // AudioFlinger only sees 16-bit PCM
985 format == AUDIO_FORMAT_PCM_8_BIT ?
986 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800987 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800988 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800989 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800990 sharedBuffer,
991 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800992 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700993 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700994 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800995 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800996 &status);
997
998 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000999 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001000 return status;
1001 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001002 sp<IMemory> iMem = track->getCblk();
1003 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001004 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001005 return NO_INIT;
1006 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001007 void *iMemPointer = iMem->pointer();
1008 if (iMemPointer == NULL) {
1009 ALOGE("Could not get control block pointer");
1010 return NO_INIT;
1011 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001012 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001013 if (mAudioTrack != 0) {
1014 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1015 mDeathNotifier.clear();
1016 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001017 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001018 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001019 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001020 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001021 size_t temp = cblk->frameCount_;
1022 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1023 // In current design, AudioTrack client checks and ensures frame count validity before
1024 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1025 // for fast track as it uses a special method of assigning frame count.
1026 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1027 }
1028 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001029 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001030 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001031 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001032 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001033 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001034 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001035 // Theoretically double-buffering is not required for fast tracks,
1036 // due to tighter scheduling. But in practice, to accommodate kernels with
1037 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1038 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1039 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001040 }
1041 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001042 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001043 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001044 // once denied, do not request again if IAudioTrack is re-created
1045 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1046 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001047 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001048 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1049 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001050 }
1051 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001052 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001053 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001054 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1055 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1056 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1057 } else {
1058 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1059 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1060 mFlags = flags;
1061 return NO_INIT;
1062 }
1063 }
1064
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001065 mRefreshRemaining = true;
1066
1067 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1068 // is the value of pointer() for the shared buffer, otherwise buffers points
1069 // immediately after the control block. This address is for the mapping within client
1070 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1071 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001072 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001073 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001074 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001075 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001076 }
1077
Eric Laurent2beeb502010-07-16 07:43:46 -07001078 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001079 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001080 mLatency = afLatency + (1000*frameCount) / sampleRate;
1081 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001082 // If IAudioTrack is re-created, don't let the requested frameCount
1083 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001084 if (frameCount > mReqFrameCount) {
1085 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001086 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001087
1088 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001089 if (sharedBuffer == 0) {
1090 mStaticProxy.clear();
1091 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1092 } else {
1093 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1094 mProxy = mStaticProxy;
1095 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001096 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1097 uint16_t(mVolume[LEFT] * 0x1000));
1098 mProxy->setSendLevel(mSendLevel);
1099 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001100 mProxy->setEpoch(epoch);
1101 mProxy->setMinimum(mNotificationFramesAct);
1102
1103 mDeathNotifier = new DeathNotifier(this);
1104 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001105
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001106 return NO_ERROR;
1107}
1108
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001109status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1110{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001111 if (audioBuffer == NULL) {
1112 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001113 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001114 if (mTransfer != TRANSFER_OBTAIN) {
1115 audioBuffer->frameCount = 0;
1116 audioBuffer->size = 0;
1117 audioBuffer->raw = NULL;
1118 return INVALID_OPERATION;
1119 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001120
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001121 const struct timespec *requested;
1122 if (waitCount == -1) {
1123 requested = &ClientProxy::kForever;
1124 } else if (waitCount == 0) {
1125 requested = &ClientProxy::kNonBlocking;
1126 } else if (waitCount > 0) {
1127 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1128 struct timespec timeout;
1129 timeout.tv_sec = ms / 1000;
1130 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1131 requested = &timeout;
1132 } else {
1133 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1134 requested = NULL;
1135 }
1136 return obtainBuffer(audioBuffer, requested);
1137}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001138
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001139status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1140 struct timespec *elapsed, size_t *nonContig)
1141{
1142 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1143 uint32_t oldSequence = 0;
1144 uint32_t newSequence;
1145
1146 Proxy::Buffer buffer;
1147 status_t status = NO_ERROR;
1148
1149 static const int32_t kMaxTries = 5;
1150 int32_t tryCounter = kMaxTries;
1151
1152 do {
1153 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1154 // keep them from going away if another thread re-creates the track during obtainBuffer()
1155 sp<AudioTrackClientProxy> proxy;
1156 sp<IMemory> iMem;
1157
1158 { // start of lock scope
1159 AutoMutex lock(mLock);
1160
1161 newSequence = mSequence;
1162 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1163 if (status == DEAD_OBJECT) {
1164 // re-create track, unless someone else has already done so
1165 if (newSequence == oldSequence) {
1166 status = restoreTrack_l("obtainBuffer");
1167 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001168 buffer.mFrameCount = 0;
1169 buffer.mRaw = NULL;
1170 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001171 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001173 }
1174 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001175 oldSequence = newSequence;
1176
1177 // Keep the extra references
1178 proxy = mProxy;
1179 iMem = mCblkMemory;
1180
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001181 if (mState == STATE_STOPPING) {
1182 status = -EINTR;
1183 buffer.mFrameCount = 0;
1184 buffer.mRaw = NULL;
1185 buffer.mNonContig = 0;
1186 break;
1187 }
1188
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001189 // Non-blocking if track is stopped or paused
1190 if (mState != STATE_ACTIVE) {
1191 requested = &ClientProxy::kNonBlocking;
1192 }
1193
1194 } // end of lock scope
1195
1196 buffer.mFrameCount = audioBuffer->frameCount;
1197 // FIXME starts the requested timeout and elapsed over from scratch
1198 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1199
1200 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1201
1202 audioBuffer->frameCount = buffer.mFrameCount;
1203 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1204 audioBuffer->raw = buffer.mRaw;
1205 if (nonContig != NULL) {
1206 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001208 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209}
1210
1211void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1212{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001213 if (mTransfer == TRANSFER_SHARED) {
1214 return;
1215 }
1216
1217 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1218 if (stepCount == 0) {
1219 return;
1220 }
1221
1222 Proxy::Buffer buffer;
1223 buffer.mFrameCount = stepCount;
1224 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001225
Eric Laurent1703cdf2011-03-07 14:52:59 -08001226 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001227 mInUnderrun = false;
1228 mProxy->releaseBuffer(&buffer);
1229
1230 // restart track if it was disabled by audioflinger due to previous underrun
1231 if (mState == STATE_ACTIVE) {
1232 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001233 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001234 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1235 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001236 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001237 mAudioTrack->start();
1238 }
1239 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240}
1241
1242// -------------------------------------------------------------------------
1243
1244ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1245{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001246 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001247 return INVALID_OPERATION;
1248 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001250 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001251 // Sanity-check: user is most-likely passing an error code, and it would
1252 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001253 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254 return BAD_VALUE;
1255 }
1256
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001257 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258 Buffer audioBuffer;
1259
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001260 while (userSize >= mFrameSize) {
1261 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001262
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001263 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001265 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001267 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 return ssize_t(err);
1269 }
1270
1271 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001272 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001274 toWrite = audioBuffer.size >> 1;
1275 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001276 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001278 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001280 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281 userSize -= toWrite;
1282 written += toWrite;
1283
1284 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001285 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286
1287 return written;
1288}
1289
1290// -------------------------------------------------------------------------
1291
John Grossman4ff14ba2012-02-08 16:37:41 -08001292TimedAudioTrack::TimedAudioTrack() {
1293 mIsTimed = true;
1294}
1295
1296status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1297{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001298 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001299 status_t result = UNKNOWN_ERROR;
1300
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001301#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001302 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1303 // while we are accessing the cblk
1304 sp<IAudioTrack> audioTrack = mAudioTrack;
1305 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001306#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001307
John Grossman4ff14ba2012-02-08 16:37:41 -08001308 // If the track is not invalid already, try to allocate a buffer. alloc
1309 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001310 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001311 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001312 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001313 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1314 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001315 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001316 }
1317 }
1318
1319 // If the track is invalid at this point, attempt to restore it. and try the
1320 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001321 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001322 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001323
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001324 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001325 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001326 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001327 }
1328
1329 return result;
1330}
1331
1332status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1333 int64_t pts)
1334{
Eric Laurentdf839842012-05-31 14:27:14 -07001335 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1336 {
1337 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001338 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001339 // restart track if it was disabled by audioflinger due to previous underrun
1340 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001341 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1342 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001343 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001344 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001345 mAudioTrack->start();
1346 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001347 }
Eric Laurentdf839842012-05-31 14:27:14 -07001348 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001349}
1350
1351status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1352 TargetTimeline target)
1353{
1354 return mAudioTrack->setMediaTimeTransform(xform, target);
1355}
1356
1357// -------------------------------------------------------------------------
1358
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001359nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001360{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001361 // Currently the AudioTrack thread is not created if there are no callbacks.
1362 // Would it ever make sense to run the thread, even without callbacks?
1363 // If so, then replace this by checks at each use for mCbf != NULL.
1364 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1365
Eric Laurent1703cdf2011-03-07 14:52:59 -08001366 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001367 if (mAwaitBoost) {
1368 mAwaitBoost = false;
1369 mLock.unlock();
1370 static const int32_t kMaxTries = 5;
1371 int32_t tryCounter = kMaxTries;
1372 uint32_t pollUs = 10000;
1373 do {
1374 int policy = sched_getscheduler(0);
1375 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1376 break;
1377 }
1378 usleep(pollUs);
1379 pollUs <<= 1;
1380 } while (tryCounter-- > 0);
1381 if (tryCounter < 0) {
1382 ALOGE("did not receive expected priority boost on time");
1383 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001384 // Run again immediately
1385 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001386 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001387
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001388 // Can only reference mCblk while locked
1389 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001390 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001391
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001392 // Check for track invalidation
1393 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001394 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1395 // AudioSystem cache. We should not exit here but after calling the callback so
1396 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001397 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001398 status_t status = restoreTrack_l("processAudioBuffer");
1399 mLock.unlock();
1400 // Run again immediately, but with a new IAudioTrack
1401 return 0;
1402 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001403 }
1404
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001405 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001406 bool active = mState == STATE_ACTIVE;
1407
1408 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1409 bool newUnderrun = false;
1410 if (flags & CBLK_UNDERRUN) {
1411#if 0
1412 // Currently in shared buffer mode, when the server reaches the end of buffer,
1413 // the track stays active in continuous underrun state. It's up to the application
1414 // to pause or stop the track, or set the position to a new offset within buffer.
1415 // This was some experimental code to auto-pause on underrun. Keeping it here
1416 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1417 if (mTransfer == TRANSFER_SHARED) {
1418 mState = STATE_PAUSED;
1419 active = false;
1420 }
1421#endif
1422 if (!mInUnderrun) {
1423 mInUnderrun = true;
1424 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425 }
1426 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001427
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001428 // Get current position of server
1429 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001430
1431 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001432 bool markerReached = false;
1433 size_t markerPosition = mMarkerPosition;
1434 // FIXME fails for wraparound, need 64 bits
1435 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1436 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437 }
1438
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001439 // Determine number of new position callback(s) that will be needed, while locked
1440 size_t newPosCount = 0;
1441 size_t newPosition = mNewPosition;
1442 size_t updatePeriod = mUpdatePeriod;
1443 // FIXME fails for wraparound, need 64 bits
1444 if (updatePeriod > 0 && position >= newPosition) {
1445 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1446 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447 }
1448
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001449 // Cache other fields that will be needed soon
1450 uint32_t loopPeriod = mLoopPeriod;
1451 uint32_t sampleRate = mSampleRate;
1452 size_t notificationFrames = mNotificationFramesAct;
1453 if (mRefreshRemaining) {
1454 mRefreshRemaining = false;
1455 mRemainingFrames = notificationFrames;
1456 mRetryOnPartialBuffer = false;
1457 }
1458 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001459 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001460
1461 // These fields don't need to be cached, because they are assigned only by set():
1462 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1463 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1464
1465 mLock.unlock();
1466
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001467 if (waitStreamEnd) {
1468 AutoMutex lock(mLock);
1469
1470 sp<AudioTrackClientProxy> proxy = mProxy;
1471 sp<IMemory> iMem = mCblkMemory;
1472
1473 struct timespec timeout;
1474 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1475 timeout.tv_nsec = 0;
1476
1477 mLock.unlock();
1478 status_t status = mProxy->waitStreamEndDone(&timeout);
1479 mLock.lock();
1480 switch (status) {
1481 case NO_ERROR:
1482 case DEAD_OBJECT:
1483 case TIMED_OUT:
1484 mLock.unlock();
1485 mCbf(EVENT_STREAM_END, mUserData, NULL);
1486 mLock.lock();
1487 if (mState == STATE_STOPPING) {
1488 mState = STATE_STOPPED;
1489 if (status != DEAD_OBJECT) {
1490 return NS_INACTIVE;
1491 }
1492 }
1493 return 0;
1494 default:
1495 return 0;
1496 }
1497 }
1498
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001499 // perform callbacks while unlocked
1500 if (newUnderrun) {
1501 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1502 }
1503 // FIXME we will miss loops if loop cycle was signaled several times since last call
1504 // to processAudioBuffer()
1505 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1506 mCbf(EVENT_LOOP_END, mUserData, NULL);
1507 }
1508 if (flags & CBLK_BUFFER_END) {
1509 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1510 }
1511 if (markerReached) {
1512 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1513 }
1514 while (newPosCount > 0) {
1515 size_t temp = newPosition;
1516 mCbf(EVENT_NEW_POS, mUserData, &temp);
1517 newPosition += updatePeriod;
1518 newPosCount--;
1519 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001520
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001521 if (mObservedSequence != sequence) {
1522 mObservedSequence = sequence;
1523 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001524 // for offloaded tracks, just wait for the upper layers to recreate the track
1525 if (isOffloaded()) {
1526 return NS_INACTIVE;
1527 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528 }
1529
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001530 // if inactive, then don't run me again until re-started
1531 if (!active) {
1532 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001533 }
1534
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001535 // Compute the estimated time until the next timed event (position, markers, loops)
1536 // FIXME only for non-compressed audio
1537 uint32_t minFrames = ~0;
1538 if (!markerReached && position < markerPosition) {
1539 minFrames = markerPosition - position;
1540 }
1541 if (loopPeriod > 0 && loopPeriod < minFrames) {
1542 minFrames = loopPeriod;
1543 }
1544 if (updatePeriod > 0 && updatePeriod < minFrames) {
1545 minFrames = updatePeriod;
1546 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001548 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1549 static const uint32_t kPoll = 0;
1550 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1551 minFrames = kPoll * notificationFrames;
1552 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001553
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001554 // Convert frame units to time units
1555 nsecs_t ns = NS_WHENEVER;
1556 if (minFrames != (uint32_t) ~0) {
1557 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1558 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1559 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1560 }
1561
1562 // If not supplying data by EVENT_MORE_DATA, then we're done
1563 if (mTransfer != TRANSFER_CALLBACK) {
1564 return ns;
1565 }
1566
1567 struct timespec timeout;
1568 const struct timespec *requested = &ClientProxy::kForever;
1569 if (ns != NS_WHENEVER) {
1570 timeout.tv_sec = ns / 1000000000LL;
1571 timeout.tv_nsec = ns % 1000000000LL;
1572 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1573 requested = &timeout;
1574 }
1575
1576 while (mRemainingFrames > 0) {
1577
1578 Buffer audioBuffer;
1579 audioBuffer.frameCount = mRemainingFrames;
1580 size_t nonContig;
1581 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1582 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1583 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1584 requested = &ClientProxy::kNonBlocking;
1585 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001586 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1587 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001588 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001589 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1590 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001591 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001592 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001593 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1594 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001595 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001596
Eric Laurent42a6f422013-08-29 14:35:05 -07001597 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001598 mRetryOnPartialBuffer = false;
1599 if (avail < mRemainingFrames) {
1600 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1601 if (ns < 0 || myns < ns) {
1602 ns = myns;
1603 }
1604 return ns;
1605 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001606 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001607
1608 // Divide buffer size by 2 to take into account the expansion
1609 // due to 8 to 16 bit conversion: the callback must fill only half
1610 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001611 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001612 audioBuffer.size >>= 1;
1613 }
1614
1615 size_t reqSize = audioBuffer.size;
1616 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001617 size_t writtenSize = audioBuffer.size;
1618 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001619
1620 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001621 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1622 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1623 reqSize, (int) writtenSize);
1624 return NS_NEVER;
1625 }
1626
1627 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001628 // The callback is done filling buffers
1629 // Keep this thread going to handle timed events and
1630 // still try to get more data in intervals of WAIT_PERIOD_MS
1631 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001632 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001633 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001634
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001635 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001636 // 8 to 16 bit conversion, note that source and destination are the same address
1637 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001638 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001639 }
1640
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001641 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1642 audioBuffer.frameCount = releasedFrames;
1643 mRemainingFrames -= releasedFrames;
1644 if (misalignment >= releasedFrames) {
1645 misalignment -= releasedFrames;
1646 } else {
1647 misalignment = 0;
1648 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649
1650 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001651
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001652 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1653 // if callback doesn't like to accept the full chunk
1654 if (writtenSize < reqSize) {
1655 continue;
1656 }
1657
1658 // There could be enough non-contiguous frames available to satisfy the remaining request
1659 if (mRemainingFrames <= nonContig) {
1660 continue;
1661 }
1662
1663#if 0
1664 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1665 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1666 // that total to a sum == notificationFrames.
1667 if (0 < misalignment && misalignment <= mRemainingFrames) {
1668 mRemainingFrames = misalignment;
1669 return (mRemainingFrames * 1100000000LL) / sampleRate;
1670 }
1671#endif
1672
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001673 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001674 mRemainingFrames = notificationFrames;
1675 mRetryOnPartialBuffer = true;
1676
1677 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1678 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679}
1680
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001681status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001682{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001683 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001684 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001685 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001686 status_t result;
1687
Glenn Kastena47f3162012-11-07 10:13:08 -08001688 // refresh the audio configuration cache in this process to make sure we get new
1689 // output parameters in getOutput_l() and createTrack_l()
1690 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001691
Glenn Kasten23a75452014-01-13 10:37:17 -08001692 if (isOffloaded_l()) {
1693 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001694 return DEAD_OBJECT;
1695 }
1696
1697 // force new output query from audio policy manager;
1698 mOutput = 0;
1699 audio_io_handle_t output = getOutput_l();
1700
Glenn Kastena47f3162012-11-07 10:13:08 -08001701 // if the new IAudioTrack is created, createTrack_l() will modify the
1702 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1703 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001704
1705 // take the frames that will be lost by track recreation into account in saved position
1706 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001707 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001708 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001709 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001710 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001711 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001712 mFlags,
1713 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001714 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001715 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001716
Glenn Kastena47f3162012-11-07 10:13:08 -08001717 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001718 // continue playback from last known position, but
1719 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1720 if (mStaticProxy != NULL) {
1721 mLoopPeriod = 0;
1722 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1723 }
1724 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1725 // track destruction have been played? This is critical for SoundPool implementation
1726 // This must be broken, and needs to be tested/debugged.
1727#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001728 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001729 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001730 // Make sure that a client relying on callback events indicating underrun or
1731 // the actual amount of audio frames played (e.g SoundPool) receives them.
1732 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001733 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001734 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001735 }
1736 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001737#endif
1738 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001739 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001740 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001741 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001742 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001743 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1744 // As getOutput was called above and resulted in an output stream to be opened,
1745 // we need to release it.
1746 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001747 ALOGW("restoreTrack_l() failed status %d", result);
1748 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001749 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001750
1751 return result;
1752}
1753
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001754status_t AudioTrack::setParameters(const String8& keyValuePairs)
1755{
1756 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001757 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001758}
1759
Glenn Kastence703742013-07-19 16:33:58 -07001760status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1761{
Glenn Kasten53cec222013-08-29 09:01:02 -07001762 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001763 // FIXME not implemented for fast tracks; should use proxy and SSQ
1764 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1765 return INVALID_OPERATION;
1766 }
1767 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1768 return INVALID_OPERATION;
1769 }
1770 status_t status = mAudioTrack->getTimestamp(timestamp);
1771 if (status == NO_ERROR) {
1772 timestamp.mPosition += mProxy->getEpoch();
1773 }
1774 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001775}
1776
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001777String8 AudioTrack::getParameters(const String8& keys)
1778{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001779 audio_io_handle_t output = getOutput();
1780 if (output != 0) {
1781 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001782 } else {
1783 return String8::empty();
1784 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001785}
1786
Glenn Kasten23a75452014-01-13 10:37:17 -08001787bool AudioTrack::isOffloaded() const
1788{
1789 AutoMutex lock(mLock);
1790 return isOffloaded_l();
1791}
1792
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001793status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001794{
1795
1796 const size_t SIZE = 256;
1797 char buffer[SIZE];
1798 String8 result;
1799
1800 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001801 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1802 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001803 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001804 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001805 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001807 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001808 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001809 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001810 result.append(buffer);
1811 ::write(fd, result.string(), result.size());
1812 return NO_ERROR;
1813}
1814
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001815uint32_t AudioTrack::getUnderrunFrames() const
1816{
1817 AutoMutex lock(mLock);
1818 return mProxy->getUnderrunFrames();
1819}
1820
1821// =========================================================================
1822
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001823void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001824{
1825 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1826 if (audioTrack != 0) {
1827 AutoMutex lock(audioTrack->mLock);
1828 audioTrack->mProxy->binderDied();
1829 }
1830}
1831
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832// =========================================================================
1833
1834AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001835 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1836 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001837{
1838}
1839
1840AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841{
1842}
1843
1844bool AudioTrack::AudioTrackThread::threadLoop()
1845{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001846 {
1847 AutoMutex _l(mMyLock);
1848 if (mPaused) {
1849 mMyCond.wait(mMyLock);
1850 // caller will check for exitPending()
1851 return true;
1852 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001853 if (mIgnoreNextPausedInt) {
1854 mIgnoreNextPausedInt = false;
1855 mPausedInt = false;
1856 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001857 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001858 if (mPausedNs > 0) {
1859 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1860 } else {
1861 mMyCond.wait(mMyLock);
1862 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001863 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001864 return true;
1865 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001866 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001867 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001868 switch (ns) {
1869 case 0:
1870 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001871 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001872 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001873 return true;
1874 case NS_NEVER:
1875 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001876 case NS_WHENEVER:
1877 // FIXME increase poll interval, or make event-driven
1878 ns = 1000000000LL;
1879 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001880 default:
1881 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001882 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001883 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001884 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001885}
1886
Glenn Kasten3acbd052012-02-28 10:39:56 -08001887void AudioTrack::AudioTrackThread::requestExit()
1888{
1889 // must be in this order to avoid a race condition
1890 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001891 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001892}
1893
1894void AudioTrack::AudioTrackThread::pause()
1895{
1896 AutoMutex _l(mMyLock);
1897 mPaused = true;
1898}
1899
1900void AudioTrack::AudioTrackThread::resume()
1901{
1902 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001903 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001904 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001905 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001906 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001907 mMyCond.signal();
1908 }
1909}
1910
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001911void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1912{
1913 AutoMutex _l(mMyLock);
1914 mPausedInt = true;
1915 mPausedNs = ns;
1916}
1917
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001918}; // namespace android