blob: a8f91ab785ca3320574ed63643e078ea018724b2 [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 }
225 mTransfer = transferType;
226
Glenn Kastene33054e2012-11-14 12:54:39 -0800227 // FIXME "int" here is legacy and will be replaced by size_t later
228 if (frameCountInt < 0) {
229 ALOGE("Invalid frame count %d", frameCountInt);
230 return BAD_VALUE;
231 }
232 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700234 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
235 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236
Glenn Kastene33054e2012-11-14 12:54:39 -0800237 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700238
Eric Laurent1703cdf2011-03-07 14:52:59 -0800239 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800240
Glenn Kasten53cec222013-08-29 09:01:02 -0700241 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700242 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000243 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 return INVALID_OPERATION;
245 }
246
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100247 mOutput = 0;
248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 if (streamType == AUDIO_STREAM_DEFAULT) {
251 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700253
Glenn Kastenb1bef512014-01-13 10:25:53 -0800254 status_t status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 if (sampleRate == 0) {
Glenn Kastenb1bef512014-01-13 10:25:53 -0800256 status = AudioSystem::getOutputSamplingRate(&sampleRate, streamType);
257 if (status != NO_ERROR) {
258 ALOGE("Could not get output sample rate for stream type %d; status %d",
259 streamType, status);
260 return status;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700261 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800263 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800266 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269
270 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700271 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800272 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 return BAD_VALUE;
274 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275
Glenn Kasten8ba90322013-10-30 11:29:27 -0700276 if (!audio_is_output_channel(channelMask)) {
277 ALOGE("Invalid channel mask %#x", channelMask);
278 return BAD_VALUE;
279 }
280
Glenn Kastene0fa4672012-04-24 14:35:14 -0700281 // AudioFlinger does not currently support 8-bit data in shared memory
282 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
283 ALOGE("8-bit data in shared memory is not supported");
284 return BAD_VALUE;
285 }
286
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100288 // or offload was requested
289 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
290 || !audio_is_linear_pcm(format)) {
291 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
292 ? "Offload request, forcing to Direct Output"
293 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700294 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800295 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700296 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700297 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700298 // only allow deep buffering for music stream type
299 if (streamType != AUDIO_STREAM_MUSIC) {
300 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
301 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700302
Glenn Kastena42ff002012-11-14 12:47:55 -0800303 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700304 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800305 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700306
Glenn Kastene3aa6592012-12-04 12:22:46 -0800307 if (audio_is_linear_pcm(format)) {
308 mFrameSize = channelCount * audio_bytes_per_sample(format);
309 mFrameSizeAF = channelCount * sizeof(int16_t);
310 } else {
311 mFrameSize = sizeof(uint8_t);
312 mFrameSizeAF = sizeof(uint8_t);
313 }
314
Dima Zavinfce7a472011-04-19 22:30:36 -0700315 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800316 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800317 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000318 flags,
319 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700320
321 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000322 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 return BAD_VALUE;
324 }
325
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800326 mVolume[LEFT] = 1.0f;
327 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800328 mSendLevel = 0.0f;
Glenn Kasten396fabd2014-01-08 08:54:23 -0800329 // mFrameCount is initialized in createTrack_l
Glenn Kastenb603744e2012-11-14 13:42:25 -0800330 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700331 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800332 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700333 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800334 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
335 mClientUid = IPCThreadState::self()->getCallingUid();
336 } else {
337 mClientUid = uid;
338 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700339 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700340 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700341 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700342
Glenn Kastena997e7a2012-08-07 09:44:19 -0700343 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700344 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700345 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
346 }
347
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800348 // create the IAudioTrack
Glenn Kastenb1bef512014-01-13 10:25:53 -0800349 status = createTrack_l(streamType,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800350 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800351 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800352 frameCount,
353 flags,
354 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800355 output,
356 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800357
Glenn Kastena997e7a2012-08-07 09:44:19 -0700358 if (status != NO_ERROR) {
359 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100360 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
361 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700362 mAudioTrackThread.clear();
363 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100364 //Use of direct and offloaded output streams is ref counted by audio policy manager.
365 // As getOutput was called above and resulted in an output stream to be opened,
366 // we need to release it.
367 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700368 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700369 }
370
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800373 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 mSharedBuffer = sharedBuffer;
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;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100386 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800387
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 return NO_ERROR;
389}
390
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391// -------------------------------------------------------------------------
392
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100393status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800395 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100396
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800397 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100398 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 }
400
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800401 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800403 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100404 if (previousState == STATE_PAUSED_STOPPING) {
405 mState = STATE_STOPPING;
406 } else {
407 mState = STATE_ACTIVE;
408 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800409 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
410 // reset current position as seen by client to 0
411 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700412 // force refresh of remaining frames by processAudioBuffer() as last
413 // write before stop could be partial.
414 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800415 }
416 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700417 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800418
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800419 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800420 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100421 if (previousState == STATE_STOPPING) {
422 mProxy->interrupt();
423 } else {
424 t->resume();
425 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800426 } else {
427 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
428 get_sched_policy(0, &mPreviousSchedulingGroup);
429 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
430 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800432 status_t status = NO_ERROR;
433 if (!(flags & CBLK_INVALID)) {
434 status = mAudioTrack->start();
435 if (status == DEAD_OBJECT) {
436 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800438 }
439 if (flags & CBLK_INVALID) {
440 status = restoreTrack_l("start");
441 }
442
443 if (status != NO_ERROR) {
444 ALOGE("start() status %d", status);
445 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100447 if (previousState != STATE_STOPPING) {
448 t->pause();
449 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700451 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700452 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 }
454 }
455
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100456 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800457}
458
459void AudioTrack::stop()
460{
461 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700462 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800463 return;
464 }
465
Glenn Kasten23a75452014-01-13 10:37:17 -0800466 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100467 mState = STATE_STOPPING;
468 } else {
469 mState = STATE_STOPPED;
470 }
471
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800472 mProxy->interrupt();
473 mAudioTrack->stop();
474 // the playback head position will reset to 0, so if a marker is set, we need
475 // to activate it again
476 mMarkerReached = false;
477#if 0
478 // Force flush if a shared buffer is used otherwise audioflinger
479 // will not stop before end of buffer is reached.
480 // It may be needed to make sure that we stop playback, likely in case looping is on.
481 if (mSharedBuffer != 0) {
482 flush_l();
483 }
484#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100485
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800486 sp<AudioTrackThread> t = mAudioTrackThread;
487 if (t != 0) {
Glenn Kasten23a75452014-01-13 10:37:17 -0800488 if (!isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100489 t->pause();
490 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800491 } else {
492 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
493 set_sched_policy(0, mPreviousSchedulingGroup);
494 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800495}
496
497bool AudioTrack::stopped() const
498{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800499 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800500 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501}
502
503void AudioTrack::flush()
504{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800505 if (mSharedBuffer != 0) {
506 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800507 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800508 AutoMutex lock(mLock);
509 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
510 return;
511 }
512 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800513}
514
Eric Laurent1703cdf2011-03-07 14:52:59 -0800515void AudioTrack::flush_l()
516{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800517 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700518
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700519 // clear playback marker and periodic update counter
520 mMarkerPosition = 0;
521 mMarkerReached = false;
522 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100523 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700524
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800525 mState = STATE_FLUSHED;
Glenn Kasten23a75452014-01-13 10:37:17 -0800526 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100527 mProxy->interrupt();
528 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800529 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800530 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531}
532
533void AudioTrack::pause()
534{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800535 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100536 if (mState == STATE_ACTIVE) {
537 mState = STATE_PAUSED;
538 } else if (mState == STATE_STOPPING) {
539 mState = STATE_PAUSED_STOPPING;
540 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800541 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800543 mProxy->interrupt();
544 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545}
546
Eric Laurentbe916aa2010-06-01 23:49:17 -0700547status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800549 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700550 return BAD_VALUE;
551 }
552
Eric Laurent1703cdf2011-03-07 14:52:59 -0800553 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 mVolume[LEFT] = left;
555 mVolume[RIGHT] = right;
556
Glenn Kastene3aa6592012-12-04 12:22:46 -0800557 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700558
Glenn Kasten23a75452014-01-13 10:37:17 -0800559 if (isOffloaded_l()) {
Eric Laurent59fe0102013-09-27 18:48:26 -0700560 mAudioTrack->signal();
561 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700562 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563}
564
Glenn Kastenb1c09932012-02-27 16:21:04 -0800565status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800567 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700568}
569
Eric Laurent2beeb502010-07-16 07:43:46 -0700570status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571{
Glenn Kasten05632a52012-01-03 14:22:33 -0800572 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700573 return BAD_VALUE;
574 }
575
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800576 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700577 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800578 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700579
580 return NO_ERROR;
581}
582
Glenn Kastena5224f32012-01-04 12:41:44 -0800583void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700584{
585 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800586 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588}
589
Glenn Kasten3b16c762012-11-14 08:44:39 -0800590status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100592 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800593 return INVALID_OPERATION;
594 }
595
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800596 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700598 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 }
600 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700601 if (rate == 0 || rate > afSamplingRate*2 ) {
602 return BAD_VALUE;
603 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604
Eric Laurent1703cdf2011-03-07 14:52:59 -0800605 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800606 mSampleRate = rate;
607 mProxy->setSampleRate(rate);
608
Eric Laurent57326622009-07-07 07:10:45 -0700609 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610}
611
Glenn Kastena5224f32012-01-04 12:41:44 -0800612uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613{
John Grossman4ff14ba2012-02-08 16:37:41 -0800614 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800615 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800616 }
617
Eric Laurent1703cdf2011-03-07 14:52:59 -0800618 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700619
620 // sample rate can be updated during playback by the offloaded decoder so we need to
621 // query the HAL and update if needed.
622// FIXME use Proxy return channel to update the rate from server and avoid polling here
Glenn Kasten23a75452014-01-13 10:37:17 -0800623 if (isOffloaded_l()) {
Eric Laurent6f59db12013-07-26 17:16:50 -0700624 if (mOutput != 0) {
625 uint32_t sampleRate = 0;
626 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
627 if (status == NO_ERROR) {
628 mSampleRate = sampleRate;
629 }
630 }
631 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800632 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633}
634
635status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
636{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100637 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800638 return INVALID_OPERATION;
639 }
640
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800642 ;
643 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
644 loopEnd - loopStart >= MIN_LOOP) {
645 ;
646 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647 return BAD_VALUE;
648 }
649
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800650 AutoMutex lock(mLock);
651 // See setPosition() regarding setting parameters such as loop points or position while active
652 if (mState == STATE_ACTIVE) {
653 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700654 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800655 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 return NO_ERROR;
657}
658
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800659void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
660{
661 // FIXME If setting a loop also sets position to start of loop, then
662 // this is correct. Otherwise it should be removed.
663 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
664 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
665 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
666}
667
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668status_t AudioTrack::setMarkerPosition(uint32_t marker)
669{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700670 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100671 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700672 return INVALID_OPERATION;
673 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800675 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700677 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678
679 return NO_ERROR;
680}
681
Glenn Kastena5224f32012-01-04 12:41:44 -0800682status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100684 if (isOffloaded()) {
685 return INVALID_OPERATION;
686 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700687 if (marker == NULL) {
688 return BAD_VALUE;
689 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800691 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692 *marker = mMarkerPosition;
693
694 return NO_ERROR;
695}
696
697status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
698{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700699 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100700 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700701 return INVALID_OPERATION;
702 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800704 AutoMutex lock(mLock);
705 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 return NO_ERROR;
708}
709
Glenn Kastena5224f32012-01-04 12:41:44 -0800710status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100712 if (isOffloaded()) {
713 return INVALID_OPERATION;
714 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700715 if (updatePeriod == NULL) {
716 return BAD_VALUE;
717 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800719 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 *updatePeriod = mUpdatePeriod;
721
722 return NO_ERROR;
723}
724
725status_t AudioTrack::setPosition(uint32_t position)
726{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100727 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700728 return INVALID_OPERATION;
729 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800730 if (position > mFrameCount) {
731 return BAD_VALUE;
732 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800733
Eric Laurent1703cdf2011-03-07 14:52:59 -0800734 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800735 // Currently we require that the player is inactive before setting parameters such as position
736 // or loop points. Otherwise, there could be a race condition: the application could read the
737 // current position, compute a new position or loop parameters, and then set that position or
738 // loop parameters but it would do the "wrong" thing since the position has continued to advance
739 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
740 // to specify how it wants to handle such scenarios.
741 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700742 return INVALID_OPERATION;
743 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800744 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
745 mLoopPeriod = 0;
746 // FIXME Check whether loops and setting position are incompatible in old code.
747 // If we use setLoop for both purposes we lose the capability to set the position while looping.
748 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700749
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 return NO_ERROR;
751}
752
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800753status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700755 if (position == NULL) {
756 return BAD_VALUE;
757 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800758
Eric Laurent1703cdf2011-03-07 14:52:59 -0800759 AutoMutex lock(mLock);
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
Eric Laurentc2f1f072009-07-17 12:17:14 -0700809audio_io_handle_t AudioTrack::getOutput()
810{
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
815// must be called with mLock held
816audio_io_handle_t AudioTrack::getOutput_l()
817{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100818 if (mOutput) {
819 return mOutput;
820 } else {
821 return AudioSystem::getOutput(mStreamType,
822 mSampleRate, mFormat, mChannelMask, mFlags);
823 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700824}
825
Eric Laurentbe916aa2010-06-01 23:49:17 -0700826status_t AudioTrack::attachAuxEffect(int effectId)
827{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800828 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700829 status_t status = mAudioTrack->attachAuxEffect(effectId);
830 if (status == NO_ERROR) {
831 mAuxEffectId = effectId;
832 }
833 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700834}
835
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800836// -------------------------------------------------------------------------
837
Eric Laurent1703cdf2011-03-07 14:52:59 -0800838// must be called with mLock held
839status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800840 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800841 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800842 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800843 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700844 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800845 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800846 audio_io_handle_t output,
847 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800848{
849 status_t status;
850 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
851 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700852 ALOGE("Could not get audioflinger");
853 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800854 }
855
Glenn Kastence8828a2013-09-16 18:07:38 -0700856 // Not all of these values are needed under all conditions, but it is easier to get them all
857
Eric Laurentd1b449a2010-05-14 03:26:45 -0700858 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700859 status = AudioSystem::getLatency(output, streamType, &afLatency);
860 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800861 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700862 return NO_INIT;
863 }
864
Glenn Kastence8828a2013-09-16 18:07:38 -0700865 size_t afFrameCount;
866 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
867 if (status != NO_ERROR) {
868 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
869 return NO_INIT;
870 }
871
872 uint32_t afSampleRate;
873 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
874 if (status != NO_ERROR) {
875 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
876 return NO_INIT;
877 }
878
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700879 // Client decides whether the track is TIMED (see below), but can only express a preference
880 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700881 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700882 // either of these use cases:
883 // use case 1: shared buffer
884 (sharedBuffer != 0) ||
885 // use case 2: callback handler
886 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800887 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700888 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700889 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700890 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700891 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700892 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700893
Glenn Kastence8828a2013-09-16 18:07:38 -0700894 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800895 // n = 1 fast track with single buffering; nBuffering is ignored
896 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700897 // n = 2 normal track, no sample rate conversion
898 // n = 3 normal track, with sample rate conversion
899 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
900 // n > 3 very high latency or very small notification interval; nBuffering is ignored
901 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
902
Eric Laurentd1b449a2010-05-14 03:26:45 -0700903 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700904
Dima Zavinfce7a472011-04-19 22:30:36 -0700905 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700906
Eric Laurentd1b449a2010-05-14 03:26:45 -0700907 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700908 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700909 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700910 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700911 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700912 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100913 if (mNotificationFramesAct != frameCount) {
914 mNotificationFramesAct = frameCount;
915 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700916 } else if (sharedBuffer != 0) {
917
Glenn Kastena42ff002012-11-14 12:47:55 -0800918 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700919 // 8-bit data in shared memory is not currently supported by AudioFlinger
920 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800921 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700922 // More than 2 channels does not require stronger alignment than stereo
923 alignment <<= 1;
924 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800925 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
926 ALOGE("Invalid buffer alignment: address %p, channel count %u",
927 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700928 return BAD_VALUE;
929 }
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 Kastena42ff002012-11-14 12:47:55 -0800935 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700936
937 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
938
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 Kastene33054e2012-11-14 12:54:39 -0800949 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
950 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800951 ", afLatency=%d",
952 minFrameCount, afFrameCount, minBufCount, sampleRate, 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;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700977 if (flags & 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
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100984 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
985 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
986 }
987
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800988 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800989 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700990 // AudioFlinger only sees 16-bit PCM
991 format == AUDIO_FORMAT_PCM_8_BIT ?
992 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800993 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800994 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800995 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800996 sharedBuffer,
997 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800998 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700999 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001000 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001001 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001002 &status);
1003
1004 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001005 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001006 return status;
1007 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001008 sp<IMemory> iMem = track->getCblk();
1009 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001010 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001011 return NO_INIT;
1012 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001013 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001014 if (mAudioTrack != 0) {
1015 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1016 mDeathNotifier.clear();
1017 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001018 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001019 mCblkMemory = iMem;
1020 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1021 mCblk = cblk;
Glenn Kastenb603744e2012-11-14 13:42:25 -08001022 size_t temp = cblk->frameCount_;
1023 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1024 // In current design, AudioTrack client checks and ensures frame count validity before
1025 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1026 // for fast track as it uses a special method of assigning frame count.
1027 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1028 }
1029 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001030 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001031 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001032 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb603744e2012-11-14 13:42:25 -08001033 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001034 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001035 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001036 // Theoretically double-buffering is not required for fast tracks,
1037 // due to tighter scheduling. But in practice, to accommodate kernels with
1038 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1039 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1040 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001041 }
1042 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001043 } else {
Glenn Kastenb603744e2012-11-14 13:42:25 -08001044 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001045 // once denied, do not request again if IAudioTrack is re-created
1046 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1047 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001048 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001049 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1050 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001051 }
1052 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001053 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001054 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001055 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1056 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1057 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1058 } else {
1059 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1060 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1061 mFlags = flags;
1062 return NO_INIT;
1063 }
1064 }
1065
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001066 mRefreshRemaining = true;
1067
1068 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1069 // is the value of pointer() for the shared buffer, otherwise buffers points
1070 // immediately after the control block. This address is for the mapping within client
1071 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1072 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001073 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001074 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001075 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001076 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001077 }
1078
Eric Laurent2beeb502010-07-16 07:43:46 -07001079 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001080 // FIXME don't believe this lie
Glenn Kastenb603744e2012-11-14 13:42:25 -08001081 mLatency = afLatency + (1000*frameCount) / sampleRate;
1082 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001083 // If IAudioTrack is re-created, don't let the requested frameCount
1084 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb603744e2012-11-14 13:42:25 -08001085 if (frameCount > mReqFrameCount) {
1086 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001087 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001088
1089 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001090 if (sharedBuffer == 0) {
1091 mStaticProxy.clear();
1092 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1093 } else {
1094 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1095 mProxy = mStaticProxy;
1096 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001097 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1098 uint16_t(mVolume[LEFT] * 0x1000));
1099 mProxy->setSendLevel(mSendLevel);
1100 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001101 mProxy->setEpoch(epoch);
1102 mProxy->setMinimum(mNotificationFramesAct);
1103
1104 mDeathNotifier = new DeathNotifier(this);
1105 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001106
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001107 return NO_ERROR;
1108}
1109
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001110status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1111{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001112 if (audioBuffer == NULL) {
1113 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001114 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001115 if (mTransfer != TRANSFER_OBTAIN) {
1116 audioBuffer->frameCount = 0;
1117 audioBuffer->size = 0;
1118 audioBuffer->raw = NULL;
1119 return INVALID_OPERATION;
1120 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001121
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001122 const struct timespec *requested;
1123 if (waitCount == -1) {
1124 requested = &ClientProxy::kForever;
1125 } else if (waitCount == 0) {
1126 requested = &ClientProxy::kNonBlocking;
1127 } else if (waitCount > 0) {
1128 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1129 struct timespec timeout;
1130 timeout.tv_sec = ms / 1000;
1131 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1132 requested = &timeout;
1133 } else {
1134 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1135 requested = NULL;
1136 }
1137 return obtainBuffer(audioBuffer, requested);
1138}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001139
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001140status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1141 struct timespec *elapsed, size_t *nonContig)
1142{
1143 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1144 uint32_t oldSequence = 0;
1145 uint32_t newSequence;
1146
1147 Proxy::Buffer buffer;
1148 status_t status = NO_ERROR;
1149
1150 static const int32_t kMaxTries = 5;
1151 int32_t tryCounter = kMaxTries;
1152
1153 do {
1154 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1155 // keep them from going away if another thread re-creates the track during obtainBuffer()
1156 sp<AudioTrackClientProxy> proxy;
1157 sp<IMemory> iMem;
1158
1159 { // start of lock scope
1160 AutoMutex lock(mLock);
1161
1162 newSequence = mSequence;
1163 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1164 if (status == DEAD_OBJECT) {
1165 // re-create track, unless someone else has already done so
1166 if (newSequence == oldSequence) {
1167 status = restoreTrack_l("obtainBuffer");
1168 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001169 buffer.mFrameCount = 0;
1170 buffer.mRaw = NULL;
1171 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001172 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001173 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001174 }
1175 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001176 oldSequence = newSequence;
1177
1178 // Keep the extra references
1179 proxy = mProxy;
1180 iMem = mCblkMemory;
1181
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001182 if (mState == STATE_STOPPING) {
1183 status = -EINTR;
1184 buffer.mFrameCount = 0;
1185 buffer.mRaw = NULL;
1186 buffer.mNonContig = 0;
1187 break;
1188 }
1189
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001190 // Non-blocking if track is stopped or paused
1191 if (mState != STATE_ACTIVE) {
1192 requested = &ClientProxy::kNonBlocking;
1193 }
1194
1195 } // end of lock scope
1196
1197 buffer.mFrameCount = audioBuffer->frameCount;
1198 // FIXME starts the requested timeout and elapsed over from scratch
1199 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1200
1201 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1202
1203 audioBuffer->frameCount = buffer.mFrameCount;
1204 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1205 audioBuffer->raw = buffer.mRaw;
1206 if (nonContig != NULL) {
1207 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001209 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001210}
1211
1212void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1213{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001214 if (mTransfer == TRANSFER_SHARED) {
1215 return;
1216 }
1217
1218 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1219 if (stepCount == 0) {
1220 return;
1221 }
1222
1223 Proxy::Buffer buffer;
1224 buffer.mFrameCount = stepCount;
1225 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001226
Eric Laurent1703cdf2011-03-07 14:52:59 -08001227 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001228 mInUnderrun = false;
1229 mProxy->releaseBuffer(&buffer);
1230
1231 // restart track if it was disabled by audioflinger due to previous underrun
1232 if (mState == STATE_ACTIVE) {
1233 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001234 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001235 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1236 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001237 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001238 mAudioTrack->start();
1239 }
1240 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001241}
1242
1243// -------------------------------------------------------------------------
1244
1245ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1246{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001247 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001248 return INVALID_OPERATION;
1249 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001251 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001252 // Sanity-check: user is most-likely passing an error code, and it would
1253 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001254 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 return BAD_VALUE;
1256 }
1257
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001258 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 Buffer audioBuffer;
1260
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001261 while (userSize >= mFrameSize) {
1262 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001263
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001264 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001266 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001268 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269 return ssize_t(err);
1270 }
1271
1272 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001273 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001275 toWrite = audioBuffer.size >> 1;
1276 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001277 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001279 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001281 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001282 userSize -= toWrite;
1283 written += toWrite;
1284
1285 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001286 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287
1288 return written;
1289}
1290
1291// -------------------------------------------------------------------------
1292
John Grossman4ff14ba2012-02-08 16:37:41 -08001293TimedAudioTrack::TimedAudioTrack() {
1294 mIsTimed = true;
1295}
1296
1297status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1298{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001299 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001300 status_t result = UNKNOWN_ERROR;
1301
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001302#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001303 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1304 // while we are accessing the cblk
1305 sp<IAudioTrack> audioTrack = mAudioTrack;
1306 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001307#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001308
John Grossman4ff14ba2012-02-08 16:37:41 -08001309 // If the track is not invalid already, try to allocate a buffer. alloc
1310 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001311 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001312 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001313 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001314 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1315 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001316 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001317 }
1318 }
1319
1320 // If the track is invalid at this point, attempt to restore it. and try the
1321 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001322 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001323 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001324
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001325 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001326 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001327 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001328 }
1329
1330 return result;
1331}
1332
1333status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1334 int64_t pts)
1335{
Eric Laurentdf839842012-05-31 14:27:14 -07001336 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1337 {
1338 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001339 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001340 // restart track if it was disabled by audioflinger due to previous underrun
1341 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001342 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1343 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001344 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001345 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001346 mAudioTrack->start();
1347 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001348 }
Eric Laurentdf839842012-05-31 14:27:14 -07001349 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001350}
1351
1352status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1353 TargetTimeline target)
1354{
1355 return mAudioTrack->setMediaTimeTransform(xform, target);
1356}
1357
1358// -------------------------------------------------------------------------
1359
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001360nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001361{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001362 // Currently the AudioTrack thread is not created if there are no callbacks.
1363 // Would it ever make sense to run the thread, even without callbacks?
1364 // If so, then replace this by checks at each use for mCbf != NULL.
1365 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1366
Eric Laurent1703cdf2011-03-07 14:52:59 -08001367 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001368 if (mAwaitBoost) {
1369 mAwaitBoost = false;
1370 mLock.unlock();
1371 static const int32_t kMaxTries = 5;
1372 int32_t tryCounter = kMaxTries;
1373 uint32_t pollUs = 10000;
1374 do {
1375 int policy = sched_getscheduler(0);
1376 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1377 break;
1378 }
1379 usleep(pollUs);
1380 pollUs <<= 1;
1381 } while (tryCounter-- > 0);
1382 if (tryCounter < 0) {
1383 ALOGE("did not receive expected priority boost on time");
1384 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001385 // Run again immediately
1386 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001387 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001388
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001389 // Can only reference mCblk while locked
1390 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001391 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001392
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001393 // Check for track invalidation
1394 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001395 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1396 // AudioSystem cache. We should not exit here but after calling the callback so
1397 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001398 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001399 status_t status = restoreTrack_l("processAudioBuffer");
1400 mLock.unlock();
1401 // Run again immediately, but with a new IAudioTrack
1402 return 0;
1403 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001404 }
1405
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001406 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001407 bool active = mState == STATE_ACTIVE;
1408
1409 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1410 bool newUnderrun = false;
1411 if (flags & CBLK_UNDERRUN) {
1412#if 0
1413 // Currently in shared buffer mode, when the server reaches the end of buffer,
1414 // the track stays active in continuous underrun state. It's up to the application
1415 // to pause or stop the track, or set the position to a new offset within buffer.
1416 // This was some experimental code to auto-pause on underrun. Keeping it here
1417 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1418 if (mTransfer == TRANSFER_SHARED) {
1419 mState = STATE_PAUSED;
1420 active = false;
1421 }
1422#endif
1423 if (!mInUnderrun) {
1424 mInUnderrun = true;
1425 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426 }
1427 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001428
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001429 // Get current position of server
1430 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001431
1432 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001433 bool markerReached = false;
1434 size_t markerPosition = mMarkerPosition;
1435 // FIXME fails for wraparound, need 64 bits
1436 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1437 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438 }
1439
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001440 // Determine number of new position callback(s) that will be needed, while locked
1441 size_t newPosCount = 0;
1442 size_t newPosition = mNewPosition;
1443 size_t updatePeriod = mUpdatePeriod;
1444 // FIXME fails for wraparound, need 64 bits
1445 if (updatePeriod > 0 && position >= newPosition) {
1446 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1447 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001448 }
1449
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001450 // Cache other fields that will be needed soon
1451 uint32_t loopPeriod = mLoopPeriod;
1452 uint32_t sampleRate = mSampleRate;
1453 size_t notificationFrames = mNotificationFramesAct;
1454 if (mRefreshRemaining) {
1455 mRefreshRemaining = false;
1456 mRemainingFrames = notificationFrames;
1457 mRetryOnPartialBuffer = false;
1458 }
1459 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001460 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001461
1462 // These fields don't need to be cached, because they are assigned only by set():
1463 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1464 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1465
1466 mLock.unlock();
1467
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001468 if (waitStreamEnd) {
1469 AutoMutex lock(mLock);
1470
1471 sp<AudioTrackClientProxy> proxy = mProxy;
1472 sp<IMemory> iMem = mCblkMemory;
1473
1474 struct timespec timeout;
1475 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1476 timeout.tv_nsec = 0;
1477
1478 mLock.unlock();
1479 status_t status = mProxy->waitStreamEndDone(&timeout);
1480 mLock.lock();
1481 switch (status) {
1482 case NO_ERROR:
1483 case DEAD_OBJECT:
1484 case TIMED_OUT:
1485 mLock.unlock();
1486 mCbf(EVENT_STREAM_END, mUserData, NULL);
1487 mLock.lock();
1488 if (mState == STATE_STOPPING) {
1489 mState = STATE_STOPPED;
1490 if (status != DEAD_OBJECT) {
1491 return NS_INACTIVE;
1492 }
1493 }
1494 return 0;
1495 default:
1496 return 0;
1497 }
1498 }
1499
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001500 // perform callbacks while unlocked
1501 if (newUnderrun) {
1502 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1503 }
1504 // FIXME we will miss loops if loop cycle was signaled several times since last call
1505 // to processAudioBuffer()
1506 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1507 mCbf(EVENT_LOOP_END, mUserData, NULL);
1508 }
1509 if (flags & CBLK_BUFFER_END) {
1510 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1511 }
1512 if (markerReached) {
1513 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1514 }
1515 while (newPosCount > 0) {
1516 size_t temp = newPosition;
1517 mCbf(EVENT_NEW_POS, mUserData, &temp);
1518 newPosition += updatePeriod;
1519 newPosCount--;
1520 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001521
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001522 if (mObservedSequence != sequence) {
1523 mObservedSequence = sequence;
1524 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001525 // for offloaded tracks, just wait for the upper layers to recreate the track
1526 if (isOffloaded()) {
1527 return NS_INACTIVE;
1528 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001529 }
1530
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001531 // if inactive, then don't run me again until re-started
1532 if (!active) {
1533 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001534 }
1535
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001536 // Compute the estimated time until the next timed event (position, markers, loops)
1537 // FIXME only for non-compressed audio
1538 uint32_t minFrames = ~0;
1539 if (!markerReached && position < markerPosition) {
1540 minFrames = markerPosition - position;
1541 }
1542 if (loopPeriod > 0 && loopPeriod < minFrames) {
1543 minFrames = loopPeriod;
1544 }
1545 if (updatePeriod > 0 && updatePeriod < minFrames) {
1546 minFrames = updatePeriod;
1547 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001548
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001549 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1550 static const uint32_t kPoll = 0;
1551 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1552 minFrames = kPoll * notificationFrames;
1553 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001554
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001555 // Convert frame units to time units
1556 nsecs_t ns = NS_WHENEVER;
1557 if (minFrames != (uint32_t) ~0) {
1558 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1559 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1560 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1561 }
1562
1563 // If not supplying data by EVENT_MORE_DATA, then we're done
1564 if (mTransfer != TRANSFER_CALLBACK) {
1565 return ns;
1566 }
1567
1568 struct timespec timeout;
1569 const struct timespec *requested = &ClientProxy::kForever;
1570 if (ns != NS_WHENEVER) {
1571 timeout.tv_sec = ns / 1000000000LL;
1572 timeout.tv_nsec = ns % 1000000000LL;
1573 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1574 requested = &timeout;
1575 }
1576
1577 while (mRemainingFrames > 0) {
1578
1579 Buffer audioBuffer;
1580 audioBuffer.frameCount = mRemainingFrames;
1581 size_t nonContig;
1582 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1583 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1584 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1585 requested = &ClientProxy::kNonBlocking;
1586 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001587 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1588 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001589 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001590 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1591 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001592 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001593 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001594 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1595 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001597
Eric Laurent42a6f422013-08-29 14:35:05 -07001598 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001599 mRetryOnPartialBuffer = false;
1600 if (avail < mRemainingFrames) {
1601 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1602 if (ns < 0 || myns < ns) {
1603 ns = myns;
1604 }
1605 return ns;
1606 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001607 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608
1609 // Divide buffer size by 2 to take into account the expansion
1610 // due to 8 to 16 bit conversion: the callback must fill only half
1611 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001612 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001613 audioBuffer.size >>= 1;
1614 }
1615
1616 size_t reqSize = audioBuffer.size;
1617 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001618 size_t writtenSize = audioBuffer.size;
1619 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001620
1621 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001622 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1623 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1624 reqSize, (int) writtenSize);
1625 return NS_NEVER;
1626 }
1627
1628 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001629 // The callback is done filling buffers
1630 // Keep this thread going to handle timed events and
1631 // still try to get more data in intervals of WAIT_PERIOD_MS
1632 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001633 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001634 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001635
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001636 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001637 // 8 to 16 bit conversion, note that source and destination are the same address
1638 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001639 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001640 }
1641
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001642 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1643 audioBuffer.frameCount = releasedFrames;
1644 mRemainingFrames -= releasedFrames;
1645 if (misalignment >= releasedFrames) {
1646 misalignment -= releasedFrames;
1647 } else {
1648 misalignment = 0;
1649 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001650
1651 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001652
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001653 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1654 // if callback doesn't like to accept the full chunk
1655 if (writtenSize < reqSize) {
1656 continue;
1657 }
1658
1659 // There could be enough non-contiguous frames available to satisfy the remaining request
1660 if (mRemainingFrames <= nonContig) {
1661 continue;
1662 }
1663
1664#if 0
1665 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1666 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1667 // that total to a sum == notificationFrames.
1668 if (0 < misalignment && misalignment <= mRemainingFrames) {
1669 mRemainingFrames = misalignment;
1670 return (mRemainingFrames * 1100000000LL) / sampleRate;
1671 }
1672#endif
1673
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001674 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001675 mRemainingFrames = notificationFrames;
1676 mRetryOnPartialBuffer = true;
1677
1678 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1679 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001680}
1681
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001682status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001683{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001684 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001685 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001686 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001687 status_t result;
1688
Glenn Kastena47f3162012-11-07 10:13:08 -08001689 // refresh the audio configuration cache in this process to make sure we get new
1690 // output parameters in getOutput_l() and createTrack_l()
1691 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001692
Glenn Kasten23a75452014-01-13 10:37:17 -08001693 if (isOffloaded_l()) {
1694 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001695 return DEAD_OBJECT;
1696 }
1697
1698 // force new output query from audio policy manager;
1699 mOutput = 0;
1700 audio_io_handle_t output = getOutput_l();
1701
Glenn Kastena47f3162012-11-07 10:13:08 -08001702 // if the new IAudioTrack is created, createTrack_l() will modify the
1703 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1704 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001705
1706 // take the frames that will be lost by track recreation into account in saved position
1707 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001708 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001709 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001710 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001711 mFormat,
Glenn Kastenb603744e2012-11-14 13:42:25 -08001712 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001713 mFlags,
1714 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001715 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001716 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001717
Glenn Kastena47f3162012-11-07 10:13:08 -08001718 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001719 // continue playback from last known position, but
1720 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1721 if (mStaticProxy != NULL) {
1722 mLoopPeriod = 0;
1723 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1724 }
1725 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1726 // track destruction have been played? This is critical for SoundPool implementation
1727 // This must be broken, and needs to be tested/debugged.
1728#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001729 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001730 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001731 // Make sure that a client relying on callback events indicating underrun or
1732 // the actual amount of audio frames played (e.g SoundPool) receives them.
1733 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001734 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001735 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001736 }
1737 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001738#endif
1739 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001740 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001741 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001742 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001743 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001744 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1745 // As getOutput was called above and resulted in an output stream to be opened,
1746 // we need to release it.
1747 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001748 ALOGW("restoreTrack_l() failed status %d", result);
1749 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001750 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001751
1752 return result;
1753}
1754
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001755status_t AudioTrack::setParameters(const String8& keyValuePairs)
1756{
1757 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001758 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001759}
1760
Glenn Kastence703742013-07-19 16:33:58 -07001761status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1762{
Glenn Kasten53cec222013-08-29 09:01:02 -07001763 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001764 // FIXME not implemented for fast tracks; should use proxy and SSQ
1765 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1766 return INVALID_OPERATION;
1767 }
1768 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1769 return INVALID_OPERATION;
1770 }
1771 status_t status = mAudioTrack->getTimestamp(timestamp);
1772 if (status == NO_ERROR) {
1773 timestamp.mPosition += mProxy->getEpoch();
1774 }
1775 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001776}
1777
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001778String8 AudioTrack::getParameters(const String8& keys)
1779{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001780 audio_io_handle_t output = getOutput();
1781 if (output != 0) {
1782 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001783 } else {
1784 return String8::empty();
1785 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001786}
1787
Glenn Kasten23a75452014-01-13 10:37:17 -08001788bool AudioTrack::isOffloaded() const
1789{
1790 AutoMutex lock(mLock);
1791 return isOffloaded_l();
1792}
1793
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001794status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001795{
1796
1797 const size_t SIZE = 256;
1798 char buffer[SIZE];
1799 String8 result;
1800
1801 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001802 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1803 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001804 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001805 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb603744e2012-11-14 13:42:25 -08001806 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001807 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001808 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001810 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811 result.append(buffer);
1812 ::write(fd, result.string(), result.size());
1813 return NO_ERROR;
1814}
1815
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001816uint32_t AudioTrack::getUnderrunFrames() const
1817{
1818 AutoMutex lock(mLock);
1819 return mProxy->getUnderrunFrames();
1820}
1821
1822// =========================================================================
1823
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001824void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001825{
1826 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1827 if (audioTrack != 0) {
1828 AutoMutex lock(audioTrack->mLock);
1829 audioTrack->mProxy->binderDied();
1830 }
1831}
1832
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001833// =========================================================================
1834
1835AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001836 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1837 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001838{
1839}
1840
1841AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001842{
1843}
1844
1845bool AudioTrack::AudioTrackThread::threadLoop()
1846{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001847 {
1848 AutoMutex _l(mMyLock);
1849 if (mPaused) {
1850 mMyCond.wait(mMyLock);
1851 // caller will check for exitPending()
1852 return true;
1853 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001854 if (mIgnoreNextPausedInt) {
1855 mIgnoreNextPausedInt = false;
1856 mPausedInt = false;
1857 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001858 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001859 if (mPausedNs > 0) {
1860 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1861 } else {
1862 mMyCond.wait(mMyLock);
1863 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001864 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001865 return true;
1866 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001867 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001868 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001869 switch (ns) {
1870 case 0:
1871 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001872 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001873 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001874 return true;
1875 case NS_NEVER:
1876 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001877 case NS_WHENEVER:
1878 // FIXME increase poll interval, or make event-driven
1879 ns = 1000000000LL;
1880 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001881 default:
1882 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001883 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001884 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001885 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001886}
1887
Glenn Kasten3acbd052012-02-28 10:39:56 -08001888void AudioTrack::AudioTrackThread::requestExit()
1889{
1890 // must be in this order to avoid a race condition
1891 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001892 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001893}
1894
1895void AudioTrack::AudioTrackThread::pause()
1896{
1897 AutoMutex _l(mMyLock);
1898 mPaused = true;
1899}
1900
1901void AudioTrack::AudioTrackThread::resume()
1902{
1903 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001904 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001905 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001906 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001907 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001908 mMyCond.signal();
1909 }
1910}
1911
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001912void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1913{
1914 AutoMutex _l(mMyLock);
1915 mPausedInt = true;
1916 mPausedNs = ns;
1917}
1918
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001919}; // namespace android