blob: fa56dc2d99e27f38bc83b2a4fc46106d73e37cc2 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080023#include <audio_utils/primitives.h>
24#include <binder/IPCThreadState.h>
25#include <media/AudioTrack.h>
26#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010030#define WAIT_PERIOD_MS 10
31#define WAIT_STREAM_END_TIMEOUT_SEC 120
32
Glenn Kasten511754b2012-01-11 09:52:19 -080033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080035// ---------------------------------------------------------------------------
36
37// static
38status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080039 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080040 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080041 uint32_t sampleRate)
42{
Glenn Kastend65d73c2012-06-22 17:21:07 -070043 if (frameCount == NULL) {
44 return BAD_VALUE;
45 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070046
Glenn Kastene0fa4672012-04-24 14:35:14 -070047 // FIXME merge with similar code in createTrack_l(), except we're missing
48 // some information here that is available in createTrack_l():
49 // audio_io_handle_t output
50 // audio_format_t format
51 // audio_channel_mask_t channelMask
52 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080053 uint32_t afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080054 status_t status;
55 status = AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
56 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080057 ALOGE("Unable to query output sample rate for stream type %d; status %d",
58 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080059 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080060 }
Glenn Kastene33054e2012-11-14 12:54:39 -080061 size_t afFrameCount;
Glenn Kasten66a04672014-01-08 08:53:44 -080062 status = AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
63 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080064 ALOGE("Unable to query output frame count for stream type %d; status %d",
65 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080066 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080067 }
68 uint32_t afLatency;
Glenn Kasten66a04672014-01-08 08:53:44 -080069 status = AudioSystem::getOutputLatency(&afLatency, streamType);
70 if (status != NO_ERROR) {
Glenn Kasten70c0bfb2014-01-14 15:47:01 -080071 ALOGE("Unable to query output latency for stream type %d; status %d",
72 streamType, status);
Glenn Kasten66a04672014-01-08 08:53:44 -080073 return status;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080074 }
75
76 // Ensure that buffer depth covers at least audio hardware latency
77 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080078 if (minBufCount < 2) {
79 minBufCount = 2;
80 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080081
82 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070083 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten66a04672014-01-08 08:53:44 -080084 // The formula above should always produce a non-zero value, but return an error
85 // in the unlikely event that it does not, as that's part of the API contract.
86 if (*frameCount == 0) {
87 ALOGE("AudioTrack::getMinFrameCount failed for streamType %d, sampleRate %d",
88 streamType, sampleRate);
89 return BAD_VALUE;
90 }
Glenn Kasten3acbd052012-02-28 10:39:56 -080091 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
92 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080093 return NO_ERROR;
94}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095
96// ---------------------------------------------------------------------------
97
98AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070099 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800100 mIsTimed(false),
101 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800102 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103{
104}
105
106AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800107 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800109 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700110 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700112 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113 callback_t cbf,
114 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700115 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800116 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000117 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800118 const audio_offload_info_t *offloadInfo,
119 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700120 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800121 mIsTimed(false),
122 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800123 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700125 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700126 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800127 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
128 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800129}
130
Andreas Huberc8139852012-01-18 10:51:55 -0800131AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800132 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800133 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800134 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700135 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700137 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 callback_t cbf,
139 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700140 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800141 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000142 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800143 const audio_offload_info_t *offloadInfo,
144 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700145 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800146 mIsTimed(false),
147 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800148 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700150 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800151 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800152 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153}
154
155AudioTrack::~AudioTrack()
156{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 if (mStatus == NO_ERROR) {
158 // Make sure that callback function exits in the case where
159 // it is looping on buffer full condition in obtainBuffer().
160 // Otherwise the callback thread will never exit.
161 stop();
162 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100163 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800164 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 mAudioTrackThread->requestExitAndWait();
166 mAudioTrackThread.clear();
167 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700168 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
169 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700171 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 }
173}
174
175status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800176 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800178 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700179 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800180 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700181 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 callback_t cbf,
183 void* user,
184 int notificationFrames,
185 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700186 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800187 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000188 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800189 const audio_offload_info_t *offloadInfo,
190 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800192 switch (transferType) {
193 case TRANSFER_DEFAULT:
194 if (sharedBuffer != 0) {
195 transferType = TRANSFER_SHARED;
196 } else if (cbf == NULL || threadCanCallJava) {
197 transferType = TRANSFER_SYNC;
198 } else {
199 transferType = TRANSFER_CALLBACK;
200 }
201 break;
202 case TRANSFER_CALLBACK:
203 if (cbf == NULL || sharedBuffer != 0) {
204 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
205 return BAD_VALUE;
206 }
207 break;
208 case TRANSFER_OBTAIN:
209 case TRANSFER_SYNC:
210 if (sharedBuffer != 0) {
211 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
212 return BAD_VALUE;
213 }
214 break;
215 case TRANSFER_SHARED:
216 if (sharedBuffer == 0) {
217 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
218 return BAD_VALUE;
219 }
220 break;
221 default:
222 ALOGE("Invalid transfer type %d", transferType);
223 return BAD_VALUE;
224 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800225 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800226 mTransfer = transferType;
227
Glenn Kastene33054e2012-11-14 12:54:39 -0800228 // FIXME "int" here is legacy and will be replaced by size_t later
229 if (frameCountInt < 0) {
230 ALOGE("Invalid frame count %d", frameCountInt);
231 return BAD_VALUE;
232 }
233 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700235 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
236 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237
Glenn Kastene33054e2012-11-14 12:54:39 -0800238 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700239
Eric Laurent1703cdf2011-03-07 14:52:59 -0800240 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800241
Glenn Kasten53cec222013-08-29 09:01:02 -0700242 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700243 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000244 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 return INVALID_OPERATION;
246 }
247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700249 if (streamType == AUDIO_STREAM_DEFAULT) {
250 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800252 if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
253 ALOGE("Invalid stream type %d", streamType);
254 return BAD_VALUE;
255 }
256 mStreamType = streamType;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700257
Glenn Kastenb1bef512014-01-13 10:25:53 -0800258 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800260 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
261 if (status != NO_ERROR) {
262 ALOGE("Could not get output sample rate for stream type %d; status %d",
263 streamType, status);
264 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700265 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800267 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700268
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800270 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700271 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273
274 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700275 if (!audio_is_valid_format(format)) {
Glenn Kastencac3daa2014-02-07 09:47:14 -0800276 ALOGE("Invalid format %#x", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800277 return BAD_VALUE;
278 }
Glenn Kastendd5f4c82014-01-13 10:26:32 -0800279 mFormat = format;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700280
Glenn Kasten8ba90322013-10-30 11:29:27 -0700281 if (!audio_is_output_channel(channelMask)) {
282 ALOGE("Invalid channel mask %#x", channelMask);
283 return BAD_VALUE;
284 }
285
Glenn Kastene0fa4672012-04-24 14:35:14 -0700286 // AudioFlinger does not currently support 8-bit data in shared memory
287 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
288 ALOGE("8-bit data in shared memory is not supported");
289 return BAD_VALUE;
290 }
291
Eric Laurentc2f1f072009-07-17 12:17:14 -0700292 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100293 // or offload was requested
294 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
295 || !audio_is_linear_pcm(format)) {
296 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
297 ? "Offload request, forcing to Direct Output"
298 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700299 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800300 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700301 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700302 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700303 // only allow deep buffering for music stream type
304 if (streamType != AUDIO_STREAM_MUSIC) {
305 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
306 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307
Glenn Kastena42ff002012-11-14 12:47:55 -0800308 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700309 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800310 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700311
Glenn Kastene3aa6592012-12-04 12:22:46 -0800312 if (audio_is_linear_pcm(format)) {
313 mFrameSize = channelCount * audio_bytes_per_sample(format);
314 mFrameSizeAF = channelCount * sizeof(int16_t);
315 } else {
316 mFrameSize = sizeof(uint8_t);
317 mFrameSizeAF = sizeof(uint8_t);
318 }
319
Glenn Kastenb5ccb2d2014-01-13 14:42:43 -0800320 // Make copy of input parameter offloadInfo so that in the future:
321 // (a) createTrack_l doesn't need it as an input parameter
322 // (b) we can support re-creation of offloaded tracks
323 if (offloadInfo != NULL) {
324 mOffloadInfoCopy = *offloadInfo;
325 mOffloadInfo = &mOffloadInfoCopy;
326 } else {
327 mOffloadInfo = NULL;
328 }
329
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800330 mVolume[LEFT] = 1.0f;
331 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800332 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800333 // mFrameCount is initialized in createTrack_l
Glenn Kastenb6037442012-11-14 13:42:25 -0800334 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700335 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800336 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700337 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800338 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
339 mClientUid = IPCThreadState::self()->getCallingUid();
340 } else {
341 mClientUid = uid;
342 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700343 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700344 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700345 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700346
Glenn Kastena997e7a2012-08-07 09:44:19 -0700347 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700348 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700349 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
350 }
351
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800352 // create the IAudioTrack
Glenn Kasten363fb752014-01-15 12:27:31 -0800353 status = createTrack_l(0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800354
Glenn Kastena997e7a2012-08-07 09:44:19 -0700355 if (status != NO_ERROR) {
356 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100357 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
358 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700359 mAudioTrackThread.clear();
360 }
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800361 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800362#if 0 // FIXME This should no longer be needed
363 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100364 // As getOutput was called above and resulted in an output stream to be opened,
365 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -0800366 if (mOutput != 0) {
367 AudioSystem::releaseOutput(mOutput);
368 mOutput = 0;
369 }
370#endif
Glenn Kastena997e7a2012-08-07 09:44:19 -0700371 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700372 }
373
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 mStatus = NO_ERROR;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800375 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800377 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700379 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380 mNewPosition = 0;
381 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700382 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800383 mSequence = 1;
384 mObservedSequence = mSequence;
385 mInUnderrun = false;
386
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 return NO_ERROR;
388}
389
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390// -------------------------------------------------------------------------
391
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100392status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800394 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100395
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800396 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100397 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398 }
399
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800400 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800402 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100403 if (previousState == STATE_PAUSED_STOPPING) {
404 mState = STATE_STOPPING;
405 } else {
406 mState = STATE_ACTIVE;
407 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800408 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
409 // reset current position as seen by client to 0
410 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700411 // force refresh of remaining frames by processAudioBuffer() as last
412 // write before stop could be partial.
413 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800414 }
415 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700416 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800417
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800419 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100420 if (previousState == STATE_STOPPING) {
421 mProxy->interrupt();
422 } else {
423 t->resume();
424 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800425 } else {
426 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
427 get_sched_policy(0, &mPreviousSchedulingGroup);
428 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
429 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800431 status_t status = NO_ERROR;
432 if (!(flags & CBLK_INVALID)) {
433 status = mAudioTrack->start();
434 if (status == DEAD_OBJECT) {
435 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800437 }
438 if (flags & CBLK_INVALID) {
439 status = restoreTrack_l("start");
440 }
441
442 if (status != NO_ERROR) {
443 ALOGE("start() status %d", status);
444 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100446 if (previousState != STATE_STOPPING) {
447 t->pause();
448 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700450 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700451 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 }
453 }
454
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100455 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800456}
457
458void AudioTrack::stop()
459{
460 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700461 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800462 return;
463 }
464
Glenn Kasten23a75452014-01-13 10:37:17 -0800465 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100466 mState = STATE_STOPPING;
467 } else {
468 mState = STATE_STOPPED;
469 }
470
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800471 mProxy->interrupt();
472 mAudioTrack->stop();
473 // the playback head position will reset to 0, so if a marker is set, we need
474 // to activate it again
475 mMarkerReached = false;
476#if 0
477 // Force flush if a shared buffer is used otherwise audioflinger
478 // will not stop before end of buffer is reached.
479 // It may be needed to make sure that we stop playback, likely in case looping is on.
480 if (mSharedBuffer != 0) {
481 flush_l();
482 }
483#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100484
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800485 sp<AudioTrackThread> t = mAudioTrackThread;
486 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800487 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100488 t->pause();
489 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800490 } else {
491 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
492 set_sched_policy(0, mPreviousSchedulingGroup);
493 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494}
495
496bool AudioTrack::stopped() const
497{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800498 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800499 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500}
501
502void AudioTrack::flush()
503{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800504 if (mSharedBuffer != 0) {
505 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800506 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800507 AutoMutex lock(mLock);
508 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
509 return;
510 }
511 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800512}
513
Eric Laurent1703cdf2011-03-07 14:52:59 -0800514void AudioTrack::flush_l()
515{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800516 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700517
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700518 // clear playback marker and periodic update counter
519 mMarkerPosition = 0;
520 mMarkerReached = false;
521 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100522 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700523
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800524 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800525 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100526 mProxy->interrupt();
527 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800528 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800529 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530}
531
532void AudioTrack::pause()
533{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800534 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100535 if (mState == STATE_ACTIVE) {
536 mState = STATE_PAUSED;
537 } else if (mState == STATE_STOPPING) {
538 mState = STATE_PAUSED_STOPPING;
539 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800540 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800542 mProxy->interrupt();
543 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544}
545
Eric Laurentbe916aa2010-06-01 23:49:17 -0700546status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800548 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700549 return BAD_VALUE;
550 }
551
Eric Laurent1703cdf2011-03-07 14:52:59 -0800552 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 mVolume[LEFT] = left;
554 mVolume[RIGHT] = right;
555
Glenn Kastene3aa6592012-12-04 12:22:46 -0800556 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700557
Glenn Kasten23a75452014-01-13 10:37:17 -0800558 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700559 mAudioTrack->signal();
560 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700561 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562}
563
Glenn Kastenb1c09932012-02-27 16:21:04 -0800564status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800566 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700567}
568
Eric Laurent2beeb502010-07-16 07:43:46 -0700569status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700570{
Glenn Kasten05632a52012-01-03 14:22:33 -0800571 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700572 return BAD_VALUE;
573 }
574
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800575 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700576 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800577 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700578
579 return NO_ERROR;
580}
581
Glenn Kastena5224f32012-01-04 12:41:44 -0800582void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700583{
584 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800585 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700586 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587}
588
Glenn Kasten3b16c762012-11-14 08:44:39 -0800589status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100591 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800592 return INVALID_OPERATION;
593 }
594
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800595 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700597 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 }
599 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700600 if (rate == 0 || rate > afSamplingRate*2 ) {
601 return BAD_VALUE;
602 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603
Eric Laurent1703cdf2011-03-07 14:52:59 -0800604 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800605 mSampleRate = rate;
606 mProxy->setSampleRate(rate);
607
Eric Laurent57326622009-07-07 07:10:45 -0700608 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609}
610
Glenn Kastena5224f32012-01-04 12:41:44 -0800611uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612{
John Grossman4ff14ba2012-02-08 16:37:41 -0800613 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800614 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800615 }
616
Eric Laurent1703cdf2011-03-07 14:52:59 -0800617 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700618
619 // sample rate can be updated during playback by the offloaded decoder so we need to
620 // query the HAL and update if needed.
621// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800622 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700623 if (mOutput != 0) {
624 uint32_t sampleRate = 0;
625 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
626 if (status == NO_ERROR) {
627 mSampleRate = sampleRate;
628 }
629 }
630 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800631 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632}
633
634status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
635{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100636 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800637 return INVALID_OPERATION;
638 }
639
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800641 ;
642 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
643 loopEnd - loopStart >= MIN_LOOP) {
644 ;
645 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646 return BAD_VALUE;
647 }
648
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800649 AutoMutex lock(mLock);
650 // See setPosition() regarding setting parameters such as loop points or position while active
651 if (mState == STATE_ACTIVE) {
652 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700653 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800654 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 return NO_ERROR;
656}
657
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800658void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
659{
660 // FIXME If setting a loop also sets position to start of loop, then
661 // this is correct. Otherwise it should be removed.
662 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
663 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
664 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
665}
666
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667status_t AudioTrack::setMarkerPosition(uint32_t marker)
668{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700669 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100670 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700671 return INVALID_OPERATION;
672 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800674 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800675 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700676 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677
678 return NO_ERROR;
679}
680
Glenn Kastena5224f32012-01-04 12:41:44 -0800681status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100683 if (isOffloaded()) {
684 return INVALID_OPERATION;
685 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700686 if (marker == NULL) {
687 return BAD_VALUE;
688 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800690 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691 *marker = mMarkerPosition;
692
693 return NO_ERROR;
694}
695
696status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
697{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700698 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100699 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700700 return INVALID_OPERATION;
701 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800703 AutoMutex lock(mLock);
704 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705 mUpdatePeriod = updatePeriod;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800706
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 return NO_ERROR;
708}
709
Glenn Kastena5224f32012-01-04 12:41:44 -0800710status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100712 if (isOffloaded()) {
713 return INVALID_OPERATION;
714 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700715 if (updatePeriod == NULL) {
716 return BAD_VALUE;
717 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800719 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 *updatePeriod = mUpdatePeriod;
721
722 return NO_ERROR;
723}
724
725status_t AudioTrack::setPosition(uint32_t position)
726{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100727 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700728 return INVALID_OPERATION;
729 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800730 if (position > mFrameCount) {
731 return BAD_VALUE;
732 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800733
Eric Laurent1703cdf2011-03-07 14:52:59 -0800734 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800735 // Currently we require that the player is inactive before setting parameters such as position
736 // or loop points. Otherwise, there could be a race condition: the application could read the
737 // current position, compute a new position or loop parameters, and then set that position or
738 // loop parameters but it would do the "wrong" thing since the position has continued to advance
739 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
740 // to specify how it wants to handle such scenarios.
741 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700742 return INVALID_OPERATION;
743 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800744 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
745 mLoopPeriod = 0;
746 // FIXME Check whether loops and setting position are incompatible in old code.
747 // If we use setLoop for both purposes we lose the capability to set the position while looping.
748 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700749
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 return NO_ERROR;
751}
752
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800753status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700755 if (position == NULL) {
756 return BAD_VALUE;
757 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800758
Eric Laurent1703cdf2011-03-07 14:52:59 -0800759 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800760 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100761 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100763 if (mOutput != 0) {
764 uint32_t halFrames;
765 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
766 }
767 *position = dspFrames;
768 } else {
769 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
770 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
771 mProxy->getPosition();
772 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800773 return NO_ERROR;
774}
775
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800776status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800777{
778 if (mSharedBuffer == 0 || mIsTimed) {
779 return INVALID_OPERATION;
780 }
781 if (position == NULL) {
782 return BAD_VALUE;
783 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800784
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800785 AutoMutex lock(mLock);
786 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800787 return NO_ERROR;
788}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800789
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800790status_t AudioTrack::reload()
791{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100792 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800793 return INVALID_OPERATION;
794 }
795
Eric Laurent1703cdf2011-03-07 14:52:59 -0800796 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800797 // See setPosition() regarding setting parameters such as loop points or position while active
798 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700799 return INVALID_OPERATION;
800 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800801 mNewPosition = mUpdatePeriod;
802 mLoopPeriod = 0;
803 // FIXME The new code cannot reload while keeping a loop specified.
804 // Need to check how the old code handled this, and whether it's a significant change.
805 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800806 return NO_ERROR;
807}
808
Glenn Kasten38e905b2014-01-13 10:21:48 -0800809audio_io_handle_t AudioTrack::getOutput() const
Eric Laurentc2f1f072009-07-17 12:17:14 -0700810{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800811 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100812 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800813}
814
Eric Laurentbe916aa2010-06-01 23:49:17 -0700815status_t AudioTrack::attachAuxEffect(int effectId)
816{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800817 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700818 status_t status = mAudioTrack->attachAuxEffect(effectId);
819 if (status == NO_ERROR) {
820 mAuxEffectId = effectId;
821 }
822 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700823}
824
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800825// -------------------------------------------------------------------------
826
Eric Laurent1703cdf2011-03-07 14:52:59 -0800827// must be called with mLock held
Glenn Kasten363fb752014-01-15 12:27:31 -0800828status_t AudioTrack::createTrack_l(size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800829{
830 status_t status;
831 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
832 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700833 ALOGE("Could not get audioflinger");
834 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800835 }
836
Glenn Kasten38e905b2014-01-13 10:21:48 -0800837 audio_io_handle_t output = AudioSystem::getOutput(mStreamType, mSampleRate, mFormat,
838 mChannelMask, mFlags, mOffloadInfo);
839 if (output == 0) {
840 ALOGE("Could not get audio output for stream type %d, sample rate %u, format %#x, "
841 "channel mask %#x, flags %#x",
842 mStreamType, mSampleRate, mFormat, mChannelMask, mFlags);
843 return BAD_VALUE;
844 }
845 {
846 // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
847 // we must release it ourselves if anything goes wrong.
848
Glenn Kastence8828a2013-09-16 18:07:38 -0700849 // Not all of these values are needed under all conditions, but it is easier to get them all
850
Eric Laurentd1b449a2010-05-14 03:26:45 -0700851 uint32_t afLatency;
Glenn Kasten363fb752014-01-15 12:27:31 -0800852 status = AudioSystem::getLatency(output, mStreamType, &afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700853 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800854 ALOGE("getLatency(%d) failed status %d", output, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800855 goto release;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700856 }
857
Glenn Kastence8828a2013-09-16 18:07:38 -0700858 size_t afFrameCount;
Glenn Kasten363fb752014-01-15 12:27:31 -0800859 status = AudioSystem::getFrameCount(output, mStreamType, &afFrameCount);
Glenn Kastence8828a2013-09-16 18:07:38 -0700860 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800861 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, mStreamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800862 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700863 }
864
865 uint32_t afSampleRate;
Glenn Kasten363fb752014-01-15 12:27:31 -0800866 status = AudioSystem::getSamplingRate(output, mStreamType, &afSampleRate);
Glenn Kastence8828a2013-09-16 18:07:38 -0700867 if (status != NO_ERROR) {
Glenn Kasten363fb752014-01-15 12:27:31 -0800868 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, mStreamType, status);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800869 goto release;
Glenn Kastence8828a2013-09-16 18:07:38 -0700870 }
871
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700872 // Client decides whether the track is TIMED (see below), but can only express a preference
873 // for FAST. Server will perform additional tests.
Glenn Kasten363fb752014-01-15 12:27:31 -0800874 if ((mFlags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700875 // either of these use cases:
876 // use case 1: shared buffer
Glenn Kasten363fb752014-01-15 12:27:31 -0800877 (mSharedBuffer != 0) ||
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700878 // use case 2: callback handler
879 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800880 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700881 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -0800882 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700883 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700884 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885
Martin Storsjo398f2132014-01-31 13:30:15 +0200886 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && sampleRate != afSampleRate) {
887 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client due to mismatching sample rate (%d vs %d)",
888 sampleRate, afSampleRate);
889 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
890 }
891
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
Glenn Kasten363fb752014-01-15 12:27:31 -0800899 const uint32_t nBuffering = (mSampleRate == afSampleRate) ? 2 : 3;
Glenn Kastence8828a2013-09-16 18:07:38 -0700900
Eric Laurentd1b449a2010-05-14 03:26:45 -0700901 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700902
Glenn Kasten363fb752014-01-15 12:27:31 -0800903 size_t frameCount = mReqFrameCount;
904 if (!audio_is_linear_pcm(mFormat)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700905
Glenn Kasten363fb752014-01-15 12:27:31 -0800906 if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700907 // Same comment as below about ignoring frameCount parameter for set()
Glenn Kasten363fb752014-01-15 12:27:31 -0800908 frameCount = mSharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700909 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700911 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100912 if (mNotificationFramesAct != frameCount) {
913 mNotificationFramesAct = frameCount;
914 }
Glenn Kasten363fb752014-01-15 12:27:31 -0800915 } else if (mSharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916
Glenn Kastena42ff002012-11-14 12:47:55 -0800917 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700918 // 8-bit data in shared memory is not currently supported by AudioFlinger
Glenn Kasten363fb752014-01-15 12:27:31 -0800919 size_t alignment = /* mFormat == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800920 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700921 // More than 2 channels does not require stronger alignment than stereo
922 alignment <<= 1;
923 }
Glenn Kasten363fb752014-01-15 12:27:31 -0800924 if (((size_t)mSharedBuffer->pointer() & (alignment - 1)) != 0) {
Glenn Kastena42ff002012-11-14 12:47:55 -0800925 ALOGE("Invalid buffer alignment: address %p, channel count %u",
Glenn Kasten363fb752014-01-15 12:27:31 -0800926 mSharedBuffer->pointer(), mChannelCount);
Glenn Kasten38e905b2014-01-13 10:21:48 -0800927 status = BAD_VALUE;
928 goto release;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700929 }
930
931 // When initializing a shared buffer AudioTrack via constructors,
932 // there's no frameCount parameter.
933 // But when initializing a shared buffer AudioTrack via set(),
934 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kasten363fb752014-01-15 12:27:31 -0800935 frameCount = mSharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700936
Glenn Kasten363fb752014-01-15 12:27:31 -0800937 } else if (!(mFlags & AUDIO_OUTPUT_FLAG_FAST)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700938
939 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700940
Eric Laurentd1b449a2010-05-14 03:26:45 -0700941 // Ensure that buffer depth covers at least audio hardware latency
942 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700943 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
944 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700945 if (minBufCount <= nBuffering) {
946 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800947 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700948
Glenn Kasten363fb752014-01-15 12:27:31 -0800949 size_t minFrameCount = (afFrameCount*mSampleRate*minBufCount)/afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -0800950 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800951 ", afLatency=%d",
Glenn Kasten363fb752014-01-15 12:27:31 -0800952 minFrameCount, afFrameCount, minBufCount, mSampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700953
954 if (frameCount == 0) {
955 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700956 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700957 // not ALOGW because it happens all the time when playing key clicks over A2DP
958 ALOGV("Minimum buffer size corrected from %d to %d",
959 frameCount, minFrameCount);
960 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800961 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700962 // Make sure that application is notified with sufficient margin before underrun
963 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
964 mNotificationFramesAct = frameCount/nBuffering;
965 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700966
Glenn Kastene0fa4672012-04-24 14:35:14 -0700967 } else {
968 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700969 }
970
Glenn Kastena075db42012-03-06 11:22:44 -0800971 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
972 if (mIsTimed) {
973 trackFlags |= IAudioFlinger::TRACK_TIMED;
974 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800975
976 pid_t tid = -1;
Glenn Kasten363fb752014-01-15 12:27:31 -0800977 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700978 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800979 if (mAudioTrackThread != 0) {
980 tid = mAudioTrackThread->getTid();
981 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700982 }
983
Glenn Kasten363fb752014-01-15 12:27:31 -0800984 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100985 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
986 }
987
Glenn Kasten74935e42013-12-19 08:56:45 -0800988 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
989 // but we will still need the original value also
Glenn Kasten363fb752014-01-15 12:27:31 -0800990 sp<IAudioTrack> track = audioFlinger->createTrack(mStreamType,
991 mSampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700992 // AudioFlinger only sees 16-bit PCM
Glenn Kasten363fb752014-01-15 12:27:31 -0800993 mFormat == AUDIO_FORMAT_PCM_8_BIT ?
994 AUDIO_FORMAT_PCM_16_BIT : mFormat,
Glenn Kastena42ff002012-11-14 12:47:55 -0800995 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800996 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -0800997 &trackFlags,
Glenn Kasten363fb752014-01-15 12:27:31 -0800998 mSharedBuffer,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800999 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001000 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001001 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001002 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001003 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 &status);
1005
1006 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001007 ALOGE("AudioFlinger could not create track, status: %d", status);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001008 goto release;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001009 }
Glenn Kasten38e905b2014-01-13 10:21:48 -08001010 // AudioFlinger now owns the reference to the I/O handle,
1011 // so we are no longer responsible for releasing it.
1012
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001013 sp<IMemory> iMem = track->getCblk();
1014 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001015 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001016 return NO_INIT;
1017 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001018 void *iMemPointer = iMem->pointer();
1019 if (iMemPointer == NULL) {
1020 ALOGE("Could not get control block pointer");
1021 return NO_INIT;
1022 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001023 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001024 if (mAudioTrack != 0) {
1025 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1026 mDeathNotifier.clear();
1027 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001028 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001029 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001030 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001031 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001032 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001033 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1034 // In current design, AudioTrack client checks and ensures frame count validity before
1035 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1036 // for fast track as it uses a special method of assigning frame count.
1037 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1038 }
1039 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001040 mAwaitBoost = false;
Glenn Kasten363fb752014-01-15 12:27:31 -08001041 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001042 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001043 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001044 mAwaitBoost = true;
Glenn Kasten363fb752014-01-15 12:27:31 -08001045 if (mSharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001046 // Theoretically double-buffering is not required for fast tracks,
1047 // due to tighter scheduling. But in practice, to accommodate kernels with
1048 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1049 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1050 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001051 }
1052 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001053 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001054 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001055 // once denied, do not request again if IAudioTrack is re-created
Glenn Kasten363fb752014-01-15 12:27:31 -08001056 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_FAST);
1057 if (mSharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001058 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1059 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001060 }
1061 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001062 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001063 }
Glenn Kasten363fb752014-01-15 12:27:31 -08001064 if (mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001065 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1066 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1067 } else {
1068 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
Glenn Kasten363fb752014-01-15 12:27:31 -08001069 mFlags = (audio_output_flags_t) (mFlags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
Glenn Kasten38e905b2014-01-13 10:21:48 -08001070 // FIXME This is a warning, not an error, so don't return error status
1071 //return NO_INIT;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001072 }
1073 }
1074
Glenn Kasten38e905b2014-01-13 10:21:48 -08001075 // We retain a copy of the I/O handle, but don't own the reference
1076 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001077 mRefreshRemaining = true;
1078
1079 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1080 // is the value of pointer() for the shared buffer, otherwise buffers points
1081 // immediately after the control block. This address is for the mapping within client
1082 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1083 void* buffers;
Glenn Kasten363fb752014-01-15 12:27:31 -08001084 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001085 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001086 } else {
Glenn Kasten363fb752014-01-15 12:27:31 -08001087 buffers = mSharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001088 }
1089
Eric Laurent2beeb502010-07-16 07:43:46 -07001090 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001091 // FIXME don't believe this lie
Glenn Kasten363fb752014-01-15 12:27:31 -08001092 mLatency = afLatency + (1000*frameCount) / mSampleRate;
Glenn Kastenb6037442012-11-14 13:42:25 -08001093 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001094 // If IAudioTrack is re-created, don't let the requested frameCount
1095 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001096 if (frameCount > mReqFrameCount) {
1097 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001098 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001099
1100 // update proxy
Glenn Kasten363fb752014-01-15 12:27:31 -08001101 if (mSharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001102 mStaticProxy.clear();
1103 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1104 } else {
1105 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1106 mProxy = mStaticProxy;
1107 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001108 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1109 uint16_t(mVolume[LEFT] * 0x1000));
1110 mProxy->setSendLevel(mSendLevel);
1111 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001112 mProxy->setEpoch(epoch);
1113 mProxy->setMinimum(mNotificationFramesAct);
1114
1115 mDeathNotifier = new DeathNotifier(this);
1116 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001117
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001118 return NO_ERROR;
Glenn Kasten38e905b2014-01-13 10:21:48 -08001119 }
1120
1121release:
1122 AudioSystem::releaseOutput(output);
1123 if (status == NO_ERROR) {
1124 status = NO_INIT;
1125 }
1126 return status;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001127}
1128
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001129status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1130{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001131 if (audioBuffer == NULL) {
1132 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001133 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001134 if (mTransfer != TRANSFER_OBTAIN) {
1135 audioBuffer->frameCount = 0;
1136 audioBuffer->size = 0;
1137 audioBuffer->raw = NULL;
1138 return INVALID_OPERATION;
1139 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001140
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001141 const struct timespec *requested;
Eric Laurentdf576992014-01-27 18:13:39 -08001142 struct timespec timeout;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001143 if (waitCount == -1) {
1144 requested = &ClientProxy::kForever;
1145 } else if (waitCount == 0) {
1146 requested = &ClientProxy::kNonBlocking;
1147 } else if (waitCount > 0) {
1148 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001149 timeout.tv_sec = ms / 1000;
1150 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1151 requested = &timeout;
1152 } else {
1153 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1154 requested = NULL;
1155 }
1156 return obtainBuffer(audioBuffer, requested);
1157}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001158
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001159status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1160 struct timespec *elapsed, size_t *nonContig)
1161{
1162 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1163 uint32_t oldSequence = 0;
1164 uint32_t newSequence;
1165
1166 Proxy::Buffer buffer;
1167 status_t status = NO_ERROR;
1168
1169 static const int32_t kMaxTries = 5;
1170 int32_t tryCounter = kMaxTries;
1171
1172 do {
1173 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1174 // keep them from going away if another thread re-creates the track during obtainBuffer()
1175 sp<AudioTrackClientProxy> proxy;
1176 sp<IMemory> iMem;
1177
1178 { // start of lock scope
1179 AutoMutex lock(mLock);
1180
1181 newSequence = mSequence;
1182 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1183 if (status == DEAD_OBJECT) {
1184 // re-create track, unless someone else has already done so
1185 if (newSequence == oldSequence) {
1186 status = restoreTrack_l("obtainBuffer");
1187 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001188 buffer.mFrameCount = 0;
1189 buffer.mRaw = NULL;
1190 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001191 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001192 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001193 }
1194 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001195 oldSequence = newSequence;
1196
1197 // Keep the extra references
1198 proxy = mProxy;
1199 iMem = mCblkMemory;
1200
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001201 if (mState == STATE_STOPPING) {
1202 status = -EINTR;
1203 buffer.mFrameCount = 0;
1204 buffer.mRaw = NULL;
1205 buffer.mNonContig = 0;
1206 break;
1207 }
1208
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001209 // Non-blocking if track is stopped or paused
1210 if (mState != STATE_ACTIVE) {
1211 requested = &ClientProxy::kNonBlocking;
1212 }
1213
1214 } // end of lock scope
1215
1216 buffer.mFrameCount = audioBuffer->frameCount;
1217 // FIXME starts the requested timeout and elapsed over from scratch
1218 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1219
1220 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1221
1222 audioBuffer->frameCount = buffer.mFrameCount;
1223 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1224 audioBuffer->raw = buffer.mRaw;
1225 if (nonContig != NULL) {
1226 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001228 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229}
1230
1231void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1232{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001233 if (mTransfer == TRANSFER_SHARED) {
1234 return;
1235 }
1236
1237 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1238 if (stepCount == 0) {
1239 return;
1240 }
1241
1242 Proxy::Buffer buffer;
1243 buffer.mFrameCount = stepCount;
1244 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001245
Eric Laurent1703cdf2011-03-07 14:52:59 -08001246 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001247 mInUnderrun = false;
1248 mProxy->releaseBuffer(&buffer);
1249
1250 // restart track if it was disabled by audioflinger due to previous underrun
1251 if (mState == STATE_ACTIVE) {
1252 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001253 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001254 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1255 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001256 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001257 mAudioTrack->start();
1258 }
1259 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260}
1261
1262// -------------------------------------------------------------------------
1263
1264ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1265{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001266 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001267 return INVALID_OPERATION;
1268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001270 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001271 // Sanity-check: user is most-likely passing an error code, and it would
1272 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001273 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 return BAD_VALUE;
1275 }
1276
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001277 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278 Buffer audioBuffer;
1279
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001280 while (userSize >= mFrameSize) {
1281 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001282
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001283 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001285 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001287 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288 return ssize_t(err);
1289 }
1290
1291 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001292 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001294 toWrite = audioBuffer.size >> 1;
1295 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001296 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001298 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001300 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301 userSize -= toWrite;
1302 written += toWrite;
1303
1304 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001305 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306
1307 return written;
1308}
1309
1310// -------------------------------------------------------------------------
1311
John Grossman4ff14ba2012-02-08 16:37:41 -08001312TimedAudioTrack::TimedAudioTrack() {
1313 mIsTimed = true;
1314}
1315
1316status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1317{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001318 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001319 status_t result = UNKNOWN_ERROR;
1320
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001321#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001322 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1323 // while we are accessing the cblk
1324 sp<IAudioTrack> audioTrack = mAudioTrack;
1325 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001326#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001327
John Grossman4ff14ba2012-02-08 16:37:41 -08001328 // If the track is not invalid already, try to allocate a buffer. alloc
1329 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001330 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001331 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001332 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001333 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1334 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001335 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001336 }
1337 }
1338
1339 // If the track is invalid at this point, attempt to restore it. and try the
1340 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001341 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001342 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001343
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001344 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001345 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001346 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001347 }
1348
1349 return result;
1350}
1351
1352status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1353 int64_t pts)
1354{
Eric Laurentdf839842012-05-31 14:27:14 -07001355 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1356 {
1357 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001358 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001359 // restart track if it was disabled by audioflinger due to previous underrun
1360 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001361 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1362 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001363 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001364 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001365 mAudioTrack->start();
1366 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001367 }
Eric Laurentdf839842012-05-31 14:27:14 -07001368 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001369}
1370
1371status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1372 TargetTimeline target)
1373{
1374 return mAudioTrack->setMediaTimeTransform(xform, target);
1375}
1376
1377// -------------------------------------------------------------------------
1378
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001379nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001380{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001381 // Currently the AudioTrack thread is not created if there are no callbacks.
1382 // Would it ever make sense to run the thread, even without callbacks?
1383 // If so, then replace this by checks at each use for mCbf != NULL.
1384 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1385
Eric Laurent1703cdf2011-03-07 14:52:59 -08001386 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001387 if (mAwaitBoost) {
1388 mAwaitBoost = false;
1389 mLock.unlock();
1390 static const int32_t kMaxTries = 5;
1391 int32_t tryCounter = kMaxTries;
1392 uint32_t pollUs = 10000;
1393 do {
1394 int policy = sched_getscheduler(0);
1395 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1396 break;
1397 }
1398 usleep(pollUs);
1399 pollUs <<= 1;
1400 } while (tryCounter-- > 0);
1401 if (tryCounter < 0) {
1402 ALOGE("did not receive expected priority boost on time");
1403 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001404 // Run again immediately
1405 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001406 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001407
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001408 // Can only reference mCblk while locked
1409 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001410 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001411
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001412 // Check for track invalidation
1413 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001414 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1415 // AudioSystem cache. We should not exit here but after calling the callback so
1416 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001417 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001418 status_t status = restoreTrack_l("processAudioBuffer");
1419 mLock.unlock();
1420 // Run again immediately, but with a new IAudioTrack
1421 return 0;
1422 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001423 }
1424
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001425 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001426 bool active = mState == STATE_ACTIVE;
1427
1428 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1429 bool newUnderrun = false;
1430 if (flags & CBLK_UNDERRUN) {
1431#if 0
1432 // Currently in shared buffer mode, when the server reaches the end of buffer,
1433 // the track stays active in continuous underrun state. It's up to the application
1434 // to pause or stop the track, or set the position to a new offset within buffer.
1435 // This was some experimental code to auto-pause on underrun. Keeping it here
1436 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1437 if (mTransfer == TRANSFER_SHARED) {
1438 mState = STATE_PAUSED;
1439 active = false;
1440 }
1441#endif
1442 if (!mInUnderrun) {
1443 mInUnderrun = true;
1444 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001445 }
1446 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001447
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001448 // Get current position of server
1449 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001450
1451 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001452 bool markerReached = false;
1453 size_t markerPosition = mMarkerPosition;
1454 // FIXME fails for wraparound, need 64 bits
1455 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1456 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001457 }
1458
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001459 // Determine number of new position callback(s) that will be needed, while locked
1460 size_t newPosCount = 0;
1461 size_t newPosition = mNewPosition;
1462 size_t updatePeriod = mUpdatePeriod;
1463 // FIXME fails for wraparound, need 64 bits
1464 if (updatePeriod > 0 && position >= newPosition) {
1465 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1466 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001467 }
1468
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001469 // Cache other fields that will be needed soon
1470 uint32_t loopPeriod = mLoopPeriod;
1471 uint32_t sampleRate = mSampleRate;
1472 size_t notificationFrames = mNotificationFramesAct;
1473 if (mRefreshRemaining) {
1474 mRefreshRemaining = false;
1475 mRemainingFrames = notificationFrames;
1476 mRetryOnPartialBuffer = false;
1477 }
1478 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001479 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001480
1481 // These fields don't need to be cached, because they are assigned only by set():
1482 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1483 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1484
1485 mLock.unlock();
1486
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001487 if (waitStreamEnd) {
1488 AutoMutex lock(mLock);
1489
1490 sp<AudioTrackClientProxy> proxy = mProxy;
1491 sp<IMemory> iMem = mCblkMemory;
1492
1493 struct timespec timeout;
1494 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1495 timeout.tv_nsec = 0;
1496
1497 mLock.unlock();
1498 status_t status = mProxy->waitStreamEndDone(&timeout);
1499 mLock.lock();
1500 switch (status) {
1501 case NO_ERROR:
1502 case DEAD_OBJECT:
1503 case TIMED_OUT:
1504 mLock.unlock();
1505 mCbf(EVENT_STREAM_END, mUserData, NULL);
1506 mLock.lock();
1507 if (mState == STATE_STOPPING) {
1508 mState = STATE_STOPPED;
1509 if (status != DEAD_OBJECT) {
1510 return NS_INACTIVE;
1511 }
1512 }
1513 return 0;
1514 default:
1515 return 0;
1516 }
1517 }
1518
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001519 // perform callbacks while unlocked
1520 if (newUnderrun) {
1521 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1522 }
1523 // FIXME we will miss loops if loop cycle was signaled several times since last call
1524 // to processAudioBuffer()
1525 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1526 mCbf(EVENT_LOOP_END, mUserData, NULL);
1527 }
1528 if (flags & CBLK_BUFFER_END) {
1529 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1530 }
1531 if (markerReached) {
1532 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1533 }
1534 while (newPosCount > 0) {
1535 size_t temp = newPosition;
1536 mCbf(EVENT_NEW_POS, mUserData, &temp);
1537 newPosition += updatePeriod;
1538 newPosCount--;
1539 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001540
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001541 if (mObservedSequence != sequence) {
1542 mObservedSequence = sequence;
1543 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001544 // for offloaded tracks, just wait for the upper layers to recreate the track
1545 if (isOffloaded()) {
1546 return NS_INACTIVE;
1547 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001548 }
1549
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001550 // if inactive, then don't run me again until re-started
1551 if (!active) {
1552 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001553 }
1554
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001555 // Compute the estimated time until the next timed event (position, markers, loops)
1556 // FIXME only for non-compressed audio
1557 uint32_t minFrames = ~0;
1558 if (!markerReached && position < markerPosition) {
1559 minFrames = markerPosition - position;
1560 }
1561 if (loopPeriod > 0 && loopPeriod < minFrames) {
1562 minFrames = loopPeriod;
1563 }
1564 if (updatePeriod > 0 && updatePeriod < minFrames) {
1565 minFrames = updatePeriod;
1566 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001568 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1569 static const uint32_t kPoll = 0;
1570 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1571 minFrames = kPoll * notificationFrames;
1572 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001573
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001574 // Convert frame units to time units
1575 nsecs_t ns = NS_WHENEVER;
1576 if (minFrames != (uint32_t) ~0) {
1577 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1578 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1579 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1580 }
1581
1582 // If not supplying data by EVENT_MORE_DATA, then we're done
1583 if (mTransfer != TRANSFER_CALLBACK) {
1584 return ns;
1585 }
1586
1587 struct timespec timeout;
1588 const struct timespec *requested = &ClientProxy::kForever;
1589 if (ns != NS_WHENEVER) {
1590 timeout.tv_sec = ns / 1000000000LL;
1591 timeout.tv_nsec = ns % 1000000000LL;
1592 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1593 requested = &timeout;
1594 }
1595
1596 while (mRemainingFrames > 0) {
1597
1598 Buffer audioBuffer;
1599 audioBuffer.frameCount = mRemainingFrames;
1600 size_t nonContig;
1601 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1602 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1603 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1604 requested = &ClientProxy::kNonBlocking;
1605 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001606 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1607 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001608 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001609 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1610 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001611 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001612 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001613 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1614 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001615 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001616
Eric Laurent42a6f422013-08-29 14:35:05 -07001617 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001618 mRetryOnPartialBuffer = false;
1619 if (avail < mRemainingFrames) {
1620 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1621 if (ns < 0 || myns < ns) {
1622 ns = myns;
1623 }
1624 return ns;
1625 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001626 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001627
1628 // Divide buffer size by 2 to take into account the expansion
1629 // due to 8 to 16 bit conversion: the callback must fill only half
1630 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001631 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001632 audioBuffer.size >>= 1;
1633 }
1634
1635 size_t reqSize = audioBuffer.size;
1636 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001637 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001638
1639 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001640 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1641 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1642 reqSize, (int) writtenSize);
1643 return NS_NEVER;
1644 }
1645
1646 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001647 // The callback is done filling buffers
1648 // Keep this thread going to handle timed events and
1649 // still try to get more data in intervals of WAIT_PERIOD_MS
1650 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001651 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001652 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001654 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001655 // 8 to 16 bit conversion, note that source and destination are the same address
1656 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001657 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001658 }
1659
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001660 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1661 audioBuffer.frameCount = releasedFrames;
1662 mRemainingFrames -= releasedFrames;
1663 if (misalignment >= releasedFrames) {
1664 misalignment -= releasedFrames;
1665 } else {
1666 misalignment = 0;
1667 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001668
1669 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001670
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001671 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1672 // if callback doesn't like to accept the full chunk
1673 if (writtenSize < reqSize) {
1674 continue;
1675 }
1676
1677 // There could be enough non-contiguous frames available to satisfy the remaining request
1678 if (mRemainingFrames <= nonContig) {
1679 continue;
1680 }
1681
1682#if 0
1683 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1684 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1685 // that total to a sum == notificationFrames.
1686 if (0 < misalignment && misalignment <= mRemainingFrames) {
1687 mRemainingFrames = misalignment;
1688 return (mRemainingFrames * 1100000000LL) / sampleRate;
1689 }
1690#endif
1691
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001692 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001693 mRemainingFrames = notificationFrames;
1694 mRetryOnPartialBuffer = true;
1695
1696 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1697 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001698}
1699
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001700status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001701{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001702 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001703 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001704 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001705 status_t result;
1706
Glenn Kastena47f3162012-11-07 10:13:08 -08001707 // refresh the audio configuration cache in this process to make sure we get new
Glenn Kasten38e905b2014-01-13 10:21:48 -08001708 // output parameters in createTrack_l()
Glenn Kastena47f3162012-11-07 10:13:08 -08001709 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001710
Glenn Kasten23a75452014-01-13 10:37:17 -08001711 if (isOffloaded_l()) {
1712 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001713 return DEAD_OBJECT;
1714 }
1715
Glenn Kastena47f3162012-11-07 10:13:08 -08001716 // if the new IAudioTrack is created, createTrack_l() will modify the
1717 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1718 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001719
1720 // take the frames that will be lost by track recreation into account in saved position
1721 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001722 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kasten363fb752014-01-15 12:27:31 -08001723 result = createTrack_l(position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001724
Glenn Kastena47f3162012-11-07 10:13:08 -08001725 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001726 // continue playback from last known position, but
1727 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1728 if (mStaticProxy != NULL) {
1729 mLoopPeriod = 0;
1730 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1731 }
1732 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1733 // track destruction have been played? This is critical for SoundPool implementation
1734 // This must be broken, and needs to be tested/debugged.
1735#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001736 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001737 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001738 // Make sure that a client relying on callback events indicating underrun or
1739 // the actual amount of audio frames played (e.g SoundPool) receives them.
1740 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001741 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001742 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001743 }
1744 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001745#endif
1746 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001747 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001748 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001749 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001750 if (result != NO_ERROR) {
Glenn Kasten2b2165c2014-01-13 08:53:36 -08001751 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001752#if 0 // FIXME This should no longer be needed
1753 //Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001754 // As getOutput was called above and resulted in an output stream to be opened,
1755 // we need to release it.
Glenn Kasten38e905b2014-01-13 10:21:48 -08001756 if (mOutput != 0) {
1757 AudioSystem::releaseOutput(mOutput);
1758 mOutput = 0;
1759 }
1760#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001761 ALOGW("restoreTrack_l() failed status %d", result);
1762 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001763 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001764
1765 return result;
1766}
1767
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001768status_t AudioTrack::setParameters(const String8& keyValuePairs)
1769{
1770 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001771 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001772}
1773
Glenn Kastence703742013-07-19 16:33:58 -07001774status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1775{
Glenn Kasten53cec222013-08-29 09:01:02 -07001776 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001777 // FIXME not implemented for fast tracks; should use proxy and SSQ
1778 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1779 return INVALID_OPERATION;
1780 }
1781 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1782 return INVALID_OPERATION;
1783 }
1784 status_t status = mAudioTrack->getTimestamp(timestamp);
1785 if (status == NO_ERROR) {
1786 timestamp.mPosition += mProxy->getEpoch();
1787 }
1788 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001789}
1790
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001791String8 AudioTrack::getParameters(const String8& keys)
1792{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001793 audio_io_handle_t output = getOutput();
1794 if (output != 0) {
1795 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001796 } else {
1797 return String8::empty();
1798 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001799}
1800
Glenn Kasten23a75452014-01-13 10:37:17 -08001801bool AudioTrack::isOffloaded() const
1802{
1803 AutoMutex lock(mLock);
1804 return isOffloaded_l();
1805}
1806
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001807status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001808{
1809
1810 const size_t SIZE = 256;
1811 char buffer[SIZE];
1812 String8 result;
1813
1814 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001815 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1816 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001817 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001818 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001819 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001820 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001821 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001823 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824 result.append(buffer);
1825 ::write(fd, result.string(), result.size());
1826 return NO_ERROR;
1827}
1828
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001829uint32_t AudioTrack::getUnderrunFrames() const
1830{
1831 AutoMutex lock(mLock);
1832 return mProxy->getUnderrunFrames();
1833}
1834
1835// =========================================================================
1836
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001837void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001838{
1839 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1840 if (audioTrack != 0) {
1841 AutoMutex lock(audioTrack->mLock);
1842 audioTrack->mProxy->binderDied();
1843 }
1844}
1845
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001846// =========================================================================
1847
1848AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001849 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1850 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001851{
1852}
1853
1854AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001855{
1856}
1857
1858bool AudioTrack::AudioTrackThread::threadLoop()
1859{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001860 {
1861 AutoMutex _l(mMyLock);
1862 if (mPaused) {
1863 mMyCond.wait(mMyLock);
1864 // caller will check for exitPending()
1865 return true;
1866 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001867 if (mIgnoreNextPausedInt) {
1868 mIgnoreNextPausedInt = false;
1869 mPausedInt = false;
1870 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001871 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001872 if (mPausedNs > 0) {
1873 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1874 } else {
1875 mMyCond.wait(mMyLock);
1876 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001877 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001878 return true;
1879 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001880 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001881 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001882 switch (ns) {
1883 case 0:
1884 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001885 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001886 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001887 return true;
1888 case NS_NEVER:
1889 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001890 case NS_WHENEVER:
1891 // FIXME increase poll interval, or make event-driven
1892 ns = 1000000000LL;
1893 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001894 default:
1895 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001896 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001897 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001898 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001899}
1900
Glenn Kasten3acbd052012-02-28 10:39:56 -08001901void AudioTrack::AudioTrackThread::requestExit()
1902{
1903 // must be in this order to avoid a race condition
1904 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001905 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001906}
1907
1908void AudioTrack::AudioTrackThread::pause()
1909{
1910 AutoMutex _l(mMyLock);
1911 mPaused = true;
1912}
1913
1914void AudioTrack::AudioTrackThread::resume()
1915{
1916 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001917 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001918 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001919 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001920 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001921 mMyCond.signal();
1922 }
1923}
1924
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001925void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1926{
1927 AutoMutex _l(mMyLock);
1928 mPausedInt = true;
1929 mPausedNs = ns;
1930}
1931
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001932}; // namespace android