blob: 74bb7a62e320399a5bcbb86878bcc4cdf13e0cbd [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080023#include <audio_utils/primitives.h>
24#include <binder/IPCThreadState.h>
25#include <media/AudioTrack.h>
26#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010030#define WAIT_PERIOD_MS 10
31#define WAIT_STREAM_END_TIMEOUT_SEC 120
32
Glenn Kasten511754b2012-01-11 09:52:19 -080033
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080035// ---------------------------------------------------------------------------
36
37// static
38status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080039 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080040 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080041 uint32_t sampleRate)
42{
Glenn Kastend65d73c2012-06-22 17:21:07 -070043 if (frameCount == NULL) {
44 return BAD_VALUE;
45 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070046
47 // default to 0 in case of error
48 *frameCount = 0;
49
Glenn Kastene0fa4672012-04-24 14:35:14 -070050 // FIXME merge with similar code in createTrack_l(), except we're missing
51 // some information here that is available in createTrack_l():
52 // audio_io_handle_t output
53 // audio_format_t format
54 // audio_channel_mask_t channelMask
55 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080056 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080057 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
58 return NO_INIT;
59 }
Glenn Kastene33054e2012-11-14 12:54:39 -080060 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080061 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
62 return NO_INIT;
63 }
64 uint32_t afLatency;
65 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
66 return NO_INIT;
67 }
68
69 // Ensure that buffer depth covers at least audio hardware latency
70 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080071 if (minBufCount < 2) {
72 minBufCount = 2;
73 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080074
75 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070076 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080077 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
78 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080079 return NO_ERROR;
80}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080081
82// ---------------------------------------------------------------------------
83
84AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070085 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080086 mIsTimed(false),
87 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -080088 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080089{
90}
91
92AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080093 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080094 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080095 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -070096 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070098 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099 callback_t cbf,
100 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700101 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800102 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000103 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800104 const audio_offload_info_t *offloadInfo,
105 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700106 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800107 mIsTimed(false),
108 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800109 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700111 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700112 frameCount, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800113 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType,
114 offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115}
116
Andreas Huberc8139852012-01-18 10:51:55 -0800117AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800118 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800120 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700121 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800122 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700123 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800124 callback_t cbf,
125 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700126 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800127 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000128 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800129 const audio_offload_info_t *offloadInfo,
130 int uid)
Glenn Kasten87913512011-06-22 16:15:25 -0700131 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800132 mIsTimed(false),
133 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800134 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700136 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800137 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800138 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo, uid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139}
140
141AudioTrack::~AudioTrack()
142{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 if (mStatus == NO_ERROR) {
144 // Make sure that callback function exits in the case where
145 // it is looping on buffer full condition in obtainBuffer().
146 // Otherwise the callback thread will never exit.
147 stop();
148 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100149 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800150 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 mAudioTrackThread->requestExitAndWait();
152 mAudioTrackThread.clear();
153 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700154 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
155 mAudioTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700157 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158 }
159}
160
161status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800162 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800164 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700165 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800166 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700167 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 callback_t cbf,
169 void* user,
170 int notificationFrames,
171 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700172 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800173 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000174 transfer_type transferType,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800175 const audio_offload_info_t *offloadInfo,
176 int uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800178 switch (transferType) {
179 case TRANSFER_DEFAULT:
180 if (sharedBuffer != 0) {
181 transferType = TRANSFER_SHARED;
182 } else if (cbf == NULL || threadCanCallJava) {
183 transferType = TRANSFER_SYNC;
184 } else {
185 transferType = TRANSFER_CALLBACK;
186 }
187 break;
188 case TRANSFER_CALLBACK:
189 if (cbf == NULL || sharedBuffer != 0) {
190 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
191 return BAD_VALUE;
192 }
193 break;
194 case TRANSFER_OBTAIN:
195 case TRANSFER_SYNC:
196 if (sharedBuffer != 0) {
197 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
198 return BAD_VALUE;
199 }
200 break;
201 case TRANSFER_SHARED:
202 if (sharedBuffer == 0) {
203 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
204 return BAD_VALUE;
205 }
206 break;
207 default:
208 ALOGE("Invalid transfer type %d", transferType);
209 return BAD_VALUE;
210 }
211 mTransfer = transferType;
212
Glenn Kastene33054e2012-11-14 12:54:39 -0800213 // FIXME "int" here is legacy and will be replaced by size_t later
214 if (frameCountInt < 0) {
215 ALOGE("Invalid frame count %d", frameCountInt);
216 return BAD_VALUE;
217 }
218 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700220 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
221 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222
Glenn Kastene33054e2012-11-14 12:54:39 -0800223 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700224
Eric Laurent1703cdf2011-03-07 14:52:59 -0800225 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800226
Glenn Kasten53cec222013-08-29 09:01:02 -0700227 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700228 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000229 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 return INVALID_OPERATION;
231 }
232
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100233 mOutput = 0;
234
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700236 if (streamType == AUDIO_STREAM_DEFAULT) {
237 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700239
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800241 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700242 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
243 return NO_INIT;
244 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 sampleRate = afSampleRate;
246 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800247 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800250 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700251 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253
254 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700255 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800256 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257 return BAD_VALUE;
258 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259
Glenn Kasten8ba90322013-10-30 11:29:27 -0700260 if (!audio_is_output_channel(channelMask)) {
261 ALOGE("Invalid channel mask %#x", channelMask);
262 return BAD_VALUE;
263 }
264
Glenn Kastene0fa4672012-04-24 14:35:14 -0700265 // AudioFlinger does not currently support 8-bit data in shared memory
266 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
267 ALOGE("8-bit data in shared memory is not supported");
268 return BAD_VALUE;
269 }
270
Eric Laurentc2f1f072009-07-17 12:17:14 -0700271 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100272 // or offload was requested
273 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
274 || !audio_is_linear_pcm(format)) {
275 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
276 ? "Offload request, forcing to Direct Output"
277 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700278 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800279 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700280 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700282 // only allow deep buffering for music stream type
283 if (streamType != AUDIO_STREAM_MUSIC) {
284 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
285 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700286
Glenn Kastena42ff002012-11-14 12:47:55 -0800287 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700288 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800289 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290
Glenn Kastene3aa6592012-12-04 12:22:46 -0800291 if (audio_is_linear_pcm(format)) {
292 mFrameSize = channelCount * audio_bytes_per_sample(format);
293 mFrameSizeAF = channelCount * sizeof(int16_t);
294 } else {
295 mFrameSize = sizeof(uint8_t);
296 mFrameSizeAF = sizeof(uint8_t);
297 }
298
Dima Zavinfce7a472011-04-19 22:30:36 -0700299 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800300 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800301 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000302 flags,
303 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304
305 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000306 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 return BAD_VALUE;
308 }
309
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800310 mVolume[LEFT] = 1.0f;
311 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800312 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700313 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800314 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700315 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800316 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700317 mSessionId = sessionId;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800318 if (uid == -1 || (IPCThreadState::self()->getCallingPid() != getpid())) {
319 mClientUid = IPCThreadState::self()->getCallingUid();
320 } else {
321 mClientUid = uid;
322 }
Eric Laurent2beeb502010-07-16 07:43:46 -0700323 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700324 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700325 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700326
Glenn Kastena997e7a2012-08-07 09:44:19 -0700327 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700328 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700329 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
330 }
331
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800332 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800333 status_t status = createTrack_l(streamType,
334 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800335 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800336 frameCount,
337 flags,
338 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800339 output,
340 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800341
Glenn Kastena997e7a2012-08-07 09:44:19 -0700342 if (status != NO_ERROR) {
343 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100344 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
345 mAudioTrackThread->requestExitAndWait();
Glenn Kastena997e7a2012-08-07 09:44:19 -0700346 mAudioTrackThread.clear();
347 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100348 //Use of direct and offloaded output streams is ref counted by audio policy manager.
349 // As getOutput was called above and resulted in an output stream to be opened,
350 // we need to release it.
351 AudioSystem::releaseOutput(output);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700352 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700353 }
354
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800357 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800359 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800361 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700363 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364 mNewPosition = 0;
365 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700366 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800367 mSequence = 1;
368 mObservedSequence = mSequence;
369 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100370 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800371
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 return NO_ERROR;
373}
374
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375// -------------------------------------------------------------------------
376
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100377status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800379 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100380
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800381 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100382 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383 }
384
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800385 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800387 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100388 if (previousState == STATE_PAUSED_STOPPING) {
389 mState = STATE_STOPPING;
390 } else {
391 mState = STATE_ACTIVE;
392 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800393 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
394 // reset current position as seen by client to 0
395 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700396 // force refresh of remaining frames by processAudioBuffer() as last
397 // write before stop could be partial.
398 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800399 }
400 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700401 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800402
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800403 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800404 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100405 if (previousState == STATE_STOPPING) {
406 mProxy->interrupt();
407 } else {
408 t->resume();
409 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800410 } else {
411 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
412 get_sched_policy(0, &mPreviousSchedulingGroup);
413 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
414 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800416 status_t status = NO_ERROR;
417 if (!(flags & CBLK_INVALID)) {
418 status = mAudioTrack->start();
419 if (status == DEAD_OBJECT) {
420 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800422 }
423 if (flags & CBLK_INVALID) {
424 status = restoreTrack_l("start");
425 }
426
427 if (status != NO_ERROR) {
428 ALOGE("start() status %d", status);
429 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100431 if (previousState != STATE_STOPPING) {
432 t->pause();
433 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700435 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700436 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437 }
438 }
439
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100440 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800441}
442
443void AudioTrack::stop()
444{
445 AutoMutex lock(mLock);
Glenn Kasten397edb32013-08-30 15:10:13 -0700446 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800447 return;
448 }
449
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100450 if (isOffloaded()) {
451 mState = STATE_STOPPING;
452 } else {
453 mState = STATE_STOPPED;
454 }
455
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800456 mProxy->interrupt();
457 mAudioTrack->stop();
458 // the playback head position will reset to 0, so if a marker is set, we need
459 // to activate it again
460 mMarkerReached = false;
461#if 0
462 // Force flush if a shared buffer is used otherwise audioflinger
463 // will not stop before end of buffer is reached.
464 // It may be needed to make sure that we stop playback, likely in case looping is on.
465 if (mSharedBuffer != 0) {
466 flush_l();
467 }
468#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100469
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800470 sp<AudioTrackThread> t = mAudioTrackThread;
471 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100472 if (!isOffloaded()) {
473 t->pause();
474 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800475 } else {
476 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
477 set_sched_policy(0, mPreviousSchedulingGroup);
478 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479}
480
481bool AudioTrack::stopped() const
482{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800483 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800484 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485}
486
487void AudioTrack::flush()
488{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800489 if (mSharedBuffer != 0) {
490 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800491 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800492 AutoMutex lock(mLock);
493 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
494 return;
495 }
496 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800497}
498
Eric Laurent1703cdf2011-03-07 14:52:59 -0800499void AudioTrack::flush_l()
500{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800501 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700502
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700503 // clear playback marker and periodic update counter
504 mMarkerPosition = 0;
505 mMarkerReached = false;
506 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100507 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700508
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800509 mState = STATE_FLUSHED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100510 if (isOffloaded()) {
511 mProxy->interrupt();
512 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800513 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800514 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515}
516
517void AudioTrack::pause()
518{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800519 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100520 if (mState == STATE_ACTIVE) {
521 mState = STATE_PAUSED;
522 } else if (mState == STATE_STOPPING) {
523 mState = STATE_PAUSED_STOPPING;
524 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800525 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527 mProxy->interrupt();
528 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529}
530
Eric Laurentbe916aa2010-06-01 23:49:17 -0700531status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800533 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700534 return BAD_VALUE;
535 }
536
Eric Laurent1703cdf2011-03-07 14:52:59 -0800537 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 mVolume[LEFT] = left;
539 mVolume[RIGHT] = right;
540
Glenn Kastene3aa6592012-12-04 12:22:46 -0800541 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700542
Eric Laurent59fe0102013-09-27 18:48:26 -0700543 if (isOffloaded()) {
544 mAudioTrack->signal();
545 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700546 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547}
548
Glenn Kastenb1c09932012-02-27 16:21:04 -0800549status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800551 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700552}
553
Eric Laurent2beeb502010-07-16 07:43:46 -0700554status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700555{
Glenn Kasten05632a52012-01-03 14:22:33 -0800556 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700557 return BAD_VALUE;
558 }
559
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800560 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700561 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800562 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700563
564 return NO_ERROR;
565}
566
Glenn Kastena5224f32012-01-04 12:41:44 -0800567void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700568{
569 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800570 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700571 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572}
573
Glenn Kasten3b16c762012-11-14 08:44:39 -0800574status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100576 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800577 return INVALID_OPERATION;
578 }
579
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800580 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700582 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583 }
584 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700585 if (rate == 0 || rate > afSamplingRate*2 ) {
586 return BAD_VALUE;
587 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588
Eric Laurent1703cdf2011-03-07 14:52:59 -0800589 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800590 mSampleRate = rate;
591 mProxy->setSampleRate(rate);
592
Eric Laurent57326622009-07-07 07:10:45 -0700593 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800594}
595
Glenn Kastena5224f32012-01-04 12:41:44 -0800596uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800597{
John Grossman4ff14ba2012-02-08 16:37:41 -0800598 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800599 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800600 }
601
Eric Laurent1703cdf2011-03-07 14:52:59 -0800602 AutoMutex lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -0700603
604 // sample rate can be updated during playback by the offloaded decoder so we need to
605 // query the HAL and update if needed.
606// FIXME use Proxy return channel to update the rate from server and avoid polling here
607 if (isOffloaded()) {
608 if (mOutput != 0) {
609 uint32_t sampleRate = 0;
610 status_t status = AudioSystem::getSamplingRate(mOutput, mStreamType, &sampleRate);
611 if (status == NO_ERROR) {
612 mSampleRate = sampleRate;
613 }
614 }
615 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800616 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617}
618
619status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
620{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100621 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800622 return INVALID_OPERATION;
623 }
624
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800626 ;
627 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
628 loopEnd - loopStart >= MIN_LOOP) {
629 ;
630 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 return BAD_VALUE;
632 }
633
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800634 AutoMutex lock(mLock);
635 // See setPosition() regarding setting parameters such as loop points or position while active
636 if (mState == STATE_ACTIVE) {
637 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700638 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800639 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640 return NO_ERROR;
641}
642
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800643void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
644{
645 // FIXME If setting a loop also sets position to start of loop, then
646 // this is correct. Otherwise it should be removed.
647 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
648 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
649 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
650}
651
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652status_t AudioTrack::setMarkerPosition(uint32_t marker)
653{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700654 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100655 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700656 return INVALID_OPERATION;
657 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800658
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800659 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700661 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662
663 return NO_ERROR;
664}
665
Glenn Kastena5224f32012-01-04 12:41:44 -0800666status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100668 if (isOffloaded()) {
669 return INVALID_OPERATION;
670 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700671 if (marker == NULL) {
672 return BAD_VALUE;
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 *marker = mMarkerPosition;
677
678 return NO_ERROR;
679}
680
681status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
682{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700683 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100684 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700685 return INVALID_OPERATION;
686 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800688 AutoMutex lock(mLock);
689 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800690 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691 return NO_ERROR;
692}
693
Glenn Kastena5224f32012-01-04 12:41:44 -0800694status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100696 if (isOffloaded()) {
697 return INVALID_OPERATION;
698 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700699 if (updatePeriod == NULL) {
700 return BAD_VALUE;
701 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800703 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704 *updatePeriod = mUpdatePeriod;
705
706 return NO_ERROR;
707}
708
709status_t AudioTrack::setPosition(uint32_t position)
710{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100711 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700712 return INVALID_OPERATION;
713 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800714 if (position > mFrameCount) {
715 return BAD_VALUE;
716 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800717
Eric Laurent1703cdf2011-03-07 14:52:59 -0800718 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800719 // Currently we require that the player is inactive before setting parameters such as position
720 // or loop points. Otherwise, there could be a race condition: the application could read the
721 // current position, compute a new position or loop parameters, and then set that position or
722 // loop parameters but it would do the "wrong" thing since the position has continued to advance
723 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
724 // to specify how it wants to handle such scenarios.
725 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700726 return INVALID_OPERATION;
727 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800728 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
729 mLoopPeriod = 0;
730 // FIXME Check whether loops and setting position are incompatible in old code.
731 // If we use setLoop for both purposes we lose the capability to set the position while looping.
732 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700733
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800734 return NO_ERROR;
735}
736
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800737status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700739 if (position == NULL) {
740 return BAD_VALUE;
741 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800742
Eric Laurent1703cdf2011-03-07 14:52:59 -0800743 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100744 if (isOffloaded()) {
745 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100747 if (mOutput != 0) {
748 uint32_t halFrames;
749 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
750 }
751 *position = dspFrames;
752 } else {
753 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
754 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
755 mProxy->getPosition();
756 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800757 return NO_ERROR;
758}
759
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800760status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800761{
762 if (mSharedBuffer == 0 || mIsTimed) {
763 return INVALID_OPERATION;
764 }
765 if (position == NULL) {
766 return BAD_VALUE;
767 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800768
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800769 AutoMutex lock(mLock);
770 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800771 return NO_ERROR;
772}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800773
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800774status_t AudioTrack::reload()
775{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100776 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800777 return INVALID_OPERATION;
778 }
779
Eric Laurent1703cdf2011-03-07 14:52:59 -0800780 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800781 // See setPosition() regarding setting parameters such as loop points or position while active
782 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700783 return INVALID_OPERATION;
784 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800785 mNewPosition = mUpdatePeriod;
786 mLoopPeriod = 0;
787 // FIXME The new code cannot reload while keeping a loop specified.
788 // Need to check how the old code handled this, and whether it's a significant change.
789 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800790 return NO_ERROR;
791}
792
Eric Laurentc2f1f072009-07-17 12:17:14 -0700793audio_io_handle_t AudioTrack::getOutput()
794{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800795 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100796 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800797}
798
799// must be called with mLock held
800audio_io_handle_t AudioTrack::getOutput_l()
801{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100802 if (mOutput) {
803 return mOutput;
804 } else {
805 return AudioSystem::getOutput(mStreamType,
806 mSampleRate, mFormat, mChannelMask, mFlags);
807 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700808}
809
Eric Laurentbe916aa2010-06-01 23:49:17 -0700810status_t AudioTrack::attachAuxEffect(int effectId)
811{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800812 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700813 status_t status = mAudioTrack->attachAuxEffect(effectId);
814 if (status == NO_ERROR) {
815 mAuxEffectId = effectId;
816 }
817 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700818}
819
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800820// -------------------------------------------------------------------------
821
Eric Laurent1703cdf2011-03-07 14:52:59 -0800822// must be called with mLock held
823status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800824 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800825 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800826 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800827 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700828 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800829 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800830 audio_io_handle_t output,
831 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800832{
833 status_t status;
834 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
835 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700836 ALOGE("Could not get audioflinger");
837 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800838 }
839
Glenn Kastence8828a2013-09-16 18:07:38 -0700840 // Not all of these values are needed under all conditions, but it is easier to get them all
841
Eric Laurentd1b449a2010-05-14 03:26:45 -0700842 uint32_t afLatency;
Glenn Kastence8828a2013-09-16 18:07:38 -0700843 status = AudioSystem::getLatency(output, streamType, &afLatency);
844 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800845 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700846 return NO_INIT;
847 }
848
Glenn Kastence8828a2013-09-16 18:07:38 -0700849 size_t afFrameCount;
850 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
851 if (status != NO_ERROR) {
852 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
853 return NO_INIT;
854 }
855
856 uint32_t afSampleRate;
857 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
858 if (status != NO_ERROR) {
859 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
860 return NO_INIT;
861 }
862
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700863 // Client decides whether the track is TIMED (see below), but can only express a preference
864 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700865 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700866 // either of these use cases:
867 // use case 1: shared buffer
868 (sharedBuffer != 0) ||
869 // use case 2: callback handler
870 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800871 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700872 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700873 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700874 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700875 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700876 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700877
Glenn Kastence8828a2013-09-16 18:07:38 -0700878 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
Glenn Kastenb5fed682013-12-03 09:06:43 -0800879 // n = 1 fast track with single buffering; nBuffering is ignored
880 // n = 2 fast track with double buffering
Glenn Kastence8828a2013-09-16 18:07:38 -0700881 // n = 2 normal track, no sample rate conversion
882 // n = 3 normal track, with sample rate conversion
883 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
884 // n > 3 very high latency or very small notification interval; nBuffering is ignored
885 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
886
Eric Laurentd1b449a2010-05-14 03:26:45 -0700887 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700888
Dima Zavinfce7a472011-04-19 22:30:36 -0700889 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700890
Eric Laurentd1b449a2010-05-14 03:26:45 -0700891 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700892 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700893 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700894 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700895 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700896 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100897 if (mNotificationFramesAct != frameCount) {
898 mNotificationFramesAct = frameCount;
899 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700900 } else if (sharedBuffer != 0) {
901
Glenn Kastena42ff002012-11-14 12:47:55 -0800902 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700903 // 8-bit data in shared memory is not currently supported by AudioFlinger
904 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800905 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700906 // More than 2 channels does not require stronger alignment than stereo
907 alignment <<= 1;
908 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800909 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
910 ALOGE("Invalid buffer alignment: address %p, channel count %u",
911 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700912 return BAD_VALUE;
913 }
914
915 // When initializing a shared buffer AudioTrack via constructors,
916 // there's no frameCount parameter.
917 // But when initializing a shared buffer AudioTrack via set(),
918 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800919 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920
921 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
922
923 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -0700924
Eric Laurentd1b449a2010-05-14 03:26:45 -0700925 // Ensure that buffer depth covers at least audio hardware latency
926 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700927 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
928 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -0700929 if (minBufCount <= nBuffering) {
930 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -0800931 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700932
Glenn Kastene33054e2012-11-14 12:54:39 -0800933 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
934 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800935 ", afLatency=%d",
936 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700937
938 if (frameCount == 0) {
939 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -0700940 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700941 // not ALOGW because it happens all the time when playing key clicks over A2DP
942 ALOGV("Minimum buffer size corrected from %d to %d",
943 frameCount, minFrameCount);
944 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800945 }
Glenn Kastence8828a2013-09-16 18:07:38 -0700946 // Make sure that application is notified with sufficient margin before underrun
947 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
948 mNotificationFramesAct = frameCount/nBuffering;
949 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700950
Glenn Kastene0fa4672012-04-24 14:35:14 -0700951 } else {
952 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700953 }
954
Glenn Kastena075db42012-03-06 11:22:44 -0800955 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
956 if (mIsTimed) {
957 trackFlags |= IAudioFlinger::TRACK_TIMED;
958 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800959
960 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700961 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700962 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800963 if (mAudioTrackThread != 0) {
964 tid = mAudioTrackThread->getTid();
965 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700966 }
967
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100968 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
969 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
970 }
971
Glenn Kasten74935e42013-12-19 08:56:45 -0800972 size_t temp = frameCount; // temp may be replaced by a revised value of frameCount,
973 // but we will still need the original value also
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800974 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800975 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700976 // AudioFlinger only sees 16-bit PCM
977 format == AUDIO_FORMAT_PCM_8_BIT ?
978 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800979 mChannelMask,
Glenn Kasten74935e42013-12-19 08:56:45 -0800980 &temp,
Glenn Kastene0b07172012-11-06 15:03:34 -0800981 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800982 sharedBuffer,
983 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800984 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700985 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -0700986 mName,
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800987 mClientUid,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800988 &status);
989
990 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000991 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800992 return status;
993 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700994 sp<IMemory> iMem = track->getCblk();
995 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000996 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800997 return NO_INIT;
998 }
Glenn Kasten53cec222013-08-29 09:01:02 -0700999 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001000 if (mAudioTrack != 0) {
1001 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1002 mDeathNotifier.clear();
1003 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001004 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001005 mCblkMemory = iMem;
1006 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1007 mCblk = cblk;
Glenn Kasten74935e42013-12-19 08:56:45 -08001008 // note that temp is the (possibly revised) value of frameCount
Glenn Kastenb6037442012-11-14 13:42:25 -08001009 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1010 // In current design, AudioTrack client checks and ensures frame count validity before
1011 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1012 // for fast track as it uses a special method of assigning frame count.
1013 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1014 }
1015 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001016 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001017 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001018 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001019 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001020 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001021 if (sharedBuffer == 0) {
Glenn Kastenb5fed682013-12-03 09:06:43 -08001022 // Theoretically double-buffering is not required for fast tracks,
1023 // due to tighter scheduling. But in practice, to accommodate kernels with
1024 // scheduling jitter, and apps with computation jitter, we use double-buffering.
1025 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1026 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001027 }
1028 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001029 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001030 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001031 // once denied, do not request again if IAudioTrack is re-created
1032 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1033 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001034 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001035 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1036 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001037 }
1038 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001039 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001040 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001041 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1042 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1043 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1044 } else {
1045 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1046 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1047 mFlags = flags;
1048 return NO_INIT;
1049 }
1050 }
1051
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001052 mRefreshRemaining = true;
1053
1054 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1055 // is the value of pointer() for the shared buffer, otherwise buffers points
1056 // immediately after the control block. This address is for the mapping within client
1057 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1058 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001059 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001060 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001061 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001062 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001063 }
1064
Eric Laurent2beeb502010-07-16 07:43:46 -07001065 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001066 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -08001067 mLatency = afLatency + (1000*frameCount) / sampleRate;
1068 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001069 // If IAudioTrack is re-created, don't let the requested frameCount
1070 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001071 if (frameCount > mReqFrameCount) {
1072 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001073 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001074
1075 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001076 if (sharedBuffer == 0) {
1077 mStaticProxy.clear();
1078 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1079 } else {
1080 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1081 mProxy = mStaticProxy;
1082 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001083 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1084 uint16_t(mVolume[LEFT] * 0x1000));
1085 mProxy->setSendLevel(mSendLevel);
1086 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001087 mProxy->setEpoch(epoch);
1088 mProxy->setMinimum(mNotificationFramesAct);
1089
1090 mDeathNotifier = new DeathNotifier(this);
1091 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001092
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001093 return NO_ERROR;
1094}
1095
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001096status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1097{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001098 if (audioBuffer == NULL) {
1099 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001100 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001101 if (mTransfer != TRANSFER_OBTAIN) {
1102 audioBuffer->frameCount = 0;
1103 audioBuffer->size = 0;
1104 audioBuffer->raw = NULL;
1105 return INVALID_OPERATION;
1106 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001107
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001108 const struct timespec *requested;
1109 if (waitCount == -1) {
1110 requested = &ClientProxy::kForever;
1111 } else if (waitCount == 0) {
1112 requested = &ClientProxy::kNonBlocking;
1113 } else if (waitCount > 0) {
1114 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1115 struct timespec timeout;
1116 timeout.tv_sec = ms / 1000;
1117 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1118 requested = &timeout;
1119 } else {
1120 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1121 requested = NULL;
1122 }
1123 return obtainBuffer(audioBuffer, requested);
1124}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001125
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001126status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1127 struct timespec *elapsed, size_t *nonContig)
1128{
1129 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1130 uint32_t oldSequence = 0;
1131 uint32_t newSequence;
1132
1133 Proxy::Buffer buffer;
1134 status_t status = NO_ERROR;
1135
1136 static const int32_t kMaxTries = 5;
1137 int32_t tryCounter = kMaxTries;
1138
1139 do {
1140 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1141 // keep them from going away if another thread re-creates the track during obtainBuffer()
1142 sp<AudioTrackClientProxy> proxy;
1143 sp<IMemory> iMem;
1144
1145 { // start of lock scope
1146 AutoMutex lock(mLock);
1147
1148 newSequence = mSequence;
1149 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1150 if (status == DEAD_OBJECT) {
1151 // re-create track, unless someone else has already done so
1152 if (newSequence == oldSequence) {
1153 status = restoreTrack_l("obtainBuffer");
1154 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001155 buffer.mFrameCount = 0;
1156 buffer.mRaw = NULL;
1157 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001158 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001159 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001160 }
1161 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001162 oldSequence = newSequence;
1163
1164 // Keep the extra references
1165 proxy = mProxy;
1166 iMem = mCblkMemory;
1167
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001168 if (mState == STATE_STOPPING) {
1169 status = -EINTR;
1170 buffer.mFrameCount = 0;
1171 buffer.mRaw = NULL;
1172 buffer.mNonContig = 0;
1173 break;
1174 }
1175
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001176 // Non-blocking if track is stopped or paused
1177 if (mState != STATE_ACTIVE) {
1178 requested = &ClientProxy::kNonBlocking;
1179 }
1180
1181 } // end of lock scope
1182
1183 buffer.mFrameCount = audioBuffer->frameCount;
1184 // FIXME starts the requested timeout and elapsed over from scratch
1185 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1186
1187 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1188
1189 audioBuffer->frameCount = buffer.mFrameCount;
1190 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1191 audioBuffer->raw = buffer.mRaw;
1192 if (nonContig != NULL) {
1193 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001194 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001195 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001196}
1197
1198void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1199{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001200 if (mTransfer == TRANSFER_SHARED) {
1201 return;
1202 }
1203
1204 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1205 if (stepCount == 0) {
1206 return;
1207 }
1208
1209 Proxy::Buffer buffer;
1210 buffer.mFrameCount = stepCount;
1211 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001212
Eric Laurent1703cdf2011-03-07 14:52:59 -08001213 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001214 mInUnderrun = false;
1215 mProxy->releaseBuffer(&buffer);
1216
1217 // restart track if it was disabled by audioflinger due to previous underrun
1218 if (mState == STATE_ACTIVE) {
1219 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001220 if (android_atomic_and(~CBLK_DISABLED, &cblk->mFlags) & CBLK_DISABLED) {
Glenn Kastend054c322013-07-12 12:59:20 -07001221 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1222 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001223 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001224 mAudioTrack->start();
1225 }
1226 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227}
1228
1229// -------------------------------------------------------------------------
1230
1231ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1232{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001233 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001234 return INVALID_OPERATION;
1235 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001237 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001238 // Sanity-check: user is most-likely passing an error code, and it would
1239 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001240 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001241 return BAD_VALUE;
1242 }
1243
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001244 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245 Buffer audioBuffer;
1246
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001247 while (userSize >= mFrameSize) {
1248 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001249
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001250 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001252 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001254 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 return ssize_t(err);
1256 }
1257
1258 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001259 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001261 toWrite = audioBuffer.size >> 1;
1262 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001263 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001265 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 userSize -= toWrite;
1269 written += toWrite;
1270
1271 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001272 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273
1274 return written;
1275}
1276
1277// -------------------------------------------------------------------------
1278
John Grossman4ff14ba2012-02-08 16:37:41 -08001279TimedAudioTrack::TimedAudioTrack() {
1280 mIsTimed = true;
1281}
1282
1283status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1284{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001285 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001286 status_t result = UNKNOWN_ERROR;
1287
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001288#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001289 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1290 // while we are accessing the cblk
1291 sp<IAudioTrack> audioTrack = mAudioTrack;
1292 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001293#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001294
John Grossman4ff14ba2012-02-08 16:37:41 -08001295 // If the track is not invalid already, try to allocate a buffer. alloc
1296 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001297 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001298 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001299 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001300 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1301 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001302 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001303 }
1304 }
1305
1306 // If the track is invalid at this point, attempt to restore it. and try the
1307 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001308 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001309 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001310
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001311 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001312 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001313 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001314 }
1315
1316 return result;
1317}
1318
1319status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1320 int64_t pts)
1321{
Eric Laurentdf839842012-05-31 14:27:14 -07001322 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1323 {
1324 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001325 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001326 // restart track if it was disabled by audioflinger due to previous underrun
1327 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001328 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1329 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001330 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001331 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001332 mAudioTrack->start();
1333 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001334 }
Eric Laurentdf839842012-05-31 14:27:14 -07001335 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001336}
1337
1338status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1339 TargetTimeline target)
1340{
1341 return mAudioTrack->setMediaTimeTransform(xform, target);
1342}
1343
1344// -------------------------------------------------------------------------
1345
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001346nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001347{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001348 // Currently the AudioTrack thread is not created if there are no callbacks.
1349 // Would it ever make sense to run the thread, even without callbacks?
1350 // If so, then replace this by checks at each use for mCbf != NULL.
1351 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1352
Eric Laurent1703cdf2011-03-07 14:52:59 -08001353 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001354 if (mAwaitBoost) {
1355 mAwaitBoost = false;
1356 mLock.unlock();
1357 static const int32_t kMaxTries = 5;
1358 int32_t tryCounter = kMaxTries;
1359 uint32_t pollUs = 10000;
1360 do {
1361 int policy = sched_getscheduler(0);
1362 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1363 break;
1364 }
1365 usleep(pollUs);
1366 pollUs <<= 1;
1367 } while (tryCounter-- > 0);
1368 if (tryCounter < 0) {
1369 ALOGE("did not receive expected priority boost on time");
1370 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001371 // Run again immediately
1372 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001373 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001374
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001375 // Can only reference mCblk while locked
1376 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001377 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001378
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001379 // Check for track invalidation
1380 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001381 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1382 // AudioSystem cache. We should not exit here but after calling the callback so
1383 // that the upper layers can recreate the track
1384 if (!isOffloaded() || (mSequence == mObservedSequence)) {
1385 status_t status = restoreTrack_l("processAudioBuffer");
1386 mLock.unlock();
1387 // Run again immediately, but with a new IAudioTrack
1388 return 0;
1389 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001390 }
1391
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001392 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001393 bool active = mState == STATE_ACTIVE;
1394
1395 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1396 bool newUnderrun = false;
1397 if (flags & CBLK_UNDERRUN) {
1398#if 0
1399 // Currently in shared buffer mode, when the server reaches the end of buffer,
1400 // the track stays active in continuous underrun state. It's up to the application
1401 // to pause or stop the track, or set the position to a new offset within buffer.
1402 // This was some experimental code to auto-pause on underrun. Keeping it here
1403 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1404 if (mTransfer == TRANSFER_SHARED) {
1405 mState = STATE_PAUSED;
1406 active = false;
1407 }
1408#endif
1409 if (!mInUnderrun) {
1410 mInUnderrun = true;
1411 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001412 }
1413 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001414
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001415 // Get current position of server
1416 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001417
1418 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001419 bool markerReached = false;
1420 size_t markerPosition = mMarkerPosition;
1421 // FIXME fails for wraparound, need 64 bits
1422 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1423 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424 }
1425
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001426 // Determine number of new position callback(s) that will be needed, while locked
1427 size_t newPosCount = 0;
1428 size_t newPosition = mNewPosition;
1429 size_t updatePeriod = mUpdatePeriod;
1430 // FIXME fails for wraparound, need 64 bits
1431 if (updatePeriod > 0 && position >= newPosition) {
1432 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1433 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001434 }
1435
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001436 // Cache other fields that will be needed soon
1437 uint32_t loopPeriod = mLoopPeriod;
1438 uint32_t sampleRate = mSampleRate;
1439 size_t notificationFrames = mNotificationFramesAct;
1440 if (mRefreshRemaining) {
1441 mRefreshRemaining = false;
1442 mRemainingFrames = notificationFrames;
1443 mRetryOnPartialBuffer = false;
1444 }
1445 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001446 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001447
1448 // These fields don't need to be cached, because they are assigned only by set():
1449 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1450 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1451
1452 mLock.unlock();
1453
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001454 if (waitStreamEnd) {
1455 AutoMutex lock(mLock);
1456
1457 sp<AudioTrackClientProxy> proxy = mProxy;
1458 sp<IMemory> iMem = mCblkMemory;
1459
1460 struct timespec timeout;
1461 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1462 timeout.tv_nsec = 0;
1463
1464 mLock.unlock();
1465 status_t status = mProxy->waitStreamEndDone(&timeout);
1466 mLock.lock();
1467 switch (status) {
1468 case NO_ERROR:
1469 case DEAD_OBJECT:
1470 case TIMED_OUT:
1471 mLock.unlock();
1472 mCbf(EVENT_STREAM_END, mUserData, NULL);
1473 mLock.lock();
1474 if (mState == STATE_STOPPING) {
1475 mState = STATE_STOPPED;
1476 if (status != DEAD_OBJECT) {
1477 return NS_INACTIVE;
1478 }
1479 }
1480 return 0;
1481 default:
1482 return 0;
1483 }
1484 }
1485
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001486 // perform callbacks while unlocked
1487 if (newUnderrun) {
1488 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1489 }
1490 // FIXME we will miss loops if loop cycle was signaled several times since last call
1491 // to processAudioBuffer()
1492 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1493 mCbf(EVENT_LOOP_END, mUserData, NULL);
1494 }
1495 if (flags & CBLK_BUFFER_END) {
1496 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1497 }
1498 if (markerReached) {
1499 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1500 }
1501 while (newPosCount > 0) {
1502 size_t temp = newPosition;
1503 mCbf(EVENT_NEW_POS, mUserData, &temp);
1504 newPosition += updatePeriod;
1505 newPosCount--;
1506 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001507
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001508 if (mObservedSequence != sequence) {
1509 mObservedSequence = sequence;
1510 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001511 // for offloaded tracks, just wait for the upper layers to recreate the track
1512 if (isOffloaded()) {
1513 return NS_INACTIVE;
1514 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001515 }
1516
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001517 // if inactive, then don't run me again until re-started
1518 if (!active) {
1519 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001520 }
1521
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001522 // Compute the estimated time until the next timed event (position, markers, loops)
1523 // FIXME only for non-compressed audio
1524 uint32_t minFrames = ~0;
1525 if (!markerReached && position < markerPosition) {
1526 minFrames = markerPosition - position;
1527 }
1528 if (loopPeriod > 0 && loopPeriod < minFrames) {
1529 minFrames = loopPeriod;
1530 }
1531 if (updatePeriod > 0 && updatePeriod < minFrames) {
1532 minFrames = updatePeriod;
1533 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001534
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001535 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1536 static const uint32_t kPoll = 0;
1537 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1538 minFrames = kPoll * notificationFrames;
1539 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001540
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001541 // Convert frame units to time units
1542 nsecs_t ns = NS_WHENEVER;
1543 if (minFrames != (uint32_t) ~0) {
1544 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1545 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1546 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1547 }
1548
1549 // If not supplying data by EVENT_MORE_DATA, then we're done
1550 if (mTransfer != TRANSFER_CALLBACK) {
1551 return ns;
1552 }
1553
1554 struct timespec timeout;
1555 const struct timespec *requested = &ClientProxy::kForever;
1556 if (ns != NS_WHENEVER) {
1557 timeout.tv_sec = ns / 1000000000LL;
1558 timeout.tv_nsec = ns % 1000000000LL;
1559 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1560 requested = &timeout;
1561 }
1562
1563 while (mRemainingFrames > 0) {
1564
1565 Buffer audioBuffer;
1566 audioBuffer.frameCount = mRemainingFrames;
1567 size_t nonContig;
1568 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1569 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1570 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1571 requested = &ClientProxy::kNonBlocking;
1572 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001573 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1574 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001575 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001576 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1577 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001578 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001579 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001580 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1581 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001583
Eric Laurent42a6f422013-08-29 14:35:05 -07001584 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001585 mRetryOnPartialBuffer = false;
1586 if (avail < mRemainingFrames) {
1587 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1588 if (ns < 0 || myns < ns) {
1589 ns = myns;
1590 }
1591 return ns;
1592 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001593 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594
1595 // Divide buffer size by 2 to take into account the expansion
1596 // due to 8 to 16 bit conversion: the callback must fill only half
1597 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001598 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 audioBuffer.size >>= 1;
1600 }
1601
1602 size_t reqSize = audioBuffer.size;
1603 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001604 size_t writtenSize = audioBuffer.size;
1605 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606
1607 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001608 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1609 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1610 reqSize, (int) writtenSize);
1611 return NS_NEVER;
1612 }
1613
1614 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001615 // The callback is done filling buffers
1616 // Keep this thread going to handle timed events and
1617 // still try to get more data in intervals of WAIT_PERIOD_MS
1618 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001619 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001620 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001621
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001622 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001623 // 8 to 16 bit conversion, note that source and destination are the same address
1624 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001625 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001626 }
1627
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001628 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1629 audioBuffer.frameCount = releasedFrames;
1630 mRemainingFrames -= releasedFrames;
1631 if (misalignment >= releasedFrames) {
1632 misalignment -= releasedFrames;
1633 } else {
1634 misalignment = 0;
1635 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001636
1637 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001638
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001639 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1640 // if callback doesn't like to accept the full chunk
1641 if (writtenSize < reqSize) {
1642 continue;
1643 }
1644
1645 // There could be enough non-contiguous frames available to satisfy the remaining request
1646 if (mRemainingFrames <= nonContig) {
1647 continue;
1648 }
1649
1650#if 0
1651 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1652 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1653 // that total to a sum == notificationFrames.
1654 if (0 < misalignment && misalignment <= mRemainingFrames) {
1655 mRemainingFrames = misalignment;
1656 return (mRemainingFrames * 1100000000LL) / sampleRate;
1657 }
1658#endif
1659
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001660 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001661 mRemainingFrames = notificationFrames;
1662 mRetryOnPartialBuffer = true;
1663
1664 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1665 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001666}
1667
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001668status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001669{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001670 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
1671 isOffloaded() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001672 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001673 status_t result;
1674
Glenn Kastena47f3162012-11-07 10:13:08 -08001675 // refresh the audio configuration cache in this process to make sure we get new
1676 // output parameters in getOutput_l() and createTrack_l()
1677 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001678
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001679 if (isOffloaded()) {
1680 return DEAD_OBJECT;
1681 }
1682
1683 // force new output query from audio policy manager;
1684 mOutput = 0;
1685 audio_io_handle_t output = getOutput_l();
1686
Glenn Kastena47f3162012-11-07 10:13:08 -08001687 // if the new IAudioTrack is created, createTrack_l() will modify the
1688 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1689 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001690
1691 // take the frames that will be lost by track recreation into account in saved position
1692 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001693 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001694 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001695 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001696 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001697 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001698 mFlags,
1699 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001700 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001701 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001702
Glenn Kastena47f3162012-11-07 10:13:08 -08001703 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001704 // continue playback from last known position, but
1705 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1706 if (mStaticProxy != NULL) {
1707 mLoopPeriod = 0;
1708 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1709 }
1710 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1711 // track destruction have been played? This is critical for SoundPool implementation
1712 // This must be broken, and needs to be tested/debugged.
1713#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001714 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001715 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001716 // Make sure that a client relying on callback events indicating underrun or
1717 // the actual amount of audio frames played (e.g SoundPool) receives them.
1718 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001719 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001720 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001721 }
1722 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001723#endif
1724 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001725 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001726 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001727 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001728 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001729 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1730 // As getOutput was called above and resulted in an output stream to be opened,
1731 // we need to release it.
1732 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001733 ALOGW("restoreTrack_l() failed status %d", result);
1734 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001735 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001736
1737 return result;
1738}
1739
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001740status_t AudioTrack::setParameters(const String8& keyValuePairs)
1741{
1742 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001743 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001744}
1745
Glenn Kastence703742013-07-19 16:33:58 -07001746status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1747{
Glenn Kasten53cec222013-08-29 09:01:02 -07001748 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001749 // FIXME not implemented for fast tracks; should use proxy and SSQ
1750 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1751 return INVALID_OPERATION;
1752 }
1753 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1754 return INVALID_OPERATION;
1755 }
1756 status_t status = mAudioTrack->getTimestamp(timestamp);
1757 if (status == NO_ERROR) {
1758 timestamp.mPosition += mProxy->getEpoch();
1759 }
1760 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001761}
1762
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001763String8 AudioTrack::getParameters(const String8& keys)
1764{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001765 if (mOutput) {
1766 return AudioSystem::getParameters(mOutput, keys);
1767 } else {
1768 return String8::empty();
1769 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001770}
1771
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001772status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1773{
1774
1775 const size_t SIZE = 256;
1776 char buffer[SIZE];
1777 String8 result;
1778
1779 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001780 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1781 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001782 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001783 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001784 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001785 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001786 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001787 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001788 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001789 result.append(buffer);
1790 ::write(fd, result.string(), result.size());
1791 return NO_ERROR;
1792}
1793
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001794uint32_t AudioTrack::getUnderrunFrames() const
1795{
1796 AutoMutex lock(mLock);
1797 return mProxy->getUnderrunFrames();
1798}
1799
1800// =========================================================================
1801
1802void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1803{
1804 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1805 if (audioTrack != 0) {
1806 AutoMutex lock(audioTrack->mLock);
1807 audioTrack->mProxy->binderDied();
1808 }
1809}
1810
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811// =========================================================================
1812
1813AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001814 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1815 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001816{
1817}
1818
1819AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001820{
1821}
1822
1823bool AudioTrack::AudioTrackThread::threadLoop()
1824{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001825 {
1826 AutoMutex _l(mMyLock);
1827 if (mPaused) {
1828 mMyCond.wait(mMyLock);
1829 // caller will check for exitPending()
1830 return true;
1831 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001832 if (mIgnoreNextPausedInt) {
1833 mIgnoreNextPausedInt = false;
1834 mPausedInt = false;
1835 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001836 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001837 if (mPausedNs > 0) {
1838 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1839 } else {
1840 mMyCond.wait(mMyLock);
1841 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001842 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001843 return true;
1844 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001845 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001846 nsecs_t ns = mReceiver.processAudioBuffer(this);
1847 switch (ns) {
1848 case 0:
1849 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001850 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001851 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001852 return true;
1853 case NS_NEVER:
1854 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001855 case NS_WHENEVER:
1856 // FIXME increase poll interval, or make event-driven
1857 ns = 1000000000LL;
1858 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001859 default:
1860 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001861 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001862 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001863 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001864}
1865
Glenn Kasten3acbd052012-02-28 10:39:56 -08001866void AudioTrack::AudioTrackThread::requestExit()
1867{
1868 // must be in this order to avoid a race condition
1869 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001870 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001871}
1872
1873void AudioTrack::AudioTrackThread::pause()
1874{
1875 AutoMutex _l(mMyLock);
1876 mPaused = true;
1877}
1878
1879void AudioTrack::AudioTrackThread::resume()
1880{
1881 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07001882 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001883 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001884 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001885 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001886 mMyCond.signal();
1887 }
1888}
1889
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001890void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
1891{
1892 AutoMutex _l(mMyLock);
1893 mPausedInt = true;
1894 mPausedNs = ns;
1895}
1896
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001897}; // namespace android