blob: 2cb345920a14cdc6606395e5dd50cba5d3b9a25b [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 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800219 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800220 mTransfer = transferType;
221
Glenn Kastene33054e2012-11-14 12:54:39 -0800222 // FIXME "int" here is legacy and will be replaced by size_t later
223 if (frameCountInt < 0) {
224 ALOGE("Invalid frame count %d", frameCountInt);
225 return BAD_VALUE;
226 }
227 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700229 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
230 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231
Glenn Kastene33054e2012-11-14 12:54:39 -0800232 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700233
Eric Laurent1703cdf2011-03-07 14:52:59 -0800234 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800235
Glenn Kasten53cec222013-08-29 09:01:02 -0700236 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700237 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000238 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800239 return INVALID_OPERATION;
240 }
241
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100242 mOutput = 0;
243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 if (streamType == AUDIO_STREAM_DEFAULT) {
246 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800248 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
249 ALOGE("Invalid stream type %d", streamType);
250 return BAD_VALUE;
251 }
252 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700253
Glenn Kastenb1bef512014-01-13 10:25:53 -0800254 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800256 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
257 if (status != NO_ERROR) {
258 ALOGE("Could not get output sample rate for stream type %d; status %d",
259 streamType, status);
260 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700261 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800263 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800266 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269
270 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700271 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800272 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 return BAD_VALUE;
274 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800275 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700276
Glenn Kasten8ba90322013-10-30 11:29:27 -0700277 if (!audio_is_output_channel(channelMask)) {
278 ALOGE("Invalid channel mask %#x", channelMask);
279 return BAD_VALUE;
280 }
281
Glenn Kastene0fa4672012-04-24 14:35:14 -0700282 // AudioFlinger does not currently support 8-bit data in shared memory
283 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
284 ALOGE("8-bit data in shared memory is not supported");
285 return BAD_VALUE;
286 }
287
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100289 // or offload was requested
290 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
291 || !audio_is_linear_pcm(format)) {
292 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
293 ? "Offload request, forcing to Direct Output"
294 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700295 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800296 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700297 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700298 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700299 // only allow deep buffering for music stream type
300 if (streamType != AUDIO_STREAM_MUSIC) {
301 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
302 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303
Glenn Kastena42ff002012-11-14 12:47:55 -0800304 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700305 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800306 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307
Glenn Kastene3aa6592012-12-04 12:22:46 -0800308 if (audio_is_linear_pcm(format)) {
309 mFrameSize = channelCount * audio_bytes_per_sample(format);
310 mFrameSizeAF = channelCount * sizeof(int16_t);
311 } else {
312 mFrameSize = sizeof(uint8_t);
313 mFrameSizeAF = sizeof(uint8_t);
314 }
315
Dima Zavinfce7a472011-04-19 22:30:36 -0700316 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800317 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800318 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000319 flags,
320 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321
322 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000323 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324 return BAD_VALUE;
325 }
326
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800327 // Make copy of input parameter offloadInfo so that in the future:
328 // (a) createTrack_l doesn't need it as an input parameter
329 // (b) we can support re-creation of offloaded tracks
330 if (offloadInfo != NULL) {
331 mOffloadInfoCopy = *offloadInfo;
332 mOffloadInfo = &mOffloadInfoCopy;
333 } else {
334 mOffloadInfo = NULL;
335 }
336
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800337 mVolume[LEFT] = 1.0f;
338 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800339 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800340 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800341 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700342 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800343 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700344 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800345 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
346 mClientUid = IPCThreadState::self()->getCallingUid();
347 } else {
348 mClientUid = uid;
349 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700350 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700351 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700352 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700353
Glenn Kastena997e7a2012-08-07 09:44:19 -0700354 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700355 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700356 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
357 }
358
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800359 // create the IAudioTrack
Glenn Kastenb1bef512014-01-13 10:25:53 -0800360 status = createTrack_l(streamType,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800361 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800362 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800363 frameCount,
364 flags,
365 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800366 output,
367 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800368
Glenn Kastena997e7a2012-08-07 09:44:19 -0700369 if (status != NO_ERROR) {
370 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100371 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
372 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700373 mAudioTrackThread.clear();
374 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100375 //Use of direct and offloaded output streams is ref counted by audio policy manager.
376 // As getOutput was called above and resulted in an output stream to be opened,
377 // we need to release it.
378 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700379 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700380 }
381
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800383 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800385 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700387 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 mNewPosition = 0;
389 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700390 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800391 mSequence = 1;
392 mObservedSequence = mSequence;
393 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100394 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 return NO_ERROR;
397}
398
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399// -------------------------------------------------------------------------
400
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100401status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800403 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100404
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800405 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100406 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 }
408
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800409 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800411 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100412 if (previousState == STATE_PAUSED_STOPPING) {
413 mState = STATE_STOPPING;
414 } else {
415 mState = STATE_ACTIVE;
416 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800417 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
418 // reset current position as seen by client to 0
419 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700420 // force refresh of remaining frames by processAudioBuffer() as last
421 // write before stop could be partial.
422 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800423 }
424 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700425 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800426
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800428 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100429 if (previousState == STATE_STOPPING) {
430 mProxy->interrupt();
431 } else {
432 t->resume();
433 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800434 } else {
435 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
436 get_sched_policy(0, &mPreviousSchedulingGroup);
437 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
438 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800440 status_t status = NO_ERROR;
441 if (!(flags & CBLK_INVALID)) {
442 status = mAudioTrack->start();
443 if (status == DEAD_OBJECT) {
444 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800446 }
447 if (flags & CBLK_INVALID) {
448 status = restoreTrack_l("start");
449 }
450
451 if (status != NO_ERROR) {
452 ALOGE("start() status %d", status);
453 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100455 if (previousState != STATE_STOPPING) {
456 t->pause();
457 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700459 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700460 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 }
462 }
463
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100464 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800465}
466
467void AudioTrack::stop()
468{
469 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700470 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800471 return;
472 }
473
Glenn Kasten23a75452014-01-13 10:37:17 -0800474 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100475 mState = STATE_STOPPING;
476 } else {
477 mState = STATE_STOPPED;
478 }
479
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800480 mProxy->interrupt();
481 mAudioTrack->stop();
482 // the playback head position will reset to 0, so if a marker is set, we need
483 // to activate it again
484 mMarkerReached = false;
485#if 0
486 // Force flush if a shared buffer is used otherwise audioflinger
487 // will not stop before end of buffer is reached.
488 // It may be needed to make sure that we stop playback, likely in case looping is on.
489 if (mSharedBuffer != 0) {
490 flush_l();
491 }
492#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100493
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800494 sp<AudioTrackThread> t = mAudioTrackThread;
495 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800496 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100497 t->pause();
498 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800499 } else {
500 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
501 set_sched_policy(0, mPreviousSchedulingGroup);
502 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503}
504
505bool AudioTrack::stopped() const
506{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800507 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800508 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509}
510
511void AudioTrack::flush()
512{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800513 if (mSharedBuffer != 0) {
514 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800515 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800516 AutoMutex lock(mLock);
517 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
518 return;
519 }
520 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800521}
522
Eric Laurent1703cdf2011-03-07 14:52:59 -0800523void AudioTrack::flush_l()
524{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800525 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700526
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700527 // clear playback marker and periodic update counter
528 mMarkerPosition = 0;
529 mMarkerReached = false;
530 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100531 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700532
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800533 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800534 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100535 mProxy->interrupt();
536 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800537 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800538 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539}
540
541void AudioTrack::pause()
542{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800543 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100544 if (mState == STATE_ACTIVE) {
545 mState = STATE_PAUSED;
546 } else if (mState == STATE_STOPPING) {
547 mState = STATE_PAUSED_STOPPING;
548 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800549 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800551 mProxy->interrupt();
552 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553}
554
Eric Laurentbe916aa2010-06-01 23:49:17 -0700555status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800557 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700558 return BAD_VALUE;
559 }
560
Eric Laurent1703cdf2011-03-07 14:52:59 -0800561 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562 mVolume[LEFT] = left;
563 mVolume[RIGHT] = right;
564
Glenn Kastene3aa6592012-12-04 12:22:46 -0800565 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700566
Glenn Kasten23a75452014-01-13 10:37:17 -0800567 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700568 mAudioTrack->signal();
569 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700570 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571}
572
Glenn Kastenb1c09932012-02-27 16:21:04 -0800573status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800575 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700576}
577
Eric Laurent2beeb502010-07-16 07:43:46 -0700578status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700579{
Glenn Kasten05632a52012-01-03 14:22:33 -0800580 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700581 return BAD_VALUE;
582 }
583
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800584 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700585 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800586 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587
588 return NO_ERROR;
589}
590
Glenn Kastena5224f32012-01-04 12:41:44 -0800591void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700592{
593 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800594 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700595 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596}
597
Glenn Kasten3b16c762012-11-14 08:44:39 -0800598status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100600 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800601 return INVALID_OPERATION;
602 }
603
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800604 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800605 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700606 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 }
608 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700609 if (rate == 0 || rate > afSamplingRate*2 ) {
610 return BAD_VALUE;
611 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612
Eric Laurent1703cdf2011-03-07 14:52:59 -0800613 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800614 mSampleRate = rate;
615 mProxy->setSampleRate(rate);
616
Eric Laurent57326622009-07-07 07:10:45 -0700617 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618}
619
Glenn Kastena5224f32012-01-04 12:41:44 -0800620uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621{
John Grossman4ff14ba2012-02-08 16:37:41 -0800622 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800623 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800624 }
625
Eric Laurent1703cdf2011-03-07 14:52:59 -0800626 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700627
628 // sample rate can be updated during playback by the offloaded decoder so we need to
629 // query the HAL and update if needed.
630// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800631 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700632 if (mOutput != 0) {
633 uint32_t sampleRate = 0;
634 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
635 if (status == NO_ERROR) {
636 mSampleRate = sampleRate;
637 }
638 }
639 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800640 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641}
642
643status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
644{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100645 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800646 return INVALID_OPERATION;
647 }
648
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800650 ;
651 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
652 loopEnd - loopStart >= MIN_LOOP) {
653 ;
654 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 return BAD_VALUE;
656 }
657
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800658 AutoMutex lock(mLock);
659 // See setPosition() regarding setting parameters such as loop points or position while active
660 if (mState == STATE_ACTIVE) {
661 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700662 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800663 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664 return NO_ERROR;
665}
666
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800667void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
668{
669 // FIXME If setting a loop also sets position to start of loop, then
670 // this is correct. Otherwise it should be removed.
671 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
672 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
673 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
674}
675
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676status_t AudioTrack::setMarkerPosition(uint32_t marker)
677{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700678 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100679 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700680 return INVALID_OPERATION;
681 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800683 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700685 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686
687 return NO_ERROR;
688}
689
Glenn Kastena5224f32012-01-04 12:41:44 -0800690status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100692 if (isOffloaded()) {
693 return INVALID_OPERATION;
694 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700695 if (marker == NULL) {
696 return BAD_VALUE;
697 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800699 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 *marker = mMarkerPosition;
701
702 return NO_ERROR;
703}
704
705status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
706{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700707 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100708 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700709 return INVALID_OPERATION;
710 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800712 AutoMutex lock(mLock);
713 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800715 return NO_ERROR;
716}
717
Glenn Kastena5224f32012-01-04 12:41:44 -0800718status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100720 if (isOffloaded()) {
721 return INVALID_OPERATION;
722 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700723 if (updatePeriod == NULL) {
724 return BAD_VALUE;
725 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800727 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 *updatePeriod = mUpdatePeriod;
729
730 return NO_ERROR;
731}
732
733status_t AudioTrack::setPosition(uint32_t position)
734{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100735 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700736 return INVALID_OPERATION;
737 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800738 if (position > mFrameCount) {
739 return BAD_VALUE;
740 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800741
Eric Laurent1703cdf2011-03-07 14:52:59 -0800742 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800743 // Currently we require that the player is inactive before setting parameters such as position
744 // or loop points. Otherwise, there could be a race condition: the application could read the
745 // current position, compute a new position or loop parameters, and then set that position or
746 // loop parameters but it would do the "wrong" thing since the position has continued to advance
747 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
748 // to specify how it wants to handle such scenarios.
749 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700750 return INVALID_OPERATION;
751 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800752 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
753 mLoopPeriod = 0;
754 // FIXME Check whether loops and setting position are incompatible in old code.
755 // If we use setLoop for both purposes we lose the capability to set the position while looping.
756 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700757
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800758 return NO_ERROR;
759}
760
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800761status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700763 if (position == NULL) {
764 return BAD_VALUE;
765 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800766
Eric Laurent1703cdf2011-03-07 14:52:59 -0800767 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800768 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100769 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800770
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100771 if (mOutput != 0) {
772 uint32_t halFrames;
773 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
774 }
775 *position = dspFrames;
776 } else {
777 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
778 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
779 mProxy->getPosition();
780 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800781 return NO_ERROR;
782}
783
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800784status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800785{
786 if (mSharedBuffer == 0 || mIsTimed) {
787 return INVALID_OPERATION;
788 }
789 if (position == NULL) {
790 return BAD_VALUE;
791 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800792
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800793 AutoMutex lock(mLock);
794 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800795 return NO_ERROR;
796}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800797
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800798status_t AudioTrack::reload()
799{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100800 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800801 return INVALID_OPERATION;
802 }
803
Eric Laurent1703cdf2011-03-07 14:52:59 -0800804 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800805 // See setPosition() regarding setting parameters such as loop points or position while active
806 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700807 return INVALID_OPERATION;
808 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800809 mNewPosition = mUpdatePeriod;
810 mLoopPeriod = 0;
811 // FIXME The new code cannot reload while keeping a loop specified.
812 // Need to check how the old code handled this, and whether it's a significant change.
813 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800814 return NO_ERROR;
815}
816
Eric Laurentc2f1f072009-07-17 12:17:14 -0700817audio_io_handle_t AudioTrack::getOutput()
818{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800819 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100820 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800821}
822
823// must be called with mLock held
824audio_io_handle_t AudioTrack::getOutput_l()
825{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100826 if (mOutput) {
827 return mOutput;
828 } else {
829 return AudioSystem::getOutput(mStreamType,
830 mSampleRate, mFormat, mChannelMask, mFlags);
831 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700832}
833
Eric Laurentbe916aa2010-06-01 23:49:17 -0700834status_t AudioTrack::attachAuxEffect(int effectId)
835{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800836 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700837 status_t status = mAudioTrack->attachAuxEffect(effectId);
838 if (status == NO_ERROR) {
839 mAuxEffectId = effectId;
840 }
841 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700842}
843
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844// -------------------------------------------------------------------------
845
Eric Laurent1703cdf2011-03-07 14:52:59 -0800846// must be called with mLock held
847status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800848 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800849 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800850 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800851 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700852 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800853 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800854 audio_io_handle_t output,
855 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800856{
857 status_t status;
858 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
859 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700860 ALOGE("Could not get audioflinger");
861 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800862 }
863
Glenn Kastence8828a2013-09-16 18:07:38 -0700864 // Not all of these values are needed under all conditions, but it is easier to get them all
865
Eric Laurentd1b449a2010-05-14 03:26:45 -0700866 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700867 status = AudioSystem::getLatency(output, streamType, &afLatency);
868 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800869 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700870 return NO_INIT;
871 }
872
Glenn Kastence8828a2013-09-16 18:07:38 -0700873 size_t afFrameCount;
874 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
875 if (status != NO_ERROR) {
876 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
877 return NO_INIT;
878 }
879
880 uint32_t afSampleRate;
881 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
882 if (status != NO_ERROR) {
883 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
884 return NO_INIT;
885 }
886
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700887 // Client decides whether the track is TIMED (see below), but can only express a preference
888 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700889 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700890 // either of these use cases:
891 // use case 1: shared buffer
892 (sharedBuffer != 0) ||
893 // use case 2: callback handler
894 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800895 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700896 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700897 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700898 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700899 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700900 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700901
Glenn Kastence8828a2013-09-16 18:07:38 -0700902 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800903 // n = 1 fast track with single buffering; nBuffering is ignored
904 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700905 // n = 2 normal track, no sample rate conversion
906 // n = 3 normal track, with sample rate conversion
907 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
908 // n > 3 very high latency or very small notification interval; nBuffering is ignored
909 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
910
Eric Laurentd1b449a2010-05-14 03:26:45 -0700911 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912
Dima Zavinfce7a472011-04-19 22:30:36 -0700913 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914
Eric Laurentd1b449a2010-05-14 03:26:45 -0700915 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700917 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700918 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700919 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700920 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100921 if (mNotificationFramesAct != frameCount) {
922 mNotificationFramesAct = frameCount;
923 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700924 } else if (sharedBuffer != 0) {
925
Glenn Kastena42ff002012-11-14 12:47:55 -0800926 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700927 // 8-bit data in shared memory is not currently supported by AudioFlinger
928 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800929 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700930 // More than 2 channels does not require stronger alignment than stereo
931 alignment <<= 1;
932 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800933 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
934 ALOGE("Invalid buffer alignment: address %p, channel count %u",
935 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700936 return BAD_VALUE;
937 }
938
939 // When initializing a shared buffer AudioTrack via constructors,
940 // there's no frameCount parameter.
941 // But when initializing a shared buffer AudioTrack via set(),
942 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800943 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700944
945 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
946
947 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700948
Eric Laurentd1b449a2010-05-14 03:26:45 -0700949 // Ensure that buffer depth covers at least audio hardware latency
950 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700951 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
952 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700953 if (minBufCount <= nBuffering) {
954 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800955 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700956
Glenn Kastene33054e2012-11-14 12:54:39 -0800957 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
958 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800959 ", afLatency=%d",
960 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700961
962 if (frameCount == 0) {
963 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700964 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700965 // not ALOGW because it happens all the time when playing key clicks over A2DP
966 ALOGV("Minimum buffer size corrected from %d to %d",
967 frameCount, minFrameCount);
968 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800969 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700970 // Make sure that application is notified with sufficient margin before underrun
971 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
972 mNotificationFramesAct = frameCount/nBuffering;
973 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700974
Glenn Kastene0fa4672012-04-24 14:35:14 -0700975 } else {
976 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700977 }
978
Glenn Kastena075db42012-03-06 11:22:44 -0800979 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
980 if (mIsTimed) {
981 trackFlags |= IAudioFlinger::TRACK_TIMED;
982 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800983
984 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700985 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700986 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800987 if (mAudioTrackThread != 0) {
988 tid = mAudioTrackThread->getTid();
989 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700990 }
991
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100992 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
993 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
994 }
995
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800996 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800997 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700998 // AudioFlinger only sees 16-bit PCM
999 format == AUDIO_FORMAT_PCM_8_BIT ?
1000 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -08001001 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001002 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -08001003 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 sharedBuffer,
1005 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001006 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001007 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001008 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001009 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001010 &status);
1011
1012 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001013 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001014 return status;
1015 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001016 sp<IMemory> iMem = track->getCblk();
1017 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001018 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001019 return NO_INIT;
1020 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001021 void *iMemPointer = iMem->pointer();
1022 if (iMemPointer == NULL) {
1023 ALOGE("Could not get control block pointer");
1024 return NO_INIT;
1025 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001026 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001027 if (mAudioTrack != 0) {
1028 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1029 mDeathNotifier.clear();
1030 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001031 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001032 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001033 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001034 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001035 size_t temp = cblk->frameCount_;
1036 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1037 // In current design, AudioTrack client checks and ensures frame count validity before
1038 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1039 // for fast track as it uses a special method of assigning frame count.
1040 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1041 }
1042 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001043 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001044 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001045 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001046 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001047 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001048 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001049 // Theoretically double-buffering is not required for fast tracks,
1050 // due to tighter scheduling. But in practice, to accommodate kernels with
1051 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1052 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1053 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001054 }
1055 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001056 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001057 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001058 // once denied, do not request again if IAudioTrack is re-created
1059 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1060 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001061 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001062 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1063 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001064 }
1065 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001066 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001067 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001068 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1069 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1070 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1071 } else {
1072 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1073 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1074 mFlags = flags;
1075 return NO_INIT;
1076 }
1077 }
1078
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001079 mRefreshRemaining = true;
1080
1081 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1082 // is the value of pointer() for the shared buffer, otherwise buffers points
1083 // immediately after the control block. This address is for the mapping within client
1084 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1085 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001086 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001087 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001088 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001089 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001090 }
1091
Eric Laurent2beeb502010-07-16 07:43:46 -07001092 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001093 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001094 mLatency = afLatency + (1000*frameCount) / sampleRate;
1095 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001096 // If IAudioTrack is re-created, don't let the requested frameCount
1097 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001098 if (frameCount > mReqFrameCount) {
1099 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001100 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001101
1102 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001103 if (sharedBuffer == 0) {
1104 mStaticProxy.clear();
1105 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1106 } else {
1107 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1108 mProxy = mStaticProxy;
1109 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001110 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1111 uint16_t(mVolume[LEFT] * 0x1000));
1112 mProxy->setSendLevel(mSendLevel);
1113 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001114 mProxy->setEpoch(epoch);
1115 mProxy->setMinimum(mNotificationFramesAct);
1116
1117 mDeathNotifier = new DeathNotifier(this);
1118 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001119
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001120 return NO_ERROR;
1121}
1122
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001123status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1124{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001125 if (audioBuffer == NULL) {
1126 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001127 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001128 if (mTransfer != TRANSFER_OBTAIN) {
1129 audioBuffer->frameCount = 0;
1130 audioBuffer->size = 0;
1131 audioBuffer->raw = NULL;
1132 return INVALID_OPERATION;
1133 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001134
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001135 const struct timespec *requested;
1136 if (waitCount == -1) {
1137 requested = &ClientProxy::kForever;
1138 } else if (waitCount == 0) {
1139 requested = &ClientProxy::kNonBlocking;
1140 } else if (waitCount > 0) {
1141 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1142 struct timespec timeout;
1143 timeout.tv_sec = ms / 1000;
1144 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1145 requested = &timeout;
1146 } else {
1147 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1148 requested = NULL;
1149 }
1150 return obtainBuffer(audioBuffer, requested);
1151}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001152
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001153status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1154 struct timespec *elapsed, size_t *nonContig)
1155{
1156 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1157 uint32_t oldSequence = 0;
1158 uint32_t newSequence;
1159
1160 Proxy::Buffer buffer;
1161 status_t status = NO_ERROR;
1162
1163 static const int32_t kMaxTries = 5;
1164 int32_t tryCounter = kMaxTries;
1165
1166 do {
1167 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1168 // keep them from going away if another thread re-creates the track during obtainBuffer()
1169 sp<AudioTrackClientProxy> proxy;
1170 sp<IMemory> iMem;
1171
1172 { // start of lock scope
1173 AutoMutex lock(mLock);
1174
1175 newSequence = mSequence;
1176 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1177 if (status == DEAD_OBJECT) {
1178 // re-create track, unless someone else has already done so
1179 if (newSequence == oldSequence) {
1180 status = restoreTrack_l("obtainBuffer");
1181 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001182 buffer.mFrameCount = 0;
1183 buffer.mRaw = NULL;
1184 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001185 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001187 }
1188 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001189 oldSequence = newSequence;
1190
1191 // Keep the extra references
1192 proxy = mProxy;
1193 iMem = mCblkMemory;
1194
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001195 if (mState == STATE_STOPPING) {
1196 status = -EINTR;
1197 buffer.mFrameCount = 0;
1198 buffer.mRaw = NULL;
1199 buffer.mNonContig = 0;
1200 break;
1201 }
1202
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001203 // Non-blocking if track is stopped or paused
1204 if (mState != STATE_ACTIVE) {
1205 requested = &ClientProxy::kNonBlocking;
1206 }
1207
1208 } // end of lock scope
1209
1210 buffer.mFrameCount = audioBuffer->frameCount;
1211 // FIXME starts the requested timeout and elapsed over from scratch
1212 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1213
1214 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1215
1216 audioBuffer->frameCount = buffer.mFrameCount;
1217 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1218 audioBuffer->raw = buffer.mRaw;
1219 if (nonContig != NULL) {
1220 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001221 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001222 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001223}
1224
1225void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1226{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001227 if (mTransfer == TRANSFER_SHARED) {
1228 return;
1229 }
1230
1231 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1232 if (stepCount == 0) {
1233 return;
1234 }
1235
1236 Proxy::Buffer buffer;
1237 buffer.mFrameCount = stepCount;
1238 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001239
Eric Laurent1703cdf2011-03-07 14:52:59 -08001240 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001241 mInUnderrun = false;
1242 mProxy->releaseBuffer(&buffer);
1243
1244 // restart track if it was disabled by audioflinger due to previous underrun
1245 if (mState == STATE_ACTIVE) {
1246 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001247 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001248 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1249 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001250 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001251 mAudioTrack->start();
1252 }
1253 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254}
1255
1256// -------------------------------------------------------------------------
1257
1258ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1259{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001260 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001261 return INVALID_OPERATION;
1262 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001264 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001265 // Sanity-check: user is most-likely passing an error code, and it would
1266 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 return BAD_VALUE;
1269 }
1270
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001271 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 Buffer audioBuffer;
1273
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001274 while (userSize >= mFrameSize) {
1275 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001276
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001277 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001279 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001281 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001282 return ssize_t(err);
1283 }
1284
1285 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001286 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001288 toWrite = audioBuffer.size >> 1;
1289 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001290 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001292 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001294 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 userSize -= toWrite;
1296 written += toWrite;
1297
1298 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001299 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300
1301 return written;
1302}
1303
1304// -------------------------------------------------------------------------
1305
John Grossman4ff14ba2012-02-08 16:37:41 -08001306TimedAudioTrack::TimedAudioTrack() {
1307 mIsTimed = true;
1308}
1309
1310status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1311{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001312 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001313 status_t result = UNKNOWN_ERROR;
1314
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001315#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001316 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1317 // while we are accessing the cblk
1318 sp<IAudioTrack> audioTrack = mAudioTrack;
1319 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001320#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001321
John Grossman4ff14ba2012-02-08 16:37:41 -08001322 // If the track is not invalid already, try to allocate a buffer. alloc
1323 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001324 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001325 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001326 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001327 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1328 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001329 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001330 }
1331 }
1332
1333 // If the track is invalid at this point, attempt to restore it. and try the
1334 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001335 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001336 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001337
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001338 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001339 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001340 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001341 }
1342
1343 return result;
1344}
1345
1346status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1347 int64_t pts)
1348{
Eric Laurentdf839842012-05-31 14:27:14 -07001349 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1350 {
1351 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001352 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001353 // restart track if it was disabled by audioflinger due to previous underrun
1354 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001355 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1356 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001357 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001358 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001359 mAudioTrack->start();
1360 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001361 }
Eric Laurentdf839842012-05-31 14:27:14 -07001362 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001363}
1364
1365status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1366 TargetTimeline target)
1367{
1368 return mAudioTrack->setMediaTimeTransform(xform, target);
1369}
1370
1371// -------------------------------------------------------------------------
1372
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001373nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001374{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001375 // Currently the AudioTrack thread is not created if there are no callbacks.
1376 // Would it ever make sense to run the thread, even without callbacks?
1377 // If so, then replace this by checks at each use for mCbf != NULL.
1378 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1379
Eric Laurent1703cdf2011-03-07 14:52:59 -08001380 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001381 if (mAwaitBoost) {
1382 mAwaitBoost = false;
1383 mLock.unlock();
1384 static const int32_t kMaxTries = 5;
1385 int32_t tryCounter = kMaxTries;
1386 uint32_t pollUs = 10000;
1387 do {
1388 int policy = sched_getscheduler(0);
1389 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1390 break;
1391 }
1392 usleep(pollUs);
1393 pollUs <<= 1;
1394 } while (tryCounter-- > 0);
1395 if (tryCounter < 0) {
1396 ALOGE("did not receive expected priority boost on time");
1397 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001398 // Run again immediately
1399 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001400 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001401
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001402 // Can only reference mCblk while locked
1403 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001404 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001405
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001406 // Check for track invalidation
1407 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001408 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1409 // AudioSystem cache. We should not exit here but after calling the callback so
1410 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001411 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001412 status_t status = restoreTrack_l("processAudioBuffer");
1413 mLock.unlock();
1414 // Run again immediately, but with a new IAudioTrack
1415 return 0;
1416 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001417 }
1418
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001419 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001420 bool active = mState == STATE_ACTIVE;
1421
1422 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1423 bool newUnderrun = false;
1424 if (flags & CBLK_UNDERRUN) {
1425#if 0
1426 // Currently in shared buffer mode, when the server reaches the end of buffer,
1427 // the track stays active in continuous underrun state. It's up to the application
1428 // to pause or stop the track, or set the position to a new offset within buffer.
1429 // This was some experimental code to auto-pause on underrun. Keeping it here
1430 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1431 if (mTransfer == TRANSFER_SHARED) {
1432 mState = STATE_PAUSED;
1433 active = false;
1434 }
1435#endif
1436 if (!mInUnderrun) {
1437 mInUnderrun = true;
1438 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001439 }
1440 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001441
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001442 // Get current position of server
1443 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444
1445 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001446 bool markerReached = false;
1447 size_t markerPosition = mMarkerPosition;
1448 // FIXME fails for wraparound, need 64 bits
1449 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1450 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001451 }
1452
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001453 // Determine number of new position callback(s) that will be needed, while locked
1454 size_t newPosCount = 0;
1455 size_t newPosition = mNewPosition;
1456 size_t updatePeriod = mUpdatePeriod;
1457 // FIXME fails for wraparound, need 64 bits
1458 if (updatePeriod > 0 && position >= newPosition) {
1459 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1460 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461 }
1462
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001463 // Cache other fields that will be needed soon
1464 uint32_t loopPeriod = mLoopPeriod;
1465 uint32_t sampleRate = mSampleRate;
1466 size_t notificationFrames = mNotificationFramesAct;
1467 if (mRefreshRemaining) {
1468 mRefreshRemaining = false;
1469 mRemainingFrames = notificationFrames;
1470 mRetryOnPartialBuffer = false;
1471 }
1472 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001473 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001474
1475 // These fields don't need to be cached, because they are assigned only by set():
1476 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1477 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1478
1479 mLock.unlock();
1480
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001481 if (waitStreamEnd) {
1482 AutoMutex lock(mLock);
1483
1484 sp<AudioTrackClientProxy> proxy = mProxy;
1485 sp<IMemory> iMem = mCblkMemory;
1486
1487 struct timespec timeout;
1488 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1489 timeout.tv_nsec = 0;
1490
1491 mLock.unlock();
1492 status_t status = mProxy->waitStreamEndDone(&timeout);
1493 mLock.lock();
1494 switch (status) {
1495 case NO_ERROR:
1496 case DEAD_OBJECT:
1497 case TIMED_OUT:
1498 mLock.unlock();
1499 mCbf(EVENT_STREAM_END, mUserData, NULL);
1500 mLock.lock();
1501 if (mState == STATE_STOPPING) {
1502 mState = STATE_STOPPED;
1503 if (status != DEAD_OBJECT) {
1504 return NS_INACTIVE;
1505 }
1506 }
1507 return 0;
1508 default:
1509 return 0;
1510 }
1511 }
1512
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001513 // perform callbacks while unlocked
1514 if (newUnderrun) {
1515 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1516 }
1517 // FIXME we will miss loops if loop cycle was signaled several times since last call
1518 // to processAudioBuffer()
1519 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1520 mCbf(EVENT_LOOP_END, mUserData, NULL);
1521 }
1522 if (flags & CBLK_BUFFER_END) {
1523 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1524 }
1525 if (markerReached) {
1526 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1527 }
1528 while (newPosCount > 0) {
1529 size_t temp = newPosition;
1530 mCbf(EVENT_NEW_POS, mUserData, &temp);
1531 newPosition += updatePeriod;
1532 newPosCount--;
1533 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001534
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001535 if (mObservedSequence != sequence) {
1536 mObservedSequence = sequence;
1537 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001538 // for offloaded tracks, just wait for the upper layers to recreate the track
1539 if (isOffloaded()) {
1540 return NS_INACTIVE;
1541 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542 }
1543
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001544 // if inactive, then don't run me again until re-started
1545 if (!active) {
1546 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001547 }
1548
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001549 // Compute the estimated time until the next timed event (position, markers, loops)
1550 // FIXME only for non-compressed audio
1551 uint32_t minFrames = ~0;
1552 if (!markerReached && position < markerPosition) {
1553 minFrames = markerPosition - position;
1554 }
1555 if (loopPeriod > 0 && loopPeriod < minFrames) {
1556 minFrames = loopPeriod;
1557 }
1558 if (updatePeriod > 0 && updatePeriod < minFrames) {
1559 minFrames = updatePeriod;
1560 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001562 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1563 static const uint32_t kPoll = 0;
1564 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1565 minFrames = kPoll * notificationFrames;
1566 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001567
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001568 // Convert frame units to time units
1569 nsecs_t ns = NS_WHENEVER;
1570 if (minFrames != (uint32_t) ~0) {
1571 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1572 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1573 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1574 }
1575
1576 // If not supplying data by EVENT_MORE_DATA, then we're done
1577 if (mTransfer != TRANSFER_CALLBACK) {
1578 return ns;
1579 }
1580
1581 struct timespec timeout;
1582 const struct timespec *requested = &ClientProxy::kForever;
1583 if (ns != NS_WHENEVER) {
1584 timeout.tv_sec = ns / 1000000000LL;
1585 timeout.tv_nsec = ns % 1000000000LL;
1586 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1587 requested = &timeout;
1588 }
1589
1590 while (mRemainingFrames > 0) {
1591
1592 Buffer audioBuffer;
1593 audioBuffer.frameCount = mRemainingFrames;
1594 size_t nonContig;
1595 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1596 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1597 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1598 requested = &ClientProxy::kNonBlocking;
1599 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001600 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1601 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001602 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001603 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1604 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001605 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001607 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1608 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001609 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001610
Eric Laurent42a6f422013-08-29 14:35:05 -07001611 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001612 mRetryOnPartialBuffer = false;
1613 if (avail < mRemainingFrames) {
1614 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1615 if (ns < 0 || myns < ns) {
1616 ns = myns;
1617 }
1618 return ns;
1619 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001620 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621
1622 // Divide buffer size by 2 to take into account the expansion
1623 // due to 8 to 16 bit conversion: the callback must fill only half
1624 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001625 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001626 audioBuffer.size >>= 1;
1627 }
1628
1629 size_t reqSize = audioBuffer.size;
1630 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001631 size_t writtenSize = audioBuffer.size;
1632 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001633
1634 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001635 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1636 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1637 reqSize, (int) writtenSize);
1638 return NS_NEVER;
1639 }
1640
1641 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001642 // The callback is done filling buffers
1643 // Keep this thread going to handle timed events and
1644 // still try to get more data in intervals of WAIT_PERIOD_MS
1645 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001646 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001647 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001649 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001650 // 8 to 16 bit conversion, note that source and destination are the same address
1651 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001652 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653 }
1654
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001655 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1656 audioBuffer.frameCount = releasedFrames;
1657 mRemainingFrames -= releasedFrames;
1658 if (misalignment >= releasedFrames) {
1659 misalignment -= releasedFrames;
1660 } else {
1661 misalignment = 0;
1662 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001663
1664 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001665
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001666 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1667 // if callback doesn't like to accept the full chunk
1668 if (writtenSize < reqSize) {
1669 continue;
1670 }
1671
1672 // There could be enough non-contiguous frames available to satisfy the remaining request
1673 if (mRemainingFrames <= nonContig) {
1674 continue;
1675 }
1676
1677#if 0
1678 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1679 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1680 // that total to a sum == notificationFrames.
1681 if (0 < misalignment && misalignment <= mRemainingFrames) {
1682 mRemainingFrames = misalignment;
1683 return (mRemainingFrames * 1100000000LL) / sampleRate;
1684 }
1685#endif
1686
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001687 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001688 mRemainingFrames = notificationFrames;
1689 mRetryOnPartialBuffer = true;
1690
1691 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1692 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001693}
1694
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001695status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001696{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001697 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001698 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001699 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001700 status_t result;
1701
Glenn Kastena47f3162012-11-07 10:13:08 -08001702 // refresh the audio configuration cache in this process to make sure we get new
1703 // output parameters in getOutput_l() and createTrack_l()
1704 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001705
Glenn Kasten23a75452014-01-13 10:37:17 -08001706 if (isOffloaded_l()) {
1707 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001708 return DEAD_OBJECT;
1709 }
1710
1711 // force new output query from audio policy manager;
1712 mOutput = 0;
1713 audio_io_handle_t output = getOutput_l();
1714
Glenn Kastena47f3162012-11-07 10:13:08 -08001715 // if the new IAudioTrack is created, createTrack_l() will modify the
1716 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1717 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001718
1719 // take the frames that will be lost by track recreation into account in saved position
1720 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001721 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001722 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001723 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001724 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001725 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001726 mFlags,
1727 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001728 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001729 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001730
Glenn Kastena47f3162012-11-07 10:13:08 -08001731 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001732 // continue playback from last known position, but
1733 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1734 if (mStaticProxy != NULL) {
1735 mLoopPeriod = 0;
1736 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1737 }
1738 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1739 // track destruction have been played? This is critical for SoundPool implementation
1740 // This must be broken, and needs to be tested/debugged.
1741#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001742 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001743 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001744 // Make sure that a client relying on callback events indicating underrun or
1745 // the actual amount of audio frames played (e.g SoundPool) receives them.
1746 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001747 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001748 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001749 }
1750 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001751#endif
1752 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001753 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001754 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001755 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001756 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001757 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1758 // As getOutput was called above and resulted in an output stream to be opened,
1759 // we need to release it.
1760 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001761 ALOGW("restoreTrack_l() failed status %d", result);
1762 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001763 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001764
1765 return result;
1766}
1767
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001768status_t AudioTrack::setParameters(const String8& keyValuePairs)
1769{
1770 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001771 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001772}
1773
Glenn Kastence703742013-07-19 16:33:58 -07001774status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1775{
Glenn Kasten53cec222013-08-29 09:01:02 -07001776 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001777 // FIXME not implemented for fast tracks; should use proxy and SSQ
1778 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1779 return INVALID_OPERATION;
1780 }
1781 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1782 return INVALID_OPERATION;
1783 }
1784 status_t status = mAudioTrack->getTimestamp(timestamp);
1785 if (status == NO_ERROR) {
1786 timestamp.mPosition += mProxy->getEpoch();
1787 }
1788 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001789}
1790
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001791String8 AudioTrack::getParameters(const String8& keys)
1792{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001793 audio_io_handle_t output = getOutput();
1794 if (output != 0) {
1795 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001796 } else {
1797 return String8::empty();
1798 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001799}
1800
Glenn Kasten23a75452014-01-13 10:37:17 -08001801bool AudioTrack::isOffloaded() const
1802{
1803 AutoMutex lock(mLock);
1804 return isOffloaded_l();
1805}
1806
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001807status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001808{
1809
1810 const size_t SIZE = 256;
1811 char buffer[SIZE];
1812 String8 result;
1813
1814 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001815 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1816 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001817 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001818 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001819 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001820 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001821 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001823 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824 result.append(buffer);
1825 ::write(fd, result.string(), result.size());
1826 return NO_ERROR;
1827}
1828
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001829uint32_t AudioTrack::getUnderrunFrames() const
1830{
1831 AutoMutex lock(mLock);
1832 return mProxy->getUnderrunFrames();
1833}
1834
1835// =========================================================================
1836
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001837void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001838{
1839 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1840 if (audioTrack != 0) {
1841 AutoMutex lock(audioTrack->mLock);
1842 audioTrack->mProxy->binderDied();
1843 }
1844}
1845
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001846// =========================================================================
1847
1848AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001849 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1850 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001851{
1852}
1853
1854AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001855{
1856}
1857
1858bool AudioTrack::AudioTrackThread::threadLoop()
1859{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001860 {
1861 AutoMutex _l(mMyLock);
1862 if (mPaused) {
1863 mMyCond.wait(mMyLock);
1864 // caller will check for exitPending()
1865 return true;
1866 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001867 if (mIgnoreNextPausedInt) {
1868 mIgnoreNextPausedInt = false;
1869 mPausedInt = false;
1870 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001871 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001872 if (mPausedNs > 0) {
1873 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1874 } else {
1875 mMyCond.wait(mMyLock);
1876 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001877 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001878 return true;
1879 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001880 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001881 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001882 switch (ns) {
1883 case 0:
1884 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001885 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001886 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001887 return true;
1888 case NS_NEVER:
1889 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001890 case NS_WHENEVER:
1891 // FIXME increase poll interval, or make event-driven
1892 ns = 1000000000LL;
1893 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001894 default:
1895 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001896 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001897 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001898 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001899}
1900
Glenn Kasten3acbd052012-02-28 10:39:56 -08001901void AudioTrack::AudioTrackThread::requestExit()
1902{
1903 // must be in this order to avoid a race condition
1904 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001905 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001906}
1907
1908void AudioTrack::AudioTrackThread::pause()
1909{
1910 AutoMutex _l(mMyLock);
1911 mPaused = true;
1912}
1913
1914void AudioTrack::AudioTrackThread::resume()
1915{
1916 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001917 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001918 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001919 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001920 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001921 mMyCond.signal();
1922 }
1923}
1924
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001925void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1926{
1927 AutoMutex _l(mMyLock);
1928 mPausedInt = true;
1929 mPausedNs = ns;
1930}
1931
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001932}; // namespace android