blob: f85b0ce995e1c4ea3408f0e6fe202d6335d3abd5 [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) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080057 ALOGE("Unable to query output sample rate for stream type %d; status %d",
58 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080059 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080060 }
Glenn Kastene33054e2012-11-14 12:54:39 -080061 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080062 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
63 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080064 ALOGE("Unable to query output frame count for stream type %d; status %d",
65 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080066 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080067 }
68 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080069 status = AudioSystem::getOutputLatency(&afLatency, streamType);
70 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080071 ALOGE("Unable to query output latency for stream type %d; status %d",
72 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080073 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080074 }
75
76 // Ensure that buffer depth covers at least audio hardware latency
77 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080078 if (minBufCount < 2) {
79 minBufCount = 2;
80 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080081
82 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070083 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080084 // The formula above should always produce a non-zero value, but return an error
85 // in the unlikely event that it does not, as that's part of the API contract.
86 if (*frameCount == 0) {
87 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d",
88 streamType, sampleRate);
89 return BAD_VALUE;
90 }
Glenn Kasten3acbd052012-02-28 10:39:56 -080091 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
92 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080093 return NO_ERROR;
94}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095
96// ---------------------------------------------------------------------------
97
98AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070099 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800100 mIsTimed(false),
101 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800102 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103{
104}
105
106AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800107 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800109 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700110 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800111 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700112 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113 callback_t cbf,
114 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800115 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800116 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000117 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800118 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800119 int uid,
120 pid_t pid)
Glenn Kasten87913512011-06-22 16:15:25 -0700121 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800122 mIsTimed(false),
123 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800124 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700126 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700127 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800128 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
Marco Nelissend457c972014-02-11 08:47:07 -0800129 offloadInfo, uid, pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130}
131
Andreas Huberc8139852012-01-18 10:51:55 -0800132AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800133 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800135 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700136 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700138 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 callback_t cbf,
140 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800141 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800142 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000143 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800144 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800145 int uid,
146 pid_t pid)
Glenn Kasten87913512011-06-22 16:15:25 -0700147 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800148 mIsTimed(false),
149 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800150 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700152 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800153 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissend457c972014-02-11 08:47:07 -0800154 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo,
155 uid, pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156}
157
158AudioTrack::~AudioTrack()
159{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 if (mStatus == NO_ERROR) {
161 // Make sure that callback function exits in the case where
162 // it is looping on buffer full condition in obtainBuffer().
163 // Otherwise the callback thread will never exit.
164 stop();
165 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100166 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800167 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 mAudioTrackThread->requestExitAndWait();
169 mAudioTrackThread.clear();
170 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700171 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
172 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173 IPCThreadState::self()->flushCommands();
Marco Nelissend457c972014-02-11 08:47:07 -0800174 ALOGV("~AudioTrack, releasing session id from %d on behalf of %d",
175 IPCThreadState::self()->getCallingPid(), mClientPid);
176 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 }
178}
179
180status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800181 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800183 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700184 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800185 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700186 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 callback_t cbf,
188 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800189 uint32_t notificationFrames,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700191 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800192 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000193 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800194 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800195 int uid,
196 pid_t pid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197{
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800198 ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
Glenn Kasten838b3d82014-02-27 15:30:41 -0800199 "flags #%x, notificationFrames %u, sessionId %d, transferType %d",
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800200 streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames,
Glenn Kasten86f04662014-02-24 15:13:05 -0800201 sessionId, transferType);
202
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800203 switch (transferType) {
204 case TRANSFER_DEFAULT:
205 if (sharedBuffer != 0) {
206 transferType = TRANSFER_SHARED;
207 } else if (cbf == NULL || threadCanCallJava) {
208 transferType = TRANSFER_SYNC;
209 } else {
210 transferType = TRANSFER_CALLBACK;
211 }
212 break;
213 case TRANSFER_CALLBACK:
214 if (cbf == NULL || sharedBuffer != 0) {
215 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
216 return BAD_VALUE;
217 }
218 break;
219 case TRANSFER_OBTAIN:
220 case TRANSFER_SYNC:
221 if (sharedBuffer != 0) {
222 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
223 return BAD_VALUE;
224 }
225 break;
226 case TRANSFER_SHARED:
227 if (sharedBuffer == 0) {
228 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
229 return BAD_VALUE;
230 }
231 break;
232 default:
233 ALOGE("Invalid transfer type %d", transferType);
234 return BAD_VALUE;
235 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800236 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800237 mTransfer = transferType;
238
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700239 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
240 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241
Glenn Kastene33054e2012-11-14 12:54:39 -0800242 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700243
Eric Laurent1703cdf2011-03-07 14:52:59 -0800244 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800245
Glenn Kasten53cec222013-08-29 09:01:02 -0700246 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700247 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000248 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 return INVALID_OPERATION;
250 }
251
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700253 if (streamType == AUDIO_STREAM_DEFAULT) {
254 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800256 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
257 ALOGE("Invalid stream type %d", streamType);
258 return BAD_VALUE;
259 }
260 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700261
Glenn Kastenb1bef512014-01-13 10:25:53 -0800262 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800264 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
265 if (status != NO_ERROR) {
266 ALOGE("Could not get output sample rate for stream type %d; status %d",
267 streamType, status);
268 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700269 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800271 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700272
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800274 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277
278 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700279 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800280 ALOGE("Invalid format %#x", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281 return BAD_VALUE;
282 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800283 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700284
Glenn Kasten8ba90322013-10-30 11:29:27 -0700285 if (!audio_is_output_channel(channelMask)) {
286 ALOGE("Invalid channel mask %#x", channelMask);
287 return BAD_VALUE;
288 }
Glenn Kastene3247bf2014-02-24 15:19:07 -0800289 mChannelMask = channelMask;
290 uint32_t channelCount = popcount(channelMask);
291 mChannelCount = channelCount;
Glenn Kasten8ba90322013-10-30 11:29:27 -0700292
Glenn Kastene0fa4672012-04-24 14:35:14 -0700293 // AudioFlinger does not currently support 8-bit data in shared memory
294 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
295 ALOGE("8-bit data in shared memory is not supported");
296 return BAD_VALUE;
297 }
298
Eric Laurentc2f1f072009-07-17 12:17:14 -0700299 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100300 // or offload was requested
301 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
302 || !audio_is_linear_pcm(format)) {
303 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
304 ? "Offload request, forcing to Direct Output"
305 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700306 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800307 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700308 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700310 // only allow deep buffering for music stream type
311 if (streamType != AUDIO_STREAM_MUSIC) {
312 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
313 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314
Glenn Kastene3aa6592012-12-04 12:22:46 -0800315 if (audio_is_linear_pcm(format)) {
316 mFrameSize = channelCount * audio_bytes_per_sample(format);
317 mFrameSizeAF = channelCount * sizeof(int16_t);
318 } else {
319 mFrameSize = sizeof(uint8_t);
320 mFrameSizeAF = sizeof(uint8_t);
321 }
322
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800323 // Make copy of input parameter offloadInfo so that in the future:
324 // (a) createTrack_l doesn't need it as an input parameter
325 // (b) we can support re-creation of offloaded tracks
326 if (offloadInfo != NULL) {
327 mOffloadInfoCopy = *offloadInfo;
328 mOffloadInfo = &mOffloadInfoCopy;
329 } else {
330 mOffloadInfo = NULL;
331 }
332
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800333 mVolume[LEFT] = 1.0f;
334 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800335 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800336 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800337 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700338 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800339 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700340 mSessionId = sessionId;
Marco Nelissend457c972014-02-11 08:47:07 -0800341 int callingpid = IPCThreadState::self()->getCallingPid();
342 int mypid = getpid();
343 if (uid == -1 || (callingpid != mypid)) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800344 mClientUid = IPCThreadState::self()->getCallingUid();
345 } else {
346 mClientUid = uid;
347 }
Marco Nelissend457c972014-02-11 08:47:07 -0800348 if (pid == -1 || (callingpid != mypid)) {
349 mClientPid = callingpid;
350 } else {
351 mClientPid = pid;
352 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700353 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700354 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700355 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700356
Glenn Kastena997e7a2012-08-07 09:44:19 -0700357 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700358 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700359 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
360 }
361
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800362 // create the IAudioTrack
Glenn Kasten363fb752014-01-15 12:27:31 -0800363 status = createTrack_l(0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800364
Glenn Kastena997e7a2012-08-07 09:44:19 -0700365 if (status != NO_ERROR) {
366 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100367 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
368 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700369 mAudioTrackThread.clear();
370 }
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800371 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800372#if 0 // FIXME This should no longer be needed
373 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100374 // As getOutput was called above and resulted in an output stream to be opened,
375 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800376 if (mOutput != 0) {
377 AudioSystem::releaseOutput(mOutput);
378 mOutput = 0;
379 }
380#endif
Glenn Kastena997e7a2012-08-07 09:44:19 -0700381 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700382 }
383
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800385 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800387 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700389 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 mNewPosition = 0;
391 mUpdatePeriod = 0;
Marco Nelissend457c972014-02-11 08:47:07 -0800392 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800393 mSequence = 1;
394 mObservedSequence = mSequence;
395 mInUnderrun = false;
396
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 return NO_ERROR;
398}
399
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400// -------------------------------------------------------------------------
401
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100402status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800403{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800404 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100405
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800406 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100407 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 }
409
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800410 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800412 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100413 if (previousState == STATE_PAUSED_STOPPING) {
414 mState = STATE_STOPPING;
415 } else {
416 mState = STATE_ACTIVE;
417 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800418 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
419 // reset current position as seen by client to 0
420 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700421 // force refresh of remaining frames by processAudioBuffer() as last
422 // write before stop could be partial.
423 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800424 }
425 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700426 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800427
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800429 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100430 if (previousState == STATE_STOPPING) {
431 mProxy->interrupt();
432 } else {
433 t->resume();
434 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800435 } else {
436 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
437 get_sched_policy(0, &mPreviousSchedulingGroup);
438 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
439 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800441 status_t status = NO_ERROR;
442 if (!(flags & CBLK_INVALID)) {
443 status = mAudioTrack->start();
444 if (status == DEAD_OBJECT) {
445 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800447 }
448 if (flags & CBLK_INVALID) {
449 status = restoreTrack_l("start");
450 }
451
452 if (status != NO_ERROR) {
453 ALOGE("start() status %d", status);
454 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100456 if (previousState != STATE_STOPPING) {
457 t->pause();
458 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700460 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700461 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462 }
463 }
464
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100465 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800466}
467
468void AudioTrack::stop()
469{
470 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700471 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800472 return;
473 }
474
Glenn Kasten23a75452014-01-13 10:37:17 -0800475 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100476 mState = STATE_STOPPING;
477 } else {
478 mState = STATE_STOPPED;
479 }
480
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800481 mProxy->interrupt();
482 mAudioTrack->stop();
483 // the playback head position will reset to 0, so if a marker is set, we need
484 // to activate it again
485 mMarkerReached = false;
486#if 0
487 // Force flush if a shared buffer is used otherwise audioflinger
488 // will not stop before end of buffer is reached.
489 // It may be needed to make sure that we stop playback, likely in case looping is on.
490 if (mSharedBuffer != 0) {
491 flush_l();
492 }
493#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100494
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800495 sp<AudioTrackThread> t = mAudioTrackThread;
496 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800497 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100498 t->pause();
499 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800500 } else {
501 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
502 set_sched_policy(0, mPreviousSchedulingGroup);
503 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504}
505
506bool AudioTrack::stopped() const
507{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800508 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800509 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510}
511
512void AudioTrack::flush()
513{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800514 if (mSharedBuffer != 0) {
515 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800516 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800517 AutoMutex lock(mLock);
518 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
519 return;
520 }
521 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800522}
523
Eric Laurent1703cdf2011-03-07 14:52:59 -0800524void AudioTrack::flush_l()
525{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800526 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700527
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700528 // clear playback marker and periodic update counter
529 mMarkerPosition = 0;
530 mMarkerReached = false;
531 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100532 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700533
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800534 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800535 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100536 mProxy->interrupt();
537 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800538 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800539 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540}
541
542void AudioTrack::pause()
543{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800544 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100545 if (mState == STATE_ACTIVE) {
546 mState = STATE_PAUSED;
547 } else if (mState == STATE_STOPPING) {
548 mState = STATE_PAUSED_STOPPING;
549 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800550 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800552 mProxy->interrupt();
553 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554}
555
Eric Laurentbe916aa2010-06-01 23:49:17 -0700556status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800558 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700559 return BAD_VALUE;
560 }
561
Eric Laurent1703cdf2011-03-07 14:52:59 -0800562 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 mVolume[LEFT] = left;
564 mVolume[RIGHT] = right;
565
Glenn Kastene3aa6592012-12-04 12:22:46 -0800566 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700567
Glenn Kasten23a75452014-01-13 10:37:17 -0800568 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700569 mAudioTrack->signal();
570 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572}
573
Glenn Kastenb1c09932012-02-27 16:21:04 -0800574status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800576 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700577}
578
Eric Laurent2beeb502010-07-16 07:43:46 -0700579status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700580{
Glenn Kasten05632a52012-01-03 14:22:33 -0800581 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700582 return BAD_VALUE;
583 }
584
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800585 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700586 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800587 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700588
589 return NO_ERROR;
590}
591
Glenn Kastena5224f32012-01-04 12:41:44 -0800592void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700593{
594 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800595 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700596 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597}
598
Glenn Kasten3b16c762012-11-14 08:44:39 -0800599status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100601 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800602 return INVALID_OPERATION;
603 }
604
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800605 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700607 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608 }
609 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700610 if (rate == 0 || rate > afSamplingRate*2 ) {
611 return BAD_VALUE;
612 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613
Eric Laurent1703cdf2011-03-07 14:52:59 -0800614 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800615 mSampleRate = rate;
616 mProxy->setSampleRate(rate);
617
Eric Laurent57326622009-07-07 07:10:45 -0700618 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619}
620
Glenn Kastena5224f32012-01-04 12:41:44 -0800621uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622{
John Grossman4ff14ba2012-02-08 16:37:41 -0800623 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800624 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800625 }
626
Eric Laurent1703cdf2011-03-07 14:52:59 -0800627 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700628
629 // sample rate can be updated during playback by the offloaded decoder so we need to
630 // query the HAL and update if needed.
631// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800632 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700633 if (mOutput != 0) {
634 uint32_t sampleRate = 0;
635 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
636 if (status == NO_ERROR) {
637 mSampleRate = sampleRate;
638 }
639 }
640 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800641 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642}
643
644status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
645{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100646 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800647 return INVALID_OPERATION;
648 }
649
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800651 ;
652 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
653 loopEnd - loopStart >= MIN_LOOP) {
654 ;
655 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 return BAD_VALUE;
657 }
658
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800659 AutoMutex lock(mLock);
660 // See setPosition() regarding setting parameters such as loop points or position while active
661 if (mState == STATE_ACTIVE) {
662 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700663 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800664 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 return NO_ERROR;
666}
667
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800668void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
669{
670 // FIXME If setting a loop also sets position to start of loop, then
671 // this is correct. Otherwise it should be removed.
672 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
673 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
674 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
675}
676
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677status_t AudioTrack::setMarkerPosition(uint32_t marker)
678{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700679 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100680 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700681 return INVALID_OPERATION;
682 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800684 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700686 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687
688 return NO_ERROR;
689}
690
Glenn Kastena5224f32012-01-04 12:41:44 -0800691status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100693 if (isOffloaded()) {
694 return INVALID_OPERATION;
695 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700696 if (marker == NULL) {
697 return BAD_VALUE;
698 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800699
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800700 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701 *marker = mMarkerPosition;
702
703 return NO_ERROR;
704}
705
706status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
707{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700708 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100709 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700710 return INVALID_OPERATION;
711 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800713 AutoMutex lock(mLock);
714 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800715 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800716
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800717 return NO_ERROR;
718}
719
Glenn Kastena5224f32012-01-04 12:41:44 -0800720status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800721{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100722 if (isOffloaded()) {
723 return INVALID_OPERATION;
724 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700725 if (updatePeriod == NULL) {
726 return BAD_VALUE;
727 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800729 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730 *updatePeriod = mUpdatePeriod;
731
732 return NO_ERROR;
733}
734
735status_t AudioTrack::setPosition(uint32_t position)
736{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100737 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700738 return INVALID_OPERATION;
739 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800740 if (position > mFrameCount) {
741 return BAD_VALUE;
742 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800743
Eric Laurent1703cdf2011-03-07 14:52:59 -0800744 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800745 // Currently we require that the player is inactive before setting parameters such as position
746 // or loop points. Otherwise, there could be a race condition: the application could read the
747 // current position, compute a new position or loop parameters, and then set that position or
748 // loop parameters but it would do the "wrong" thing since the position has continued to advance
749 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
750 // to specify how it wants to handle such scenarios.
751 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700752 return INVALID_OPERATION;
753 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800754 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
755 mLoopPeriod = 0;
756 // FIXME Check whether loops and setting position are incompatible in old code.
757 // If we use setLoop for both purposes we lose the capability to set the position while looping.
758 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700759
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760 return NO_ERROR;
761}
762
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800763status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800764{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700765 if (position == NULL) {
766 return BAD_VALUE;
767 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800768
Eric Laurent1703cdf2011-03-07 14:52:59 -0800769 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800770 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100771 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800772
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100773 if (mOutput != 0) {
774 uint32_t halFrames;
775 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
776 }
777 *position = dspFrames;
778 } else {
779 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
780 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
781 mProxy->getPosition();
782 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800783 return NO_ERROR;
784}
785
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000786status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800787{
788 if (mSharedBuffer == 0 || mIsTimed) {
789 return INVALID_OPERATION;
790 }
791 if (position == NULL) {
792 return BAD_VALUE;
793 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800794
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800795 AutoMutex lock(mLock);
796 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800797 return NO_ERROR;
798}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800799
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800800status_t AudioTrack::reload()
801{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100802 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800803 return INVALID_OPERATION;
804 }
805
Eric Laurent1703cdf2011-03-07 14:52:59 -0800806 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800807 // See setPosition() regarding setting parameters such as loop points or position while active
808 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700809 return INVALID_OPERATION;
810 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800811 mNewPosition = mUpdatePeriod;
812 mLoopPeriod = 0;
813 // FIXME The new code cannot reload while keeping a loop specified.
814 // Need to check how the old code handled this, and whether it's a significant change.
815 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800816 return NO_ERROR;
817}
818
Glenn Kasten38e905b2014-01-13 10:21:48 -0800819audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700820{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800821 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100822 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800823}
824
Eric Laurentbe916aa2010-06-01 23:49:17 -0700825status_t AudioTrack::attachAuxEffect(int effectId)
826{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800827 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700828 status_t status = mAudioTrack->attachAuxEffect(effectId);
829 if (status == NO_ERROR) {
830 mAuxEffectId = effectId;
831 }
832 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700833}
834
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800835// -------------------------------------------------------------------------
836
Eric Laurent1703cdf2011-03-07 14:52:59 -0800837// must be called with mLock held
Glenn Kasten363fb752014-01-15 12:27:31 -0800838status_t AudioTrack::createTrack_l(size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800839{
840 status_t status;
841 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
842 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700843 ALOGE("Could not get audioflinger");
844 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800845 }
846
Glenn Kasten38e905b2014-01-13 10:21:48 -0800847 audio_io_handle_t output = AudioSystem::getOutput(mStreamType, mSampleRate, mFormat,
848 mChannelMask, mFlags, mOffloadInfo);
849 if (output == 0) {
850 ALOGE("Could not get audio output for stream type %d, sample rate %u, format %#x, "
851 "channel mask %#x, flags %#x",
852 mStreamType, mSampleRate, mFormat, mChannelMask, mFlags);
853 return BAD_VALUE;
854 }
855 {
856 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
857 // we must release it ourselves if anything goes wrong.
858
Glenn Kastence8828a2013-09-16 18:07:38 -0700859 // Not all of these values are needed under all conditions, but it is easier to get them all
860
Eric Laurentd1b449a2010-05-14 03:26:45 -0700861 uint32_t afLatency;
Glenn Kasten363fb752014-01-15 12:27:31 -0800862 status = AudioSystem::getLatency(output, mStreamType, &afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700863 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800864 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800865 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700866 }
867
Glenn Kastence8828a2013-09-16 18:07:38 -0700868 size_t afFrameCount;
Glenn Kasten363fb752014-01-15 12:27:31 -0800869 status = AudioSystem::getFrameCount(output, mStreamType, &afFrameCount);
Glenn Kastence8828a2013-09-16 18:07:38 -0700870 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800871 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, mStreamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800872 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700873 }
874
875 uint32_t afSampleRate;
Glenn Kasten363fb752014-01-15 12:27:31 -0800876 status = AudioSystem::getSamplingRate(output, mStreamType, &afSampleRate);
Glenn Kastence8828a2013-09-16 18:07:38 -0700877 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800878 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, mStreamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800879 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700880 }
881
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700882 // Client decides whether the track is TIMED (see below), but can only express a preference
883 // for FAST. Server will perform additional tests.
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800884 if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !((
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885 // either of these use cases:
886 // use case 1: shared buffer
Glenn Kasten363fb752014-01-15 12:27:31 -0800887 (mSharedBuffer != 0) ||
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700888 // use case 2: callback handler
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800889 (mCbf != NULL)) &&
890 // matching sample rate
891 (mSampleRate == afSampleRate))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800892 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700893 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -0800894 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700895 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700896 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700897
Glenn Kastence8828a2013-09-16 18:07:38 -0700898 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800899 // n = 1 fast track with single buffering; nBuffering is ignored
900 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700901 // n = 2 normal track, no sample rate conversion
902 // n = 3 normal track, with sample rate conversion
903 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
904 // n > 3 very high latency or very small notification interval; nBuffering is ignored
Glenn Kasten363fb752014-01-15 12:27:31 -0800905 const uint32_t nBuffering = (mSampleRate == afSampleRate) ? 2 : 3;
Glenn Kastence8828a2013-09-16 18:07:38 -0700906
Eric Laurentd1b449a2010-05-14 03:26:45 -0700907 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700908
Glenn Kasten363fb752014-01-15 12:27:31 -0800909 size_t frameCount = mReqFrameCount;
910 if (!audio_is_linear_pcm(mFormat)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700911
Glenn Kasten363fb752014-01-15 12:27:31 -0800912 if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700913 // Same comment as below about ignoring frameCount parameter for set()
Glenn Kasten363fb752014-01-15 12:27:31 -0800914 frameCount = mSharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700915 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700917 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100918 if (mNotificationFramesAct != frameCount) {
919 mNotificationFramesAct = frameCount;
920 }
Glenn Kasten363fb752014-01-15 12:27:31 -0800921 } else if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922
Glenn Kastena42ff002012-11-14 12:47:55 -0800923 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700924 // 8-bit data in shared memory is not currently supported by AudioFlinger
Glenn Kasten363fb752014-01-15 12:27:31 -0800925 size_t alignment = /* mFormat == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800926 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700927 // More than 2 channels does not require stronger alignment than stereo
928 alignment <<= 1;
929 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000930 if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800931 ALOGE("Invalid buffer alignment: address %p, channel count %u",
Glenn Kasten363fb752014-01-15 12:27:31 -0800932 mSharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800933 status = BAD_VALUE;
934 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700935 }
936
937 // When initializing a shared buffer AudioTrack via constructors,
938 // there's no frameCount parameter.
939 // But when initializing a shared buffer AudioTrack via set(),
940 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kasten363fb752014-01-15 12:27:31 -0800941 frameCount = mSharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700942
Glenn Kasten363fb752014-01-15 12:27:31 -0800943 } else if (!(mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700944
945 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700946
Eric Laurentd1b449a2010-05-14 03:26:45 -0700947 // Ensure that buffer depth covers at least audio hardware latency
948 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700949 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
950 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700951 if (minBufCount <= nBuffering) {
952 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800953 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700954
Glenn Kasten363fb752014-01-15 12:27:31 -0800955 size_t minFrameCount = (afFrameCount*mSampleRate*minBufCount)/afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -0800956 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800957 ", afLatency=%d",
Glenn Kasten363fb752014-01-15 12:27:31 -0800958 minFrameCount, afFrameCount, minBufCount, mSampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700959
960 if (frameCount == 0) {
961 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700962 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700963 // not ALOGW because it happens all the time when playing key clicks over A2DP
964 ALOGV("Minimum buffer size corrected from %d to %d",
965 frameCount, minFrameCount);
966 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800967 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700968 // Make sure that application is notified with sufficient margin before underrun
969 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
970 mNotificationFramesAct = frameCount/nBuffering;
971 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700972
Glenn Kastene0fa4672012-04-24 14:35:14 -0700973 } else {
974 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975 }
976
Glenn Kastena075db42012-03-06 11:22:44 -0800977 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
978 if (mIsTimed) {
979 trackFlags |= IAudioFlinger::TRACK_TIMED;
980 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800981
982 pid_t tid = -1;
Glenn Kasten363fb752014-01-15 12:27:31 -0800983 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700984 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800985 if (mAudioTrackThread != 0) {
986 tid = mAudioTrackThread->getTid();
987 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700988 }
989
Glenn Kasten363fb752014-01-15 12:27:31 -0800990 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100991 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
992 }
993
Glenn Kasten74935e42013-12-19 08:56:45 -0800994 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
995 // but we will still need the original value also
Glenn Kasten363fb752014-01-15 12:27:31 -0800996 sp<IAudioTrack> track = audioFlinger->createTrack(mStreamType,
997 mSampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700998 // AudioFlinger only sees 16-bit PCM
Glenn Kasten363fb752014-01-15 12:27:31 -0800999 mFormat == AUDIO_FORMAT_PCM_8_BIT ?
1000 AUDIO_FORMAT_PCM_16_BIT : mFormat,
Glenn Kastena42ff002012-11-14 12:47:55 -08001001 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001002 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -08001003 &trackFlags,
Glenn Kasten363fb752014-01-15 12:27:31 -08001004 mSharedBuffer,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001005 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
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001012 if (status != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00001013 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001014 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001015 }
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001016 ALOG_ASSERT(track != 0);
1017
Glenn Kasten38e905b2014-01-13 10:21:48 -08001018 // AudioFlinger now owns the reference to the I/O handle,
1019 // so we are no longer responsible for releasing it.
1020
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001021 sp<IMemory> iMem = track->getCblk();
1022 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001023 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001024 return NO_INIT;
1025 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001026 void *iMemPointer = iMem->pointer();
1027 if (iMemPointer == NULL) {
1028 ALOGE("Could not get control block pointer");
1029 return NO_INIT;
1030 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001031 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001032 if (mAudioTrack != 0) {
1033 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1034 mDeathNotifier.clear();
1035 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001036 mAudioTrack = track;
Glenn Kasten5f631512014-02-24 15:16:07 -08001037
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001038 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001039 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001040 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001041 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001042 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1043 // In current design, AudioTrack client checks and ensures frame count validity before
1044 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1045 // for fast track as it uses a special method of assigning frame count.
1046 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1047 }
1048 frameCount = temp;
Glenn Kasten5f631512014-02-24 15:16:07 -08001049
Glenn Kastena07f17c2013-04-23 12:39:37 -07001050 mAwaitBoost = false;
Glenn Kasten363fb752014-01-15 12:27:31 -08001051 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001052 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001053 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001054 mAwaitBoost = true;
Glenn Kasten363fb752014-01-15 12:27:31 -08001055 if (mSharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001056 // Theoretically double-buffering is not required for fast tracks,
1057 // due to tighter scheduling. But in practice, to accommodate kernels with
1058 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1059 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1060 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001061 }
1062 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001063 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001064 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001065 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001066 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
1067 if (mSharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001068 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1069 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001070 }
1071 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001072 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001073 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001074 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001075 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1076 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1077 } else {
1078 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
Glenn Kasten363fb752014-01-15 12:27:31 -08001079 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001080 // FIXME This is a warning, not an error, so don't return error status
1081 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001082 }
1083 }
1084
Glenn Kasten38e905b2014-01-13 10:21:48 -08001085 // We retain a copy of the I/O handle, but don't own the reference
1086 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001087 mRefreshRemaining = true;
1088
1089 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1090 // is the value of pointer() for the shared buffer, otherwise buffers points
1091 // immediately after the control block. This address is for the mapping within client
1092 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1093 void* buffers;
Glenn Kasten363fb752014-01-15 12:27:31 -08001094 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001095 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001096 } else {
Glenn Kasten363fb752014-01-15 12:27:31 -08001097 buffers = mSharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001098 }
1099
Eric Laurent2beeb502010-07-16 07:43:46 -07001100 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001101 // FIXME don't believe this lie
Glenn Kasten363fb752014-01-15 12:27:31 -08001102 mLatency = afLatency + (1000*frameCount) / mSampleRate;
Glenn Kasten5f631512014-02-24 15:16:07 -08001103
Glenn Kastenb6037442012-11-14 13:42:25 -08001104 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001105 // If IAudioTrack is re-created, don't let the requested frameCount
1106 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001107 if (frameCount > mReqFrameCount) {
1108 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001109 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001110
1111 // update proxy
Glenn Kasten363fb752014-01-15 12:27:31 -08001112 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001113 mStaticProxy.clear();
1114 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1115 } else {
1116 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1117 mProxy = mStaticProxy;
1118 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001119 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1120 uint16_t(mVolume[LEFT] * 0x1000));
1121 mProxy->setSendLevel(mSendLevel);
1122 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001123 mProxy->setEpoch(epoch);
1124 mProxy->setMinimum(mNotificationFramesAct);
1125
1126 mDeathNotifier = new DeathNotifier(this);
1127 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001128
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001129 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001130 }
1131
1132release:
1133 AudioSystem::releaseOutput(output);
1134 if (status == NO_ERROR) {
1135 status = NO_INIT;
1136 }
1137 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001138}
1139
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001140status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1141{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001142 if (audioBuffer == NULL) {
1143 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001144 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001145 if (mTransfer != TRANSFER_OBTAIN) {
1146 audioBuffer->frameCount = 0;
1147 audioBuffer->size = 0;
1148 audioBuffer->raw = NULL;
1149 return INVALID_OPERATION;
1150 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001151
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001152 const struct timespec *requested;
Eric Laurentdf576992014-01-27 18:13:39 -08001153 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001154 if (waitCount == -1) {
1155 requested = &ClientProxy::kForever;
1156 } else if (waitCount == 0) {
1157 requested = &ClientProxy::kNonBlocking;
1158 } else if (waitCount > 0) {
1159 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001160 timeout.tv_sec = ms / 1000;
1161 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1162 requested = &timeout;
1163 } else {
1164 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1165 requested = NULL;
1166 }
1167 return obtainBuffer(audioBuffer, requested);
1168}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001169
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001170status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1171 struct timespec *elapsed, size_t *nonContig)
1172{
1173 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1174 uint32_t oldSequence = 0;
1175 uint32_t newSequence;
1176
1177 Proxy::Buffer buffer;
1178 status_t status = NO_ERROR;
1179
1180 static const int32_t kMaxTries = 5;
1181 int32_t tryCounter = kMaxTries;
1182
1183 do {
1184 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1185 // keep them from going away if another thread re-creates the track during obtainBuffer()
1186 sp<AudioTrackClientProxy> proxy;
1187 sp<IMemory> iMem;
1188
1189 { // start of lock scope
1190 AutoMutex lock(mLock);
1191
1192 newSequence = mSequence;
1193 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1194 if (status == DEAD_OBJECT) {
1195 // re-create track, unless someone else has already done so
1196 if (newSequence == oldSequence) {
1197 status = restoreTrack_l("obtainBuffer");
1198 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001199 buffer.mFrameCount = 0;
1200 buffer.mRaw = NULL;
1201 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001202 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204 }
1205 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001206 oldSequence = newSequence;
1207
1208 // Keep the extra references
1209 proxy = mProxy;
1210 iMem = mCblkMemory;
1211
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001212 if (mState == STATE_STOPPING) {
1213 status = -EINTR;
1214 buffer.mFrameCount = 0;
1215 buffer.mRaw = NULL;
1216 buffer.mNonContig = 0;
1217 break;
1218 }
1219
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001220 // Non-blocking if track is stopped or paused
1221 if (mState != STATE_ACTIVE) {
1222 requested = &ClientProxy::kNonBlocking;
1223 }
1224
1225 } // end of lock scope
1226
1227 buffer.mFrameCount = audioBuffer->frameCount;
1228 // FIXME starts the requested timeout and elapsed over from scratch
1229 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1230
1231 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1232
1233 audioBuffer->frameCount = buffer.mFrameCount;
1234 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1235 audioBuffer->raw = buffer.mRaw;
1236 if (nonContig != NULL) {
1237 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001239 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240}
1241
1242void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1243{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001244 if (mTransfer == TRANSFER_SHARED) {
1245 return;
1246 }
1247
1248 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1249 if (stepCount == 0) {
1250 return;
1251 }
1252
1253 Proxy::Buffer buffer;
1254 buffer.mFrameCount = stepCount;
1255 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001256
Eric Laurent1703cdf2011-03-07 14:52:59 -08001257 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001258 mInUnderrun = false;
1259 mProxy->releaseBuffer(&buffer);
1260
1261 // restart track if it was disabled by audioflinger due to previous underrun
1262 if (mState == STATE_ACTIVE) {
1263 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001264 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001265 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1266 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001268 mAudioTrack->start();
1269 }
1270 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271}
1272
1273// -------------------------------------------------------------------------
1274
1275ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1276{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001277 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001278 return INVALID_OPERATION;
1279 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001281 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001282 // Sanity-check: user is most-likely passing an error code, and it would
1283 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001284 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285 return BAD_VALUE;
1286 }
1287
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001288 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001289 Buffer audioBuffer;
1290
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001291 while (userSize >= mFrameSize) {
1292 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001293
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001294 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001296 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001298 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 return ssize_t(err);
1300 }
1301
1302 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001303 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001305 toWrite = audioBuffer.size >> 1;
1306 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001307 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001308 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001309 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001311 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001312 userSize -= toWrite;
1313 written += toWrite;
1314
1315 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001316 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001317
1318 return written;
1319}
1320
1321// -------------------------------------------------------------------------
1322
John Grossman4ff14ba2012-02-08 16:37:41 -08001323TimedAudioTrack::TimedAudioTrack() {
1324 mIsTimed = true;
1325}
1326
1327status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1328{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001329 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001330 status_t result = UNKNOWN_ERROR;
1331
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001332#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001333 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1334 // while we are accessing the cblk
1335 sp<IAudioTrack> audioTrack = mAudioTrack;
1336 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001337#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001338
John Grossman4ff14ba2012-02-08 16:37:41 -08001339 // If the track is not invalid already, try to allocate a buffer. alloc
1340 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001341 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001342 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001343 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001344 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1345 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001346 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001347 }
1348 }
1349
1350 // If the track is invalid at this point, attempt to restore it. and try the
1351 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001352 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001353 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001354
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001355 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001356 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001357 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001358 }
1359
1360 return result;
1361}
1362
1363status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1364 int64_t pts)
1365{
Eric Laurentdf839842012-05-31 14:27:14 -07001366 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1367 {
1368 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001369 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001370 // restart track if it was disabled by audioflinger due to previous underrun
1371 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001372 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1373 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001374 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001375 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001376 mAudioTrack->start();
1377 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001378 }
Eric Laurentdf839842012-05-31 14:27:14 -07001379 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001380}
1381
1382status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1383 TargetTimeline target)
1384{
1385 return mAudioTrack->setMediaTimeTransform(xform, target);
1386}
1387
1388// -------------------------------------------------------------------------
1389
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001390nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001391{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001392 // Currently the AudioTrack thread is not created if there are no callbacks.
1393 // Would it ever make sense to run the thread, even without callbacks?
1394 // If so, then replace this by checks at each use for mCbf != NULL.
1395 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1396
Eric Laurent1703cdf2011-03-07 14:52:59 -08001397 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001398 if (mAwaitBoost) {
1399 mAwaitBoost = false;
1400 mLock.unlock();
1401 static const int32_t kMaxTries = 5;
1402 int32_t tryCounter = kMaxTries;
1403 uint32_t pollUs = 10000;
1404 do {
1405 int policy = sched_getscheduler(0);
1406 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1407 break;
1408 }
1409 usleep(pollUs);
1410 pollUs <<= 1;
1411 } while (tryCounter-- > 0);
1412 if (tryCounter < 0) {
1413 ALOGE("did not receive expected priority boost on time");
1414 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001415 // Run again immediately
1416 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001417 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001418
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001419 // Can only reference mCblk while locked
1420 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001421 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001422
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001423 // Check for track invalidation
1424 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001425 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1426 // AudioSystem cache. We should not exit here but after calling the callback so
1427 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001428 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001429 status_t status = restoreTrack_l("processAudioBuffer");
1430 mLock.unlock();
1431 // Run again immediately, but with a new IAudioTrack
1432 return 0;
1433 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001434 }
1435
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001436 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001437 bool active = mState == STATE_ACTIVE;
1438
1439 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1440 bool newUnderrun = false;
1441 if (flags & CBLK_UNDERRUN) {
1442#if 0
1443 // Currently in shared buffer mode, when the server reaches the end of buffer,
1444 // the track stays active in continuous underrun state. It's up to the application
1445 // to pause or stop the track, or set the position to a new offset within buffer.
1446 // This was some experimental code to auto-pause on underrun. Keeping it here
1447 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1448 if (mTransfer == TRANSFER_SHARED) {
1449 mState = STATE_PAUSED;
1450 active = false;
1451 }
1452#endif
1453 if (!mInUnderrun) {
1454 mInUnderrun = true;
1455 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456 }
1457 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001458
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001459 // Get current position of server
1460 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461
1462 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001463 bool markerReached = false;
1464 size_t markerPosition = mMarkerPosition;
1465 // FIXME fails for wraparound, need 64 bits
1466 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1467 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001468 }
1469
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001470 // Determine number of new position callback(s) that will be needed, while locked
1471 size_t newPosCount = 0;
1472 size_t newPosition = mNewPosition;
1473 size_t updatePeriod = mUpdatePeriod;
1474 // FIXME fails for wraparound, need 64 bits
1475 if (updatePeriod > 0 && position >= newPosition) {
1476 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1477 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001478 }
1479
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001480 // Cache other fields that will be needed soon
1481 uint32_t loopPeriod = mLoopPeriod;
1482 uint32_t sampleRate = mSampleRate;
Glenn Kasten838b3d82014-02-27 15:30:41 -08001483 uint32_t notificationFrames = mNotificationFramesAct;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001484 if (mRefreshRemaining) {
1485 mRefreshRemaining = false;
1486 mRemainingFrames = notificationFrames;
1487 mRetryOnPartialBuffer = false;
1488 }
1489 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001490 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001491
1492 // These fields don't need to be cached, because they are assigned only by set():
1493 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1494 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1495
1496 mLock.unlock();
1497
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001498 if (waitStreamEnd) {
1499 AutoMutex lock(mLock);
1500
1501 sp<AudioTrackClientProxy> proxy = mProxy;
1502 sp<IMemory> iMem = mCblkMemory;
1503
1504 struct timespec timeout;
1505 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1506 timeout.tv_nsec = 0;
1507
1508 mLock.unlock();
1509 status_t status = mProxy->waitStreamEndDone(&timeout);
1510 mLock.lock();
1511 switch (status) {
1512 case NO_ERROR:
1513 case DEAD_OBJECT:
1514 case TIMED_OUT:
1515 mLock.unlock();
1516 mCbf(EVENT_STREAM_END, mUserData, NULL);
1517 mLock.lock();
1518 if (mState == STATE_STOPPING) {
1519 mState = STATE_STOPPED;
1520 if (status != DEAD_OBJECT) {
1521 return NS_INACTIVE;
1522 }
1523 }
1524 return 0;
1525 default:
1526 return 0;
1527 }
1528 }
1529
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001530 // perform callbacks while unlocked
1531 if (newUnderrun) {
1532 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1533 }
1534 // FIXME we will miss loops if loop cycle was signaled several times since last call
1535 // to processAudioBuffer()
1536 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1537 mCbf(EVENT_LOOP_END, mUserData, NULL);
1538 }
1539 if (flags & CBLK_BUFFER_END) {
1540 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1541 }
1542 if (markerReached) {
1543 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1544 }
1545 while (newPosCount > 0) {
1546 size_t temp = newPosition;
1547 mCbf(EVENT_NEW_POS, mUserData, &temp);
1548 newPosition += updatePeriod;
1549 newPosCount--;
1550 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001551
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001552 if (mObservedSequence != sequence) {
1553 mObservedSequence = sequence;
1554 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001555 // for offloaded tracks, just wait for the upper layers to recreate the track
1556 if (isOffloaded()) {
1557 return NS_INACTIVE;
1558 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559 }
1560
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001561 // if inactive, then don't run me again until re-started
1562 if (!active) {
1563 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001564 }
1565
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001566 // Compute the estimated time until the next timed event (position, markers, loops)
1567 // FIXME only for non-compressed audio
1568 uint32_t minFrames = ~0;
1569 if (!markerReached && position < markerPosition) {
1570 minFrames = markerPosition - position;
1571 }
1572 if (loopPeriod > 0 && loopPeriod < minFrames) {
1573 minFrames = loopPeriod;
1574 }
1575 if (updatePeriod > 0 && updatePeriod < minFrames) {
1576 minFrames = updatePeriod;
1577 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001578
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001579 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1580 static const uint32_t kPoll = 0;
1581 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1582 minFrames = kPoll * notificationFrames;
1583 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001584
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001585 // Convert frame units to time units
1586 nsecs_t ns = NS_WHENEVER;
1587 if (minFrames != (uint32_t) ~0) {
1588 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1589 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1590 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1591 }
1592
1593 // If not supplying data by EVENT_MORE_DATA, then we're done
1594 if (mTransfer != TRANSFER_CALLBACK) {
1595 return ns;
1596 }
1597
1598 struct timespec timeout;
1599 const struct timespec *requested = &ClientProxy::kForever;
1600 if (ns != NS_WHENEVER) {
1601 timeout.tv_sec = ns / 1000000000LL;
1602 timeout.tv_nsec = ns % 1000000000LL;
1603 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1604 requested = &timeout;
1605 }
1606
1607 while (mRemainingFrames > 0) {
1608
1609 Buffer audioBuffer;
1610 audioBuffer.frameCount = mRemainingFrames;
1611 size_t nonContig;
1612 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1613 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1614 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1615 requested = &ClientProxy::kNonBlocking;
1616 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001617 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1618 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001619 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001620 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1621 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001622 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001623 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001624 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1625 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001626 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001627
Eric Laurent42a6f422013-08-29 14:35:05 -07001628 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001629 mRetryOnPartialBuffer = false;
1630 if (avail < mRemainingFrames) {
1631 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1632 if (ns < 0 || myns < ns) {
1633 ns = myns;
1634 }
1635 return ns;
1636 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001637 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001638
1639 // Divide buffer size by 2 to take into account the expansion
1640 // due to 8 to 16 bit conversion: the callback must fill only half
1641 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001642 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643 audioBuffer.size >>= 1;
1644 }
1645
1646 size_t reqSize = audioBuffer.size;
1647 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001648 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649
1650 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001651 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1652 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1653 reqSize, (int) writtenSize);
1654 return NS_NEVER;
1655 }
1656
1657 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001658 // The callback is done filling buffers
1659 // Keep this thread going to handle timed events and
1660 // still try to get more data in intervals of WAIT_PERIOD_MS
1661 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001662 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001663 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001664
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001665 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001666 // 8 to 16 bit conversion, note that source and destination are the same address
1667 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001668 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001669 }
1670
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001671 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1672 audioBuffer.frameCount = releasedFrames;
1673 mRemainingFrames -= releasedFrames;
1674 if (misalignment >= releasedFrames) {
1675 misalignment -= releasedFrames;
1676 } else {
1677 misalignment = 0;
1678 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679
1680 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001682 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1683 // if callback doesn't like to accept the full chunk
1684 if (writtenSize < reqSize) {
1685 continue;
1686 }
1687
1688 // There could be enough non-contiguous frames available to satisfy the remaining request
1689 if (mRemainingFrames <= nonContig) {
1690 continue;
1691 }
1692
1693#if 0
1694 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1695 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1696 // that total to a sum == notificationFrames.
1697 if (0 < misalignment && misalignment <= mRemainingFrames) {
1698 mRemainingFrames = misalignment;
1699 return (mRemainingFrames * 1100000000LL) / sampleRate;
1700 }
1701#endif
1702
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001703 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001704 mRemainingFrames = notificationFrames;
1705 mRetryOnPartialBuffer = true;
1706
1707 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1708 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001709}
1710
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001711status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001712{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001713 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001714 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001715 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001716 status_t result;
1717
Glenn Kastena47f3162012-11-07 10:13:08 -08001718 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kasten38e905b2014-01-13 10:21:48 -08001719 // output parameters in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001720 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001721
Glenn Kasten23a75452014-01-13 10:37:17 -08001722 if (isOffloaded_l()) {
1723 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001724 return DEAD_OBJECT;
1725 }
1726
Glenn Kastena47f3162012-11-07 10:13:08 -08001727 // if the new IAudioTrack is created, createTrack_l() will modify the
1728 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1729 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001730
1731 // take the frames that will be lost by track recreation into account in saved position
1732 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001733 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kasten363fb752014-01-15 12:27:31 -08001734 result = createTrack_l(position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001735
Glenn Kastena47f3162012-11-07 10:13:08 -08001736 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001737 // continue playback from last known position, but
1738 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1739 if (mStaticProxy != NULL) {
1740 mLoopPeriod = 0;
1741 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1742 }
1743 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1744 // track destruction have been played? This is critical for SoundPool implementation
1745 // This must be broken, and needs to be tested/debugged.
1746#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001747 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001748 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001749 // Make sure that a client relying on callback events indicating underrun or
1750 // the actual amount of audio frames played (e.g SoundPool) receives them.
1751 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001752 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001753 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001754 }
1755 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001756#endif
1757 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001758 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001759 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001760 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001761 if (result != NO_ERROR) {
Glenn Kasten2b2165c2014-01-13 08:53:36 -08001762 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001763#if 0 // FIXME This should no longer be needed
1764 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001765 // As getOutput was called above and resulted in an output stream to be opened,
1766 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001767 if (mOutput != 0) {
1768 AudioSystem::releaseOutput(mOutput);
1769 mOutput = 0;
1770 }
1771#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001772 ALOGW("restoreTrack_l() failed status %d", result);
1773 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001774 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001775
1776 return result;
1777}
1778
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001779status_t AudioTrack::setParameters(const String8& keyValuePairs)
1780{
1781 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001782 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001783}
1784
Glenn Kastence703742013-07-19 16:33:58 -07001785status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1786{
Glenn Kasten53cec222013-08-29 09:01:02 -07001787 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001788 // FIXME not implemented for fast tracks; should use proxy and SSQ
1789 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1790 return INVALID_OPERATION;
1791 }
1792 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1793 return INVALID_OPERATION;
1794 }
1795 status_t status = mAudioTrack->getTimestamp(timestamp);
1796 if (status == NO_ERROR) {
1797 timestamp.mPosition += mProxy->getEpoch();
1798 }
1799 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001800}
1801
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001802String8 AudioTrack::getParameters(const String8& keys)
1803{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001804 audio_io_handle_t output = getOutput();
1805 if (output != 0) {
1806 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001807 } else {
1808 return String8::empty();
1809 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001810}
1811
Glenn Kasten23a75452014-01-13 10:37:17 -08001812bool AudioTrack::isOffloaded() const
1813{
1814 AutoMutex lock(mLock);
1815 return isOffloaded_l();
1816}
1817
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001818status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001819{
1820
1821 const size_t SIZE = 256;
1822 char buffer[SIZE];
1823 String8 result;
1824
1825 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001826 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1827 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001828 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001829 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001830 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001831 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001832 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001833 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001834 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835 result.append(buffer);
1836 ::write(fd, result.string(), result.size());
1837 return NO_ERROR;
1838}
1839
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001840uint32_t AudioTrack::getUnderrunFrames() const
1841{
1842 AutoMutex lock(mLock);
1843 return mProxy->getUnderrunFrames();
1844}
1845
1846// =========================================================================
1847
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001848void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001849{
1850 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1851 if (audioTrack != 0) {
1852 AutoMutex lock(audioTrack->mLock);
1853 audioTrack->mProxy->binderDied();
1854 }
1855}
1856
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001857// =========================================================================
1858
1859AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001860 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1861 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001862{
1863}
1864
1865AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001866{
1867}
1868
1869bool AudioTrack::AudioTrackThread::threadLoop()
1870{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001871 {
1872 AutoMutex _l(mMyLock);
1873 if (mPaused) {
1874 mMyCond.wait(mMyLock);
1875 // caller will check for exitPending()
1876 return true;
1877 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001878 if (mIgnoreNextPausedInt) {
1879 mIgnoreNextPausedInt = false;
1880 mPausedInt = false;
1881 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001882 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001883 if (mPausedNs > 0) {
1884 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1885 } else {
1886 mMyCond.wait(mMyLock);
1887 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001888 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001889 return true;
1890 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001891 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001892 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001893 switch (ns) {
1894 case 0:
1895 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001896 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001897 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001898 return true;
1899 case NS_NEVER:
1900 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001901 case NS_WHENEVER:
1902 // FIXME increase poll interval, or make event-driven
1903 ns = 1000000000LL;
1904 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001905 default:
1906 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001907 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001908 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001909 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001910}
1911
Glenn Kasten3acbd052012-02-28 10:39:56 -08001912void AudioTrack::AudioTrackThread::requestExit()
1913{
1914 // must be in this order to avoid a race condition
1915 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001916 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001917}
1918
1919void AudioTrack::AudioTrackThread::pause()
1920{
1921 AutoMutex _l(mMyLock);
1922 mPaused = true;
1923}
1924
1925void AudioTrack::AudioTrackThread::resume()
1926{
1927 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001928 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001929 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001930 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001931 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001932 mMyCond.signal();
1933 }
1934}
1935
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001936void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1937{
1938 AutoMutex _l(mMyLock);
1939 mPausedInt = true;
1940 mPausedNs = ns;
1941}
1942
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001943}; // namespace android