blob: f626f6fab740e7fefe144a36c8fcde898f6e15e1 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
Alok Kediya601e9b52013-09-27 11:55:10 +05302** Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
3** Not a Contribution.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08004** Copyright 2007, The Android Open Source Project
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17*/
18
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "AudioTrack"
22
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080023#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080024#include <audio_utils/primitives.h>
25#include <binder/IPCThreadState.h>
26#include <media/AudioTrack.h>
27#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070029#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080030
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +010031#define WAIT_PERIOD_MS 10
32#define WAIT_STREAM_END_TIMEOUT_SEC 120
33
Glenn Kasten511754b2012-01-11 09:52:19 -080034
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080036// ---------------------------------------------------------------------------
37
38// static
39status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080040 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080041 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080042 uint32_t sampleRate)
43{
Glenn Kastend65d73c2012-06-22 17:21:07 -070044 if (frameCount == NULL) {
45 return BAD_VALUE;
46 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070047
48 // default to 0 in case of error
49 *frameCount = 0;
50
Glenn Kastene0fa4672012-04-24 14:35:14 -070051 // FIXME merge with similar code in createTrack_l(), except we're missing
52 // some information here that is available in createTrack_l():
53 // audio_io_handle_t output
54 // audio_format_t format
55 // audio_channel_mask_t channelMask
56 // audio_output_flags_t flags
Alok Kediya601e9b52013-09-27 11:55:10 +053057 uint32_t afSampleRate = 0;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080058 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
59 return NO_INIT;
60 }
Alok Kediya601e9b52013-09-27 11:55:10 +053061 size_t afFrameCount = 0;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080062 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
63 return NO_INIT;
64 }
Alok Kediya601e9b52013-09-27 11:55:10 +053065 uint32_t afLatency = 0;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080066 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
67 return NO_INIT;
68 }
Alok Kediya601e9b52013-09-27 11:55:10 +053069 if(!afSampleRate || !afFrameCount) {
70 ALOGW("samplerate or framecount 0");
71 return NO_INIT;
72 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080073 // Ensure that buffer depth covers at least audio hardware latency
74 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080075 if (minBufCount < 2) {
76 minBufCount = 2;
77 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080078
79 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070080 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080081 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
82 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080083 return NO_ERROR;
84}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085
86// ---------------------------------------------------------------------------
87
88AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070089 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080090 mIsTimed(false),
91 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Alok Kediya601e9b52013-09-27 11:55:10 +053092 mPreviousSchedulingGroup(SP_DEFAULT),
93 mAudioFlinger(NULL),
94 mObserver(NULL),
95 mCblk(NULL)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096{
97}
98
99AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800100 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800102 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700103 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700105 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800106 callback_t cbf,
107 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700108 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800109 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000110 transfer_type transferType,
111 const audio_offload_info_t *offloadInfo)
Glenn Kasten87913512011-06-22 16:15:25 -0700112 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800113 mIsTimed(false),
114 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Alok Kediya601e9b52013-09-27 11:55:10 +0530115 mPreviousSchedulingGroup(SP_DEFAULT),
116 mAudioFlinger(NULL),
117 mObserver(NULL),
118 mCblk(NULL)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700120 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700121 frameCount, flags, cbf, user, notificationFrames,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000122 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123}
124
Andreas Huberc8139852012-01-18 10:51:55 -0800125AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800126 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800128 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700129 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700131 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 callback_t cbf,
133 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700134 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800135 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000136 transfer_type transferType,
137 const audio_offload_info_t *offloadInfo)
Glenn Kasten87913512011-06-22 16:15:25 -0700138 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800139 mIsTimed(false),
140 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Alok Kediya601e9b52013-09-27 11:55:10 +0530141 mPreviousSchedulingGroup(SP_DEFAULT),
142 mProxy(NULL),
143 mAudioFlinger(NULL),
144 mObserver(NULL),
145 mCblk(NULL)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700147 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800148 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000149 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150}
151
152AudioTrack::~AudioTrack()
153{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 if (mStatus == NO_ERROR) {
155 // Make sure that callback function exits in the case where
156 // it is looping on buffer full condition in obtainBuffer().
157 // Otherwise the callback thread will never exit.
158 stop();
159 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100160 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800161 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162 mAudioTrackThread->requestExitAndWait();
163 mAudioTrackThread.clear();
164 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530165 if (mAudioTrack != 0) {
166 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
167 mAudioTrack.clear();
168 AudioSystem::releaseAudioSessionId(mSessionId);
169 }
170
171 if (mDirectTrack != 0) {
172 mDirectTrack.clear();
173 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174 IPCThreadState::self()->flushCommands();
175 }
176}
177
178status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800179 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800181 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700182 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800183 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700184 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 callback_t cbf,
186 void* user,
187 int notificationFrames,
188 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700189 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800190 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000191 transfer_type transferType,
192 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800194 switch (transferType) {
195 case TRANSFER_DEFAULT:
196 if (sharedBuffer != 0) {
197 transferType = TRANSFER_SHARED;
198 } else if (cbf == NULL || threadCanCallJava) {
199 transferType = TRANSFER_SYNC;
200 } else {
201 transferType = TRANSFER_CALLBACK;
202 }
203 break;
204 case TRANSFER_CALLBACK:
205 if (cbf == NULL || sharedBuffer != 0) {
206 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
207 return BAD_VALUE;
208 }
209 break;
210 case TRANSFER_OBTAIN:
211 case TRANSFER_SYNC:
212 if (sharedBuffer != 0) {
213 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
214 return BAD_VALUE;
215 }
216 break;
217 case TRANSFER_SHARED:
218 if (sharedBuffer == 0) {
219 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
220 return BAD_VALUE;
221 }
222 break;
223 default:
224 ALOGE("Invalid transfer type %d", transferType);
225 return BAD_VALUE;
226 }
227 mTransfer = transferType;
228
Glenn Kastene33054e2012-11-14 12:54:39 -0800229 // FIXME "int" here is legacy and will be replaced by size_t later
230 if (frameCountInt < 0) {
231 ALOGE("Invalid frame count %d", frameCountInt);
232 return BAD_VALUE;
233 }
234 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700236 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
237 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800238
Glenn Kastene33054e2012-11-14 12:54:39 -0800239 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700240
Eric Laurent1703cdf2011-03-07 14:52:59 -0800241 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800242
Glenn Kasten53cec222013-08-29 09:01:02 -0700243 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700244 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000245 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800246 return INVALID_OPERATION;
247 }
248
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100249 mOutput = 0;
250
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700252 if (streamType == AUDIO_STREAM_DEFAULT) {
253 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700255
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800257 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700258 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
259 return NO_INIT;
260 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 sampleRate = afSampleRate;
262 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800263 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800266 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700267 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700269 if (channelMask == 0) {
270 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 }
272
273 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700274 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800275 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 return BAD_VALUE;
277 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700278
Glenn Kastene0fa4672012-04-24 14:35:14 -0700279 // AudioFlinger does not currently support 8-bit data in shared memory
280 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
281 ALOGE("8-bit data in shared memory is not supported");
282 return BAD_VALUE;
283 }
284
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100286 // or offload was requested
287 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
288 || !audio_is_linear_pcm(format)) {
289 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
290 ? "Offload request, forcing to Direct Output"
291 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700292 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800293 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700294 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700295 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700296 // only allow deep buffering for music stream type
297 if (streamType != AUDIO_STREAM_MUSIC) {
298 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
299 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700300
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700301 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700302 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303 return BAD_VALUE;
304 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800305 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700306 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800307 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700308
Alok Kediya601e9b52013-09-27 11:55:10 +0530309 if ((streamType == AUDIO_STREAM_VOICE_CALL)
310 && (channelCount == 1)
311 && ((sampleRate == 8000 || sampleRate == 16000)))
312 {
313 ALOGD("Turn on Direct Output for VOIP RX");
314 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_VOIP_RX|AUDIO_OUTPUT_FLAG_DIRECT);
315 }
316
317 if ((audio_stream_type_t)streamType == AUDIO_STREAM_VOICE_CALL) {
318 if (audio_is_linear_pcm(format)) {
319 mFrameSize = channelCount * audio_bytes_per_sample(format);
320 mFrameSizeAF = channelCount * sizeof(int16_t);
321 } else {
322 mFrameSize = sizeof(uint16_t);
323 mFrameSizeAF = sizeof(uint16_t);
324 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800325 } else {
Alok Kediya601e9b52013-09-27 11:55:10 +0530326 if (audio_is_linear_pcm(format)) {
327 mFrameSize = channelCount * audio_bytes_per_sample(format);
328 mFrameSizeAF = channelCount * sizeof(int16_t);
329 } else {
330 mFrameSize = sizeof(uint8_t);
331 mFrameSizeAF = sizeof(uint8_t);
332 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800333 }
334
Dima Zavinfce7a472011-04-19 22:30:36 -0700335 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800336 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800337 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000338 flags,
339 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700340
341 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000342 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 return BAD_VALUE;
344 }
345
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800346 mVolume[LEFT] = 1.0f;
347 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800348 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700349 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800350 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700351 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800352 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700353 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700354 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700355 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700356 mCbf = cbf;
Alok Kediya601e9b52013-09-27 11:55:10 +0530357 mSampleRate = sampleRate;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700358
Alok Kediya601e9b52013-09-27 11:55:10 +0530359 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL) {
360 ALOGV("Creating Direct Track");
361 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
362 if (audioFlinger == 0) {
363 ALOGE("Could not get audioflinger");
364 return NO_INIT;
Glenn Kastena997e7a2012-08-07 09:44:19 -0700365 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530366 mAudioFlinger = audioFlinger;
367 status_t status = NO_ERROR;
368 mAudioDirectOutput = output;
369 mDirectTrack = audioFlinger->createDirectTrack( getpid(),
370 sampleRate,
371 channelMask,
372 mAudioDirectOutput,
373 &mSessionId,
374 this,
375 streamType,
376 &status);
377 if(status != NO_ERROR) {
378 ALOGE("createDirectTrack returned with status %d", status);
379 return status;
380 }
381 mAudioTrack = NULL;
382 mSharedBuffer = NULL;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700383 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530384 else {
385 if (cbf != NULL) {
386 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
387 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
388 }
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700389
Alok Kediya601e9b52013-09-27 11:55:10 +0530390 // create the IAudioTrack
391 status_t status = createTrack_l(streamType,
392 sampleRate,
393 format,
394 frameCount,
395 flags,
396 sharedBuffer,
397 output,
398 0 /*epoch*/);
399
400 if (status != NO_ERROR) {
401 if (mAudioTrackThread != 0) {
402 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
403 mAudioTrackThread->requestExitAndWait();
404 mAudioTrackThread.clear();
405 }
406 //Use of direct and offloaded output streams is ref counted by audio policy manager.
407 // As getOutput was called above and resulted in an output stream to be opened,
408 // we need to release it.
409 AudioSystem::releaseOutput(output);
410 return status;
411 }
412 AudioSystem::acquireAudioSessionId(mSessionId);
413 mAudioDirectOutput = -1;
414 mDirectTrack = NULL;
415 mSharedBuffer = sharedBuffer;
416 }
417 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800419 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800420 mFormat = format;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800421 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800423 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700425 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800426 mNewPosition = 0;
427 mUpdatePeriod = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800428 mSequence = 1;
429 mObservedSequence = mSequence;
430 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100431 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800432
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433 return NO_ERROR;
434}
435
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436// -------------------------------------------------------------------------
437
Alok Kediya601e9b52013-09-27 11:55:10 +0530438uint32_t AudioTrack::latency() const
439{
440 if (mAudioDirectOutput != -1) {
441 return mAudioFlinger->latency(mAudioDirectOutput);
442 } else if (mOutput != 0) {
443 uint32_t afLatency = 0;
444 uint32_t newLatency = 0;
445 AudioSystem::getLatency(mOutput, mStreamType, &afLatency);
446 if (0 != mSampleRate){
447 newLatency = afLatency + (1000*mCblk->frameCount_) / mSampleRate;
448 }
449 ALOGD("latency() mLatency = %d, newLatency = %d", mLatency, newLatency);
450 return newLatency;
451 }
452 return mLatency;
453}
454
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100455status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800456{
Alok Kediya601e9b52013-09-27 11:55:10 +0530457 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800458 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800459 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100460 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 }
462
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800463 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800465 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100466 if (previousState == STATE_PAUSED_STOPPING) {
467 mState = STATE_STOPPING;
468 } else {
469 mState = STATE_ACTIVE;
470 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530471 if (mDirectTrack != NULL) {
472 mDirectTrack->start();
473 return status;
474 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800475 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
476 // reset current position as seen by client to 0
477 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700478 // force refresh of remaining frames by processAudioBuffer() as last
479 // write before stop could be partial.
480 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800481 }
482 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700483 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800484
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800486 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100487 if (previousState == STATE_STOPPING) {
488 mProxy->interrupt();
489 } else {
490 t->resume();
491 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800492 } else {
493 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
494 get_sched_policy(0, &mPreviousSchedulingGroup);
495 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
496 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800498 if (!(flags & CBLK_INVALID)) {
499 status = mAudioTrack->start();
500 if (status == DEAD_OBJECT) {
501 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800503 }
504 if (flags & CBLK_INVALID) {
505 status = restoreTrack_l("start");
506 }
507
508 if (status != NO_ERROR) {
509 ALOGE("start() status %d", status);
510 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100512 if (previousState != STATE_STOPPING) {
513 t->pause();
514 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700516 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700517 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 }
519 }
520
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100521 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800522}
523
524void AudioTrack::stop()
525{
526 AutoMutex lock(mLock);
527 // FIXME pause then stop should not be a nop
528 if (mState != STATE_ACTIVE) {
529 return;
530 }
531
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100532 if (isOffloaded()) {
533 mState = STATE_STOPPING;
534 } else {
535 mState = STATE_STOPPED;
536 }
537
Alok Kediya601e9b52013-09-27 11:55:10 +0530538 if(mDirectTrack != NULL) {
539 mDirectTrack->stop();
540 } else if (mAudioTrack != NULL) {
541 mProxy->interrupt();
542 mAudioTrack->stop();
543 // the playback head position will reset to 0, so if a marker is set, we need
544 // to activate it again
545 mMarkerReached = false;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800546#if 0
Alok Kediya601e9b52013-09-27 11:55:10 +0530547 // Force flush if a shared buffer is used otherwise audioflinger
548 // will not stop before end of buffer is reached.
549 // It may be needed to make sure that we stop playback, likely in case looping is on.
550 if (mSharedBuffer != 0) {
551 flush_l();
552 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800553#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100554
Alok Kediya601e9b52013-09-27 11:55:10 +0530555 sp<AudioTrackThread> t = mAudioTrackThread;
556 if (t != 0) {
557 if (!isOffloaded()) {
558 t->pause();
559 }
560 } else {
561 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
562 set_sched_policy(0, mPreviousSchedulingGroup);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100563 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800564 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565}
566
567bool AudioTrack::stopped() const
568{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800569 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800570 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571}
572
573void AudioTrack::flush()
574{
Alok Kediya601e9b52013-09-27 11:55:10 +0530575 if(mDirectTrack != NULL) {
576 ALOGE("mdirect track flush");
577 mDirectTrack->flush();
578 return ;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800579 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800580 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
581 return;
582 }
583 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800584}
585
Eric Laurent1703cdf2011-03-07 14:52:59 -0800586void AudioTrack::flush_l()
587{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800588 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700589
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700590 // clear playback marker and periodic update counter
591 mMarkerPosition = 0;
592 mMarkerReached = false;
593 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100594 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700595
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800596 mState = STATE_FLUSHED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100597 if (isOffloaded()) {
598 mProxy->interrupt();
599 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800600 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800601 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602}
603
604void AudioTrack::pause()
605{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800606 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100607 if (mState == STATE_ACTIVE) {
608 mState = STATE_PAUSED;
609 } else if (mState == STATE_STOPPING) {
610 mState = STATE_PAUSED_STOPPING;
611 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800612 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530614 if(mDirectTrack != NULL) {
615 ALOGV("mDirectTrack pause");
616 mDirectTrack->pause();
617 } else {
618 mProxy->interrupt();
619 mAudioTrack->pause();
620 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621}
622
Eric Laurentbe916aa2010-06-01 23:49:17 -0700623status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800625 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700626 return BAD_VALUE;
627 }
628
Eric Laurent1703cdf2011-03-07 14:52:59 -0800629 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630 mVolume[LEFT] = left;
631 mVolume[RIGHT] = right;
Alok Kediya601e9b52013-09-27 11:55:10 +0530632 if(mDirectTrack != NULL) {
633 ALOGV("mDirectTrack->setVolume(left = %f , right = %f)", left,right);
634 mDirectTrack->setVolume(left, right);
635 } else {
636 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
637 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700638
Eric Laurent59fe0102013-09-27 18:48:26 -0700639 if (isOffloaded()) {
640 mAudioTrack->signal();
641 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700642 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643}
644
Glenn Kastenb1c09932012-02-27 16:21:04 -0800645status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800647 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700648}
649
Eric Laurent2beeb502010-07-16 07:43:46 -0700650status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700651{
Alok Kediya601e9b52013-09-27 11:55:10 +0530652 if (mDirectTrack != NULL) {
653 return NO_ERROR;
654 }
Glenn Kasten05632a52012-01-03 14:22:33 -0800655 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700656 return BAD_VALUE;
657 }
658
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800659 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700660 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800661 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700662
663 return NO_ERROR;
664}
665
Glenn Kastena5224f32012-01-04 12:41:44 -0800666void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700667{
668 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800669 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700670 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671}
672
Glenn Kasten3b16c762012-11-14 08:44:39 -0800673status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100675 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800676 return INVALID_OPERATION;
677 }
678
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800679 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800680 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700681 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682 }
683 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700684 if (rate == 0 || rate > afSamplingRate*2 ) {
685 return BAD_VALUE;
686 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687
Eric Laurent1703cdf2011-03-07 14:52:59 -0800688 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800689 mSampleRate = rate;
690 mProxy->setSampleRate(rate);
691
Eric Laurent57326622009-07-07 07:10:45 -0700692 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693}
694
Glenn Kastena5224f32012-01-04 12:41:44 -0800695uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696{
John Grossman4ff14ba2012-02-08 16:37:41 -0800697 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800698 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800699 }
700
Eric Laurent1703cdf2011-03-07 14:52:59 -0800701 AutoMutex lock(mLock);
Alok Kediya601e9b52013-09-27 11:55:10 +0530702 if(mAudioDirectOutput != -1) {
703 return mAudioFlinger->sampleRate(mAudioDirectOutput);
704 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800705 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706}
707
708status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
709{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100710 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800711 return INVALID_OPERATION;
712 }
713
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800715 ;
716 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
717 loopEnd - loopStart >= MIN_LOOP) {
718 ;
719 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 return BAD_VALUE;
721 }
722
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800723 AutoMutex lock(mLock);
724 // See setPosition() regarding setting parameters such as loop points or position while active
725 if (mState == STATE_ACTIVE) {
726 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700727 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800728 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729 return NO_ERROR;
730}
731
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800732void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
733{
734 // FIXME If setting a loop also sets position to start of loop, then
735 // this is correct. Otherwise it should be removed.
736 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
737 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
738 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
739}
740
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800741status_t AudioTrack::setMarkerPosition(uint32_t marker)
742{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700743 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100744 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700745 return INVALID_OPERATION;
746 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800747
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800748 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700750 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751
752 return NO_ERROR;
753}
754
Glenn Kastena5224f32012-01-04 12:41:44 -0800755status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800756{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100757 if (isOffloaded()) {
758 return INVALID_OPERATION;
759 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700760 if (marker == NULL) {
761 return BAD_VALUE;
762 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800763
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800764 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 *marker = mMarkerPosition;
766
767 return NO_ERROR;
768}
769
770status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
771{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700772 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100773 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700774 return INVALID_OPERATION;
775 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800776
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800777 AutoMutex lock(mLock);
778 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800780 return NO_ERROR;
781}
782
Glenn Kastena5224f32012-01-04 12:41:44 -0800783status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800784{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100785 if (isOffloaded()) {
786 return INVALID_OPERATION;
787 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700788 if (updatePeriod == NULL) {
789 return BAD_VALUE;
790 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800791
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800792 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800793 *updatePeriod = mUpdatePeriod;
794
795 return NO_ERROR;
796}
797
798status_t AudioTrack::setPosition(uint32_t position)
799{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100800 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700801 return INVALID_OPERATION;
802 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800803 if (position > mFrameCount) {
804 return BAD_VALUE;
805 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800806
Eric Laurent1703cdf2011-03-07 14:52:59 -0800807 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800808 // Currently we require that the player is inactive before setting parameters such as position
809 // or loop points. Otherwise, there could be a race condition: the application could read the
810 // current position, compute a new position or loop parameters, and then set that position or
811 // loop parameters but it would do the "wrong" thing since the position has continued to advance
812 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
813 // to specify how it wants to handle such scenarios.
814 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700815 return INVALID_OPERATION;
816 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800817 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
818 mLoopPeriod = 0;
819 // FIXME Check whether loops and setting position are incompatible in old code.
820 // If we use setLoop for both purposes we lose the capability to set the position while looping.
821 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700822
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823 return NO_ERROR;
824}
825
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800826status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800827{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700828 if (position == NULL) {
829 return BAD_VALUE;
830 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800831
Eric Laurent1703cdf2011-03-07 14:52:59 -0800832 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100833 if (isOffloaded()) {
834 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800835
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100836 if (mOutput != 0) {
837 uint32_t halFrames;
838 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
839 }
840 *position = dspFrames;
841 } else {
842 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
843 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
844 mProxy->getPosition();
845 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800846 return NO_ERROR;
847}
848
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800849status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800850{
851 if (mSharedBuffer == 0 || mIsTimed) {
852 return INVALID_OPERATION;
853 }
854 if (position == NULL) {
855 return BAD_VALUE;
856 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800857
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800858 AutoMutex lock(mLock);
859 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800860 return NO_ERROR;
861}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800862
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800863status_t AudioTrack::reload()
864{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100865 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800866 return INVALID_OPERATION;
867 }
868
Eric Laurent1703cdf2011-03-07 14:52:59 -0800869 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800870 // See setPosition() regarding setting parameters such as loop points or position while active
871 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700872 return INVALID_OPERATION;
873 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800874 mNewPosition = mUpdatePeriod;
875 mLoopPeriod = 0;
876 // FIXME The new code cannot reload while keeping a loop specified.
877 // Need to check how the old code handled this, and whether it's a significant change.
878 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800879 return NO_ERROR;
880}
881
Eric Laurentc2f1f072009-07-17 12:17:14 -0700882audio_io_handle_t AudioTrack::getOutput()
883{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800884 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100885 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800886}
887
888// must be called with mLock held
889audio_io_handle_t AudioTrack::getOutput_l()
890{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100891 if (mOutput) {
892 return mOutput;
893 } else {
894 return AudioSystem::getOutput(mStreamType,
895 mSampleRate, mFormat, mChannelMask, mFlags);
896 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700897}
898
Eric Laurentbe916aa2010-06-01 23:49:17 -0700899status_t AudioTrack::attachAuxEffect(int effectId)
900{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800901 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700902 status_t status = mAudioTrack->attachAuxEffect(effectId);
903 if (status == NO_ERROR) {
904 mAuxEffectId = effectId;
905 }
906 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700907}
908
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909// -------------------------------------------------------------------------
910
Eric Laurent1703cdf2011-03-07 14:52:59 -0800911// must be called with mLock held
912status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800913 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800914 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800915 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800916 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700917 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800918 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800919 audio_io_handle_t output,
920 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800921{
922 status_t status;
923 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
924 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700925 ALOGE("Could not get audioflinger");
926 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800927 }
928
Glenn Kastence8828a2013-09-16 18:07:38 -0700929 // Not all of these values are needed under all conditions, but it is easier to get them all
930
Alok Kediya601e9b52013-09-27 11:55:10 +0530931 uint32_t afLatency = 0;
Glenn Kastence8828a2013-09-16 18:07:38 -0700932 status = AudioSystem::getLatency(output, streamType, &afLatency);
933 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800934 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700935 return NO_INIT;
936 }
937
Alok Kediya601e9b52013-09-27 11:55:10 +0530938 size_t afFrameCount = 0;
Glenn Kastence8828a2013-09-16 18:07:38 -0700939 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
940 if (status != NO_ERROR) {
941 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
942 return NO_INIT;
943 }
944
Alok Kediya601e9b52013-09-27 11:55:10 +0530945 uint32_t afSampleRate = 0;
Glenn Kastence8828a2013-09-16 18:07:38 -0700946 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
947 if (status != NO_ERROR) {
948 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
949 return NO_INIT;
950 }
951
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700952 // Client decides whether the track is TIMED (see below), but can only express a preference
953 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700954 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700955 // either of these use cases:
956 // use case 1: shared buffer
957 (sharedBuffer != 0) ||
958 // use case 2: callback handler
959 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800960 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700961 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700962 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700963 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700964 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700965 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700966
Glenn Kastence8828a2013-09-16 18:07:38 -0700967 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
968 // n = 1 fast track; nBuffering is ignored
969 // n = 2 normal track, no sample rate conversion
970 // n = 3 normal track, with sample rate conversion
971 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
972 // n > 3 very high latency or very small notification interval; nBuffering is ignored
973 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
974
Eric Laurentd1b449a2010-05-14 03:26:45 -0700975 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700976
Dima Zavinfce7a472011-04-19 22:30:36 -0700977 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700978
Eric Laurentd1b449a2010-05-14 03:26:45 -0700979 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700980 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700981 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700982 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700983 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700984 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100985 if (mNotificationFramesAct != frameCount) {
986 mNotificationFramesAct = frameCount;
987 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700988 } else if (sharedBuffer != 0) {
989
Glenn Kastena42ff002012-11-14 12:47:55 -0800990 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700991 // 8-bit data in shared memory is not currently supported by AudioFlinger
992 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800993 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700994 // More than 2 channels does not require stronger alignment than stereo
995 alignment <<= 1;
996 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800997 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
998 ALOGE("Invalid buffer alignment: address %p, channel count %u",
999 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001000 return BAD_VALUE;
1001 }
1002
1003 // When initializing a shared buffer AudioTrack via constructors,
1004 // there's no frameCount parameter.
1005 // But when initializing a shared buffer AudioTrack via set(),
1006 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -08001007 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001008
1009 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
1010
1011 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -07001012
Eric Laurentd1b449a2010-05-14 03:26:45 -07001013 // Ensure that buffer depth covers at least audio hardware latency
Alok Kediya601e9b52013-09-27 11:55:10 +05301014 if(!afSampleRate && !afFrameCount) {
1015 ALOGW("samplerate or framecount zero");
1016 return NO_INIT;
1017 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001018 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -07001019 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
1020 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -07001021 if (minBufCount <= nBuffering) {
1022 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -08001023 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001024
Glenn Kastene33054e2012-11-14 12:54:39 -08001025 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
1026 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -08001027 ", afLatency=%d",
1028 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001029
1030 if (frameCount == 0) {
1031 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -07001032 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001033 // not ALOGW because it happens all the time when playing key clicks over A2DP
1034 ALOGV("Minimum buffer size corrected from %d to %d",
1035 frameCount, minFrameCount);
1036 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001037 }
Glenn Kastence8828a2013-09-16 18:07:38 -07001038 // Make sure that application is notified with sufficient margin before underrun
1039 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1040 mNotificationFramesAct = frameCount/nBuffering;
1041 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001042
Glenn Kastene0fa4672012-04-24 14:35:14 -07001043 } else {
1044 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -07001045 }
1046
Glenn Kastena075db42012-03-06 11:22:44 -08001047 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
1048 if (mIsTimed) {
1049 trackFlags |= IAudioFlinger::TRACK_TIMED;
1050 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001051
1052 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001053 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001054 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001055 if (mAudioTrackThread != 0) {
1056 tid = mAudioTrackThread->getTid();
1057 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001058 }
1059
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001060 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1061 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1062 }
1063
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001064 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001065 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001066 // AudioFlinger only sees 16-bit PCM
1067 format == AUDIO_FORMAT_PCM_8_BIT ?
1068 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -08001069 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001070 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -08001071 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001072 sharedBuffer,
1073 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001074 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001075 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001076 mName,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001077 &status);
1078
1079 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001080 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001081 return status;
1082 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001083 sp<IMemory> iMem = track->getCblk();
1084 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001085 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001086 return NO_INIT;
1087 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001088 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001089 if (mAudioTrack != 0) {
1090 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1091 mDeathNotifier.clear();
1092 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001093 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001094 mCblkMemory = iMem;
1095 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1096 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001097 size_t temp = cblk->frameCount_;
1098 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1099 // In current design, AudioTrack client checks and ensures frame count validity before
1100 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1101 // for fast track as it uses a special method of assigning frame count.
1102 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1103 }
1104 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001105 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001106 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001107 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001108 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001109 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001110 if (sharedBuffer == 0) {
1111 // double-buffering is not required for fast tracks, due to tighter scheduling
1112 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount) {
1113 mNotificationFramesAct = frameCount;
1114 }
1115 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001116 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001117 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001118 // once denied, do not request again if IAudioTrack is re-created
1119 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1120 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001121 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001122 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1123 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001124 }
1125 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001126 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001127 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001128 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1129 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1130 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1131 } else {
1132 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1133 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1134 mFlags = flags;
1135 return NO_INIT;
1136 }
1137 }
1138
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001139 mRefreshRemaining = true;
1140
1141 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1142 // is the value of pointer() for the shared buffer, otherwise buffers points
1143 // immediately after the control block. This address is for the mapping within client
1144 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1145 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001146 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001147 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001148 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001149 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001150 }
1151
Eric Laurent2beeb502010-07-16 07:43:46 -07001152 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001153 // FIXME don't believe this lie
Alok Kediya601e9b52013-09-27 11:55:10 +05301154 if(sampleRate){
1155 mLatency = afLatency + (1000*frameCount) / sampleRate;
1156 }else{
1157 mLatency = afLatency;
1158 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001159 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001160 // If IAudioTrack is re-created, don't let the requested frameCount
1161 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001162 if (frameCount > mReqFrameCount) {
1163 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001164 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001165
1166 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001167 if (sharedBuffer == 0) {
1168 mStaticProxy.clear();
1169 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1170 } else {
1171 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1172 mProxy = mStaticProxy;
1173 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001174 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1175 uint16_t(mVolume[LEFT] * 0x1000));
1176 mProxy->setSendLevel(mSendLevel);
1177 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001178 mProxy->setEpoch(epoch);
1179 mProxy->setMinimum(mNotificationFramesAct);
1180
1181 mDeathNotifier = new DeathNotifier(this);
1182 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001183
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001184 return NO_ERROR;
1185}
1186
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001187status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1188{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001189 if (audioBuffer == NULL) {
1190 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001191 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001192 if (mTransfer != TRANSFER_OBTAIN) {
1193 audioBuffer->frameCount = 0;
1194 audioBuffer->size = 0;
1195 audioBuffer->raw = NULL;
1196 return INVALID_OPERATION;
1197 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001198
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001199 const struct timespec *requested;
1200 if (waitCount == -1) {
1201 requested = &ClientProxy::kForever;
1202 } else if (waitCount == 0) {
1203 requested = &ClientProxy::kNonBlocking;
1204 } else if (waitCount > 0) {
1205 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1206 struct timespec timeout;
1207 timeout.tv_sec = ms / 1000;
1208 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1209 requested = &timeout;
1210 } else {
1211 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1212 requested = NULL;
1213 }
1214 return obtainBuffer(audioBuffer, requested);
1215}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001216
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001217status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1218 struct timespec *elapsed, size_t *nonContig)
1219{
1220 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1221 uint32_t oldSequence = 0;
1222 uint32_t newSequence;
1223
1224 Proxy::Buffer buffer;
1225 status_t status = NO_ERROR;
1226
1227 static const int32_t kMaxTries = 5;
1228 int32_t tryCounter = kMaxTries;
1229
1230 do {
1231 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1232 // keep them from going away if another thread re-creates the track during obtainBuffer()
1233 sp<AudioTrackClientProxy> proxy;
1234 sp<IMemory> iMem;
1235
1236 { // start of lock scope
1237 AutoMutex lock(mLock);
1238
1239 newSequence = mSequence;
1240 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1241 if (status == DEAD_OBJECT) {
1242 // re-create track, unless someone else has already done so
1243 if (newSequence == oldSequence) {
1244 status = restoreTrack_l("obtainBuffer");
1245 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001246 buffer.mFrameCount = 0;
1247 buffer.mRaw = NULL;
1248 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001249 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 }
1252 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001253 oldSequence = newSequence;
1254
1255 // Keep the extra references
1256 proxy = mProxy;
1257 iMem = mCblkMemory;
1258
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001259 if (mState == STATE_STOPPING) {
1260 status = -EINTR;
1261 buffer.mFrameCount = 0;
1262 buffer.mRaw = NULL;
1263 buffer.mNonContig = 0;
1264 break;
1265 }
1266
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001267 // Non-blocking if track is stopped or paused
1268 if (mState != STATE_ACTIVE) {
1269 requested = &ClientProxy::kNonBlocking;
1270 }
1271
1272 } // end of lock scope
1273
1274 buffer.mFrameCount = audioBuffer->frameCount;
1275 // FIXME starts the requested timeout and elapsed over from scratch
1276 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1277
1278 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1279
1280 audioBuffer->frameCount = buffer.mFrameCount;
1281 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1282 audioBuffer->raw = buffer.mRaw;
1283 if (nonContig != NULL) {
1284 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001286 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287}
1288
1289void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1290{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001291 if (mTransfer == TRANSFER_SHARED) {
1292 return;
1293 }
1294
1295 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1296 if (stepCount == 0) {
1297 return;
1298 }
1299
1300 Proxy::Buffer buffer;
1301 buffer.mFrameCount = stepCount;
1302 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001303
Eric Laurent1703cdf2011-03-07 14:52:59 -08001304 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001305 mInUnderrun = false;
1306 mProxy->releaseBuffer(&buffer);
1307
1308 // restart track if it was disabled by audioflinger due to previous underrun
1309 if (mState == STATE_ACTIVE) {
1310 audio_track_cblk_t* cblk = mCblk;
Alok Kediya601e9b52013-09-27 11:55:10 +05301311 if (cblk->mFlags & CBLK_DISABLED) {
1312 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Glenn Kastend054c322013-07-12 12:59:20 -07001313 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1314 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001315 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001316 mAudioTrack->start();
1317 }
1318 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319}
1320
1321// -------------------------------------------------------------------------
1322
1323ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1324{
Alok Kediya601e9b52013-09-27 11:55:10 +05301325 if (mDirectTrack != NULL) {
1326 ssize_t written = 0;
1327 written = mDirectTrack->write(buffer,userSize);
1328 return written;
1329 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001330 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001331 return INVALID_OPERATION;
1332 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001333
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001334 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001335 // Sanity-check: user is most-likely passing an error code, and it would
1336 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001337 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001338 return BAD_VALUE;
1339 }
1340
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001341 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001342 Buffer audioBuffer;
1343
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001344 while (userSize >= mFrameSize) {
1345 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001346
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001347 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001348 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001349 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001350 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001351 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001352 return ssize_t(err);
1353 }
1354
1355 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001356 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001357 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001358 toWrite = audioBuffer.size >> 1;
1359 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001360 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001361 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001362 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001363 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001364 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001365 userSize -= toWrite;
1366 written += toWrite;
1367
1368 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001369 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001370
1371 return written;
1372}
1373
1374// -------------------------------------------------------------------------
1375
John Grossman4ff14ba2012-02-08 16:37:41 -08001376TimedAudioTrack::TimedAudioTrack() {
1377 mIsTimed = true;
1378}
1379
1380status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1381{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001382 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001383 status_t result = UNKNOWN_ERROR;
1384
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001385#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001386 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1387 // while we are accessing the cblk
1388 sp<IAudioTrack> audioTrack = mAudioTrack;
1389 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001390#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001391
John Grossman4ff14ba2012-02-08 16:37:41 -08001392 // If the track is not invalid already, try to allocate a buffer. alloc
1393 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001394 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001395 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001396 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001397 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1398 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001399 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001400 }
1401 }
1402
1403 // If the track is invalid at this point, attempt to restore it. and try the
1404 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001405 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001406 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001407
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001408 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001409 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001410 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001411 }
1412
1413 return result;
1414}
1415
1416status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1417 int64_t pts)
1418{
Eric Laurentdf839842012-05-31 14:27:14 -07001419 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1420 {
1421 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001422 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001423 // restart track if it was disabled by audioflinger due to previous underrun
1424 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001425 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1426 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001427 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001428 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001429 mAudioTrack->start();
1430 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001431 }
Eric Laurentdf839842012-05-31 14:27:14 -07001432 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001433}
1434
1435status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1436 TargetTimeline target)
1437{
1438 return mAudioTrack->setMediaTimeTransform(xform, target);
1439}
1440
1441// -------------------------------------------------------------------------
1442
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001443nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001445 // Currently the AudioTrack thread is not created if there are no callbacks.
1446 // Would it ever make sense to run the thread, even without callbacks?
1447 // If so, then replace this by checks at each use for mCbf != NULL.
1448 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1449
Eric Laurent1703cdf2011-03-07 14:52:59 -08001450 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001451 if (mAwaitBoost) {
1452 mAwaitBoost = false;
1453 mLock.unlock();
1454 static const int32_t kMaxTries = 5;
1455 int32_t tryCounter = kMaxTries;
1456 uint32_t pollUs = 10000;
1457 do {
1458 int policy = sched_getscheduler(0);
1459 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1460 break;
1461 }
1462 usleep(pollUs);
1463 pollUs <<= 1;
1464 } while (tryCounter-- > 0);
1465 if (tryCounter < 0) {
1466 ALOGE("did not receive expected priority boost on time");
1467 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001468 // Run again immediately
1469 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001470 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001471
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001472 // Can only reference mCblk while locked
1473 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001474 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001475
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001476 // Check for track invalidation
1477 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001478 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1479 // AudioSystem cache. We should not exit here but after calling the callback so
1480 // that the upper layers can recreate the track
1481 if (!isOffloaded() || (mSequence == mObservedSequence)) {
1482 status_t status = restoreTrack_l("processAudioBuffer");
1483 mLock.unlock();
1484 // Run again immediately, but with a new IAudioTrack
1485 return 0;
1486 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001487 }
1488
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001489 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001490 bool active = mState == STATE_ACTIVE;
1491
1492 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1493 bool newUnderrun = false;
1494 if (flags & CBLK_UNDERRUN) {
1495#if 0
1496 // Currently in shared buffer mode, when the server reaches the end of buffer,
1497 // the track stays active in continuous underrun state. It's up to the application
1498 // to pause or stop the track, or set the position to a new offset within buffer.
1499 // This was some experimental code to auto-pause on underrun. Keeping it here
1500 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1501 if (mTransfer == TRANSFER_SHARED) {
1502 mState = STATE_PAUSED;
1503 active = false;
1504 }
1505#endif
1506 if (!mInUnderrun) {
1507 mInUnderrun = true;
1508 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001509 }
1510 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001511
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001512 // Get current position of server
1513 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001514
1515 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001516 bool markerReached = false;
1517 size_t markerPosition = mMarkerPosition;
1518 // FIXME fails for wraparound, need 64 bits
1519 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1520 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001521 }
1522
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001523 // Determine number of new position callback(s) that will be needed, while locked
1524 size_t newPosCount = 0;
1525 size_t newPosition = mNewPosition;
1526 size_t updatePeriod = mUpdatePeriod;
1527 // FIXME fails for wraparound, need 64 bits
1528 if (updatePeriod > 0 && position >= newPosition) {
1529 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1530 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001531 }
1532
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001533 // Cache other fields that will be needed soon
1534 uint32_t loopPeriod = mLoopPeriod;
1535 uint32_t sampleRate = mSampleRate;
1536 size_t notificationFrames = mNotificationFramesAct;
1537 if (mRefreshRemaining) {
1538 mRefreshRemaining = false;
1539 mRemainingFrames = notificationFrames;
1540 mRetryOnPartialBuffer = false;
1541 }
1542 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001543 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001544
1545 // These fields don't need to be cached, because they are assigned only by set():
1546 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1547 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1548
1549 mLock.unlock();
1550
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001551 if (waitStreamEnd) {
1552 AutoMutex lock(mLock);
1553
1554 sp<AudioTrackClientProxy> proxy = mProxy;
1555 sp<IMemory> iMem = mCblkMemory;
1556
1557 struct timespec timeout;
1558 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1559 timeout.tv_nsec = 0;
1560
1561 mLock.unlock();
1562 status_t status = mProxy->waitStreamEndDone(&timeout);
1563 mLock.lock();
1564 switch (status) {
1565 case NO_ERROR:
1566 case DEAD_OBJECT:
1567 case TIMED_OUT:
1568 mLock.unlock();
1569 mCbf(EVENT_STREAM_END, mUserData, NULL);
1570 mLock.lock();
1571 if (mState == STATE_STOPPING) {
1572 mState = STATE_STOPPED;
1573 if (status != DEAD_OBJECT) {
1574 return NS_INACTIVE;
1575 }
1576 }
1577 return 0;
1578 default:
1579 return 0;
1580 }
1581 }
1582
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001583 // perform callbacks while unlocked
1584 if (newUnderrun) {
1585 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1586 }
1587 // FIXME we will miss loops if loop cycle was signaled several times since last call
1588 // to processAudioBuffer()
1589 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1590 mCbf(EVENT_LOOP_END, mUserData, NULL);
1591 }
1592 if (flags & CBLK_BUFFER_END) {
1593 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1594 }
1595 if (markerReached) {
1596 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1597 }
1598 while (newPosCount > 0) {
1599 size_t temp = newPosition;
1600 mCbf(EVENT_NEW_POS, mUserData, &temp);
1601 newPosition += updatePeriod;
1602 newPosCount--;
1603 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001604
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001605 if (mObservedSequence != sequence) {
1606 mObservedSequence = sequence;
1607 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001608 // for offloaded tracks, just wait for the upper layers to recreate the track
1609 if (isOffloaded()) {
1610 return NS_INACTIVE;
1611 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001612 }
1613
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001614 // if inactive, then don't run me again until re-started
1615 if (!active) {
1616 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001617 }
1618
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001619 // Compute the estimated time until the next timed event (position, markers, loops)
1620 // FIXME only for non-compressed audio
1621 uint32_t minFrames = ~0;
1622 if (!markerReached && position < markerPosition) {
1623 minFrames = markerPosition - position;
1624 }
1625 if (loopPeriod > 0 && loopPeriod < minFrames) {
1626 minFrames = loopPeriod;
1627 }
1628 if (updatePeriod > 0 && updatePeriod < minFrames) {
1629 minFrames = updatePeriod;
1630 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001631
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001632 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1633 static const uint32_t kPoll = 0;
1634 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1635 minFrames = kPoll * notificationFrames;
1636 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001637
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001638 // Convert frame units to time units
1639 nsecs_t ns = NS_WHENEVER;
1640 if (minFrames != (uint32_t) ~0) {
1641 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1642 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1643 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1644 }
1645
1646 // If not supplying data by EVENT_MORE_DATA, then we're done
1647 if (mTransfer != TRANSFER_CALLBACK) {
1648 return ns;
1649 }
1650
1651 struct timespec timeout;
1652 const struct timespec *requested = &ClientProxy::kForever;
1653 if (ns != NS_WHENEVER) {
1654 timeout.tv_sec = ns / 1000000000LL;
1655 timeout.tv_nsec = ns % 1000000000LL;
1656 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1657 requested = &timeout;
1658 }
1659
1660 while (mRemainingFrames > 0) {
1661
1662 Buffer audioBuffer;
1663 audioBuffer.frameCount = mRemainingFrames;
1664 size_t nonContig;
1665 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1666 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1667 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1668 requested = &ClientProxy::kNonBlocking;
1669 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001670 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1671 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001672 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001673 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1674 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001675 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001676 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001677 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1678 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001680
Eric Laurent42a6f422013-08-29 14:35:05 -07001681 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001682 mRetryOnPartialBuffer = false;
1683 if (avail < mRemainingFrames) {
1684 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1685 if (ns < 0 || myns < ns) {
1686 ns = myns;
1687 }
1688 return ns;
1689 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001690 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001691
1692 // Divide buffer size by 2 to take into account the expansion
1693 // due to 8 to 16 bit conversion: the callback must fill only half
1694 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001695 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001696 audioBuffer.size >>= 1;
1697 }
1698
1699 size_t reqSize = audioBuffer.size;
1700 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001701 size_t writtenSize = audioBuffer.size;
1702 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001703
1704 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001705 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1706 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1707 reqSize, (int) writtenSize);
1708 return NS_NEVER;
1709 }
1710
1711 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001712 // The callback is done filling buffers
1713 // Keep this thread going to handle timed events and
1714 // still try to get more data in intervals of WAIT_PERIOD_MS
1715 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001716 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001717 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001718
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001719 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001720 // 8 to 16 bit conversion, note that source and destination are the same address
1721 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001722 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001723 }
1724
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001725 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1726 audioBuffer.frameCount = releasedFrames;
1727 mRemainingFrames -= releasedFrames;
1728 if (misalignment >= releasedFrames) {
1729 misalignment -= releasedFrames;
1730 } else {
1731 misalignment = 0;
1732 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001733
1734 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001735
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001736 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1737 // if callback doesn't like to accept the full chunk
1738 if (writtenSize < reqSize) {
1739 continue;
1740 }
1741
1742 // There could be enough non-contiguous frames available to satisfy the remaining request
1743 if (mRemainingFrames <= nonContig) {
1744 continue;
1745 }
1746
1747#if 0
1748 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1749 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1750 // that total to a sum == notificationFrames.
1751 if (0 < misalignment && misalignment <= mRemainingFrames) {
1752 mRemainingFrames = misalignment;
1753 return (mRemainingFrames * 1100000000LL) / sampleRate;
1754 }
1755#endif
1756
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001757 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001758 mRemainingFrames = notificationFrames;
1759 mRetryOnPartialBuffer = true;
1760
1761 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1762 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001763}
1764
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001765status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001766{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001767 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
1768 isOffloaded() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001769 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001770 status_t result;
1771
Glenn Kastena47f3162012-11-07 10:13:08 -08001772 // refresh the audio configuration cache in this process to make sure we get new
1773 // output parameters in getOutput_l() and createTrack_l()
1774 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001775
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001776 if (isOffloaded()) {
1777 return DEAD_OBJECT;
1778 }
1779
1780 // force new output query from audio policy manager;
1781 mOutput = 0;
1782 audio_io_handle_t output = getOutput_l();
1783
Glenn Kastena47f3162012-11-07 10:13:08 -08001784 // if the new IAudioTrack is created, createTrack_l() will modify the
1785 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1786 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001787
1788 // take the frames that will be lost by track recreation into account in saved position
1789 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001790 mNewPosition = position + mUpdatePeriod;
1791 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001792 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001793 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001794 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001795 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001796 mFlags,
1797 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001798 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001799 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001800
Glenn Kastena47f3162012-11-07 10:13:08 -08001801 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001802 // continue playback from last known position, but
1803 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1804 if (mStaticProxy != NULL) {
1805 mLoopPeriod = 0;
1806 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1807 }
1808 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1809 // track destruction have been played? This is critical for SoundPool implementation
1810 // This must be broken, and needs to be tested/debugged.
1811#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001812 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001813 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001814 // Make sure that a client relying on callback events indicating underrun or
1815 // the actual amount of audio frames played (e.g SoundPool) receives them.
1816 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001817 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001818 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001819 }
1820 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001821#endif
1822 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001823 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001824 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001825 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001826 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001827 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1828 // As getOutput was called above and resulted in an output stream to be opened,
1829 // we need to release it.
1830 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001831 ALOGW("restoreTrack_l() failed status %d", result);
1832 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001833 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001834
1835 return result;
1836}
1837
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001838status_t AudioTrack::setParameters(const String8& keyValuePairs)
1839{
1840 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001841 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001842}
1843
Glenn Kastence703742013-07-19 16:33:58 -07001844status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1845{
Glenn Kasten53cec222013-08-29 09:01:02 -07001846 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001847 // FIXME not implemented for fast tracks; should use proxy and SSQ
1848 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1849 return INVALID_OPERATION;
1850 }
1851 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1852 return INVALID_OPERATION;
1853 }
1854 status_t status = mAudioTrack->getTimestamp(timestamp);
1855 if (status == NO_ERROR) {
1856 timestamp.mPosition += mProxy->getEpoch();
1857 }
1858 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001859}
1860
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001861String8 AudioTrack::getParameters(const String8& keys)
1862{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001863 if (mOutput) {
1864 return AudioSystem::getParameters(mOutput, keys);
1865 } else {
1866 return String8::empty();
1867 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001868}
1869
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001870status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1871{
1872
1873 const size_t SIZE = 256;
1874 char buffer[SIZE];
1875 String8 result;
1876
1877 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001878 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1879 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001880 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001881 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001882 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001883 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001884 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001885 result.append(buffer);
Alok Kediya601e9b52013-09-27 11:55:10 +05301886 uint32_t afLatency = 0;
1887 AudioSystem::getLatency(mOutput, mStreamType, &afLatency);
1888 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, afLatency + (1000*mCblk->frameCount_) / mSampleRate);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001889 result.append(buffer);
1890 ::write(fd, result.string(), result.size());
1891 return NO_ERROR;
1892}
1893
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001894uint32_t AudioTrack::getUnderrunFrames() const
1895{
1896 AutoMutex lock(mLock);
1897 return mProxy->getUnderrunFrames();
1898}
1899
1900// =========================================================================
1901
1902void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1903{
1904 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1905 if (audioTrack != 0) {
1906 AutoMutex lock(audioTrack->mLock);
1907 audioTrack->mProxy->binderDied();
1908 }
1909}
1910
Alok Kediya601e9b52013-09-27 11:55:10 +05301911void AudioTrack::notify(int msg) {
1912 if (msg == EVENT_UNDERRUN) {
1913 ALOGV("Posting event underrun to Audio Sink.");
1914 mCbf(EVENT_UNDERRUN, mUserData, 0);
1915 }
1916 if (msg == EVENT_HW_FAIL) {
1917 ALOGV("Posting event HW fail to Audio Sink.");
1918 mCbf(EVENT_HW_FAIL, mUserData, 0);
1919 }
1920}
1921
1922status_t AudioTrack::getTimeStamp(uint64_t *tstamp) {
1923 if (mDirectTrack != NULL) {
1924 *tstamp = mDirectTrack->getTimeStamp();
1925 ALOGE("Timestamp %lld ", *tstamp);
1926 }
1927 return NO_ERROR;
1928}
1929
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001930// =========================================================================
1931
1932AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07001933 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1934 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001935{
1936}
1937
1938AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001939{
1940}
1941
1942bool AudioTrack::AudioTrackThread::threadLoop()
1943{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001944 {
1945 AutoMutex _l(mMyLock);
1946 if (mPaused) {
1947 mMyCond.wait(mMyLock);
1948 // caller will check for exitPending()
1949 return true;
1950 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07001951 if (mIgnoreNextPausedInt) {
1952 mIgnoreNextPausedInt = false;
1953 mPausedInt = false;
1954 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001955 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001956 if (mPausedNs > 0) {
1957 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1958 } else {
1959 mMyCond.wait(mMyLock);
1960 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07001961 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001962 return true;
1963 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001964 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001965 nsecs_t ns = mReceiver.processAudioBuffer(this);
1966 switch (ns) {
1967 case 0:
1968 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001969 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001970 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001971 return true;
1972 case NS_NEVER:
1973 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001974 case NS_WHENEVER:
1975 // FIXME increase poll interval, or make event-driven
1976 ns = 1000000000LL;
1977 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001978 default:
1979 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07001980 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001981 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001982 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001983}
1984
Glenn Kasten3acbd052012-02-28 10:39:56 -08001985void AudioTrack::AudioTrackThread::requestExit()
1986{
1987 // must be in this order to avoid a race condition
1988 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07001989 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001990}
1991
1992void AudioTrack::AudioTrackThread::pause()
1993{
1994 AutoMutex _l(mMyLock);
1995 mPaused = true;
1996}
1997
1998void AudioTrack::AudioTrackThread::resume()
1999{
2000 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07002001 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002002 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08002003 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002004 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08002005 mMyCond.signal();
2006 }
2007}
2008
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002009void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
2010{
2011 AutoMutex _l(mMyLock);
2012 mPausedInt = true;
2013 mPausedNs = ns;
2014}
2015
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002016}; // namespace android