blob: 640a2c9871c0b83ed5e74390d8e85867573c6056 [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 Kastenb6037442012-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 }
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800364 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100365 // 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;
Glenn Kasten2b2165c2014-01-13 08:53:36 -0800707
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 return NO_ERROR;
709}
710
Glenn Kastena5224f32012-01-04 12:41:44 -0800711status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800712{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100713 if (isOffloaded()) {
714 return INVALID_OPERATION;
715 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700716 if (updatePeriod == NULL) {
717 return BAD_VALUE;
718 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800720 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800721 *updatePeriod = mUpdatePeriod;
722
723 return NO_ERROR;
724}
725
726status_t AudioTrack::setPosition(uint32_t position)
727{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100728 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700729 return INVALID_OPERATION;
730 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800731 if (position > mFrameCount) {
732 return BAD_VALUE;
733 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800734
Eric Laurent1703cdf2011-03-07 14:52:59 -0800735 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800736 // Currently we require that the player is inactive before setting parameters such as position
737 // or loop points. Otherwise, there could be a race condition: the application could read the
738 // current position, compute a new position or loop parameters, and then set that position or
739 // loop parameters but it would do the "wrong" thing since the position has continued to advance
740 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
741 // to specify how it wants to handle such scenarios.
742 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700743 return INVALID_OPERATION;
744 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800745 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
746 mLoopPeriod = 0;
747 // FIXME Check whether loops and setting position are incompatible in old code.
748 // If we use setLoop for both purposes we lose the capability to set the position while looping.
749 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700750
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751 return NO_ERROR;
752}
753
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800754status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800755{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700756 if (position == NULL) {
757 return BAD_VALUE;
758 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800759
Eric Laurent1703cdf2011-03-07 14:52:59 -0800760 AutoMutex lock(mLock);
Glenn Kasten23a75452014-01-13 10:37:17 -0800761 if (isOffloaded_l()) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100762 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800763
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100764 if (mOutput != 0) {
765 uint32_t halFrames;
766 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
767 }
768 *position = dspFrames;
769 } else {
770 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
771 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
772 mProxy->getPosition();
773 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800774 return NO_ERROR;
775}
776
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800777status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800778{
779 if (mSharedBuffer == 0 || mIsTimed) {
780 return INVALID_OPERATION;
781 }
782 if (position == NULL) {
783 return BAD_VALUE;
784 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800785
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800786 AutoMutex lock(mLock);
787 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800788 return NO_ERROR;
789}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800790
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800791status_t AudioTrack::reload()
792{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100793 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800794 return INVALID_OPERATION;
795 }
796
Eric Laurent1703cdf2011-03-07 14:52:59 -0800797 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800798 // See setPosition() regarding setting parameters such as loop points or position while active
799 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700800 return INVALID_OPERATION;
801 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800802 mNewPosition = mUpdatePeriod;
803 mLoopPeriod = 0;
804 // FIXME The new code cannot reload while keeping a loop specified.
805 // Need to check how the old code handled this, and whether it's a significant change.
806 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800807 return NO_ERROR;
808}
809
Eric Laurentc2f1f072009-07-17 12:17:14 -0700810audio_io_handle_t AudioTrack::getOutput()
811{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800812 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100813 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800814}
815
816// must be called with mLock held
817audio_io_handle_t AudioTrack::getOutput_l()
818{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100819 if (mOutput) {
820 return mOutput;
821 } else {
822 return AudioSystem::getOutput(mStreamType,
823 mSampleRate, mFormat, mChannelMask, mFlags);
824 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700825}
826
Eric Laurentbe916aa2010-06-01 23:49:17 -0700827status_t AudioTrack::attachAuxEffect(int effectId)
828{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800829 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700830 status_t status = mAudioTrack->attachAuxEffect(effectId);
831 if (status == NO_ERROR) {
832 mAuxEffectId = effectId;
833 }
834 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700835}
836
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800837// -------------------------------------------------------------------------
838
Eric Laurent1703cdf2011-03-07 14:52:59 -0800839// must be called with mLock held
840status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800841 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800842 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800843 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800844 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700845 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800846 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800847 audio_io_handle_t output,
848 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800849{
850 status_t status;
851 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
852 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700853 ALOGE("Could not get audioflinger");
854 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800855 }
856
Glenn Kastence8828a2013-09-16 18:07:38 -0700857 // Not all of these values are needed under all conditions, but it is easier to get them all
858
Eric Laurentd1b449a2010-05-14 03:26:45 -0700859 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700860 status = AudioSystem::getLatency(output, streamType, &afLatency);
861 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800862 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700863 return NO_INIT;
864 }
865
Glenn Kastence8828a2013-09-16 18:07:38 -0700866 size_t afFrameCount;
867 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
868 if (status != NO_ERROR) {
869 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
870 return NO_INIT;
871 }
872
873 uint32_t afSampleRate;
874 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
875 if (status != NO_ERROR) {
876 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
877 return NO_INIT;
878 }
879
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700880 // Client decides whether the track is TIMED (see below), but can only express a preference
881 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700882 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700883 // either of these use cases:
884 // use case 1: shared buffer
885 (sharedBuffer != 0) ||
886 // use case 2: callback handler
887 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800888 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700889 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700890 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700891 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700892 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700893 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700894
Glenn Kastence8828a2013-09-16 18:07:38 -0700895 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800896 // n = 1 fast track with single buffering; nBuffering is ignored
897 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700898 // n = 2 normal track, no sample rate conversion
899 // n = 3 normal track, with sample rate conversion
900 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
901 // n > 3 very high latency or very small notification interval; nBuffering is ignored
902 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
903
Eric Laurentd1b449a2010-05-14 03:26:45 -0700904 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700905
Dima Zavinfce7a472011-04-19 22:30:36 -0700906 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700907
Eric Laurentd1b449a2010-05-14 03:26:45 -0700908 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700909 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700910 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700911 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700913 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100914 if (mNotificationFramesAct != frameCount) {
915 mNotificationFramesAct = frameCount;
916 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700917 } else if (sharedBuffer != 0) {
918
Glenn Kastena42ff002012-11-14 12:47:55 -0800919 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920 // 8-bit data in shared memory is not currently supported by AudioFlinger
921 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800922 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700923 // More than 2 channels does not require stronger alignment than stereo
924 alignment <<= 1;
925 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800926 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
927 ALOGE("Invalid buffer alignment: address %p, channel count %u",
928 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700929 return BAD_VALUE;
930 }
931
932 // When initializing a shared buffer AudioTrack via constructors,
933 // there's no frameCount parameter.
934 // But when initializing a shared buffer AudioTrack via set(),
935 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800936 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700937
938 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
939
940 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700941
Eric Laurentd1b449a2010-05-14 03:26:45 -0700942 // Ensure that buffer depth covers at least audio hardware latency
943 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700944 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
945 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700946 if (minBufCount <= nBuffering) {
947 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800948 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700949
Glenn Kastene33054e2012-11-14 12:54:39 -0800950 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
951 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800952 ", afLatency=%d",
953 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700954
955 if (frameCount == 0) {
956 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700957 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700958 // not ALOGW because it happens all the time when playing key clicks over A2DP
959 ALOGV("Minimum buffer size corrected from %d to %d",
960 frameCount, minFrameCount);
961 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800962 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700963 // Make sure that application is notified with sufficient margin before underrun
964 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
965 mNotificationFramesAct = frameCount/nBuffering;
966 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700967
Glenn Kastene0fa4672012-04-24 14:35:14 -0700968 } else {
969 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700970 }
971
Glenn Kastena075db42012-03-06 11:22:44 -0800972 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
973 if (mIsTimed) {
974 trackFlags |= IAudioFlinger::TRACK_TIMED;
975 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800976
977 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700978 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700979 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800980 if (mAudioTrackThread != 0) {
981 tid = mAudioTrackThread->getTid();
982 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700983 }
984
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100985 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
986 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
987 }
988
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800989 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800990 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700991 // AudioFlinger only sees 16-bit PCM
992 format == AUDIO_FORMAT_PCM_8_BIT ?
993 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800994 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800995 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800996 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800997 sharedBuffer,
998 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800999 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001000 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001001 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001002 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001003 &status);
1004
1005 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001006 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001007 return status;
1008 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001009 sp<IMemory> iMem = track->getCblk();
1010 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001011 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001012 return NO_INIT;
1013 }
Glenn Kasten0cde0762014-01-16 15:06:36 -08001014 void *iMemPointer = iMem->pointer();
1015 if (iMemPointer == NULL) {
1016 ALOGE("Could not get control block pointer");
1017 return NO_INIT;
1018 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001019 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001020 if (mAudioTrack != 0) {
1021 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1022 mDeathNotifier.clear();
1023 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001024 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001025 mCblkMemory = iMem;
Glenn Kasten0cde0762014-01-16 15:06:36 -08001026 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001027 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001028 size_t temp = cblk->frameCount_;
1029 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1030 // In current design, AudioTrack client checks and ensures frame count validity before
1031 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1032 // for fast track as it uses a special method of assigning frame count.
1033 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1034 }
1035 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001036 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001037 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001038 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001039 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001040 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001041 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001042 // Theoretically double-buffering is not required for fast tracks,
1043 // due to tighter scheduling. But in practice, to accommodate kernels with
1044 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1045 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1046 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001047 }
1048 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001049 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001050 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001051 // once denied, do not request again if IAudioTrack is re-created
1052 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1053 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001054 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001055 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1056 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001057 }
1058 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001059 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001060 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001061 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1062 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1063 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1064 } else {
1065 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1066 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1067 mFlags = flags;
1068 return NO_INIT;
1069 }
1070 }
1071
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001072 mRefreshRemaining = true;
1073
1074 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1075 // is the value of pointer() for the shared buffer, otherwise buffers points
1076 // immediately after the control block. This address is for the mapping within client
1077 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1078 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001079 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001080 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001081 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001082 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001083 }
1084
Eric Laurent2beeb502010-07-16 07:43:46 -07001085 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001086 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001087 mLatency = afLatency + (1000*frameCount) / sampleRate;
1088 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001089 // If IAudioTrack is re-created, don't let the requested frameCount
1090 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001091 if (frameCount > mReqFrameCount) {
1092 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001093 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001094
1095 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001096 if (sharedBuffer == 0) {
1097 mStaticProxy.clear();
1098 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1099 } else {
1100 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1101 mProxy = mStaticProxy;
1102 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001103 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1104 uint16_t(mVolume[LEFT] * 0x1000));
1105 mProxy->setSendLevel(mSendLevel);
1106 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001107 mProxy->setEpoch(epoch);
1108 mProxy->setMinimum(mNotificationFramesAct);
1109
1110 mDeathNotifier = new DeathNotifier(this);
1111 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001112
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001113 return NO_ERROR;
1114}
1115
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1117{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001118 if (audioBuffer == NULL) {
1119 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001120 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001121 if (mTransfer != TRANSFER_OBTAIN) {
1122 audioBuffer->frameCount = 0;
1123 audioBuffer->size = 0;
1124 audioBuffer->raw = NULL;
1125 return INVALID_OPERATION;
1126 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001127
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001128 const struct timespec *requested;
1129 if (waitCount == -1) {
1130 requested = &ClientProxy::kForever;
1131 } else if (waitCount == 0) {
1132 requested = &ClientProxy::kNonBlocking;
1133 } else if (waitCount > 0) {
1134 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1135 struct timespec timeout;
1136 timeout.tv_sec = ms / 1000;
1137 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1138 requested = &timeout;
1139 } else {
1140 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1141 requested = NULL;
1142 }
1143 return obtainBuffer(audioBuffer, requested);
1144}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001145
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001146status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1147 struct timespec *elapsed, size_t *nonContig)
1148{
1149 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1150 uint32_t oldSequence = 0;
1151 uint32_t newSequence;
1152
1153 Proxy::Buffer buffer;
1154 status_t status = NO_ERROR;
1155
1156 static const int32_t kMaxTries = 5;
1157 int32_t tryCounter = kMaxTries;
1158
1159 do {
1160 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1161 // keep them from going away if another thread re-creates the track during obtainBuffer()
1162 sp<AudioTrackClientProxy> proxy;
1163 sp<IMemory> iMem;
1164
1165 { // start of lock scope
1166 AutoMutex lock(mLock);
1167
1168 newSequence = mSequence;
1169 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1170 if (status == DEAD_OBJECT) {
1171 // re-create track, unless someone else has already done so
1172 if (newSequence == oldSequence) {
1173 status = restoreTrack_l("obtainBuffer");
1174 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001175 buffer.mFrameCount = 0;
1176 buffer.mRaw = NULL;
1177 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001178 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001179 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001180 }
1181 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001182 oldSequence = newSequence;
1183
1184 // Keep the extra references
1185 proxy = mProxy;
1186 iMem = mCblkMemory;
1187
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001188 if (mState == STATE_STOPPING) {
1189 status = -EINTR;
1190 buffer.mFrameCount = 0;
1191 buffer.mRaw = NULL;
1192 buffer.mNonContig = 0;
1193 break;
1194 }
1195
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001196 // Non-blocking if track is stopped or paused
1197 if (mState != STATE_ACTIVE) {
1198 requested = &ClientProxy::kNonBlocking;
1199 }
1200
1201 } // end of lock scope
1202
1203 buffer.mFrameCount = audioBuffer->frameCount;
1204 // FIXME starts the requested timeout and elapsed over from scratch
1205 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1206
1207 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1208
1209 audioBuffer->frameCount = buffer.mFrameCount;
1210 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1211 audioBuffer->raw = buffer.mRaw;
1212 if (nonContig != NULL) {
1213 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001215 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216}
1217
1218void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1219{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001220 if (mTransfer == TRANSFER_SHARED) {
1221 return;
1222 }
1223
1224 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1225 if (stepCount == 0) {
1226 return;
1227 }
1228
1229 Proxy::Buffer buffer;
1230 buffer.mFrameCount = stepCount;
1231 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001232
Eric Laurent1703cdf2011-03-07 14:52:59 -08001233 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001234 mInUnderrun = false;
1235 mProxy->releaseBuffer(&buffer);
1236
1237 // restart track if it was disabled by audioflinger due to previous underrun
1238 if (mState == STATE_ACTIVE) {
1239 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001240 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001241 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1242 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001243 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001244 mAudioTrack->start();
1245 }
1246 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001247}
1248
1249// -------------------------------------------------------------------------
1250
1251ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1252{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001253 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001254 return INVALID_OPERATION;
1255 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001257 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001258 // Sanity-check: user is most-likely passing an error code, and it would
1259 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001260 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261 return BAD_VALUE;
1262 }
1263
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001264 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 Buffer audioBuffer;
1266
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 while (userSize >= mFrameSize) {
1268 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001269
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001270 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001272 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001274 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001275 return ssize_t(err);
1276 }
1277
1278 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001279 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001281 toWrite = audioBuffer.size >> 1;
1282 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001283 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001285 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001286 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001287 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288 userSize -= toWrite;
1289 written += toWrite;
1290
1291 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001292 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293
1294 return written;
1295}
1296
1297// -------------------------------------------------------------------------
1298
John Grossman4ff14ba2012-02-08 16:37:41 -08001299TimedAudioTrack::TimedAudioTrack() {
1300 mIsTimed = true;
1301}
1302
1303status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1304{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001305 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001306 status_t result = UNKNOWN_ERROR;
1307
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001308#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001309 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1310 // while we are accessing the cblk
1311 sp<IAudioTrack> audioTrack = mAudioTrack;
1312 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001313#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001314
John Grossman4ff14ba2012-02-08 16:37:41 -08001315 // If the track is not invalid already, try to allocate a buffer. alloc
1316 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001317 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001318 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001319 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001320 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1321 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001322 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001323 }
1324 }
1325
1326 // If the track is invalid at this point, attempt to restore it. and try the
1327 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001328 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001329 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001330
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001331 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001332 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001333 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001334 }
1335
1336 return result;
1337}
1338
1339status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1340 int64_t pts)
1341{
Eric Laurentdf839842012-05-31 14:27:14 -07001342 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1343 {
1344 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001345 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001346 // restart track if it was disabled by audioflinger due to previous underrun
1347 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001348 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1349 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001350 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001351 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001352 mAudioTrack->start();
1353 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001354 }
Eric Laurentdf839842012-05-31 14:27:14 -07001355 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001356}
1357
1358status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1359 TargetTimeline target)
1360{
1361 return mAudioTrack->setMediaTimeTransform(xform, target);
1362}
1363
1364// -------------------------------------------------------------------------
1365
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001366nsecs_t AudioTrack::processAudioBuffer()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001367{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001368 // Currently the AudioTrack thread is not created if there are no callbacks.
1369 // Would it ever make sense to run the thread, even without callbacks?
1370 // If so, then replace this by checks at each use for mCbf != NULL.
1371 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1372
Eric Laurent1703cdf2011-03-07 14:52:59 -08001373 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001374 if (mAwaitBoost) {
1375 mAwaitBoost = false;
1376 mLock.unlock();
1377 static const int32_t kMaxTries = 5;
1378 int32_t tryCounter = kMaxTries;
1379 uint32_t pollUs = 10000;
1380 do {
1381 int policy = sched_getscheduler(0);
1382 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1383 break;
1384 }
1385 usleep(pollUs);
1386 pollUs <<= 1;
1387 } while (tryCounter-- > 0);
1388 if (tryCounter < 0) {
1389 ALOGE("did not receive expected priority boost on time");
1390 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001391 // Run again immediately
1392 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001393 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001394
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001395 // Can only reference mCblk while locked
1396 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001397 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001398
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001399 // Check for track invalidation
1400 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001401 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1402 // AudioSystem cache. We should not exit here but after calling the callback so
1403 // that the upper layers can recreate the track
Glenn Kasten23a75452014-01-13 10:37:17 -08001404 if (!isOffloaded_l() || (mSequence == mObservedSequence)) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001405 status_t status = restoreTrack_l("processAudioBuffer");
1406 mLock.unlock();
1407 // Run again immediately, but with a new IAudioTrack
1408 return 0;
1409 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001410 }
1411
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001412 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001413 bool active = mState == STATE_ACTIVE;
1414
1415 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1416 bool newUnderrun = false;
1417 if (flags & CBLK_UNDERRUN) {
1418#if 0
1419 // Currently in shared buffer mode, when the server reaches the end of buffer,
1420 // the track stays active in continuous underrun state. It's up to the application
1421 // to pause or stop the track, or set the position to a new offset within buffer.
1422 // This was some experimental code to auto-pause on underrun. Keeping it here
1423 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1424 if (mTransfer == TRANSFER_SHARED) {
1425 mState = STATE_PAUSED;
1426 active = false;
1427 }
1428#endif
1429 if (!mInUnderrun) {
1430 mInUnderrun = true;
1431 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432 }
1433 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001434
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001435 // Get current position of server
1436 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437
1438 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001439 bool markerReached = false;
1440 size_t markerPosition = mMarkerPosition;
1441 // FIXME fails for wraparound, need 64 bits
1442 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1443 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444 }
1445
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001446 // Determine number of new position callback(s) that will be needed, while locked
1447 size_t newPosCount = 0;
1448 size_t newPosition = mNewPosition;
1449 size_t updatePeriod = mUpdatePeriod;
1450 // FIXME fails for wraparound, need 64 bits
1451 if (updatePeriod > 0 && position >= newPosition) {
1452 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1453 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 }
1455
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001456 // Cache other fields that will be needed soon
1457 uint32_t loopPeriod = mLoopPeriod;
1458 uint32_t sampleRate = mSampleRate;
1459 size_t notificationFrames = mNotificationFramesAct;
1460 if (mRefreshRemaining) {
1461 mRefreshRemaining = false;
1462 mRemainingFrames = notificationFrames;
1463 mRetryOnPartialBuffer = false;
1464 }
1465 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001466 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001467
1468 // These fields don't need to be cached, because they are assigned only by set():
1469 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1470 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1471
1472 mLock.unlock();
1473
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001474 if (waitStreamEnd) {
1475 AutoMutex lock(mLock);
1476
1477 sp<AudioTrackClientProxy> proxy = mProxy;
1478 sp<IMemory> iMem = mCblkMemory;
1479
1480 struct timespec timeout;
1481 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1482 timeout.tv_nsec = 0;
1483
1484 mLock.unlock();
1485 status_t status = mProxy->waitStreamEndDone(&timeout);
1486 mLock.lock();
1487 switch (status) {
1488 case NO_ERROR:
1489 case DEAD_OBJECT:
1490 case TIMED_OUT:
1491 mLock.unlock();
1492 mCbf(EVENT_STREAM_END, mUserData, NULL);
1493 mLock.lock();
1494 if (mState == STATE_STOPPING) {
1495 mState = STATE_STOPPED;
1496 if (status != DEAD_OBJECT) {
1497 return NS_INACTIVE;
1498 }
1499 }
1500 return 0;
1501 default:
1502 return 0;
1503 }
1504 }
1505
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001506 // perform callbacks while unlocked
1507 if (newUnderrun) {
1508 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1509 }
1510 // FIXME we will miss loops if loop cycle was signaled several times since last call
1511 // to processAudioBuffer()
1512 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1513 mCbf(EVENT_LOOP_END, mUserData, NULL);
1514 }
1515 if (flags & CBLK_BUFFER_END) {
1516 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1517 }
1518 if (markerReached) {
1519 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1520 }
1521 while (newPosCount > 0) {
1522 size_t temp = newPosition;
1523 mCbf(EVENT_NEW_POS, mUserData, &temp);
1524 newPosition += updatePeriod;
1525 newPosCount--;
1526 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001527
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001528 if (mObservedSequence != sequence) {
1529 mObservedSequence = sequence;
1530 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001531 // for offloaded tracks, just wait for the upper layers to recreate the track
1532 if (isOffloaded()) {
1533 return NS_INACTIVE;
1534 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001535 }
1536
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001537 // if inactive, then don't run me again until re-started
1538 if (!active) {
1539 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001540 }
1541
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001542 // Compute the estimated time until the next timed event (position, markers, loops)
1543 // FIXME only for non-compressed audio
1544 uint32_t minFrames = ~0;
1545 if (!markerReached && position < markerPosition) {
1546 minFrames = markerPosition - position;
1547 }
1548 if (loopPeriod > 0 && loopPeriod < minFrames) {
1549 minFrames = loopPeriod;
1550 }
1551 if (updatePeriod > 0 && updatePeriod < minFrames) {
1552 minFrames = updatePeriod;
1553 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001554
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001555 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1556 static const uint32_t kPoll = 0;
1557 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1558 minFrames = kPoll * notificationFrames;
1559 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001560
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001561 // Convert frame units to time units
1562 nsecs_t ns = NS_WHENEVER;
1563 if (minFrames != (uint32_t) ~0) {
1564 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1565 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1566 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1567 }
1568
1569 // If not supplying data by EVENT_MORE_DATA, then we're done
1570 if (mTransfer != TRANSFER_CALLBACK) {
1571 return ns;
1572 }
1573
1574 struct timespec timeout;
1575 const struct timespec *requested = &ClientProxy::kForever;
1576 if (ns != NS_WHENEVER) {
1577 timeout.tv_sec = ns / 1000000000LL;
1578 timeout.tv_nsec = ns % 1000000000LL;
1579 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1580 requested = &timeout;
1581 }
1582
1583 while (mRemainingFrames > 0) {
1584
1585 Buffer audioBuffer;
1586 audioBuffer.frameCount = mRemainingFrames;
1587 size_t nonContig;
1588 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1589 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1590 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1591 requested = &ClientProxy::kNonBlocking;
1592 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001593 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1594 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001595 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001596 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1597 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001598 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001600 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1601 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001602 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001603
Eric Laurent42a6f422013-08-29 14:35:05 -07001604 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001605 mRetryOnPartialBuffer = false;
1606 if (avail < mRemainingFrames) {
1607 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1608 if (ns < 0 || myns < ns) {
1609 ns = myns;
1610 }
1611 return ns;
1612 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001613 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001614
1615 // Divide buffer size by 2 to take into account the expansion
1616 // due to 8 to 16 bit conversion: the callback must fill only half
1617 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001618 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001619 audioBuffer.size >>= 1;
1620 }
1621
1622 size_t reqSize = audioBuffer.size;
1623 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001624 size_t writtenSize = audioBuffer.size;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001625
1626 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001627 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1628 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1629 reqSize, (int) writtenSize);
1630 return NS_NEVER;
1631 }
1632
1633 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001634 // The callback is done filling buffers
1635 // Keep this thread going to handle timed events and
1636 // still try to get more data in intervals of WAIT_PERIOD_MS
1637 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001638 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001639 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001640
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001641 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001642 // 8 to 16 bit conversion, note that source and destination are the same address
1643 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001644 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645 }
1646
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001647 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1648 audioBuffer.frameCount = releasedFrames;
1649 mRemainingFrames -= releasedFrames;
1650 if (misalignment >= releasedFrames) {
1651 misalignment -= releasedFrames;
1652 } else {
1653 misalignment = 0;
1654 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001655
1656 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001657
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001658 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1659 // if callback doesn't like to accept the full chunk
1660 if (writtenSize < reqSize) {
1661 continue;
1662 }
1663
1664 // There could be enough non-contiguous frames available to satisfy the remaining request
1665 if (mRemainingFrames <= nonContig) {
1666 continue;
1667 }
1668
1669#if 0
1670 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1671 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1672 // that total to a sum == notificationFrames.
1673 if (0 < misalignment && misalignment <= mRemainingFrames) {
1674 mRemainingFrames = misalignment;
1675 return (mRemainingFrames * 1100000000LL) / sampleRate;
1676 }
1677#endif
1678
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001680 mRemainingFrames = notificationFrames;
1681 mRetryOnPartialBuffer = true;
1682
1683 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1684 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001685}
1686
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001687status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001688{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001689 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
Glenn Kasten23a75452014-01-13 10:37:17 -08001690 isOffloaded_l() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001691 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001692 status_t result;
1693
Glenn Kastena47f3162012-11-07 10:13:08 -08001694 // refresh the audio configuration cache in this process to make sure we get new
1695 // output parameters in getOutput_l() and createTrack_l()
1696 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001697
Glenn Kasten23a75452014-01-13 10:37:17 -08001698 if (isOffloaded_l()) {
1699 // FIXME re-creation of offloaded tracks is not yet implemented
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001700 return DEAD_OBJECT;
1701 }
1702
1703 // force new output query from audio policy manager;
1704 mOutput = 0;
1705 audio_io_handle_t output = getOutput_l();
1706
Glenn Kastena47f3162012-11-07 10:13:08 -08001707 // if the new IAudioTrack is created, createTrack_l() will modify the
1708 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1709 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001710
1711 // take the frames that will be lost by track recreation into account in saved position
1712 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001713 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001714 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001715 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001716 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001717 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001718 mFlags,
1719 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001720 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001721 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001722
Glenn Kastena47f3162012-11-07 10:13:08 -08001723 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001724 // continue playback from last known position, but
1725 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1726 if (mStaticProxy != NULL) {
1727 mLoopPeriod = 0;
1728 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1729 }
1730 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1731 // track destruction have been played? This is critical for SoundPool implementation
1732 // This must be broken, and needs to be tested/debugged.
1733#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001734 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001735 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001736 // Make sure that a client relying on callback events indicating underrun or
1737 // the actual amount of audio frames played (e.g SoundPool) receives them.
1738 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001739 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001740 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001741 }
1742 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001743#endif
1744 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001745 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001746 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001747 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001748 if (result != NO_ERROR) {
Glenn Kasten2b2165c2014-01-13 08:53:36 -08001749 // Use of direct and offloaded output streams is ref counted by audio policy manager.
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001750 // As getOutput was called above and resulted in an output stream to be opened,
1751 // we need to release it.
1752 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001753 ALOGW("restoreTrack_l() failed status %d", result);
1754 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001755 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001756
1757 return result;
1758}
1759
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001760status_t AudioTrack::setParameters(const String8& keyValuePairs)
1761{
1762 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001763 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001764}
1765
Glenn Kastence703742013-07-19 16:33:58 -07001766status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1767{
Glenn Kasten53cec222013-08-29 09:01:02 -07001768 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001769 // FIXME not implemented for fast tracks; should use proxy and SSQ
1770 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1771 return INVALID_OPERATION;
1772 }
1773 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1774 return INVALID_OPERATION;
1775 }
1776 status_t status = mAudioTrack->getTimestamp(timestamp);
1777 if (status == NO_ERROR) {
1778 timestamp.mPosition += mProxy->getEpoch();
1779 }
1780 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001781}
1782
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001783String8 AudioTrack::getParameters(const String8& keys)
1784{
Glenn Kasten2c6c5292014-01-13 10:29:08 -08001785 audio_io_handle_t output = getOutput();
1786 if (output != 0) {
1787 return AudioSystem::getParameters(output, keys);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001788 } else {
1789 return String8::empty();
1790 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001791}
1792
Glenn Kasten23a75452014-01-13 10:37:17 -08001793bool AudioTrack::isOffloaded() const
1794{
1795 AutoMutex lock(mLock);
1796 return isOffloaded_l();
1797}
1798
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001799status_t AudioTrack::dump(int fd, const Vector<String16>& args __unused) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001800{
1801
1802 const size_t SIZE = 256;
1803 char buffer[SIZE];
1804 String8 result;
1805
1806 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001807 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1808 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001810 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001811 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001813 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001814 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001815 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001816 result.append(buffer);
1817 ::write(fd, result.string(), result.size());
1818 return NO_ERROR;
1819}
1820
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001821uint32_t AudioTrack::getUnderrunFrames() const
1822{
1823 AutoMutex lock(mLock);
1824 return mProxy->getUnderrunFrames();
1825}
1826
1827// =========================================================================
1828
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001829void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001830{
1831 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1832 if (audioTrack != 0) {
1833 AutoMutex lock(audioTrack->mLock);
1834 audioTrack->mProxy->binderDied();
1835 }
1836}
1837
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001838// =========================================================================
1839
1840AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001841 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1842 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001843{
1844}
1845
1846AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001847{
1848}
1849
1850bool AudioTrack::AudioTrackThread::threadLoop()
1851{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001852 {
1853 AutoMutex _l(mMyLock);
1854 if (mPaused) {
1855 mMyCond.wait(mMyLock);
1856 // caller will check for exitPending()
1857 return true;
1858 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001859 if (mIgnoreNextPausedInt) {
1860 mIgnoreNextPausedInt = false;
1861 mPausedInt = false;
1862 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001863 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001864 if (mPausedNs > 0) {
1865 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1866 } else {
1867 mMyCond.wait(mMyLock);
1868 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001869 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001870 return true;
1871 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001872 }
Glenn Kasten7c7be1e2013-12-19 16:34:04 -08001873 nsecs_t ns = mReceiver.processAudioBuffer();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001874 switch (ns) {
1875 case 0:
1876 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001877 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001878 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001879 return true;
1880 case NS_NEVER:
1881 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001882 case NS_WHENEVER:
1883 // FIXME increase poll interval, or make event-driven
1884 ns = 1000000000LL;
1885 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001886 default:
1887 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001888 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001889 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001890 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001891}
1892
Glenn Kasten3acbd052012-02-28 10:39:56 -08001893void AudioTrack::AudioTrackThread::requestExit()
1894{
1895 // must be in this order to avoid a race condition
1896 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001897 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001898}
1899
1900void AudioTrack::AudioTrackThread::pause()
1901{
1902 AutoMutex _l(mMyLock);
1903 mPaused = true;
1904}
1905
1906void AudioTrack::AudioTrackThread::resume()
1907{
1908 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001909 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001910 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001911 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001912 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001913 mMyCond.signal();
1914 }
1915}
1916
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001917void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1918{
1919 AutoMutex _l(mMyLock);
1920 mPausedInt = true;
1921 mPausedNs = ns;
1922}
1923
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001924}; // namespace android