blob: 72be5ca1881438f3a438f1eaeed00edff9c3e5b4 [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
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100248 mOutput = 0;
249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700251 if (streamType == AUDIO_STREAM_DEFAULT) {
252 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800254 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
255 ALOGE("Invalid stream type %d", streamType);
256 return BAD_VALUE;
257 }
258 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700259
Glenn Kastenb1bef512014-01-13 10:25:53 -0800260 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800262 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
263 if (status != NO_ERROR) {
264 ALOGE("Could not get output sample rate for stream type %d; status %d",
265 streamType, status);
266 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700267 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800269 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700270
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800272 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700273 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275
276 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700277 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800278 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 return BAD_VALUE;
280 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800281 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700282
Glenn Kasten8ba90322013-10-30 11:29:27 -0700283 if (!audio_is_output_channel(channelMask)) {
284 ALOGE("Invalid channel mask %#x", channelMask);
285 return BAD_VALUE;
286 }
287
Glenn Kastene0fa4672012-04-24 14:35:14 -0700288 // AudioFlinger does not currently support 8-bit data in shared memory
289 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
290 ALOGE("8-bit data in shared memory is not supported");
291 return BAD_VALUE;
292 }
293
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100295 // or offload was requested
296 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
297 || !audio_is_linear_pcm(format)) {
298 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
299 ? "Offload request, forcing to Direct Output"
300 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700301 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800302 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700303 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700305 // only allow deep buffering for music stream type
306 if (streamType != AUDIO_STREAM_MUSIC) {
307 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
308 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309
Glenn Kastena42ff002012-11-14 12:47:55 -0800310 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700311 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800312 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700313
Glenn Kastene3aa6592012-12-04 12:22:46 -0800314 if (audio_is_linear_pcm(format)) {
315 mFrameSize = channelCount * audio_bytes_per_sample(format);
316 mFrameSizeAF = channelCount * sizeof(int16_t);
317 } else {
318 mFrameSize = sizeof(uint8_t);
319 mFrameSizeAF = sizeof(uint8_t);
320 }
321
Dima Zavinfce7a472011-04-19 22:30:36 -0700322 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800323 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800324 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000325 flags,
326 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327
328 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000329 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330 return BAD_VALUE;
331 }
332
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800333 // Make copy of input parameter offloadInfo so that in the future:
334 // (a) createTrack_l doesn't need it as an input parameter
335 // (b) we can support re-creation of offloaded tracks
336 if (offloadInfo != NULL) {
337 mOffloadInfoCopy = *offloadInfo;
338 mOffloadInfo = &mOffloadInfoCopy;
339 } else {
340 mOffloadInfo = NULL;
341 }
342
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800343 mVolume[LEFT] = 1.0f;
344 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800345 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800346 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800347 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700348 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800349 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700350 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800351 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
352 mClientUid = IPCThreadState::self()->getCallingUid();
353 } else {
354 mClientUid = uid;
355 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700356 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700357 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700358 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700359
Glenn Kastena997e7a2012-08-07 09:44:19 -0700360 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700361 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700362 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
363 }
364
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800365 // create the IAudioTrack
Glenn Kastenb1bef512014-01-13 10:25:53 -0800366 status = createTrack_l(streamType,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800367 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800368 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800369 frameCount,
370 flags,
371 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800372 output,
373 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800374
Glenn Kastena997e7a2012-08-07 09:44:19 -0700375 if (status != NO_ERROR) {
376 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100377 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
378 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700379 mAudioTrackThread.clear();
380 }
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800381 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100382 // As getOutput was called above and resulted in an output stream to be opened,
383 // we need to release it.
384 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700385 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700386 }
387
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800389 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800391 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700393 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 mNewPosition = 0;
395 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700396 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800397 mSequence = 1;
398 mObservedSequence = mSequence;
399 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100400 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800401
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 return NO_ERROR;
403}
404
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405// -------------------------------------------------------------------------
406
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100407status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800409 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100410
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800411 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100412 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 }
414
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800415 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800417 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100418 if (previousState == STATE_PAUSED_STOPPING) {
419 mState = STATE_STOPPING;
420 } else {
421 mState = STATE_ACTIVE;
422 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800423 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
424 // reset current position as seen by client to 0
425 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700426 // force refresh of remaining frames by processAudioBuffer() as last
427 // write before stop could be partial.
428 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800429 }
430 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700431 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800432
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800434 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100435 if (previousState == STATE_STOPPING) {
436 mProxy->interrupt();
437 } else {
438 t->resume();
439 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800440 } else {
441 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
442 get_sched_policy(0, &mPreviousSchedulingGroup);
443 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
444 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800446 status_t status = NO_ERROR;
447 if (!(flags & CBLK_INVALID)) {
448 status = mAudioTrack->start();
449 if (status == DEAD_OBJECT) {
450 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800452 }
453 if (flags & CBLK_INVALID) {
454 status = restoreTrack_l("start");
455 }
456
457 if (status != NO_ERROR) {
458 ALOGE("start() status %d", status);
459 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100461 if (previousState != STATE_STOPPING) {
462 t->pause();
463 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700465 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700466 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 }
468 }
469
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100470 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800471}
472
473void AudioTrack::stop()
474{
475 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700476 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800477 return;
478 }
479
Glenn Kasten23a75452014-01-13 10:37:17 -0800480 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100481 mState = STATE_STOPPING;
482 } else {
483 mState = STATE_STOPPED;
484 }
485
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800486 mProxy->interrupt();
487 mAudioTrack->stop();
488 // the playback head position will reset to 0, so if a marker is set, we need
489 // to activate it again
490 mMarkerReached = false;
491#if 0
492 // Force flush if a shared buffer is used otherwise audioflinger
493 // will not stop before end of buffer is reached.
494 // It may be needed to make sure that we stop playback, likely in case looping is on.
495 if (mSharedBuffer != 0) {
496 flush_l();
497 }
498#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100499
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800500 sp<AudioTrackThread> t = mAudioTrackThread;
501 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800502 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100503 t->pause();
504 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800505 } else {
506 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
507 set_sched_policy(0, mPreviousSchedulingGroup);
508 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509}
510
511bool AudioTrack::stopped() const
512{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800513 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800514 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515}
516
517void AudioTrack::flush()
518{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 if (mSharedBuffer != 0) {
520 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800521 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800522 AutoMutex lock(mLock);
523 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
524 return;
525 }
526 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800527}
528
Eric Laurent1703cdf2011-03-07 14:52:59 -0800529void AudioTrack::flush_l()
530{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800531 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700532
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700533 // clear playback marker and periodic update counter
534 mMarkerPosition = 0;
535 mMarkerReached = false;
536 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100537 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700538
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800539 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800540 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100541 mProxy->interrupt();
542 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800543 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800544 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545}
546
547void AudioTrack::pause()
548{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800549 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100550 if (mState == STATE_ACTIVE) {
551 mState = STATE_PAUSED;
552 } else if (mState == STATE_STOPPING) {
553 mState = STATE_PAUSED_STOPPING;
554 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800555 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800557 mProxy->interrupt();
558 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559}
560
Eric Laurentbe916aa2010-06-01 23:49:17 -0700561status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800563 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700564 return BAD_VALUE;
565 }
566
Eric Laurent1703cdf2011-03-07 14:52:59 -0800567 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 mVolume[LEFT] = left;
569 mVolume[RIGHT] = right;
570
Glenn Kastene3aa6592012-12-04 12:22:46 -0800571 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700572
Glenn Kasten23a75452014-01-13 10:37:17 -0800573 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700574 mAudioTrack->signal();
575 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700576 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577}
578
Glenn Kastenb1c09932012-02-27 16:21:04 -0800579status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800580{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800581 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700582}
583
Eric Laurent2beeb502010-07-16 07:43:46 -0700584status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700585{
Glenn Kasten05632a52012-01-03 14:22:33 -0800586 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587 return BAD_VALUE;
588 }
589
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800590 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700591 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800592 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700593
594 return NO_ERROR;
595}
596
Glenn Kastena5224f32012-01-04 12:41:44 -0800597void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700598{
599 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800600 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700601 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602}
603
Glenn Kasten3b16c762012-11-14 08:44:39 -0800604status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800605{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100606 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800607 return INVALID_OPERATION;
608 }
609
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800610 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700612 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 }
614 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700615 if (rate == 0 || rate > afSamplingRate*2 ) {
616 return BAD_VALUE;
617 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618
Eric Laurent1703cdf2011-03-07 14:52:59 -0800619 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800620 mSampleRate = rate;
621 mProxy->setSampleRate(rate);
622
Eric Laurent57326622009-07-07 07:10:45 -0700623 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624}
625
Glenn Kastena5224f32012-01-04 12:41:44 -0800626uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627{
John Grossman4ff14ba2012-02-08 16:37:41 -0800628 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800629 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800630 }
631
Eric Laurent1703cdf2011-03-07 14:52:59 -0800632 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700633
634 // sample rate can be updated during playback by the offloaded decoder so we need to
635 // query the HAL and update if needed.
636// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800637 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700638 if (mOutput != 0) {
639 uint32_t sampleRate = 0;
640 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
641 if (status == NO_ERROR) {
642 mSampleRate = sampleRate;
643 }
644 }
645 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800646 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647}
648
649status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
650{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100651 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800652 return INVALID_OPERATION;
653 }
654
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800656 ;
657 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
658 loopEnd - loopStart >= MIN_LOOP) {
659 ;
660 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661 return BAD_VALUE;
662 }
663
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800664 AutoMutex lock(mLock);
665 // See setPosition() regarding setting parameters such as loop points or position while active
666 if (mState == STATE_ACTIVE) {
667 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700668 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800669 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 return NO_ERROR;
671}
672
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800673void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
674{
675 // FIXME If setting a loop also sets position to start of loop, then
676 // this is correct. Otherwise it should be removed.
677 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
678 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
679 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
680}
681
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682status_t AudioTrack::setMarkerPosition(uint32_t marker)
683{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700684 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100685 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700686 return INVALID_OPERATION;
687 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800689 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700691 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692
693 return NO_ERROR;
694}
695
Glenn Kastena5224f32012-01-04 12:41:44 -0800696status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100698 if (isOffloaded()) {
699 return INVALID_OPERATION;
700 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700701 if (marker == NULL) {
702 return BAD_VALUE;
703 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800705 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 *marker = mMarkerPosition;
707
708 return NO_ERROR;
709}
710
711status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
712{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700713 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100714 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700715 return INVALID_OPERATION;
716 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800717
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800718 AutoMutex lock(mLock);
719 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800721
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800722 return NO_ERROR;
723}
724
Glenn Kastena5224f32012-01-04 12:41:44 -0800725status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100727 if (isOffloaded()) {
728 return INVALID_OPERATION;
729 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700730 if (updatePeriod == NULL) {
731 return BAD_VALUE;
732 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800733
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800734 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735 *updatePeriod = mUpdatePeriod;
736
737 return NO_ERROR;
738}
739
740status_t AudioTrack::setPosition(uint32_t position)
741{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100742 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700743 return INVALID_OPERATION;
744 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800745 if (position > mFrameCount) {
746 return BAD_VALUE;
747 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800748
Eric Laurent1703cdf2011-03-07 14:52:59 -0800749 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800750 // Currently we require that the player is inactive before setting parameters such as position
751 // or loop points. Otherwise, there could be a race condition: the application could read the
752 // current position, compute a new position or loop parameters, and then set that position or
753 // loop parameters but it would do the "wrong" thing since the position has continued to advance
754 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
755 // to specify how it wants to handle such scenarios.
756 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700757 return INVALID_OPERATION;
758 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800759 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
760 mLoopPeriod = 0;
761 // FIXME Check whether loops and setting position are incompatible in old code.
762 // If we use setLoop for both purposes we lose the capability to set the position while looping.
763 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700764
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 return NO_ERROR;
766}
767
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800768status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700770 if (position == NULL) {
771 return BAD_VALUE;
772 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800773
Eric Laurent1703cdf2011-03-07 14:52:59 -0800774 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800775 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100776 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800777
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100778 if (mOutput != 0) {
779 uint32_t halFrames;
780 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
781 }
782 *position = dspFrames;
783 } else {
784 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
785 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
786 mProxy->getPosition();
787 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800788 return NO_ERROR;
789}
790
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800791status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800792{
793 if (mSharedBuffer == 0 || mIsTimed) {
794 return INVALID_OPERATION;
795 }
796 if (position == NULL) {
797 return BAD_VALUE;
798 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800799
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800800 AutoMutex lock(mLock);
801 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800802 return NO_ERROR;
803}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800804
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800805status_t AudioTrack::reload()
806{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100807 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800808 return INVALID_OPERATION;
809 }
810
Eric Laurent1703cdf2011-03-07 14:52:59 -0800811 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800812 // See setPosition() regarding setting parameters such as loop points or position while active
813 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700814 return INVALID_OPERATION;
815 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800816 mNewPosition = mUpdatePeriod;
817 mLoopPeriod = 0;
818 // FIXME The new code cannot reload while keeping a loop specified.
819 // Need to check how the old code handled this, and whether it's a significant change.
820 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800821 return NO_ERROR;
822}
823
Eric Laurentc2f1f072009-07-17 12:17:14 -0700824audio_io_handle_t AudioTrack::getOutput()
825{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800826 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100827 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800828}
829
830// must be called with mLock held
831audio_io_handle_t AudioTrack::getOutput_l()
832{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100833 if (mOutput) {
834 return mOutput;
835 } else {
836 return AudioSystem::getOutput(mStreamType,
837 mSampleRate, mFormat, mChannelMask, mFlags);
838 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700839}
840
Eric Laurentbe916aa2010-06-01 23:49:17 -0700841status_t AudioTrack::attachAuxEffect(int effectId)
842{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800843 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700844 status_t status = mAudioTrack->attachAuxEffect(effectId);
845 if (status == NO_ERROR) {
846 mAuxEffectId = effectId;
847 }
848 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700849}
850
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800851// -------------------------------------------------------------------------
852
Eric Laurent1703cdf2011-03-07 14:52:59 -0800853// must be called with mLock held
854status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800855 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800856 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800857 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800858 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700859 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800860 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800861 audio_io_handle_t output,
862 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800863{
864 status_t status;
865 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
866 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700867 ALOGE("Could not get audioflinger");
868 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800869 }
870
Glenn Kastence8828a2013-09-16 18:07:38 -0700871 // Not all of these values are needed under all conditions, but it is easier to get them all
872
Eric Laurentd1b449a2010-05-14 03:26:45 -0700873 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700874 status = AudioSystem::getLatency(output, streamType, &afLatency);
875 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800876 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700877 return NO_INIT;
878 }
879
Glenn Kastence8828a2013-09-16 18:07:38 -0700880 size_t afFrameCount;
881 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
882 if (status != NO_ERROR) {
883 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
884 return NO_INIT;
885 }
886
887 uint32_t afSampleRate;
888 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
889 if (status != NO_ERROR) {
890 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
891 return NO_INIT;
892 }
893
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700894 // Client decides whether the track is TIMED (see below), but can only express a preference
895 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700896 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700897 // either of these use cases:
898 // use case 1: shared buffer
899 (sharedBuffer != 0) ||
900 // use case 2: callback handler
901 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800902 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700903 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700904 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700905 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700906 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700907 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700908
Glenn Kastence8828a2013-09-16 18:07:38 -0700909 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800910 // n = 1 fast track with single buffering; nBuffering is ignored
911 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700912 // n = 2 normal track, no sample rate conversion
913 // n = 3 normal track, with sample rate conversion
914 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
915 // n > 3 very high latency or very small notification interval; nBuffering is ignored
916 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
917
Eric Laurentd1b449a2010-05-14 03:26:45 -0700918 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700919
Dima Zavinfce7a472011-04-19 22:30:36 -0700920 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700921
Eric Laurentd1b449a2010-05-14 03:26:45 -0700922 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700923 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700924 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700925 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700926 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700927 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100928 if (mNotificationFramesAct != frameCount) {
929 mNotificationFramesAct = frameCount;
930 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700931 } else if (sharedBuffer != 0) {
932
Glenn Kastena42ff002012-11-14 12:47:55 -0800933 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934 // 8-bit data in shared memory is not currently supported by AudioFlinger
935 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800936 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700937 // More than 2 channels does not require stronger alignment than stereo
938 alignment <<= 1;
939 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800940 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
941 ALOGE("Invalid buffer alignment: address %p, channel count %u",
942 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700943 return BAD_VALUE;
944 }
945
946 // When initializing a shared buffer AudioTrack via constructors,
947 // there's no frameCount parameter.
948 // But when initializing a shared buffer AudioTrack via set(),
949 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800950 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700951
952 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
953
954 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700955
Eric Laurentd1b449a2010-05-14 03:26:45 -0700956 // Ensure that buffer depth covers at least audio hardware latency
957 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700958 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
959 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700960 if (minBufCount <= nBuffering) {
961 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800962 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700963
Glenn Kastene33054e2012-11-14 12:54:39 -0800964 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
965 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800966 ", afLatency=%d",
967 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700968
969 if (frameCount == 0) {
970 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700971 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700972 // not ALOGW because it happens all the time when playing key clicks over A2DP
973 ALOGV("Minimum buffer size corrected from %d to %d",
974 frameCount, minFrameCount);
975 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800976 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700977 // Make sure that application is notified with sufficient margin before underrun
978 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
979 mNotificationFramesAct = frameCount/nBuffering;
980 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700981
Glenn Kastene0fa4672012-04-24 14:35:14 -0700982 } else {
983 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700984 }
985
Glenn Kastena075db42012-03-06 11:22:44 -0800986 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
987 if (mIsTimed) {
988 trackFlags |= IAudioFlinger::TRACK_TIMED;
989 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800990
991 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700992 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700993 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800994 if (mAudioTrackThread != 0) {
995 tid = mAudioTrackThread->getTid();
996 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700997 }
998
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100999 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1000 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1001 }
1002
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001003 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001005 // AudioFlinger only sees 16-bit PCM
1006 format == AUDIO_FORMAT_PCM_8_BIT ?
1007 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -08001008 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001009 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -08001010 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001011 sharedBuffer,
1012 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001013 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001014 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001015 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001016 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001017 &status);
1018
1019 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001020 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001021 return status;
1022 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001023 sp<IMemory> iMem = track->getCblk();
1024 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001025 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001026 return NO_INIT;
1027 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001028 void *iMemPointer = iMem->pointer();
1029 if (iMemPointer == NULL) {
1030 ALOGE("Could not get control block pointer");
1031 return NO_INIT;
1032 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001033 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001034 if (mAudioTrack != 0) {
1035 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1036 mDeathNotifier.clear();
1037 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001038 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001039 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001040 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001041 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001042 size_t temp = cblk->frameCount_;
1043 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1044 // In current design, AudioTrack client checks and ensures frame count validity before
1045 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1046 // for fast track as it uses a special method of assigning frame count.
1047 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1048 }
1049 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001050 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001051 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001052 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001053 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001054 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001055 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001056 // Theoretically double-buffering is not required for fast tracks,
1057 // due to tighter scheduling. But in practice, to accommodate kernels with
1058 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1059 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1060 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001061 }
1062 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001063 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001064 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001065 // once denied, do not request again if IAudioTrack is re-created
1066 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1067 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001068 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001069 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1070 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001071 }
1072 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001073 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001074 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001075 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1076 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1077 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1078 } else {
1079 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1080 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1081 mFlags = flags;
1082 return NO_INIT;
1083 }
1084 }
1085
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;
1128}
1129
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001130status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1131{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001132 if (audioBuffer == NULL) {
1133 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001134 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001135 if (mTransfer != TRANSFER_OBTAIN) {
1136 audioBuffer->frameCount = 0;
1137 audioBuffer->size = 0;
1138 audioBuffer->raw = NULL;
1139 return INVALID_OPERATION;
1140 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001141
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001142 const struct timespec *requested;
1143 if (waitCount == -1) {
1144 requested = &ClientProxy::kForever;
1145 } else if (waitCount == 0) {
1146 requested = &ClientProxy::kNonBlocking;
1147 } else if (waitCount > 0) {
1148 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1149 struct timespec timeout;
1150 timeout.tv_sec = ms / 1000;
1151 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1152 requested = &timeout;
1153 } else {
1154 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1155 requested = NULL;
1156 }
1157 return obtainBuffer(audioBuffer, requested);
1158}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001159
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001160status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1161 struct timespec *elapsed, size_t *nonContig)
1162{
1163 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1164 uint32_t oldSequence = 0;
1165 uint32_t newSequence;
1166
1167 Proxy::Buffer buffer;
1168 status_t status = NO_ERROR;
1169
1170 static const int32_t kMaxTries = 5;
1171 int32_t tryCounter = kMaxTries;
1172
1173 do {
1174 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1175 // keep them from going away if another thread re-creates the track during obtainBuffer()
1176 sp<AudioTrackClientProxy> proxy;
1177 sp<IMemory> iMem;
1178
1179 { // start of lock scope
1180 AutoMutex lock(mLock);
1181
1182 newSequence = mSequence;
1183 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1184 if (status == DEAD_OBJECT) {
1185 // re-create track, unless someone else has already done so
1186 if (newSequence == oldSequence) {
1187 status = restoreTrack_l("obtainBuffer");
1188 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001189 buffer.mFrameCount = 0;
1190 buffer.mRaw = NULL;
1191 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001192 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001193 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001194 }
1195 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001196 oldSequence = newSequence;
1197
1198 // Keep the extra references
1199 proxy = mProxy;
1200 iMem = mCblkMemory;
1201
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001202 if (mState == STATE_STOPPING) {
1203 status = -EINTR;
1204 buffer.mFrameCount = 0;
1205 buffer.mRaw = NULL;
1206 buffer.mNonContig = 0;
1207 break;
1208 }
1209
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001210 // Non-blocking if track is stopped or paused
1211 if (mState != STATE_ACTIVE) {
1212 requested = &ClientProxy::kNonBlocking;
1213 }
1214
1215 } // end of lock scope
1216
1217 buffer.mFrameCount = audioBuffer->frameCount;
1218 // FIXME starts the requested timeout and elapsed over from scratch
1219 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1220
1221 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1222
1223 audioBuffer->frameCount = buffer.mFrameCount;
1224 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1225 audioBuffer->raw = buffer.mRaw;
1226 if (nonContig != NULL) {
1227 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001229 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230}
1231
1232void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1233{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001234 if (mTransfer == TRANSFER_SHARED) {
1235 return;
1236 }
1237
1238 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1239 if (stepCount == 0) {
1240 return;
1241 }
1242
1243 Proxy::Buffer buffer;
1244 buffer.mFrameCount = stepCount;
1245 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001246
Eric Laurent1703cdf2011-03-07 14:52:59 -08001247 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001248 mInUnderrun = false;
1249 mProxy->releaseBuffer(&buffer);
1250
1251 // restart track if it was disabled by audioflinger due to previous underrun
1252 if (mState == STATE_ACTIVE) {
1253 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001254 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001255 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1256 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001257 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001258 mAudioTrack->start();
1259 }
1260 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261}
1262
1263// -------------------------------------------------------------------------
1264
1265ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1266{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001268 return INVALID_OPERATION;
1269 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001271 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001272 // Sanity-check: user is most-likely passing an error code, and it would
1273 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001274 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 return BAD_VALUE;
1276 }
1277
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001278 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279 Buffer audioBuffer;
1280
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001281 while (userSize >= mFrameSize) {
1282 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001283
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001284 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001286 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001288 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001289 return ssize_t(err);
1290 }
1291
1292 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001293 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001295 toWrite = audioBuffer.size >> 1;
1296 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001297 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001299 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001301 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 userSize -= toWrite;
1303 written += toWrite;
1304
1305 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001306 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307
1308 return written;
1309}
1310
1311// -------------------------------------------------------------------------
1312
John Grossman4ff14ba2012-02-08 16:37:41 -08001313TimedAudioTrack::TimedAudioTrack() {
1314 mIsTimed = true;
1315}
1316
1317status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1318{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001319 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001320 status_t result = UNKNOWN_ERROR;
1321
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001322#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001323 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1324 // while we are accessing the cblk
1325 sp<IAudioTrack> audioTrack = mAudioTrack;
1326 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001327#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001328
John Grossman4ff14ba2012-02-08 16:37:41 -08001329 // If the track is not invalid already, try to allocate a buffer. alloc
1330 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001331 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001332 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001333 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001334 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1335 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001336 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001337 }
1338 }
1339
1340 // If the track is invalid at this point, attempt to restore it. and try the
1341 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001342 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001343 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001344
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001345 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001346 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001347 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001348 }
1349
1350 return result;
1351}
1352
1353status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1354 int64_t pts)
1355{
Eric Laurentdf839842012-05-31 14:27:14 -07001356 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1357 {
1358 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001359 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001360 // restart track if it was disabled by audioflinger due to previous underrun
1361 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001362 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1363 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001364 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001365 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001366 mAudioTrack->start();
1367 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001368 }
Eric Laurentdf839842012-05-31 14:27:14 -07001369 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001370}
1371
1372status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1373 TargetTimeline target)
1374{
1375 return mAudioTrack->setMediaTimeTransform(xform, target);
1376}
1377
1378// -------------------------------------------------------------------------
1379
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001380nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001382 // Currently the AudioTrack thread is not created if there are no callbacks.
1383 // Would it ever make sense to run the thread, even without callbacks?
1384 // If so, then replace this by checks at each use for mCbf != NULL.
1385 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1386
Eric Laurent1703cdf2011-03-07 14:52:59 -08001387 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001388 if (mAwaitBoost) {
1389 mAwaitBoost = false;
1390 mLock.unlock();
1391 static const int32_t kMaxTries = 5;
1392 int32_t tryCounter = kMaxTries;
1393 uint32_t pollUs = 10000;
1394 do {
1395 int policy = sched_getscheduler(0);
1396 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1397 break;
1398 }
1399 usleep(pollUs);
1400 pollUs <<= 1;
1401 } while (tryCounter-- > 0);
1402 if (tryCounter < 0) {
1403 ALOGE("did not receive expected priority boost on time");
1404 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001405 // Run again immediately
1406 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001407 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001408
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001409 // Can only reference mCblk while locked
1410 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001411 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001412
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001413 // Check for track invalidation
1414 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001415 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1416 // AudioSystem cache. We should not exit here but after calling the callback so
1417 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001418 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001419 status_t status = restoreTrack_l("processAudioBuffer");
1420 mLock.unlock();
1421 // Run again immediately, but with a new IAudioTrack
1422 return 0;
1423 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001424 }
1425
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001426 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001427 bool active = mState == STATE_ACTIVE;
1428
1429 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1430 bool newUnderrun = false;
1431 if (flags & CBLK_UNDERRUN) {
1432#if 0
1433 // Currently in shared buffer mode, when the server reaches the end of buffer,
1434 // the track stays active in continuous underrun state. It's up to the application
1435 // to pause or stop the track, or set the position to a new offset within buffer.
1436 // This was some experimental code to auto-pause on underrun. Keeping it here
1437 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1438 if (mTransfer == TRANSFER_SHARED) {
1439 mState = STATE_PAUSED;
1440 active = false;
1441 }
1442#endif
1443 if (!mInUnderrun) {
1444 mInUnderrun = true;
1445 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001446 }
1447 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001448
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001449 // Get current position of server
1450 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001451
1452 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001453 bool markerReached = false;
1454 size_t markerPosition = mMarkerPosition;
1455 // FIXME fails for wraparound, need 64 bits
1456 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1457 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458 }
1459
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001460 // Determine number of new position callback(s) that will be needed, while locked
1461 size_t newPosCount = 0;
1462 size_t newPosition = mNewPosition;
1463 size_t updatePeriod = mUpdatePeriod;
1464 // FIXME fails for wraparound, need 64 bits
1465 if (updatePeriod > 0 && position >= newPosition) {
1466 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1467 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001468 }
1469
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001470 // Cache other fields that will be needed soon
1471 uint32_t loopPeriod = mLoopPeriod;
1472 uint32_t sampleRate = mSampleRate;
1473 size_t notificationFrames = mNotificationFramesAct;
1474 if (mRefreshRemaining) {
1475 mRefreshRemaining = false;
1476 mRemainingFrames = notificationFrames;
1477 mRetryOnPartialBuffer = false;
1478 }
1479 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001480 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001481
1482 // These fields don't need to be cached, because they are assigned only by set():
1483 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1484 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1485
1486 mLock.unlock();
1487
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001488 if (waitStreamEnd) {
1489 AutoMutex lock(mLock);
1490
1491 sp<AudioTrackClientProxy> proxy = mProxy;
1492 sp<IMemory> iMem = mCblkMemory;
1493
1494 struct timespec timeout;
1495 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1496 timeout.tv_nsec = 0;
1497
1498 mLock.unlock();
1499 status_t status = mProxy->waitStreamEndDone(&timeout);
1500 mLock.lock();
1501 switch (status) {
1502 case NO_ERROR:
1503 case DEAD_OBJECT:
1504 case TIMED_OUT:
1505 mLock.unlock();
1506 mCbf(EVENT_STREAM_END, mUserData, NULL);
1507 mLock.lock();
1508 if (mState == STATE_STOPPING) {
1509 mState = STATE_STOPPED;
1510 if (status != DEAD_OBJECT) {
1511 return NS_INACTIVE;
1512 }
1513 }
1514 return 0;
1515 default:
1516 return 0;
1517 }
1518 }
1519
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001520 // perform callbacks while unlocked
1521 if (newUnderrun) {
1522 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1523 }
1524 // FIXME we will miss loops if loop cycle was signaled several times since last call
1525 // to processAudioBuffer()
1526 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1527 mCbf(EVENT_LOOP_END, mUserData, NULL);
1528 }
1529 if (flags & CBLK_BUFFER_END) {
1530 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1531 }
1532 if (markerReached) {
1533 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1534 }
1535 while (newPosCount > 0) {
1536 size_t temp = newPosition;
1537 mCbf(EVENT_NEW_POS, mUserData, &temp);
1538 newPosition += updatePeriod;
1539 newPosCount--;
1540 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001541
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001542 if (mObservedSequence != sequence) {
1543 mObservedSequence = sequence;
1544 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001545 // for offloaded tracks, just wait for the upper layers to recreate the track
1546 if (isOffloaded()) {
1547 return NS_INACTIVE;
1548 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001549 }
1550
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001551 // if inactive, then don't run me again until re-started
1552 if (!active) {
1553 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001554 }
1555
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001556 // Compute the estimated time until the next timed event (position, markers, loops)
1557 // FIXME only for non-compressed audio
1558 uint32_t minFrames = ~0;
1559 if (!markerReached && position < markerPosition) {
1560 minFrames = markerPosition - position;
1561 }
1562 if (loopPeriod > 0 && loopPeriod < minFrames) {
1563 minFrames = loopPeriod;
1564 }
1565 if (updatePeriod > 0 && updatePeriod < minFrames) {
1566 minFrames = updatePeriod;
1567 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001568
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001569 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1570 static const uint32_t kPoll = 0;
1571 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1572 minFrames = kPoll * notificationFrames;
1573 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001574
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001575 // Convert frame units to time units
1576 nsecs_t ns = NS_WHENEVER;
1577 if (minFrames != (uint32_t) ~0) {
1578 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1579 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1580 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1581 }
1582
1583 // If not supplying data by EVENT_MORE_DATA, then we're done
1584 if (mTransfer != TRANSFER_CALLBACK) {
1585 return ns;
1586 }
1587
1588 struct timespec timeout;
1589 const struct timespec *requested = &ClientProxy::kForever;
1590 if (ns != NS_WHENEVER) {
1591 timeout.tv_sec = ns / 1000000000LL;
1592 timeout.tv_nsec = ns % 1000000000LL;
1593 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1594 requested = &timeout;
1595 }
1596
1597 while (mRemainingFrames > 0) {
1598
1599 Buffer audioBuffer;
1600 audioBuffer.frameCount = mRemainingFrames;
1601 size_t nonContig;
1602 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1603 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1604 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1605 requested = &ClientProxy::kNonBlocking;
1606 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001607 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1608 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001609 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001610 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1611 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001612 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001613 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001614 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1615 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001617
Eric Laurent42a6f422013-08-29 14:35:05 -07001618 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001619 mRetryOnPartialBuffer = false;
1620 if (avail < mRemainingFrames) {
1621 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1622 if (ns < 0 || myns < ns) {
1623 ns = myns;
1624 }
1625 return ns;
1626 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001627 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001628
1629 // Divide buffer size by 2 to take into account the expansion
1630 // due to 8 to 16 bit conversion: the callback must fill only half
1631 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001632 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001633 audioBuffer.size >>= 1;
1634 }
1635
1636 size_t reqSize = audioBuffer.size;
1637 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001638 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001639
1640 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001641 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1642 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1643 reqSize, (int) writtenSize);
1644 return NS_NEVER;
1645 }
1646
1647 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001648 // The callback is done filling buffers
1649 // Keep this thread going to handle timed events and
1650 // still try to get more data in intervals of WAIT_PERIOD_MS
1651 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001652 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001653 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001654
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001655 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001656 // 8 to 16 bit conversion, note that source and destination are the same address
1657 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001658 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001659 }
1660
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001661 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1662 audioBuffer.frameCount = releasedFrames;
1663 mRemainingFrames -= releasedFrames;
1664 if (misalignment >= releasedFrames) {
1665 misalignment -= releasedFrames;
1666 } else {
1667 misalignment = 0;
1668 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001669
1670 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001671
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001672 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1673 // if callback doesn't like to accept the full chunk
1674 if (writtenSize < reqSize) {
1675 continue;
1676 }
1677
1678 // There could be enough non-contiguous frames available to satisfy the remaining request
1679 if (mRemainingFrames <= nonContig) {
1680 continue;
1681 }
1682
1683#if 0
1684 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1685 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1686 // that total to a sum == notificationFrames.
1687 if (0 < misalignment && misalignment <= mRemainingFrames) {
1688 mRemainingFrames = misalignment;
1689 return (mRemainingFrames * 1100000000LL) / sampleRate;
1690 }
1691#endif
1692
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001693 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001694 mRemainingFrames = notificationFrames;
1695 mRetryOnPartialBuffer = true;
1696
1697 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1698 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001699}
1700
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001701status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001702{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001703 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001704 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001705 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001706 status_t result;
1707
Glenn Kastena47f3162012-11-07 10:13:08 -08001708 // refresh the audio configuration cache in this process to make sure we get new
1709 // output parameters in getOutput_l() and createTrack_l()
1710 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001711
Glenn Kasten23a75452014-01-13 10:37:17 -08001712 if (isOffloaded_l()) {
1713 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001714 return DEAD_OBJECT;
1715 }
1716
1717 // force new output query from audio policy manager;
1718 mOutput = 0;
1719 audio_io_handle_t output = getOutput_l();
1720
Glenn Kastena47f3162012-11-07 10:13:08 -08001721 // if the new IAudioTrack is created, createTrack_l() will modify the
1722 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1723 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001724
1725 // take the frames that will be lost by track recreation into account in saved position
1726 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001727 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001728 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001729 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001730 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001731 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001732 mFlags,
1733 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001734 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001735 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001736
Glenn Kastena47f3162012-11-07 10:13:08 -08001737 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001738 // continue playback from last known position, but
1739 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1740 if (mStaticProxy != NULL) {
1741 mLoopPeriod = 0;
1742 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1743 }
1744 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1745 // track destruction have been played? This is critical for SoundPool implementation
1746 // This must be broken, and needs to be tested/debugged.
1747#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001748 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001749 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001750 // Make sure that a client relying on callback events indicating underrun or
1751 // the actual amount of audio frames played (e.g SoundPool) receives them.
1752 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001753 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001754 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001755 }
1756 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001757#endif
1758 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001759 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001760 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001761 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001762 if (result != NO_ERROR) {
Glenn Kasten2b2165c2014-01-13 08:53:36 -08001763 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001764 // As getOutput was called above and resulted in an output stream to be opened,
1765 // we need to release it.
1766 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001767 ALOGW("restoreTrack_l() failed status %d", result);
1768 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001769 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001770
1771 return result;
1772}
1773
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001774status_t AudioTrack::setParameters(const String8& keyValuePairs)
1775{
1776 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001777 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001778}
1779
Glenn Kastence703742013-07-19 16:33:58 -07001780status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1781{
Glenn Kasten53cec222013-08-29 09:01:02 -07001782 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001783 // FIXME not implemented for fast tracks; should use proxy and SSQ
1784 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1785 return INVALID_OPERATION;
1786 }
1787 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1788 return INVALID_OPERATION;
1789 }
1790 status_t status = mAudioTrack->getTimestamp(timestamp);
1791 if (status == NO_ERROR) {
1792 timestamp.mPosition += mProxy->getEpoch();
1793 }
1794 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001795}
1796
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001797String8 AudioTrack::getParameters(const String8& keys)
1798{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001799 audio_io_handle_t output = getOutput();
1800 if (output != 0) {
1801 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001802 } else {
1803 return String8::empty();
1804 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001805}
1806
Glenn Kasten23a75452014-01-13 10:37:17 -08001807bool AudioTrack::isOffloaded() const
1808{
1809 AutoMutex lock(mLock);
1810 return isOffloaded_l();
1811}
1812
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001813status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001814{
1815
1816 const size_t SIZE = 256;
1817 char buffer[SIZE];
1818 String8 result;
1819
1820 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001821 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1822 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001823 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001824 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001825 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001826 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001827 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001828 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001829 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001830 result.append(buffer);
1831 ::write(fd, result.string(), result.size());
1832 return NO_ERROR;
1833}
1834
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001835uint32_t AudioTrack::getUnderrunFrames() const
1836{
1837 AutoMutex lock(mLock);
1838 return mProxy->getUnderrunFrames();
1839}
1840
1841// =========================================================================
1842
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001843void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001844{
1845 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1846 if (audioTrack != 0) {
1847 AutoMutex lock(audioTrack->mLock);
1848 audioTrack->mProxy->binderDied();
1849 }
1850}
1851
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001852// =========================================================================
1853
1854AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001855 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1856 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001857{
1858}
1859
1860AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001861{
1862}
1863
1864bool AudioTrack::AudioTrackThread::threadLoop()
1865{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001866 {
1867 AutoMutex _l(mMyLock);
1868 if (mPaused) {
1869 mMyCond.wait(mMyLock);
1870 // caller will check for exitPending()
1871 return true;
1872 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001873 if (mIgnoreNextPausedInt) {
1874 mIgnoreNextPausedInt = false;
1875 mPausedInt = false;
1876 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001877 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001878 if (mPausedNs > 0) {
1879 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1880 } else {
1881 mMyCond.wait(mMyLock);
1882 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001883 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001884 return true;
1885 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001886 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001887 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001888 switch (ns) {
1889 case 0:
1890 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001891 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001892 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001893 return true;
1894 case NS_NEVER:
1895 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001896 case NS_WHENEVER:
1897 // FIXME increase poll interval, or make event-driven
1898 ns = 1000000000LL;
1899 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001900 default:
1901 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001902 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001903 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001904 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001905}
1906
Glenn Kasten3acbd052012-02-28 10:39:56 -08001907void AudioTrack::AudioTrackThread::requestExit()
1908{
1909 // must be in this order to avoid a race condition
1910 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001911 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001912}
1913
1914void AudioTrack::AudioTrackThread::pause()
1915{
1916 AutoMutex _l(mMyLock);
1917 mPaused = true;
1918}
1919
1920void AudioTrack::AudioTrackThread::resume()
1921{
1922 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001923 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001924 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001925 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001926 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001927 mMyCond.signal();
1928 }
1929}
1930
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001931void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1932{
1933 AutoMutex _l(mMyLock);
1934 mPausedInt = true;
1935 mPausedNs = ns;
1936}
1937
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001938}; // namespace android