blob: eeaab44a16938e6421654132aeef564e495b9dd3 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080023#include <audio_utils/primitives.h>
24#include <binder/IPCThreadState.h>
25#include <media/AudioTrack.h>
26#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010030#define WAIT_PERIOD_MS 10
31#define WAIT_STREAM_END_TIMEOUT_SEC 120
32
Glenn Kasten511754b2012-01-11 09:52:19 -080033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080035// ---------------------------------------------------------------------------
36
37// static
38status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080039 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080040 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080041 uint32_t sampleRate)
42{
Glenn Kastend65d73c2012-06-22 17:21:07 -070043 if (frameCount == NULL) {
44 return BAD_VALUE;
45 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070046
Glenn Kastene0fa4672012-04-24 14:35:14 -070047 // FIXME merge with similar code in createTrack_l(), except we're missing
48 // some information here that is available in createTrack_l():
49 // audio_io_handle_t output
50 // audio_format_t format
51 // audio_channel_mask_t channelMask
52 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080053 uint32_t afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080054 status_t status;
55 status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
56 if (status != NO_ERROR) {
57 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080058 }
Glenn Kastene33054e2012-11-14 12:54:39 -080059 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080060 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
61 if (status != NO_ERROR) {
62 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080063 }
64 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080065 status = AudioSystem::getOutputLatency(&afLatency, streamType);
66 if (status != NO_ERROR) {
67 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080068 }
69
70 // Ensure that buffer depth covers at least audio hardware latency
71 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080072 if (minBufCount < 2) {
73 minBufCount = 2;
74 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080075
76 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070077 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080078 // The formula above should always produce a non-zero value, but return an error
79 // in the unlikely event that it does not, as that's part of the API contract.
80 if (*frameCount == 0) {
81 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d",
82 streamType, sampleRate);
83 return BAD_VALUE;
84 }
Glenn Kasten3acbd052012-02-28 10:39:56 -080085 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
86 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080087 return NO_ERROR;
88}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089
90// ---------------------------------------------------------------------------
91
92AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070093 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080094 mIsTimed(false),
95 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -080096 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097{
98}
99
100AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800101 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800103 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700104 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700106 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 callback_t cbf,
108 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700109 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800110 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000111 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800112 const audio_offload_info_t *offloadInfo,
113 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700114 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800115 mIsTimed(false),
116 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800117 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700119 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700120 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800121 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
122 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123}
124
Andreas Huberc8139852012-01-18 10:51:55 -0800125AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800126 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800128 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700129 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700131 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 callback_t cbf,
133 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700134 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800135 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000136 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800137 const audio_offload_info_t *offloadInfo,
138 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700139 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800140 mIsTimed(false),
141 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800142 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700144 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800145 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800146 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147}
148
149AudioTrack::~AudioTrack()
150{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 if (mStatus == NO_ERROR) {
152 // Make sure that callback function exits in the case where
153 // it is looping on buffer full condition in obtainBuffer().
154 // Otherwise the callback thread will never exit.
155 stop();
156 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100157 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800158 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 mAudioTrackThread->requestExitAndWait();
160 mAudioTrackThread.clear();
161 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700162 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
163 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700165 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 }
167}
168
169status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800170 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800172 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700173 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800174 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700175 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 callback_t cbf,
177 void* user,
178 int notificationFrames,
179 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700180 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800181 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000182 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800183 const audio_offload_info_t *offloadInfo,
184 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800186 switch (transferType) {
187 case TRANSFER_DEFAULT:
188 if (sharedBuffer != 0) {
189 transferType = TRANSFER_SHARED;
190 } else if (cbf == NULL || threadCanCallJava) {
191 transferType = TRANSFER_SYNC;
192 } else {
193 transferType = TRANSFER_CALLBACK;
194 }
195 break;
196 case TRANSFER_CALLBACK:
197 if (cbf == NULL || sharedBuffer != 0) {
198 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
199 return BAD_VALUE;
200 }
201 break;
202 case TRANSFER_OBTAIN:
203 case TRANSFER_SYNC:
204 if (sharedBuffer != 0) {
205 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
206 return BAD_VALUE;
207 }
208 break;
209 case TRANSFER_SHARED:
210 if (sharedBuffer == 0) {
211 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
212 return BAD_VALUE;
213 }
214 break;
215 default:
216 ALOGE("Invalid transfer type %d", transferType);
217 return BAD_VALUE;
218 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800219 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800220 mTransfer = transferType;
221
Glenn Kastene33054e2012-11-14 12:54:39 -0800222 // FIXME "int" here is legacy and will be replaced by size_t later
223 if (frameCountInt < 0) {
224 ALOGE("Invalid frame count %d", frameCountInt);
225 return BAD_VALUE;
226 }
227 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700229 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
230 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231
Glenn Kastene33054e2012-11-14 12:54:39 -0800232 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700233
Eric Laurent1703cdf2011-03-07 14:52:59 -0800234 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800235
Glenn Kasten53cec222013-08-29 09:01:02 -0700236 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700237 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000238 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800239 return INVALID_OPERATION;
240 }
241
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100242 mOutput = 0;
243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 if (streamType == AUDIO_STREAM_DEFAULT) {
246 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800248 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
249 ALOGE("Invalid stream type %d", streamType);
250 return BAD_VALUE;
251 }
252 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700253
Glenn Kastenb1bef512014-01-13 10:25:53 -0800254 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800256 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
257 if (status != NO_ERROR) {
258 ALOGE("Could not get output sample rate for stream type %d; status %d",
259 streamType, status);
260 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700261 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800263 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800266 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269
270 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700271 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800272 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 return BAD_VALUE;
274 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800275 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700276
Glenn Kasten8ba90322013-10-30 11:29:27 -0700277 if (!audio_is_output_channel(channelMask)) {
278 ALOGE("Invalid channel mask %#x", channelMask);
279 return BAD_VALUE;
280 }
281
Glenn Kastene0fa4672012-04-24 14:35:14 -0700282 // AudioFlinger does not currently support 8-bit data in shared memory
283 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
284 ALOGE("8-bit data in shared memory is not supported");
285 return BAD_VALUE;
286 }
287
Eric Laurentc2f1f072009-07-17 12:17:14 -0700288 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100289 // or offload was requested
290 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
291 || !audio_is_linear_pcm(format)) {
292 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
293 ? "Offload request, forcing to Direct Output"
294 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700295 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800296 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700297 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700298 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700299 // only allow deep buffering for music stream type
300 if (streamType != AUDIO_STREAM_MUSIC) {
301 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
302 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303
Glenn Kastena42ff002012-11-14 12:47:55 -0800304 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700305 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800306 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307
Glenn Kastene3aa6592012-12-04 12:22:46 -0800308 if (audio_is_linear_pcm(format)) {
309 mFrameSize = channelCount * audio_bytes_per_sample(format);
310 mFrameSizeAF = channelCount * sizeof(int16_t);
311 } else {
312 mFrameSize = sizeof(uint8_t);
313 mFrameSizeAF = sizeof(uint8_t);
314 }
315
Dima Zavinfce7a472011-04-19 22:30:36 -0700316 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800317 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800318 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000319 flags,
320 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700321
322 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000323 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324 return BAD_VALUE;
325 }
326
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800327 mVolume[LEFT] = 1.0f;
328 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800329 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800330 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800331 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700332 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800333 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700334 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800335 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
336 mClientUid = IPCThreadState::self()->getCallingUid();
337 } else {
338 mClientUid = uid;
339 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700340 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700341 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700342 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700343
Glenn Kastena997e7a2012-08-07 09:44:19 -0700344 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700345 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700346 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
347 }
348
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800349 // create the IAudioTrack
Glenn Kastenb1bef512014-01-13 10:25:53 -0800350 status = createTrack_l(streamType,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800351 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800352 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800353 frameCount,
354 flags,
355 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800356 output,
357 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800358
Glenn Kastena997e7a2012-08-07 09:44:19 -0700359 if (status != NO_ERROR) {
360 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100361 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
362 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700363 mAudioTrackThread.clear();
364 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100365 //Use of direct and offloaded output streams is ref counted by audio policy manager.
366 // As getOutput was called above and resulted in an output stream to be opened,
367 // we need to release it.
368 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700369 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700370 }
371
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800373 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800375 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700377 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378 mNewPosition = 0;
379 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700380 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800381 mSequence = 1;
382 mObservedSequence = mSequence;
383 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100384 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800385
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 return NO_ERROR;
387}
388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389// -------------------------------------------------------------------------
390
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100391status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800393 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100394
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800395 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100396 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 }
398
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800399 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800401 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100402 if (previousState == STATE_PAUSED_STOPPING) {
403 mState = STATE_STOPPING;
404 } else {
405 mState = STATE_ACTIVE;
406 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800407 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
408 // reset current position as seen by client to 0
409 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700410 // force refresh of remaining frames by processAudioBuffer() as last
411 // write before stop could be partial.
412 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800413 }
414 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700415 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800416
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800418 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100419 if (previousState == STATE_STOPPING) {
420 mProxy->interrupt();
421 } else {
422 t->resume();
423 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800424 } else {
425 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
426 get_sched_policy(0, &mPreviousSchedulingGroup);
427 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
428 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800430 status_t status = NO_ERROR;
431 if (!(flags & CBLK_INVALID)) {
432 status = mAudioTrack->start();
433 if (status == DEAD_OBJECT) {
434 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800436 }
437 if (flags & CBLK_INVALID) {
438 status = restoreTrack_l("start");
439 }
440
441 if (status != NO_ERROR) {
442 ALOGE("start() status %d", status);
443 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100445 if (previousState != STATE_STOPPING) {
446 t->pause();
447 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700449 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700450 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 }
452 }
453
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100454 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800455}
456
457void AudioTrack::stop()
458{
459 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700460 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800461 return;
462 }
463
Glenn Kasten23a75452014-01-13 10:37:17 -0800464 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100465 mState = STATE_STOPPING;
466 } else {
467 mState = STATE_STOPPED;
468 }
469
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800470 mProxy->interrupt();
471 mAudioTrack->stop();
472 // the playback head position will reset to 0, so if a marker is set, we need
473 // to activate it again
474 mMarkerReached = false;
475#if 0
476 // Force flush if a shared buffer is used otherwise audioflinger
477 // will not stop before end of buffer is reached.
478 // It may be needed to make sure that we stop playback, likely in case looping is on.
479 if (mSharedBuffer != 0) {
480 flush_l();
481 }
482#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100483
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800484 sp<AudioTrackThread> t = mAudioTrackThread;
485 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800486 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100487 t->pause();
488 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800489 } else {
490 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
491 set_sched_policy(0, mPreviousSchedulingGroup);
492 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493}
494
495bool AudioTrack::stopped() const
496{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800497 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800498 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499}
500
501void AudioTrack::flush()
502{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800503 if (mSharedBuffer != 0) {
504 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800505 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800506 AutoMutex lock(mLock);
507 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
508 return;
509 }
510 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800511}
512
Eric Laurent1703cdf2011-03-07 14:52:59 -0800513void AudioTrack::flush_l()
514{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800515 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700516
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700517 // clear playback marker and periodic update counter
518 mMarkerPosition = 0;
519 mMarkerReached = false;
520 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100521 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700522
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800523 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800524 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100525 mProxy->interrupt();
526 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800528 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529}
530
531void AudioTrack::pause()
532{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800533 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100534 if (mState == STATE_ACTIVE) {
535 mState = STATE_PAUSED;
536 } else if (mState == STATE_STOPPING) {
537 mState = STATE_PAUSED_STOPPING;
538 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800539 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800541 mProxy->interrupt();
542 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543}
544
Eric Laurentbe916aa2010-06-01 23:49:17 -0700545status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800547 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700548 return BAD_VALUE;
549 }
550
Eric Laurent1703cdf2011-03-07 14:52:59 -0800551 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 mVolume[LEFT] = left;
553 mVolume[RIGHT] = right;
554
Glenn Kastene3aa6592012-12-04 12:22:46 -0800555 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700556
Glenn Kasten23a75452014-01-13 10:37:17 -0800557 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700558 mAudioTrack->signal();
559 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700560 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561}
562
Glenn Kastenb1c09932012-02-27 16:21:04 -0800563status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800565 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700566}
567
Eric Laurent2beeb502010-07-16 07:43:46 -0700568status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700569{
Glenn Kasten05632a52012-01-03 14:22:33 -0800570 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571 return BAD_VALUE;
572 }
573
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800574 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700575 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800576 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700577
578 return NO_ERROR;
579}
580
Glenn Kastena5224f32012-01-04 12:41:44 -0800581void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700582{
583 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800584 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700585 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586}
587
Glenn Kasten3b16c762012-11-14 08:44:39 -0800588status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100590 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800591 return INVALID_OPERATION;
592 }
593
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800594 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700596 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 }
598 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700599 if (rate == 0 || rate > afSamplingRate*2 ) {
600 return BAD_VALUE;
601 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602
Eric Laurent1703cdf2011-03-07 14:52:59 -0800603 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800604 mSampleRate = rate;
605 mProxy->setSampleRate(rate);
606
Eric Laurent57326622009-07-07 07:10:45 -0700607 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608}
609
Glenn Kastena5224f32012-01-04 12:41:44 -0800610uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611{
John Grossman4ff14ba2012-02-08 16:37:41 -0800612 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800613 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800614 }
615
Eric Laurent1703cdf2011-03-07 14:52:59 -0800616 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700617
618 // sample rate can be updated during playback by the offloaded decoder so we need to
619 // query the HAL and update if needed.
620// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800621 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700622 if (mOutput != 0) {
623 uint32_t sampleRate = 0;
624 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
625 if (status == NO_ERROR) {
626 mSampleRate = sampleRate;
627 }
628 }
629 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800630 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631}
632
633status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
634{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100635 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800636 return INVALID_OPERATION;
637 }
638
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800640 ;
641 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
642 loopEnd - loopStart >= MIN_LOOP) {
643 ;
644 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 return BAD_VALUE;
646 }
647
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800648 AutoMutex lock(mLock);
649 // See setPosition() regarding setting parameters such as loop points or position while active
650 if (mState == STATE_ACTIVE) {
651 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700652 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800653 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654 return NO_ERROR;
655}
656
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800657void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
658{
659 // FIXME If setting a loop also sets position to start of loop, then
660 // this is correct. Otherwise it should be removed.
661 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
662 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
663 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
664}
665
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666status_t AudioTrack::setMarkerPosition(uint32_t marker)
667{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700668 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100669 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700670 return INVALID_OPERATION;
671 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800673 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700675 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676
677 return NO_ERROR;
678}
679
Glenn Kastena5224f32012-01-04 12:41:44 -0800680status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100682 if (isOffloaded()) {
683 return INVALID_OPERATION;
684 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700685 if (marker == NULL) {
686 return BAD_VALUE;
687 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800689 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690 *marker = mMarkerPosition;
691
692 return NO_ERROR;
693}
694
695status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
696{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700697 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100698 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700699 return INVALID_OPERATION;
700 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800702 AutoMutex lock(mLock);
703 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705 return NO_ERROR;
706}
707
Glenn Kastena5224f32012-01-04 12:41:44 -0800708status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800709{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100710 if (isOffloaded()) {
711 return INVALID_OPERATION;
712 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700713 if (updatePeriod == NULL) {
714 return BAD_VALUE;
715 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800717 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718 *updatePeriod = mUpdatePeriod;
719
720 return NO_ERROR;
721}
722
723status_t AudioTrack::setPosition(uint32_t position)
724{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100725 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700726 return INVALID_OPERATION;
727 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800728 if (position > mFrameCount) {
729 return BAD_VALUE;
730 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800731
Eric Laurent1703cdf2011-03-07 14:52:59 -0800732 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800733 // Currently we require that the player is inactive before setting parameters such as position
734 // or loop points. Otherwise, there could be a race condition: the application could read the
735 // current position, compute a new position or loop parameters, and then set that position or
736 // loop parameters but it would do the "wrong" thing since the position has continued to advance
737 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
738 // to specify how it wants to handle such scenarios.
739 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700740 return INVALID_OPERATION;
741 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800742 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
743 mLoopPeriod = 0;
744 // FIXME Check whether loops and setting position are incompatible in old code.
745 // If we use setLoop for both purposes we lose the capability to set the position while looping.
746 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700747
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800748 return NO_ERROR;
749}
750
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800751status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800752{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700753 if (position == NULL) {
754 return BAD_VALUE;
755 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800756
Eric Laurent1703cdf2011-03-07 14:52:59 -0800757 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800758 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100759 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100761 if (mOutput != 0) {
762 uint32_t halFrames;
763 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
764 }
765 *position = dspFrames;
766 } else {
767 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
768 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
769 mProxy->getPosition();
770 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771 return NO_ERROR;
772}
773
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800774status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800775{
776 if (mSharedBuffer == 0 || mIsTimed) {
777 return INVALID_OPERATION;
778 }
779 if (position == NULL) {
780 return BAD_VALUE;
781 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800782
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800783 AutoMutex lock(mLock);
784 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800785 return NO_ERROR;
786}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800787
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800788status_t AudioTrack::reload()
789{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100790 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800791 return INVALID_OPERATION;
792 }
793
Eric Laurent1703cdf2011-03-07 14:52:59 -0800794 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800795 // See setPosition() regarding setting parameters such as loop points or position while active
796 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700797 return INVALID_OPERATION;
798 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800799 mNewPosition = mUpdatePeriod;
800 mLoopPeriod = 0;
801 // FIXME The new code cannot reload while keeping a loop specified.
802 // Need to check how the old code handled this, and whether it's a significant change.
803 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800804 return NO_ERROR;
805}
806
Eric Laurentc2f1f072009-07-17 12:17:14 -0700807audio_io_handle_t AudioTrack::getOutput()
808{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800809 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100810 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800811}
812
813// must be called with mLock held
814audio_io_handle_t AudioTrack::getOutput_l()
815{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100816 if (mOutput) {
817 return mOutput;
818 } else {
819 return AudioSystem::getOutput(mStreamType,
820 mSampleRate, mFormat, mChannelMask, mFlags);
821 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700822}
823
Eric Laurentbe916aa2010-06-01 23:49:17 -0700824status_t AudioTrack::attachAuxEffect(int effectId)
825{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800826 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700827 status_t status = mAudioTrack->attachAuxEffect(effectId);
828 if (status == NO_ERROR) {
829 mAuxEffectId = effectId;
830 }
831 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700832}
833
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800834// -------------------------------------------------------------------------
835
Eric Laurent1703cdf2011-03-07 14:52:59 -0800836// must be called with mLock held
837status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800838 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800839 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800840 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800841 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700842 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800843 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800844 audio_io_handle_t output,
845 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800846{
847 status_t status;
848 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
849 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700850 ALOGE("Could not get audioflinger");
851 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800852 }
853
Glenn Kastence8828a2013-09-16 18:07:38 -0700854 // Not all of these values are needed under all conditions, but it is easier to get them all
855
Eric Laurentd1b449a2010-05-14 03:26:45 -0700856 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700857 status = AudioSystem::getLatency(output, streamType, &afLatency);
858 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800859 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700860 return NO_INIT;
861 }
862
Glenn Kastence8828a2013-09-16 18:07:38 -0700863 size_t afFrameCount;
864 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
865 if (status != NO_ERROR) {
866 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
867 return NO_INIT;
868 }
869
870 uint32_t afSampleRate;
871 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
872 if (status != NO_ERROR) {
873 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
874 return NO_INIT;
875 }
876
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700877 // Client decides whether the track is TIMED (see below), but can only express a preference
878 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700879 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700880 // either of these use cases:
881 // use case 1: shared buffer
882 (sharedBuffer != 0) ||
883 // use case 2: callback handler
884 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800885 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700886 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700887 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700888 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700889 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700890 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700891
Glenn Kastence8828a2013-09-16 18:07:38 -0700892 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800893 // n = 1 fast track with single buffering; nBuffering is ignored
894 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700895 // n = 2 normal track, no sample rate conversion
896 // n = 3 normal track, with sample rate conversion
897 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
898 // n > 3 very high latency or very small notification interval; nBuffering is ignored
899 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
900
Eric Laurentd1b449a2010-05-14 03:26:45 -0700901 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700902
Dima Zavinfce7a472011-04-19 22:30:36 -0700903 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700904
Eric Laurentd1b449a2010-05-14 03:26:45 -0700905 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700906 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700907 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700908 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700909 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700910 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100911 if (mNotificationFramesAct != frameCount) {
912 mNotificationFramesAct = frameCount;
913 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700914 } else if (sharedBuffer != 0) {
915
Glenn Kastena42ff002012-11-14 12:47:55 -0800916 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700917 // 8-bit data in shared memory is not currently supported by AudioFlinger
918 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800919 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920 // More than 2 channels does not require stronger alignment than stereo
921 alignment <<= 1;
922 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800923 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
924 ALOGE("Invalid buffer alignment: address %p, channel count %u",
925 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700926 return BAD_VALUE;
927 }
928
929 // When initializing a shared buffer AudioTrack via constructors,
930 // there's no frameCount parameter.
931 // But when initializing a shared buffer AudioTrack via set(),
932 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800933 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934
935 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
936
937 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700938
Eric Laurentd1b449a2010-05-14 03:26:45 -0700939 // Ensure that buffer depth covers at least audio hardware latency
940 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700941 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
942 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700943 if (minBufCount <= nBuffering) {
944 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800945 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700946
Glenn Kastene33054e2012-11-14 12:54:39 -0800947 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
948 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800949 ", afLatency=%d",
950 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700951
952 if (frameCount == 0) {
953 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700954 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700955 // not ALOGW because it happens all the time when playing key clicks over A2DP
956 ALOGV("Minimum buffer size corrected from %d to %d",
957 frameCount, minFrameCount);
958 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800959 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700960 // Make sure that application is notified with sufficient margin before underrun
961 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
962 mNotificationFramesAct = frameCount/nBuffering;
963 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700964
Glenn Kastene0fa4672012-04-24 14:35:14 -0700965 } else {
966 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700967 }
968
Glenn Kastena075db42012-03-06 11:22:44 -0800969 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
970 if (mIsTimed) {
971 trackFlags |= IAudioFlinger::TRACK_TIMED;
972 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800973
974 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700975 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700976 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800977 if (mAudioTrackThread != 0) {
978 tid = mAudioTrackThread->getTid();
979 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700980 }
981
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100982 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
983 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
984 }
985
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800986 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800987 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700988 // AudioFlinger only sees 16-bit PCM
989 format == AUDIO_FORMAT_PCM_8_BIT ?
990 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800991 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800992 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800993 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800994 sharedBuffer,
995 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800996 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700997 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700998 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800999 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001000 &status);
1001
1002 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001003 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 return status;
1005 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001006 sp<IMemory> iMem = track->getCblk();
1007 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001008 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001009 return NO_INIT;
1010 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001011 void *iMemPointer = iMem->pointer();
1012 if (iMemPointer == NULL) {
1013 ALOGE("Could not get control block pointer");
1014 return NO_INIT;
1015 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001016 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001017 if (mAudioTrack != 0) {
1018 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1019 mDeathNotifier.clear();
1020 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001021 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001022 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001023 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001024 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001025 size_t temp = cblk->frameCount_;
1026 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1027 // In current design, AudioTrack client checks and ensures frame count validity before
1028 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1029 // for fast track as it uses a special method of assigning frame count.
1030 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1031 }
1032 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001033 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001034 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001035 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001036 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001037 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001038 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001039 // Theoretically double-buffering is not required for fast tracks,
1040 // due to tighter scheduling. But in practice, to accommodate kernels with
1041 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1042 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1043 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001044 }
1045 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001046 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001047 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001048 // once denied, do not request again if IAudioTrack is re-created
1049 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1050 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001051 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001052 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1053 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001054 }
1055 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001056 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001057 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001058 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1059 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1060 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1061 } else {
1062 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1063 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1064 mFlags = flags;
1065 return NO_INIT;
1066 }
1067 }
1068
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001069 mRefreshRemaining = true;
1070
1071 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1072 // is the value of pointer() for the shared buffer, otherwise buffers points
1073 // immediately after the control block. This address is for the mapping within client
1074 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1075 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001076 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001077 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001078 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001079 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001080 }
1081
Eric Laurent2beeb502010-07-16 07:43:46 -07001082 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001083 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001084 mLatency = afLatency + (1000*frameCount) / sampleRate;
1085 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001086 // If IAudioTrack is re-created, don't let the requested frameCount
1087 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001088 if (frameCount > mReqFrameCount) {
1089 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001090 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001091
1092 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001093 if (sharedBuffer == 0) {
1094 mStaticProxy.clear();
1095 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1096 } else {
1097 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1098 mProxy = mStaticProxy;
1099 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001100 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1101 uint16_t(mVolume[LEFT] * 0x1000));
1102 mProxy->setSendLevel(mSendLevel);
1103 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001104 mProxy->setEpoch(epoch);
1105 mProxy->setMinimum(mNotificationFramesAct);
1106
1107 mDeathNotifier = new DeathNotifier(this);
1108 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001109
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001110 return NO_ERROR;
1111}
1112
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001113status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1114{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001115 if (audioBuffer == NULL) {
1116 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001117 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001118 if (mTransfer != TRANSFER_OBTAIN) {
1119 audioBuffer->frameCount = 0;
1120 audioBuffer->size = 0;
1121 audioBuffer->raw = NULL;
1122 return INVALID_OPERATION;
1123 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001124
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001125 const struct timespec *requested;
1126 if (waitCount == -1) {
1127 requested = &ClientProxy::kForever;
1128 } else if (waitCount == 0) {
1129 requested = &ClientProxy::kNonBlocking;
1130 } else if (waitCount > 0) {
1131 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1132 struct timespec timeout;
1133 timeout.tv_sec = ms / 1000;
1134 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1135 requested = &timeout;
1136 } else {
1137 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1138 requested = NULL;
1139 }
1140 return obtainBuffer(audioBuffer, requested);
1141}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001142
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001143status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1144 struct timespec *elapsed, size_t *nonContig)
1145{
1146 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1147 uint32_t oldSequence = 0;
1148 uint32_t newSequence;
1149
1150 Proxy::Buffer buffer;
1151 status_t status = NO_ERROR;
1152
1153 static const int32_t kMaxTries = 5;
1154 int32_t tryCounter = kMaxTries;
1155
1156 do {
1157 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1158 // keep them from going away if another thread re-creates the track during obtainBuffer()
1159 sp<AudioTrackClientProxy> proxy;
1160 sp<IMemory> iMem;
1161
1162 { // start of lock scope
1163 AutoMutex lock(mLock);
1164
1165 newSequence = mSequence;
1166 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1167 if (status == DEAD_OBJECT) {
1168 // re-create track, unless someone else has already done so
1169 if (newSequence == oldSequence) {
1170 status = restoreTrack_l("obtainBuffer");
1171 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001172 buffer.mFrameCount = 0;
1173 buffer.mRaw = NULL;
1174 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001175 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001176 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001177 }
1178 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001179 oldSequence = newSequence;
1180
1181 // Keep the extra references
1182 proxy = mProxy;
1183 iMem = mCblkMemory;
1184
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001185 if (mState == STATE_STOPPING) {
1186 status = -EINTR;
1187 buffer.mFrameCount = 0;
1188 buffer.mRaw = NULL;
1189 buffer.mNonContig = 0;
1190 break;
1191 }
1192
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001193 // Non-blocking if track is stopped or paused
1194 if (mState != STATE_ACTIVE) {
1195 requested = &ClientProxy::kNonBlocking;
1196 }
1197
1198 } // end of lock scope
1199
1200 buffer.mFrameCount = audioBuffer->frameCount;
1201 // FIXME starts the requested timeout and elapsed over from scratch
1202 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1203
1204 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1205
1206 audioBuffer->frameCount = buffer.mFrameCount;
1207 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1208 audioBuffer->raw = buffer.mRaw;
1209 if (nonContig != NULL) {
1210 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001211 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001212 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213}
1214
1215void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1216{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001217 if (mTransfer == TRANSFER_SHARED) {
1218 return;
1219 }
1220
1221 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1222 if (stepCount == 0) {
1223 return;
1224 }
1225
1226 Proxy::Buffer buffer;
1227 buffer.mFrameCount = stepCount;
1228 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001229
Eric Laurent1703cdf2011-03-07 14:52:59 -08001230 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001231 mInUnderrun = false;
1232 mProxy->releaseBuffer(&buffer);
1233
1234 // restart track if it was disabled by audioflinger due to previous underrun
1235 if (mState == STATE_ACTIVE) {
1236 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001237 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001238 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1239 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001240 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001241 mAudioTrack->start();
1242 }
1243 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001244}
1245
1246// -------------------------------------------------------------------------
1247
1248ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1249{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001250 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001251 return INVALID_OPERATION;
1252 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001254 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001255 // Sanity-check: user is most-likely passing an error code, and it would
1256 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001257 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258 return BAD_VALUE;
1259 }
1260
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001261 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 Buffer audioBuffer;
1263
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001264 while (userSize >= mFrameSize) {
1265 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001266
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001269 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001271 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001272 return ssize_t(err);
1273 }
1274
1275 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001276 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001278 toWrite = audioBuffer.size >> 1;
1279 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001280 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001282 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001284 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285 userSize -= toWrite;
1286 written += toWrite;
1287
1288 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001289 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290
1291 return written;
1292}
1293
1294// -------------------------------------------------------------------------
1295
John Grossman4ff14ba2012-02-08 16:37:41 -08001296TimedAudioTrack::TimedAudioTrack() {
1297 mIsTimed = true;
1298}
1299
1300status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1301{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001302 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001303 status_t result = UNKNOWN_ERROR;
1304
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001305#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001306 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1307 // while we are accessing the cblk
1308 sp<IAudioTrack> audioTrack = mAudioTrack;
1309 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001310#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001311
John Grossman4ff14ba2012-02-08 16:37:41 -08001312 // If the track is not invalid already, try to allocate a buffer. alloc
1313 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001314 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001315 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001316 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001317 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1318 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001319 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001320 }
1321 }
1322
1323 // If the track is invalid at this point, attempt to restore it. and try the
1324 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001325 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001326 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001327
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001328 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001329 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001330 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001331 }
1332
1333 return result;
1334}
1335
1336status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1337 int64_t pts)
1338{
Eric Laurentdf839842012-05-31 14:27:14 -07001339 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1340 {
1341 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001342 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001343 // restart track if it was disabled by audioflinger due to previous underrun
1344 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001345 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1346 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001347 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001348 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001349 mAudioTrack->start();
1350 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001351 }
Eric Laurentdf839842012-05-31 14:27:14 -07001352 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001353}
1354
1355status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1356 TargetTimeline target)
1357{
1358 return mAudioTrack->setMediaTimeTransform(xform, target);
1359}
1360
1361// -------------------------------------------------------------------------
1362
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001363nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001365 // Currently the AudioTrack thread is not created if there are no callbacks.
1366 // Would it ever make sense to run the thread, even without callbacks?
1367 // If so, then replace this by checks at each use for mCbf != NULL.
1368 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1369
Eric Laurent1703cdf2011-03-07 14:52:59 -08001370 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001371 if (mAwaitBoost) {
1372 mAwaitBoost = false;
1373 mLock.unlock();
1374 static const int32_t kMaxTries = 5;
1375 int32_t tryCounter = kMaxTries;
1376 uint32_t pollUs = 10000;
1377 do {
1378 int policy = sched_getscheduler(0);
1379 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1380 break;
1381 }
1382 usleep(pollUs);
1383 pollUs <<= 1;
1384 } while (tryCounter-- > 0);
1385 if (tryCounter < 0) {
1386 ALOGE("did not receive expected priority boost on time");
1387 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001388 // Run again immediately
1389 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001390 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001391
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001392 // Can only reference mCblk while locked
1393 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001394 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001395
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001396 // Check for track invalidation
1397 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001398 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1399 // AudioSystem cache. We should not exit here but after calling the callback so
1400 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001401 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001402 status_t status = restoreTrack_l("processAudioBuffer");
1403 mLock.unlock();
1404 // Run again immediately, but with a new IAudioTrack
1405 return 0;
1406 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001407 }
1408
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001409 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001410 bool active = mState == STATE_ACTIVE;
1411
1412 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1413 bool newUnderrun = false;
1414 if (flags & CBLK_UNDERRUN) {
1415#if 0
1416 // Currently in shared buffer mode, when the server reaches the end of buffer,
1417 // the track stays active in continuous underrun state. It's up to the application
1418 // to pause or stop the track, or set the position to a new offset within buffer.
1419 // This was some experimental code to auto-pause on underrun. Keeping it here
1420 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1421 if (mTransfer == TRANSFER_SHARED) {
1422 mState = STATE_PAUSED;
1423 active = false;
1424 }
1425#endif
1426 if (!mInUnderrun) {
1427 mInUnderrun = true;
1428 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429 }
1430 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001431
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001432 // Get current position of server
1433 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001434
1435 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001436 bool markerReached = false;
1437 size_t markerPosition = mMarkerPosition;
1438 // FIXME fails for wraparound, need 64 bits
1439 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1440 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 }
1442
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001443 // Determine number of new position callback(s) that will be needed, while locked
1444 size_t newPosCount = 0;
1445 size_t newPosition = mNewPosition;
1446 size_t updatePeriod = mUpdatePeriod;
1447 // FIXME fails for wraparound, need 64 bits
1448 if (updatePeriod > 0 && position >= newPosition) {
1449 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1450 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001451 }
1452
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001453 // Cache other fields that will be needed soon
1454 uint32_t loopPeriod = mLoopPeriod;
1455 uint32_t sampleRate = mSampleRate;
1456 size_t notificationFrames = mNotificationFramesAct;
1457 if (mRefreshRemaining) {
1458 mRefreshRemaining = false;
1459 mRemainingFrames = notificationFrames;
1460 mRetryOnPartialBuffer = false;
1461 }
1462 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001463 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001464
1465 // These fields don't need to be cached, because they are assigned only by set():
1466 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1467 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1468
1469 mLock.unlock();
1470
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001471 if (waitStreamEnd) {
1472 AutoMutex lock(mLock);
1473
1474 sp<AudioTrackClientProxy> proxy = mProxy;
1475 sp<IMemory> iMem = mCblkMemory;
1476
1477 struct timespec timeout;
1478 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1479 timeout.tv_nsec = 0;
1480
1481 mLock.unlock();
1482 status_t status = mProxy->waitStreamEndDone(&timeout);
1483 mLock.lock();
1484 switch (status) {
1485 case NO_ERROR:
1486 case DEAD_OBJECT:
1487 case TIMED_OUT:
1488 mLock.unlock();
1489 mCbf(EVENT_STREAM_END, mUserData, NULL);
1490 mLock.lock();
1491 if (mState == STATE_STOPPING) {
1492 mState = STATE_STOPPED;
1493 if (status != DEAD_OBJECT) {
1494 return NS_INACTIVE;
1495 }
1496 }
1497 return 0;
1498 default:
1499 return 0;
1500 }
1501 }
1502
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001503 // perform callbacks while unlocked
1504 if (newUnderrun) {
1505 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1506 }
1507 // FIXME we will miss loops if loop cycle was signaled several times since last call
1508 // to processAudioBuffer()
1509 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1510 mCbf(EVENT_LOOP_END, mUserData, NULL);
1511 }
1512 if (flags & CBLK_BUFFER_END) {
1513 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1514 }
1515 if (markerReached) {
1516 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1517 }
1518 while (newPosCount > 0) {
1519 size_t temp = newPosition;
1520 mCbf(EVENT_NEW_POS, mUserData, &temp);
1521 newPosition += updatePeriod;
1522 newPosCount--;
1523 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001524
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001525 if (mObservedSequence != sequence) {
1526 mObservedSequence = sequence;
1527 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001528 // for offloaded tracks, just wait for the upper layers to recreate the track
1529 if (isOffloaded()) {
1530 return NS_INACTIVE;
1531 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001532 }
1533
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001534 // if inactive, then don't run me again until re-started
1535 if (!active) {
1536 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001537 }
1538
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001539 // Compute the estimated time until the next timed event (position, markers, loops)
1540 // FIXME only for non-compressed audio
1541 uint32_t minFrames = ~0;
1542 if (!markerReached && position < markerPosition) {
1543 minFrames = markerPosition - position;
1544 }
1545 if (loopPeriod > 0 && loopPeriod < minFrames) {
1546 minFrames = loopPeriod;
1547 }
1548 if (updatePeriod > 0 && updatePeriod < minFrames) {
1549 minFrames = updatePeriod;
1550 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001552 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1553 static const uint32_t kPoll = 0;
1554 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1555 minFrames = kPoll * notificationFrames;
1556 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001557
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001558 // Convert frame units to time units
1559 nsecs_t ns = NS_WHENEVER;
1560 if (minFrames != (uint32_t) ~0) {
1561 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1562 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1563 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1564 }
1565
1566 // If not supplying data by EVENT_MORE_DATA, then we're done
1567 if (mTransfer != TRANSFER_CALLBACK) {
1568 return ns;
1569 }
1570
1571 struct timespec timeout;
1572 const struct timespec *requested = &ClientProxy::kForever;
1573 if (ns != NS_WHENEVER) {
1574 timeout.tv_sec = ns / 1000000000LL;
1575 timeout.tv_nsec = ns % 1000000000LL;
1576 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1577 requested = &timeout;
1578 }
1579
1580 while (mRemainingFrames > 0) {
1581
1582 Buffer audioBuffer;
1583 audioBuffer.frameCount = mRemainingFrames;
1584 size_t nonContig;
1585 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1586 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1587 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1588 requested = &ClientProxy::kNonBlocking;
1589 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001590 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1591 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001592 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001593 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1594 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001595 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001597 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1598 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001600
Eric Laurent42a6f422013-08-29 14:35:05 -07001601 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001602 mRetryOnPartialBuffer = false;
1603 if (avail < mRemainingFrames) {
1604 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1605 if (ns < 0 || myns < ns) {
1606 ns = myns;
1607 }
1608 return ns;
1609 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001610 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001611
1612 // Divide buffer size by 2 to take into account the expansion
1613 // due to 8 to 16 bit conversion: the callback must fill only half
1614 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001615 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616 audioBuffer.size >>= 1;
1617 }
1618
1619 size_t reqSize = audioBuffer.size;
1620 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001621 size_t writtenSize = audioBuffer.size;
1622 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001623
1624 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001625 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1626 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1627 reqSize, (int) writtenSize);
1628 return NS_NEVER;
1629 }
1630
1631 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001632 // The callback is done filling buffers
1633 // Keep this thread going to handle timed events and
1634 // still try to get more data in intervals of WAIT_PERIOD_MS
1635 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001636 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001637 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001638
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001639 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001640 // 8 to 16 bit conversion, note that source and destination are the same address
1641 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001642 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643 }
1644
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001645 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1646 audioBuffer.frameCount = releasedFrames;
1647 mRemainingFrames -= releasedFrames;
1648 if (misalignment >= releasedFrames) {
1649 misalignment -= releasedFrames;
1650 } else {
1651 misalignment = 0;
1652 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653
1654 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001655
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001656 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1657 // if callback doesn't like to accept the full chunk
1658 if (writtenSize < reqSize) {
1659 continue;
1660 }
1661
1662 // There could be enough non-contiguous frames available to satisfy the remaining request
1663 if (mRemainingFrames <= nonContig) {
1664 continue;
1665 }
1666
1667#if 0
1668 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1669 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1670 // that total to a sum == notificationFrames.
1671 if (0 < misalignment && misalignment <= mRemainingFrames) {
1672 mRemainingFrames = misalignment;
1673 return (mRemainingFrames * 1100000000LL) / sampleRate;
1674 }
1675#endif
1676
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001677 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001678 mRemainingFrames = notificationFrames;
1679 mRetryOnPartialBuffer = true;
1680
1681 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1682 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001683}
1684
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001685status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001686{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001687 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001688 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001689 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001690 status_t result;
1691
Glenn Kastena47f3162012-11-07 10:13:08 -08001692 // refresh the audio configuration cache in this process to make sure we get new
1693 // output parameters in getOutput_l() and createTrack_l()
1694 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001695
Glenn Kasten23a75452014-01-13 10:37:17 -08001696 if (isOffloaded_l()) {
1697 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001698 return DEAD_OBJECT;
1699 }
1700
1701 // force new output query from audio policy manager;
1702 mOutput = 0;
1703 audio_io_handle_t output = getOutput_l();
1704
Glenn Kastena47f3162012-11-07 10:13:08 -08001705 // if the new IAudioTrack is created, createTrack_l() will modify the
1706 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1707 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001708
1709 // take the frames that will be lost by track recreation into account in saved position
1710 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001711 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001712 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001713 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001714 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001715 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001716 mFlags,
1717 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001718 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001719 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001720
Glenn Kastena47f3162012-11-07 10:13:08 -08001721 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001722 // continue playback from last known position, but
1723 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1724 if (mStaticProxy != NULL) {
1725 mLoopPeriod = 0;
1726 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1727 }
1728 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1729 // track destruction have been played? This is critical for SoundPool implementation
1730 // This must be broken, and needs to be tested/debugged.
1731#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001732 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001733 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001734 // Make sure that a client relying on callback events indicating underrun or
1735 // the actual amount of audio frames played (e.g SoundPool) receives them.
1736 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001737 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001738 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001739 }
1740 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001741#endif
1742 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001743 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001744 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001745 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001746 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001747 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1748 // As getOutput was called above and resulted in an output stream to be opened,
1749 // we need to release it.
1750 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001751 ALOGW("restoreTrack_l() failed status %d", result);
1752 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001753 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001754
1755 return result;
1756}
1757
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001758status_t AudioTrack::setParameters(const String8& keyValuePairs)
1759{
1760 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001761 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001762}
1763
Glenn Kastence703742013-07-19 16:33:58 -07001764status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1765{
Glenn Kasten53cec222013-08-29 09:01:02 -07001766 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001767 // FIXME not implemented for fast tracks; should use proxy and SSQ
1768 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1769 return INVALID_OPERATION;
1770 }
1771 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1772 return INVALID_OPERATION;
1773 }
1774 status_t status = mAudioTrack->getTimestamp(timestamp);
1775 if (status == NO_ERROR) {
1776 timestamp.mPosition += mProxy->getEpoch();
1777 }
1778 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001779}
1780
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001781String8 AudioTrack::getParameters(const String8& keys)
1782{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001783 audio_io_handle_t output = getOutput();
1784 if (output != 0) {
1785 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001786 } else {
1787 return String8::empty();
1788 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001789}
1790
Glenn Kasten23a75452014-01-13 10:37:17 -08001791bool AudioTrack::isOffloaded() const
1792{
1793 AutoMutex lock(mLock);
1794 return isOffloaded_l();
1795}
1796
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001797status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001798{
1799
1800 const size_t SIZE = 256;
1801 char buffer[SIZE];
1802 String8 result;
1803
1804 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001805 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1806 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001807 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001808 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001809 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001810 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001811 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001813 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001814 result.append(buffer);
1815 ::write(fd, result.string(), result.size());
1816 return NO_ERROR;
1817}
1818
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001819uint32_t AudioTrack::getUnderrunFrames() const
1820{
1821 AutoMutex lock(mLock);
1822 return mProxy->getUnderrunFrames();
1823}
1824
1825// =========================================================================
1826
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001827void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001828{
1829 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1830 if (audioTrack != 0) {
1831 AutoMutex lock(audioTrack->mLock);
1832 audioTrack->mProxy->binderDied();
1833 }
1834}
1835
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001836// =========================================================================
1837
1838AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001839 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1840 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001841{
1842}
1843
1844AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001845{
1846}
1847
1848bool AudioTrack::AudioTrackThread::threadLoop()
1849{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001850 {
1851 AutoMutex _l(mMyLock);
1852 if (mPaused) {
1853 mMyCond.wait(mMyLock);
1854 // caller will check for exitPending()
1855 return true;
1856 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001857 if (mIgnoreNextPausedInt) {
1858 mIgnoreNextPausedInt = false;
1859 mPausedInt = false;
1860 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001861 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001862 if (mPausedNs > 0) {
1863 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1864 } else {
1865 mMyCond.wait(mMyLock);
1866 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001867 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001868 return true;
1869 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001870 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001871 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001872 switch (ns) {
1873 case 0:
1874 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001875 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001876 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001877 return true;
1878 case NS_NEVER:
1879 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001880 case NS_WHENEVER:
1881 // FIXME increase poll interval, or make event-driven
1882 ns = 1000000000LL;
1883 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001884 default:
1885 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001886 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001887 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001888 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001889}
1890
Glenn Kasten3acbd052012-02-28 10:39:56 -08001891void AudioTrack::AudioTrackThread::requestExit()
1892{
1893 // must be in this order to avoid a race condition
1894 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001895 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001896}
1897
1898void AudioTrack::AudioTrackThread::pause()
1899{
1900 AutoMutex _l(mMyLock);
1901 mPaused = true;
1902}
1903
1904void AudioTrack::AudioTrackThread::resume()
1905{
1906 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001907 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001908 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001909 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001910 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001911 mMyCond.signal();
1912 }
1913}
1914
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001915void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1916{
1917 AutoMutex _l(mMyLock);
1918 mPausedInt = true;
1919 mPausedNs = ns;
1920}
1921
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001922}; // namespace android