blob: 1d5fc95a238a4ab79ad1cba80eb2a3ad5b41aa89 [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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080018//#define LOG_NDEBUG 0
19#define LOG_TAG "AudioTrack"
20
Mark Salyzyn34fb2962014-06-18 16:30:56 -070021#include <inttypes.h>
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>
Mark Salyzyn34fb2962014-06-18 16:30:56 -070024
Glenn Kasten9f80dd22012-12-18 15:57:32 -080025#include <audio_utils/primitives.h>
26#include <binder/IPCThreadState.h>
27#include <media/AudioTrack.h>
28#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070030#include <media/IAudioFlinger.h>
Eric Laurente83b55d2014-11-14 10:06:21 -080031#include <media/AudioPolicyHelper.h>
Andy Hungcd044842014-08-07 11:04:34 -070032#include <media/AudioResamplerPublic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080033
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010034#define WAIT_PERIOD_MS 10
35#define WAIT_STREAM_END_TIMEOUT_SEC 120
Andy Hung53c3b5f2014-12-15 16:42:05 -080036static const int kMaxLoopCountNotifications = 32;
Glenn Kasten511754b2012-01-11 09:52:19 -080037
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080038namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080039// ---------------------------------------------------------------------------
40
Andy Hung4ede21d2014-12-12 15:37:34 -080041template <typename T>
42const T &min(const T &x, const T &y) {
43 return x < y ? x : y;
44}
45
Andy Hung7f1bc8a2014-09-12 14:43:11 -070046static int64_t convertTimespecToUs(const struct timespec &tv)
47{
48 return tv.tv_sec * 1000000ll + tv.tv_nsec / 1000;
49}
50
51// current monotonic time in microseconds.
52static int64_t getNowUs()
53{
54 struct timespec tv;
55 (void) clock_gettime(CLOCK_MONOTONIC, &tv);
56 return convertTimespecToUs(tv);
57}
58
Chia-chi Yeh33005a92010-06-16 06:33:13 +080059// static
60status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080061 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080062 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080063 uint32_t sampleRate)
64{
Glenn Kastend65d73c2012-06-22 17:21:07 -070065 if (frameCount == NULL) {
66 return BAD_VALUE;
67 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070068
Andy Hung0e48d252015-01-26 11:43:15 -080069 // FIXME handle in server, like createTrack_l(), possible missing info:
Glenn Kastene0fa4672012-04-24 14:35:14 -070070 // audio_io_handle_t output
71 // audio_format_t format
72 // audio_channel_mask_t channelMask
Andy Hung0e48d252015-01-26 11:43:15 -080073 // audio_output_flags_t flags (FAST)
Glenn Kasten3b16c762012-11-14 08:44:39 -080074 uint32_t afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080075 status_t status;
76 status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
77 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080078 ALOGE("Unable to query output sample rate for stream type %d; status %d",
79 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080080 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080081 }
Glenn Kastene33054e2012-11-14 12:54:39 -080082 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080083 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
84 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080085 ALOGE("Unable to query output frame count for stream type %d; status %d",
86 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080087 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080088 }
89 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080090 status = AudioSystem::getOutputLatency(&afLatency, streamType);
91 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080092 ALOGE("Unable to query output latency for stream type %d; status %d",
93 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080094 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080095 }
96
97 // Ensure that buffer depth covers at least audio hardware latency
98 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080099 if (minBufCount < 2) {
100 minBufCount = 2;
101 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800102
Andy Hung0e48d252015-01-26 11:43:15 -0800103 *frameCount = minBufCount * sourceFramesNeeded(sampleRate, afFrameCount, afSampleRate);
104 // The formula above should always produce a non-zero value under normal circumstances:
105 // AudioTrack.SAMPLE_RATE_HZ_MIN <= sampleRate <= AudioTrack.SAMPLE_RATE_HZ_MAX.
106 // Return error in the unlikely event that it does not, as that's part of the API contract.
Glenn Kasten66a04672014-01-08 08:53:44 -0800107 if (*frameCount == 0) {
Andy Hung0e48d252015-01-26 11:43:15 -0800108 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %u",
Glenn Kasten66a04672014-01-08 08:53:44 -0800109 streamType, sampleRate);
110 return BAD_VALUE;
111 }
Andy Hung0e48d252015-01-26 11:43:15 -0800112 ALOGV("getMinFrameCount=%zu: afFrameCount=%zu, minBufCount=%u, afSampleRate=%u, afLatency=%u",
Glenn Kasten3acbd052012-02-28 10:39:56 -0800113 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800114 return NO_ERROR;
115}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116
117// ---------------------------------------------------------------------------
118
119AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -0700120 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800121 mIsTimed(false),
122 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800123 mPreviousSchedulingGroup(SP_DEFAULT),
124 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125{
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700126 mAttributes.content_type = AUDIO_CONTENT_TYPE_UNKNOWN;
127 mAttributes.usage = AUDIO_USAGE_UNKNOWN;
128 mAttributes.flags = 0x0;
129 strcpy(mAttributes.tags, "");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130}
131
132AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800133 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800135 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700136 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800137 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700138 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 callback_t cbf,
140 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800141 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800142 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000143 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800144 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800145 int uid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700146 pid_t pid,
147 const audio_attributes_t* pAttributes)
Glenn Kasten87913512011-06-22 16:15:25 -0700148 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800149 mIsTimed(false),
150 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800151 mPreviousSchedulingGroup(SP_DEFAULT),
152 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700154 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700155 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800156 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700157 offloadInfo, uid, pid, pAttributes);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158}
159
Andreas Huberc8139852012-01-18 10:51:55 -0800160AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800161 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800163 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700164 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700166 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167 callback_t cbf,
168 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800169 uint32_t notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800170 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000171 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800172 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800173 int uid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700174 pid_t pid,
175 const audio_attributes_t* pAttributes)
Glenn Kasten87913512011-06-22 16:15:25 -0700176 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 mIsTimed(false),
178 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800179 mPreviousSchedulingGroup(SP_DEFAULT),
180 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700182 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800183 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissend457c972014-02-11 08:47:07 -0800184 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700185 uid, pid, pAttributes);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186}
187
188AudioTrack::~AudioTrack()
189{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190 if (mStatus == NO_ERROR) {
191 // Make sure that callback function exits in the case where
192 // it is looping on buffer full condition in obtainBuffer().
193 // Otherwise the callback thread will never exit.
194 stop();
195 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100196 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800197 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198 mAudioTrackThread->requestExitAndWait();
199 mAudioTrackThread.clear();
200 }
Marco Nelissenf8880202014-11-14 07:58:25 -0800201 IInterface::asBinder(mAudioTrack)->unlinkToDeath(mDeathNotifier, this);
Glenn Kasten53cec222013-08-29 09:01:02 -0700202 mAudioTrack.clear();
Eric Laurent3bcffa12014-06-12 18:38:45 -0700203 mCblkMemory.clear();
204 mSharedBuffer.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 IPCThreadState::self()->flushCommands();
Marco Nelissend457c972014-02-11 08:47:07 -0800206 ALOGV("~AudioTrack, releasing session id from %d on behalf of %d",
207 IPCThreadState::self()->getCallingPid(), mClientPid);
208 AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 }
210}
211
212status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800213 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800215 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700216 audio_channel_mask_t channelMask,
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800217 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700218 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 callback_t cbf,
220 void* user,
Glenn Kasten838b3d82014-02-27 15:30:41 -0800221 uint32_t notificationFrames,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700223 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800224 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000225 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800226 const audio_offload_info_t *offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -0800227 int uid,
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700228 pid_t pid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700229 const audio_attributes_t* pAttributes)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230{
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800231 ALOGV("set(): streamType %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
Glenn Kasten838b3d82014-02-27 15:30:41 -0800232 "flags #%x, notificationFrames %u, sessionId %d, transferType %d",
Glenn Kastenbce50bf2014-02-27 15:29:51 -0800233 streamType, sampleRate, format, channelMask, frameCount, flags, notificationFrames,
Glenn Kasten86f04662014-02-24 15:13:05 -0800234 sessionId, transferType);
235
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800236 switch (transferType) {
237 case TRANSFER_DEFAULT:
238 if (sharedBuffer != 0) {
239 transferType = TRANSFER_SHARED;
240 } else if (cbf == NULL || threadCanCallJava) {
241 transferType = TRANSFER_SYNC;
242 } else {
243 transferType = TRANSFER_CALLBACK;
244 }
245 break;
246 case TRANSFER_CALLBACK:
247 if (cbf == NULL || sharedBuffer != 0) {
248 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
249 return BAD_VALUE;
250 }
251 break;
252 case TRANSFER_OBTAIN:
253 case TRANSFER_SYNC:
254 if (sharedBuffer != 0) {
255 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
256 return BAD_VALUE;
257 }
258 break;
259 case TRANSFER_SHARED:
260 if (sharedBuffer == 0) {
261 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
262 return BAD_VALUE;
263 }
264 break;
265 default:
266 ALOGE("Invalid transfer type %d", transferType);
267 return BAD_VALUE;
268 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800269 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800270 mTransfer = transferType;
271
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700272 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
273 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700275 ALOGV("set() streamType %d frameCount %zu flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700276
Eric Laurent1703cdf2011-03-07 14:52:59 -0800277 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800278
Glenn Kasten53cec222013-08-29 09:01:02 -0700279 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700280 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000281 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 return INVALID_OPERATION;
283 }
284
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 // handle default values first.
Eric Laurente83b55d2014-11-14 10:06:21 -0800286 if (streamType == AUDIO_STREAM_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700287 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800288 }
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700289 if (pAttributes == NULL) {
Eric Laurent223fd5c2014-11-11 13:43:36 -0800290 if (uint32_t(streamType) >= AUDIO_STREAM_PUBLIC_CNT) {
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700291 ALOGE("Invalid stream type %d", streamType);
292 return BAD_VALUE;
293 }
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700294 mStreamType = streamType;
Eric Laurente83b55d2014-11-14 10:06:21 -0800295
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700296 } else {
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700297 // stream type shouldn't be looked at, this track has audio attributes
298 memcpy(&mAttributes, pAttributes, sizeof(audio_attributes_t));
Jean-Michel Trivifaabb512014-06-11 16:55:06 -0700299 ALOGV("Building AudioTrack with attributes: usage=%d content=%d flags=0x%x tags=[%s]",
300 mAttributes.usage, mAttributes.content_type, mAttributes.flags, mAttributes.tags);
Eric Laurente83b55d2014-11-14 10:06:21 -0800301 mStreamType = AUDIO_STREAM_DEFAULT;
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800302 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700303
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800305 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700306 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308
309 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700310 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800311 ALOGE("Invalid format %#x", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 return BAD_VALUE;
313 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800314 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700315
Glenn Kasten8ba90322013-10-30 11:29:27 -0700316 if (!audio_is_output_channel(channelMask)) {
317 ALOGE("Invalid channel mask %#x", channelMask);
318 return BAD_VALUE;
319 }
Glenn Kastene3247bf2014-02-24 15:19:07 -0800320 mChannelMask = channelMask;
Andy Hunge5412692014-05-16 11:25:07 -0700321 uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
Glenn Kastene3247bf2014-02-24 15:19:07 -0800322 mChannelCount = channelCount;
Glenn Kasten8ba90322013-10-30 11:29:27 -0700323
Eric Laurentc2f1f072009-07-17 12:17:14 -0700324 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100325 // or offload was requested
326 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
327 || !audio_is_linear_pcm(format)) {
328 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
329 ? "Offload request, forcing to Direct Output"
330 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700331 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800332 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700333 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700334 }
335
Eric Laurentd1f69b02014-12-15 14:33:13 -0800336 // force direct flag if HW A/V sync requested
337 if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) {
338 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
339 }
340
Glenn Kastenb7730382014-04-30 15:50:31 -0700341 if (flags & AUDIO_OUTPUT_FLAG_DIRECT) {
342 if (audio_is_linear_pcm(format)) {
343 mFrameSize = channelCount * audio_bytes_per_sample(format);
344 } else {
345 mFrameSize = sizeof(uint8_t);
346 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800347 } else {
Glenn Kastenb7730382014-04-30 15:50:31 -0700348 ALOG_ASSERT(audio_is_linear_pcm(format));
349 mFrameSize = channelCount * audio_bytes_per_sample(format);
Glenn Kastenb7730382014-04-30 15:50:31 -0700350 // createTrack will return an error if PCM format is not supported by server,
351 // so no need to check for specific PCM formats here
Glenn Kastene3aa6592012-12-04 12:22:46 -0800352 }
353
Eric Laurent0d6db582014-11-12 18:39:44 -0800354 // sampling rate must be specified for direct outputs
355 if (sampleRate == 0 && (flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) {
356 return BAD_VALUE;
357 }
358 mSampleRate = sampleRate;
359
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800360 // Make copy of input parameter offloadInfo so that in the future:
361 // (a) createTrack_l doesn't need it as an input parameter
362 // (b) we can support re-creation of offloaded tracks
363 if (offloadInfo != NULL) {
364 mOffloadInfoCopy = *offloadInfo;
365 mOffloadInfo = &mOffloadInfoCopy;
366 } else {
367 mOffloadInfo = NULL;
368 }
369
Glenn Kasten66e46352014-01-16 17:44:23 -0800370 mVolume[AUDIO_INTERLEAVE_LEFT] = 1.0f;
371 mVolume[AUDIO_INTERLEAVE_RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800372 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800373 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800374 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700375 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800376 mNotificationFramesAct = 0;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800377 if (sessionId == AUDIO_SESSION_ALLOCATE) {
378 mSessionId = AudioSystem::newAudioUniqueId();
379 } else {
380 mSessionId = sessionId;
381 }
Marco Nelissend457c972014-02-11 08:47:07 -0800382 int callingpid = IPCThreadState::self()->getCallingPid();
383 int mypid = getpid();
384 if (uid == -1 || (callingpid != mypid)) {
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800385 mClientUid = IPCThreadState::self()->getCallingUid();
386 } else {
387 mClientUid = uid;
388 }
Marco Nelissend457c972014-02-11 08:47:07 -0800389 if (pid == -1 || (callingpid != mypid)) {
390 mClientPid = callingpid;
391 } else {
392 mClientPid = pid;
393 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700394 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700395 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700396 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700397
Glenn Kastena997e7a2012-08-07 09:44:19 -0700398 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700399 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700400 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
401 }
402
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800403 // create the IAudioTrack
Eric Laurent0d6db582014-11-12 18:39:44 -0800404 status_t status = createTrack_l();
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800405
Glenn Kastena997e7a2012-08-07 09:44:19 -0700406 if (status != NO_ERROR) {
407 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100408 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
409 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700410 mAudioTrackThread.clear();
411 }
412 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700413 }
414
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800416 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 mUserData = user;
Andy Hung4ede21d2014-12-12 15:37:34 -0800418 mLoopCount = 0;
419 mLoopStart = 0;
420 mLoopEnd = 0;
Andy Hung53c3b5f2014-12-15 16:42:05 -0800421 mLoopCountNotified = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700423 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 mNewPosition = 0;
425 mUpdatePeriod = 0;
Glenn Kasten200092b2014-08-15 15:13:30 -0700426 mServer = 0;
427 mPosition = 0;
428 mReleased = 0;
Andy Hung7f1bc8a2014-09-12 14:43:11 -0700429 mStartUs = 0;
Marco Nelissend457c972014-02-11 08:47:07 -0800430 AudioSystem::acquireAudioSessionId(mSessionId, mClientPid);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431 mSequence = 1;
432 mObservedSequence = mSequence;
433 mInUnderrun = false;
434
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 return NO_ERROR;
436}
437
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438// -------------------------------------------------------------------------
439
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100440status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800442 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100443
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800444 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100445 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446 }
447
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800448 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800450 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100451 if (previousState == STATE_PAUSED_STOPPING) {
452 mState = STATE_STOPPING;
453 } else {
454 mState = STATE_ACTIVE;
455 }
Glenn Kasten200092b2014-08-15 15:13:30 -0700456 (void) updateAndGetPosition_l();
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800457 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
458 // reset current position as seen by client to 0
Glenn Kasten200092b2014-08-15 15:13:30 -0700459 mPosition = 0;
Andy Hung7f1bc8a2014-09-12 14:43:11 -0700460 // For offloaded tracks, we don't know if the hardware counters are really zero here,
461 // since the flush is asynchronous and stop may not fully drain.
462 // We save the time when the track is started to later verify whether
463 // the counters are realistic (i.e. start from zero after this time).
464 mStartUs = getNowUs();
465
Eric Laurentec9a0322013-08-28 10:23:01 -0700466 // force refresh of remaining frames by processAudioBuffer() as last
467 // write before stop could be partial.
468 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800469 }
Glenn Kasten200092b2014-08-15 15:13:30 -0700470 mNewPosition = mPosition + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700471 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800472
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800474 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100475 if (previousState == STATE_STOPPING) {
476 mProxy->interrupt();
477 } else {
478 t->resume();
479 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800480 } else {
481 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
482 get_sched_policy(0, &mPreviousSchedulingGroup);
483 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
484 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800486 status_t status = NO_ERROR;
487 if (!(flags & CBLK_INVALID)) {
488 status = mAudioTrack->start();
489 if (status == DEAD_OBJECT) {
490 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800492 }
493 if (flags & CBLK_INVALID) {
494 status = restoreTrack_l("start");
495 }
496
497 if (status != NO_ERROR) {
498 ALOGE("start() status %d", status);
499 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100501 if (previousState != STATE_STOPPING) {
502 t->pause();
503 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700505 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700506 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507 }
508 }
509
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100510 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800511}
512
513void AudioTrack::stop()
514{
515 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700516 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800517 return;
518 }
519
Glenn Kasten23a75452014-01-13 10:37:17 -0800520 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100521 mState = STATE_STOPPING;
522 } else {
523 mState = STATE_STOPPED;
Andy Hungc2813e52014-10-16 17:54:34 -0700524 mReleased = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100525 }
526
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527 mProxy->interrupt();
528 mAudioTrack->stop();
529 // the playback head position will reset to 0, so if a marker is set, we need
530 // to activate it again
531 mMarkerReached = false;
Andy Hung9b461582014-12-01 17:56:29 -0800532
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800533 if (mSharedBuffer != 0) {
Andy Hung9b461582014-12-01 17:56:29 -0800534 // clear buffer position and loop count.
Andy Hung9b461582014-12-01 17:56:29 -0800535 mStaticProxy->setBufferPositionAndLoop(0 /* position */,
536 0 /* loopStart */, 0 /* loopEnd */, 0 /* loopCount */);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800537 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100538
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800539 sp<AudioTrackThread> t = mAudioTrackThread;
540 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800541 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100542 t->pause();
543 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800544 } else {
545 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
546 set_sched_policy(0, mPreviousSchedulingGroup);
547 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548}
549
550bool AudioTrack::stopped() const
551{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800552 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800553 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554}
555
556void AudioTrack::flush()
557{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800558 if (mSharedBuffer != 0) {
559 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800560 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800561 AutoMutex lock(mLock);
562 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
563 return;
564 }
565 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800566}
567
Eric Laurent1703cdf2011-03-07 14:52:59 -0800568void AudioTrack::flush_l()
569{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800570 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700571
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700572 // clear playback marker and periodic update counter
573 mMarkerPosition = 0;
574 mMarkerReached = false;
575 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100576 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700577
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800578 mState = STATE_FLUSHED;
Andy Hungc2813e52014-10-16 17:54:34 -0700579 mReleased = 0;
Glenn Kasten23a75452014-01-13 10:37:17 -0800580 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100581 mProxy->interrupt();
582 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800583 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800584 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585}
586
587void AudioTrack::pause()
588{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800589 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100590 if (mState == STATE_ACTIVE) {
591 mState = STATE_PAUSED;
592 } else if (mState == STATE_STOPPING) {
593 mState = STATE_PAUSED_STOPPING;
594 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800595 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800597 mProxy->interrupt();
598 mAudioTrack->pause();
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800599
Marco Nelissen3a90f282014-03-10 11:21:43 -0700600 if (isOffloaded_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700601 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Andy Hung7f1bc8a2014-09-12 14:43:11 -0700602 // An offload output can be re-used between two audio tracks having
603 // the same configuration. A timestamp query for a paused track
604 // while the other is running would return an incorrect time.
605 // To fix this, cache the playback position on a pause() and return
606 // this time when requested until the track is resumed.
607
608 // OffloadThread sends HAL pause in its threadLoop. Time saved
609 // here can be slightly off.
610
611 // TODO: check return code for getRenderPosition.
612
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800613 uint32_t halFrames;
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800614 AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition);
615 ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition);
616 }
617 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618}
619
Eric Laurentbe916aa2010-06-01 23:49:17 -0700620status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621{
Glenn Kastenc56f3422014-03-21 17:53:17 -0700622 // This duplicates a test by AudioTrack JNI, but that is not the only caller
623 if (isnanf(left) || left < GAIN_FLOAT_ZERO || left > GAIN_FLOAT_UNITY ||
624 isnanf(right) || right < GAIN_FLOAT_ZERO || right > GAIN_FLOAT_UNITY) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700625 return BAD_VALUE;
626 }
627
Eric Laurent1703cdf2011-03-07 14:52:59 -0800628 AutoMutex lock(mLock);
Glenn Kasten66e46352014-01-16 17:44:23 -0800629 mVolume[AUDIO_INTERLEAVE_LEFT] = left;
630 mVolume[AUDIO_INTERLEAVE_RIGHT] = right;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631
Glenn Kastenc56f3422014-03-21 17:53:17 -0700632 mProxy->setVolumeLR(gain_minifloat_pack(gain_from_float(left), gain_from_float(right)));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700633
Glenn Kasten23a75452014-01-13 10:37:17 -0800634 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700635 mAudioTrack->signal();
636 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700637 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638}
639
Glenn Kastenb1c09932012-02-27 16:21:04 -0800640status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800642 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700643}
644
Eric Laurent2beeb502010-07-16 07:43:46 -0700645status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700646{
Glenn Kastenc56f3422014-03-21 17:53:17 -0700647 // This duplicates a test by AudioTrack JNI, but that is not the only caller
648 if (isnanf(level) || level < GAIN_FLOAT_ZERO || level > GAIN_FLOAT_UNITY) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700649 return BAD_VALUE;
650 }
651
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800652 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700653 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800654 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700655
656 return NO_ERROR;
657}
658
Glenn Kastena5224f32012-01-04 12:41:44 -0800659void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700660{
661 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800662 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700663 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664}
665
Glenn Kasten3b16c762012-11-14 08:44:39 -0800666status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667{
Eric Laurentab5cdba2014-06-09 17:22:27 -0700668 if (mIsTimed || isOffloadedOrDirect()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800669 return INVALID_OPERATION;
670 }
671
Eric Laurent0d6db582014-11-12 18:39:44 -0800672 AutoMutex lock(mLock);
673 if (mOutput == AUDIO_IO_HANDLE_NONE) {
674 return NO_INIT;
675 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800676 uint32_t afSamplingRate;
Eric Laurent0d6db582014-11-12 18:39:44 -0800677 if (AudioSystem::getSamplingRate(mOutput, &afSamplingRate) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700678 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679 }
Andy Hungcd044842014-08-07 11:04:34 -0700680 if (rate == 0 || rate > afSamplingRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700681 return BAD_VALUE;
682 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683
Glenn Kastene3aa6592012-12-04 12:22:46 -0800684 mSampleRate = rate;
685 mProxy->setSampleRate(rate);
686
Eric Laurent57326622009-07-07 07:10:45 -0700687 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688}
689
Glenn Kastena5224f32012-01-04 12:41:44 -0800690uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691{
John Grossman4ff14ba2012-02-08 16:37:41 -0800692 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800693 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800694 }
695
Eric Laurent1703cdf2011-03-07 14:52:59 -0800696 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700697
698 // sample rate can be updated during playback by the offloaded decoder so we need to
699 // query the HAL and update if needed.
700// FIXME use Proxy return channel to update the rate from server and avoid polling here
Eric Laurentab5cdba2014-06-09 17:22:27 -0700701 if (isOffloadedOrDirect_l()) {
Glenn Kasten142f5192014-03-25 17:44:59 -0700702 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700703 uint32_t sampleRate = 0;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700704 status_t status = AudioSystem::getSamplingRate(mOutput, &sampleRate);
Eric Laurent6f59db12013-07-26 17:16:50 -0700705 if (status == NO_ERROR) {
706 mSampleRate = sampleRate;
707 }
708 }
709 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800710 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711}
712
713status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
714{
Eric Laurentab5cdba2014-06-09 17:22:27 -0700715 if (mSharedBuffer == 0 || mIsTimed || isOffloadedOrDirect()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800716 return INVALID_OPERATION;
717 }
718
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800720 ;
721 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
722 loopEnd - loopStart >= MIN_LOOP) {
723 ;
724 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800725 return BAD_VALUE;
726 }
727
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800728 AutoMutex lock(mLock);
729 // See setPosition() regarding setting parameters such as loop points or position while active
730 if (mState == STATE_ACTIVE) {
731 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700732 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800733 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800734 return NO_ERROR;
735}
736
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800737void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
738{
Andy Hung4ede21d2014-12-12 15:37:34 -0800739 // We do not update the periodic notification point.
740 // mNewPosition = updateAndGetPosition_l() + mUpdatePeriod;
741 mLoopCount = loopCount;
742 mLoopEnd = loopEnd;
743 mLoopStart = loopStart;
Andy Hung53c3b5f2014-12-15 16:42:05 -0800744 mLoopCountNotified = loopCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800745 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
Andy Hung3c09c782014-12-29 18:39:32 -0800746
747 // Waking the AudioTrackThread is not needed as this cannot be called when active.
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800748}
749
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750status_t AudioTrack::setMarkerPosition(uint32_t marker)
751{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700752 // The only purpose of setting marker position is to get a callback
Eric Laurentab5cdba2014-06-09 17:22:27 -0700753 if (mCbf == NULL || isOffloadedOrDirect()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700754 return INVALID_OPERATION;
755 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800757 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800758 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700759 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760
Andy Hung3c09c782014-12-29 18:39:32 -0800761 sp<AudioTrackThread> t = mAudioTrackThread;
762 if (t != 0) {
763 t->wake();
764 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 return NO_ERROR;
766}
767
Glenn Kastena5224f32012-01-04 12:41:44 -0800768status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769{
Eric Laurentab5cdba2014-06-09 17:22:27 -0700770 if (isOffloadedOrDirect()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100771 return INVALID_OPERATION;
772 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700773 if (marker == NULL) {
774 return BAD_VALUE;
775 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800776
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800777 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778 *marker = mMarkerPosition;
779
780 return NO_ERROR;
781}
782
783status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
784{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700785 // The only purpose of setting position update period is to get a callback
Eric Laurentab5cdba2014-06-09 17:22:27 -0700786 if (mCbf == NULL || isOffloadedOrDirect()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700787 return INVALID_OPERATION;
788 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800789
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800790 AutoMutex lock(mLock);
Glenn Kasten200092b2014-08-15 15:13:30 -0700791 mNewPosition = updateAndGetPosition_l() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800792 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800793
Andy Hung3c09c782014-12-29 18:39:32 -0800794 sp<AudioTrackThread> t = mAudioTrackThread;
795 if (t != 0) {
796 t->wake();
797 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800798 return NO_ERROR;
799}
800
Glenn Kastena5224f32012-01-04 12:41:44 -0800801status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800802{
Eric Laurentab5cdba2014-06-09 17:22:27 -0700803 if (isOffloadedOrDirect()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100804 return INVALID_OPERATION;
805 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700806 if (updatePeriod == NULL) {
807 return BAD_VALUE;
808 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800809
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800810 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800811 *updatePeriod = mUpdatePeriod;
812
813 return NO_ERROR;
814}
815
816status_t AudioTrack::setPosition(uint32_t position)
817{
Eric Laurentab5cdba2014-06-09 17:22:27 -0700818 if (mSharedBuffer == 0 || mIsTimed || isOffloadedOrDirect()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700819 return INVALID_OPERATION;
820 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800821 if (position > mFrameCount) {
822 return BAD_VALUE;
823 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800824
Eric Laurent1703cdf2011-03-07 14:52:59 -0800825 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800826 // Currently we require that the player is inactive before setting parameters such as position
827 // or loop points. Otherwise, there could be a race condition: the application could read the
828 // current position, compute a new position or loop parameters, and then set that position or
829 // loop parameters but it would do the "wrong" thing since the position has continued to advance
830 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
831 // to specify how it wants to handle such scenarios.
832 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700833 return INVALID_OPERATION;
834 }
Andy Hung9b461582014-12-01 17:56:29 -0800835 // After setting the position, use full update period before notification.
Glenn Kasten200092b2014-08-15 15:13:30 -0700836 mNewPosition = updateAndGetPosition_l() + mUpdatePeriod;
Andy Hung9b461582014-12-01 17:56:29 -0800837 mStaticProxy->setBufferPosition(position);
Andy Hung3c09c782014-12-29 18:39:32 -0800838
839 // Waking the AudioTrackThread is not needed as this cannot be called when active.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800840 return NO_ERROR;
841}
842
Glenn Kasten200092b2014-08-15 15:13:30 -0700843status_t AudioTrack::getPosition(uint32_t *position)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700845 if (position == NULL) {
846 return BAD_VALUE;
847 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800848
Eric Laurent1703cdf2011-03-07 14:52:59 -0800849 AutoMutex lock(mLock);
Eric Laurentab5cdba2014-06-09 17:22:27 -0700850 if (isOffloadedOrDirect_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100851 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800852
Eric Laurentab5cdba2014-06-09 17:22:27 -0700853 if (isOffloaded_l() && ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING))) {
Haynes Mathew George7064fd22014-01-08 13:59:53 -0800854 ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition);
855 *position = mPausedPosition;
856 return NO_ERROR;
857 }
858
Glenn Kasten142f5192014-03-25 17:44:59 -0700859 if (mOutput != AUDIO_IO_HANDLE_NONE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100860 uint32_t halFrames;
861 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
862 }
Andy Hung7f1bc8a2014-09-12 14:43:11 -0700863 // FIXME: dspFrames may not be zero in (mState == STATE_STOPPED || mState == STATE_FLUSHED)
864 // due to hardware latency. We leave this behavior for now.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100865 *position = dspFrames;
866 } else {
Eric Laurent275e8e92014-11-30 15:14:47 -0800867 if (mCblk->mFlags & CBLK_INVALID) {
868 restoreTrack_l("getPosition");
869 }
870
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100871 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
Glenn Kasten200092b2014-08-15 15:13:30 -0700872 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ?
873 0 : updateAndGetPosition_l();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100874 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800875 return NO_ERROR;
876}
877
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000878status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800879{
880 if (mSharedBuffer == 0 || mIsTimed) {
881 return INVALID_OPERATION;
882 }
883 if (position == NULL) {
884 return BAD_VALUE;
885 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800886
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800887 AutoMutex lock(mLock);
888 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800889 return NO_ERROR;
890}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800891
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800892status_t AudioTrack::reload()
893{
Eric Laurentab5cdba2014-06-09 17:22:27 -0700894 if (mSharedBuffer == 0 || mIsTimed || isOffloadedOrDirect()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800895 return INVALID_OPERATION;
896 }
897
Eric Laurent1703cdf2011-03-07 14:52:59 -0800898 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800899 // See setPosition() regarding setting parameters such as loop points or position while active
900 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700901 return INVALID_OPERATION;
902 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800903 mNewPosition = mUpdatePeriod;
Andy Hung9b461582014-12-01 17:56:29 -0800904 (void) updateAndGetPosition_l();
905 mPosition = 0;
Andy Hung53c3b5f2014-12-15 16:42:05 -0800906#if 0
Andy Hung9b461582014-12-01 17:56:29 -0800907 // The documentation is not clear on the behavior of reload() and the restoration
Andy Hung53c3b5f2014-12-15 16:42:05 -0800908 // of loop count. Historically we have not restored loop count, start, end,
909 // but it makes sense if one desires to repeat playing a particular sound.
910 if (mLoopCount != 0) {
911 mLoopCountNotified = mLoopCount;
912 mStaticProxy->setLoop(mLoopStart, mLoopEnd, mLoopCount);
913 }
914#endif
Andy Hung9b461582014-12-01 17:56:29 -0800915 mStaticProxy->setBufferPosition(0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800916 return NO_ERROR;
917}
918
Glenn Kasten38e905b2014-01-13 10:21:48 -0800919audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700920{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800921 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100922 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800923}
924
Eric Laurentbe916aa2010-06-01 23:49:17 -0700925status_t AudioTrack::attachAuxEffect(int effectId)
926{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800927 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700928 status_t status = mAudioTrack->attachAuxEffect(effectId);
929 if (status == NO_ERROR) {
930 mAuxEffectId = effectId;
931 }
932 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700933}
934
Eric Laurente83b55d2014-11-14 10:06:21 -0800935audio_stream_type_t AudioTrack::streamType() const
936{
937 if (mStreamType == AUDIO_STREAM_DEFAULT) {
938 return audio_attributes_to_stream_type(&mAttributes);
939 }
940 return mStreamType;
941}
942
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943// -------------------------------------------------------------------------
944
Eric Laurent1703cdf2011-03-07 14:52:59 -0800945// must be called with mLock held
Glenn Kasten200092b2014-08-15 15:13:30 -0700946status_t AudioTrack::createTrack_l()
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800947{
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800948 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
949 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700950 ALOGE("Could not get audioflinger");
951 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800952 }
953
Eric Laurente83b55d2014-11-14 10:06:21 -0800954 audio_io_handle_t output;
955 audio_stream_type_t streamType = mStreamType;
956 audio_attributes_t *attr = (mStreamType == AUDIO_STREAM_DEFAULT) ? &mAttributes : NULL;
957 status_t status = AudioSystem::getOutputForAttr(attr, &output,
958 (audio_session_t)mSessionId, &streamType,
959 mSampleRate, mFormat, mChannelMask,
960 mFlags, mOffloadInfo);
961
962
963 if (status != NO_ERROR || output == AUDIO_IO_HANDLE_NONE) {
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700964 ALOGE("Could not get audio output for stream type %d, usage %d, sample rate %u, format %#x,"
965 " channel mask %#x, flags %#x",
Eric Laurente83b55d2014-11-14 10:06:21 -0800966 streamType, mAttributes.usage, mSampleRate, mFormat, mChannelMask, mFlags);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800967 return BAD_VALUE;
968 }
969 {
970 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
971 // we must release it ourselves if anything goes wrong.
972
Glenn Kastence8828a2013-09-16 18:07:38 -0700973 // Not all of these values are needed under all conditions, but it is easier to get them all
974
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975 uint32_t afLatency;
Glenn Kasten241618f2014-03-25 17:48:57 -0700976 status = AudioSystem::getLatency(output, &afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700977 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800978 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800979 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700980 }
981
Glenn Kastence8828a2013-09-16 18:07:38 -0700982 size_t afFrameCount;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700983 status = AudioSystem::getFrameCount(output, &afFrameCount);
Glenn Kastence8828a2013-09-16 18:07:38 -0700984 if (status != NO_ERROR) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700985 ALOGE("getFrameCount(output=%d) status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800986 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700987 }
988
989 uint32_t afSampleRate;
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700990 status = AudioSystem::getSamplingRate(output, &afSampleRate);
Glenn Kastence8828a2013-09-16 18:07:38 -0700991 if (status != NO_ERROR) {
Jean-Michel Trivib7f24b12014-06-11 10:05:30 -0700992 ALOGE("getSamplingRate(output=%d) status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800993 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700994 }
Eric Laurent0d6db582014-11-12 18:39:44 -0800995 if (mSampleRate == 0) {
996 mSampleRate = afSampleRate;
997 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700998 // Client decides whether the track is TIMED (see below), but can only express a preference
999 // for FAST. Server will perform additional tests.
Glenn Kasten43bdc1d2014-02-10 09:53:55 -08001000 if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !((
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001001 // either of these use cases:
1002 // use case 1: shared buffer
Glenn Kasten363fb752014-01-15 12:27:31 -08001003 (mSharedBuffer != 0) ||
Glenn Kastenc6ba8232014-02-27 13:34:29 -08001004 // use case 2: callback transfer mode
1005 (mTransfer == TRANSFER_CALLBACK)) &&
Glenn Kasten43bdc1d2014-02-10 09:53:55 -08001006 // matching sample rate
1007 (mSampleRate == afSampleRate))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001008 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -07001009 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001010 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001011 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001012 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001013
Glenn Kastence8828a2013-09-16 18:07:38 -07001014 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -08001015 // n = 1 fast track with single buffering; nBuffering is ignored
1016 // n = 2 fast track with double buffering
Andy Hung0e48d252015-01-26 11:43:15 -08001017 // n = 2 normal track, (including those with sample rate conversion)
1018 // n >= 3 very high latency or very small notification interval (unused).
1019 const uint32_t nBuffering = 2;
Glenn Kastence8828a2013-09-16 18:07:38 -07001020
Eric Laurentd1b449a2010-05-14 03:26:45 -07001021 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -07001022
Glenn Kasten363fb752014-01-15 12:27:31 -08001023 size_t frameCount = mReqFrameCount;
1024 if (!audio_is_linear_pcm(mFormat)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001025
Glenn Kasten363fb752014-01-15 12:27:31 -08001026 if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001027 // Same comment as below about ignoring frameCount parameter for set()
Glenn Kasten363fb752014-01-15 12:27:31 -08001028 frameCount = mSharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -07001029 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001030 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001031 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001032 if (mNotificationFramesAct != frameCount) {
1033 mNotificationFramesAct = frameCount;
1034 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001035 } else if (mSharedBuffer != 0) {
Andy Hungabdb9902015-01-12 15:08:22 -08001036 // FIXME: Ensure client side memory buffers need
1037 // not have additional alignment beyond sample
1038 // (e.g. 16 bit stereo accessed as 32 bit frame).
1039 size_t alignment = audio_bytes_per_sample(mFormat);
Glenn Kastenb7730382014-04-30 15:50:31 -07001040 if (alignment & 1) {
Andy Hungabdb9902015-01-12 15:08:22 -08001041 // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java).
Glenn Kastenb7730382014-04-30 15:50:31 -07001042 alignment = 1;
1043 }
Glenn Kastena42ff002012-11-14 12:47:55 -08001044 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001045 // More than 2 channels does not require stronger alignment than stereo
1046 alignment <<= 1;
1047 }
Narayan Kamath1d6fa7a2014-02-11 13:47:53 +00001048 if (((uintptr_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -08001049 ALOGE("Invalid buffer alignment: address %p, channel count %u",
Glenn Kasten363fb752014-01-15 12:27:31 -08001050 mSharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001051 status = BAD_VALUE;
1052 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -07001053 }
1054
1055 // When initializing a shared buffer AudioTrack via constructors,
1056 // there's no frameCount parameter.
1057 // But when initializing a shared buffer AudioTrack via set(),
1058 // there _is_ a frameCount parameter. We silently ignore it.
Andy Hungabdb9902015-01-12 15:08:22 -08001059 frameCount = mSharedBuffer->size() / mFrameSize;
Glenn Kastene0fa4672012-04-24 14:35:14 -07001060 } else {
Andy Hung0e48d252015-01-26 11:43:15 -08001061 // For fast and normal streaming tracks,
1062 // the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -07001063 }
1064
Glenn Kastena075db42012-03-06 11:22:44 -08001065 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
1066 if (mIsTimed) {
1067 trackFlags |= IAudioFlinger::TRACK_TIMED;
1068 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001069
1070 pid_t tid = -1;
Glenn Kasten363fb752014-01-15 12:27:31 -08001071 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001072 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001073 if (mAudioTrackThread != 0) {
1074 tid = mAudioTrackThread->getTid();
1075 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001076 }
1077
Glenn Kasten363fb752014-01-15 12:27:31 -08001078 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001079 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1080 }
1081
Eric Laurentab5cdba2014-06-09 17:22:27 -07001082 if (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1083 trackFlags |= IAudioFlinger::TRACK_DIRECT;
1084 }
1085
Glenn Kasten74935e42013-12-19 08:56:45 -08001086 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
1087 // but we will still need the original value also
Eric Laurente83b55d2014-11-14 10:06:21 -08001088 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Glenn Kasten363fb752014-01-15 12:27:31 -08001089 mSampleRate,
Andy Hungabdb9902015-01-12 15:08:22 -08001090 mFormat,
Glenn Kastena42ff002012-11-14 12:47:55 -08001091 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001092 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -08001093 &trackFlags,
Glenn Kasten363fb752014-01-15 12:27:31 -08001094 mSharedBuffer,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001095 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001096 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001097 &mSessionId,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001098 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001099 &status);
1100
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001101 if (status != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00001102 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001103 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001104 }
Glenn Kastenc08d20b2014-02-24 15:21:10 -08001105 ALOG_ASSERT(track != 0);
1106
Glenn Kasten38e905b2014-01-13 10:21:48 -08001107 // AudioFlinger now owns the reference to the I/O handle,
1108 // so we are no longer responsible for releasing it.
1109
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001110 sp<IMemory> iMem = track->getCblk();
1111 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001112 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001113 return NO_INIT;
1114 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001115 void *iMemPointer = iMem->pointer();
1116 if (iMemPointer == NULL) {
1117 ALOGE("Could not get control block pointer");
1118 return NO_INIT;
1119 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001120 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001121 if (mAudioTrack != 0) {
Marco Nelissenf8880202014-11-14 07:58:25 -08001122 IInterface::asBinder(mAudioTrack)->unlinkToDeath(mDeathNotifier, this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001123 mDeathNotifier.clear();
1124 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001125 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001126 mCblkMemory = iMem;
Eric Laurent3bcffa12014-06-12 18:38:45 -07001127 IPCThreadState::self()->flushCommands();
1128
Glenn Kasten0cde0762014-01-16 15:06:36 -08001129 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001130 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001131 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001132 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1133 // In current design, AudioTrack client checks and ensures frame count validity before
1134 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1135 // for fast track as it uses a special method of assigning frame count.
Mark Salyzyn34fb2962014-06-18 16:30:56 -07001136 ALOGW("Requested frameCount %zu but received frameCount %zu", frameCount, temp);
Glenn Kastenb6037442012-11-14 13:42:25 -08001137 }
1138 frameCount = temp;
Glenn Kasten5f631512014-02-24 15:16:07 -08001139
Glenn Kastena07f17c2013-04-23 12:39:37 -07001140 mAwaitBoost = false;
Glenn Kasten363fb752014-01-15 12:27:31 -08001141 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001142 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -07001143 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %zu", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001144 mAwaitBoost = true;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001145 } else {
Mark Salyzyn34fb2962014-06-18 16:30:56 -07001146 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %zu", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001147 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001148 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001149 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001150 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001151 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001152 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1153 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1154 } else {
1155 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
Glenn Kasten363fb752014-01-15 12:27:31 -08001156 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001157 // FIXME This is a warning, not an error, so don't return error status
1158 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001159 }
1160 }
Eric Laurentab5cdba2014-06-09 17:22:27 -07001161 if (mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
1162 if (trackFlags & IAudioFlinger::TRACK_DIRECT) {
1163 ALOGV("AUDIO_OUTPUT_FLAG_DIRECT successful");
1164 } else {
1165 ALOGW("AUDIO_OUTPUT_FLAG_DIRECT denied by server");
1166 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_DIRECT);
1167 // FIXME This is a warning, not an error, so don't return error status
1168 //return NO_INIT;
1169 }
1170 }
Andy Hung0e48d252015-01-26 11:43:15 -08001171 // Make sure that application is notified with sufficient margin before underrun
1172 if (mSharedBuffer == 0 && audio_is_linear_pcm(mFormat)) {
1173 // Theoretically double-buffering is not required for fast tracks,
1174 // due to tighter scheduling. But in practice, to accommodate kernels with
1175 // scheduling jitter, and apps with computation jitter, we use double-buffering
1176 // for fast tracks just like normal streaming tracks.
1177 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount / nBuffering) {
1178 mNotificationFramesAct = frameCount / nBuffering;
1179 }
1180 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001181
Glenn Kasten38e905b2014-01-13 10:21:48 -08001182 // We retain a copy of the I/O handle, but don't own the reference
1183 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001184 mRefreshRemaining = true;
1185
1186 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1187 // is the value of pointer() for the shared buffer, otherwise buffers points
1188 // immediately after the control block. This address is for the mapping within client
1189 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1190 void* buffers;
Glenn Kasten363fb752014-01-15 12:27:31 -08001191 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001192 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001193 } else {
Glenn Kasten363fb752014-01-15 12:27:31 -08001194 buffers = mSharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001195 }
1196
Eric Laurent2beeb502010-07-16 07:43:46 -07001197 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001198 // FIXME don't believe this lie
Glenn Kasten363fb752014-01-15 12:27:31 -08001199 mLatency = afLatency + (1000*frameCount) / mSampleRate;
Glenn Kasten5f631512014-02-24 15:16:07 -08001200
Glenn Kastenb6037442012-11-14 13:42:25 -08001201 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001202 // If IAudioTrack is re-created, don't let the requested frameCount
1203 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001204 if (frameCount > mReqFrameCount) {
1205 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001206 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001207
1208 // update proxy
Glenn Kasten363fb752014-01-15 12:27:31 -08001209 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001210 mStaticProxy.clear();
Andy Hungabdb9902015-01-12 15:08:22 -08001211 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001212 } else {
Andy Hungabdb9902015-01-12 15:08:22 -08001213 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001214 mProxy = mStaticProxy;
1215 }
seunghak.hane6a9d6582014-11-22 15:22:35 +09001216
1217 mProxy->setVolumeLR(gain_minifloat_pack(
1218 gain_from_float(mVolume[AUDIO_INTERLEAVE_LEFT]),
1219 gain_from_float(mVolume[AUDIO_INTERLEAVE_RIGHT])));
1220
Glenn Kastene3aa6592012-12-04 12:22:46 -08001221 mProxy->setSendLevel(mSendLevel);
1222 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001223 mProxy->setMinimum(mNotificationFramesAct);
1224
1225 mDeathNotifier = new DeathNotifier(this);
Marco Nelissenf8880202014-11-14 07:58:25 -08001226 IInterface::asBinder(mAudioTrack)->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001227
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001228 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001229 }
1230
1231release:
Eric Laurente83b55d2014-11-14 10:06:21 -08001232 AudioSystem::releaseOutput(output, streamType, (audio_session_t)mSessionId);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001233 if (status == NO_ERROR) {
1234 status = NO_INIT;
1235 }
1236 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001237}
1238
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001239status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1240{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001241 if (audioBuffer == NULL) {
1242 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001243 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001244 if (mTransfer != TRANSFER_OBTAIN) {
1245 audioBuffer->frameCount = 0;
1246 audioBuffer->size = 0;
1247 audioBuffer->raw = NULL;
1248 return INVALID_OPERATION;
1249 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001250
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001251 const struct timespec *requested;
Eric Laurentdf576992014-01-27 18:13:39 -08001252 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001253 if (waitCount == -1) {
1254 requested = &ClientProxy::kForever;
1255 } else if (waitCount == 0) {
1256 requested = &ClientProxy::kNonBlocking;
1257 } else if (waitCount > 0) {
1258 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001259 timeout.tv_sec = ms / 1000;
1260 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1261 requested = &timeout;
1262 } else {
1263 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1264 requested = NULL;
1265 }
1266 return obtainBuffer(audioBuffer, requested);
1267}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001268
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001269status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1270 struct timespec *elapsed, size_t *nonContig)
1271{
1272 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1273 uint32_t oldSequence = 0;
1274 uint32_t newSequence;
1275
1276 Proxy::Buffer buffer;
1277 status_t status = NO_ERROR;
1278
1279 static const int32_t kMaxTries = 5;
1280 int32_t tryCounter = kMaxTries;
1281
1282 do {
1283 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1284 // keep them from going away if another thread re-creates the track during obtainBuffer()
1285 sp<AudioTrackClientProxy> proxy;
1286 sp<IMemory> iMem;
1287
1288 { // start of lock scope
1289 AutoMutex lock(mLock);
1290
1291 newSequence = mSequence;
1292 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1293 if (status == DEAD_OBJECT) {
1294 // re-create track, unless someone else has already done so
1295 if (newSequence == oldSequence) {
1296 status = restoreTrack_l("obtainBuffer");
1297 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001298 buffer.mFrameCount = 0;
1299 buffer.mRaw = NULL;
1300 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001301 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001303 }
1304 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001305 oldSequence = newSequence;
1306
1307 // Keep the extra references
1308 proxy = mProxy;
1309 iMem = mCblkMemory;
1310
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001311 if (mState == STATE_STOPPING) {
1312 status = -EINTR;
1313 buffer.mFrameCount = 0;
1314 buffer.mRaw = NULL;
1315 buffer.mNonContig = 0;
1316 break;
1317 }
1318
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001319 // Non-blocking if track is stopped or paused
1320 if (mState != STATE_ACTIVE) {
1321 requested = &ClientProxy::kNonBlocking;
1322 }
1323
1324 } // end of lock scope
1325
1326 buffer.mFrameCount = audioBuffer->frameCount;
1327 // FIXME starts the requested timeout and elapsed over from scratch
1328 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1329
1330 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1331
1332 audioBuffer->frameCount = buffer.mFrameCount;
Andy Hungabdb9902015-01-12 15:08:22 -08001333 audioBuffer->size = buffer.mFrameCount * mFrameSize;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001334 audioBuffer->raw = buffer.mRaw;
1335 if (nonContig != NULL) {
1336 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001337 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001338 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001339}
1340
1341void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1342{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001343 if (mTransfer == TRANSFER_SHARED) {
1344 return;
1345 }
1346
Andy Hungabdb9902015-01-12 15:08:22 -08001347 size_t stepCount = audioBuffer->size / mFrameSize;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001348 if (stepCount == 0) {
1349 return;
1350 }
1351
1352 Proxy::Buffer buffer;
1353 buffer.mFrameCount = stepCount;
1354 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001355
Eric Laurent1703cdf2011-03-07 14:52:59 -08001356 AutoMutex lock(mLock);
Glenn Kasten200092b2014-08-15 15:13:30 -07001357 mReleased += stepCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001358 mInUnderrun = false;
1359 mProxy->releaseBuffer(&buffer);
1360
1361 // restart track if it was disabled by audioflinger due to previous underrun
1362 if (mState == STATE_ACTIVE) {
1363 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001364 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastenc5a17422014-03-13 14:59:59 -07001365 ALOGW("releaseBuffer() track %p disabled due to previous underrun, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001366 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001367 mAudioTrack->start();
1368 }
1369 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001370}
1371
1372// -------------------------------------------------------------------------
1373
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001374ssize_t AudioTrack::write(const void* buffer, size_t userSize, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001375{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001376 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001377 return INVALID_OPERATION;
1378 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001379
Eric Laurentab5cdba2014-06-09 17:22:27 -07001380 if (isDirect()) {
1381 AutoMutex lock(mLock);
1382 int32_t flags = android_atomic_and(
1383 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END),
1384 &mCblk->mFlags);
1385 if (flags & CBLK_INVALID) {
1386 return DEAD_OBJECT;
1387 }
1388 }
1389
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001390 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001391 // Sanity-check: user is most-likely passing an error code, and it would
1392 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001393 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001394 return BAD_VALUE;
1395 }
1396
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001397 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398 Buffer audioBuffer;
1399
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001400 while (userSize >= mFrameSize) {
1401 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001402
Jean-Michel Trivi720ad9d2014-02-04 11:00:59 -08001403 status_t err = obtainBuffer(&audioBuffer,
1404 blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001406 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001407 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001408 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001409 return ssize_t(err);
1410 }
1411
1412 size_t toWrite;
Andy Hungabdb9902015-01-12 15:08:22 -08001413 toWrite = audioBuffer.size;
1414 memcpy(audioBuffer.i8, buffer, toWrite);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001415 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001416 userSize -= toWrite;
1417 written += toWrite;
1418
1419 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001420 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001421
1422 return written;
1423}
1424
1425// -------------------------------------------------------------------------
1426
John Grossman4ff14ba2012-02-08 16:37:41 -08001427TimedAudioTrack::TimedAudioTrack() {
1428 mIsTimed = true;
1429}
1430
1431status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1432{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001433 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001434 status_t result = UNKNOWN_ERROR;
1435
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001436#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001437 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1438 // while we are accessing the cblk
1439 sp<IAudioTrack> audioTrack = mAudioTrack;
1440 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001441#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001442
John Grossman4ff14ba2012-02-08 16:37:41 -08001443 // If the track is not invalid already, try to allocate a buffer. alloc
1444 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001445 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001446 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001447 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001448 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1449 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001450 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001451 }
1452 }
1453
1454 // If the track is invalid at this point, attempt to restore it. and try the
1455 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001456 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001457 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001458
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001459 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001460 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001461 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001462 }
1463
1464 return result;
1465}
1466
1467status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1468 int64_t pts)
1469{
Eric Laurentdf839842012-05-31 14:27:14 -07001470 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1471 {
1472 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001473 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001474 // restart track if it was disabled by audioflinger due to previous underrun
1475 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001476 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1477 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001478 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001479 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001480 mAudioTrack->start();
1481 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001482 }
Eric Laurentdf839842012-05-31 14:27:14 -07001483 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001484}
1485
1486status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1487 TargetTimeline target)
1488{
1489 return mAudioTrack->setMediaTimeTransform(xform, target);
1490}
1491
1492// -------------------------------------------------------------------------
1493
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001494nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001495{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001496 // Currently the AudioTrack thread is not created if there are no callbacks.
1497 // Would it ever make sense to run the thread, even without callbacks?
1498 // If so, then replace this by checks at each use for mCbf != NULL.
1499 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1500
Eric Laurent1703cdf2011-03-07 14:52:59 -08001501 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001502 if (mAwaitBoost) {
1503 mAwaitBoost = false;
1504 mLock.unlock();
1505 static const int32_t kMaxTries = 5;
1506 int32_t tryCounter = kMaxTries;
1507 uint32_t pollUs = 10000;
1508 do {
1509 int policy = sched_getscheduler(0);
1510 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1511 break;
1512 }
1513 usleep(pollUs);
1514 pollUs <<= 1;
1515 } while (tryCounter-- > 0);
1516 if (tryCounter < 0) {
1517 ALOGE("did not receive expected priority boost on time");
1518 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001519 // Run again immediately
1520 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001521 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001522
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001523 // Can only reference mCblk while locked
1524 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001525 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001526
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001527 // Check for track invalidation
1528 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001529 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1530 // AudioSystem cache. We should not exit here but after calling the callback so
1531 // that the upper layers can recreate the track
Eric Laurentab5cdba2014-06-09 17:22:27 -07001532 if (!isOffloadedOrDirect_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001533 status_t status = restoreTrack_l("processAudioBuffer");
Andy Hung53c3b5f2014-12-15 16:42:05 -08001534 // after restoration, continue below to make sure that the loop and buffer events
1535 // are notified because they have been cleared from mCblk->mFlags above.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001536 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001537 }
1538
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001539 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001540 bool active = mState == STATE_ACTIVE;
1541
1542 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1543 bool newUnderrun = false;
1544 if (flags & CBLK_UNDERRUN) {
1545#if 0
1546 // Currently in shared buffer mode, when the server reaches the end of buffer,
1547 // the track stays active in continuous underrun state. It's up to the application
1548 // to pause or stop the track, or set the position to a new offset within buffer.
1549 // This was some experimental code to auto-pause on underrun. Keeping it here
1550 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1551 if (mTransfer == TRANSFER_SHARED) {
1552 mState = STATE_PAUSED;
1553 active = false;
1554 }
1555#endif
1556 if (!mInUnderrun) {
1557 mInUnderrun = true;
1558 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559 }
1560 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001561
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001562 // Get current position of server
Glenn Kasten200092b2014-08-15 15:13:30 -07001563 size_t position = updateAndGetPosition_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001564
1565 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001566 bool markerReached = false;
1567 size_t markerPosition = mMarkerPosition;
1568 // FIXME fails for wraparound, need 64 bits
1569 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1570 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001571 }
1572
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001573 // Determine number of new position callback(s) that will be needed, while locked
1574 size_t newPosCount = 0;
1575 size_t newPosition = mNewPosition;
1576 size_t updatePeriod = mUpdatePeriod;
1577 // FIXME fails for wraparound, need 64 bits
1578 if (updatePeriod > 0 && position >= newPosition) {
1579 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1580 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001581 }
1582
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001583 // Cache other fields that will be needed soon
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001584 uint32_t sampleRate = mSampleRate;
Glenn Kasten838b3d82014-02-27 15:30:41 -08001585 uint32_t notificationFrames = mNotificationFramesAct;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001586 if (mRefreshRemaining) {
1587 mRefreshRemaining = false;
1588 mRemainingFrames = notificationFrames;
1589 mRetryOnPartialBuffer = false;
1590 }
1591 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001592 uint32_t sequence = mSequence;
Glenn Kasten96f04882013-09-20 09:28:56 -07001593 sp<AudioTrackClientProxy> proxy = mProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001594
Andy Hung53c3b5f2014-12-15 16:42:05 -08001595 // Determine the number of new loop callback(s) that will be needed, while locked.
1596 int loopCountNotifications = 0;
1597 uint32_t loopPeriod = 0; // time in frames for next EVENT_LOOP_END or EVENT_BUFFER_END
1598
1599 if (mLoopCount > 0) {
1600 int loopCount;
1601 size_t bufferPosition;
1602 mStaticProxy->getBufferPositionAndLoopCount(&bufferPosition, &loopCount);
1603 loopPeriod = ((loopCount > 0) ? mLoopEnd : mFrameCount) - bufferPosition;
1604 loopCountNotifications = min(mLoopCountNotified - loopCount, kMaxLoopCountNotifications);
1605 mLoopCountNotified = loopCount; // discard any excess notifications
1606 } else if (mLoopCount < 0) {
1607 // FIXME: We're not accurate with notification count and position with infinite looping
1608 // since loopCount from server side will always return -1 (we could decrement it).
1609 size_t bufferPosition = mStaticProxy->getBufferPosition();
1610 loopCountNotifications = int((flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) != 0);
1611 loopPeriod = mLoopEnd - bufferPosition;
1612 } else if (/* mLoopCount == 0 && */ mSharedBuffer != 0) {
1613 size_t bufferPosition = mStaticProxy->getBufferPosition();
1614 loopPeriod = mFrameCount - bufferPosition;
1615 }
1616
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001617 // These fields don't need to be cached, because they are assigned only by set():
Andy Hungabdb9902015-01-12 15:08:22 -08001618 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFlags
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001619 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1620
1621 mLock.unlock();
1622
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001623 if (waitStreamEnd) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001624 struct timespec timeout;
1625 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1626 timeout.tv_nsec = 0;
1627
Glenn Kasten96f04882013-09-20 09:28:56 -07001628 status_t status = proxy->waitStreamEndDone(&timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001629 switch (status) {
1630 case NO_ERROR:
1631 case DEAD_OBJECT:
1632 case TIMED_OUT:
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001633 mCbf(EVENT_STREAM_END, mUserData, NULL);
Glenn Kasten96f04882013-09-20 09:28:56 -07001634 {
1635 AutoMutex lock(mLock);
1636 // The previously assigned value of waitStreamEnd is no longer valid,
1637 // since the mutex has been unlocked and either the callback handler
1638 // or another thread could have re-started the AudioTrack during that time.
1639 waitStreamEnd = mState == STATE_STOPPING;
1640 if (waitStreamEnd) {
1641 mState = STATE_STOPPED;
Andy Hungc2813e52014-10-16 17:54:34 -07001642 mReleased = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001643 }
1644 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001645 if (waitStreamEnd && status != DEAD_OBJECT) {
1646 return NS_INACTIVE;
1647 }
1648 break;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001649 }
Glenn Kasten96f04882013-09-20 09:28:56 -07001650 return 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001651 }
1652
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001653 // perform callbacks while unlocked
1654 if (newUnderrun) {
1655 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1656 }
Andy Hung53c3b5f2014-12-15 16:42:05 -08001657 while (loopCountNotifications > 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001658 mCbf(EVENT_LOOP_END, mUserData, NULL);
Andy Hung53c3b5f2014-12-15 16:42:05 -08001659 --loopCountNotifications;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001660 }
1661 if (flags & CBLK_BUFFER_END) {
1662 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1663 }
1664 if (markerReached) {
1665 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1666 }
1667 while (newPosCount > 0) {
1668 size_t temp = newPosition;
1669 mCbf(EVENT_NEW_POS, mUserData, &temp);
1670 newPosition += updatePeriod;
1671 newPosCount--;
1672 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001673
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001674 if (mObservedSequence != sequence) {
1675 mObservedSequence = sequence;
1676 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001677 // for offloaded tracks, just wait for the upper layers to recreate the track
Eric Laurentab5cdba2014-06-09 17:22:27 -07001678 if (isOffloadedOrDirect()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001679 return NS_INACTIVE;
1680 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681 }
1682
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001683 // if inactive, then don't run me again until re-started
1684 if (!active) {
1685 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001686 }
1687
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001688 // Compute the estimated time until the next timed event (position, markers, loops)
1689 // FIXME only for non-compressed audio
1690 uint32_t minFrames = ~0;
1691 if (!markerReached && position < markerPosition) {
1692 minFrames = markerPosition - position;
1693 }
1694 if (loopPeriod > 0 && loopPeriod < minFrames) {
Andy Hung2d85f092015-01-07 12:45:13 -08001695 // loopPeriod is already adjusted for actual position.
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001696 minFrames = loopPeriod;
1697 }
Andy Hung2d85f092015-01-07 12:45:13 -08001698 if (updatePeriod > 0) {
1699 minFrames = min(minFrames, uint32_t(newPosition - position));
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001700 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001701
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001702 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1703 static const uint32_t kPoll = 0;
1704 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1705 minFrames = kPoll * notificationFrames;
1706 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001707
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001708 // Convert frame units to time units
1709 nsecs_t ns = NS_WHENEVER;
1710 if (minFrames != (uint32_t) ~0) {
1711 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1712 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1713 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1714 }
1715
1716 // If not supplying data by EVENT_MORE_DATA, then we're done
1717 if (mTransfer != TRANSFER_CALLBACK) {
1718 return ns;
1719 }
1720
1721 struct timespec timeout;
1722 const struct timespec *requested = &ClientProxy::kForever;
1723 if (ns != NS_WHENEVER) {
1724 timeout.tv_sec = ns / 1000000000LL;
1725 timeout.tv_nsec = ns % 1000000000LL;
1726 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1727 requested = &timeout;
1728 }
1729
1730 while (mRemainingFrames > 0) {
1731
1732 Buffer audioBuffer;
1733 audioBuffer.frameCount = mRemainingFrames;
1734 size_t nonContig;
1735 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1736 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
Mark Salyzyn34fb2962014-06-18 16:30:56 -07001737 "obtainBuffer() err=%d frameCount=%zu", err, audioBuffer.frameCount);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001738 requested = &ClientProxy::kNonBlocking;
1739 size_t avail = audioBuffer.frameCount + nonContig;
Mark Salyzyn34fb2962014-06-18 16:30:56 -07001740 ALOGV("obtainBuffer(%u) returned %zu = %zu + %zu err %d",
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001741 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001742 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001743 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1744 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001745 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001746 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001747 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1748 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001749 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001750
Eric Laurent42a6f422013-08-29 14:35:05 -07001751 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001752 mRetryOnPartialBuffer = false;
1753 if (avail < mRemainingFrames) {
1754 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1755 if (ns < 0 || myns < ns) {
1756 ns = myns;
1757 }
1758 return ns;
1759 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001760 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001761
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001762 size_t reqSize = audioBuffer.size;
1763 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001764 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001765
1766 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001767 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
Mark Salyzyn34fb2962014-06-18 16:30:56 -07001768 ALOGE("EVENT_MORE_DATA requested %zu bytes but callback returned %zd bytes",
1769 reqSize, ssize_t(writtenSize));
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001770 return NS_NEVER;
1771 }
1772
1773 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001774 // The callback is done filling buffers
1775 // Keep this thread going to handle timed events and
1776 // still try to get more data in intervals of WAIT_PERIOD_MS
1777 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001778 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001779 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001780
Andy Hungabdb9902015-01-12 15:08:22 -08001781 size_t releasedFrames = audioBuffer.size / mFrameSize;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001782 audioBuffer.frameCount = releasedFrames;
1783 mRemainingFrames -= releasedFrames;
1784 if (misalignment >= releasedFrames) {
1785 misalignment -= releasedFrames;
1786 } else {
1787 misalignment = 0;
1788 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001789
1790 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001791
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001792 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1793 // if callback doesn't like to accept the full chunk
1794 if (writtenSize < reqSize) {
1795 continue;
1796 }
1797
1798 // There could be enough non-contiguous frames available to satisfy the remaining request
1799 if (mRemainingFrames <= nonContig) {
1800 continue;
1801 }
1802
1803#if 0
1804 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1805 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1806 // that total to a sum == notificationFrames.
1807 if (0 < misalignment && misalignment <= mRemainingFrames) {
1808 mRemainingFrames = misalignment;
1809 return (mRemainingFrames * 1100000000LL) / sampleRate;
1810 }
1811#endif
1812
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001813 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001814 mRemainingFrames = notificationFrames;
1815 mRetryOnPartialBuffer = true;
1816
1817 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1818 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001819}
1820
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001821status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001822{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001823 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Eric Laurentab5cdba2014-06-09 17:22:27 -07001824 isOffloadedOrDirect_l() ? "Offloaded or Direct" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001825 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001826 status_t result;
1827
Glenn Kastena47f3162012-11-07 10:13:08 -08001828 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kastend2d089f2014-11-05 11:48:12 -08001829 // output parameters and new IAudioFlinger in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001830 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001831
Eric Laurentab5cdba2014-06-09 17:22:27 -07001832 if (isOffloadedOrDirect_l()) {
Glenn Kasten23a75452014-01-13 10:37:17 -08001833 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001834 return DEAD_OBJECT;
1835 }
1836
Glenn Kasten200092b2014-08-15 15:13:30 -07001837 // save the old static buffer position
Andy Hung4ede21d2014-12-12 15:37:34 -08001838 size_t bufferPosition = 0;
1839 int loopCount = 0;
1840 if (mStaticProxy != 0) {
1841 mStaticProxy->getBufferPositionAndLoopCount(&bufferPosition, &loopCount);
1842 }
Glenn Kasten200092b2014-08-15 15:13:30 -07001843
1844 // If a new IAudioTrack is successfully created, createTrack_l() will modify the
Glenn Kastena47f3162012-11-07 10:13:08 -08001845 // following member variables: mAudioTrack, mCblkMemory and mCblk.
Glenn Kasten200092b2014-08-15 15:13:30 -07001846 // It will also delete the strong references on previous IAudioTrack and IMemory.
1847 // If a new IAudioTrack cannot be created, the previous (dead) instance will be left intact.
1848 result = createTrack_l();
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001849
1850 // take the frames that will be lost by track recreation into account in saved position
Andy Hung9b461582014-12-01 17:56:29 -08001851 // For streaming tracks, this is the amount we obtained from the user/client
1852 // (not the number actually consumed at the server - those are already lost).
Glenn Kasten200092b2014-08-15 15:13:30 -07001853 (void) updateAndGetPosition_l();
Andy Hung9b461582014-12-01 17:56:29 -08001854 if (mStaticProxy != 0) {
1855 mPosition = mReleased;
1856 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001857
Glenn Kastena47f3162012-11-07 10:13:08 -08001858 if (result == NO_ERROR) {
Andy Hung4ede21d2014-12-12 15:37:34 -08001859 // Continue playback from last known position and restore loop.
1860 if (mStaticProxy != 0) {
1861 if (loopCount != 0) {
1862 mStaticProxy->setBufferPositionAndLoop(bufferPosition,
1863 mLoopStart, mLoopEnd, loopCount);
1864 } else {
1865 mStaticProxy->setBufferPosition(bufferPosition);
Andy Hung53c3b5f2014-12-15 16:42:05 -08001866 if (bufferPosition == mFrameCount) {
1867 ALOGD("restoring track at end of static buffer");
1868 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001869 }
1870 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001871 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001872 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001873 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001874 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001875 if (result != NO_ERROR) {
1876 ALOGW("restoreTrack_l() failed status %d", result);
1877 mState = STATE_STOPPED;
Andy Hungc2813e52014-10-16 17:54:34 -07001878 mReleased = 0;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001879 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001880
1881 return result;
1882}
1883
Glenn Kasten200092b2014-08-15 15:13:30 -07001884uint32_t AudioTrack::updateAndGetPosition_l()
1885{
1886 // This is the sole place to read server consumed frames
1887 uint32_t newServer = mProxy->getPosition();
1888 int32_t delta = newServer - mServer;
1889 mServer = newServer;
1890 // TODO There is controversy about whether there can be "negative jitter" in server position.
1891 // This should be investigated further, and if possible, it should be addressed.
1892 // A more definite failure mode is infrequent polling by client.
1893 // One could call (void)getPosition_l() in releaseBuffer(),
1894 // so mReleased and mPosition are always lock-step as best possible.
1895 // That should ensure delta never goes negative for infrequent polling
1896 // unless the server has more than 2^31 frames in its buffer,
1897 // in which case the use of uint32_t for these counters has bigger issues.
1898 if (delta < 0) {
1899 ALOGE("detected illegal retrograde motion by the server: mServer advanced by %d", delta);
1900 delta = 0;
1901 }
1902 return mPosition += (uint32_t) delta;
1903}
1904
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001905status_t AudioTrack::setParameters(const String8& keyValuePairs)
1906{
1907 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001908 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001909}
1910
Glenn Kastence703742013-07-19 16:33:58 -07001911status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1912{
Glenn Kasten53cec222013-08-29 09:01:02 -07001913 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001914 // FIXME not implemented for fast tracks; should use proxy and SSQ
1915 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1916 return INVALID_OPERATION;
1917 }
Andy Hung7f1bc8a2014-09-12 14:43:11 -07001918
1919 switch (mState) {
1920 case STATE_ACTIVE:
1921 case STATE_PAUSED:
1922 break; // handle below
1923 case STATE_FLUSHED:
1924 case STATE_STOPPED:
1925 return WOULD_BLOCK;
1926 case STATE_STOPPING:
1927 case STATE_PAUSED_STOPPING:
1928 if (!isOffloaded_l()) {
1929 return INVALID_OPERATION;
1930 }
1931 break; // offloaded tracks handled below
1932 default:
1933 LOG_ALWAYS_FATAL("Invalid mState in getTimestamp(): %d", mState);
1934 break;
Glenn Kastenfe346c72013-08-30 13:28:22 -07001935 }
Andy Hung7f1bc8a2014-09-12 14:43:11 -07001936
Eric Laurent275e8e92014-11-30 15:14:47 -08001937 if (mCblk->mFlags & CBLK_INVALID) {
1938 restoreTrack_l("getTimestamp");
1939 }
1940
Glenn Kasten200092b2014-08-15 15:13:30 -07001941 // The presented frame count must always lag behind the consumed frame count.
1942 // To avoid a race, read the presented frames first. This ensures that presented <= consumed.
Glenn Kastenfe346c72013-08-30 13:28:22 -07001943 status_t status = mAudioTrack->getTimestamp(timestamp);
Andy Hung7f1bc8a2014-09-12 14:43:11 -07001944 if (status != NO_ERROR) {
Glenn Kastendfc34da2014-09-19 09:05:05 -07001945 ALOGV_IF(status != WOULD_BLOCK, "getTimestamp error:%#x", status);
Andy Hung7f1bc8a2014-09-12 14:43:11 -07001946 return status;
1947 }
1948 if (isOffloadedOrDirect_l()) {
1949 if (isOffloaded_l() && (mState == STATE_PAUSED || mState == STATE_PAUSED_STOPPING)) {
1950 // use cached paused position in case another offloaded track is running.
1951 timestamp.mPosition = mPausedPosition;
1952 clock_gettime(CLOCK_MONOTONIC, &timestamp.mTime);
1953 return NO_ERROR;
1954 }
1955
1956 // Check whether a pending flush or stop has completed, as those commands may
1957 // be asynchronous or return near finish.
1958 if (mStartUs != 0 && mSampleRate != 0) {
1959 static const int kTimeJitterUs = 100000; // 100 ms
1960 static const int k1SecUs = 1000000;
1961
1962 const int64_t timeNow = getNowUs();
1963
1964 if (timeNow < mStartUs + k1SecUs) { // within first second of starting
1965 const int64_t timestampTimeUs = convertTimespecToUs(timestamp.mTime);
1966 if (timestampTimeUs < mStartUs) {
1967 return WOULD_BLOCK; // stale timestamp time, occurs before start.
1968 }
1969 const int64_t deltaTimeUs = timestampTimeUs - mStartUs;
1970 const int64_t deltaPositionByUs = timestamp.mPosition * 1000000LL / mSampleRate;
1971
1972 if (deltaPositionByUs > deltaTimeUs + kTimeJitterUs) {
1973 // Verify that the counter can't count faster than the sample rate
1974 // since the start time. If greater, then that means we have failed
1975 // to completely flush or stop the previous playing track.
1976 ALOGW("incomplete flush or stop:"
1977 " deltaTimeUs(%lld) deltaPositionUs(%lld) tsmPosition(%u)",
1978 (long long)deltaTimeUs, (long long)deltaPositionByUs,
1979 timestamp.mPosition);
1980 return WOULD_BLOCK;
1981 }
1982 }
1983 mStartUs = 0; // no need to check again, start timestamp has either expired or unneeded.
1984 }
1985 } else {
Glenn Kasten200092b2014-08-15 15:13:30 -07001986 // Update the mapping between local consumed (mPosition) and server consumed (mServer)
1987 (void) updateAndGetPosition_l();
1988 // Server consumed (mServer) and presented both use the same server time base,
1989 // and server consumed is always >= presented.
1990 // The delta between these represents the number of frames in the buffer pipeline.
1991 // If this delta between these is greater than the client position, it means that
1992 // actually presented is still stuck at the starting line (figuratively speaking),
1993 // waiting for the first frame to go by. So we can't report a valid timestamp yet.
1994 if ((uint32_t) (mServer - timestamp.mPosition) > mPosition) {
1995 return INVALID_OPERATION;
1996 }
1997 // Convert timestamp position from server time base to client time base.
1998 // TODO The following code should work OK now because timestamp.mPosition is 32-bit.
1999 // But if we change it to 64-bit then this could fail.
2000 // If (mPosition - mServer) can be negative then should use:
2001 // (int32_t)(mPosition - mServer)
2002 timestamp.mPosition += mPosition - mServer;
2003 // Immediately after a call to getPosition_l(), mPosition and
2004 // mServer both represent the same frame position. mPosition is
2005 // in client's point of view, and mServer is in server's point of
2006 // view. So the difference between them is the "fudge factor"
2007 // between client and server views due to stop() and/or new
2008 // IAudioTrack. And timestamp.mPosition is initially in server's
2009 // point of view, so we need to apply the same fudge factor to it.
Glenn Kastenfe346c72013-08-30 13:28:22 -07002010 }
2011 return status;
Glenn Kastence703742013-07-19 16:33:58 -07002012}
2013
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002014String8 AudioTrack::getParameters(const String8& keys)
2015{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08002016 audio_io_handle_t output = getOutput();
Glenn Kasten142f5192014-03-25 17:44:59 -07002017 if (output != AUDIO_IO_HANDLE_NONE) {
Glenn Kasten2c6c5292014-01-13 10:29:08 -08002018 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01002019 } else {
2020 return String8::empty();
2021 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002022}
2023
Glenn Kasten23a75452014-01-13 10:37:17 -08002024bool AudioTrack::isOffloaded() const
2025{
2026 AutoMutex lock(mLock);
2027 return isOffloaded_l();
2028}
2029
Eric Laurentab5cdba2014-06-09 17:22:27 -07002030bool AudioTrack::isDirect() const
2031{
2032 AutoMutex lock(mLock);
2033 return isDirect_l();
2034}
2035
2036bool AudioTrack::isOffloadedOrDirect() const
2037{
2038 AutoMutex lock(mLock);
2039 return isOffloadedOrDirect_l();
2040}
2041
2042
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08002043status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002044{
2045
2046 const size_t SIZE = 256;
2047 char buffer[SIZE];
2048 String8 result;
2049
2050 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07002051 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
Glenn Kasten877a0ac2014-04-30 17:04:13 -07002052 mVolume[AUDIO_INTERLEAVE_LEFT], mVolume[AUDIO_INTERLEAVE_RIGHT]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002053 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00002054 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08002055 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002056 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08002057 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002058 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002059 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002060 result.append(buffer);
2061 ::write(fd, result.string(), result.size());
2062 return NO_ERROR;
2063}
2064
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002065uint32_t AudioTrack::getUnderrunFrames() const
2066{
2067 AutoMutex lock(mLock);
2068 return mProxy->getUnderrunFrames();
2069}
2070
2071// =========================================================================
2072
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08002073void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002074{
2075 sp<AudioTrack> audioTrack = mAudioTrack.promote();
2076 if (audioTrack != 0) {
2077 AutoMutex lock(audioTrack->mLock);
2078 audioTrack->mProxy->binderDied();
2079 }
2080}
2081
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002082// =========================================================================
2083
2084AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07002085 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
2086 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08002087{
2088}
2089
2090AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002091{
2092}
2093
2094bool AudioTrack::AudioTrackThread::threadLoop()
2095{
Glenn Kasten3acbd052012-02-28 10:39:56 -08002096 {
2097 AutoMutex _l(mMyLock);
2098 if (mPaused) {
2099 mMyCond.wait(mMyLock);
2100 // caller will check for exitPending()
2101 return true;
2102 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07002103 if (mIgnoreNextPausedInt) {
2104 mIgnoreNextPausedInt = false;
2105 mPausedInt = false;
2106 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002107 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002108 if (mPausedNs > 0) {
2109 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
2110 } else {
2111 mMyCond.wait(mMyLock);
2112 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002113 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002114 return true;
2115 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08002116 }
Eric Laurent7985dcb2014-10-07 15:45:14 -07002117 if (exitPending()) {
2118 return false;
2119 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08002120 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002121 switch (ns) {
2122 case 0:
2123 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002124 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002125 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002126 return true;
2127 case NS_NEVER:
2128 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002129 case NS_WHENEVER:
Andy Hung3c09c782014-12-29 18:39:32 -08002130 // Event driven: call wake() when callback notifications conditions change.
2131 ns = INT64_MAX;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002132 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002133 default:
Mark Salyzyn34fb2962014-06-18 16:30:56 -07002134 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %" PRId64, ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002135 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002136 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07002137 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002138}
2139
Glenn Kasten3acbd052012-02-28 10:39:56 -08002140void AudioTrack::AudioTrackThread::requestExit()
2141{
2142 // must be in this order to avoid a race condition
2143 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07002144 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08002145}
2146
2147void AudioTrack::AudioTrackThread::pause()
2148{
2149 AutoMutex _l(mMyLock);
2150 mPaused = true;
2151}
2152
2153void AudioTrack::AudioTrackThread::resume()
2154{
2155 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07002156 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002157 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08002158 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002159 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08002160 mMyCond.signal();
2161 }
2162}
2163
Andy Hung3c09c782014-12-29 18:39:32 -08002164void AudioTrack::AudioTrackThread::wake()
2165{
2166 AutoMutex _l(mMyLock);
2167 if (!mPaused && mPausedInt && mPausedNs > 0) {
2168 // audio track is active and internally paused with timeout.
2169 mIgnoreNextPausedInt = true;
2170 mPausedInt = false;
2171 mMyCond.signal();
2172 }
2173}
2174
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002175void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
2176{
2177 AutoMutex _l(mMyLock);
2178 mPausedInt = true;
2179 mPausedNs = ns;
2180}
2181
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002182}; // namespace android