blob: e6827ee49c6a0f14f6423311506fd510376f79e9 [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
Glenn Kastenc56f3422014-03-21 17:53:17 -070022#include <math.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080023#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080024#include <audio_utils/primitives.h>
25#include <binder/IPCThreadState.h>
26#include <media/AudioTrack.h>
27#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070029#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080030
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010031#define WAIT_PERIOD_MS 10
32#define WAIT_STREAM_END_TIMEOUT_SEC 120
33
Glenn Kasten511754b2012-01-11 09:52:19 -080034
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080036// ---------------------------------------------------------------------------
37
38// static
39status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080040 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080041 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080042 uint32_t sampleRate)
43{
Glenn Kastend65d73c2012-06-22 17:21:07 -070044 if (frameCount == NULL) {
45 return BAD_VALUE;
46 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070047
Glenn Kastene0fa4672012-04-24 14:35:14 -070048 // FIXME merge with similar code in createTrack_l(), except we're missing
49 // some information here that is available in createTrack_l():
50 // audio_io_handle_t output
51 // audio_format_t format
52 // audio_channel_mask_t channelMask
53 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080054 uint32_t afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080055 status_t status;
56 status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
57 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080058 ALOGE("Unable to query output sample rate for stream type %d; status %d",
59 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080060 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080061 }
Glenn Kastene33054e2012-11-14 12:54:39 -080062 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080063 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
64 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080065 ALOGE("Unable to query output frame count for stream type %d; status %d",
66 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080067 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 }
69 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080070 status = AudioSystem::getOutputLatency(&afLatency, streamType);
71 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080072 ALOGE("Unable to query output latency for stream type %d; status %d",
73 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080074 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080075 }
76
77 // Ensure that buffer depth covers at least audio hardware latency
78 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080079 if (minBufCount < 2) {
80 minBufCount = 2;
81 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080082
83 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070084 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080085 // The formula above should always produce a non-zero value, but return an error
86 // in the unlikely event that it does not, as that's part of the API contract.
87 if (*frameCount == 0) {
88 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d",
89 streamType, sampleRate);
90 return BAD_VALUE;
91 }
Glenn Kasten3acbd052012-02-28 10:39:56 -080092 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
93 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080094 return NO_ERROR;
95}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096
97// ---------------------------------------------------------------------------
98
99AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -0700100 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800101 mIsTimed(false),
102 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800103 mPreviousSchedulingGroup(SP_DEFAULT),
104 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105{
106}
107
108AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800109 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800111 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700112 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800113 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700114 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115 callback_t cbf,
116 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800117 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800118 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000119 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800120 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800121 int uid,
122 pid_t pid)
Glenn Kasten87913512011-06-22 16:15:25 -0700123 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800124 mIsTimed(false),
125 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800126 mPreviousSchedulingGroup(SP_DEFAULT),
127 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800128{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700129 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700130 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800131 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
Marco Nelissend457c972014-02-11 08:47:07 -0800132 offloadInfo, uid, pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800133}
134
Andreas Huberc8139852012-01-18 10:51:55 -0800135AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800136 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800138 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700139 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700141 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 callback_t cbf,
143 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800144 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800145 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000146 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800147 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800148 int uid,
149 pid_t pid)
Glenn Kasten87913512011-06-22 16:15:25 -0700150 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800151 mIsTimed(false),
152 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800153 mPreviousSchedulingGroup(SP_DEFAULT),
154 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700156 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800157 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissend457c972014-02-11 08:47:07 -0800158 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo,
159 uid, pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160}
161
162AudioTrack::~AudioTrack()
163{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 if (mStatus == NO_ERROR) {
165 // Make sure that callback function exits in the case where
166 // it is looping on buffer full condition in obtainBuffer().
167 // Otherwise the callback thread will never exit.
168 stop();
169 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100170 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800171 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 mAudioTrackThread->requestExitAndWait();
173 mAudioTrackThread.clear();
174 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700175 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
176 mAudioTrack.clear();
Eric Laurent3bcffa12014-06-12 18:38:45 -0700177 mCblkMemory.clear();
178 mSharedBuffer.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179 IPCThreadState::self()->flushCommands();
Marco Nelissend457c972014-02-11 08:47:07 -0800180 ALOGV("~AudioTrack, releasing session id from %d on behalf of %d",
181 IPCThreadState::self()->getCallingPid(), mClientPid);
182 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 }
184}
185
186status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800187 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800189 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700190 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800191 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700192 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 callback_t cbf,
194 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800195 uint32_t notificationFrames,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700197 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800198 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000199 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800200 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800201 int uid,
202 pid_t pid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203{
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800204 ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
Glenn Kasten838b3d82014-02-27 15:30:41 -0800205 "flags #%x, notificationFrames %u, sessionId %d, transferType %d",
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800206 streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames,
Glenn Kasten86f04662014-02-24 15:13:05 -0800207 sessionId, transferType);
208
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800209 switch (transferType) {
210 case TRANSFER_DEFAULT:
211 if (sharedBuffer != 0) {
212 transferType = TRANSFER_SHARED;
213 } else if (cbf == NULL || threadCanCallJava) {
214 transferType = TRANSFER_SYNC;
215 } else {
216 transferType = TRANSFER_CALLBACK;
217 }
218 break;
219 case TRANSFER_CALLBACK:
220 if (cbf == NULL || sharedBuffer != 0) {
221 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
222 return BAD_VALUE;
223 }
224 break;
225 case TRANSFER_OBTAIN:
226 case TRANSFER_SYNC:
227 if (sharedBuffer != 0) {
228 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
229 return BAD_VALUE;
230 }
231 break;
232 case TRANSFER_SHARED:
233 if (sharedBuffer == 0) {
234 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
235 return BAD_VALUE;
236 }
237 break;
238 default:
239 ALOGE("Invalid transfer type %d", transferType);
240 return BAD_VALUE;
241 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800242 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800243 mTransfer = transferType;
244
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700245 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
246 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247
Glenn Kastene33054e2012-11-14 12:54:39 -0800248 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700249
Eric Laurent1703cdf2011-03-07 14:52:59 -0800250 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800251
Glenn Kasten53cec222013-08-29 09:01:02 -0700252 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700253 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000254 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 return INVALID_OPERATION;
256 }
257
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 if (streamType == AUDIO_STREAM_DEFAULT) {
260 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800262 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
263 ALOGE("Invalid stream type %d", streamType);
264 return BAD_VALUE;
265 }
266 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700267
Glenn Kastenb1bef512014-01-13 10:25:53 -0800268 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800270 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
271 if (status != NO_ERROR) {
272 ALOGE("Could not get output sample rate for stream type %d; status %d",
273 streamType, status);
274 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700275 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800277 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700278
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800280 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700281 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800283
284 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700285 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800286 ALOGE("Invalid format %#x", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 return BAD_VALUE;
288 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800289 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290
Glenn Kasten8ba90322013-10-30 11:29:27 -0700291 if (!audio_is_output_channel(channelMask)) {
292 ALOGE("Invalid channel mask %#x", channelMask);
293 return BAD_VALUE;
294 }
Glenn Kastene3247bf2014-02-24 15:19:07 -0800295 mChannelMask = channelMask;
Andy Hunge5412692014-05-16 11:25:07 -0700296 uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastene3247bf2014-02-24 15:19:07 -0800297 mChannelCount = channelCount;
Glenn Kasten8ba90322013-10-30 11:29:27 -0700298
Glenn Kastene0fa4672012-04-24 14:35:14 -0700299 // AudioFlinger does not currently support 8-bit data in shared memory
300 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
301 ALOGE("8-bit data in shared memory is not supported");
302 return BAD_VALUE;
303 }
304
Eric Laurentc2f1f072009-07-17 12:17:14 -0700305 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100306 // or offload was requested
307 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
308 || !audio_is_linear_pcm(format)) {
309 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
310 ? "Offload request, forcing to Direct Output"
311 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700312 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800313 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700314 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700316 // only allow deep buffering for music stream type
317 if (streamType != AUDIO_STREAM_MUSIC) {
318 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
319 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700320
Glenn Kastenb7730382014-04-30 15:50:31 -0700321 if (flags & AUDIO_OUTPUT_FLAG_DIRECT) {
322 if (audio_is_linear_pcm(format)) {
323 mFrameSize = channelCount * audio_bytes_per_sample(format);
324 } else {
325 mFrameSize = sizeof(uint8_t);
326 }
327 mFrameSizeAF = mFrameSize;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800328 } else {
Glenn Kastenb7730382014-04-30 15:50:31 -0700329 ALOG_ASSERT(audio_is_linear_pcm(format));
330 mFrameSize = channelCount * audio_bytes_per_sample(format);
331 mFrameSizeAF = channelCount * audio_bytes_per_sample(
332 format == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : format);
333 // createTrack will return an error if PCM format is not supported by server,
334 // so no need to check for specific PCM formats here
Glenn Kastene3aa6592012-12-04 12:22:46 -0800335 }
336
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800337 // Make copy of input parameter offloadInfo so that in the future:
338 // (a) createTrack_l doesn't need it as an input parameter
339 // (b) we can support re-creation of offloaded tracks
340 if (offloadInfo != NULL) {
341 mOffloadInfoCopy = *offloadInfo;
342 mOffloadInfo = &mOffloadInfoCopy;
343 } else {
344 mOffloadInfo = NULL;
345 }
346
Glenn Kasten66e46352014-01-16 17:44:23 -0800347 mVolume[AUDIO_INTERLEAVE_LEFT] = 1.0f;
348 mVolume[AUDIO_INTERLEAVE_RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800349 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800350 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800351 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700352 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800353 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700354 mSessionId = sessionId;
Marco Nelissend457c972014-02-11 08:47:07 -0800355 int callingpid = IPCThreadState::self()->getCallingPid();
356 int mypid = getpid();
357 if (uid == -1 || (callingpid != mypid)) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800358 mClientUid = IPCThreadState::self()->getCallingUid();
359 } else {
360 mClientUid = uid;
361 }
Marco Nelissend457c972014-02-11 08:47:07 -0800362 if (pid == -1 || (callingpid != mypid)) {
363 mClientPid = callingpid;
364 } else {
365 mClientPid = pid;
366 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700367 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700368 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700369 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700370
Glenn Kastena997e7a2012-08-07 09:44:19 -0700371 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700372 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700373 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
374 }
375
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800376 // create the IAudioTrack
Glenn Kasten363fb752014-01-15 12:27:31 -0800377 status = createTrack_l(0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800378
Glenn Kastena997e7a2012-08-07 09:44:19 -0700379 if (status != NO_ERROR) {
380 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100381 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
382 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700383 mAudioTrackThread.clear();
384 }
385 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700386 }
387
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800391 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700393 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 mNewPosition = 0;
395 mUpdatePeriod = 0;
Marco Nelissend457c972014-02-11 08:47:07 -0800396 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800397 mSequence = 1;
398 mObservedSequence = mSequence;
399 mInUnderrun = false;
400
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 return NO_ERROR;
402}
403
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404// -------------------------------------------------------------------------
405
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100406status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800408 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100409
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800410 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100411 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 }
413
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800414 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800416 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100417 if (previousState == STATE_PAUSED_STOPPING) {
418 mState = STATE_STOPPING;
419 } else {
420 mState = STATE_ACTIVE;
421 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800422 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
423 // reset current position as seen by client to 0
424 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700425 // force refresh of remaining frames by processAudioBuffer() as last
426 // write before stop could be partial.
427 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800428 }
429 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700430 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800433 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100434 if (previousState == STATE_STOPPING) {
435 mProxy->interrupt();
436 } else {
437 t->resume();
438 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800439 } else {
440 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
441 get_sched_policy(0, &mPreviousSchedulingGroup);
442 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
443 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800445 status_t status = NO_ERROR;
446 if (!(flags & CBLK_INVALID)) {
447 status = mAudioTrack->start();
448 if (status == DEAD_OBJECT) {
449 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800451 }
452 if (flags & CBLK_INVALID) {
453 status = restoreTrack_l("start");
454 }
455
456 if (status != NO_ERROR) {
457 ALOGE("start() status %d", status);
458 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100460 if (previousState != STATE_STOPPING) {
461 t->pause();
462 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700464 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700465 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 }
467 }
468
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100469 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800470}
471
472void AudioTrack::stop()
473{
474 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700475 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800476 return;
477 }
478
Glenn Kasten23a75452014-01-13 10:37:17 -0800479 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100480 mState = STATE_STOPPING;
481 } else {
482 mState = STATE_STOPPED;
483 }
484
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800485 mProxy->interrupt();
486 mAudioTrack->stop();
487 // the playback head position will reset to 0, so if a marker is set, we need
488 // to activate it again
489 mMarkerReached = false;
490#if 0
491 // Force flush if a shared buffer is used otherwise audioflinger
492 // will not stop before end of buffer is reached.
493 // It may be needed to make sure that we stop playback, likely in case looping is on.
494 if (mSharedBuffer != 0) {
495 flush_l();
496 }
497#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100498
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800499 sp<AudioTrackThread> t = mAudioTrackThread;
500 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800501 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100502 t->pause();
503 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800504 } else {
505 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
506 set_sched_policy(0, mPreviousSchedulingGroup);
507 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508}
509
510bool AudioTrack::stopped() const
511{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800512 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800513 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514}
515
516void AudioTrack::flush()
517{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800518 if (mSharedBuffer != 0) {
519 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800520 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800521 AutoMutex lock(mLock);
522 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
523 return;
524 }
525 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800526}
527
Eric Laurent1703cdf2011-03-07 14:52:59 -0800528void AudioTrack::flush_l()
529{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800530 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700531
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700532 // clear playback marker and periodic update counter
533 mMarkerPosition = 0;
534 mMarkerReached = false;
535 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100536 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700537
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800538 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800539 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100540 mProxy->interrupt();
541 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800542 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800543 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544}
545
546void AudioTrack::pause()
547{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800548 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100549 if (mState == STATE_ACTIVE) {
550 mState = STATE_PAUSED;
551 } else if (mState == STATE_STOPPING) {
552 mState = STATE_PAUSED_STOPPING;
553 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800554 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800556 mProxy->interrupt();
557 mAudioTrack->pause();
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800558
Marco Nelissen3a90f282014-03-10 11:21:43 -0700559 if (isOffloaded_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700560 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800561 uint32_t halFrames;
562 // OffloadThread sends HAL pause in its threadLoop.. time saved
563 // here can be slightly off
564 AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition);
565 ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition);
566 }
567 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568}
569
Eric Laurentbe916aa2010-06-01 23:49:17 -0700570status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571{
Glenn Kastenc56f3422014-03-21 17:53:17 -0700572 // This duplicates a test by AudioTrack JNI, but that is not the only caller
573 if (isnanf(left) || left < GAIN_FLOAT_ZERO || left > GAIN_FLOAT_UNITY ||
574 isnanf(right) || right < GAIN_FLOAT_ZERO || right > GAIN_FLOAT_UNITY) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700575 return BAD_VALUE;
576 }
577
Eric Laurent1703cdf2011-03-07 14:52:59 -0800578 AutoMutex lock(mLock);
Glenn Kasten66e46352014-01-16 17:44:23 -0800579 mVolume[AUDIO_INTERLEAVE_LEFT] = left;
580 mVolume[AUDIO_INTERLEAVE_RIGHT] = right;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581
Glenn Kastenc56f3422014-03-21 17:53:17 -0700582 mProxy->setVolumeLR(gain_minifloat_pack(gain_from_float(left), gain_from_float(right)));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700583
Glenn Kasten23a75452014-01-13 10:37:17 -0800584 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700585 mAudioTrack->signal();
586 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588}
589
Glenn Kastenb1c09932012-02-27 16:21:04 -0800590status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800592 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700593}
594
Eric Laurent2beeb502010-07-16 07:43:46 -0700595status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700596{
Glenn Kastenc56f3422014-03-21 17:53:17 -0700597 // This duplicates a test by AudioTrack JNI, but that is not the only caller
598 if (isnanf(level) || level < GAIN_FLOAT_ZERO || level > GAIN_FLOAT_UNITY) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700599 return BAD_VALUE;
600 }
601
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800602 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700603 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800604 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700605
606 return NO_ERROR;
607}
608
Glenn Kastena5224f32012-01-04 12:41:44 -0800609void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700610{
611 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800612 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700613 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614}
615
Glenn Kasten3b16c762012-11-14 08:44:39 -0800616status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100618 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800619 return INVALID_OPERATION;
620 }
621
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800622 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700624 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625 }
626 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700627 if (rate == 0 || rate > afSamplingRate*2 ) {
628 return BAD_VALUE;
629 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630
Eric Laurent1703cdf2011-03-07 14:52:59 -0800631 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800632 mSampleRate = rate;
633 mProxy->setSampleRate(rate);
634
Eric Laurent57326622009-07-07 07:10:45 -0700635 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636}
637
Glenn Kastena5224f32012-01-04 12:41:44 -0800638uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639{
John Grossman4ff14ba2012-02-08 16:37:41 -0800640 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800641 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800642 }
643
Eric Laurent1703cdf2011-03-07 14:52:59 -0800644 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700645
646 // sample rate can be updated during playback by the offloaded decoder so we need to
647 // query the HAL and update if needed.
648// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800649 if (isOffloaded_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700650 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700651 uint32_t sampleRate = 0;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700652 status_t status = AudioSystem::getSamplingRate(mOutput, &sampleRate);
Eric Laurent6f59db12013-07-26 17:16:50 -0700653 if (status == NO_ERROR) {
654 mSampleRate = sampleRate;
655 }
656 }
657 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800658 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659}
660
661status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
662{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100663 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800664 return INVALID_OPERATION;
665 }
666
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800668 ;
669 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
670 loopEnd - loopStart >= MIN_LOOP) {
671 ;
672 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673 return BAD_VALUE;
674 }
675
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800676 AutoMutex lock(mLock);
677 // See setPosition() regarding setting parameters such as loop points or position while active
678 if (mState == STATE_ACTIVE) {
679 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700680 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800681 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682 return NO_ERROR;
683}
684
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800685void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
686{
687 // FIXME If setting a loop also sets position to start of loop, then
688 // this is correct. Otherwise it should be removed.
689 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
690 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
691 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
692}
693
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694status_t AudioTrack::setMarkerPosition(uint32_t marker)
695{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700696 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100697 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700698 return INVALID_OPERATION;
699 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800701 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700703 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704
705 return NO_ERROR;
706}
707
Glenn Kastena5224f32012-01-04 12:41:44 -0800708status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100710 if (isOffloaded()) {
711 return INVALID_OPERATION;
712 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700713 if (marker == NULL) {
714 return BAD_VALUE;
715 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800717 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718 *marker = mMarkerPosition;
719
720 return NO_ERROR;
721}
722
723status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
724{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700725 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100726 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700727 return INVALID_OPERATION;
728 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800730 AutoMutex lock(mLock);
731 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800732 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800733
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800734 return NO_ERROR;
735}
736
Glenn Kastena5224f32012-01-04 12:41:44 -0800737status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100739 if (isOffloaded()) {
740 return INVALID_OPERATION;
741 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700742 if (updatePeriod == NULL) {
743 return BAD_VALUE;
744 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800745
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800746 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800747 *updatePeriod = mUpdatePeriod;
748
749 return NO_ERROR;
750}
751
752status_t AudioTrack::setPosition(uint32_t position)
753{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100754 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700755 return INVALID_OPERATION;
756 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800757 if (position > mFrameCount) {
758 return BAD_VALUE;
759 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800760
Eric Laurent1703cdf2011-03-07 14:52:59 -0800761 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800762 // Currently we require that the player is inactive before setting parameters such as position
763 // or loop points. Otherwise, there could be a race condition: the application could read the
764 // current position, compute a new position or loop parameters, and then set that position or
765 // loop parameters but it would do the "wrong" thing since the position has continued to advance
766 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
767 // to specify how it wants to handle such scenarios.
768 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700769 return INVALID_OPERATION;
770 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800771 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
772 mLoopPeriod = 0;
773 // FIXME Check whether loops and setting position are incompatible in old code.
774 // If we use setLoop for both purposes we lose the capability to set the position while looping.
775 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700776
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800777 return NO_ERROR;
778}
779
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800780status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800781{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700782 if (position == NULL) {
783 return BAD_VALUE;
784 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800785
Eric Laurent1703cdf2011-03-07 14:52:59 -0800786 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800787 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100788 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800789
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800790 if ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING)) {
791 ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition);
792 *position = mPausedPosition;
793 return NO_ERROR;
794 }
795
Glenn Kasten142f5192014-03-25 17:44:59 -0700796 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100797 uint32_t halFrames;
798 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
799 }
800 *position = dspFrames;
801 } else {
802 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
803 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
804 mProxy->getPosition();
805 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800806 return NO_ERROR;
807}
808
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000809status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800810{
811 if (mSharedBuffer == 0 || mIsTimed) {
812 return INVALID_OPERATION;
813 }
814 if (position == NULL) {
815 return BAD_VALUE;
816 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800817
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800818 AutoMutex lock(mLock);
819 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800820 return NO_ERROR;
821}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800822
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823status_t AudioTrack::reload()
824{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100825 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800826 return INVALID_OPERATION;
827 }
828
Eric Laurent1703cdf2011-03-07 14:52:59 -0800829 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800830 // See setPosition() regarding setting parameters such as loop points or position while active
831 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700832 return INVALID_OPERATION;
833 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800834 mNewPosition = mUpdatePeriod;
835 mLoopPeriod = 0;
836 // FIXME The new code cannot reload while keeping a loop specified.
837 // Need to check how the old code handled this, and whether it's a significant change.
838 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800839 return NO_ERROR;
840}
841
Glenn Kasten38e905b2014-01-13 10:21:48 -0800842audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700843{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800844 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100845 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800846}
847
Eric Laurentbe916aa2010-06-01 23:49:17 -0700848status_t AudioTrack::attachAuxEffect(int effectId)
849{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800850 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700851 status_t status = mAudioTrack->attachAuxEffect(effectId);
852 if (status == NO_ERROR) {
853 mAuxEffectId = effectId;
854 }
855 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700856}
857
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800858// -------------------------------------------------------------------------
859
Eric Laurent1703cdf2011-03-07 14:52:59 -0800860// must be called with mLock held
Glenn Kasten363fb752014-01-15 12:27:31 -0800861status_t AudioTrack::createTrack_l(size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800862{
863 status_t status;
864 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
865 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700866 ALOGE("Could not get audioflinger");
867 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800868 }
869
Glenn Kasten38e905b2014-01-13 10:21:48 -0800870 audio_io_handle_t output = AudioSystem::getOutput(mStreamType, mSampleRate, mFormat,
871 mChannelMask, mFlags, mOffloadInfo);
Glenn Kasten142f5192014-03-25 17:44:59 -0700872 if (output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasten38e905b2014-01-13 10:21:48 -0800873 ALOGE("Could not get audio output for stream type %d, sample rate %u, format %#x, "
874 "channel mask %#x, flags %#x",
875 mStreamType, mSampleRate, mFormat, mChannelMask, mFlags);
876 return BAD_VALUE;
877 }
878 {
879 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
880 // we must release it ourselves if anything goes wrong.
881
Glenn Kastence8828a2013-09-16 18:07:38 -0700882 // Not all of these values are needed under all conditions, but it is easier to get them all
883
Eric Laurentd1b449a2010-05-14 03:26:45 -0700884 uint32_t afLatency;
Glenn Kasten241618f2014-03-25 17:48:57 -0700885 status = AudioSystem::getLatency(output, &afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700886 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800887 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800888 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700889 }
890
Glenn Kastence8828a2013-09-16 18:07:38 -0700891 size_t afFrameCount;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700892 status = AudioSystem::getFrameCount(output, &afFrameCount);
Glenn Kastence8828a2013-09-16 18:07:38 -0700893 if (status != NO_ERROR) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700894 ALOGE("getFrameCount(output=%d) status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800895 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700896 }
897
898 uint32_t afSampleRate;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700899 status = AudioSystem::getSamplingRate(output, &afSampleRate);
Glenn Kastence8828a2013-09-16 18:07:38 -0700900 if (status != NO_ERROR) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700901 ALOGE("getSamplingRate(output=%d) status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800902 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700903 }
904
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700905 // Client decides whether the track is TIMED (see below), but can only express a preference
906 // for FAST. Server will perform additional tests.
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800907 if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !((
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700908 // either of these use cases:
909 // use case 1: shared buffer
Glenn Kasten363fb752014-01-15 12:27:31 -0800910 (mSharedBuffer != 0) ||
Glenn Kastenc6ba8232014-02-27 13:34:29 -0800911 // use case 2: callback transfer mode
912 (mTransfer == TRANSFER_CALLBACK)) &&
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800913 // matching sample rate
914 (mSampleRate == afSampleRate))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800915 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700916 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -0800917 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700918 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700919 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700920
Glenn Kastence8828a2013-09-16 18:07:38 -0700921 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800922 // n = 1 fast track with single buffering; nBuffering is ignored
923 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700924 // n = 2 normal track, no sample rate conversion
925 // n = 3 normal track, with sample rate conversion
926 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
927 // n > 3 very high latency or very small notification interval; nBuffering is ignored
Glenn Kasten363fb752014-01-15 12:27:31 -0800928 const uint32_t nBuffering = (mSampleRate == afSampleRate) ? 2 : 3;
Glenn Kastence8828a2013-09-16 18:07:38 -0700929
Eric Laurentd1b449a2010-05-14 03:26:45 -0700930 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700931
Glenn Kasten363fb752014-01-15 12:27:31 -0800932 size_t frameCount = mReqFrameCount;
933 if (!audio_is_linear_pcm(mFormat)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934
Glenn Kasten363fb752014-01-15 12:27:31 -0800935 if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700936 // Same comment as below about ignoring frameCount parameter for set()
Glenn Kasten363fb752014-01-15 12:27:31 -0800937 frameCount = mSharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700938 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700939 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700940 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100941 if (mNotificationFramesAct != frameCount) {
942 mNotificationFramesAct = frameCount;
943 }
Glenn Kasten363fb752014-01-15 12:27:31 -0800944 } else if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700945
Glenn Kastena42ff002012-11-14 12:47:55 -0800946 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700947 // 8-bit data in shared memory is not currently supported by AudioFlinger
Glenn Kastenb7730382014-04-30 15:50:31 -0700948 size_t alignment = audio_bytes_per_sample(
949 mFormat == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : mFormat);
950 if (alignment & 1) {
951 alignment = 1;
952 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800953 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700954 // More than 2 channels does not require stronger alignment than stereo
955 alignment <<= 1;
956 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000957 if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800958 ALOGE("Invalid buffer alignment: address %p, channel count %u",
Glenn Kasten363fb752014-01-15 12:27:31 -0800959 mSharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800960 status = BAD_VALUE;
961 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700962 }
963
964 // When initializing a shared buffer AudioTrack via constructors,
965 // there's no frameCount parameter.
966 // But when initializing a shared buffer AudioTrack via set(),
967 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastenb7730382014-04-30 15:50:31 -0700968 frameCount = mSharedBuffer->size() / mFrameSizeAF;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700969
Glenn Kasten363fb752014-01-15 12:27:31 -0800970 } else if (!(mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700971
972 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700973
Eric Laurentd1b449a2010-05-14 03:26:45 -0700974 // Ensure that buffer depth covers at least audio hardware latency
975 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700976 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
977 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700978 if (minBufCount <= nBuffering) {
979 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800980 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700981
Glenn Kasten363fb752014-01-15 12:27:31 -0800982 size_t minFrameCount = (afFrameCount*mSampleRate*minBufCount)/afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -0800983 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800984 ", afLatency=%d",
Glenn Kasten363fb752014-01-15 12:27:31 -0800985 minFrameCount, afFrameCount, minBufCount, mSampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700986
987 if (frameCount == 0) {
988 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700989 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700990 // not ALOGW because it happens all the time when playing key clicks over A2DP
991 ALOGV("Minimum buffer size corrected from %d to %d",
992 frameCount, minFrameCount);
993 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800994 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700995 // Make sure that application is notified with sufficient margin before underrun
996 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
997 mNotificationFramesAct = frameCount/nBuffering;
998 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700999
Glenn Kastene0fa4672012-04-24 14:35:14 -07001000 } else {
1001 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -07001002 }
1003
Glenn Kastena075db42012-03-06 11:22:44 -08001004 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
1005 if (mIsTimed) {
1006 trackFlags |= IAudioFlinger::TRACK_TIMED;
1007 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001008
1009 pid_t tid = -1;
Glenn Kasten363fb752014-01-15 12:27:31 -08001010 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001011 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001012 if (mAudioTrackThread != 0) {
1013 tid = mAudioTrackThread->getTid();
1014 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001015 }
1016
Glenn Kasten363fb752014-01-15 12:27:31 -08001017 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001018 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1019 }
1020
Glenn Kasten74935e42013-12-19 08:56:45 -08001021 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
1022 // but we will still need the original value also
Glenn Kasten363fb752014-01-15 12:27:31 -08001023 sp<IAudioTrack> track = audioFlinger->createTrack(mStreamType,
1024 mSampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001025 // AudioFlinger only sees 16-bit PCM
Glenn Kastenc4b88a82014-04-30 16:54:30 -07001026 mFormat == AUDIO_FORMAT_PCM_8_BIT &&
1027 !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT) ?
Glenn Kasten363fb752014-01-15 12:27:31 -08001028 AUDIO_FORMAT_PCM_16_BIT : mFormat,
Glenn Kastena42ff002012-11-14 12:47:55 -08001029 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001030 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -08001031 &trackFlags,
Glenn Kasten363fb752014-01-15 12:27:31 -08001032 mSharedBuffer,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001033 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001034 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001035 &mSessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001036 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001037 &status);
1038
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001039 if (status != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00001040 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001041 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001042 }
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001043 ALOG_ASSERT(track != 0);
1044
Glenn Kasten38e905b2014-01-13 10:21:48 -08001045 // AudioFlinger now owns the reference to the I/O handle,
1046 // so we are no longer responsible for releasing it.
1047
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001048 sp<IMemory> iMem = track->getCblk();
1049 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001050 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001051 return NO_INIT;
1052 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001053 void *iMemPointer = iMem->pointer();
1054 if (iMemPointer == NULL) {
1055 ALOGE("Could not get control block pointer");
1056 return NO_INIT;
1057 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001058 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001059 if (mAudioTrack != 0) {
1060 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1061 mDeathNotifier.clear();
1062 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001063 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001064 mCblkMemory = iMem;
Eric Laurent3bcffa12014-06-12 18:38:45 -07001065 IPCThreadState::self()->flushCommands();
1066
Glenn Kasten0cde0762014-01-16 15:06:36 -08001067 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001068 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001069 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001070 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1071 // In current design, AudioTrack client checks and ensures frame count validity before
1072 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1073 // for fast track as it uses a special method of assigning frame count.
1074 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1075 }
1076 frameCount = temp;
Glenn Kasten5f631512014-02-24 15:16:07 -08001077
Glenn Kastena07f17c2013-04-23 12:39:37 -07001078 mAwaitBoost = false;
Glenn Kasten363fb752014-01-15 12:27:31 -08001079 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001080 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001081 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001082 mAwaitBoost = true;
Glenn Kasten363fb752014-01-15 12:27:31 -08001083 if (mSharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001084 // Theoretically double-buffering is not required for fast tracks,
1085 // due to tighter scheduling. But in practice, to accommodate kernels with
1086 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1087 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1088 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001089 }
1090 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001091 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001092 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001093 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001094 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
1095 if (mSharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001096 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1097 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001098 }
1099 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001100 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001101 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001102 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001103 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1104 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1105 } else {
1106 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
Glenn Kasten363fb752014-01-15 12:27:31 -08001107 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001108 // FIXME This is a warning, not an error, so don't return error status
1109 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001110 }
1111 }
1112
Glenn Kasten38e905b2014-01-13 10:21:48 -08001113 // We retain a copy of the I/O handle, but don't own the reference
1114 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001115 mRefreshRemaining = true;
1116
1117 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1118 // is the value of pointer() for the shared buffer, otherwise buffers points
1119 // immediately after the control block. This address is for the mapping within client
1120 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1121 void* buffers;
Glenn Kasten363fb752014-01-15 12:27:31 -08001122 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001123 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001124 } else {
Glenn Kasten363fb752014-01-15 12:27:31 -08001125 buffers = mSharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001126 }
1127
Eric Laurent2beeb502010-07-16 07:43:46 -07001128 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001129 // FIXME don't believe this lie
Glenn Kasten363fb752014-01-15 12:27:31 -08001130 mLatency = afLatency + (1000*frameCount) / mSampleRate;
Glenn Kasten5f631512014-02-24 15:16:07 -08001131
Glenn Kastenb6037442012-11-14 13:42:25 -08001132 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001133 // If IAudioTrack is re-created, don't let the requested frameCount
1134 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001135 if (frameCount > mReqFrameCount) {
1136 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001137 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001138
1139 // update proxy
Glenn Kasten363fb752014-01-15 12:27:31 -08001140 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001141 mStaticProxy.clear();
1142 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1143 } else {
1144 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1145 mProxy = mStaticProxy;
1146 }
Glenn Kastenc56f3422014-03-21 17:53:17 -07001147 mProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001148 mProxy->setSendLevel(mSendLevel);
1149 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001150 mProxy->setEpoch(epoch);
1151 mProxy->setMinimum(mNotificationFramesAct);
1152
1153 mDeathNotifier = new DeathNotifier(this);
1154 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001155
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001156 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001157 }
1158
1159release:
1160 AudioSystem::releaseOutput(output);
1161 if (status == NO_ERROR) {
1162 status = NO_INIT;
1163 }
1164 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001165}
1166
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1168{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001169 if (audioBuffer == NULL) {
1170 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001171 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001172 if (mTransfer != TRANSFER_OBTAIN) {
1173 audioBuffer->frameCount = 0;
1174 audioBuffer->size = 0;
1175 audioBuffer->raw = NULL;
1176 return INVALID_OPERATION;
1177 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001178
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001179 const struct timespec *requested;
Eric Laurentdf576992014-01-27 18:13:39 -08001180 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001181 if (waitCount == -1) {
1182 requested = &ClientProxy::kForever;
1183 } else if (waitCount == 0) {
1184 requested = &ClientProxy::kNonBlocking;
1185 } else if (waitCount > 0) {
1186 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001187 timeout.tv_sec = ms / 1000;
1188 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1189 requested = &timeout;
1190 } else {
1191 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1192 requested = NULL;
1193 }
1194 return obtainBuffer(audioBuffer, requested);
1195}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001196
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001197status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1198 struct timespec *elapsed, size_t *nonContig)
1199{
1200 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1201 uint32_t oldSequence = 0;
1202 uint32_t newSequence;
1203
1204 Proxy::Buffer buffer;
1205 status_t status = NO_ERROR;
1206
1207 static const int32_t kMaxTries = 5;
1208 int32_t tryCounter = kMaxTries;
1209
1210 do {
1211 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1212 // keep them from going away if another thread re-creates the track during obtainBuffer()
1213 sp<AudioTrackClientProxy> proxy;
1214 sp<IMemory> iMem;
1215
1216 { // start of lock scope
1217 AutoMutex lock(mLock);
1218
1219 newSequence = mSequence;
1220 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1221 if (status == DEAD_OBJECT) {
1222 // re-create track, unless someone else has already done so
1223 if (newSequence == oldSequence) {
1224 status = restoreTrack_l("obtainBuffer");
1225 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001226 buffer.mFrameCount = 0;
1227 buffer.mRaw = NULL;
1228 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001229 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001231 }
1232 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001233 oldSequence = newSequence;
1234
1235 // Keep the extra references
1236 proxy = mProxy;
1237 iMem = mCblkMemory;
1238
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001239 if (mState == STATE_STOPPING) {
1240 status = -EINTR;
1241 buffer.mFrameCount = 0;
1242 buffer.mRaw = NULL;
1243 buffer.mNonContig = 0;
1244 break;
1245 }
1246
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001247 // Non-blocking if track is stopped or paused
1248 if (mState != STATE_ACTIVE) {
1249 requested = &ClientProxy::kNonBlocking;
1250 }
1251
1252 } // end of lock scope
1253
1254 buffer.mFrameCount = audioBuffer->frameCount;
1255 // FIXME starts the requested timeout and elapsed over from scratch
1256 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1257
1258 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1259
1260 audioBuffer->frameCount = buffer.mFrameCount;
1261 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1262 audioBuffer->raw = buffer.mRaw;
1263 if (nonContig != NULL) {
1264 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001266 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267}
1268
1269void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1270{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001271 if (mTransfer == TRANSFER_SHARED) {
1272 return;
1273 }
1274
1275 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1276 if (stepCount == 0) {
1277 return;
1278 }
1279
1280 Proxy::Buffer buffer;
1281 buffer.mFrameCount = stepCount;
1282 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001283
Eric Laurent1703cdf2011-03-07 14:52:59 -08001284 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001285 mInUnderrun = false;
1286 mProxy->releaseBuffer(&buffer);
1287
1288 // restart track if it was disabled by audioflinger due to previous underrun
1289 if (mState == STATE_ACTIVE) {
1290 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001291 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastenc5a17422014-03-13 14:59:59 -07001292 ALOGW("releaseBuffer() track %p disabled due to previous underrun, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001293 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001294 mAudioTrack->start();
1295 }
1296 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297}
1298
1299// -------------------------------------------------------------------------
1300
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001301ssize_t AudioTrack::write(const void* buffer, size_t userSize, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001303 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001304 return INVALID_OPERATION;
1305 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001307 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001308 // Sanity-check: user is most-likely passing an error code, and it would
1309 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001310 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001311 return BAD_VALUE;
1312 }
1313
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001314 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 Buffer audioBuffer;
1316
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001317 while (userSize >= mFrameSize) {
1318 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001319
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001320 status_t err = obtainBuffer(&audioBuffer,
1321 blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001322 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001323 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001324 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001325 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326 return ssize_t(err);
1327 }
1328
1329 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001330 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001331 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001332 toWrite = audioBuffer.size >> 1;
1333 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001334 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001335 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001336 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001337 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001338 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001339 userSize -= toWrite;
1340 written += toWrite;
1341
1342 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001343 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001344
1345 return written;
1346}
1347
1348// -------------------------------------------------------------------------
1349
John Grossman4ff14ba2012-02-08 16:37:41 -08001350TimedAudioTrack::TimedAudioTrack() {
1351 mIsTimed = true;
1352}
1353
1354status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1355{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001356 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001357 status_t result = UNKNOWN_ERROR;
1358
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001359#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001360 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1361 // while we are accessing the cblk
1362 sp<IAudioTrack> audioTrack = mAudioTrack;
1363 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001364#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001365
John Grossman4ff14ba2012-02-08 16:37:41 -08001366 // If the track is not invalid already, try to allocate a buffer. alloc
1367 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001368 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001369 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001370 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001371 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1372 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001373 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001374 }
1375 }
1376
1377 // If the track is invalid at this point, attempt to restore it. and try the
1378 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001379 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001380 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001381
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001382 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001383 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001384 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001385 }
1386
1387 return result;
1388}
1389
1390status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1391 int64_t pts)
1392{
Eric Laurentdf839842012-05-31 14:27:14 -07001393 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1394 {
1395 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001396 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001397 // restart track if it was disabled by audioflinger due to previous underrun
1398 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001399 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1400 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001401 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001402 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001403 mAudioTrack->start();
1404 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001405 }
Eric Laurentdf839842012-05-31 14:27:14 -07001406 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001407}
1408
1409status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1410 TargetTimeline target)
1411{
1412 return mAudioTrack->setMediaTimeTransform(xform, target);
1413}
1414
1415// -------------------------------------------------------------------------
1416
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001417nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001418{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001419 // Currently the AudioTrack thread is not created if there are no callbacks.
1420 // Would it ever make sense to run the thread, even without callbacks?
1421 // If so, then replace this by checks at each use for mCbf != NULL.
1422 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1423
Eric Laurent1703cdf2011-03-07 14:52:59 -08001424 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001425 if (mAwaitBoost) {
1426 mAwaitBoost = false;
1427 mLock.unlock();
1428 static const int32_t kMaxTries = 5;
1429 int32_t tryCounter = kMaxTries;
1430 uint32_t pollUs = 10000;
1431 do {
1432 int policy = sched_getscheduler(0);
1433 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1434 break;
1435 }
1436 usleep(pollUs);
1437 pollUs <<= 1;
1438 } while (tryCounter-- > 0);
1439 if (tryCounter < 0) {
1440 ALOGE("did not receive expected priority boost on time");
1441 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001442 // Run again immediately
1443 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001444 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001445
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001446 // Can only reference mCblk while locked
1447 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001448 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001449
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001450 // Check for track invalidation
1451 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001452 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1453 // AudioSystem cache. We should not exit here but after calling the callback so
1454 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001455 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001456 status_t status = restoreTrack_l("processAudioBuffer");
1457 mLock.unlock();
1458 // Run again immediately, but with a new IAudioTrack
1459 return 0;
1460 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001461 }
1462
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001463 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001464 bool active = mState == STATE_ACTIVE;
1465
1466 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1467 bool newUnderrun = false;
1468 if (flags & CBLK_UNDERRUN) {
1469#if 0
1470 // Currently in shared buffer mode, when the server reaches the end of buffer,
1471 // the track stays active in continuous underrun state. It's up to the application
1472 // to pause or stop the track, or set the position to a new offset within buffer.
1473 // This was some experimental code to auto-pause on underrun. Keeping it here
1474 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1475 if (mTransfer == TRANSFER_SHARED) {
1476 mState = STATE_PAUSED;
1477 active = false;
1478 }
1479#endif
1480 if (!mInUnderrun) {
1481 mInUnderrun = true;
1482 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001483 }
1484 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001485
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001486 // Get current position of server
1487 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001488
1489 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001490 bool markerReached = false;
1491 size_t markerPosition = mMarkerPosition;
1492 // FIXME fails for wraparound, need 64 bits
1493 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1494 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001495 }
1496
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001497 // Determine number of new position callback(s) that will be needed, while locked
1498 size_t newPosCount = 0;
1499 size_t newPosition = mNewPosition;
1500 size_t updatePeriod = mUpdatePeriod;
1501 // FIXME fails for wraparound, need 64 bits
1502 if (updatePeriod > 0 && position >= newPosition) {
1503 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1504 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505 }
1506
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001507 // Cache other fields that will be needed soon
1508 uint32_t loopPeriod = mLoopPeriod;
1509 uint32_t sampleRate = mSampleRate;
Glenn Kasten838b3d82014-02-27 15:30:41 -08001510 uint32_t notificationFrames = mNotificationFramesAct;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001511 if (mRefreshRemaining) {
1512 mRefreshRemaining = false;
1513 mRemainingFrames = notificationFrames;
1514 mRetryOnPartialBuffer = false;
1515 }
1516 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001517 uint32_t sequence = mSequence;
Glenn Kasten96f04882013-09-20 09:28:56 -07001518 sp<AudioTrackClientProxy> proxy = mProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001519
1520 // These fields don't need to be cached, because they are assigned only by set():
1521 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1522 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1523
1524 mLock.unlock();
1525
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001526 if (waitStreamEnd) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001527 struct timespec timeout;
1528 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1529 timeout.tv_nsec = 0;
1530
Glenn Kasten96f04882013-09-20 09:28:56 -07001531 status_t status = proxy->waitStreamEndDone(&timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001532 switch (status) {
1533 case NO_ERROR:
1534 case DEAD_OBJECT:
1535 case TIMED_OUT:
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001536 mCbf(EVENT_STREAM_END, mUserData, NULL);
Glenn Kasten96f04882013-09-20 09:28:56 -07001537 {
1538 AutoMutex lock(mLock);
1539 // The previously assigned value of waitStreamEnd is no longer valid,
1540 // since the mutex has been unlocked and either the callback handler
1541 // or another thread could have re-started the AudioTrack during that time.
1542 waitStreamEnd = mState == STATE_STOPPING;
1543 if (waitStreamEnd) {
1544 mState = STATE_STOPPED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001545 }
1546 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001547 if (waitStreamEnd && status != DEAD_OBJECT) {
1548 return NS_INACTIVE;
1549 }
1550 break;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001551 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001552 return 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001553 }
1554
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001555 // perform callbacks while unlocked
1556 if (newUnderrun) {
1557 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1558 }
1559 // FIXME we will miss loops if loop cycle was signaled several times since last call
1560 // to processAudioBuffer()
1561 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1562 mCbf(EVENT_LOOP_END, mUserData, NULL);
1563 }
1564 if (flags & CBLK_BUFFER_END) {
1565 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1566 }
1567 if (markerReached) {
1568 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1569 }
1570 while (newPosCount > 0) {
1571 size_t temp = newPosition;
1572 mCbf(EVENT_NEW_POS, mUserData, &temp);
1573 newPosition += updatePeriod;
1574 newPosCount--;
1575 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001576
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001577 if (mObservedSequence != sequence) {
1578 mObservedSequence = sequence;
1579 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001580 // for offloaded tracks, just wait for the upper layers to recreate the track
1581 if (isOffloaded()) {
1582 return NS_INACTIVE;
1583 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001584 }
1585
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001586 // if inactive, then don't run me again until re-started
1587 if (!active) {
1588 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001589 }
1590
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001591 // Compute the estimated time until the next timed event (position, markers, loops)
1592 // FIXME only for non-compressed audio
1593 uint32_t minFrames = ~0;
1594 if (!markerReached && position < markerPosition) {
1595 minFrames = markerPosition - position;
1596 }
1597 if (loopPeriod > 0 && loopPeriod < minFrames) {
1598 minFrames = loopPeriod;
1599 }
1600 if (updatePeriod > 0 && updatePeriod < minFrames) {
1601 minFrames = updatePeriod;
1602 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001604 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1605 static const uint32_t kPoll = 0;
1606 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1607 minFrames = kPoll * notificationFrames;
1608 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001609
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001610 // Convert frame units to time units
1611 nsecs_t ns = NS_WHENEVER;
1612 if (minFrames != (uint32_t) ~0) {
1613 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1614 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1615 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1616 }
1617
1618 // If not supplying data by EVENT_MORE_DATA, then we're done
1619 if (mTransfer != TRANSFER_CALLBACK) {
1620 return ns;
1621 }
1622
1623 struct timespec timeout;
1624 const struct timespec *requested = &ClientProxy::kForever;
1625 if (ns != NS_WHENEVER) {
1626 timeout.tv_sec = ns / 1000000000LL;
1627 timeout.tv_nsec = ns % 1000000000LL;
1628 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1629 requested = &timeout;
1630 }
1631
1632 while (mRemainingFrames > 0) {
1633
1634 Buffer audioBuffer;
1635 audioBuffer.frameCount = mRemainingFrames;
1636 size_t nonContig;
1637 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1638 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1639 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1640 requested = &ClientProxy::kNonBlocking;
1641 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001642 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1643 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001644 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001645 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1646 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001647 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001649 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1650 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001651 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001652
Eric Laurent42a6f422013-08-29 14:35:05 -07001653 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001654 mRetryOnPartialBuffer = false;
1655 if (avail < mRemainingFrames) {
1656 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1657 if (ns < 0 || myns < ns) {
1658 ns = myns;
1659 }
1660 return ns;
1661 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001662 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001663
1664 // Divide buffer size by 2 to take into account the expansion
1665 // due to 8 to 16 bit conversion: the callback must fill only half
1666 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001667 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001668 audioBuffer.size >>= 1;
1669 }
1670
1671 size_t reqSize = audioBuffer.size;
1672 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001673 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001674
1675 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001676 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1677 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1678 reqSize, (int) writtenSize);
1679 return NS_NEVER;
1680 }
1681
1682 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001683 // The callback is done filling buffers
1684 // Keep this thread going to handle timed events and
1685 // still try to get more data in intervals of WAIT_PERIOD_MS
1686 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001687 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001688 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001689
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001690 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001691 // 8 to 16 bit conversion, note that source and destination are the same address
1692 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001693 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001694 }
1695
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001696 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1697 audioBuffer.frameCount = releasedFrames;
1698 mRemainingFrames -= releasedFrames;
1699 if (misalignment >= releasedFrames) {
1700 misalignment -= releasedFrames;
1701 } else {
1702 misalignment = 0;
1703 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001704
1705 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001706
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001707 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1708 // if callback doesn't like to accept the full chunk
1709 if (writtenSize < reqSize) {
1710 continue;
1711 }
1712
1713 // There could be enough non-contiguous frames available to satisfy the remaining request
1714 if (mRemainingFrames <= nonContig) {
1715 continue;
1716 }
1717
1718#if 0
1719 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1720 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1721 // that total to a sum == notificationFrames.
1722 if (0 < misalignment && misalignment <= mRemainingFrames) {
1723 mRemainingFrames = misalignment;
1724 return (mRemainingFrames * 1100000000LL) / sampleRate;
1725 }
1726#endif
1727
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001728 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001729 mRemainingFrames = notificationFrames;
1730 mRetryOnPartialBuffer = true;
1731
1732 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1733 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001734}
1735
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001736status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001737{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001738 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001739 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001740 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001741 status_t result;
1742
Glenn Kastena47f3162012-11-07 10:13:08 -08001743 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kasten38e905b2014-01-13 10:21:48 -08001744 // output parameters in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001745 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001746
Glenn Kasten23a75452014-01-13 10:37:17 -08001747 if (isOffloaded_l()) {
1748 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001749 return DEAD_OBJECT;
1750 }
1751
Glenn Kastena47f3162012-11-07 10:13:08 -08001752 // if the new IAudioTrack is created, createTrack_l() will modify the
1753 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1754 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001755
1756 // take the frames that will be lost by track recreation into account in saved position
1757 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001758 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kasten363fb752014-01-15 12:27:31 -08001759 result = createTrack_l(position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001760
Glenn Kastena47f3162012-11-07 10:13:08 -08001761 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001762 // continue playback from last known position, but
1763 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1764 if (mStaticProxy != NULL) {
1765 mLoopPeriod = 0;
1766 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1767 }
1768 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1769 // track destruction have been played? This is critical for SoundPool implementation
1770 // This must be broken, and needs to be tested/debugged.
1771#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001772 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001773 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001774 // Make sure that a client relying on callback events indicating underrun or
1775 // the actual amount of audio frames played (e.g SoundPool) receives them.
1776 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001777 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001778 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001779 }
1780 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001781#endif
1782 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001783 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001784 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001785 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001786 if (result != NO_ERROR) {
1787 ALOGW("restoreTrack_l() failed status %d", result);
1788 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001789 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001790
1791 return result;
1792}
1793
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001794status_t AudioTrack::setParameters(const String8& keyValuePairs)
1795{
1796 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001797 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001798}
1799
Glenn Kastence703742013-07-19 16:33:58 -07001800status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1801{
Glenn Kasten53cec222013-08-29 09:01:02 -07001802 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001803 // FIXME not implemented for fast tracks; should use proxy and SSQ
1804 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1805 return INVALID_OPERATION;
1806 }
1807 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1808 return INVALID_OPERATION;
1809 }
1810 status_t status = mAudioTrack->getTimestamp(timestamp);
1811 if (status == NO_ERROR) {
1812 timestamp.mPosition += mProxy->getEpoch();
1813 }
1814 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001815}
1816
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001817String8 AudioTrack::getParameters(const String8& keys)
1818{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001819 audio_io_handle_t output = getOutput();
Glenn Kasten142f5192014-03-25 17:44:59 -07001820 if (output != AUDIO_IO_HANDLE_NONE) {
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001821 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001822 } else {
1823 return String8::empty();
1824 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001825}
1826
Glenn Kasten23a75452014-01-13 10:37:17 -08001827bool AudioTrack::isOffloaded() const
1828{
1829 AutoMutex lock(mLock);
1830 return isOffloaded_l();
1831}
1832
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001833status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001834{
1835
1836 const size_t SIZE = 256;
1837 char buffer[SIZE];
1838 String8 result;
1839
1840 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001841 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
Glenn Kasten877a0ac2014-04-30 17:04:13 -07001842 mVolume[AUDIO_INTERLEAVE_LEFT], mVolume[AUDIO_INTERLEAVE_RIGHT]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001843 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001844 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001845 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001846 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001847 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001848 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001849 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001850 result.append(buffer);
1851 ::write(fd, result.string(), result.size());
1852 return NO_ERROR;
1853}
1854
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001855uint32_t AudioTrack::getUnderrunFrames() const
1856{
1857 AutoMutex lock(mLock);
1858 return mProxy->getUnderrunFrames();
1859}
1860
1861// =========================================================================
1862
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001863void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001864{
1865 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1866 if (audioTrack != 0) {
1867 AutoMutex lock(audioTrack->mLock);
1868 audioTrack->mProxy->binderDied();
1869 }
1870}
1871
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001872// =========================================================================
1873
1874AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001875 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1876 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001877{
1878}
1879
1880AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001881{
1882}
1883
1884bool AudioTrack::AudioTrackThread::threadLoop()
1885{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001886 {
1887 AutoMutex _l(mMyLock);
1888 if (mPaused) {
1889 mMyCond.wait(mMyLock);
1890 // caller will check for exitPending()
1891 return true;
1892 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001893 if (mIgnoreNextPausedInt) {
1894 mIgnoreNextPausedInt = false;
1895 mPausedInt = false;
1896 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001897 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001898 if (mPausedNs > 0) {
1899 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1900 } else {
1901 mMyCond.wait(mMyLock);
1902 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001903 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001904 return true;
1905 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001906 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001907 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001908 switch (ns) {
1909 case 0:
1910 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001911 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001912 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001913 return true;
1914 case NS_NEVER:
1915 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001916 case NS_WHENEVER:
1917 // FIXME increase poll interval, or make event-driven
1918 ns = 1000000000LL;
1919 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001920 default:
1921 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001922 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001923 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001924 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001925}
1926
Glenn Kasten3acbd052012-02-28 10:39:56 -08001927void AudioTrack::AudioTrackThread::requestExit()
1928{
1929 // must be in this order to avoid a race condition
1930 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001931 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001932}
1933
1934void AudioTrack::AudioTrackThread::pause()
1935{
1936 AutoMutex _l(mMyLock);
1937 mPaused = true;
1938}
1939
1940void AudioTrack::AudioTrackThread::resume()
1941{
1942 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001943 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001944 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001945 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001946 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001947 mMyCond.signal();
1948 }
1949}
1950
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001951void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1952{
1953 AutoMutex _l(mMyLock);
1954 mPausedInt = true;
1955 mPausedNs = ns;
1956}
1957
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001958}; // namespace android