blob: b4d6d76ae41995776b8b54d335224a5a10b9b3fa [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),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800102 mPreviousSchedulingGroup(SP_DEFAULT),
103 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104{
105}
106
107AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800108 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800110 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700111 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800112 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700113 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800114 callback_t cbf,
115 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800116 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800117 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000118 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800119 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800120 int uid,
121 pid_t pid)
Glenn Kasten87913512011-06-22 16:15:25 -0700122 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800123 mIsTimed(false),
124 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800125 mPreviousSchedulingGroup(SP_DEFAULT),
126 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700128 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700129 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800130 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
Marco Nelissend457c972014-02-11 08:47:07 -0800131 offloadInfo, uid, pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132}
133
Andreas Huberc8139852012-01-18 10:51:55 -0800134AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800135 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800137 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700138 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700140 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 callback_t cbf,
142 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800143 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800144 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000145 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800146 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800147 int uid,
148 pid_t pid)
Glenn Kasten87913512011-06-22 16:15:25 -0700149 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800150 mIsTimed(false),
151 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800152 mPreviousSchedulingGroup(SP_DEFAULT),
153 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700155 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800156 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissend457c972014-02-11 08:47:07 -0800157 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo,
158 uid, pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159}
160
161AudioTrack::~AudioTrack()
162{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 if (mStatus == NO_ERROR) {
164 // Make sure that callback function exits in the case where
165 // it is looping on buffer full condition in obtainBuffer().
166 // Otherwise the callback thread will never exit.
167 stop();
168 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100169 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800170 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 mAudioTrackThread->requestExitAndWait();
172 mAudioTrackThread.clear();
173 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700174 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
175 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 IPCThreadState::self()->flushCommands();
Marco Nelissend457c972014-02-11 08:47:07 -0800177 ALOGV("~AudioTrack, releasing session id from %d on behalf of %d",
178 IPCThreadState::self()->getCallingPid(), mClientPid);
179 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180 }
181}
182
183status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800184 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800186 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700187 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800188 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700189 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 callback_t cbf,
191 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800192 uint32_t notificationFrames,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700194 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800195 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000196 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800197 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800198 int uid,
199 pid_t pid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200{
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800201 ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
Glenn Kasten838b3d82014-02-27 15:30:41 -0800202 "flags #%x, notificationFrames %u, sessionId %d, transferType %d",
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800203 streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames,
Glenn Kasten86f04662014-02-24 15:13:05 -0800204 sessionId, transferType);
205
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800206 switch (transferType) {
207 case TRANSFER_DEFAULT:
208 if (sharedBuffer != 0) {
209 transferType = TRANSFER_SHARED;
210 } else if (cbf == NULL || threadCanCallJava) {
211 transferType = TRANSFER_SYNC;
212 } else {
213 transferType = TRANSFER_CALLBACK;
214 }
215 break;
216 case TRANSFER_CALLBACK:
217 if (cbf == NULL || sharedBuffer != 0) {
218 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
219 return BAD_VALUE;
220 }
221 break;
222 case TRANSFER_OBTAIN:
223 case TRANSFER_SYNC:
224 if (sharedBuffer != 0) {
225 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
226 return BAD_VALUE;
227 }
228 break;
229 case TRANSFER_SHARED:
230 if (sharedBuffer == 0) {
231 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
232 return BAD_VALUE;
233 }
234 break;
235 default:
236 ALOGE("Invalid transfer type %d", transferType);
237 return BAD_VALUE;
238 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800239 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800240 mTransfer = transferType;
241
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700242 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
243 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244
Glenn Kastene33054e2012-11-14 12:54:39 -0800245 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700246
Eric Laurent1703cdf2011-03-07 14:52:59 -0800247 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800248
Glenn Kasten53cec222013-08-29 09:01:02 -0700249 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700250 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000251 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 return INVALID_OPERATION;
253 }
254
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700256 if (streamType == AUDIO_STREAM_DEFAULT) {
257 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800259 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
260 ALOGE("Invalid stream type %d", streamType);
261 return BAD_VALUE;
262 }
263 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700264
Glenn Kastenb1bef512014-01-13 10:25:53 -0800265 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800267 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
268 if (status != NO_ERROR) {
269 ALOGE("Could not get output sample rate for stream type %d; status %d",
270 streamType, status);
271 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700272 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800274 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700275
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800277 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700278 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280
281 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800283 ALOGE("Invalid format %#x", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284 return BAD_VALUE;
285 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800286 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287
Glenn Kasten8ba90322013-10-30 11:29:27 -0700288 if (!audio_is_output_channel(channelMask)) {
289 ALOGE("Invalid channel mask %#x", channelMask);
290 return BAD_VALUE;
291 }
Glenn Kastene3247bf2014-02-24 15:19:07 -0800292 mChannelMask = channelMask;
Andy Hunge5412692014-05-16 11:25:07 -0700293 uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastene3247bf2014-02-24 15:19:07 -0800294 mChannelCount = channelCount;
Glenn Kasten8ba90322013-10-30 11:29:27 -0700295
Glenn Kastene0fa4672012-04-24 14:35:14 -0700296 // AudioFlinger does not currently support 8-bit data in shared memory
297 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
298 ALOGE("8-bit data in shared memory is not supported");
299 return BAD_VALUE;
300 }
301
Eric Laurentc2f1f072009-07-17 12:17:14 -0700302 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100303 // or offload was requested
304 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
305 || !audio_is_linear_pcm(format)) {
306 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
307 ? "Offload request, forcing to Direct Output"
308 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700309 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800310 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700311 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700312 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700313 // only allow deep buffering for music stream type
314 if (streamType != AUDIO_STREAM_MUSIC) {
315 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
316 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700317
Glenn Kastenb7730382014-04-30 15:50:31 -0700318 if (flags & AUDIO_OUTPUT_FLAG_DIRECT) {
319 if (audio_is_linear_pcm(format)) {
320 mFrameSize = channelCount * audio_bytes_per_sample(format);
321 } else {
322 mFrameSize = sizeof(uint8_t);
323 }
324 mFrameSizeAF = mFrameSize;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800325 } else {
Glenn Kastenb7730382014-04-30 15:50:31 -0700326 ALOG_ASSERT(audio_is_linear_pcm(format));
327 mFrameSize = channelCount * audio_bytes_per_sample(format);
328 mFrameSizeAF = channelCount * audio_bytes_per_sample(
329 format == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : format);
330 // createTrack will return an error if PCM format is not supported by server,
331 // so no need to check for specific PCM formats here
Glenn Kastene3aa6592012-12-04 12:22:46 -0800332 }
333
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800334 // Make copy of input parameter offloadInfo so that in the future:
335 // (a) createTrack_l doesn't need it as an input parameter
336 // (b) we can support re-creation of offloaded tracks
337 if (offloadInfo != NULL) {
338 mOffloadInfoCopy = *offloadInfo;
339 mOffloadInfo = &mOffloadInfoCopy;
340 } else {
341 mOffloadInfo = NULL;
342 }
343
Glenn Kasten66e46352014-01-16 17:44:23 -0800344 mVolume[AUDIO_INTERLEAVE_LEFT] = 1.0f;
345 mVolume[AUDIO_INTERLEAVE_RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800346 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800347 // mFrameCount is initialized in createTrack_l
Glenn Kastenb603744e2012-11-14 13:42:25 -0800348 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700349 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800350 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700351 mSessionId = sessionId;
Marco Nelissend457c972014-02-11 08:47:07 -0800352 int callingpid = IPCThreadState::self()->getCallingPid();
353 int mypid = getpid();
354 if (uid == -1 || (callingpid != mypid)) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800355 mClientUid = IPCThreadState::self()->getCallingUid();
356 } else {
357 mClientUid = uid;
358 }
Marco Nelissend457c972014-02-11 08:47:07 -0800359 if (pid == -1 || (callingpid != mypid)) {
360 mClientPid = callingpid;
361 } else {
362 mClientPid = pid;
363 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700364 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700365 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700366 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700367
Glenn Kastena997e7a2012-08-07 09:44:19 -0700368 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700369 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700370 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
371 }
372
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800373 // create the IAudioTrack
Glenn Kasten363fb752014-01-15 12:27:31 -0800374 status = createTrack_l(0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800375
Glenn Kastena997e7a2012-08-07 09:44:19 -0700376 if (status != NO_ERROR) {
377 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100378 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
379 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700380 mAudioTrackThread.clear();
381 }
382 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700383 }
384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800386 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800388 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700390 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 mNewPosition = 0;
392 mUpdatePeriod = 0;
Marco Nelissend457c972014-02-11 08:47:07 -0800393 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800394 mSequence = 1;
395 mObservedSequence = mSequence;
396 mInUnderrun = false;
397
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398 return NO_ERROR;
399}
400
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401// -------------------------------------------------------------------------
402
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100403status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800405 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100406
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800407 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100408 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 }
410
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800411 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800413 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100414 if (previousState == STATE_PAUSED_STOPPING) {
415 mState = STATE_STOPPING;
416 } else {
417 mState = STATE_ACTIVE;
418 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800419 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
420 // reset current position as seen by client to 0
421 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700422 // force refresh of remaining frames by processAudioBuffer() as last
423 // write before stop could be partial.
424 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800425 }
426 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700427 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800428
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800430 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100431 if (previousState == STATE_STOPPING) {
432 mProxy->interrupt();
433 } else {
434 t->resume();
435 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800436 } else {
437 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
438 get_sched_policy(0, &mPreviousSchedulingGroup);
439 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
440 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800442 status_t status = NO_ERROR;
443 if (!(flags & CBLK_INVALID)) {
444 status = mAudioTrack->start();
445 if (status == DEAD_OBJECT) {
446 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800448 }
449 if (flags & CBLK_INVALID) {
450 status = restoreTrack_l("start");
451 }
452
453 if (status != NO_ERROR) {
454 ALOGE("start() status %d", status);
455 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800456 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100457 if (previousState != STATE_STOPPING) {
458 t->pause();
459 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700461 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700462 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463 }
464 }
465
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100466 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800467}
468
469void AudioTrack::stop()
470{
471 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700472 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800473 return;
474 }
475
Glenn Kasten23a75452014-01-13 10:37:17 -0800476 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100477 mState = STATE_STOPPING;
478 } else {
479 mState = STATE_STOPPED;
480 }
481
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800482 mProxy->interrupt();
483 mAudioTrack->stop();
484 // the playback head position will reset to 0, so if a marker is set, we need
485 // to activate it again
486 mMarkerReached = false;
487#if 0
488 // Force flush if a shared buffer is used otherwise audioflinger
489 // will not stop before end of buffer is reached.
490 // It may be needed to make sure that we stop playback, likely in case looping is on.
491 if (mSharedBuffer != 0) {
492 flush_l();
493 }
494#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100495
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800496 sp<AudioTrackThread> t = mAudioTrackThread;
497 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800498 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100499 t->pause();
500 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800501 } else {
502 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
503 set_sched_policy(0, mPreviousSchedulingGroup);
504 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505}
506
507bool AudioTrack::stopped() const
508{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800509 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800510 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511}
512
513void AudioTrack::flush()
514{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800515 if (mSharedBuffer != 0) {
516 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800517 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800518 AutoMutex lock(mLock);
519 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
520 return;
521 }
522 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800523}
524
Eric Laurent1703cdf2011-03-07 14:52:59 -0800525void AudioTrack::flush_l()
526{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700528
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700529 // clear playback marker and periodic update counter
530 mMarkerPosition = 0;
531 mMarkerReached = false;
532 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100533 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700534
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800535 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800536 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100537 mProxy->interrupt();
538 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800539 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800540 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541}
542
543void AudioTrack::pause()
544{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800545 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100546 if (mState == STATE_ACTIVE) {
547 mState = STATE_PAUSED;
548 } else if (mState == STATE_STOPPING) {
549 mState = STATE_PAUSED_STOPPING;
550 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800551 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800553 mProxy->interrupt();
554 mAudioTrack->pause();
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800555
Marco Nelissen3a90f282014-03-10 11:21:43 -0700556 if (isOffloaded_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700557 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800558 uint32_t halFrames;
559 // OffloadThread sends HAL pause in its threadLoop.. time saved
560 // here can be slightly off
561 AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition);
562 ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition);
563 }
564 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565}
566
Eric Laurentbe916aa2010-06-01 23:49:17 -0700567status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800569 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700570 return BAD_VALUE;
571 }
572
Eric Laurent1703cdf2011-03-07 14:52:59 -0800573 AutoMutex lock(mLock);
Glenn Kasten66e46352014-01-16 17:44:23 -0800574 mVolume[AUDIO_INTERLEAVE_LEFT] = left;
575 mVolume[AUDIO_INTERLEAVE_RIGHT] = right;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576
Glenn Kastene3aa6592012-12-04 12:22:46 -0800577 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700578
Glenn Kasten23a75452014-01-13 10:37:17 -0800579 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700580 mAudioTrack->signal();
581 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700582 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583}
584
Glenn Kastenb1c09932012-02-27 16:21:04 -0800585status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800587 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700588}
589
Eric Laurent2beeb502010-07-16 07:43:46 -0700590status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700591{
Glenn Kasten05632a52012-01-03 14:22:33 -0800592 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700593 return BAD_VALUE;
594 }
595
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800596 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700597 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800598 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700599
600 return NO_ERROR;
601}
602
Glenn Kastena5224f32012-01-04 12:41:44 -0800603void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700604{
605 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800606 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700607 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608}
609
Glenn Kasten3b16c762012-11-14 08:44:39 -0800610status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100612 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800613 return INVALID_OPERATION;
614 }
615
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800616 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700618 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619 }
620 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700621 if (rate == 0 || rate > afSamplingRate*2 ) {
622 return BAD_VALUE;
623 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624
Eric Laurent1703cdf2011-03-07 14:52:59 -0800625 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800626 mSampleRate = rate;
627 mProxy->setSampleRate(rate);
628
Eric Laurent57326622009-07-07 07:10:45 -0700629 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630}
631
Glenn Kastena5224f32012-01-04 12:41:44 -0800632uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633{
John Grossman4ff14ba2012-02-08 16:37:41 -0800634 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800635 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800636 }
637
Eric Laurent1703cdf2011-03-07 14:52:59 -0800638 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700639
640 // sample rate can be updated during playback by the offloaded decoder so we need to
641 // query the HAL and update if needed.
642// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800643 if (isOffloaded_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700644 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700645 uint32_t sampleRate = 0;
646 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
647 if (status == NO_ERROR) {
648 mSampleRate = sampleRate;
649 }
650 }
651 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800652 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653}
654
655status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
656{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100657 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800658 return INVALID_OPERATION;
659 }
660
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800662 ;
663 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
664 loopEnd - loopStart >= MIN_LOOP) {
665 ;
666 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 return BAD_VALUE;
668 }
669
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800670 AutoMutex lock(mLock);
671 // See setPosition() regarding setting parameters such as loop points or position while active
672 if (mState == STATE_ACTIVE) {
673 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700674 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800675 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 return NO_ERROR;
677}
678
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800679void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
680{
681 // FIXME If setting a loop also sets position to start of loop, then
682 // this is correct. Otherwise it should be removed.
683 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
684 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
685 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
686}
687
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688status_t AudioTrack::setMarkerPosition(uint32_t marker)
689{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700690 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100691 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700692 return INVALID_OPERATION;
693 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800695 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700697 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698
699 return NO_ERROR;
700}
701
Glenn Kastena5224f32012-01-04 12:41:44 -0800702status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100704 if (isOffloaded()) {
705 return INVALID_OPERATION;
706 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700707 if (marker == NULL) {
708 return BAD_VALUE;
709 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800710
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800711 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712 *marker = mMarkerPosition;
713
714 return NO_ERROR;
715}
716
717status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
718{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700719 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100720 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700721 return INVALID_OPERATION;
722 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800723
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800724 AutoMutex lock(mLock);
725 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800727
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 return NO_ERROR;
729}
730
Glenn Kastena5224f32012-01-04 12:41:44 -0800731status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800732{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100733 if (isOffloaded()) {
734 return INVALID_OPERATION;
735 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700736 if (updatePeriod == NULL) {
737 return BAD_VALUE;
738 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800739
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800740 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741 *updatePeriod = mUpdatePeriod;
742
743 return NO_ERROR;
744}
745
746status_t AudioTrack::setPosition(uint32_t position)
747{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100748 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700749 return INVALID_OPERATION;
750 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800751 if (position > mFrameCount) {
752 return BAD_VALUE;
753 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800754
Eric Laurent1703cdf2011-03-07 14:52:59 -0800755 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800756 // Currently we require that the player is inactive before setting parameters such as position
757 // or loop points. Otherwise, there could be a race condition: the application could read the
758 // current position, compute a new position or loop parameters, and then set that position or
759 // loop parameters but it would do the "wrong" thing since the position has continued to advance
760 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
761 // to specify how it wants to handle such scenarios.
762 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700763 return INVALID_OPERATION;
764 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800765 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
766 mLoopPeriod = 0;
767 // FIXME Check whether loops and setting position are incompatible in old code.
768 // If we use setLoop for both purposes we lose the capability to set the position while looping.
769 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700770
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771 return NO_ERROR;
772}
773
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800774status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800775{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700776 if (position == NULL) {
777 return BAD_VALUE;
778 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800779
Eric Laurent1703cdf2011-03-07 14:52:59 -0800780 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800781 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100782 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800783
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800784 if ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING)) {
785 ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition);
786 *position = mPausedPosition;
787 return NO_ERROR;
788 }
789
Glenn Kasten142f5192014-03-25 17:44:59 -0700790 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100791 uint32_t halFrames;
792 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
793 }
794 *position = dspFrames;
795 } else {
796 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
797 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
798 mProxy->getPosition();
799 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800800 return NO_ERROR;
801}
802
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000803status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800804{
805 if (mSharedBuffer == 0 || mIsTimed) {
806 return INVALID_OPERATION;
807 }
808 if (position == NULL) {
809 return BAD_VALUE;
810 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800811
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800812 AutoMutex lock(mLock);
813 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800814 return NO_ERROR;
815}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800816
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800817status_t AudioTrack::reload()
818{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100819 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800820 return INVALID_OPERATION;
821 }
822
Eric Laurent1703cdf2011-03-07 14:52:59 -0800823 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800824 // See setPosition() regarding setting parameters such as loop points or position while active
825 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700826 return INVALID_OPERATION;
827 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800828 mNewPosition = mUpdatePeriod;
829 mLoopPeriod = 0;
830 // FIXME The new code cannot reload while keeping a loop specified.
831 // Need to check how the old code handled this, and whether it's a significant change.
832 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800833 return NO_ERROR;
834}
835
Glenn Kasten38e905b2014-01-13 10:21:48 -0800836audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700837{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800838 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100839 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800840}
841
Eric Laurentbe916aa2010-06-01 23:49:17 -0700842status_t AudioTrack::attachAuxEffect(int effectId)
843{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800844 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700845 status_t status = mAudioTrack->attachAuxEffect(effectId);
846 if (status == NO_ERROR) {
847 mAuxEffectId = effectId;
848 }
849 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700850}
851
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800852// -------------------------------------------------------------------------
853
Eric Laurent1703cdf2011-03-07 14:52:59 -0800854// must be called with mLock held
Glenn Kasten363fb752014-01-15 12:27:31 -0800855status_t AudioTrack::createTrack_l(size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800856{
857 status_t status;
858 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
859 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700860 ALOGE("Could not get audioflinger");
861 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800862 }
863
Glenn Kasten38e905b2014-01-13 10:21:48 -0800864 audio_io_handle_t output = AudioSystem::getOutput(mStreamType, mSampleRate, mFormat,
865 mChannelMask, mFlags, mOffloadInfo);
Glenn Kasten142f5192014-03-25 17:44:59 -0700866 if (output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasten38e905b2014-01-13 10:21:48 -0800867 ALOGE("Could not get audio output for stream type %d, sample rate %u, format %#x, "
868 "channel mask %#x, flags %#x",
869 mStreamType, mSampleRate, mFormat, mChannelMask, mFlags);
870 return BAD_VALUE;
871 }
872 {
873 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
874 // we must release it ourselves if anything goes wrong.
875
Glenn Kastence8828a2013-09-16 18:07:38 -0700876 // Not all of these values are needed under all conditions, but it is easier to get them all
877
Eric Laurentd1b449a2010-05-14 03:26:45 -0700878 uint32_t afLatency;
Glenn Kasten241618f2014-03-25 17:48:57 -0700879 status = AudioSystem::getLatency(output, &afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700880 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800881 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800882 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700883 }
884
Glenn Kastence8828a2013-09-16 18:07:38 -0700885 size_t afFrameCount;
Glenn Kasten363fb752014-01-15 12:27:31 -0800886 status = AudioSystem::getFrameCount(output, mStreamType, &afFrameCount);
Glenn Kastence8828a2013-09-16 18:07:38 -0700887 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800888 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, mStreamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800889 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700890 }
891
892 uint32_t afSampleRate;
Glenn Kasten363fb752014-01-15 12:27:31 -0800893 status = AudioSystem::getSamplingRate(output, mStreamType, &afSampleRate);
Glenn Kastence8828a2013-09-16 18:07:38 -0700894 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800895 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, mStreamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800896 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700897 }
898
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700899 // Client decides whether the track is TIMED (see below), but can only express a preference
900 // for FAST. Server will perform additional tests.
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800901 if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !((
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700902 // either of these use cases:
903 // use case 1: shared buffer
Glenn Kasten363fb752014-01-15 12:27:31 -0800904 (mSharedBuffer != 0) ||
Glenn Kastenc6ba8232014-02-27 13:34:29 -0800905 // use case 2: callback transfer mode
906 (mTransfer == TRANSFER_CALLBACK)) &&
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800907 // matching sample rate
908 (mSampleRate == afSampleRate))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800909 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700910 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -0800911 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700912 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700913 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700914
Glenn Kastence8828a2013-09-16 18:07:38 -0700915 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800916 // n = 1 fast track with single buffering; nBuffering is ignored
917 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700918 // n = 2 normal track, no sample rate conversion
919 // n = 3 normal track, with sample rate conversion
920 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
921 // n > 3 very high latency or very small notification interval; nBuffering is ignored
Glenn Kasten363fb752014-01-15 12:27:31 -0800922 const uint32_t nBuffering = (mSampleRate == afSampleRate) ? 2 : 3;
Glenn Kastence8828a2013-09-16 18:07:38 -0700923
Eric Laurentd1b449a2010-05-14 03:26:45 -0700924 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700925
Glenn Kasten363fb752014-01-15 12:27:31 -0800926 size_t frameCount = mReqFrameCount;
927 if (!audio_is_linear_pcm(mFormat)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700928
Glenn Kasten363fb752014-01-15 12:27:31 -0800929 if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700930 // Same comment as below about ignoring frameCount parameter for set()
Glenn Kasten363fb752014-01-15 12:27:31 -0800931 frameCount = mSharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700932 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700933 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700934 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100935 if (mNotificationFramesAct != frameCount) {
936 mNotificationFramesAct = frameCount;
937 }
Glenn Kasten363fb752014-01-15 12:27:31 -0800938 } else if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700939
Glenn Kastena42ff002012-11-14 12:47:55 -0800940 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700941 // 8-bit data in shared memory is not currently supported by AudioFlinger
Glenn Kastenb7730382014-04-30 15:50:31 -0700942 size_t alignment = audio_bytes_per_sample(
943 mFormat == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : mFormat);
944 if (alignment & 1) {
945 alignment = 1;
946 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800947 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700948 // More than 2 channels does not require stronger alignment than stereo
949 alignment <<= 1;
950 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000951 if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800952 ALOGE("Invalid buffer alignment: address %p, channel count %u",
Glenn Kasten363fb752014-01-15 12:27:31 -0800953 mSharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800954 status = BAD_VALUE;
955 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700956 }
957
958 // When initializing a shared buffer AudioTrack via constructors,
959 // there's no frameCount parameter.
960 // But when initializing a shared buffer AudioTrack via set(),
961 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastenb7730382014-04-30 15:50:31 -0700962 frameCount = mSharedBuffer->size() / mFrameSizeAF;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700963
Glenn Kasten363fb752014-01-15 12:27:31 -0800964 } else if (!(mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700965
966 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700967
Eric Laurentd1b449a2010-05-14 03:26:45 -0700968 // Ensure that buffer depth covers at least audio hardware latency
969 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700970 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
971 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700972 if (minBufCount <= nBuffering) {
973 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800974 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975
Glenn Kasten363fb752014-01-15 12:27:31 -0800976 size_t minFrameCount = (afFrameCount*mSampleRate*minBufCount)/afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -0800977 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800978 ", afLatency=%d",
Glenn Kasten363fb752014-01-15 12:27:31 -0800979 minFrameCount, afFrameCount, minBufCount, mSampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700980
981 if (frameCount == 0) {
982 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700983 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700984 // not ALOGW because it happens all the time when playing key clicks over A2DP
985 ALOGV("Minimum buffer size corrected from %d to %d",
986 frameCount, minFrameCount);
987 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800988 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700989 // Make sure that application is notified with sufficient margin before underrun
990 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
991 mNotificationFramesAct = frameCount/nBuffering;
992 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700993
Glenn Kastene0fa4672012-04-24 14:35:14 -0700994 } else {
995 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700996 }
997
Glenn Kastena075db42012-03-06 11:22:44 -0800998 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
999 if (mIsTimed) {
1000 trackFlags |= IAudioFlinger::TRACK_TIMED;
1001 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001002
1003 pid_t tid = -1;
Glenn Kasten363fb752014-01-15 12:27:31 -08001004 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001005 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001006 if (mAudioTrackThread != 0) {
1007 tid = mAudioTrackThread->getTid();
1008 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001009 }
1010
Glenn Kasten363fb752014-01-15 12:27:31 -08001011 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001012 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1013 }
1014
Glenn Kasten74935e42013-12-19 08:56:45 -08001015 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
1016 // but we will still need the original value also
Glenn Kasten363fb752014-01-15 12:27:31 -08001017 sp<IAudioTrack> track = audioFlinger->createTrack(mStreamType,
1018 mSampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001019 // AudioFlinger only sees 16-bit PCM
Glenn Kastenc4b88a82014-04-30 16:54:30 -07001020 mFormat == AUDIO_FORMAT_PCM_8_BIT &&
1021 !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT) ?
Glenn Kasten363fb752014-01-15 12:27:31 -08001022 AUDIO_FORMAT_PCM_16_BIT : mFormat,
Glenn Kastena42ff002012-11-14 12:47:55 -08001023 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001024 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -08001025 &trackFlags,
Glenn Kasten363fb752014-01-15 12:27:31 -08001026 mSharedBuffer,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001027 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001028 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001029 &mSessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001030 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001031 &status);
1032
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001033 if (status != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00001034 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001035 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001036 }
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001037 ALOG_ASSERT(track != 0);
1038
Glenn Kasten38e905b2014-01-13 10:21:48 -08001039 // AudioFlinger now owns the reference to the I/O handle,
1040 // so we are no longer responsible for releasing it.
1041
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001042 sp<IMemory> iMem = track->getCblk();
1043 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001044 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001045 return NO_INIT;
1046 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001047 void *iMemPointer = iMem->pointer();
1048 if (iMemPointer == NULL) {
1049 ALOGE("Could not get control block pointer");
1050 return NO_INIT;
1051 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001052 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001053 if (mAudioTrack != 0) {
1054 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1055 mDeathNotifier.clear();
1056 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001057 mAudioTrack = track;
Glenn Kasten5f631512014-02-24 15:16:07 -08001058
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001059 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001060 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001061 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001062 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb603744e2012-11-14 13:42:25 -08001063 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1064 // In current design, AudioTrack client checks and ensures frame count validity before
1065 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1066 // for fast track as it uses a special method of assigning frame count.
1067 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1068 }
1069 frameCount = temp;
Glenn Kasten5f631512014-02-24 15:16:07 -08001070
Glenn Kastena07f17c2013-04-23 12:39:37 -07001071 mAwaitBoost = false;
Glenn Kasten363fb752014-01-15 12:27:31 -08001072 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001073 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb603744e2012-11-14 13:42:25 -08001074 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001075 mAwaitBoost = true;
Glenn Kasten363fb752014-01-15 12:27:31 -08001076 if (mSharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001077 // Theoretically double-buffering is not required for fast tracks,
1078 // due to tighter scheduling. But in practice, to accommodate kernels with
1079 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1080 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1081 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001082 }
1083 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001084 } else {
Glenn Kastenb603744e2012-11-14 13:42:25 -08001085 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001086 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001087 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
1088 if (mSharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001089 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1090 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001091 }
1092 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001093 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001094 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001095 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001096 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1097 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1098 } else {
1099 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
Glenn Kasten363fb752014-01-15 12:27:31 -08001100 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001101 // FIXME This is a warning, not an error, so don't return error status
1102 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001103 }
1104 }
1105
Glenn Kasten38e905b2014-01-13 10:21:48 -08001106 // We retain a copy of the I/O handle, but don't own the reference
1107 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001108 mRefreshRemaining = true;
1109
1110 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1111 // is the value of pointer() for the shared buffer, otherwise buffers points
1112 // immediately after the control block. This address is for the mapping within client
1113 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1114 void* buffers;
Glenn Kasten363fb752014-01-15 12:27:31 -08001115 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001116 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001117 } else {
Glenn Kasten363fb752014-01-15 12:27:31 -08001118 buffers = mSharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001119 }
1120
Eric Laurent2beeb502010-07-16 07:43:46 -07001121 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001122 // FIXME don't believe this lie
Glenn Kasten363fb752014-01-15 12:27:31 -08001123 mLatency = afLatency + (1000*frameCount) / mSampleRate;
Glenn Kasten5f631512014-02-24 15:16:07 -08001124
Glenn Kastenb603744e2012-11-14 13:42:25 -08001125 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001126 // If IAudioTrack is re-created, don't let the requested frameCount
1127 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb603744e2012-11-14 13:42:25 -08001128 if (frameCount > mReqFrameCount) {
1129 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001130 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001131
1132 // update proxy
Glenn Kasten363fb752014-01-15 12:27:31 -08001133 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001134 mStaticProxy.clear();
1135 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1136 } else {
1137 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1138 mProxy = mStaticProxy;
1139 }
Glenn Kasten66e46352014-01-16 17:44:23 -08001140 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[AUDIO_INTERLEAVE_RIGHT] * 0x1000)) << 16) |
1141 uint16_t(mVolume[AUDIO_INTERLEAVE_LEFT] * 0x1000));
Glenn Kastene3aa6592012-12-04 12:22:46 -08001142 mProxy->setSendLevel(mSendLevel);
1143 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001144 mProxy->setEpoch(epoch);
1145 mProxy->setMinimum(mNotificationFramesAct);
1146
1147 mDeathNotifier = new DeathNotifier(this);
1148 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001149
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001150 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001151 }
1152
1153release:
1154 AudioSystem::releaseOutput(output);
1155 if (status == NO_ERROR) {
1156 status = NO_INIT;
1157 }
1158 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001159}
1160
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001161status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1162{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001163 if (audioBuffer == NULL) {
1164 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001165 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001166 if (mTransfer != TRANSFER_OBTAIN) {
1167 audioBuffer->frameCount = 0;
1168 audioBuffer->size = 0;
1169 audioBuffer->raw = NULL;
1170 return INVALID_OPERATION;
1171 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001172
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001173 const struct timespec *requested;
Eric Laurentdf576992014-01-27 18:13:39 -08001174 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001175 if (waitCount == -1) {
1176 requested = &ClientProxy::kForever;
1177 } else if (waitCount == 0) {
1178 requested = &ClientProxy::kNonBlocking;
1179 } else if (waitCount > 0) {
1180 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001181 timeout.tv_sec = ms / 1000;
1182 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1183 requested = &timeout;
1184 } else {
1185 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1186 requested = NULL;
1187 }
1188 return obtainBuffer(audioBuffer, requested);
1189}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001190
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001191status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1192 struct timespec *elapsed, size_t *nonContig)
1193{
1194 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1195 uint32_t oldSequence = 0;
1196 uint32_t newSequence;
1197
1198 Proxy::Buffer buffer;
1199 status_t status = NO_ERROR;
1200
1201 static const int32_t kMaxTries = 5;
1202 int32_t tryCounter = kMaxTries;
1203
1204 do {
1205 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1206 // keep them from going away if another thread re-creates the track during obtainBuffer()
1207 sp<AudioTrackClientProxy> proxy;
1208 sp<IMemory> iMem;
1209
1210 { // start of lock scope
1211 AutoMutex lock(mLock);
1212
1213 newSequence = mSequence;
1214 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1215 if (status == DEAD_OBJECT) {
1216 // re-create track, unless someone else has already done so
1217 if (newSequence == oldSequence) {
1218 status = restoreTrack_l("obtainBuffer");
1219 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001220 buffer.mFrameCount = 0;
1221 buffer.mRaw = NULL;
1222 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001223 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001224 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225 }
1226 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001227 oldSequence = newSequence;
1228
1229 // Keep the extra references
1230 proxy = mProxy;
1231 iMem = mCblkMemory;
1232
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001233 if (mState == STATE_STOPPING) {
1234 status = -EINTR;
1235 buffer.mFrameCount = 0;
1236 buffer.mRaw = NULL;
1237 buffer.mNonContig = 0;
1238 break;
1239 }
1240
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001241 // Non-blocking if track is stopped or paused
1242 if (mState != STATE_ACTIVE) {
1243 requested = &ClientProxy::kNonBlocking;
1244 }
1245
1246 } // end of lock scope
1247
1248 buffer.mFrameCount = audioBuffer->frameCount;
1249 // FIXME starts the requested timeout and elapsed over from scratch
1250 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1251
1252 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1253
1254 audioBuffer->frameCount = buffer.mFrameCount;
1255 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1256 audioBuffer->raw = buffer.mRaw;
1257 if (nonContig != NULL) {
1258 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001260 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261}
1262
1263void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1264{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001265 if (mTransfer == TRANSFER_SHARED) {
1266 return;
1267 }
1268
1269 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1270 if (stepCount == 0) {
1271 return;
1272 }
1273
1274 Proxy::Buffer buffer;
1275 buffer.mFrameCount = stepCount;
1276 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001277
Eric Laurent1703cdf2011-03-07 14:52:59 -08001278 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001279 mInUnderrun = false;
1280 mProxy->releaseBuffer(&buffer);
1281
1282 // restart track if it was disabled by audioflinger due to previous underrun
1283 if (mState == STATE_ACTIVE) {
1284 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001285 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastenc5a17422014-03-13 14:59:59 -07001286 ALOGW("releaseBuffer() track %p disabled due to previous underrun, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001287 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001288 mAudioTrack->start();
1289 }
1290 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291}
1292
1293// -------------------------------------------------------------------------
1294
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001295ssize_t AudioTrack::write(const void* buffer, size_t userSize, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001296{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001297 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001298 return INVALID_OPERATION;
1299 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001301 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001302 // Sanity-check: user is most-likely passing an error code, and it would
1303 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001304 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 return BAD_VALUE;
1306 }
1307
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001308 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001309 Buffer audioBuffer;
1310
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001311 while (userSize >= mFrameSize) {
1312 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001313
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001314 status_t err = obtainBuffer(&audioBuffer,
1315 blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001317 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001318 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001319 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001320 return ssize_t(err);
1321 }
1322
1323 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001324 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001325 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001326 toWrite = audioBuffer.size >> 1;
1327 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001328 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001329 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001330 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001331 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001332 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001333 userSize -= toWrite;
1334 written += toWrite;
1335
1336 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001337 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001338
1339 return written;
1340}
1341
1342// -------------------------------------------------------------------------
1343
John Grossman4ff14ba2012-02-08 16:37:41 -08001344TimedAudioTrack::TimedAudioTrack() {
1345 mIsTimed = true;
1346}
1347
1348status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1349{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001350 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001351 status_t result = UNKNOWN_ERROR;
1352
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001353#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001354 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1355 // while we are accessing the cblk
1356 sp<IAudioTrack> audioTrack = mAudioTrack;
1357 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001358#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001359
John Grossman4ff14ba2012-02-08 16:37:41 -08001360 // If the track is not invalid already, try to allocate a buffer. alloc
1361 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001362 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001363 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001364 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001365 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1366 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001367 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001368 }
1369 }
1370
1371 // If the track is invalid at this point, attempt to restore it. and try the
1372 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001373 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001374 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001375
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001376 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001377 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001378 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001379 }
1380
1381 return result;
1382}
1383
1384status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1385 int64_t pts)
1386{
Eric Laurentdf839842012-05-31 14:27:14 -07001387 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1388 {
1389 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001390 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001391 // restart track if it was disabled by audioflinger due to previous underrun
1392 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001393 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1394 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001395 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001396 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001397 mAudioTrack->start();
1398 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001399 }
Eric Laurentdf839842012-05-31 14:27:14 -07001400 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001401}
1402
1403status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1404 TargetTimeline target)
1405{
1406 return mAudioTrack->setMediaTimeTransform(xform, target);
1407}
1408
1409// -------------------------------------------------------------------------
1410
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001411nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001412{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001413 // Currently the AudioTrack thread is not created if there are no callbacks.
1414 // Would it ever make sense to run the thread, even without callbacks?
1415 // If so, then replace this by checks at each use for mCbf != NULL.
1416 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1417
Eric Laurent1703cdf2011-03-07 14:52:59 -08001418 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001419 if (mAwaitBoost) {
1420 mAwaitBoost = false;
1421 mLock.unlock();
1422 static const int32_t kMaxTries = 5;
1423 int32_t tryCounter = kMaxTries;
1424 uint32_t pollUs = 10000;
1425 do {
1426 int policy = sched_getscheduler(0);
1427 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1428 break;
1429 }
1430 usleep(pollUs);
1431 pollUs <<= 1;
1432 } while (tryCounter-- > 0);
1433 if (tryCounter < 0) {
1434 ALOGE("did not receive expected priority boost on time");
1435 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001436 // Run again immediately
1437 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001438 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001439
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001440 // Can only reference mCblk while locked
1441 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001442 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001443
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001444 // Check for track invalidation
1445 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001446 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1447 // AudioSystem cache. We should not exit here but after calling the callback so
1448 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001449 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001450 status_t status = restoreTrack_l("processAudioBuffer");
1451 mLock.unlock();
1452 // Run again immediately, but with a new IAudioTrack
1453 return 0;
1454 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001455 }
1456
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001457 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001458 bool active = mState == STATE_ACTIVE;
1459
1460 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1461 bool newUnderrun = false;
1462 if (flags & CBLK_UNDERRUN) {
1463#if 0
1464 // Currently in shared buffer mode, when the server reaches the end of buffer,
1465 // the track stays active in continuous underrun state. It's up to the application
1466 // to pause or stop the track, or set the position to a new offset within buffer.
1467 // This was some experimental code to auto-pause on underrun. Keeping it here
1468 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1469 if (mTransfer == TRANSFER_SHARED) {
1470 mState = STATE_PAUSED;
1471 active = false;
1472 }
1473#endif
1474 if (!mInUnderrun) {
1475 mInUnderrun = true;
1476 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001477 }
1478 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001479
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001480 // Get current position of server
1481 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001482
1483 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001484 bool markerReached = false;
1485 size_t markerPosition = mMarkerPosition;
1486 // FIXME fails for wraparound, need 64 bits
1487 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1488 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001489 }
1490
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001491 // Determine number of new position callback(s) that will be needed, while locked
1492 size_t newPosCount = 0;
1493 size_t newPosition = mNewPosition;
1494 size_t updatePeriod = mUpdatePeriod;
1495 // FIXME fails for wraparound, need 64 bits
1496 if (updatePeriod > 0 && position >= newPosition) {
1497 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1498 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001499 }
1500
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001501 // Cache other fields that will be needed soon
1502 uint32_t loopPeriod = mLoopPeriod;
1503 uint32_t sampleRate = mSampleRate;
Glenn Kasten838b3d82014-02-27 15:30:41 -08001504 uint32_t notificationFrames = mNotificationFramesAct;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001505 if (mRefreshRemaining) {
1506 mRefreshRemaining = false;
1507 mRemainingFrames = notificationFrames;
1508 mRetryOnPartialBuffer = false;
1509 }
1510 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001511 uint32_t sequence = mSequence;
Glenn Kasten96f04882013-09-20 09:28:56 -07001512 sp<AudioTrackClientProxy> proxy = mProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001513
1514 // These fields don't need to be cached, because they are assigned only by set():
1515 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1516 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1517
1518 mLock.unlock();
1519
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001520 if (waitStreamEnd) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001521 struct timespec timeout;
1522 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1523 timeout.tv_nsec = 0;
1524
Glenn Kasten96f04882013-09-20 09:28:56 -07001525 status_t status = proxy->waitStreamEndDone(&timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001526 switch (status) {
1527 case NO_ERROR:
1528 case DEAD_OBJECT:
1529 case TIMED_OUT:
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001530 mCbf(EVENT_STREAM_END, mUserData, NULL);
Glenn Kasten96f04882013-09-20 09:28:56 -07001531 {
1532 AutoMutex lock(mLock);
1533 // The previously assigned value of waitStreamEnd is no longer valid,
1534 // since the mutex has been unlocked and either the callback handler
1535 // or another thread could have re-started the AudioTrack during that time.
1536 waitStreamEnd = mState == STATE_STOPPING;
1537 if (waitStreamEnd) {
1538 mState = STATE_STOPPED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001539 }
1540 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001541 if (waitStreamEnd && status != DEAD_OBJECT) {
1542 return NS_INACTIVE;
1543 }
1544 break;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001545 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001546 return 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001547 }
1548
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001549 // perform callbacks while unlocked
1550 if (newUnderrun) {
1551 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1552 }
1553 // FIXME we will miss loops if loop cycle was signaled several times since last call
1554 // to processAudioBuffer()
1555 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1556 mCbf(EVENT_LOOP_END, mUserData, NULL);
1557 }
1558 if (flags & CBLK_BUFFER_END) {
1559 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1560 }
1561 if (markerReached) {
1562 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1563 }
1564 while (newPosCount > 0) {
1565 size_t temp = newPosition;
1566 mCbf(EVENT_NEW_POS, mUserData, &temp);
1567 newPosition += updatePeriod;
1568 newPosCount--;
1569 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001570
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001571 if (mObservedSequence != sequence) {
1572 mObservedSequence = sequence;
1573 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001574 // for offloaded tracks, just wait for the upper layers to recreate the track
1575 if (isOffloaded()) {
1576 return NS_INACTIVE;
1577 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001578 }
1579
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001580 // if inactive, then don't run me again until re-started
1581 if (!active) {
1582 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001583 }
1584
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001585 // Compute the estimated time until the next timed event (position, markers, loops)
1586 // FIXME only for non-compressed audio
1587 uint32_t minFrames = ~0;
1588 if (!markerReached && position < markerPosition) {
1589 minFrames = markerPosition - position;
1590 }
1591 if (loopPeriod > 0 && loopPeriod < minFrames) {
1592 minFrames = loopPeriod;
1593 }
1594 if (updatePeriod > 0 && updatePeriod < minFrames) {
1595 minFrames = updatePeriod;
1596 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001597
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001598 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1599 static const uint32_t kPoll = 0;
1600 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1601 minFrames = kPoll * notificationFrames;
1602 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001603
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001604 // Convert frame units to time units
1605 nsecs_t ns = NS_WHENEVER;
1606 if (minFrames != (uint32_t) ~0) {
1607 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1608 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1609 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1610 }
1611
1612 // If not supplying data by EVENT_MORE_DATA, then we're done
1613 if (mTransfer != TRANSFER_CALLBACK) {
1614 return ns;
1615 }
1616
1617 struct timespec timeout;
1618 const struct timespec *requested = &ClientProxy::kForever;
1619 if (ns != NS_WHENEVER) {
1620 timeout.tv_sec = ns / 1000000000LL;
1621 timeout.tv_nsec = ns % 1000000000LL;
1622 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1623 requested = &timeout;
1624 }
1625
1626 while (mRemainingFrames > 0) {
1627
1628 Buffer audioBuffer;
1629 audioBuffer.frameCount = mRemainingFrames;
1630 size_t nonContig;
1631 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1632 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1633 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1634 requested = &ClientProxy::kNonBlocking;
1635 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001636 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1637 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001638 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001639 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1640 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001641 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001643 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1644 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001646
Eric Laurent42a6f422013-08-29 14:35:05 -07001647 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001648 mRetryOnPartialBuffer = false;
1649 if (avail < mRemainingFrames) {
1650 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1651 if (ns < 0 || myns < ns) {
1652 ns = myns;
1653 }
1654 return ns;
1655 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001656 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001657
1658 // Divide buffer size by 2 to take into account the expansion
1659 // due to 8 to 16 bit conversion: the callback must fill only half
1660 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001661 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001662 audioBuffer.size >>= 1;
1663 }
1664
1665 size_t reqSize = audioBuffer.size;
1666 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001667 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001668
1669 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001670 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1671 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1672 reqSize, (int) writtenSize);
1673 return NS_NEVER;
1674 }
1675
1676 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001677 // The callback is done filling buffers
1678 // Keep this thread going to handle timed events and
1679 // still try to get more data in intervals of WAIT_PERIOD_MS
1680 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001681 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001682 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001683
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001684 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001685 // 8 to 16 bit conversion, note that source and destination are the same address
1686 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001687 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001688 }
1689
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001690 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1691 audioBuffer.frameCount = releasedFrames;
1692 mRemainingFrames -= releasedFrames;
1693 if (misalignment >= releasedFrames) {
1694 misalignment -= releasedFrames;
1695 } else {
1696 misalignment = 0;
1697 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001698
1699 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001700
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001701 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1702 // if callback doesn't like to accept the full chunk
1703 if (writtenSize < reqSize) {
1704 continue;
1705 }
1706
1707 // There could be enough non-contiguous frames available to satisfy the remaining request
1708 if (mRemainingFrames <= nonContig) {
1709 continue;
1710 }
1711
1712#if 0
1713 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1714 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1715 // that total to a sum == notificationFrames.
1716 if (0 < misalignment && misalignment <= mRemainingFrames) {
1717 mRemainingFrames = misalignment;
1718 return (mRemainingFrames * 1100000000LL) / sampleRate;
1719 }
1720#endif
1721
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001722 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001723 mRemainingFrames = notificationFrames;
1724 mRetryOnPartialBuffer = true;
1725
1726 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1727 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001728}
1729
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001730status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001731{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001732 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001733 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001734 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001735 status_t result;
1736
Glenn Kastena47f3162012-11-07 10:13:08 -08001737 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kasten38e905b2014-01-13 10:21:48 -08001738 // output parameters in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001739 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001740
Glenn Kasten23a75452014-01-13 10:37:17 -08001741 if (isOffloaded_l()) {
1742 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001743 return DEAD_OBJECT;
1744 }
1745
Glenn Kastena47f3162012-11-07 10:13:08 -08001746 // if the new IAudioTrack is created, createTrack_l() will modify the
1747 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1748 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001749
1750 // take the frames that will be lost by track recreation into account in saved position
1751 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001752 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kasten363fb752014-01-15 12:27:31 -08001753 result = createTrack_l(position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001754
Glenn Kastena47f3162012-11-07 10:13:08 -08001755 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001756 // continue playback from last known position, but
1757 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1758 if (mStaticProxy != NULL) {
1759 mLoopPeriod = 0;
1760 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1761 }
1762 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1763 // track destruction have been played? This is critical for SoundPool implementation
1764 // This must be broken, and needs to be tested/debugged.
1765#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001766 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001767 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001768 // Make sure that a client relying on callback events indicating underrun or
1769 // the actual amount of audio frames played (e.g SoundPool) receives them.
1770 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001771 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001772 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001773 }
1774 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001775#endif
1776 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001777 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001778 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001779 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001780 if (result != NO_ERROR) {
1781 ALOGW("restoreTrack_l() failed status %d", result);
1782 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001783 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001784
1785 return result;
1786}
1787
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001788status_t AudioTrack::setParameters(const String8& keyValuePairs)
1789{
1790 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001791 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001792}
1793
Glenn Kastence703742013-07-19 16:33:58 -07001794status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1795{
Glenn Kasten53cec222013-08-29 09:01:02 -07001796 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001797 // FIXME not implemented for fast tracks; should use proxy and SSQ
1798 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1799 return INVALID_OPERATION;
1800 }
1801 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1802 return INVALID_OPERATION;
1803 }
1804 status_t status = mAudioTrack->getTimestamp(timestamp);
1805 if (status == NO_ERROR) {
1806 timestamp.mPosition += mProxy->getEpoch();
1807 }
1808 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001809}
1810
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001811String8 AudioTrack::getParameters(const String8& keys)
1812{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001813 audio_io_handle_t output = getOutput();
Glenn Kasten142f5192014-03-25 17:44:59 -07001814 if (output != AUDIO_IO_HANDLE_NONE) {
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001815 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001816 } else {
1817 return String8::empty();
1818 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001819}
1820
Glenn Kasten23a75452014-01-13 10:37:17 -08001821bool AudioTrack::isOffloaded() const
1822{
1823 AutoMutex lock(mLock);
1824 return isOffloaded_l();
1825}
1826
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001827status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001828{
1829
1830 const size_t SIZE = 256;
1831 char buffer[SIZE];
1832 String8 result;
1833
1834 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001835 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
Glenn Kasten877a0ac2014-04-30 17:04:13 -07001836 mVolume[AUDIO_INTERLEAVE_LEFT], mVolume[AUDIO_INTERLEAVE_RIGHT]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001837 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001838 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb603744e2012-11-14 13:42:25 -08001839 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001840 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001841 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001842 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001843 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001844 result.append(buffer);
1845 ::write(fd, result.string(), result.size());
1846 return NO_ERROR;
1847}
1848
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001849uint32_t AudioTrack::getUnderrunFrames() const
1850{
1851 AutoMutex lock(mLock);
1852 return mProxy->getUnderrunFrames();
1853}
1854
1855// =========================================================================
1856
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001857void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001858{
1859 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1860 if (audioTrack != 0) {
1861 AutoMutex lock(audioTrack->mLock);
1862 audioTrack->mProxy->binderDied();
1863 }
1864}
1865
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001866// =========================================================================
1867
1868AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001869 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1870 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001871{
1872}
1873
1874AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001875{
1876}
1877
1878bool AudioTrack::AudioTrackThread::threadLoop()
1879{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001880 {
1881 AutoMutex _l(mMyLock);
1882 if (mPaused) {
1883 mMyCond.wait(mMyLock);
1884 // caller will check for exitPending()
1885 return true;
1886 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001887 if (mIgnoreNextPausedInt) {
1888 mIgnoreNextPausedInt = false;
1889 mPausedInt = false;
1890 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001891 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001892 if (mPausedNs > 0) {
1893 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1894 } else {
1895 mMyCond.wait(mMyLock);
1896 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001897 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001898 return true;
1899 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001900 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001901 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001902 switch (ns) {
1903 case 0:
1904 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001905 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001906 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001907 return true;
1908 case NS_NEVER:
1909 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001910 case NS_WHENEVER:
1911 // FIXME increase poll interval, or make event-driven
1912 ns = 1000000000LL;
1913 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001914 default:
1915 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001916 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001917 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001918 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001919}
1920
Glenn Kasten3acbd052012-02-28 10:39:56 -08001921void AudioTrack::AudioTrackThread::requestExit()
1922{
1923 // must be in this order to avoid a race condition
1924 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001925 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001926}
1927
1928void AudioTrack::AudioTrackThread::pause()
1929{
1930 AutoMutex _l(mMyLock);
1931 mPaused = true;
1932}
1933
1934void AudioTrack::AudioTrackThread::resume()
1935{
1936 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001937 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001938 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001939 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001940 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001941 mMyCond.signal();
1942 }
1943}
1944
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001945void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1946{
1947 AutoMutex _l(mMyLock);
1948 mPausedInt = true;
1949 mPausedNs = ns;
1950}
1951
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001952}; // namespace android