blob: 3f3a88cca2369a6a7f46949a2b98da401f295d34 [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
47 // default to 0 in case of error
48 *frameCount = 0;
49
Glenn Kastene0fa4672012-04-24 14:35:14 -070050 // FIXME merge with similar code in createTrack_l(), except we're missing
51 // some information here that is available in createTrack_l():
52 // audio_io_handle_t output
53 // audio_format_t format
54 // audio_channel_mask_t channelMask
55 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080056 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080057 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
58 return NO_INIT;
59 }
Glenn Kastene33054e2012-11-14 12:54:39 -080060 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080061 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
62 return NO_INIT;
63 }
64 uint32_t afLatency;
65 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
66 return NO_INIT;
67 }
68
69 // Ensure that buffer depth covers at least audio hardware latency
70 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080071 if (minBufCount < 2) {
72 minBufCount = 2;
73 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080074
75 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070076 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080077 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
78 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080079 return NO_ERROR;
80}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080081
82// ---------------------------------------------------------------------------
83
84AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070085 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080086 mIsTimed(false),
87 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George2048c222014-01-08 13:59:53 -080088 mPreviousSchedulingGroup(SP_DEFAULT),
89 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090{
91}
92
93AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080094 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080096 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -070097 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070099 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100 callback_t cbf,
101 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700102 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800103 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000104 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800105 const audio_offload_info_t *offloadInfo,
106 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700107 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800108 mIsTimed(false),
109 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George2048c222014-01-08 13:59:53 -0800110 mPreviousSchedulingGroup(SP_DEFAULT),
111 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800112{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700113 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700114 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800115 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
116 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117}
118
Andreas Huberc8139852012-01-18 10:51:55 -0800119AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800120 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800122 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700123 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700125 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800126 callback_t cbf,
127 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700128 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800129 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000130 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800131 const audio_offload_info_t *offloadInfo,
132 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700133 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800134 mIsTimed(false),
135 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Haynes Mathew George2048c222014-01-08 13:59:53 -0800136 mPreviousSchedulingGroup(SP_DEFAULT),
137 mPausedPosition(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700139 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800140 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800141 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142}
143
144AudioTrack::~AudioTrack()
145{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 if (mStatus == NO_ERROR) {
147 // Make sure that callback function exits in the case where
148 // it is looping on buffer full condition in obtainBuffer().
149 // Otherwise the callback thread will never exit.
150 stop();
151 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100152 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800153 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 mAudioTrackThread->requestExitAndWait();
155 mAudioTrackThread.clear();
156 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700157 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
158 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700160 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162}
163
164status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800165 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800167 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700168 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800169 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700170 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 callback_t cbf,
172 void* user,
173 int notificationFrames,
174 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700175 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800176 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000177 transfer_type transferType,
Marco Nelissen9cae2172013-01-14 14:12:05 -0800178 const audio_offload_info_t *offloadInfo,
179 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800181 switch (transferType) {
182 case TRANSFER_DEFAULT:
183 if (sharedBuffer != 0) {
184 transferType = TRANSFER_SHARED;
185 } else if (cbf == NULL || threadCanCallJava) {
186 transferType = TRANSFER_SYNC;
187 } else {
188 transferType = TRANSFER_CALLBACK;
189 }
190 break;
191 case TRANSFER_CALLBACK:
192 if (cbf == NULL || sharedBuffer != 0) {
193 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
194 return BAD_VALUE;
195 }
196 break;
197 case TRANSFER_OBTAIN:
198 case TRANSFER_SYNC:
199 if (sharedBuffer != 0) {
200 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
201 return BAD_VALUE;
202 }
203 break;
204 case TRANSFER_SHARED:
205 if (sharedBuffer == 0) {
206 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
207 return BAD_VALUE;
208 }
209 break;
210 default:
211 ALOGE("Invalid transfer type %d", transferType);
212 return BAD_VALUE;
213 }
214 mTransfer = transferType;
215
Glenn Kastene33054e2012-11-14 12:54:39 -0800216 // FIXME "int" here is legacy and will be replaced by size_t later
217 if (frameCountInt < 0) {
218 ALOGE("Invalid frame count %d", frameCountInt);
219 return BAD_VALUE;
220 }
221 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700223 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
224 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225
Glenn Kastene33054e2012-11-14 12:54:39 -0800226 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700227
Eric Laurent1703cdf2011-03-07 14:52:59 -0800228 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800229
Glenn Kasten53cec222013-08-29 09:01:02 -0700230 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700231 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000232 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233 return INVALID_OPERATION;
234 }
235
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100236 mOutput = 0;
237
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700239 if (streamType == AUDIO_STREAM_DEFAULT) {
240 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800244 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700245 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
246 return NO_INIT;
247 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 sampleRate = afSampleRate;
249 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800250 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700251
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800253 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700254 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700256 if (channelMask == 0) {
257 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 }
259
260 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700261 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800262 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263 return BAD_VALUE;
264 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265
Glenn Kastene0fa4672012-04-24 14:35:14 -0700266 // AudioFlinger does not currently support 8-bit data in shared memory
267 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
268 ALOGE("8-bit data in shared memory is not supported");
269 return BAD_VALUE;
270 }
271
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100273 // or offload was requested
274 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
275 || !audio_is_linear_pcm(format)) {
276 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
277 ? "Offload request, forcing to Direct Output"
278 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700279 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800280 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700281 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700282 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700283 // only allow deep buffering for music stream type
284 if (streamType != AUDIO_STREAM_MUSIC) {
285 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
286 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700288 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700289 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290 return BAD_VALUE;
291 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800292 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700293 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800294 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700295
Glenn Kastene3aa6592012-12-04 12:22:46 -0800296 if (audio_is_linear_pcm(format)) {
297 mFrameSize = channelCount * audio_bytes_per_sample(format);
298 mFrameSizeAF = channelCount * sizeof(int16_t);
299 } else {
300 mFrameSize = sizeof(uint8_t);
301 mFrameSizeAF = sizeof(uint8_t);
302 }
303
Dima Zavinfce7a472011-04-19 22:30:36 -0700304 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800305 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800306 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000307 flags,
308 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309
310 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000311 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 return BAD_VALUE;
313 }
314
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800315 mVolume[LEFT] = 1.0f;
316 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800317 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700318 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800319 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700320 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800321 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700322 mSessionId = sessionId;
Marco Nelissen9cae2172013-01-14 14:12:05 -0800323 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
324 mClientUid = IPCThreadState::self()->getCallingUid();
325 } else {
326 mClientUid = uid;
327 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700328 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700329 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700330 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700331
Glenn Kastena997e7a2012-08-07 09:44:19 -0700332 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700333 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700334 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
335 }
336
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800337 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800338 status_t status = createTrack_l(streamType,
339 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800340 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800341 frameCount,
342 flags,
343 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800344 output,
345 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800346
Glenn Kastena997e7a2012-08-07 09:44:19 -0700347 if (status != NO_ERROR) {
348 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100349 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
350 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700351 mAudioTrackThread.clear();
352 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100353 //Use of direct and offloaded output streams is ref counted by audio policy manager.
354 // As getOutput was called above and resulted in an output stream to be opened,
355 // we need to release it.
356 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700357 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700358 }
359
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800362 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800364 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800366 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700368 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800369 mNewPosition = 0;
370 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700371 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800372 mSequence = 1;
373 mObservedSequence = mSequence;
374 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100375 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800376
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377 return NO_ERROR;
378}
379
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380// -------------------------------------------------------------------------
381
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100382status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800384 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100385
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800386 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100387 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 }
389
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800390 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800392 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100393 if (previousState == STATE_PAUSED_STOPPING) {
394 mState = STATE_STOPPING;
395 } else {
396 mState = STATE_ACTIVE;
397 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800398 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
399 // reset current position as seen by client to 0
400 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700401 // force refresh of remaining frames by processAudioBuffer() as last
402 // write before stop could be partial.
403 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800404 }
405 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700406 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800409 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100410 if (previousState == STATE_STOPPING) {
411 mProxy->interrupt();
412 } else {
413 t->resume();
414 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800415 } else {
416 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
417 get_sched_policy(0, &mPreviousSchedulingGroup);
418 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
419 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800420
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800421 status_t status = NO_ERROR;
422 if (!(flags & CBLK_INVALID)) {
423 status = mAudioTrack->start();
424 if (status == DEAD_OBJECT) {
425 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800427 }
428 if (flags & CBLK_INVALID) {
429 status = restoreTrack_l("start");
430 }
431
432 if (status != NO_ERROR) {
433 ALOGE("start() status %d", status);
434 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100436 if (previousState != STATE_STOPPING) {
437 t->pause();
438 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800439 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700440 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700441 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 }
443 }
444
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100445 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800446}
447
448void AudioTrack::stop()
449{
450 AutoMutex lock(mLock);
451 // FIXME pause then stop should not be a nop
452 if (mState != STATE_ACTIVE) {
453 return;
454 }
455
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100456 if (isOffloaded()) {
457 mState = STATE_STOPPING;
458 } else {
459 mState = STATE_STOPPED;
460 }
461
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800462 mProxy->interrupt();
463 mAudioTrack->stop();
464 // the playback head position will reset to 0, so if a marker is set, we need
465 // to activate it again
466 mMarkerReached = false;
467#if 0
468 // Force flush if a shared buffer is used otherwise audioflinger
469 // will not stop before end of buffer is reached.
470 // It may be needed to make sure that we stop playback, likely in case looping is on.
471 if (mSharedBuffer != 0) {
472 flush_l();
473 }
474#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100475
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800476 sp<AudioTrackThread> t = mAudioTrackThread;
477 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100478 if (!isOffloaded()) {
479 t->pause();
480 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800481 } else {
482 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
483 set_sched_policy(0, mPreviousSchedulingGroup);
484 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485}
486
487bool AudioTrack::stopped() const
488{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800489 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800490 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491}
492
493void AudioTrack::flush()
494{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800495 if (mSharedBuffer != 0) {
496 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800497 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800498 AutoMutex lock(mLock);
499 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
500 return;
501 }
502 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800503}
504
Eric Laurent1703cdf2011-03-07 14:52:59 -0800505void AudioTrack::flush_l()
506{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800507 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700508
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700509 // clear playback marker and periodic update counter
510 mMarkerPosition = 0;
511 mMarkerReached = false;
512 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100513 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700514
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800515 mState = STATE_FLUSHED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100516 if (isOffloaded()) {
517 mProxy->interrupt();
518 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800519 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800520 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521}
522
523void AudioTrack::pause()
524{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800525 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100526 if (mState == STATE_ACTIVE) {
527 mState = STATE_PAUSED;
528 } else if (mState == STATE_STOPPING) {
529 mState = STATE_PAUSED_STOPPING;
530 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800531 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800533 mProxy->interrupt();
534 mAudioTrack->pause();
Haynes Mathew George2048c222014-01-08 13:59:53 -0800535
536 if (isOffloaded()) {
537 if (mOutput != 0) {
538 uint32_t halFrames;
539 // OffloadThread sends HAL pause in its threadLoop.. time saved
540 // here can be slightly off
541 AudioSystem::getRenderPosition(mOutput, &halFrames, &mPausedPosition);
542 ALOGV("AudioTrack::pause for offload, cache current position %u", mPausedPosition);
543 }
544 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545}
546
Eric Laurentbe916aa2010-06-01 23:49:17 -0700547status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800549 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700550 return BAD_VALUE;
551 }
552
Eric Laurent1703cdf2011-03-07 14:52:59 -0800553 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 mVolume[LEFT] = left;
555 mVolume[RIGHT] = right;
556
Glenn Kastene3aa6592012-12-04 12:22:46 -0800557 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700558
Eric Laurent59fe0102013-09-27 18:48:26 -0700559 if (isOffloaded()) {
560 mAudioTrack->signal();
561 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700562 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563}
564
Glenn Kastenb1c09932012-02-27 16:21:04 -0800565status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800567 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700568}
569
Eric Laurent2beeb502010-07-16 07:43:46 -0700570status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571{
Glenn Kasten05632a52012-01-03 14:22:33 -0800572 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700573 return BAD_VALUE;
574 }
575
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800576 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700577 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800578 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700579
580 return NO_ERROR;
581}
582
Glenn Kastena5224f32012-01-04 12:41:44 -0800583void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700584{
585 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800586 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588}
589
Glenn Kasten3b16c762012-11-14 08:44:39 -0800590status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100592 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800593 return INVALID_OPERATION;
594 }
595
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800596 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700598 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 }
600 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700601 if (rate == 0 || rate > afSamplingRate*2 ) {
602 return BAD_VALUE;
603 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604
Eric Laurent1703cdf2011-03-07 14:52:59 -0800605 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800606 mSampleRate = rate;
607 mProxy->setSampleRate(rate);
608
Eric Laurent57326622009-07-07 07:10:45 -0700609 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610}
611
Glenn Kastena5224f32012-01-04 12:41:44 -0800612uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613{
John Grossman4ff14ba2012-02-08 16:37:41 -0800614 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800615 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800616 }
617
Eric Laurent1703cdf2011-03-07 14:52:59 -0800618 AutoMutex lock(mLock);
Eric Laurent5e49afd2013-07-26 17:16:50 -0700619
620 // sample rate can be updated during playback by the offloaded decoder so we need to
621 // query the HAL and update if needed.
622// FIXME use Proxy return channel to update the rate from server and avoid polling here
623 if (isOffloaded()) {
624 if (mOutput != 0) {
625 uint32_t sampleRate = 0;
626 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
627 if (status == NO_ERROR) {
628 mSampleRate = sampleRate;
629 }
630 }
631 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800632 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633}
634
635status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
636{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100637 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800638 return INVALID_OPERATION;
639 }
640
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800642 ;
643 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
644 loopEnd - loopStart >= MIN_LOOP) {
645 ;
646 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647 return BAD_VALUE;
648 }
649
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800650 AutoMutex lock(mLock);
651 // See setPosition() regarding setting parameters such as loop points or position while active
652 if (mState == STATE_ACTIVE) {
653 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700654 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800655 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 return NO_ERROR;
657}
658
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800659void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
660{
661 // FIXME If setting a loop also sets position to start of loop, then
662 // this is correct. Otherwise it should be removed.
663 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
664 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
665 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
666}
667
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668status_t AudioTrack::setMarkerPosition(uint32_t marker)
669{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700670 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100671 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700672 return INVALID_OPERATION;
673 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800675 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700677 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678
679 return NO_ERROR;
680}
681
Glenn Kastena5224f32012-01-04 12:41:44 -0800682status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100684 if (isOffloaded()) {
685 return INVALID_OPERATION;
686 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700687 if (marker == NULL) {
688 return BAD_VALUE;
689 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800691 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692 *marker = mMarkerPosition;
693
694 return NO_ERROR;
695}
696
697status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
698{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700699 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100700 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700701 return INVALID_OPERATION;
702 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800704 AutoMutex lock(mLock);
705 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 return NO_ERROR;
708}
709
Glenn Kastena5224f32012-01-04 12:41:44 -0800710status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100712 if (isOffloaded()) {
713 return INVALID_OPERATION;
714 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700715 if (updatePeriod == NULL) {
716 return BAD_VALUE;
717 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800719 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 *updatePeriod = mUpdatePeriod;
721
722 return NO_ERROR;
723}
724
725status_t AudioTrack::setPosition(uint32_t position)
726{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100727 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700728 return INVALID_OPERATION;
729 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800730 if (position > mFrameCount) {
731 return BAD_VALUE;
732 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800733
Eric Laurent1703cdf2011-03-07 14:52:59 -0800734 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800735 // Currently we require that the player is inactive before setting parameters such as position
736 // or loop points. Otherwise, there could be a race condition: the application could read the
737 // current position, compute a new position or loop parameters, and then set that position or
738 // loop parameters but it would do the "wrong" thing since the position has continued to advance
739 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
740 // to specify how it wants to handle such scenarios.
741 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700742 return INVALID_OPERATION;
743 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800744 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
745 mLoopPeriod = 0;
746 // FIXME Check whether loops and setting position are incompatible in old code.
747 // If we use setLoop for both purposes we lose the capability to set the position while looping.
748 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700749
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 return NO_ERROR;
751}
752
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800753status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700755 if (position == NULL) {
756 return BAD_VALUE;
757 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800758
Eric Laurent1703cdf2011-03-07 14:52:59 -0800759 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100760 if (isOffloaded()) {
761 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762
Haynes Mathew George2048c222014-01-08 13:59:53 -0800763 if ((mState == STATE_PAUSED) || (mState == STATE_PAUSED_STOPPING)) {
764 ALOGV("getPosition called in paused state, return cached position %u", mPausedPosition);
765 *position = mPausedPosition;
766 return NO_ERROR;
767 }
768
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100769 if (mOutput != 0) {
770 uint32_t halFrames;
771 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
772 }
773 *position = dspFrames;
774 } else {
775 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
776 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
777 mProxy->getPosition();
778 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 return NO_ERROR;
780}
781
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000782status_t AudioTrack::getBufferPosition(uint32_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800783{
784 if (mSharedBuffer == 0 || mIsTimed) {
785 return INVALID_OPERATION;
786 }
787 if (position == NULL) {
788 return BAD_VALUE;
789 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800790
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800791 AutoMutex lock(mLock);
792 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800793 return NO_ERROR;
794}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800795
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800796status_t AudioTrack::reload()
797{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100798 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800799 return INVALID_OPERATION;
800 }
801
Eric Laurent1703cdf2011-03-07 14:52:59 -0800802 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800803 // See setPosition() regarding setting parameters such as loop points or position while active
804 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700805 return INVALID_OPERATION;
806 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800807 mNewPosition = mUpdatePeriod;
808 mLoopPeriod = 0;
809 // FIXME The new code cannot reload while keeping a loop specified.
810 // Need to check how the old code handled this, and whether it's a significant change.
811 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812 return NO_ERROR;
813}
814
Eric Laurentc2f1f072009-07-17 12:17:14 -0700815audio_io_handle_t AudioTrack::getOutput()
816{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800817 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100818 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800819}
820
821// must be called with mLock held
822audio_io_handle_t AudioTrack::getOutput_l()
823{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100824 if (mOutput) {
825 return mOutput;
826 } else {
827 return AudioSystem::getOutput(mStreamType,
828 mSampleRate, mFormat, mChannelMask, mFlags);
829 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700830}
831
Eric Laurentbe916aa2010-06-01 23:49:17 -0700832status_t AudioTrack::attachAuxEffect(int effectId)
833{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800834 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700835 status_t status = mAudioTrack->attachAuxEffect(effectId);
836 if (status == NO_ERROR) {
837 mAuxEffectId = effectId;
838 }
839 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700840}
841
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800842// -------------------------------------------------------------------------
843
Eric Laurent1703cdf2011-03-07 14:52:59 -0800844// must be called with mLock held
845status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800846 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800847 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800848 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800849 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700850 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800851 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800852 audio_io_handle_t output,
853 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800854{
855 status_t status;
856 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
857 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700858 ALOGE("Could not get audioflinger");
859 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800860 }
861
Glenn Kastence8828a2013-09-16 18:07:38 -0700862 // Not all of these values are needed under all conditions, but it is easier to get them all
863
Eric Laurentd1b449a2010-05-14 03:26:45 -0700864 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700865 status = AudioSystem::getLatency(output, streamType, &afLatency);
866 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800867 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700868 return NO_INIT;
869 }
870
Glenn Kastence8828a2013-09-16 18:07:38 -0700871 size_t afFrameCount;
872 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
873 if (status != NO_ERROR) {
874 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
875 return NO_INIT;
876 }
877
878 uint32_t afSampleRate;
879 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
880 if (status != NO_ERROR) {
881 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
882 return NO_INIT;
883 }
884
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885 // Client decides whether the track is TIMED (see below), but can only express a preference
886 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700887 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700888 // either of these use cases:
889 // use case 1: shared buffer
890 (sharedBuffer != 0) ||
891 // use case 2: callback handler
892 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800893 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700894 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700895 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700896 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700897 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700898 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700899
Martin Storsjo398f2132014-01-31 13:30:15 +0200900 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && sampleRate != afSampleRate) {
901 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client due to mismatching sample rate (%d vs %d)",
902 sampleRate, afSampleRate);
903 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
904 }
905
Glenn Kastence8828a2013-09-16 18:07:38 -0700906 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800907 // n = 1 fast track with single buffering; nBuffering is ignored
908 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700909 // n = 2 normal track, no sample rate conversion
910 // n = 3 normal track, with sample rate conversion
911 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
912 // n > 3 very high latency or very small notification interval; nBuffering is ignored
913 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
914
Eric Laurentd1b449a2010-05-14 03:26:45 -0700915 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916
Dima Zavinfce7a472011-04-19 22:30:36 -0700917 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700918
Eric Laurentd1b449a2010-05-14 03:26:45 -0700919 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700921 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700923 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700924 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100925 if (mNotificationFramesAct != frameCount) {
926 mNotificationFramesAct = frameCount;
927 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700928 } else if (sharedBuffer != 0) {
929
Glenn Kastena42ff002012-11-14 12:47:55 -0800930 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700931 // 8-bit data in shared memory is not currently supported by AudioFlinger
932 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800933 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934 // More than 2 channels does not require stronger alignment than stereo
935 alignment <<= 1;
936 }
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000937 if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800938 ALOGE("Invalid buffer alignment: address %p, channel count %u",
939 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700940 return BAD_VALUE;
941 }
942
943 // When initializing a shared buffer AudioTrack via constructors,
944 // there's no frameCount parameter.
945 // But when initializing a shared buffer AudioTrack via set(),
946 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800947 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700948
949 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
950
951 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700952
Eric Laurentd1b449a2010-05-14 03:26:45 -0700953 // Ensure that buffer depth covers at least audio hardware latency
954 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700955 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
956 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700957 if (minBufCount <= nBuffering) {
958 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800959 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700960
Glenn Kastene33054e2012-11-14 12:54:39 -0800961 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
962 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800963 ", afLatency=%d",
964 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700965
966 if (frameCount == 0) {
967 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700968 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700969 // not ALOGW because it happens all the time when playing key clicks over A2DP
970 ALOGV("Minimum buffer size corrected from %d to %d",
971 frameCount, minFrameCount);
972 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800973 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700974 // Make sure that application is notified with sufficient margin before underrun
975 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
976 mNotificationFramesAct = frameCount/nBuffering;
977 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700978
Glenn Kastene0fa4672012-04-24 14:35:14 -0700979 } else {
980 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700981 }
982
Glenn Kastena075db42012-03-06 11:22:44 -0800983 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
984 if (mIsTimed) {
985 trackFlags |= IAudioFlinger::TRACK_TIMED;
986 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800987
988 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700989 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700990 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800991 if (mAudioTrackThread != 0) {
992 tid = mAudioTrackThread->getTid();
993 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700994 }
995
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100996 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
997 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
998 }
999
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001000 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001001 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001002 // AudioFlinger only sees 16-bit PCM
1003 format == AUDIO_FORMAT_PCM_8_BIT ?
1004 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -08001005 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001006 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -08001007 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001008 sharedBuffer,
1009 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001010 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001011 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001012 mName,
Marco Nelissen9cae2172013-01-14 14:12:05 -08001013 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001014 &status);
1015
1016 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001017 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001018 return status;
1019 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001020 sp<IMemory> iMem = track->getCblk();
1021 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001022 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001023 return NO_INIT;
1024 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001025 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001026 if (mAudioTrack != 0) {
1027 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1028 mDeathNotifier.clear();
1029 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001030 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001031 mCblkMemory = iMem;
1032 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1033 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001034 size_t temp = cblk->frameCount_;
1035 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1036 // In current design, AudioTrack client checks and ensures frame count validity before
1037 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1038 // for fast track as it uses a special method of assigning frame count.
1039 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1040 }
1041 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001042 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001043 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001044 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001045 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001046 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001047 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001048 // Theoretically double-buffering is not required for fast tracks,
1049 // due to tighter scheduling. But in practice, to accommodate kernels with
1050 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1051 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1052 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001053 }
1054 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001055 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001056 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001057 // once denied, do not request again if IAudioTrack is re-created
1058 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1059 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001060 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001061 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1062 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001063 }
1064 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001065 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001066 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001067 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1068 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1069 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1070 } else {
1071 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1072 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1073 mFlags = flags;
1074 return NO_INIT;
1075 }
1076 }
1077
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001078 mRefreshRemaining = true;
1079
1080 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1081 // is the value of pointer() for the shared buffer, otherwise buffers points
1082 // immediately after the control block. This address is for the mapping within client
1083 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1084 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001085 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001086 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001087 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001088 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001089 }
1090
Eric Laurent2beeb502010-07-16 07:43:46 -07001091 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001092 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001093 mLatency = afLatency + (1000*frameCount) / sampleRate;
1094 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001095 // If IAudioTrack is re-created, don't let the requested frameCount
1096 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001097 if (frameCount > mReqFrameCount) {
1098 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001099 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001100
1101 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001102 if (sharedBuffer == 0) {
1103 mStaticProxy.clear();
1104 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1105 } else {
1106 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1107 mProxy = mStaticProxy;
1108 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001109 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1110 uint16_t(mVolume[LEFT] * 0x1000));
1111 mProxy->setSendLevel(mSendLevel);
1112 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001113 mProxy->setEpoch(epoch);
1114 mProxy->setMinimum(mNotificationFramesAct);
1115
1116 mDeathNotifier = new DeathNotifier(this);
1117 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001118
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001119 return NO_ERROR;
1120}
1121
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1123{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001124 if (audioBuffer == NULL) {
1125 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001126 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001127 if (mTransfer != TRANSFER_OBTAIN) {
1128 audioBuffer->frameCount = 0;
1129 audioBuffer->size = 0;
1130 audioBuffer->raw = NULL;
1131 return INVALID_OPERATION;
1132 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001133
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001134 const struct timespec *requested;
Eric Laurent88876fb2014-01-27 18:13:39 -08001135 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001136 if (waitCount == -1) {
1137 requested = &ClientProxy::kForever;
1138 } else if (waitCount == 0) {
1139 requested = &ClientProxy::kNonBlocking;
1140 } else if (waitCount > 0) {
1141 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001142 timeout.tv_sec = ms / 1000;
1143 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1144 requested = &timeout;
1145 } else {
1146 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1147 requested = NULL;
1148 }
1149 return obtainBuffer(audioBuffer, requested);
1150}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001151
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001152status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1153 struct timespec *elapsed, size_t *nonContig)
1154{
1155 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1156 uint32_t oldSequence = 0;
1157 uint32_t newSequence;
1158
1159 Proxy::Buffer buffer;
1160 status_t status = NO_ERROR;
1161
1162 static const int32_t kMaxTries = 5;
1163 int32_t tryCounter = kMaxTries;
1164
1165 do {
1166 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1167 // keep them from going away if another thread re-creates the track during obtainBuffer()
1168 sp<AudioTrackClientProxy> proxy;
1169 sp<IMemory> iMem;
1170
1171 { // start of lock scope
1172 AutoMutex lock(mLock);
1173
1174 newSequence = mSequence;
1175 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1176 if (status == DEAD_OBJECT) {
1177 // re-create track, unless someone else has already done so
1178 if (newSequence == oldSequence) {
1179 status = restoreTrack_l("obtainBuffer");
1180 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001181 buffer.mFrameCount = 0;
1182 buffer.mRaw = NULL;
1183 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001184 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001185 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186 }
1187 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001188 oldSequence = newSequence;
1189
1190 // Keep the extra references
1191 proxy = mProxy;
1192 iMem = mCblkMemory;
1193
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001194 if (mState == STATE_STOPPING) {
1195 status = -EINTR;
1196 buffer.mFrameCount = 0;
1197 buffer.mRaw = NULL;
1198 buffer.mNonContig = 0;
1199 break;
1200 }
1201
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001202 // Non-blocking if track is stopped or paused
1203 if (mState != STATE_ACTIVE) {
1204 requested = &ClientProxy::kNonBlocking;
1205 }
1206
1207 } // end of lock scope
1208
1209 buffer.mFrameCount = audioBuffer->frameCount;
1210 // FIXME starts the requested timeout and elapsed over from scratch
1211 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1212
1213 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1214
1215 audioBuffer->frameCount = buffer.mFrameCount;
1216 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1217 audioBuffer->raw = buffer.mRaw;
1218 if (nonContig != NULL) {
1219 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001220 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001221 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222}
1223
1224void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1225{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001226 if (mTransfer == TRANSFER_SHARED) {
1227 return;
1228 }
1229
1230 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1231 if (stepCount == 0) {
1232 return;
1233 }
1234
1235 Proxy::Buffer buffer;
1236 buffer.mFrameCount = stepCount;
1237 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001238
Eric Laurent1703cdf2011-03-07 14:52:59 -08001239 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001240 mInUnderrun = false;
1241 mProxy->releaseBuffer(&buffer);
1242
1243 // restart track if it was disabled by audioflinger due to previous underrun
1244 if (mState == STATE_ACTIVE) {
1245 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001246 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001247 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1248 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001249 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001250 mAudioTrack->start();
1251 }
1252 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253}
1254
1255// -------------------------------------------------------------------------
1256
1257ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1258{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001259 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001260 return INVALID_OPERATION;
1261 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001263 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001264 // Sanity-check: user is most-likely passing an error code, and it would
1265 // make the return value ambiguous (actualSize vs error).
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001266 ALOGE("AudioTrack::write(buffer=%p, size=%zu (%zd)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267 return BAD_VALUE;
1268 }
1269
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001270 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 Buffer audioBuffer;
1272
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001273 while (userSize >= mFrameSize) {
1274 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001275
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001276 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001278 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001280 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281 return ssize_t(err);
1282 }
1283
1284 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001285 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001287 toWrite = audioBuffer.size >> 1;
1288 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001289 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001291 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001293 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 userSize -= toWrite;
1295 written += toWrite;
1296
1297 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001298 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299
1300 return written;
1301}
1302
1303// -------------------------------------------------------------------------
1304
John Grossman4ff14ba2012-02-08 16:37:41 -08001305TimedAudioTrack::TimedAudioTrack() {
1306 mIsTimed = true;
1307}
1308
1309status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1310{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001311 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001312 status_t result = UNKNOWN_ERROR;
1313
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001314#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001315 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1316 // while we are accessing the cblk
1317 sp<IAudioTrack> audioTrack = mAudioTrack;
1318 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001319#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001320
John Grossman4ff14ba2012-02-08 16:37:41 -08001321 // If the track is not invalid already, try to allocate a buffer. alloc
1322 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001323 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001324 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001325 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001326 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1327 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001328 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001329 }
1330 }
1331
1332 // If the track is invalid at this point, attempt to restore it. and try the
1333 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001334 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001335 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001336
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001337 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001338 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001339 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001340 }
1341
1342 return result;
1343}
1344
1345status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1346 int64_t pts)
1347{
Eric Laurentdf839842012-05-31 14:27:14 -07001348 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1349 {
1350 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001351 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001352 // restart track if it was disabled by audioflinger due to previous underrun
1353 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001354 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1355 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001356 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001357 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001358 mAudioTrack->start();
1359 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001360 }
Eric Laurentdf839842012-05-31 14:27:14 -07001361 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001362}
1363
1364status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1365 TargetTimeline target)
1366{
1367 return mAudioTrack->setMediaTimeTransform(xform, target);
1368}
1369
1370// -------------------------------------------------------------------------
1371
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001372nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001373{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001374 // Currently the AudioTrack thread is not created if there are no callbacks.
1375 // Would it ever make sense to run the thread, even without callbacks?
1376 // If so, then replace this by checks at each use for mCbf != NULL.
1377 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1378
Eric Laurent1703cdf2011-03-07 14:52:59 -08001379 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001380 if (mAwaitBoost) {
1381 mAwaitBoost = false;
1382 mLock.unlock();
1383 static const int32_t kMaxTries = 5;
1384 int32_t tryCounter = kMaxTries;
1385 uint32_t pollUs = 10000;
1386 do {
1387 int policy = sched_getscheduler(0);
1388 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1389 break;
1390 }
1391 usleep(pollUs);
1392 pollUs <<= 1;
1393 } while (tryCounter-- > 0);
1394 if (tryCounter < 0) {
1395 ALOGE("did not receive expected priority boost on time");
1396 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001397 // Run again immediately
1398 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001399 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001400
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001401 // Can only reference mCblk while locked
1402 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001403 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001404
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001405 // Check for track invalidation
1406 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001407 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1408 // AudioSystem cache. We should not exit here but after calling the callback so
1409 // that the upper layers can recreate the track
1410 if (!isOffloaded() || (mSequence == mObservedSequence)) {
1411 status_t status = restoreTrack_l("processAudioBuffer");
1412 mLock.unlock();
1413 // Run again immediately, but with a new IAudioTrack
1414 return 0;
1415 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001416 }
1417
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001418 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001419 bool active = mState == STATE_ACTIVE;
1420
1421 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1422 bool newUnderrun = false;
1423 if (flags & CBLK_UNDERRUN) {
1424#if 0
1425 // Currently in shared buffer mode, when the server reaches the end of buffer,
1426 // the track stays active in continuous underrun state. It's up to the application
1427 // to pause or stop the track, or set the position to a new offset within buffer.
1428 // This was some experimental code to auto-pause on underrun. Keeping it here
1429 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1430 if (mTransfer == TRANSFER_SHARED) {
1431 mState = STATE_PAUSED;
1432 active = false;
1433 }
1434#endif
1435 if (!mInUnderrun) {
1436 mInUnderrun = true;
1437 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438 }
1439 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001440
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001441 // Get current position of server
1442 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443
1444 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001445 bool markerReached = false;
1446 size_t markerPosition = mMarkerPosition;
1447 // FIXME fails for wraparound, need 64 bits
1448 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1449 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001450 }
1451
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001452 // Determine number of new position callback(s) that will be needed, while locked
1453 size_t newPosCount = 0;
1454 size_t newPosition = mNewPosition;
1455 size_t updatePeriod = mUpdatePeriod;
1456 // FIXME fails for wraparound, need 64 bits
1457 if (updatePeriod > 0 && position >= newPosition) {
1458 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1459 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460 }
1461
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001462 // Cache other fields that will be needed soon
1463 uint32_t loopPeriod = mLoopPeriod;
1464 uint32_t sampleRate = mSampleRate;
1465 size_t notificationFrames = mNotificationFramesAct;
1466 if (mRefreshRemaining) {
1467 mRefreshRemaining = false;
1468 mRemainingFrames = notificationFrames;
1469 mRetryOnPartialBuffer = false;
1470 }
1471 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001472 uint32_t sequence = mSequence;
Glenn Kastendcec9032013-09-20 09:28:56 -07001473 sp<AudioTrackClientProxy> proxy = mProxy;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001474
1475 // These fields don't need to be cached, because they are assigned only by set():
1476 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1477 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1478
1479 mLock.unlock();
1480
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001481 if (waitStreamEnd) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001482 struct timespec timeout;
1483 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1484 timeout.tv_nsec = 0;
1485
Glenn Kastendcec9032013-09-20 09:28:56 -07001486 status_t status = proxy->waitStreamEndDone(&timeout);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001487 switch (status) {
1488 case NO_ERROR:
1489 case DEAD_OBJECT:
1490 case TIMED_OUT:
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001491 mCbf(EVENT_STREAM_END, mUserData, NULL);
Glenn Kastendcec9032013-09-20 09:28:56 -07001492 {
1493 AutoMutex lock(mLock);
1494 // The previously assigned value of waitStreamEnd is no longer valid,
1495 // since the mutex has been unlocked and either the callback handler
1496 // or another thread could have re-started the AudioTrack during that time.
1497 waitStreamEnd = mState == STATE_STOPPING;
1498 if (waitStreamEnd) {
1499 mState = STATE_STOPPED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001500 }
1501 }
Glenn Kastendcec9032013-09-20 09:28:56 -07001502 if (waitStreamEnd && status != DEAD_OBJECT) {
1503 return NS_INACTIVE;
1504 }
1505 break;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001506 }
Glenn Kastendcec9032013-09-20 09:28:56 -07001507 return 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001508 }
1509
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001510 // perform callbacks while unlocked
1511 if (newUnderrun) {
1512 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1513 }
1514 // FIXME we will miss loops if loop cycle was signaled several times since last call
1515 // to processAudioBuffer()
1516 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1517 mCbf(EVENT_LOOP_END, mUserData, NULL);
1518 }
1519 if (flags & CBLK_BUFFER_END) {
1520 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1521 }
1522 if (markerReached) {
1523 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1524 }
1525 while (newPosCount > 0) {
1526 size_t temp = newPosition;
1527 mCbf(EVENT_NEW_POS, mUserData, &temp);
1528 newPosition += updatePeriod;
1529 newPosCount--;
1530 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001531
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001532 if (mObservedSequence != sequence) {
1533 mObservedSequence = sequence;
1534 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001535 // for offloaded tracks, just wait for the upper layers to recreate the track
1536 if (isOffloaded()) {
1537 return NS_INACTIVE;
1538 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001539 }
1540
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001541 // if inactive, then don't run me again until re-started
1542 if (!active) {
1543 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001544 }
1545
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001546 // Compute the estimated time until the next timed event (position, markers, loops)
1547 // FIXME only for non-compressed audio
1548 uint32_t minFrames = ~0;
1549 if (!markerReached && position < markerPosition) {
1550 minFrames = markerPosition - position;
1551 }
1552 if (loopPeriod > 0 && loopPeriod < minFrames) {
1553 minFrames = loopPeriod;
1554 }
1555 if (updatePeriod > 0 && updatePeriod < minFrames) {
1556 minFrames = updatePeriod;
1557 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001558
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001559 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1560 static const uint32_t kPoll = 0;
1561 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1562 minFrames = kPoll * notificationFrames;
1563 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001564
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001565 // Convert frame units to time units
1566 nsecs_t ns = NS_WHENEVER;
1567 if (minFrames != (uint32_t) ~0) {
1568 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1569 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1570 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1571 }
1572
1573 // If not supplying data by EVENT_MORE_DATA, then we're done
1574 if (mTransfer != TRANSFER_CALLBACK) {
1575 return ns;
1576 }
1577
1578 struct timespec timeout;
1579 const struct timespec *requested = &ClientProxy::kForever;
1580 if (ns != NS_WHENEVER) {
1581 timeout.tv_sec = ns / 1000000000LL;
1582 timeout.tv_nsec = ns % 1000000000LL;
1583 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1584 requested = &timeout;
1585 }
1586
1587 while (mRemainingFrames > 0) {
1588
1589 Buffer audioBuffer;
1590 audioBuffer.frameCount = mRemainingFrames;
1591 size_t nonContig;
1592 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1593 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1594 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1595 requested = &ClientProxy::kNonBlocking;
1596 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001597 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1598 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001599 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001600 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1601 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001602 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001604 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1605 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001607
Eric Laurent42a6f422013-08-29 14:35:05 -07001608 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001609 mRetryOnPartialBuffer = false;
1610 if (avail < mRemainingFrames) {
1611 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1612 if (ns < 0 || myns < ns) {
1613 ns = myns;
1614 }
1615 return ns;
1616 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001617 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001618
1619 // Divide buffer size by 2 to take into account the expansion
1620 // due to 8 to 16 bit conversion: the callback must fill only half
1621 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001622 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001623 audioBuffer.size >>= 1;
1624 }
1625
1626 size_t reqSize = audioBuffer.size;
1627 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001628 size_t writtenSize = audioBuffer.size;
1629 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630
1631 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001632 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1633 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1634 reqSize, (int) writtenSize);
1635 return NS_NEVER;
1636 }
1637
1638 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001639 // The callback is done filling buffers
1640 // Keep this thread going to handle timed events and
1641 // still try to get more data in intervals of WAIT_PERIOD_MS
1642 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001643 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001644 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001646 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001647 // 8 to 16 bit conversion, note that source and destination are the same address
1648 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001649 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001650 }
1651
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001652 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1653 audioBuffer.frameCount = releasedFrames;
1654 mRemainingFrames -= releasedFrames;
1655 if (misalignment >= releasedFrames) {
1656 misalignment -= releasedFrames;
1657 } else {
1658 misalignment = 0;
1659 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001660
1661 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001662
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001663 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1664 // if callback doesn't like to accept the full chunk
1665 if (writtenSize < reqSize) {
1666 continue;
1667 }
1668
1669 // There could be enough non-contiguous frames available to satisfy the remaining request
1670 if (mRemainingFrames <= nonContig) {
1671 continue;
1672 }
1673
1674#if 0
1675 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1676 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1677 // that total to a sum == notificationFrames.
1678 if (0 < misalignment && misalignment <= mRemainingFrames) {
1679 mRemainingFrames = misalignment;
1680 return (mRemainingFrames * 1100000000LL) / sampleRate;
1681 }
1682#endif
1683
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001684 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001685 mRemainingFrames = notificationFrames;
1686 mRetryOnPartialBuffer = true;
1687
1688 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1689 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001690}
1691
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001692status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001693{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001694 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
1695 isOffloaded() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001696 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001697 status_t result;
1698
Glenn Kastena47f3162012-11-07 10:13:08 -08001699 // refresh the audio configuration cache in this process to make sure we get new
1700 // output parameters in getOutput_l() and createTrack_l()
1701 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001702
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001703 if (isOffloaded()) {
1704 return DEAD_OBJECT;
1705 }
1706
1707 // force new output query from audio policy manager;
1708 mOutput = 0;
1709 audio_io_handle_t output = getOutput_l();
1710
Glenn Kastena47f3162012-11-07 10:13:08 -08001711 // if the new IAudioTrack is created, createTrack_l() will modify the
1712 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1713 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001714
1715 // take the frames that will be lost by track recreation into account in saved position
1716 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001717 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001718 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001719 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001720 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001721 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001722 mFlags,
1723 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001724 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001725 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001726
Glenn Kastena47f3162012-11-07 10:13:08 -08001727 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001728 // continue playback from last known position, but
1729 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1730 if (mStaticProxy != NULL) {
1731 mLoopPeriod = 0;
1732 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1733 }
1734 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1735 // track destruction have been played? This is critical for SoundPool implementation
1736 // This must be broken, and needs to be tested/debugged.
1737#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001738 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001739 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001740 // Make sure that a client relying on callback events indicating underrun or
1741 // the actual amount of audio frames played (e.g SoundPool) receives them.
1742 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001743 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001744 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001745 }
1746 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001747#endif
1748 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001749 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001750 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001751 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001752 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001753 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1754 // As getOutput was called above and resulted in an output stream to be opened,
1755 // we need to release it.
1756 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001757 ALOGW("restoreTrack_l() failed status %d", result);
1758 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001759 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001760
1761 return result;
1762}
1763
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001764status_t AudioTrack::setParameters(const String8& keyValuePairs)
1765{
1766 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001767 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001768}
1769
Glenn Kastence703742013-07-19 16:33:58 -07001770status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1771{
Glenn Kasten53cec222013-08-29 09:01:02 -07001772 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001773 // FIXME not implemented for fast tracks; should use proxy and SSQ
1774 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1775 return INVALID_OPERATION;
1776 }
1777 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1778 return INVALID_OPERATION;
1779 }
1780 status_t status = mAudioTrack->getTimestamp(timestamp);
1781 if (status == NO_ERROR) {
1782 timestamp.mPosition += mProxy->getEpoch();
1783 }
1784 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001785}
1786
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001787String8 AudioTrack::getParameters(const String8& keys)
1788{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001789 if (mOutput) {
1790 return AudioSystem::getParameters(mOutput, keys);
1791 } else {
1792 return String8::empty();
1793 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001794}
1795
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001796status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1797{
1798
1799 const size_t SIZE = 256;
1800 char buffer[SIZE];
1801 String8 result;
1802
1803 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001804 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1805 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806 result.append(buffer);
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001807 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%zu)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001808 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001810 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001812 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001813 result.append(buffer);
1814 ::write(fd, result.string(), result.size());
1815 return NO_ERROR;
1816}
1817
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001818uint32_t AudioTrack::getUnderrunFrames() const
1819{
1820 AutoMutex lock(mLock);
1821 return mProxy->getUnderrunFrames();
1822}
1823
1824// =========================================================================
1825
1826void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1827{
1828 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1829 if (audioTrack != 0) {
1830 AutoMutex lock(audioTrack->mLock);
1831 audioTrack->mProxy->binderDied();
1832 }
1833}
1834
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835// =========================================================================
1836
1837AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001838 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1839 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001840{
1841}
1842
1843AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001844{
1845}
1846
1847bool AudioTrack::AudioTrackThread::threadLoop()
1848{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001849 {
1850 AutoMutex _l(mMyLock);
1851 if (mPaused) {
1852 mMyCond.wait(mMyLock);
1853 // caller will check for exitPending()
1854 return true;
1855 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001856 if (mIgnoreNextPausedInt) {
1857 mIgnoreNextPausedInt = false;
1858 mPausedInt = false;
1859 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001860 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001861 if (mPausedNs > 0) {
1862 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1863 } else {
1864 mMyCond.wait(mMyLock);
1865 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001866 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001867 return true;
1868 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001869 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001870 nsecs_t ns = mReceiver.processAudioBuffer(this);
1871 switch (ns) {
1872 case 0:
1873 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001874 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001875 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001876 return true;
1877 case NS_NEVER:
1878 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001879 case NS_WHENEVER:
1880 // FIXME increase poll interval, or make event-driven
1881 ns = 1000000000LL;
1882 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001883 default:
1884 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001885 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001886 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001887 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001888}
1889
Glenn Kasten3acbd052012-02-28 10:39:56 -08001890void AudioTrack::AudioTrackThread::requestExit()
1891{
1892 // must be in this order to avoid a race condition
1893 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001894 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001895}
1896
1897void AudioTrack::AudioTrackThread::pause()
1898{
1899 AutoMutex _l(mMyLock);
1900 mPaused = true;
1901}
1902
1903void AudioTrack::AudioTrackThread::resume()
1904{
1905 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001906 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001907 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001908 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001909 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001910 mMyCond.signal();
1911 }
1912}
1913
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001914void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1915{
1916 AutoMutex _l(mMyLock);
1917 mPausedInt = true;
1918 mPausedNs = ns;
1919}
1920
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001921}; // namespace android