blob: 6989aafdd78c2b4aee3b7ae5ea4f4e37ff53dab6 [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();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 IPCThreadState::self()->flushCommands();
Marco Nelissend457c972014-02-11 08:47:07 -0800178 ALOGV("~AudioTrack, releasing session id from %d on behalf of %d",
179 IPCThreadState::self()->getCallingPid(), mClientPid);
180 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181 }
182}
183
184status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800185 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800187 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700188 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800189 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700190 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191 callback_t cbf,
192 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800193 uint32_t notificationFrames,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700195 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800196 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000197 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800198 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800199 int uid,
200 pid_t pid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201{
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800202 ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
Glenn Kasten838b3d82014-02-27 15:30:41 -0800203 "flags #%x, notificationFrames %u, sessionId %d, transferType %d",
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800204 streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames,
Glenn Kasten86f04662014-02-24 15:13:05 -0800205 sessionId, transferType);
206
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800207 switch (transferType) {
208 case TRANSFER_DEFAULT:
209 if (sharedBuffer != 0) {
210 transferType = TRANSFER_SHARED;
211 } else if (cbf == NULL || threadCanCallJava) {
212 transferType = TRANSFER_SYNC;
213 } else {
214 transferType = TRANSFER_CALLBACK;
215 }
216 break;
217 case TRANSFER_CALLBACK:
218 if (cbf == NULL || sharedBuffer != 0) {
219 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
220 return BAD_VALUE;
221 }
222 break;
223 case TRANSFER_OBTAIN:
224 case TRANSFER_SYNC:
225 if (sharedBuffer != 0) {
226 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
227 return BAD_VALUE;
228 }
229 break;
230 case TRANSFER_SHARED:
231 if (sharedBuffer == 0) {
232 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
233 return BAD_VALUE;
234 }
235 break;
236 default:
237 ALOGE("Invalid transfer type %d", transferType);
238 return BAD_VALUE;
239 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800240 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800241 mTransfer = transferType;
242
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700243 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
244 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245
Glenn Kastene33054e2012-11-14 12:54:39 -0800246 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700247
Eric Laurent1703cdf2011-03-07 14:52:59 -0800248 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800249
Glenn Kasten53cec222013-08-29 09:01:02 -0700250 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700251 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000252 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 return INVALID_OPERATION;
254 }
255
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700257 if (streamType == AUDIO_STREAM_DEFAULT) {
258 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800260 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
261 ALOGE("Invalid stream type %d", streamType);
262 return BAD_VALUE;
263 }
264 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700265
Glenn Kastenb1bef512014-01-13 10:25:53 -0800266 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800268 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
269 if (status != NO_ERROR) {
270 ALOGE("Could not get output sample rate for stream type %d; status %d",
271 streamType, status);
272 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700273 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800275 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700276
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800278 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700279 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281
282 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700283 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800284 ALOGE("Invalid format %#x", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 return BAD_VALUE;
286 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800287 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288
Glenn Kasten8ba90322013-10-30 11:29:27 -0700289 if (!audio_is_output_channel(channelMask)) {
290 ALOGE("Invalid channel mask %#x", channelMask);
291 return BAD_VALUE;
292 }
Glenn Kastene3247bf2014-02-24 15:19:07 -0800293 mChannelMask = channelMask;
Andy Hunge5412692014-05-16 11:25:07 -0700294 uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastene3247bf2014-02-24 15:19:07 -0800295 mChannelCount = channelCount;
Glenn Kasten8ba90322013-10-30 11:29:27 -0700296
Glenn Kastene0fa4672012-04-24 14:35:14 -0700297 // AudioFlinger does not currently support 8-bit data in shared memory
298 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
299 ALOGE("8-bit data in shared memory is not supported");
300 return BAD_VALUE;
301 }
302
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100304 // or offload was requested
305 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
306 || !audio_is_linear_pcm(format)) {
307 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
308 ? "Offload request, forcing to Direct Output"
309 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700310 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800311 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700312 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700314 // only allow deep buffering for music stream type
315 if (streamType != AUDIO_STREAM_MUSIC) {
316 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
317 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700318
Glenn Kastenb7730382014-04-30 15:50:31 -0700319 if (flags & AUDIO_OUTPUT_FLAG_DIRECT) {
320 if (audio_is_linear_pcm(format)) {
321 mFrameSize = channelCount * audio_bytes_per_sample(format);
322 } else {
323 mFrameSize = sizeof(uint8_t);
324 }
325 mFrameSizeAF = mFrameSize;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800326 } else {
Glenn Kastenb7730382014-04-30 15:50:31 -0700327 ALOG_ASSERT(audio_is_linear_pcm(format));
328 mFrameSize = channelCount * audio_bytes_per_sample(format);
329 mFrameSizeAF = channelCount * audio_bytes_per_sample(
330 format == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : format);
331 // createTrack will return an error if PCM format is not supported by server,
332 // so no need to check for specific PCM formats here
Glenn Kastene3aa6592012-12-04 12:22:46 -0800333 }
334
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800335 // Make copy of input parameter offloadInfo so that in the future:
336 // (a) createTrack_l doesn't need it as an input parameter
337 // (b) we can support re-creation of offloaded tracks
338 if (offloadInfo != NULL) {
339 mOffloadInfoCopy = *offloadInfo;
340 mOffloadInfo = &mOffloadInfoCopy;
341 } else {
342 mOffloadInfo = NULL;
343 }
344
Glenn Kasten66e46352014-01-16 17:44:23 -0800345 mVolume[AUDIO_INTERLEAVE_LEFT] = 1.0f;
346 mVolume[AUDIO_INTERLEAVE_RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800347 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800348 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800349 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700350 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800351 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700352 mSessionId = sessionId;
Marco Nelissend457c972014-02-11 08:47:07 -0800353 int callingpid = IPCThreadState::self()->getCallingPid();
354 int mypid = getpid();
355 if (uid == -1 || (callingpid != mypid)) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800356 mClientUid = IPCThreadState::self()->getCallingUid();
357 } else {
358 mClientUid = uid;
359 }
Marco Nelissend457c972014-02-11 08:47:07 -0800360 if (pid == -1 || (callingpid != mypid)) {
361 mClientPid = callingpid;
362 } else {
363 mClientPid = pid;
364 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700365 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700366 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700367 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700368
Glenn Kastena997e7a2012-08-07 09:44:19 -0700369 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700370 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700371 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
372 }
373
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800374 // create the IAudioTrack
Glenn Kasten363fb752014-01-15 12:27:31 -0800375 status = createTrack_l(0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800376
Glenn Kastena997e7a2012-08-07 09:44:19 -0700377 if (status != NO_ERROR) {
378 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100379 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
380 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700381 mAudioTrackThread.clear();
382 }
383 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700384 }
385
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800387 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700391 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 mNewPosition = 0;
393 mUpdatePeriod = 0;
Marco Nelissend457c972014-02-11 08:47:07 -0800394 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395 mSequence = 1;
396 mObservedSequence = mSequence;
397 mInUnderrun = false;
398
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 return NO_ERROR;
400}
401
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402// -------------------------------------------------------------------------
403
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100404status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800406 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100407
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800408 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100409 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410 }
411
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800412 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800414 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100415 if (previousState == STATE_PAUSED_STOPPING) {
416 mState = STATE_STOPPING;
417 } else {
418 mState = STATE_ACTIVE;
419 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800420 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
421 // reset current position as seen by client to 0
422 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700423 // force refresh of remaining frames by processAudioBuffer() as last
424 // write before stop could be partial.
425 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800426 }
427 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700428 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800429
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100432 if (previousState == STATE_STOPPING) {
433 mProxy->interrupt();
434 } else {
435 t->resume();
436 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800437 } else {
438 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
439 get_sched_policy(0, &mPreviousSchedulingGroup);
440 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
441 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800443 status_t status = NO_ERROR;
444 if (!(flags & CBLK_INVALID)) {
445 status = mAudioTrack->start();
446 if (status == DEAD_OBJECT) {
447 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800449 }
450 if (flags & CBLK_INVALID) {
451 status = restoreTrack_l("start");
452 }
453
454 if (status != NO_ERROR) {
455 ALOGE("start() status %d", status);
456 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100458 if (previousState != STATE_STOPPING) {
459 t->pause();
460 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700462 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700463 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 }
465 }
466
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100467 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800468}
469
470void AudioTrack::stop()
471{
472 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700473 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800474 return;
475 }
476
Glenn Kasten23a75452014-01-13 10:37:17 -0800477 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100478 mState = STATE_STOPPING;
479 } else {
480 mState = STATE_STOPPED;
481 }
482
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800483 mProxy->interrupt();
484 mAudioTrack->stop();
485 // the playback head position will reset to 0, so if a marker is set, we need
486 // to activate it again
487 mMarkerReached = false;
488#if 0
489 // Force flush if a shared buffer is used otherwise audioflinger
490 // will not stop before end of buffer is reached.
491 // It may be needed to make sure that we stop playback, likely in case looping is on.
492 if (mSharedBuffer != 0) {
493 flush_l();
494 }
495#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100496
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800497 sp<AudioTrackThread> t = mAudioTrackThread;
498 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800499 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100500 t->pause();
501 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800502 } else {
503 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
504 set_sched_policy(0, mPreviousSchedulingGroup);
505 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506}
507
508bool AudioTrack::stopped() const
509{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800510 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800511 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512}
513
514void AudioTrack::flush()
515{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800516 if (mSharedBuffer != 0) {
517 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800518 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 AutoMutex lock(mLock);
520 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
521 return;
522 }
523 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800524}
525
Eric Laurent1703cdf2011-03-07 14:52:59 -0800526void AudioTrack::flush_l()
527{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800528 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700529
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700530 // clear playback marker and periodic update counter
531 mMarkerPosition = 0;
532 mMarkerReached = false;
533 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100534 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700535
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800536 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800537 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100538 mProxy->interrupt();
539 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800540 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800541 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542}
543
544void AudioTrack::pause()
545{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800546 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100547 if (mState == STATE_ACTIVE) {
548 mState = STATE_PAUSED;
549 } else if (mState == STATE_STOPPING) {
550 mState = STATE_PAUSED_STOPPING;
551 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800552 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800554 mProxy->interrupt();
555 mAudioTrack->pause();
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800556
Marco Nelissen3a90f282014-03-10 11:21:43 -0700557 if (isOffloaded_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700558 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800559 uint32_t halFrames;
560 // OffloadThread sends HAL pause in its threadLoop.. time saved
561 // here can be slightly off
562 AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition);
563 ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition);
564 }
565 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566}
567
Eric Laurentbe916aa2010-06-01 23:49:17 -0700568status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800569{
Glenn Kastenc56f3422014-03-21 17:53:17 -0700570 // This duplicates a test by AudioTrack JNI, but that is not the only caller
571 if (isnanf(left) || left < GAIN_FLOAT_ZERO || left > GAIN_FLOAT_UNITY ||
572 isnanf(right) || right < GAIN_FLOAT_ZERO || right > GAIN_FLOAT_UNITY) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700573 return BAD_VALUE;
574 }
575
Eric Laurent1703cdf2011-03-07 14:52:59 -0800576 AutoMutex lock(mLock);
Glenn Kasten66e46352014-01-16 17:44:23 -0800577 mVolume[AUDIO_INTERLEAVE_LEFT] = left;
578 mVolume[AUDIO_INTERLEAVE_RIGHT] = right;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579
Glenn Kastenc56f3422014-03-21 17:53:17 -0700580 mProxy->setVolumeLR(gain_minifloat_pack(gain_from_float(left), gain_from_float(right)));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700581
Glenn Kasten23a75452014-01-13 10:37:17 -0800582 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700583 mAudioTrack->signal();
584 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700585 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586}
587
Glenn Kastenb1c09932012-02-27 16:21:04 -0800588status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800590 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700591}
592
Eric Laurent2beeb502010-07-16 07:43:46 -0700593status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700594{
Glenn Kastenc56f3422014-03-21 17:53:17 -0700595 // This duplicates a test by AudioTrack JNI, but that is not the only caller
596 if (isnanf(level) || level < GAIN_FLOAT_ZERO || level > GAIN_FLOAT_UNITY) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700597 return BAD_VALUE;
598 }
599
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800600 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700601 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800602 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700603
604 return NO_ERROR;
605}
606
Glenn Kastena5224f32012-01-04 12:41:44 -0800607void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700608{
609 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800610 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700611 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612}
613
Glenn Kasten3b16c762012-11-14 08:44:39 -0800614status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100616 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800617 return INVALID_OPERATION;
618 }
619
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800620 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700622 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 }
624 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700625 if (rate == 0 || rate > afSamplingRate*2 ) {
626 return BAD_VALUE;
627 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628
Eric Laurent1703cdf2011-03-07 14:52:59 -0800629 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800630 mSampleRate = rate;
631 mProxy->setSampleRate(rate);
632
Eric Laurent57326622009-07-07 07:10:45 -0700633 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634}
635
Glenn Kastena5224f32012-01-04 12:41:44 -0800636uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637{
John Grossman4ff14ba2012-02-08 16:37:41 -0800638 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800639 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800640 }
641
Eric Laurent1703cdf2011-03-07 14:52:59 -0800642 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700643
644 // sample rate can be updated during playback by the offloaded decoder so we need to
645 // query the HAL and update if needed.
646// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800647 if (isOffloaded_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700648 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700649 uint32_t sampleRate = 0;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700650 status_t status = AudioSystem::getSamplingRate(mOutput, &sampleRate);
Eric Laurent6f59db12013-07-26 17:16:50 -0700651 if (status == NO_ERROR) {
652 mSampleRate = sampleRate;
653 }
654 }
655 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800656 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800657}
658
659status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
660{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100661 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800662 return INVALID_OPERATION;
663 }
664
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800666 ;
667 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
668 loopEnd - loopStart >= MIN_LOOP) {
669 ;
670 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671 return BAD_VALUE;
672 }
673
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800674 AutoMutex lock(mLock);
675 // See setPosition() regarding setting parameters such as loop points or position while active
676 if (mState == STATE_ACTIVE) {
677 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700678 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800679 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800680 return NO_ERROR;
681}
682
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800683void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
684{
685 // FIXME If setting a loop also sets position to start of loop, then
686 // this is correct. Otherwise it should be removed.
687 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
688 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
689 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
690}
691
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692status_t AudioTrack::setMarkerPosition(uint32_t marker)
693{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700694 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100695 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700696 return INVALID_OPERATION;
697 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800699 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700701 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702
703 return NO_ERROR;
704}
705
Glenn Kastena5224f32012-01-04 12:41:44 -0800706status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100708 if (isOffloaded()) {
709 return INVALID_OPERATION;
710 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700711 if (marker == NULL) {
712 return BAD_VALUE;
713 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800715 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716 *marker = mMarkerPosition;
717
718 return NO_ERROR;
719}
720
721status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
722{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700723 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100724 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700725 return INVALID_OPERATION;
726 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800727
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800728 AutoMutex lock(mLock);
729 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800731
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800732 return NO_ERROR;
733}
734
Glenn Kastena5224f32012-01-04 12:41:44 -0800735status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800736{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100737 if (isOffloaded()) {
738 return INVALID_OPERATION;
739 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700740 if (updatePeriod == NULL) {
741 return BAD_VALUE;
742 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800743
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800744 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800745 *updatePeriod = mUpdatePeriod;
746
747 return NO_ERROR;
748}
749
750status_t AudioTrack::setPosition(uint32_t position)
751{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100752 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700753 return INVALID_OPERATION;
754 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800755 if (position > mFrameCount) {
756 return BAD_VALUE;
757 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800758
Eric Laurent1703cdf2011-03-07 14:52:59 -0800759 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800760 // Currently we require that the player is inactive before setting parameters such as position
761 // or loop points. Otherwise, there could be a race condition: the application could read the
762 // current position, compute a new position or loop parameters, and then set that position or
763 // loop parameters but it would do the "wrong" thing since the position has continued to advance
764 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
765 // to specify how it wants to handle such scenarios.
766 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700767 return INVALID_OPERATION;
768 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800769 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
770 mLoopPeriod = 0;
771 // FIXME Check whether loops and setting position are incompatible in old code.
772 // If we use setLoop for both purposes we lose the capability to set the position while looping.
773 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700774
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800775 return NO_ERROR;
776}
777
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800778status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700780 if (position == NULL) {
781 return BAD_VALUE;
782 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800783
Eric Laurent1703cdf2011-03-07 14:52:59 -0800784 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800785 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100786 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800787
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800788 if ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING)) {
789 ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition);
790 *position = mPausedPosition;
791 return NO_ERROR;
792 }
793
Glenn Kasten142f5192014-03-25 17:44:59 -0700794 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100795 uint32_t halFrames;
796 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
797 }
798 *position = dspFrames;
799 } else {
800 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
801 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
802 mProxy->getPosition();
803 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800804 return NO_ERROR;
805}
806
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000807status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800808{
809 if (mSharedBuffer == 0 || mIsTimed) {
810 return INVALID_OPERATION;
811 }
812 if (position == NULL) {
813 return BAD_VALUE;
814 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800815
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800816 AutoMutex lock(mLock);
817 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800818 return NO_ERROR;
819}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800820
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800821status_t AudioTrack::reload()
822{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100823 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800824 return INVALID_OPERATION;
825 }
826
Eric Laurent1703cdf2011-03-07 14:52:59 -0800827 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800828 // See setPosition() regarding setting parameters such as loop points or position while active
829 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700830 return INVALID_OPERATION;
831 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800832 mNewPosition = mUpdatePeriod;
833 mLoopPeriod = 0;
834 // FIXME The new code cannot reload while keeping a loop specified.
835 // Need to check how the old code handled this, and whether it's a significant change.
836 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800837 return NO_ERROR;
838}
839
Glenn Kasten38e905b2014-01-13 10:21:48 -0800840audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700841{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800842 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100843 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800844}
845
Eric Laurentbe916aa2010-06-01 23:49:17 -0700846status_t AudioTrack::attachAuxEffect(int effectId)
847{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800848 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700849 status_t status = mAudioTrack->attachAuxEffect(effectId);
850 if (status == NO_ERROR) {
851 mAuxEffectId = effectId;
852 }
853 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700854}
855
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856// -------------------------------------------------------------------------
857
Eric Laurent1703cdf2011-03-07 14:52:59 -0800858// must be called with mLock held
Glenn Kasten363fb752014-01-15 12:27:31 -0800859status_t AudioTrack::createTrack_l(size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800860{
861 status_t status;
862 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
863 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700864 ALOGE("Could not get audioflinger");
865 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800866 }
867
Glenn Kasten38e905b2014-01-13 10:21:48 -0800868 audio_io_handle_t output = AudioSystem::getOutput(mStreamType, mSampleRate, mFormat,
869 mChannelMask, mFlags, mOffloadInfo);
Glenn Kasten142f5192014-03-25 17:44:59 -0700870 if (output == AUDIO_IO_HANDLE_NONE) {
Glenn Kasten38e905b2014-01-13 10:21:48 -0800871 ALOGE("Could not get audio output for stream type %d, sample rate %u, format %#x, "
872 "channel mask %#x, flags %#x",
873 mStreamType, mSampleRate, mFormat, mChannelMask, mFlags);
874 return BAD_VALUE;
875 }
876 {
877 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
878 // we must release it ourselves if anything goes wrong.
879
Glenn Kastence8828a2013-09-16 18:07:38 -0700880 // Not all of these values are needed under all conditions, but it is easier to get them all
881
Eric Laurentd1b449a2010-05-14 03:26:45 -0700882 uint32_t afLatency;
Glenn Kasten241618f2014-03-25 17:48:57 -0700883 status = AudioSystem::getLatency(output, &afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700884 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800885 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800886 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700887 }
888
Glenn Kastence8828a2013-09-16 18:07:38 -0700889 size_t afFrameCount;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700890 status = AudioSystem::getFrameCount(output, &afFrameCount);
Glenn Kastence8828a2013-09-16 18:07:38 -0700891 if (status != NO_ERROR) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700892 ALOGE("getFrameCount(output=%d) status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800893 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700894 }
895
896 uint32_t afSampleRate;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700897 status = AudioSystem::getSamplingRate(output, &afSampleRate);
Glenn Kastence8828a2013-09-16 18:07:38 -0700898 if (status != NO_ERROR) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700899 ALOGE("getSamplingRate(output=%d) status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800900 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700901 }
902
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700903 // Client decides whether the track is TIMED (see below), but can only express a preference
904 // for FAST. Server will perform additional tests.
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800905 if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !((
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700906 // either of these use cases:
907 // use case 1: shared buffer
Glenn Kasten363fb752014-01-15 12:27:31 -0800908 (mSharedBuffer != 0) ||
Glenn Kastenc6ba8232014-02-27 13:34:29 -0800909 // use case 2: callback transfer mode
910 (mTransfer == TRANSFER_CALLBACK)) &&
Glenn Kasten43bdc1d2014-02-10 09:53:55 -0800911 // matching sample rate
912 (mSampleRate == afSampleRate))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800913 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700914 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -0800915 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700916 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700917 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700918
Glenn Kastence8828a2013-09-16 18:07:38 -0700919 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800920 // n = 1 fast track with single buffering; nBuffering is ignored
921 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700922 // n = 2 normal track, no sample rate conversion
923 // n = 3 normal track, with sample rate conversion
924 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
925 // n > 3 very high latency or very small notification interval; nBuffering is ignored
Glenn Kasten363fb752014-01-15 12:27:31 -0800926 const uint32_t nBuffering = (mSampleRate == afSampleRate) ? 2 : 3;
Glenn Kastence8828a2013-09-16 18:07:38 -0700927
Eric Laurentd1b449a2010-05-14 03:26:45 -0700928 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700929
Glenn Kasten363fb752014-01-15 12:27:31 -0800930 size_t frameCount = mReqFrameCount;
931 if (!audio_is_linear_pcm(mFormat)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700932
Glenn Kasten363fb752014-01-15 12:27:31 -0800933 if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934 // Same comment as below about ignoring frameCount parameter for set()
Glenn Kasten363fb752014-01-15 12:27:31 -0800935 frameCount = mSharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700936 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700937 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700938 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100939 if (mNotificationFramesAct != frameCount) {
940 mNotificationFramesAct = frameCount;
941 }
Glenn Kasten363fb752014-01-15 12:27:31 -0800942 } else if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700943
Glenn Kastena42ff002012-11-14 12:47:55 -0800944 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700945 // 8-bit data in shared memory is not currently supported by AudioFlinger
Glenn Kastenb7730382014-04-30 15:50:31 -0700946 size_t alignment = audio_bytes_per_sample(
947 mFormat == AUDIO_FORMAT_PCM_8_BIT ? AUDIO_FORMAT_PCM_16_BIT : mFormat);
948 if (alignment & 1) {
949 alignment = 1;
950 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800951 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700952 // More than 2 channels does not require stronger alignment than stereo
953 alignment <<= 1;
954 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +0000955 if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800956 ALOGE("Invalid buffer alignment: address %p, channel count %u",
Glenn Kasten363fb752014-01-15 12:27:31 -0800957 mSharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800958 status = BAD_VALUE;
959 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700960 }
961
962 // When initializing a shared buffer AudioTrack via constructors,
963 // there's no frameCount parameter.
964 // But when initializing a shared buffer AudioTrack via set(),
965 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastenb7730382014-04-30 15:50:31 -0700966 frameCount = mSharedBuffer->size() / mFrameSizeAF;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700967
Glenn Kasten363fb752014-01-15 12:27:31 -0800968 } else if (!(mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700969
970 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700971
Eric Laurentd1b449a2010-05-14 03:26:45 -0700972 // Ensure that buffer depth covers at least audio hardware latency
973 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700974 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
975 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700976 if (minBufCount <= nBuffering) {
977 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800978 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700979
Glenn Kasten363fb752014-01-15 12:27:31 -0800980 size_t minFrameCount = (afFrameCount*mSampleRate*minBufCount)/afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -0800981 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800982 ", afLatency=%d",
Glenn Kasten363fb752014-01-15 12:27:31 -0800983 minFrameCount, afFrameCount, minBufCount, mSampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700984
985 if (frameCount == 0) {
986 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700987 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700988 // not ALOGW because it happens all the time when playing key clicks over A2DP
989 ALOGV("Minimum buffer size corrected from %d to %d",
990 frameCount, minFrameCount);
991 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800992 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700993 // Make sure that application is notified with sufficient margin before underrun
994 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
995 mNotificationFramesAct = frameCount/nBuffering;
996 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700997
Glenn Kastene0fa4672012-04-24 14:35:14 -0700998 } else {
999 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -07001000 }
1001
Glenn Kastena075db42012-03-06 11:22:44 -08001002 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
1003 if (mIsTimed) {
1004 trackFlags |= IAudioFlinger::TRACK_TIMED;
1005 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001006
1007 pid_t tid = -1;
Glenn Kasten363fb752014-01-15 12:27:31 -08001008 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001009 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001010 if (mAudioTrackThread != 0) {
1011 tid = mAudioTrackThread->getTid();
1012 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001013 }
1014
Glenn Kasten363fb752014-01-15 12:27:31 -08001015 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001016 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1017 }
1018
Glenn Kasten74935e42013-12-19 08:56:45 -08001019 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
1020 // but we will still need the original value also
Glenn Kasten363fb752014-01-15 12:27:31 -08001021 sp<IAudioTrack> track = audioFlinger->createTrack(mStreamType,
1022 mSampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001023 // AudioFlinger only sees 16-bit PCM
Glenn Kastenc4b88a82014-04-30 16:54:30 -07001024 mFormat == AUDIO_FORMAT_PCM_8_BIT &&
1025 !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT) ?
Glenn Kasten363fb752014-01-15 12:27:31 -08001026 AUDIO_FORMAT_PCM_16_BIT : mFormat,
Glenn Kastena42ff002012-11-14 12:47:55 -08001027 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001028 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -08001029 &trackFlags,
Glenn Kasten363fb752014-01-15 12:27:31 -08001030 mSharedBuffer,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001031 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001032 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001033 &mSessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001034 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001035 &status);
1036
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001037 if (status != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00001038 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001039 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001040 }
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001041 ALOG_ASSERT(track != 0);
1042
Glenn Kasten38e905b2014-01-13 10:21:48 -08001043 // AudioFlinger now owns the reference to the I/O handle,
1044 // so we are no longer responsible for releasing it.
1045
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001046 sp<IMemory> iMem = track->getCblk();
1047 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001048 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001049 return NO_INIT;
1050 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001051 void *iMemPointer = iMem->pointer();
1052 if (iMemPointer == NULL) {
1053 ALOGE("Could not get control block pointer");
1054 return NO_INIT;
1055 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001056 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001057 if (mAudioTrack != 0) {
1058 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1059 mDeathNotifier.clear();
1060 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001061 mAudioTrack = track;
Glenn Kasten5f631512014-02-24 15:16:07 -08001062
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001063 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001064 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001065 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001066 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001067 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1068 // In current design, AudioTrack client checks and ensures frame count validity before
1069 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1070 // for fast track as it uses a special method of assigning frame count.
1071 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1072 }
1073 frameCount = temp;
Glenn Kasten5f631512014-02-24 15:16:07 -08001074
Glenn Kastena07f17c2013-04-23 12:39:37 -07001075 mAwaitBoost = false;
Glenn Kasten363fb752014-01-15 12:27:31 -08001076 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001077 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001078 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001079 mAwaitBoost = true;
Glenn Kasten363fb752014-01-15 12:27:31 -08001080 if (mSharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001081 // Theoretically double-buffering is not required for fast tracks,
1082 // due to tighter scheduling. But in practice, to accommodate kernels with
1083 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1084 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1085 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001086 }
1087 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001088 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001089 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001090 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001091 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
1092 if (mSharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001093 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1094 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001095 }
1096 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001097 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001098 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001099 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001100 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1101 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1102 } else {
1103 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
Glenn Kasten363fb752014-01-15 12:27:31 -08001104 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001105 // FIXME This is a warning, not an error, so don't return error status
1106 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001107 }
1108 }
1109
Glenn Kasten38e905b2014-01-13 10:21:48 -08001110 // We retain a copy of the I/O handle, but don't own the reference
1111 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001112 mRefreshRemaining = true;
1113
1114 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1115 // is the value of pointer() for the shared buffer, otherwise buffers points
1116 // immediately after the control block. This address is for the mapping within client
1117 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1118 void* buffers;
Glenn Kasten363fb752014-01-15 12:27:31 -08001119 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001120 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001121 } else {
Glenn Kasten363fb752014-01-15 12:27:31 -08001122 buffers = mSharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001123 }
1124
Eric Laurent2beeb502010-07-16 07:43:46 -07001125 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001126 // FIXME don't believe this lie
Glenn Kasten363fb752014-01-15 12:27:31 -08001127 mLatency = afLatency + (1000*frameCount) / mSampleRate;
Glenn Kasten5f631512014-02-24 15:16:07 -08001128
Glenn Kastenb6037442012-11-14 13:42:25 -08001129 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001130 // If IAudioTrack is re-created, don't let the requested frameCount
1131 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001132 if (frameCount > mReqFrameCount) {
1133 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001134 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001135
1136 // update proxy
Glenn Kasten363fb752014-01-15 12:27:31 -08001137 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001138 mStaticProxy.clear();
1139 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1140 } else {
1141 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1142 mProxy = mStaticProxy;
1143 }
Glenn Kastenc56f3422014-03-21 17:53:17 -07001144 mProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001145 mProxy->setSendLevel(mSendLevel);
1146 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001147 mProxy->setEpoch(epoch);
1148 mProxy->setMinimum(mNotificationFramesAct);
1149
1150 mDeathNotifier = new DeathNotifier(this);
1151 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001152
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001153 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001154 }
1155
1156release:
1157 AudioSystem::releaseOutput(output);
1158 if (status == NO_ERROR) {
1159 status = NO_INIT;
1160 }
1161 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001162}
1163
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001164status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1165{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001166 if (audioBuffer == NULL) {
1167 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001168 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001169 if (mTransfer != TRANSFER_OBTAIN) {
1170 audioBuffer->frameCount = 0;
1171 audioBuffer->size = 0;
1172 audioBuffer->raw = NULL;
1173 return INVALID_OPERATION;
1174 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001175
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001176 const struct timespec *requested;
Eric Laurentdf576992014-01-27 18:13:39 -08001177 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001178 if (waitCount == -1) {
1179 requested = &ClientProxy::kForever;
1180 } else if (waitCount == 0) {
1181 requested = &ClientProxy::kNonBlocking;
1182 } else if (waitCount > 0) {
1183 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001184 timeout.tv_sec = ms / 1000;
1185 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1186 requested = &timeout;
1187 } else {
1188 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1189 requested = NULL;
1190 }
1191 return obtainBuffer(audioBuffer, requested);
1192}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001193
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001194status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1195 struct timespec *elapsed, size_t *nonContig)
1196{
1197 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1198 uint32_t oldSequence = 0;
1199 uint32_t newSequence;
1200
1201 Proxy::Buffer buffer;
1202 status_t status = NO_ERROR;
1203
1204 static const int32_t kMaxTries = 5;
1205 int32_t tryCounter = kMaxTries;
1206
1207 do {
1208 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1209 // keep them from going away if another thread re-creates the track during obtainBuffer()
1210 sp<AudioTrackClientProxy> proxy;
1211 sp<IMemory> iMem;
1212
1213 { // start of lock scope
1214 AutoMutex lock(mLock);
1215
1216 newSequence = mSequence;
1217 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1218 if (status == DEAD_OBJECT) {
1219 // re-create track, unless someone else has already done so
1220 if (newSequence == oldSequence) {
1221 status = restoreTrack_l("obtainBuffer");
1222 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001223 buffer.mFrameCount = 0;
1224 buffer.mRaw = NULL;
1225 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001226 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 }
1229 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001230 oldSequence = newSequence;
1231
1232 // Keep the extra references
1233 proxy = mProxy;
1234 iMem = mCblkMemory;
1235
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001236 if (mState == STATE_STOPPING) {
1237 status = -EINTR;
1238 buffer.mFrameCount = 0;
1239 buffer.mRaw = NULL;
1240 buffer.mNonContig = 0;
1241 break;
1242 }
1243
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001244 // Non-blocking if track is stopped or paused
1245 if (mState != STATE_ACTIVE) {
1246 requested = &ClientProxy::kNonBlocking;
1247 }
1248
1249 } // end of lock scope
1250
1251 buffer.mFrameCount = audioBuffer->frameCount;
1252 // FIXME starts the requested timeout and elapsed over from scratch
1253 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1254
1255 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1256
1257 audioBuffer->frameCount = buffer.mFrameCount;
1258 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1259 audioBuffer->raw = buffer.mRaw;
1260 if (nonContig != NULL) {
1261 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001263 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264}
1265
1266void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1267{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001268 if (mTransfer == TRANSFER_SHARED) {
1269 return;
1270 }
1271
1272 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1273 if (stepCount == 0) {
1274 return;
1275 }
1276
1277 Proxy::Buffer buffer;
1278 buffer.mFrameCount = stepCount;
1279 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001280
Eric Laurent1703cdf2011-03-07 14:52:59 -08001281 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001282 mInUnderrun = false;
1283 mProxy->releaseBuffer(&buffer);
1284
1285 // restart track if it was disabled by audioflinger due to previous underrun
1286 if (mState == STATE_ACTIVE) {
1287 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001288 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastenc5a17422014-03-13 14:59:59 -07001289 ALOGW("releaseBuffer() track %p disabled due to previous underrun, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001290 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001291 mAudioTrack->start();
1292 }
1293 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294}
1295
1296// -------------------------------------------------------------------------
1297
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001298ssize_t AudioTrack::write(const void* buffer, size_t userSize, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001300 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001301 return INVALID_OPERATION;
1302 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001303
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001304 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001305 // Sanity-check: user is most-likely passing an error code, and it would
1306 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001307 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001308 return BAD_VALUE;
1309 }
1310
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001311 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001312 Buffer audioBuffer;
1313
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001314 while (userSize >= mFrameSize) {
1315 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001316
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001317 status_t err = obtainBuffer(&audioBuffer,
1318 blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001320 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001322 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001323 return ssize_t(err);
1324 }
1325
1326 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001327 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001328 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001329 toWrite = audioBuffer.size >> 1;
1330 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001331 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001332 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001333 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001334 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001335 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001336 userSize -= toWrite;
1337 written += toWrite;
1338
1339 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001340 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001341
1342 return written;
1343}
1344
1345// -------------------------------------------------------------------------
1346
John Grossman4ff14ba2012-02-08 16:37:41 -08001347TimedAudioTrack::TimedAudioTrack() {
1348 mIsTimed = true;
1349}
1350
1351status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1352{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001353 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001354 status_t result = UNKNOWN_ERROR;
1355
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001356#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001357 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1358 // while we are accessing the cblk
1359 sp<IAudioTrack> audioTrack = mAudioTrack;
1360 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001361#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001362
John Grossman4ff14ba2012-02-08 16:37:41 -08001363 // If the track is not invalid already, try to allocate a buffer. alloc
1364 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001365 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001366 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001367 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001368 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1369 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001370 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001371 }
1372 }
1373
1374 // If the track is invalid at this point, attempt to restore it. and try the
1375 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001376 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001377 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001378
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001379 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001380 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001381 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001382 }
1383
1384 return result;
1385}
1386
1387status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1388 int64_t pts)
1389{
Eric Laurentdf839842012-05-31 14:27:14 -07001390 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1391 {
1392 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001393 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001394 // restart track if it was disabled by audioflinger due to previous underrun
1395 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001396 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1397 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001398 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001399 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001400 mAudioTrack->start();
1401 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001402 }
Eric Laurentdf839842012-05-31 14:27:14 -07001403 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001404}
1405
1406status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1407 TargetTimeline target)
1408{
1409 return mAudioTrack->setMediaTimeTransform(xform, target);
1410}
1411
1412// -------------------------------------------------------------------------
1413
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001414nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001415{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001416 // Currently the AudioTrack thread is not created if there are no callbacks.
1417 // Would it ever make sense to run the thread, even without callbacks?
1418 // If so, then replace this by checks at each use for mCbf != NULL.
1419 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1420
Eric Laurent1703cdf2011-03-07 14:52:59 -08001421 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001422 if (mAwaitBoost) {
1423 mAwaitBoost = false;
1424 mLock.unlock();
1425 static const int32_t kMaxTries = 5;
1426 int32_t tryCounter = kMaxTries;
1427 uint32_t pollUs = 10000;
1428 do {
1429 int policy = sched_getscheduler(0);
1430 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1431 break;
1432 }
1433 usleep(pollUs);
1434 pollUs <<= 1;
1435 } while (tryCounter-- > 0);
1436 if (tryCounter < 0) {
1437 ALOGE("did not receive expected priority boost on time");
1438 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001439 // Run again immediately
1440 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001441 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001442
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001443 // Can only reference mCblk while locked
1444 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001445 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001446
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001447 // Check for track invalidation
1448 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001449 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1450 // AudioSystem cache. We should not exit here but after calling the callback so
1451 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001452 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001453 status_t status = restoreTrack_l("processAudioBuffer");
1454 mLock.unlock();
1455 // Run again immediately, but with a new IAudioTrack
1456 return 0;
1457 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001458 }
1459
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001460 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001461 bool active = mState == STATE_ACTIVE;
1462
1463 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1464 bool newUnderrun = false;
1465 if (flags & CBLK_UNDERRUN) {
1466#if 0
1467 // Currently in shared buffer mode, when the server reaches the end of buffer,
1468 // the track stays active in continuous underrun state. It's up to the application
1469 // to pause or stop the track, or set the position to a new offset within buffer.
1470 // This was some experimental code to auto-pause on underrun. Keeping it here
1471 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1472 if (mTransfer == TRANSFER_SHARED) {
1473 mState = STATE_PAUSED;
1474 active = false;
1475 }
1476#endif
1477 if (!mInUnderrun) {
1478 mInUnderrun = true;
1479 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480 }
1481 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001482
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001483 // Get current position of server
1484 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001485
1486 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001487 bool markerReached = false;
1488 size_t markerPosition = mMarkerPosition;
1489 // FIXME fails for wraparound, need 64 bits
1490 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1491 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492 }
1493
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001494 // Determine number of new position callback(s) that will be needed, while locked
1495 size_t newPosCount = 0;
1496 size_t newPosition = mNewPosition;
1497 size_t updatePeriod = mUpdatePeriod;
1498 // FIXME fails for wraparound, need 64 bits
1499 if (updatePeriod > 0 && position >= newPosition) {
1500 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1501 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502 }
1503
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001504 // Cache other fields that will be needed soon
1505 uint32_t loopPeriod = mLoopPeriod;
1506 uint32_t sampleRate = mSampleRate;
Glenn Kasten838b3d82014-02-27 15:30:41 -08001507 uint32_t notificationFrames = mNotificationFramesAct;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001508 if (mRefreshRemaining) {
1509 mRefreshRemaining = false;
1510 mRemainingFrames = notificationFrames;
1511 mRetryOnPartialBuffer = false;
1512 }
1513 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001514 uint32_t sequence = mSequence;
Glenn Kasten96f04882013-09-20 09:28:56 -07001515 sp<AudioTrackClientProxy> proxy = mProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001516
1517 // These fields don't need to be cached, because they are assigned only by set():
1518 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1519 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1520
1521 mLock.unlock();
1522
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001523 if (waitStreamEnd) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001524 struct timespec timeout;
1525 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1526 timeout.tv_nsec = 0;
1527
Glenn Kasten96f04882013-09-20 09:28:56 -07001528 status_t status = proxy->waitStreamEndDone(&timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001529 switch (status) {
1530 case NO_ERROR:
1531 case DEAD_OBJECT:
1532 case TIMED_OUT:
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001533 mCbf(EVENT_STREAM_END, mUserData, NULL);
Glenn Kasten96f04882013-09-20 09:28:56 -07001534 {
1535 AutoMutex lock(mLock);
1536 // The previously assigned value of waitStreamEnd is no longer valid,
1537 // since the mutex has been unlocked and either the callback handler
1538 // or another thread could have re-started the AudioTrack during that time.
1539 waitStreamEnd = mState == STATE_STOPPING;
1540 if (waitStreamEnd) {
1541 mState = STATE_STOPPED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001542 }
1543 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001544 if (waitStreamEnd && status != DEAD_OBJECT) {
1545 return NS_INACTIVE;
1546 }
1547 break;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001548 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001549 return 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001550 }
1551
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001552 // perform callbacks while unlocked
1553 if (newUnderrun) {
1554 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1555 }
1556 // FIXME we will miss loops if loop cycle was signaled several times since last call
1557 // to processAudioBuffer()
1558 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1559 mCbf(EVENT_LOOP_END, mUserData, NULL);
1560 }
1561 if (flags & CBLK_BUFFER_END) {
1562 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1563 }
1564 if (markerReached) {
1565 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1566 }
1567 while (newPosCount > 0) {
1568 size_t temp = newPosition;
1569 mCbf(EVENT_NEW_POS, mUserData, &temp);
1570 newPosition += updatePeriod;
1571 newPosCount--;
1572 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001573
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001574 if (mObservedSequence != sequence) {
1575 mObservedSequence = sequence;
1576 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001577 // for offloaded tracks, just wait for the upper layers to recreate the track
1578 if (isOffloaded()) {
1579 return NS_INACTIVE;
1580 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001581 }
1582
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001583 // if inactive, then don't run me again until re-started
1584 if (!active) {
1585 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001586 }
1587
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001588 // Compute the estimated time until the next timed event (position, markers, loops)
1589 // FIXME only for non-compressed audio
1590 uint32_t minFrames = ~0;
1591 if (!markerReached && position < markerPosition) {
1592 minFrames = markerPosition - position;
1593 }
1594 if (loopPeriod > 0 && loopPeriod < minFrames) {
1595 minFrames = loopPeriod;
1596 }
1597 if (updatePeriod > 0 && updatePeriod < minFrames) {
1598 minFrames = updatePeriod;
1599 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001600
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001601 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1602 static const uint32_t kPoll = 0;
1603 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1604 minFrames = kPoll * notificationFrames;
1605 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001606
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001607 // Convert frame units to time units
1608 nsecs_t ns = NS_WHENEVER;
1609 if (minFrames != (uint32_t) ~0) {
1610 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1611 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1612 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1613 }
1614
1615 // If not supplying data by EVENT_MORE_DATA, then we're done
1616 if (mTransfer != TRANSFER_CALLBACK) {
1617 return ns;
1618 }
1619
1620 struct timespec timeout;
1621 const struct timespec *requested = &ClientProxy::kForever;
1622 if (ns != NS_WHENEVER) {
1623 timeout.tv_sec = ns / 1000000000LL;
1624 timeout.tv_nsec = ns % 1000000000LL;
1625 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1626 requested = &timeout;
1627 }
1628
1629 while (mRemainingFrames > 0) {
1630
1631 Buffer audioBuffer;
1632 audioBuffer.frameCount = mRemainingFrames;
1633 size_t nonContig;
1634 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1635 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1636 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1637 requested = &ClientProxy::kNonBlocking;
1638 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001639 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1640 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001641 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001642 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1643 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001644 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001646 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1647 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001649
Eric Laurent42a6f422013-08-29 14:35:05 -07001650 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001651 mRetryOnPartialBuffer = false;
1652 if (avail < mRemainingFrames) {
1653 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1654 if (ns < 0 || myns < ns) {
1655 ns = myns;
1656 }
1657 return ns;
1658 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001659 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001660
1661 // Divide buffer size by 2 to take into account the expansion
1662 // due to 8 to 16 bit conversion: the callback must fill only half
1663 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001664 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001665 audioBuffer.size >>= 1;
1666 }
1667
1668 size_t reqSize = audioBuffer.size;
1669 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001670 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001671
1672 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001673 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1674 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1675 reqSize, (int) writtenSize);
1676 return NS_NEVER;
1677 }
1678
1679 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001680 // The callback is done filling buffers
1681 // Keep this thread going to handle timed events and
1682 // still try to get more data in intervals of WAIT_PERIOD_MS
1683 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001684 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001685 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001686
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001687 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001688 // 8 to 16 bit conversion, note that source and destination are the same address
1689 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001690 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001691 }
1692
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001693 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1694 audioBuffer.frameCount = releasedFrames;
1695 mRemainingFrames -= releasedFrames;
1696 if (misalignment >= releasedFrames) {
1697 misalignment -= releasedFrames;
1698 } else {
1699 misalignment = 0;
1700 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001701
1702 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001703
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001704 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1705 // if callback doesn't like to accept the full chunk
1706 if (writtenSize < reqSize) {
1707 continue;
1708 }
1709
1710 // There could be enough non-contiguous frames available to satisfy the remaining request
1711 if (mRemainingFrames <= nonContig) {
1712 continue;
1713 }
1714
1715#if 0
1716 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1717 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1718 // that total to a sum == notificationFrames.
1719 if (0 < misalignment && misalignment <= mRemainingFrames) {
1720 mRemainingFrames = misalignment;
1721 return (mRemainingFrames * 1100000000LL) / sampleRate;
1722 }
1723#endif
1724
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001725 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001726 mRemainingFrames = notificationFrames;
1727 mRetryOnPartialBuffer = true;
1728
1729 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1730 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001731}
1732
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001733status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001734{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001735 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001736 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001737 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001738 status_t result;
1739
Glenn Kastena47f3162012-11-07 10:13:08 -08001740 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kasten38e905b2014-01-13 10:21:48 -08001741 // output parameters in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001742 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001743
Glenn Kasten23a75452014-01-13 10:37:17 -08001744 if (isOffloaded_l()) {
1745 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001746 return DEAD_OBJECT;
1747 }
1748
Glenn Kastena47f3162012-11-07 10:13:08 -08001749 // if the new IAudioTrack is created, createTrack_l() will modify the
1750 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1751 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001752
1753 // take the frames that will be lost by track recreation into account in saved position
1754 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001755 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kasten363fb752014-01-15 12:27:31 -08001756 result = createTrack_l(position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001757
Glenn Kastena47f3162012-11-07 10:13:08 -08001758 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001759 // continue playback from last known position, but
1760 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1761 if (mStaticProxy != NULL) {
1762 mLoopPeriod = 0;
1763 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1764 }
1765 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1766 // track destruction have been played? This is critical for SoundPool implementation
1767 // This must be broken, and needs to be tested/debugged.
1768#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001769 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001770 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001771 // Make sure that a client relying on callback events indicating underrun or
1772 // the actual amount of audio frames played (e.g SoundPool) receives them.
1773 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001774 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001775 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001776 }
1777 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001778#endif
1779 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001780 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001781 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001782 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001783 if (result != NO_ERROR) {
1784 ALOGW("restoreTrack_l() failed status %d", result);
1785 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001786 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001787
1788 return result;
1789}
1790
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001791status_t AudioTrack::setParameters(const String8& keyValuePairs)
1792{
1793 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001794 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001795}
1796
Glenn Kastence703742013-07-19 16:33:58 -07001797status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1798{
Glenn Kasten53cec222013-08-29 09:01:02 -07001799 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001800 // FIXME not implemented for fast tracks; should use proxy and SSQ
1801 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1802 return INVALID_OPERATION;
1803 }
1804 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1805 return INVALID_OPERATION;
1806 }
1807 status_t status = mAudioTrack->getTimestamp(timestamp);
1808 if (status == NO_ERROR) {
1809 timestamp.mPosition += mProxy->getEpoch();
1810 }
1811 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001812}
1813
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001814String8 AudioTrack::getParameters(const String8& keys)
1815{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001816 audio_io_handle_t output = getOutput();
Glenn Kasten142f5192014-03-25 17:44:59 -07001817 if (output != AUDIO_IO_HANDLE_NONE) {
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001818 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001819 } else {
1820 return String8::empty();
1821 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001822}
1823
Glenn Kasten23a75452014-01-13 10:37:17 -08001824bool AudioTrack::isOffloaded() const
1825{
1826 AutoMutex lock(mLock);
1827 return isOffloaded_l();
1828}
1829
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001830status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001831{
1832
1833 const size_t SIZE = 256;
1834 char buffer[SIZE];
1835 String8 result;
1836
1837 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001838 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
Glenn Kasten877a0ac2014-04-30 17:04:13 -07001839 mVolume[AUDIO_INTERLEAVE_LEFT], mVolume[AUDIO_INTERLEAVE_RIGHT]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001840 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001841 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001842 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001843 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001844 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001845 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001846 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001847 result.append(buffer);
1848 ::write(fd, result.string(), result.size());
1849 return NO_ERROR;
1850}
1851
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001852uint32_t AudioTrack::getUnderrunFrames() const
1853{
1854 AutoMutex lock(mLock);
1855 return mProxy->getUnderrunFrames();
1856}
1857
1858// =========================================================================
1859
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001860void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001861{
1862 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1863 if (audioTrack != 0) {
1864 AutoMutex lock(audioTrack->mLock);
1865 audioTrack->mProxy->binderDied();
1866 }
1867}
1868
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001869// =========================================================================
1870
1871AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001872 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1873 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001874{
1875}
1876
1877AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001878{
1879}
1880
1881bool AudioTrack::AudioTrackThread::threadLoop()
1882{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001883 {
1884 AutoMutex _l(mMyLock);
1885 if (mPaused) {
1886 mMyCond.wait(mMyLock);
1887 // caller will check for exitPending()
1888 return true;
1889 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001890 if (mIgnoreNextPausedInt) {
1891 mIgnoreNextPausedInt = false;
1892 mPausedInt = false;
1893 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001894 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001895 if (mPausedNs > 0) {
1896 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1897 } else {
1898 mMyCond.wait(mMyLock);
1899 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001900 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001901 return true;
1902 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001903 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001904 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001905 switch (ns) {
1906 case 0:
1907 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001908 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001909 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001910 return true;
1911 case NS_NEVER:
1912 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001913 case NS_WHENEVER:
1914 // FIXME increase poll interval, or make event-driven
1915 ns = 1000000000LL;
1916 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001917 default:
1918 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001919 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001920 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001921 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001922}
1923
Glenn Kasten3acbd052012-02-28 10:39:56 -08001924void AudioTrack::AudioTrackThread::requestExit()
1925{
1926 // must be in this order to avoid a race condition
1927 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001928 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001929}
1930
1931void AudioTrack::AudioTrackThread::pause()
1932{
1933 AutoMutex _l(mMyLock);
1934 mPaused = true;
1935}
1936
1937void AudioTrack::AudioTrackThread::resume()
1938{
1939 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001940 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001941 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001942 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001943 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001944 mMyCond.signal();
1945 }
1946}
1947
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001948void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1949{
1950 AutoMutex _l(mMyLock);
1951 mPausedInt = true;
1952 mPausedNs = ns;
1953}
1954
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001955}; // namespace android