blob: 8e91f126a9563bc0f5e551d07559b2c21914a925 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080023#include <audio_utils/primitives.h>
24#include <binder/IPCThreadState.h>
25#include <media/AudioTrack.h>
26#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010030#define WAIT_PERIOD_MS 10
31#define WAIT_STREAM_END_TIMEOUT_SEC 120
32
Glenn Kasten511754b2012-01-11 09:52:19 -080033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080035// ---------------------------------------------------------------------------
36
37// static
38status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080039 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080040 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080041 uint32_t sampleRate)
42{
Glenn Kastend65d73c2012-06-22 17:21:07 -070043 if (frameCount == NULL) {
44 return BAD_VALUE;
45 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070046
Glenn Kastene0fa4672012-04-24 14:35:14 -070047 // FIXME merge with similar code in createTrack_l(), except we're missing
48 // some information here that is available in createTrack_l():
49 // audio_io_handle_t output
50 // audio_format_t format
51 // audio_channel_mask_t channelMask
52 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080053 uint32_t afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080054 status_t status;
55 status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
56 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080057 ALOGE("Unable to query output sample rate for stream type %d; status %d",
58 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080059 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080060 }
Glenn Kastene33054e2012-11-14 12:54:39 -080061 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080062 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
63 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080064 ALOGE("Unable to query output frame count for stream type %d; status %d",
65 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080066 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080067 }
68 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080069 status = AudioSystem::getOutputLatency(&afLatency, streamType);
70 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080071 ALOGE("Unable to query output latency for stream type %d; status %d",
72 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080073 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080074 }
75
76 // Ensure that buffer depth covers at least audio hardware latency
77 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080078 if (minBufCount < 2) {
79 minBufCount = 2;
80 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080081
82 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070083 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080084 // The formula above should always produce a non-zero value, but return an error
85 // in the unlikely event that it does not, as that's part of the API contract.
86 if (*frameCount == 0) {
87 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d",
88 streamType, sampleRate);
89 return BAD_VALUE;
90 }
Glenn Kasten3acbd052012-02-28 10:39:56 -080091 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
92 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080093 return NO_ERROR;
94}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095
96// ---------------------------------------------------------------------------
97
98AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070099 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800100 mIsTimed(false),
101 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800102 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103{
104}
105
106AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800107 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800109 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700110 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700112 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113 callback_t cbf,
114 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700115 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800116 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000117 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800118 const audio_offload_info_t *offloadInfo,
119 int uid)
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),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800123 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700125 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700126 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800127 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
128 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800129}
130
Andreas Huberc8139852012-01-18 10:51:55 -0800131AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800132 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800133 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800134 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700135 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700137 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 callback_t cbf,
139 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700140 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800141 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000142 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800143 const audio_offload_info_t *offloadInfo,
144 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700145 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800146 mIsTimed(false),
147 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800148 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700150 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800151 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800152 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153}
154
155AudioTrack::~AudioTrack()
156{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 if (mStatus == NO_ERROR) {
158 // Make sure that callback function exits in the case where
159 // it is looping on buffer full condition in obtainBuffer().
160 // Otherwise the callback thread will never exit.
161 stop();
162 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100163 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800164 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 mAudioTrackThread->requestExitAndWait();
166 mAudioTrackThread.clear();
167 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700168 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
169 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700171 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 }
173}
174
175status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800176 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800178 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700179 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800180 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700181 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 callback_t cbf,
183 void* user,
184 int notificationFrames,
185 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700186 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800187 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000188 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800189 const audio_offload_info_t *offloadInfo,
190 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800192 switch (transferType) {
193 case TRANSFER_DEFAULT:
194 if (sharedBuffer != 0) {
195 transferType = TRANSFER_SHARED;
196 } else if (cbf == NULL || threadCanCallJava) {
197 transferType = TRANSFER_SYNC;
198 } else {
199 transferType = TRANSFER_CALLBACK;
200 }
201 break;
202 case TRANSFER_CALLBACK:
203 if (cbf == NULL || sharedBuffer != 0) {
204 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
205 return BAD_VALUE;
206 }
207 break;
208 case TRANSFER_OBTAIN:
209 case TRANSFER_SYNC:
210 if (sharedBuffer != 0) {
211 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
212 return BAD_VALUE;
213 }
214 break;
215 case TRANSFER_SHARED:
216 if (sharedBuffer == 0) {
217 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
218 return BAD_VALUE;
219 }
220 break;
221 default:
222 ALOGE("Invalid transfer type %d", transferType);
223 return BAD_VALUE;
224 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800225 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800226 mTransfer = transferType;
227
Glenn Kastene33054e2012-11-14 12:54:39 -0800228 // FIXME "int" here is legacy and will be replaced by size_t later
229 if (frameCountInt < 0) {
230 ALOGE("Invalid frame count %d", frameCountInt);
231 return BAD_VALUE;
232 }
233 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700235 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
236 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237
Glenn Kastene33054e2012-11-14 12:54:39 -0800238 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700239
Eric Laurent1703cdf2011-03-07 14:52:59 -0800240 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800241
Glenn Kasten53cec222013-08-29 09:01:02 -0700242 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700243 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000244 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 return INVALID_OPERATION;
246 }
247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700249 if (streamType == AUDIO_STREAM_DEFAULT) {
250 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800252 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
253 ALOGE("Invalid stream type %d", streamType);
254 return BAD_VALUE;
255 }
256 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700257
Glenn Kastenb1bef512014-01-13 10:25:53 -0800258 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800260 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
261 if (status != NO_ERROR) {
262 ALOGE("Could not get output sample rate for stream type %d; status %d",
263 streamType, status);
264 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700265 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800267 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700268
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800270 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700271 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273
274 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800276 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 return BAD_VALUE;
278 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800279 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280
Glenn Kasten8ba90322013-10-30 11:29:27 -0700281 if (!audio_is_output_channel(channelMask)) {
282 ALOGE("Invalid channel mask %#x", channelMask);
283 return BAD_VALUE;
284 }
285
Glenn Kastene0fa4672012-04-24 14:35:14 -0700286 // AudioFlinger does not currently support 8-bit data in shared memory
287 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
288 ALOGE("8-bit data in shared memory is not supported");
289 return BAD_VALUE;
290 }
291
Eric Laurentc2f1f072009-07-17 12:17:14 -0700292 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100293 // or offload was requested
294 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
295 || !audio_is_linear_pcm(format)) {
296 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
297 ? "Offload request, forcing to Direct Output"
298 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700299 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800300 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700301 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700302 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700303 // only allow deep buffering for music stream type
304 if (streamType != AUDIO_STREAM_MUSIC) {
305 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
306 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307
Glenn Kastena42ff002012-11-14 12:47:55 -0800308 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700309 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800310 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311
Glenn Kastene3aa6592012-12-04 12:22:46 -0800312 if (audio_is_linear_pcm(format)) {
313 mFrameSize = channelCount * audio_bytes_per_sample(format);
314 mFrameSizeAF = channelCount * sizeof(int16_t);
315 } else {
316 mFrameSize = sizeof(uint8_t);
317 mFrameSizeAF = sizeof(uint8_t);
318 }
319
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800320 // Make copy of input parameter offloadInfo so that in the future:
321 // (a) createTrack_l doesn't need it as an input parameter
322 // (b) we can support re-creation of offloaded tracks
323 if (offloadInfo != NULL) {
324 mOffloadInfoCopy = *offloadInfo;
325 mOffloadInfo = &mOffloadInfoCopy;
326 } else {
327 mOffloadInfo = NULL;
328 }
329
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800330 mVolume[LEFT] = 1.0f;
331 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800332 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800333 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800334 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700335 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800336 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700337 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800338 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
339 mClientUid = IPCThreadState::self()->getCallingUid();
340 } else {
341 mClientUid = uid;
342 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700343 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700344 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700345 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700346
Glenn Kastena997e7a2012-08-07 09:44:19 -0700347 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700348 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700349 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
350 }
351
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800352 // create the IAudioTrack
Glenn Kastenb1bef512014-01-13 10:25:53 -0800353 status = createTrack_l(streamType,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800354 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800355 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800356 frameCount,
357 flags,
358 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800359 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800360
Glenn Kastena997e7a2012-08-07 09:44:19 -0700361 if (status != NO_ERROR) {
362 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100363 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
364 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700365 mAudioTrackThread.clear();
366 }
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800367 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800368#if 0 // FIXME This should no longer be needed
369 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100370 // As getOutput was called above and resulted in an output stream to be opened,
371 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800372 if (mOutput != 0) {
373 AudioSystem::releaseOutput(mOutput);
374 mOutput = 0;
375 }
376#endif
Glenn Kastena997e7a2012-08-07 09:44:19 -0700377 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700378 }
379
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800381 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800383 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700385 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 mNewPosition = 0;
387 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700388 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389 mSequence = 1;
390 mObservedSequence = mSequence;
391 mInUnderrun = false;
392
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 return NO_ERROR;
394}
395
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396// -------------------------------------------------------------------------
397
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100398status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800400 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100401
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800402 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100403 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800404 }
405
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800406 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800408 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100409 if (previousState == STATE_PAUSED_STOPPING) {
410 mState = STATE_STOPPING;
411 } else {
412 mState = STATE_ACTIVE;
413 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800414 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
415 // reset current position as seen by client to 0
416 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700417 // force refresh of remaining frames by processAudioBuffer() as last
418 // write before stop could be partial.
419 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800420 }
421 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700422 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800423
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800425 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100426 if (previousState == STATE_STOPPING) {
427 mProxy->interrupt();
428 } else {
429 t->resume();
430 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431 } else {
432 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
433 get_sched_policy(0, &mPreviousSchedulingGroup);
434 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
435 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800437 status_t status = NO_ERROR;
438 if (!(flags & CBLK_INVALID)) {
439 status = mAudioTrack->start();
440 if (status == DEAD_OBJECT) {
441 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800443 }
444 if (flags & CBLK_INVALID) {
445 status = restoreTrack_l("start");
446 }
447
448 if (status != NO_ERROR) {
449 ALOGE("start() status %d", status);
450 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100452 if (previousState != STATE_STOPPING) {
453 t->pause();
454 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700456 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700457 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458 }
459 }
460
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100461 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800462}
463
464void AudioTrack::stop()
465{
466 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700467 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800468 return;
469 }
470
Glenn Kasten23a75452014-01-13 10:37:17 -0800471 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100472 mState = STATE_STOPPING;
473 } else {
474 mState = STATE_STOPPED;
475 }
476
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800477 mProxy->interrupt();
478 mAudioTrack->stop();
479 // the playback head position will reset to 0, so if a marker is set, we need
480 // to activate it again
481 mMarkerReached = false;
482#if 0
483 // Force flush if a shared buffer is used otherwise audioflinger
484 // will not stop before end of buffer is reached.
485 // It may be needed to make sure that we stop playback, likely in case looping is on.
486 if (mSharedBuffer != 0) {
487 flush_l();
488 }
489#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100490
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800491 sp<AudioTrackThread> t = mAudioTrackThread;
492 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800493 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100494 t->pause();
495 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800496 } else {
497 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
498 set_sched_policy(0, mPreviousSchedulingGroup);
499 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500}
501
502bool AudioTrack::stopped() const
503{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800504 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800505 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506}
507
508void AudioTrack::flush()
509{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800510 if (mSharedBuffer != 0) {
511 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800512 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800513 AutoMutex lock(mLock);
514 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
515 return;
516 }
517 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800518}
519
Eric Laurent1703cdf2011-03-07 14:52:59 -0800520void AudioTrack::flush_l()
521{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800522 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700523
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700524 // clear playback marker and periodic update counter
525 mMarkerPosition = 0;
526 mMarkerReached = false;
527 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100528 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700529
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800530 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800531 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100532 mProxy->interrupt();
533 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800534 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800535 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536}
537
538void AudioTrack::pause()
539{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800540 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100541 if (mState == STATE_ACTIVE) {
542 mState = STATE_PAUSED;
543 } else if (mState == STATE_STOPPING) {
544 mState = STATE_PAUSED_STOPPING;
545 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800546 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800548 mProxy->interrupt();
549 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550}
551
Eric Laurentbe916aa2010-06-01 23:49:17 -0700552status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800554 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700555 return BAD_VALUE;
556 }
557
Eric Laurent1703cdf2011-03-07 14:52:59 -0800558 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559 mVolume[LEFT] = left;
560 mVolume[RIGHT] = right;
561
Glenn Kastene3aa6592012-12-04 12:22:46 -0800562 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700563
Glenn Kasten23a75452014-01-13 10:37:17 -0800564 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700565 mAudioTrack->signal();
566 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700567 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568}
569
Glenn Kastenb1c09932012-02-27 16:21:04 -0800570status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800572 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700573}
574
Eric Laurent2beeb502010-07-16 07:43:46 -0700575status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700576{
Glenn Kasten05632a52012-01-03 14:22:33 -0800577 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700578 return BAD_VALUE;
579 }
580
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800581 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700582 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800583 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700584
585 return NO_ERROR;
586}
587
Glenn Kastena5224f32012-01-04 12:41:44 -0800588void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700589{
590 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800591 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700592 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593}
594
Glenn Kasten3b16c762012-11-14 08:44:39 -0800595status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100597 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800598 return INVALID_OPERATION;
599 }
600
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800601 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700603 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 }
605 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700606 if (rate == 0 || rate > afSamplingRate*2 ) {
607 return BAD_VALUE;
608 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609
Eric Laurent1703cdf2011-03-07 14:52:59 -0800610 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800611 mSampleRate = rate;
612 mProxy->setSampleRate(rate);
613
Eric Laurent57326622009-07-07 07:10:45 -0700614 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615}
616
Glenn Kastena5224f32012-01-04 12:41:44 -0800617uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618{
John Grossman4ff14ba2012-02-08 16:37:41 -0800619 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800620 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800621 }
622
Eric Laurent1703cdf2011-03-07 14:52:59 -0800623 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700624
625 // sample rate can be updated during playback by the offloaded decoder so we need to
626 // query the HAL and update if needed.
627// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800628 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700629 if (mOutput != 0) {
630 uint32_t sampleRate = 0;
631 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
632 if (status == NO_ERROR) {
633 mSampleRate = sampleRate;
634 }
635 }
636 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800637 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638}
639
640status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
641{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100642 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800643 return INVALID_OPERATION;
644 }
645
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800647 ;
648 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
649 loopEnd - loopStart >= MIN_LOOP) {
650 ;
651 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return BAD_VALUE;
653 }
654
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800655 AutoMutex lock(mLock);
656 // See setPosition() regarding setting parameters such as loop points or position while active
657 if (mState == STATE_ACTIVE) {
658 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700659 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800660 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661 return NO_ERROR;
662}
663
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800664void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
665{
666 // FIXME If setting a loop also sets position to start of loop, then
667 // this is correct. Otherwise it should be removed.
668 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
669 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
670 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
671}
672
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673status_t AudioTrack::setMarkerPosition(uint32_t marker)
674{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700675 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100676 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700677 return INVALID_OPERATION;
678 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800680 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700682 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683
684 return NO_ERROR;
685}
686
Glenn Kastena5224f32012-01-04 12:41:44 -0800687status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100689 if (isOffloaded()) {
690 return INVALID_OPERATION;
691 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700692 if (marker == NULL) {
693 return BAD_VALUE;
694 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800696 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697 *marker = mMarkerPosition;
698
699 return NO_ERROR;
700}
701
702status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
703{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700704 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100705 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700706 return INVALID_OPERATION;
707 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800709 AutoMutex lock(mLock);
710 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800712
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800713 return NO_ERROR;
714}
715
Glenn Kastena5224f32012-01-04 12:41:44 -0800716status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800717{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100718 if (isOffloaded()) {
719 return INVALID_OPERATION;
720 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700721 if (updatePeriod == NULL) {
722 return BAD_VALUE;
723 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800724
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800725 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726 *updatePeriod = mUpdatePeriod;
727
728 return NO_ERROR;
729}
730
731status_t AudioTrack::setPosition(uint32_t position)
732{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100733 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700734 return INVALID_OPERATION;
735 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800736 if (position > mFrameCount) {
737 return BAD_VALUE;
738 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800739
Eric Laurent1703cdf2011-03-07 14:52:59 -0800740 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800741 // Currently we require that the player is inactive before setting parameters such as position
742 // or loop points. Otherwise, there could be a race condition: the application could read the
743 // current position, compute a new position or loop parameters, and then set that position or
744 // loop parameters but it would do the "wrong" thing since the position has continued to advance
745 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
746 // to specify how it wants to handle such scenarios.
747 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700748 return INVALID_OPERATION;
749 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800750 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
751 mLoopPeriod = 0;
752 // FIXME Check whether loops and setting position are incompatible in old code.
753 // If we use setLoop for both purposes we lose the capability to set the position while looping.
754 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700755
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756 return NO_ERROR;
757}
758
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800759status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700761 if (position == NULL) {
762 return BAD_VALUE;
763 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800764
Eric Laurent1703cdf2011-03-07 14:52:59 -0800765 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800766 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100767 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800768
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100769 if (mOutput != 0) {
770 uint32_t halFrames;
771 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
772 }
773 *position = dspFrames;
774 } else {
775 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
776 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
777 mProxy->getPosition();
778 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 return NO_ERROR;
780}
781
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800782status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800783{
784 if (mSharedBuffer == 0 || mIsTimed) {
785 return INVALID_OPERATION;
786 }
787 if (position == NULL) {
788 return BAD_VALUE;
789 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800790
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800791 AutoMutex lock(mLock);
792 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800793 return NO_ERROR;
794}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800795
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800796status_t AudioTrack::reload()
797{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100798 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800799 return INVALID_OPERATION;
800 }
801
Eric Laurent1703cdf2011-03-07 14:52:59 -0800802 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800803 // See setPosition() regarding setting parameters such as loop points or position while active
804 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700805 return INVALID_OPERATION;
806 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800807 mNewPosition = mUpdatePeriod;
808 mLoopPeriod = 0;
809 // FIXME The new code cannot reload while keeping a loop specified.
810 // Need to check how the old code handled this, and whether it's a significant change.
811 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812 return NO_ERROR;
813}
814
Glenn Kasten38e905b2014-01-13 10:21:48 -0800815audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700816{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800817 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100818 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800819}
820
Eric Laurentbe916aa2010-06-01 23:49:17 -0700821status_t AudioTrack::attachAuxEffect(int effectId)
822{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800823 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700824 status_t status = mAudioTrack->attachAuxEffect(effectId);
825 if (status == NO_ERROR) {
826 mAuxEffectId = effectId;
827 }
828 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700829}
830
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831// -------------------------------------------------------------------------
832
Eric Laurent1703cdf2011-03-07 14:52:59 -0800833// must be called with mLock held
834status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800835 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800836 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800837 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800838 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700839 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800840 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800841 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800842{
843 status_t status;
844 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
845 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700846 ALOGE("Could not get audioflinger");
847 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800848 }
849
Glenn Kasten38e905b2014-01-13 10:21:48 -0800850 audio_io_handle_t output = AudioSystem::getOutput(mStreamType, mSampleRate, mFormat,
851 mChannelMask, mFlags, mOffloadInfo);
852 if (output == 0) {
853 ALOGE("Could not get audio output for stream type %d, sample rate %u, format %#x, "
854 "channel mask %#x, flags %#x",
855 mStreamType, mSampleRate, mFormat, mChannelMask, mFlags);
856 return BAD_VALUE;
857 }
858 {
859 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
860 // we must release it ourselves if anything goes wrong.
861
Glenn Kastence8828a2013-09-16 18:07:38 -0700862 // Not all of these values are needed under all conditions, but it is easier to get them all
863
Eric Laurentd1b449a2010-05-14 03:26:45 -0700864 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700865 status = AudioSystem::getLatency(output, streamType, &afLatency);
866 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800867 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800868 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700869 }
870
Glenn Kastence8828a2013-09-16 18:07:38 -0700871 size_t afFrameCount;
872 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
873 if (status != NO_ERROR) {
874 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800875 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700876 }
877
878 uint32_t afSampleRate;
879 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
880 if (status != NO_ERROR) {
881 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800882 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700883 }
884
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885 // Client decides whether the track is TIMED (see below), but can only express a preference
886 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700887 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700888 // either of these use cases:
889 // use case 1: shared buffer
890 (sharedBuffer != 0) ||
891 // use case 2: callback handler
892 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800893 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700894 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700895 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700896 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700897 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700898 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700899
Glenn Kastence8828a2013-09-16 18:07:38 -0700900 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800901 // n = 1 fast track with single buffering; nBuffering is ignored
902 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700903 // n = 2 normal track, no sample rate conversion
904 // n = 3 normal track, with sample rate conversion
905 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
906 // n > 3 very high latency or very small notification interval; nBuffering is ignored
907 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
908
Eric Laurentd1b449a2010-05-14 03:26:45 -0700909 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910
Dima Zavinfce7a472011-04-19 22:30:36 -0700911 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912
Eric Laurentd1b449a2010-05-14 03:26:45 -0700913 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700915 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700917 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700918 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100919 if (mNotificationFramesAct != frameCount) {
920 mNotificationFramesAct = frameCount;
921 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922 } else if (sharedBuffer != 0) {
923
Glenn Kastena42ff002012-11-14 12:47:55 -0800924 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700925 // 8-bit data in shared memory is not currently supported by AudioFlinger
926 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800927 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700928 // More than 2 channels does not require stronger alignment than stereo
929 alignment <<= 1;
930 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800931 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
932 ALOGE("Invalid buffer alignment: address %p, channel count %u",
933 sharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800934 status = BAD_VALUE;
935 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700936 }
937
938 // When initializing a shared buffer AudioTrack via constructors,
939 // there's no frameCount parameter.
940 // But when initializing a shared buffer AudioTrack via set(),
941 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800942 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700943
944 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
945
946 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700947
Eric Laurentd1b449a2010-05-14 03:26:45 -0700948 // Ensure that buffer depth covers at least audio hardware latency
949 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700950 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
951 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700952 if (minBufCount <= nBuffering) {
953 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800954 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700955
Glenn Kastene33054e2012-11-14 12:54:39 -0800956 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
957 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800958 ", afLatency=%d",
959 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700960
961 if (frameCount == 0) {
962 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700963 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700964 // not ALOGW because it happens all the time when playing key clicks over A2DP
965 ALOGV("Minimum buffer size corrected from %d to %d",
966 frameCount, minFrameCount);
967 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800968 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700969 // Make sure that application is notified with sufficient margin before underrun
970 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
971 mNotificationFramesAct = frameCount/nBuffering;
972 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700973
Glenn Kastene0fa4672012-04-24 14:35:14 -0700974 } else {
975 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700976 }
977
Glenn Kastena075db42012-03-06 11:22:44 -0800978 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
979 if (mIsTimed) {
980 trackFlags |= IAudioFlinger::TRACK_TIMED;
981 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800982
983 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700984 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700985 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800986 if (mAudioTrackThread != 0) {
987 tid = mAudioTrackThread->getTid();
988 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700989 }
990
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100991 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
992 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
993 }
994
Glenn Kasten74935e42013-12-19 08:56:45 -0800995 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
996 // but we will still need the original value also
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800997 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800998 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700999 // AudioFlinger only sees 16-bit PCM
1000 format == AUDIO_FORMAT_PCM_8_BIT ?
1001 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -08001002 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -08001003 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -08001004 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001005 sharedBuffer,
1006 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001007 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001008 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001009 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001010 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001011 &status);
1012
1013 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001014 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001015 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001016 }
Glenn Kasten38e905b2014-01-13 10:21:48 -08001017 // AudioFlinger now owns the reference to the I/O handle,
1018 // so we are no longer responsible for releasing it.
1019
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001020 sp<IMemory> iMem = track->getCblk();
1021 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001022 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001023 return NO_INIT;
1024 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001025 void *iMemPointer = iMem->pointer();
1026 if (iMemPointer == NULL) {
1027 ALOGE("Could not get control block pointer");
1028 return NO_INIT;
1029 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001030 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001031 if (mAudioTrack != 0) {
1032 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1033 mDeathNotifier.clear();
1034 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001035 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001036 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001037 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001038 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001039 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001040 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1041 // In current design, AudioTrack client checks and ensures frame count validity before
1042 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1043 // for fast track as it uses a special method of assigning frame count.
1044 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1045 }
1046 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001047 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001048 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001049 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001050 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001051 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001052 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001053 // Theoretically double-buffering is not required for fast tracks,
1054 // due to tighter scheduling. But in practice, to accommodate kernels with
1055 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1056 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1057 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001058 }
1059 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001060 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001061 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001062 // once denied, do not request again if IAudioTrack is re-created
1063 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1064 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001065 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001066 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1067 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001068 }
1069 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001070 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001071 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001072 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1073 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1074 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1075 } else {
1076 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1077 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1078 mFlags = flags;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001079 // FIXME This is a warning, not an error, so don't return error status
1080 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001081 }
1082 }
1083
Glenn Kasten38e905b2014-01-13 10:21:48 -08001084 // We retain a copy of the I/O handle, but don't own the reference
1085 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001086 mRefreshRemaining = true;
1087
1088 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1089 // is the value of pointer() for the shared buffer, otherwise buffers points
1090 // immediately after the control block. This address is for the mapping within client
1091 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1092 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001093 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001094 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001095 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001096 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001097 }
1098
Eric Laurent2beeb502010-07-16 07:43:46 -07001099 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001100 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001101 mLatency = afLatency + (1000*frameCount) / sampleRate;
1102 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001103 // If IAudioTrack is re-created, don't let the requested frameCount
1104 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001105 if (frameCount > mReqFrameCount) {
1106 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001107 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001108
1109 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001110 if (sharedBuffer == 0) {
1111 mStaticProxy.clear();
1112 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1113 } else {
1114 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1115 mProxy = mStaticProxy;
1116 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001117 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1118 uint16_t(mVolume[LEFT] * 0x1000));
1119 mProxy->setSendLevel(mSendLevel);
1120 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001121 mProxy->setEpoch(epoch);
1122 mProxy->setMinimum(mNotificationFramesAct);
1123
1124 mDeathNotifier = new DeathNotifier(this);
1125 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001126
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001127 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001128 }
1129
1130release:
1131 AudioSystem::releaseOutput(output);
1132 if (status == NO_ERROR) {
1133 status = NO_INIT;
1134 }
1135 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001136}
1137
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001138status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1139{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001140 if (audioBuffer == NULL) {
1141 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001142 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001143 if (mTransfer != TRANSFER_OBTAIN) {
1144 audioBuffer->frameCount = 0;
1145 audioBuffer->size = 0;
1146 audioBuffer->raw = NULL;
1147 return INVALID_OPERATION;
1148 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001149
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001150 const struct timespec *requested;
1151 if (waitCount == -1) {
1152 requested = &ClientProxy::kForever;
1153 } else if (waitCount == 0) {
1154 requested = &ClientProxy::kNonBlocking;
1155 } else if (waitCount > 0) {
1156 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1157 struct timespec timeout;
1158 timeout.tv_sec = ms / 1000;
1159 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1160 requested = &timeout;
1161 } else {
1162 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1163 requested = NULL;
1164 }
1165 return obtainBuffer(audioBuffer, requested);
1166}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001167
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001168status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1169 struct timespec *elapsed, size_t *nonContig)
1170{
1171 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1172 uint32_t oldSequence = 0;
1173 uint32_t newSequence;
1174
1175 Proxy::Buffer buffer;
1176 status_t status = NO_ERROR;
1177
1178 static const int32_t kMaxTries = 5;
1179 int32_t tryCounter = kMaxTries;
1180
1181 do {
1182 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1183 // keep them from going away if another thread re-creates the track during obtainBuffer()
1184 sp<AudioTrackClientProxy> proxy;
1185 sp<IMemory> iMem;
1186
1187 { // start of lock scope
1188 AutoMutex lock(mLock);
1189
1190 newSequence = mSequence;
1191 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1192 if (status == DEAD_OBJECT) {
1193 // re-create track, unless someone else has already done so
1194 if (newSequence == oldSequence) {
1195 status = restoreTrack_l("obtainBuffer");
1196 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001197 buffer.mFrameCount = 0;
1198 buffer.mRaw = NULL;
1199 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001200 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001202 }
1203 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001204 oldSequence = newSequence;
1205
1206 // Keep the extra references
1207 proxy = mProxy;
1208 iMem = mCblkMemory;
1209
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001210 if (mState == STATE_STOPPING) {
1211 status = -EINTR;
1212 buffer.mFrameCount = 0;
1213 buffer.mRaw = NULL;
1214 buffer.mNonContig = 0;
1215 break;
1216 }
1217
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001218 // Non-blocking if track is stopped or paused
1219 if (mState != STATE_ACTIVE) {
1220 requested = &ClientProxy::kNonBlocking;
1221 }
1222
1223 } // end of lock scope
1224
1225 buffer.mFrameCount = audioBuffer->frameCount;
1226 // FIXME starts the requested timeout and elapsed over from scratch
1227 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1228
1229 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1230
1231 audioBuffer->frameCount = buffer.mFrameCount;
1232 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1233 audioBuffer->raw = buffer.mRaw;
1234 if (nonContig != NULL) {
1235 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001237 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238}
1239
1240void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1241{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001242 if (mTransfer == TRANSFER_SHARED) {
1243 return;
1244 }
1245
1246 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1247 if (stepCount == 0) {
1248 return;
1249 }
1250
1251 Proxy::Buffer buffer;
1252 buffer.mFrameCount = stepCount;
1253 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001254
Eric Laurent1703cdf2011-03-07 14:52:59 -08001255 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001256 mInUnderrun = false;
1257 mProxy->releaseBuffer(&buffer);
1258
1259 // restart track if it was disabled by audioflinger due to previous underrun
1260 if (mState == STATE_ACTIVE) {
1261 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001262 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001263 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1264 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001265 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001266 mAudioTrack->start();
1267 }
1268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269}
1270
1271// -------------------------------------------------------------------------
1272
1273ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1274{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001275 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001276 return INVALID_OPERATION;
1277 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001279 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001280 // Sanity-check: user is most-likely passing an error code, and it would
1281 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001282 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283 return BAD_VALUE;
1284 }
1285
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001286 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 Buffer audioBuffer;
1288
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001289 while (userSize >= mFrameSize) {
1290 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001291
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001292 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001294 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001296 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 return ssize_t(err);
1298 }
1299
1300 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001301 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001303 toWrite = audioBuffer.size >> 1;
1304 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001305 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001307 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001308 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001309 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310 userSize -= toWrite;
1311 written += toWrite;
1312
1313 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001314 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315
1316 return written;
1317}
1318
1319// -------------------------------------------------------------------------
1320
John Grossman4ff14ba2012-02-08 16:37:41 -08001321TimedAudioTrack::TimedAudioTrack() {
1322 mIsTimed = true;
1323}
1324
1325status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1326{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001327 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001328 status_t result = UNKNOWN_ERROR;
1329
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001330#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001331 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1332 // while we are accessing the cblk
1333 sp<IAudioTrack> audioTrack = mAudioTrack;
1334 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001335#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001336
John Grossman4ff14ba2012-02-08 16:37:41 -08001337 // If the track is not invalid already, try to allocate a buffer. alloc
1338 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001339 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001340 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001341 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001342 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1343 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001344 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001345 }
1346 }
1347
1348 // If the track is invalid at this point, attempt to restore it. and try the
1349 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001350 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001351 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001352
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001353 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001354 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001355 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001356 }
1357
1358 return result;
1359}
1360
1361status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1362 int64_t pts)
1363{
Eric Laurentdf839842012-05-31 14:27:14 -07001364 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1365 {
1366 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001367 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001368 // restart track if it was disabled by audioflinger due to previous underrun
1369 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001370 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1371 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001372 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001373 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001374 mAudioTrack->start();
1375 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001376 }
Eric Laurentdf839842012-05-31 14:27:14 -07001377 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001378}
1379
1380status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1381 TargetTimeline target)
1382{
1383 return mAudioTrack->setMediaTimeTransform(xform, target);
1384}
1385
1386// -------------------------------------------------------------------------
1387
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001388nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001389{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001390 // Currently the AudioTrack thread is not created if there are no callbacks.
1391 // Would it ever make sense to run the thread, even without callbacks?
1392 // If so, then replace this by checks at each use for mCbf != NULL.
1393 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1394
Eric Laurent1703cdf2011-03-07 14:52:59 -08001395 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001396 if (mAwaitBoost) {
1397 mAwaitBoost = false;
1398 mLock.unlock();
1399 static const int32_t kMaxTries = 5;
1400 int32_t tryCounter = kMaxTries;
1401 uint32_t pollUs = 10000;
1402 do {
1403 int policy = sched_getscheduler(0);
1404 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1405 break;
1406 }
1407 usleep(pollUs);
1408 pollUs <<= 1;
1409 } while (tryCounter-- > 0);
1410 if (tryCounter < 0) {
1411 ALOGE("did not receive expected priority boost on time");
1412 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001413 // Run again immediately
1414 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001415 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001416
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001417 // Can only reference mCblk while locked
1418 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001419 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001420
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001421 // Check for track invalidation
1422 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001423 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1424 // AudioSystem cache. We should not exit here but after calling the callback so
1425 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001426 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001427 status_t status = restoreTrack_l("processAudioBuffer");
1428 mLock.unlock();
1429 // Run again immediately, but with a new IAudioTrack
1430 return 0;
1431 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001432 }
1433
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001434 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001435 bool active = mState == STATE_ACTIVE;
1436
1437 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1438 bool newUnderrun = false;
1439 if (flags & CBLK_UNDERRUN) {
1440#if 0
1441 // Currently in shared buffer mode, when the server reaches the end of buffer,
1442 // the track stays active in continuous underrun state. It's up to the application
1443 // to pause or stop the track, or set the position to a new offset within buffer.
1444 // This was some experimental code to auto-pause on underrun. Keeping it here
1445 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1446 if (mTransfer == TRANSFER_SHARED) {
1447 mState = STATE_PAUSED;
1448 active = false;
1449 }
1450#endif
1451 if (!mInUnderrun) {
1452 mInUnderrun = true;
1453 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 }
1455 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001456
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001457 // Get current position of server
1458 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001459
1460 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001461 bool markerReached = false;
1462 size_t markerPosition = mMarkerPosition;
1463 // FIXME fails for wraparound, need 64 bits
1464 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1465 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001466 }
1467
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001468 // Determine number of new position callback(s) that will be needed, while locked
1469 size_t newPosCount = 0;
1470 size_t newPosition = mNewPosition;
1471 size_t updatePeriod = mUpdatePeriod;
1472 // FIXME fails for wraparound, need 64 bits
1473 if (updatePeriod > 0 && position >= newPosition) {
1474 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1475 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001476 }
1477
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001478 // Cache other fields that will be needed soon
1479 uint32_t loopPeriod = mLoopPeriod;
1480 uint32_t sampleRate = mSampleRate;
1481 size_t notificationFrames = mNotificationFramesAct;
1482 if (mRefreshRemaining) {
1483 mRefreshRemaining = false;
1484 mRemainingFrames = notificationFrames;
1485 mRetryOnPartialBuffer = false;
1486 }
1487 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001488 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001489
1490 // These fields don't need to be cached, because they are assigned only by set():
1491 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1492 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1493
1494 mLock.unlock();
1495
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001496 if (waitStreamEnd) {
1497 AutoMutex lock(mLock);
1498
1499 sp<AudioTrackClientProxy> proxy = mProxy;
1500 sp<IMemory> iMem = mCblkMemory;
1501
1502 struct timespec timeout;
1503 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1504 timeout.tv_nsec = 0;
1505
1506 mLock.unlock();
1507 status_t status = mProxy->waitStreamEndDone(&timeout);
1508 mLock.lock();
1509 switch (status) {
1510 case NO_ERROR:
1511 case DEAD_OBJECT:
1512 case TIMED_OUT:
1513 mLock.unlock();
1514 mCbf(EVENT_STREAM_END, mUserData, NULL);
1515 mLock.lock();
1516 if (mState == STATE_STOPPING) {
1517 mState = STATE_STOPPED;
1518 if (status != DEAD_OBJECT) {
1519 return NS_INACTIVE;
1520 }
1521 }
1522 return 0;
1523 default:
1524 return 0;
1525 }
1526 }
1527
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001528 // perform callbacks while unlocked
1529 if (newUnderrun) {
1530 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1531 }
1532 // FIXME we will miss loops if loop cycle was signaled several times since last call
1533 // to processAudioBuffer()
1534 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1535 mCbf(EVENT_LOOP_END, mUserData, NULL);
1536 }
1537 if (flags & CBLK_BUFFER_END) {
1538 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1539 }
1540 if (markerReached) {
1541 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1542 }
1543 while (newPosCount > 0) {
1544 size_t temp = newPosition;
1545 mCbf(EVENT_NEW_POS, mUserData, &temp);
1546 newPosition += updatePeriod;
1547 newPosCount--;
1548 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001549
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001550 if (mObservedSequence != sequence) {
1551 mObservedSequence = sequence;
1552 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001553 // for offloaded tracks, just wait for the upper layers to recreate the track
1554 if (isOffloaded()) {
1555 return NS_INACTIVE;
1556 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001557 }
1558
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001559 // if inactive, then don't run me again until re-started
1560 if (!active) {
1561 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001562 }
1563
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001564 // Compute the estimated time until the next timed event (position, markers, loops)
1565 // FIXME only for non-compressed audio
1566 uint32_t minFrames = ~0;
1567 if (!markerReached && position < markerPosition) {
1568 minFrames = markerPosition - position;
1569 }
1570 if (loopPeriod > 0 && loopPeriod < minFrames) {
1571 minFrames = loopPeriod;
1572 }
1573 if (updatePeriod > 0 && updatePeriod < minFrames) {
1574 minFrames = updatePeriod;
1575 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001576
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001577 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1578 static const uint32_t kPoll = 0;
1579 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1580 minFrames = kPoll * notificationFrames;
1581 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001582
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001583 // Convert frame units to time units
1584 nsecs_t ns = NS_WHENEVER;
1585 if (minFrames != (uint32_t) ~0) {
1586 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1587 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1588 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1589 }
1590
1591 // If not supplying data by EVENT_MORE_DATA, then we're done
1592 if (mTransfer != TRANSFER_CALLBACK) {
1593 return ns;
1594 }
1595
1596 struct timespec timeout;
1597 const struct timespec *requested = &ClientProxy::kForever;
1598 if (ns != NS_WHENEVER) {
1599 timeout.tv_sec = ns / 1000000000LL;
1600 timeout.tv_nsec = ns % 1000000000LL;
1601 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1602 requested = &timeout;
1603 }
1604
1605 while (mRemainingFrames > 0) {
1606
1607 Buffer audioBuffer;
1608 audioBuffer.frameCount = mRemainingFrames;
1609 size_t nonContig;
1610 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1611 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1612 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1613 requested = &ClientProxy::kNonBlocking;
1614 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001615 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1616 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001617 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001618 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1619 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001620 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001622 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1623 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001624 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001625
Eric Laurent42a6f422013-08-29 14:35:05 -07001626 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001627 mRetryOnPartialBuffer = false;
1628 if (avail < mRemainingFrames) {
1629 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1630 if (ns < 0 || myns < ns) {
1631 ns = myns;
1632 }
1633 return ns;
1634 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001635 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001636
1637 // Divide buffer size by 2 to take into account the expansion
1638 // due to 8 to 16 bit conversion: the callback must fill only half
1639 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001640 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001641 audioBuffer.size >>= 1;
1642 }
1643
1644 size_t reqSize = audioBuffer.size;
1645 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001646 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001647
1648 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001649 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1650 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1651 reqSize, (int) writtenSize);
1652 return NS_NEVER;
1653 }
1654
1655 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001656 // The callback is done filling buffers
1657 // Keep this thread going to handle timed events and
1658 // still try to get more data in intervals of WAIT_PERIOD_MS
1659 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001660 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001661 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001662
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001663 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001664 // 8 to 16 bit conversion, note that source and destination are the same address
1665 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001666 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001667 }
1668
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001669 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1670 audioBuffer.frameCount = releasedFrames;
1671 mRemainingFrames -= releasedFrames;
1672 if (misalignment >= releasedFrames) {
1673 misalignment -= releasedFrames;
1674 } else {
1675 misalignment = 0;
1676 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001677
1678 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001680 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1681 // if callback doesn't like to accept the full chunk
1682 if (writtenSize < reqSize) {
1683 continue;
1684 }
1685
1686 // There could be enough non-contiguous frames available to satisfy the remaining request
1687 if (mRemainingFrames <= nonContig) {
1688 continue;
1689 }
1690
1691#if 0
1692 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1693 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1694 // that total to a sum == notificationFrames.
1695 if (0 < misalignment && misalignment <= mRemainingFrames) {
1696 mRemainingFrames = misalignment;
1697 return (mRemainingFrames * 1100000000LL) / sampleRate;
1698 }
1699#endif
1700
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001701 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001702 mRemainingFrames = notificationFrames;
1703 mRetryOnPartialBuffer = true;
1704
1705 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1706 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001707}
1708
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001709status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001710{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001711 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001712 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001713 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001714 status_t result;
1715
Glenn Kastena47f3162012-11-07 10:13:08 -08001716 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kasten38e905b2014-01-13 10:21:48 -08001717 // output parameters in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001718 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001719
Glenn Kasten23a75452014-01-13 10:37:17 -08001720 if (isOffloaded_l()) {
1721 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001722 return DEAD_OBJECT;
1723 }
1724
Glenn Kastena47f3162012-11-07 10:13:08 -08001725 // if the new IAudioTrack is created, createTrack_l() will modify the
1726 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1727 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001728
1729 // take the frames that will be lost by track recreation into account in saved position
1730 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001731 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001732 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001733 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001734 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001735 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001736 mFlags,
1737 mSharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001738 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001739
Glenn Kastena47f3162012-11-07 10:13:08 -08001740 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001741 // continue playback from last known position, but
1742 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1743 if (mStaticProxy != NULL) {
1744 mLoopPeriod = 0;
1745 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1746 }
1747 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1748 // track destruction have been played? This is critical for SoundPool implementation
1749 // This must be broken, and needs to be tested/debugged.
1750#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001751 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001752 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001753 // Make sure that a client relying on callback events indicating underrun or
1754 // the actual amount of audio frames played (e.g SoundPool) receives them.
1755 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001756 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001757 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001758 }
1759 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001760#endif
1761 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001762 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001763 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001764 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001765 if (result != NO_ERROR) {
Glenn Kasten2b2165c2014-01-13 08:53:36 -08001766 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001767#if 0 // FIXME This should no longer be needed
1768 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001769 // As getOutput was called above and resulted in an output stream to be opened,
1770 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001771 if (mOutput != 0) {
1772 AudioSystem::releaseOutput(mOutput);
1773 mOutput = 0;
1774 }
1775#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001776 ALOGW("restoreTrack_l() failed status %d", result);
1777 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001778 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001779
1780 return result;
1781}
1782
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001783status_t AudioTrack::setParameters(const String8& keyValuePairs)
1784{
1785 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001786 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001787}
1788
Glenn Kastence703742013-07-19 16:33:58 -07001789status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1790{
Glenn Kasten53cec222013-08-29 09:01:02 -07001791 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001792 // FIXME not implemented for fast tracks; should use proxy and SSQ
1793 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1794 return INVALID_OPERATION;
1795 }
1796 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1797 return INVALID_OPERATION;
1798 }
1799 status_t status = mAudioTrack->getTimestamp(timestamp);
1800 if (status == NO_ERROR) {
1801 timestamp.mPosition += mProxy->getEpoch();
1802 }
1803 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001804}
1805
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001806String8 AudioTrack::getParameters(const String8& keys)
1807{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001808 audio_io_handle_t output = getOutput();
1809 if (output != 0) {
1810 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001811 } else {
1812 return String8::empty();
1813 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001814}
1815
Glenn Kasten23a75452014-01-13 10:37:17 -08001816bool AudioTrack::isOffloaded() const
1817{
1818 AutoMutex lock(mLock);
1819 return isOffloaded_l();
1820}
1821
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001822status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001823{
1824
1825 const size_t SIZE = 256;
1826 char buffer[SIZE];
1827 String8 result;
1828
1829 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001830 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1831 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001833 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001834 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001836 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001837 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001838 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001839 result.append(buffer);
1840 ::write(fd, result.string(), result.size());
1841 return NO_ERROR;
1842}
1843
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001844uint32_t AudioTrack::getUnderrunFrames() const
1845{
1846 AutoMutex lock(mLock);
1847 return mProxy->getUnderrunFrames();
1848}
1849
1850// =========================================================================
1851
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001852void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001853{
1854 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1855 if (audioTrack != 0) {
1856 AutoMutex lock(audioTrack->mLock);
1857 audioTrack->mProxy->binderDied();
1858 }
1859}
1860
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001861// =========================================================================
1862
1863AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001864 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1865 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001866{
1867}
1868
1869AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001870{
1871}
1872
1873bool AudioTrack::AudioTrackThread::threadLoop()
1874{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001875 {
1876 AutoMutex _l(mMyLock);
1877 if (mPaused) {
1878 mMyCond.wait(mMyLock);
1879 // caller will check for exitPending()
1880 return true;
1881 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001882 if (mIgnoreNextPausedInt) {
1883 mIgnoreNextPausedInt = false;
1884 mPausedInt = false;
1885 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001886 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001887 if (mPausedNs > 0) {
1888 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1889 } else {
1890 mMyCond.wait(mMyLock);
1891 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001892 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001893 return true;
1894 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001895 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001896 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001897 switch (ns) {
1898 case 0:
1899 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001900 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001901 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001902 return true;
1903 case NS_NEVER:
1904 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001905 case NS_WHENEVER:
1906 // FIXME increase poll interval, or make event-driven
1907 ns = 1000000000LL;
1908 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001909 default:
1910 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001911 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001912 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001913 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001914}
1915
Glenn Kasten3acbd052012-02-28 10:39:56 -08001916void AudioTrack::AudioTrackThread::requestExit()
1917{
1918 // must be in this order to avoid a race condition
1919 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001920 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001921}
1922
1923void AudioTrack::AudioTrackThread::pause()
1924{
1925 AutoMutex _l(mMyLock);
1926 mPaused = true;
1927}
1928
1929void AudioTrack::AudioTrackThread::resume()
1930{
1931 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001932 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001933 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001934 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001935 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001936 mMyCond.signal();
1937 }
1938}
1939
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001940void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1941{
1942 AutoMutex _l(mMyLock);
1943 mPausedInt = true;
1944 mPausedNs = ns;
1945}
1946
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001947}; // namespace android