blob: eed7a0b056a401cda1642c043d31137e33bae362 [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,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 int 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,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700115 int 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,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700141 int 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 Kastene33054e2012-11-14 12:54:39 -0800185 int frameCountInt,
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,
189 int notificationFrames,
190 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 Kasten86f04662014-02-24 15:13:05 -0800198 ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %d, "
199 "flags #%x, notificationFrames %d, sessionId %d, transferType %d",
200 streamType, sampleRate, format, channelMask, frameCountInt, flags, notificationFrames,
201 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 Kastene33054e2012-11-14 12:54:39 -0800239 // FIXME "int" here is legacy and will be replaced by size_t later
240 if (frameCountInt < 0) {
241 ALOGE("Invalid frame count %d", frameCountInt);
242 return BAD_VALUE;
243 }
244 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700246 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
247 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248
Glenn Kastene33054e2012-11-14 12:54:39 -0800249 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700250
Eric Laurent1703cdf2011-03-07 14:52:59 -0800251 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800252
Glenn Kasten53cec222013-08-29 09:01:02 -0700253 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700254 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000255 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 return INVALID_OPERATION;
257 }
258
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 if (streamType == AUDIO_STREAM_DEFAULT) {
261 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800263 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
264 ALOGE("Invalid stream type %d", streamType);
265 return BAD_VALUE;
266 }
267 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700268
Glenn Kastenb1bef512014-01-13 10:25:53 -0800269 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800271 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
272 if (status != NO_ERROR) {
273 ALOGE("Could not get output sample rate for stream type %d; status %d",
274 streamType, status);
275 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700276 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800278 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700279
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800281 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284
285 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700286 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800287 ALOGE("Invalid format %#x", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 return BAD_VALUE;
289 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800290 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291
Glenn Kasten8ba90322013-10-30 11:29:27 -0700292 if (!audio_is_output_channel(channelMask)) {
293 ALOGE("Invalid channel mask %#x", channelMask);
294 return BAD_VALUE;
295 }
Glenn Kastene3247bf2014-02-24 15:19:07 -0800296 mChannelMask = channelMask;
297 uint32_t channelCount = popcount(channelMask);
298 mChannelCount = channelCount;
Glenn Kasten8ba90322013-10-30 11:29:27 -0700299
Glenn Kastene0fa4672012-04-24 14:35:14 -0700300 // AudioFlinger does not currently support 8-bit data in shared memory
301 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
302 ALOGE("8-bit data in shared memory is not supported");
303 return BAD_VALUE;
304 }
305
Eric Laurentc2f1f072009-07-17 12:17:14 -0700306 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100307 // or offload was requested
308 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
309 || !audio_is_linear_pcm(format)) {
310 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
311 ? "Offload request, forcing to Direct Output"
312 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700313 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800314 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700315 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700316 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700317 // only allow deep buffering for music stream type
318 if (streamType != AUDIO_STREAM_MUSIC) {
319 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
320 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321
Glenn Kastene3aa6592012-12-04 12:22:46 -0800322 if (audio_is_linear_pcm(format)) {
323 mFrameSize = channelCount * audio_bytes_per_sample(format);
324 mFrameSizeAF = channelCount * sizeof(int16_t);
325 } else {
326 mFrameSize = sizeof(uint8_t);
327 mFrameSizeAF = sizeof(uint8_t);
328 }
329
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800330 // Make copy of input parameter offloadInfo so that in the future:
331 // (a) createTrack_l doesn't need it as an input parameter
332 // (b) we can support re-creation of offloaded tracks
333 if (offloadInfo != NULL) {
334 mOffloadInfoCopy = *offloadInfo;
335 mOffloadInfo = &mOffloadInfoCopy;
336 } else {
337 mOffloadInfo = NULL;
338 }
339
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800340 mVolume[LEFT] = 1.0f;
341 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800342 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800343 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800344 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700345 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800346 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700347 mSessionId = sessionId;
Marco Nelissend457c972014-02-11 08:47:07 -0800348 int callingpid = IPCThreadState::self()->getCallingPid();
349 int mypid = getpid();
350 if (uid == -1 || (callingpid != mypid)) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800351 mClientUid = IPCThreadState::self()->getCallingUid();
352 } else {
353 mClientUid = uid;
354 }
Marco Nelissend457c972014-02-11 08:47:07 -0800355 if (pid == -1 || (callingpid != mypid)) {
356 mClientPid = callingpid;
357 } else {
358 mClientPid = pid;
359 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700360 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700361 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700362 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700363
Glenn Kastena997e7a2012-08-07 09:44:19 -0700364 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700365 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700366 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
367 }
368
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800369 // create the IAudioTrack
Glenn Kasten363fb752014-01-15 12:27:31 -0800370 status = createTrack_l(0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800371
Glenn Kastena997e7a2012-08-07 09:44:19 -0700372 if (status != NO_ERROR) {
373 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100374 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
375 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700376 mAudioTrackThread.clear();
377 }
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800378 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800379#if 0 // FIXME This should no longer be needed
380 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100381 // As getOutput was called above and resulted in an output stream to be opened,
382 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800383 if (mOutput != 0) {
384 AudioSystem::releaseOutput(mOutput);
385 mOutput = 0;
386 }
387#endif
Glenn Kastena997e7a2012-08-07 09:44:19 -0700388 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700389 }
390
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800392 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800394 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700396 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 mNewPosition = 0;
398 mUpdatePeriod = 0;
Marco Nelissend457c972014-02-11 08:47:07 -0800399 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800400 mSequence = 1;
401 mObservedSequence = mSequence;
402 mInUnderrun = false;
403
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 return NO_ERROR;
405}
406
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407// -------------------------------------------------------------------------
408
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100409status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800411 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100412
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800413 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100414 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 }
416
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800417 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800419 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100420 if (previousState == STATE_PAUSED_STOPPING) {
421 mState = STATE_STOPPING;
422 } else {
423 mState = STATE_ACTIVE;
424 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800425 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
426 // reset current position as seen by client to 0
427 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700428 // force refresh of remaining frames by processAudioBuffer() as last
429 // write before stop could be partial.
430 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431 }
432 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700433 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800434
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800436 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100437 if (previousState == STATE_STOPPING) {
438 mProxy->interrupt();
439 } else {
440 t->resume();
441 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800442 } else {
443 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
444 get_sched_policy(0, &mPreviousSchedulingGroup);
445 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
446 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800448 status_t status = NO_ERROR;
449 if (!(flags & CBLK_INVALID)) {
450 status = mAudioTrack->start();
451 if (status == DEAD_OBJECT) {
452 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800454 }
455 if (flags & CBLK_INVALID) {
456 status = restoreTrack_l("start");
457 }
458
459 if (status != NO_ERROR) {
460 ALOGE("start() status %d", status);
461 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100463 if (previousState != STATE_STOPPING) {
464 t->pause();
465 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700467 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700468 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 }
470 }
471
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100472 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800473}
474
475void AudioTrack::stop()
476{
477 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700478 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800479 return;
480 }
481
Glenn Kasten23a75452014-01-13 10:37:17 -0800482 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100483 mState = STATE_STOPPING;
484 } else {
485 mState = STATE_STOPPED;
486 }
487
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800488 mProxy->interrupt();
489 mAudioTrack->stop();
490 // the playback head position will reset to 0, so if a marker is set, we need
491 // to activate it again
492 mMarkerReached = false;
493#if 0
494 // Force flush if a shared buffer is used otherwise audioflinger
495 // will not stop before end of buffer is reached.
496 // It may be needed to make sure that we stop playback, likely in case looping is on.
497 if (mSharedBuffer != 0) {
498 flush_l();
499 }
500#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100501
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800502 sp<AudioTrackThread> t = mAudioTrackThread;
503 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800504 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100505 t->pause();
506 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800507 } else {
508 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
509 set_sched_policy(0, mPreviousSchedulingGroup);
510 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511}
512
513bool AudioTrack::stopped() const
514{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800515 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800516 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517}
518
519void AudioTrack::flush()
520{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800521 if (mSharedBuffer != 0) {
522 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800523 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800524 AutoMutex lock(mLock);
525 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
526 return;
527 }
528 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800529}
530
Eric Laurent1703cdf2011-03-07 14:52:59 -0800531void AudioTrack::flush_l()
532{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800533 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700534
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700535 // clear playback marker and periodic update counter
536 mMarkerPosition = 0;
537 mMarkerReached = false;
538 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100539 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700540
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800541 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800542 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100543 mProxy->interrupt();
544 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800545 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800546 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547}
548
549void AudioTrack::pause()
550{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800551 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100552 if (mState == STATE_ACTIVE) {
553 mState = STATE_PAUSED;
554 } else if (mState == STATE_STOPPING) {
555 mState = STATE_PAUSED_STOPPING;
556 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800557 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800559 mProxy->interrupt();
560 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561}
562
Eric Laurentbe916aa2010-06-01 23:49:17 -0700563status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800565 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700566 return BAD_VALUE;
567 }
568
Eric Laurent1703cdf2011-03-07 14:52:59 -0800569 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800570 mVolume[LEFT] = left;
571 mVolume[RIGHT] = right;
572
Glenn Kastene3aa6592012-12-04 12:22:46 -0800573 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700574
Glenn Kasten23a75452014-01-13 10:37:17 -0800575 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700576 mAudioTrack->signal();
577 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700578 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579}
580
Glenn Kastenb1c09932012-02-27 16:21:04 -0800581status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800583 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700584}
585
Eric Laurent2beeb502010-07-16 07:43:46 -0700586status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587{
Glenn Kasten05632a52012-01-03 14:22:33 -0800588 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700589 return BAD_VALUE;
590 }
591
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800592 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700593 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800594 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700595
596 return NO_ERROR;
597}
598
Glenn Kastena5224f32012-01-04 12:41:44 -0800599void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700600{
601 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800602 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700603 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604}
605
Glenn Kasten3b16c762012-11-14 08:44:39 -0800606status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100608 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800609 return INVALID_OPERATION;
610 }
611
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800612 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700614 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 }
616 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700617 if (rate == 0 || rate > afSamplingRate*2 ) {
618 return BAD_VALUE;
619 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620
Eric Laurent1703cdf2011-03-07 14:52:59 -0800621 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800622 mSampleRate = rate;
623 mProxy->setSampleRate(rate);
624
Eric Laurent57326622009-07-07 07:10:45 -0700625 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626}
627
Glenn Kastena5224f32012-01-04 12:41:44 -0800628uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629{
John Grossman4ff14ba2012-02-08 16:37:41 -0800630 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800631 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800632 }
633
Eric Laurent1703cdf2011-03-07 14:52:59 -0800634 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700635
636 // sample rate can be updated during playback by the offloaded decoder so we need to
637 // query the HAL and update if needed.
638// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800639 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700640 if (mOutput != 0) {
641 uint32_t sampleRate = 0;
642 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
643 if (status == NO_ERROR) {
644 mSampleRate = sampleRate;
645 }
646 }
647 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800648 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649}
650
651status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
652{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100653 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800654 return INVALID_OPERATION;
655 }
656
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800657 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800658 ;
659 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
660 loopEnd - loopStart >= MIN_LOOP) {
661 ;
662 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663 return BAD_VALUE;
664 }
665
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800666 AutoMutex lock(mLock);
667 // See setPosition() regarding setting parameters such as loop points or position while active
668 if (mState == STATE_ACTIVE) {
669 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700670 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800671 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672 return NO_ERROR;
673}
674
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800675void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
676{
677 // FIXME If setting a loop also sets position to start of loop, then
678 // this is correct. Otherwise it should be removed.
679 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
680 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
681 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
682}
683
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684status_t AudioTrack::setMarkerPosition(uint32_t marker)
685{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700686 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100687 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700688 return INVALID_OPERATION;
689 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800691 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700693 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694
695 return NO_ERROR;
696}
697
Glenn Kastena5224f32012-01-04 12:41:44 -0800698status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800699{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100700 if (isOffloaded()) {
701 return INVALID_OPERATION;
702 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700703 if (marker == NULL) {
704 return BAD_VALUE;
705 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800707 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 *marker = mMarkerPosition;
709
710 return NO_ERROR;
711}
712
713status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
714{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700715 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100716 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700717 return INVALID_OPERATION;
718 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800720 AutoMutex lock(mLock);
721 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800722 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800723
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800724 return NO_ERROR;
725}
726
Glenn Kastena5224f32012-01-04 12:41:44 -0800727status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100729 if (isOffloaded()) {
730 return INVALID_OPERATION;
731 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700732 if (updatePeriod == NULL) {
733 return BAD_VALUE;
734 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800736 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800737 *updatePeriod = mUpdatePeriod;
738
739 return NO_ERROR;
740}
741
742status_t AudioTrack::setPosition(uint32_t position)
743{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100744 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700745 return INVALID_OPERATION;
746 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800747 if (position > mFrameCount) {
748 return BAD_VALUE;
749 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800750
Eric Laurent1703cdf2011-03-07 14:52:59 -0800751 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800752 // Currently we require that the player is inactive before setting parameters such as position
753 // or loop points. Otherwise, there could be a race condition: the application could read the
754 // current position, compute a new position or loop parameters, and then set that position or
755 // loop parameters but it would do the "wrong" thing since the position has continued to advance
756 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
757 // to specify how it wants to handle such scenarios.
758 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700759 return INVALID_OPERATION;
760 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800761 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
762 mLoopPeriod = 0;
763 // FIXME Check whether loops and setting position are incompatible in old code.
764 // If we use setLoop for both purposes we lose the capability to set the position while looping.
765 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700766
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800767 return NO_ERROR;
768}
769
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800770status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700772 if (position == NULL) {
773 return BAD_VALUE;
774 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800775
Eric Laurent1703cdf2011-03-07 14:52:59 -0800776 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800777 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100778 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100780 if (mOutput != 0) {
781 uint32_t halFrames;
782 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
783 }
784 *position = dspFrames;
785 } else {
786 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
787 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
788 mProxy->getPosition();
789 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800790 return NO_ERROR;
791}
792
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000793status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800794{
795 if (mSharedBuffer == 0 || mIsTimed) {
796 return INVALID_OPERATION;
797 }
798 if (position == NULL) {
799 return BAD_VALUE;
800 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800801
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800802 AutoMutex lock(mLock);
803 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800804 return NO_ERROR;
805}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800806
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800807status_t AudioTrack::reload()
808{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100809 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800810 return INVALID_OPERATION;
811 }
812
Eric Laurent1703cdf2011-03-07 14:52:59 -0800813 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800814 // See setPosition() regarding setting parameters such as loop points or position while active
815 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700816 return INVALID_OPERATION;
817 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800818 mNewPosition = mUpdatePeriod;
819 mLoopPeriod = 0;
820 // FIXME The new code cannot reload while keeping a loop specified.
821 // Need to check how the old code handled this, and whether it's a significant change.
822 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823 return NO_ERROR;
824}
825
Glenn Kasten38e905b2014-01-13 10:21:48 -0800826audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700827{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800828 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100829 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800830}
831
Eric Laurentbe916aa2010-06-01 23:49:17 -0700832status_t AudioTrack::attachAuxEffect(int effectId)
833{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800834 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700835 status_t status = mAudioTrack->attachAuxEffect(effectId);
836 if (status == NO_ERROR) {
837 mAuxEffectId = effectId;
838 }
839 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700840}
841
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800842// -------------------------------------------------------------------------
843
Eric Laurent1703cdf2011-03-07 14:52:59 -0800844// must be called with mLock held
Glenn Kasten363fb752014-01-15 12:27:31 -0800845status_t AudioTrack::createTrack_l(size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800846{
847 status_t status;
848 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
849 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700850 ALOGE("Could not get audioflinger");
851 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800852 }
853
Glenn Kasten38e905b2014-01-13 10:21:48 -0800854 audio_io_handle_t output = AudioSystem::getOutput(mStreamType, mSampleRate, mFormat,
855 mChannelMask, mFlags, mOffloadInfo);
856 if (output == 0) {
857 ALOGE("Could not get audio output for stream type %d, sample rate %u, format %#x, "
858 "channel mask %#x, flags %#x",
859 mStreamType, mSampleRate, mFormat, mChannelMask, mFlags);
860 return BAD_VALUE;
861 }
862 {
863 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
864 // we must release it ourselves if anything goes wrong.
865
Glenn Kastence8828a2013-09-16 18:07:38 -0700866 // Not all of these values are needed under all conditions, but it is easier to get them all
867
Eric Laurentd1b449a2010-05-14 03:26:45 -0700868 uint32_t afLatency;
Glenn Kasten363fb752014-01-15 12:27:31 -0800869 status = AudioSystem::getLatency(output, mStreamType, &afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700870 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800871 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800872 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700873 }
874
Glenn Kastence8828a2013-09-16 18:07:38 -0700875 size_t afFrameCount;
Glenn Kasten363fb752014-01-15 12:27:31 -0800876 status = AudioSystem::getFrameCount(output, mStreamType, &afFrameCount);
Glenn Kastence8828a2013-09-16 18:07:38 -0700877 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800878 ALOGE("getFrameCount(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
882 uint32_t afSampleRate;
Glenn Kasten363fb752014-01-15 12:27:31 -0800883 status = AudioSystem::getSamplingRate(output, mStreamType, &afSampleRate);
Glenn Kastence8828a2013-09-16 18:07:38 -0700884 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800885 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, mStreamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800886 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700887 }
888
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700889 // Client decides whether the track is TIMED (see below), but can only express a preference
890 // for FAST. Server will perform additional tests.
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800891 if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !((
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700892 // either of these use cases:
893 // use case 1: shared buffer
Glenn Kasten363fb752014-01-15 12:27:31 -0800894 (mSharedBuffer != 0) ||
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700895 // use case 2: callback handler
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800896 (mCbf != NULL)) &&
897 // matching sample rate
898 (mSampleRate == afSampleRate))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800899 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700900 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -0800901 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700902 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700903 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700904
Glenn Kastence8828a2013-09-16 18:07:38 -0700905 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800906 // n = 1 fast track with single buffering; nBuffering is ignored
907 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700908 // n = 2 normal track, no sample rate conversion
909 // n = 3 normal track, with sample rate conversion
910 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
911 // n > 3 very high latency or very small notification interval; nBuffering is ignored
Glenn Kasten363fb752014-01-15 12:27:31 -0800912 const uint32_t nBuffering = (mSampleRate == afSampleRate) ? 2 : 3;
Glenn Kastence8828a2013-09-16 18:07:38 -0700913
Eric Laurentd1b449a2010-05-14 03:26:45 -0700914 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700915
Glenn Kasten363fb752014-01-15 12:27:31 -0800916 size_t frameCount = mReqFrameCount;
917 if (!audio_is_linear_pcm(mFormat)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700918
Glenn Kasten363fb752014-01-15 12:27:31 -0800919 if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920 // Same comment as below about ignoring frameCount parameter for set()
Glenn Kasten363fb752014-01-15 12:27:31 -0800921 frameCount = mSharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700923 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700924 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100925 if (mNotificationFramesAct != frameCount) {
926 mNotificationFramesAct = frameCount;
927 }
Glenn Kasten363fb752014-01-15 12:27:31 -0800928 } else if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700929
Glenn Kastena42ff002012-11-14 12:47:55 -0800930 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700931 // 8-bit data in shared memory is not currently supported by AudioFlinger
Glenn Kasten363fb752014-01-15 12:27:31 -0800932 size_t alignment = /* mFormat == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800933 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934 // More than 2 channels does not require stronger alignment than stereo
935 alignment <<= 1;
936 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000937 if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800938 ALOGE("Invalid buffer alignment: address %p, channel count %u",
Glenn Kasten363fb752014-01-15 12:27:31 -0800939 mSharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800940 status = BAD_VALUE;
941 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700942 }
943
944 // When initializing a shared buffer AudioTrack via constructors,
945 // there's no frameCount parameter.
946 // But when initializing a shared buffer AudioTrack via set(),
947 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kasten363fb752014-01-15 12:27:31 -0800948 frameCount = mSharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700949
Glenn Kasten363fb752014-01-15 12:27:31 -0800950 } else if (!(mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700951
952 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700953
Eric Laurentd1b449a2010-05-14 03:26:45 -0700954 // Ensure that buffer depth covers at least audio hardware latency
955 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700956 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
957 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700958 if (minBufCount <= nBuffering) {
959 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800960 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700961
Glenn Kasten363fb752014-01-15 12:27:31 -0800962 size_t minFrameCount = (afFrameCount*mSampleRate*minBufCount)/afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -0800963 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800964 ", afLatency=%d",
Glenn Kasten363fb752014-01-15 12:27:31 -0800965 minFrameCount, afFrameCount, minBufCount, mSampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700966
967 if (frameCount == 0) {
968 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700969 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700970 // not ALOGW because it happens all the time when playing key clicks over A2DP
971 ALOGV("Minimum buffer size corrected from %d to %d",
972 frameCount, minFrameCount);
973 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800974 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700975 // Make sure that application is notified with sufficient margin before underrun
976 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
977 mNotificationFramesAct = frameCount/nBuffering;
978 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700979
Glenn Kastene0fa4672012-04-24 14:35:14 -0700980 } else {
981 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700982 }
983
Glenn Kastena075db42012-03-06 11:22:44 -0800984 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
985 if (mIsTimed) {
986 trackFlags |= IAudioFlinger::TRACK_TIMED;
987 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800988
989 pid_t tid = -1;
Glenn Kasten363fb752014-01-15 12:27:31 -0800990 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700991 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800992 if (mAudioTrackThread != 0) {
993 tid = mAudioTrackThread->getTid();
994 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700995 }
996
Glenn Kasten363fb752014-01-15 12:27:31 -0800997 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100998 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
999 }
1000
Glenn Kasten74935e42013-12-19 08:56:45 -08001001 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
1002 // but we will still need the original value also
Glenn Kasten363fb752014-01-15 12:27:31 -08001003 sp<IAudioTrack> track = audioFlinger->createTrack(mStreamType,
1004 mSampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001005 // AudioFlinger only sees 16-bit PCM
Glenn Kasten363fb752014-01-15 12:27:31 -08001006 mFormat == AUDIO_FORMAT_PCM_8_BIT ?
1007 AUDIO_FORMAT_PCM_16_BIT : mFormat,
Glenn Kastena42ff002012-11-14 12:47:55 -08001008 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001009 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -08001010 &trackFlags,
Glenn Kasten363fb752014-01-15 12:27:31 -08001011 mSharedBuffer,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001012 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001013 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001014 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001015 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001016 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001017 &status);
1018
1019 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001020 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001021 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001022 }
Glenn Kasten38e905b2014-01-13 10:21:48 -08001023 // AudioFlinger now owns the reference to the I/O handle,
1024 // so we are no longer responsible for releasing it.
1025
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001026 sp<IMemory> iMem = track->getCblk();
1027 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001028 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001029 return NO_INIT;
1030 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001031 void *iMemPointer = iMem->pointer();
1032 if (iMemPointer == NULL) {
1033 ALOGE("Could not get control block pointer");
1034 return NO_INIT;
1035 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001036 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001037 if (mAudioTrack != 0) {
1038 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1039 mDeathNotifier.clear();
1040 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001041 mAudioTrack = track;
Glenn Kasten5f631512014-02-24 15:16:07 -08001042
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001043 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001044 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001045 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001046 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001047 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1048 // In current design, AudioTrack client checks and ensures frame count validity before
1049 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1050 // for fast track as it uses a special method of assigning frame count.
1051 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1052 }
1053 frameCount = temp;
Glenn Kasten5f631512014-02-24 15:16:07 -08001054
Glenn Kastena07f17c2013-04-23 12:39:37 -07001055 mAwaitBoost = false;
Glenn Kasten363fb752014-01-15 12:27:31 -08001056 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001057 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001058 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001059 mAwaitBoost = true;
Glenn Kasten363fb752014-01-15 12:27:31 -08001060 if (mSharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001061 // Theoretically double-buffering is not required for fast tracks,
1062 // due to tighter scheduling. But in practice, to accommodate kernels with
1063 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1064 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1065 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001066 }
1067 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001068 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001069 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001070 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001071 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
1072 if (mSharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001073 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1074 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001075 }
1076 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001077 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001078 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001079 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001080 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1081 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1082 } else {
1083 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
Glenn Kasten363fb752014-01-15 12:27:31 -08001084 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001085 // FIXME This is a warning, not an error, so don't return error status
1086 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001087 }
1088 }
1089
Glenn Kasten38e905b2014-01-13 10:21:48 -08001090 // We retain a copy of the I/O handle, but don't own the reference
1091 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001092 mRefreshRemaining = true;
1093
1094 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1095 // is the value of pointer() for the shared buffer, otherwise buffers points
1096 // immediately after the control block. This address is for the mapping within client
1097 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1098 void* buffers;
Glenn Kasten363fb752014-01-15 12:27:31 -08001099 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001100 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001101 } else {
Glenn Kasten363fb752014-01-15 12:27:31 -08001102 buffers = mSharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001103 }
1104
Eric Laurent2beeb502010-07-16 07:43:46 -07001105 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001106 // FIXME don't believe this lie
Glenn Kasten363fb752014-01-15 12:27:31 -08001107 mLatency = afLatency + (1000*frameCount) / mSampleRate;
Glenn Kasten5f631512014-02-24 15:16:07 -08001108
Glenn Kastenb6037442012-11-14 13:42:25 -08001109 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001110 // If IAudioTrack is re-created, don't let the requested frameCount
1111 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001112 if (frameCount > mReqFrameCount) {
1113 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001114 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001115
1116 // update proxy
Glenn Kasten363fb752014-01-15 12:27:31 -08001117 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001118 mStaticProxy.clear();
1119 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1120 } else {
1121 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1122 mProxy = mStaticProxy;
1123 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001124 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1125 uint16_t(mVolume[LEFT] * 0x1000));
1126 mProxy->setSendLevel(mSendLevel);
1127 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001128 mProxy->setEpoch(epoch);
1129 mProxy->setMinimum(mNotificationFramesAct);
1130
1131 mDeathNotifier = new DeathNotifier(this);
1132 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001133
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001134 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001135 }
1136
1137release:
1138 AudioSystem::releaseOutput(output);
1139 if (status == NO_ERROR) {
1140 status = NO_INIT;
1141 }
1142 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001143}
1144
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001145status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1146{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001147 if (audioBuffer == NULL) {
1148 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001149 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001150 if (mTransfer != TRANSFER_OBTAIN) {
1151 audioBuffer->frameCount = 0;
1152 audioBuffer->size = 0;
1153 audioBuffer->raw = NULL;
1154 return INVALID_OPERATION;
1155 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001156
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001157 const struct timespec *requested;
Eric Laurentdf576992014-01-27 18:13:39 -08001158 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001159 if (waitCount == -1) {
1160 requested = &ClientProxy::kForever;
1161 } else if (waitCount == 0) {
1162 requested = &ClientProxy::kNonBlocking;
1163 } else if (waitCount > 0) {
1164 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001165 timeout.tv_sec = ms / 1000;
1166 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1167 requested = &timeout;
1168 } else {
1169 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1170 requested = NULL;
1171 }
1172 return obtainBuffer(audioBuffer, requested);
1173}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001174
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001175status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1176 struct timespec *elapsed, size_t *nonContig)
1177{
1178 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1179 uint32_t oldSequence = 0;
1180 uint32_t newSequence;
1181
1182 Proxy::Buffer buffer;
1183 status_t status = NO_ERROR;
1184
1185 static const int32_t kMaxTries = 5;
1186 int32_t tryCounter = kMaxTries;
1187
1188 do {
1189 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1190 // keep them from going away if another thread re-creates the track during obtainBuffer()
1191 sp<AudioTrackClientProxy> proxy;
1192 sp<IMemory> iMem;
1193
1194 { // start of lock scope
1195 AutoMutex lock(mLock);
1196
1197 newSequence = mSequence;
1198 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1199 if (status == DEAD_OBJECT) {
1200 // re-create track, unless someone else has already done so
1201 if (newSequence == oldSequence) {
1202 status = restoreTrack_l("obtainBuffer");
1203 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001204 buffer.mFrameCount = 0;
1205 buffer.mRaw = NULL;
1206 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001207 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209 }
1210 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001211 oldSequence = newSequence;
1212
1213 // Keep the extra references
1214 proxy = mProxy;
1215 iMem = mCblkMemory;
1216
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001217 if (mState == STATE_STOPPING) {
1218 status = -EINTR;
1219 buffer.mFrameCount = 0;
1220 buffer.mRaw = NULL;
1221 buffer.mNonContig = 0;
1222 break;
1223 }
1224
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001225 // Non-blocking if track is stopped or paused
1226 if (mState != STATE_ACTIVE) {
1227 requested = &ClientProxy::kNonBlocking;
1228 }
1229
1230 } // end of lock scope
1231
1232 buffer.mFrameCount = audioBuffer->frameCount;
1233 // FIXME starts the requested timeout and elapsed over from scratch
1234 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1235
1236 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1237
1238 audioBuffer->frameCount = buffer.mFrameCount;
1239 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1240 audioBuffer->raw = buffer.mRaw;
1241 if (nonContig != NULL) {
1242 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001243 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001244 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245}
1246
1247void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1248{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001249 if (mTransfer == TRANSFER_SHARED) {
1250 return;
1251 }
1252
1253 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1254 if (stepCount == 0) {
1255 return;
1256 }
1257
1258 Proxy::Buffer buffer;
1259 buffer.mFrameCount = stepCount;
1260 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001261
Eric Laurent1703cdf2011-03-07 14:52:59 -08001262 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001263 mInUnderrun = false;
1264 mProxy->releaseBuffer(&buffer);
1265
1266 // restart track if it was disabled by audioflinger due to previous underrun
1267 if (mState == STATE_ACTIVE) {
1268 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001269 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001270 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1271 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001272 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001273 mAudioTrack->start();
1274 }
1275 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276}
1277
1278// -------------------------------------------------------------------------
1279
1280ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1281{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001282 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001283 return INVALID_OPERATION;
1284 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001286 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001287 // Sanity-check: user is most-likely passing an error code, and it would
1288 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001289 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 return BAD_VALUE;
1291 }
1292
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001293 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 Buffer audioBuffer;
1295
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001296 while (userSize >= mFrameSize) {
1297 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001298
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001299 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001301 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001303 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304 return ssize_t(err);
1305 }
1306
1307 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001308 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001309 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001310 toWrite = audioBuffer.size >> 1;
1311 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001312 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001313 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001314 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001316 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001317 userSize -= toWrite;
1318 written += toWrite;
1319
1320 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001321 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001322
1323 return written;
1324}
1325
1326// -------------------------------------------------------------------------
1327
John Grossman4ff14ba2012-02-08 16:37:41 -08001328TimedAudioTrack::TimedAudioTrack() {
1329 mIsTimed = true;
1330}
1331
1332status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1333{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001334 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001335 status_t result = UNKNOWN_ERROR;
1336
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001337#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001338 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1339 // while we are accessing the cblk
1340 sp<IAudioTrack> audioTrack = mAudioTrack;
1341 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001342#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001343
John Grossman4ff14ba2012-02-08 16:37:41 -08001344 // If the track is not invalid already, try to allocate a buffer. alloc
1345 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001346 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001347 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001348 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001349 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1350 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001351 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001352 }
1353 }
1354
1355 // If the track is invalid at this point, attempt to restore it. and try the
1356 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001357 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001358 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001359
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001360 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001361 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001362 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001363 }
1364
1365 return result;
1366}
1367
1368status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1369 int64_t pts)
1370{
Eric Laurentdf839842012-05-31 14:27:14 -07001371 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1372 {
1373 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001374 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001375 // restart track if it was disabled by audioflinger due to previous underrun
1376 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001377 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1378 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001379 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001380 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001381 mAudioTrack->start();
1382 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001383 }
Eric Laurentdf839842012-05-31 14:27:14 -07001384 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001385}
1386
1387status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1388 TargetTimeline target)
1389{
1390 return mAudioTrack->setMediaTimeTransform(xform, target);
1391}
1392
1393// -------------------------------------------------------------------------
1394
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001395nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001396{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001397 // Currently the AudioTrack thread is not created if there are no callbacks.
1398 // Would it ever make sense to run the thread, even without callbacks?
1399 // If so, then replace this by checks at each use for mCbf != NULL.
1400 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1401
Eric Laurent1703cdf2011-03-07 14:52:59 -08001402 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001403 if (mAwaitBoost) {
1404 mAwaitBoost = false;
1405 mLock.unlock();
1406 static const int32_t kMaxTries = 5;
1407 int32_t tryCounter = kMaxTries;
1408 uint32_t pollUs = 10000;
1409 do {
1410 int policy = sched_getscheduler(0);
1411 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1412 break;
1413 }
1414 usleep(pollUs);
1415 pollUs <<= 1;
1416 } while (tryCounter-- > 0);
1417 if (tryCounter < 0) {
1418 ALOGE("did not receive expected priority boost on time");
1419 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001420 // Run again immediately
1421 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001422 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001423
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001424 // Can only reference mCblk while locked
1425 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001426 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001427
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001428 // Check for track invalidation
1429 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001430 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1431 // AudioSystem cache. We should not exit here but after calling the callback so
1432 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001433 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001434 status_t status = restoreTrack_l("processAudioBuffer");
1435 mLock.unlock();
1436 // Run again immediately, but with a new IAudioTrack
1437 return 0;
1438 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001439 }
1440
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001441 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001442 bool active = mState == STATE_ACTIVE;
1443
1444 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1445 bool newUnderrun = false;
1446 if (flags & CBLK_UNDERRUN) {
1447#if 0
1448 // Currently in shared buffer mode, when the server reaches the end of buffer,
1449 // the track stays active in continuous underrun state. It's up to the application
1450 // to pause or stop the track, or set the position to a new offset within buffer.
1451 // This was some experimental code to auto-pause on underrun. Keeping it here
1452 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1453 if (mTransfer == TRANSFER_SHARED) {
1454 mState = STATE_PAUSED;
1455 active = false;
1456 }
1457#endif
1458 if (!mInUnderrun) {
1459 mInUnderrun = true;
1460 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461 }
1462 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001463
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001464 // Get current position of server
1465 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001466
1467 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001468 bool markerReached = false;
1469 size_t markerPosition = mMarkerPosition;
1470 // FIXME fails for wraparound, need 64 bits
1471 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1472 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473 }
1474
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001475 // Determine number of new position callback(s) that will be needed, while locked
1476 size_t newPosCount = 0;
1477 size_t newPosition = mNewPosition;
1478 size_t updatePeriod = mUpdatePeriod;
1479 // FIXME fails for wraparound, need 64 bits
1480 if (updatePeriod > 0 && position >= newPosition) {
1481 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1482 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001483 }
1484
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001485 // Cache other fields that will be needed soon
1486 uint32_t loopPeriod = mLoopPeriod;
1487 uint32_t sampleRate = mSampleRate;
1488 size_t notificationFrames = mNotificationFramesAct;
1489 if (mRefreshRemaining) {
1490 mRefreshRemaining = false;
1491 mRemainingFrames = notificationFrames;
1492 mRetryOnPartialBuffer = false;
1493 }
1494 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001495 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001496
1497 // These fields don't need to be cached, because they are assigned only by set():
1498 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1499 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1500
1501 mLock.unlock();
1502
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001503 if (waitStreamEnd) {
1504 AutoMutex lock(mLock);
1505
1506 sp<AudioTrackClientProxy> proxy = mProxy;
1507 sp<IMemory> iMem = mCblkMemory;
1508
1509 struct timespec timeout;
1510 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1511 timeout.tv_nsec = 0;
1512
1513 mLock.unlock();
1514 status_t status = mProxy->waitStreamEndDone(&timeout);
1515 mLock.lock();
1516 switch (status) {
1517 case NO_ERROR:
1518 case DEAD_OBJECT:
1519 case TIMED_OUT:
1520 mLock.unlock();
1521 mCbf(EVENT_STREAM_END, mUserData, NULL);
1522 mLock.lock();
1523 if (mState == STATE_STOPPING) {
1524 mState = STATE_STOPPED;
1525 if (status != DEAD_OBJECT) {
1526 return NS_INACTIVE;
1527 }
1528 }
1529 return 0;
1530 default:
1531 return 0;
1532 }
1533 }
1534
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001535 // perform callbacks while unlocked
1536 if (newUnderrun) {
1537 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1538 }
1539 // FIXME we will miss loops if loop cycle was signaled several times since last call
1540 // to processAudioBuffer()
1541 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1542 mCbf(EVENT_LOOP_END, mUserData, NULL);
1543 }
1544 if (flags & CBLK_BUFFER_END) {
1545 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1546 }
1547 if (markerReached) {
1548 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1549 }
1550 while (newPosCount > 0) {
1551 size_t temp = newPosition;
1552 mCbf(EVENT_NEW_POS, mUserData, &temp);
1553 newPosition += updatePeriod;
1554 newPosCount--;
1555 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001556
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001557 if (mObservedSequence != sequence) {
1558 mObservedSequence = sequence;
1559 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001560 // for offloaded tracks, just wait for the upper layers to recreate the track
1561 if (isOffloaded()) {
1562 return NS_INACTIVE;
1563 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001564 }
1565
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001566 // if inactive, then don't run me again until re-started
1567 if (!active) {
1568 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001569 }
1570
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001571 // Compute the estimated time until the next timed event (position, markers, loops)
1572 // FIXME only for non-compressed audio
1573 uint32_t minFrames = ~0;
1574 if (!markerReached && position < markerPosition) {
1575 minFrames = markerPosition - position;
1576 }
1577 if (loopPeriod > 0 && loopPeriod < minFrames) {
1578 minFrames = loopPeriod;
1579 }
1580 if (updatePeriod > 0 && updatePeriod < minFrames) {
1581 minFrames = updatePeriod;
1582 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001583
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001584 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1585 static const uint32_t kPoll = 0;
1586 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1587 minFrames = kPoll * notificationFrames;
1588 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001589
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001590 // Convert frame units to time units
1591 nsecs_t ns = NS_WHENEVER;
1592 if (minFrames != (uint32_t) ~0) {
1593 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1594 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1595 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1596 }
1597
1598 // If not supplying data by EVENT_MORE_DATA, then we're done
1599 if (mTransfer != TRANSFER_CALLBACK) {
1600 return ns;
1601 }
1602
1603 struct timespec timeout;
1604 const struct timespec *requested = &ClientProxy::kForever;
1605 if (ns != NS_WHENEVER) {
1606 timeout.tv_sec = ns / 1000000000LL;
1607 timeout.tv_nsec = ns % 1000000000LL;
1608 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1609 requested = &timeout;
1610 }
1611
1612 while (mRemainingFrames > 0) {
1613
1614 Buffer audioBuffer;
1615 audioBuffer.frameCount = mRemainingFrames;
1616 size_t nonContig;
1617 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1618 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1619 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1620 requested = &ClientProxy::kNonBlocking;
1621 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001622 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1623 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001624 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001625 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1626 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001627 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001628 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001629 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1630 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001631 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001632
Eric Laurent42a6f422013-08-29 14:35:05 -07001633 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001634 mRetryOnPartialBuffer = false;
1635 if (avail < mRemainingFrames) {
1636 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1637 if (ns < 0 || myns < ns) {
1638 ns = myns;
1639 }
1640 return ns;
1641 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001642 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643
1644 // Divide buffer size by 2 to take into account the expansion
1645 // due to 8 to 16 bit conversion: the callback must fill only half
1646 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001647 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648 audioBuffer.size >>= 1;
1649 }
1650
1651 size_t reqSize = audioBuffer.size;
1652 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001653 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001654
1655 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001656 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1657 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1658 reqSize, (int) writtenSize);
1659 return NS_NEVER;
1660 }
1661
1662 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001663 // The callback is done filling buffers
1664 // Keep this thread going to handle timed events and
1665 // still try to get more data in intervals of WAIT_PERIOD_MS
1666 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001667 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001668 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001669
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001670 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001671 // 8 to 16 bit conversion, note that source and destination are the same address
1672 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001673 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001674 }
1675
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001676 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1677 audioBuffer.frameCount = releasedFrames;
1678 mRemainingFrames -= releasedFrames;
1679 if (misalignment >= releasedFrames) {
1680 misalignment -= releasedFrames;
1681 } else {
1682 misalignment = 0;
1683 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001684
1685 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001686
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001687 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1688 // if callback doesn't like to accept the full chunk
1689 if (writtenSize < reqSize) {
1690 continue;
1691 }
1692
1693 // There could be enough non-contiguous frames available to satisfy the remaining request
1694 if (mRemainingFrames <= nonContig) {
1695 continue;
1696 }
1697
1698#if 0
1699 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1700 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1701 // that total to a sum == notificationFrames.
1702 if (0 < misalignment && misalignment <= mRemainingFrames) {
1703 mRemainingFrames = misalignment;
1704 return (mRemainingFrames * 1100000000LL) / sampleRate;
1705 }
1706#endif
1707
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001708 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001709 mRemainingFrames = notificationFrames;
1710 mRetryOnPartialBuffer = true;
1711
1712 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1713 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001714}
1715
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001716status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001717{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001718 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001719 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001720 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001721 status_t result;
1722
Glenn Kastena47f3162012-11-07 10:13:08 -08001723 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kasten38e905b2014-01-13 10:21:48 -08001724 // output parameters in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001725 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001726
Glenn Kasten23a75452014-01-13 10:37:17 -08001727 if (isOffloaded_l()) {
1728 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001729 return DEAD_OBJECT;
1730 }
1731
Glenn Kastena47f3162012-11-07 10:13:08 -08001732 // if the new IAudioTrack is created, createTrack_l() will modify the
1733 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1734 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001735
1736 // take the frames that will be lost by track recreation into account in saved position
1737 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001738 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kasten363fb752014-01-15 12:27:31 -08001739 result = createTrack_l(position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001740
Glenn Kastena47f3162012-11-07 10:13:08 -08001741 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001742 // continue playback from last known position, but
1743 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1744 if (mStaticProxy != NULL) {
1745 mLoopPeriod = 0;
1746 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1747 }
1748 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1749 // track destruction have been played? This is critical for SoundPool implementation
1750 // This must be broken, and needs to be tested/debugged.
1751#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001752 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001753 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001754 // Make sure that a client relying on callback events indicating underrun or
1755 // the actual amount of audio frames played (e.g SoundPool) receives them.
1756 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001757 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001758 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001759 }
1760 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001761#endif
1762 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001763 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001764 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001765 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001766 if (result != NO_ERROR) {
Glenn Kasten2b2165c2014-01-13 08:53:36 -08001767 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001768#if 0 // FIXME This should no longer be needed
1769 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001770 // As getOutput was called above and resulted in an output stream to be opened,
1771 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001772 if (mOutput != 0) {
1773 AudioSystem::releaseOutput(mOutput);
1774 mOutput = 0;
1775 }
1776#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001777 ALOGW("restoreTrack_l() failed status %d", result);
1778 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001779 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001780
1781 return result;
1782}
1783
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001784status_t AudioTrack::setParameters(const String8& keyValuePairs)
1785{
1786 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001787 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001788}
1789
Glenn Kastence703742013-07-19 16:33:58 -07001790status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1791{
Glenn Kasten53cec222013-08-29 09:01:02 -07001792 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001793 // FIXME not implemented for fast tracks; should use proxy and SSQ
1794 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1795 return INVALID_OPERATION;
1796 }
1797 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1798 return INVALID_OPERATION;
1799 }
1800 status_t status = mAudioTrack->getTimestamp(timestamp);
1801 if (status == NO_ERROR) {
1802 timestamp.mPosition += mProxy->getEpoch();
1803 }
1804 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001805}
1806
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001807String8 AudioTrack::getParameters(const String8& keys)
1808{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001809 audio_io_handle_t output = getOutput();
1810 if (output != 0) {
1811 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001812 } else {
1813 return String8::empty();
1814 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001815}
1816
Glenn Kasten23a75452014-01-13 10:37:17 -08001817bool AudioTrack::isOffloaded() const
1818{
1819 AutoMutex lock(mLock);
1820 return isOffloaded_l();
1821}
1822
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001823status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824{
1825
1826 const size_t SIZE = 256;
1827 char buffer[SIZE];
1828 String8 result;
1829
1830 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001831 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1832 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001833 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001834 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001835 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001836 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001837 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001838 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001839 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001840 result.append(buffer);
1841 ::write(fd, result.string(), result.size());
1842 return NO_ERROR;
1843}
1844
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001845uint32_t AudioTrack::getUnderrunFrames() const
1846{
1847 AutoMutex lock(mLock);
1848 return mProxy->getUnderrunFrames();
1849}
1850
1851// =========================================================================
1852
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001853void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001854{
1855 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1856 if (audioTrack != 0) {
1857 AutoMutex lock(audioTrack->mLock);
1858 audioTrack->mProxy->binderDied();
1859 }
1860}
1861
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001862// =========================================================================
1863
1864AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001865 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1866 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001867{
1868}
1869
1870AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001871{
1872}
1873
1874bool AudioTrack::AudioTrackThread::threadLoop()
1875{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001876 {
1877 AutoMutex _l(mMyLock);
1878 if (mPaused) {
1879 mMyCond.wait(mMyLock);
1880 // caller will check for exitPending()
1881 return true;
1882 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001883 if (mIgnoreNextPausedInt) {
1884 mIgnoreNextPausedInt = false;
1885 mPausedInt = false;
1886 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001887 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001888 if (mPausedNs > 0) {
1889 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1890 } else {
1891 mMyCond.wait(mMyLock);
1892 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001893 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001894 return true;
1895 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001896 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001897 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001898 switch (ns) {
1899 case 0:
1900 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001901 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001902 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001903 return true;
1904 case NS_NEVER:
1905 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001906 case NS_WHENEVER:
1907 // FIXME increase poll interval, or make event-driven
1908 ns = 1000000000LL;
1909 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001910 default:
1911 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001912 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001913 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001914 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001915}
1916
Glenn Kasten3acbd052012-02-28 10:39:56 -08001917void AudioTrack::AudioTrackThread::requestExit()
1918{
1919 // must be in this order to avoid a race condition
1920 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001921 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001922}
1923
1924void AudioTrack::AudioTrackThread::pause()
1925{
1926 AutoMutex _l(mMyLock);
1927 mPaused = true;
1928}
1929
1930void AudioTrack::AudioTrackThread::resume()
1931{
1932 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001933 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001934 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001935 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001936 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001937 mMyCond.signal();
1938 }
1939}
1940
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001941void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1942{
1943 AutoMutex _l(mMyLock);
1944 mPausedInt = true;
1945 mPausedNs = ns;
1946}
1947
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001948}; // namespace android