blob: 17b1f2c3ae196858eacd24e838f51f0d307e1a59 [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),
Ethan Chen8d420ed2013-11-14 00:40:54 -080092#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +053093 mPreviousSchedulingGroup(SP_DEFAULT),
94 mAudioFlinger(NULL),
95 mObserver(NULL),
96 mCblk(NULL)
Ethan Chen8d420ed2013-11-14 00:40:54 -080097#else
98 mPreviousSchedulingGroup(SP_DEFAULT)
99#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100{
101}
102
103AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800104 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800106 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700107 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700109 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800110 callback_t cbf,
111 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700112 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800113 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000114 transfer_type transferType,
115 const audio_offload_info_t *offloadInfo)
Glenn Kasten87913512011-06-22 16:15:25 -0700116 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800117 mIsTimed(false),
118 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Ethan Chen8d420ed2013-11-14 00:40:54 -0800119#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530120 mPreviousSchedulingGroup(SP_DEFAULT),
121 mAudioFlinger(NULL),
122 mObserver(NULL),
123 mCblk(NULL)
Ethan Chen8d420ed2013-11-14 00:40:54 -0800124#else
125 mPreviousSchedulingGroup(SP_DEFAULT)
126#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700128 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700129 frameCount, flags, cbf, user, notificationFrames,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000130 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800131}
132
Andreas Huberc8139852012-01-18 10:51:55 -0800133AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800134 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800136 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700137 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700139 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140 callback_t cbf,
141 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700142 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800143 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000144 transfer_type transferType,
145 const audio_offload_info_t *offloadInfo)
Glenn Kasten87913512011-06-22 16:15:25 -0700146 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800147 mIsTimed(false),
148 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Ethan Chen8d420ed2013-11-14 00:40:54 -0800149#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530150 mPreviousSchedulingGroup(SP_DEFAULT),
151 mProxy(NULL),
152 mAudioFlinger(NULL),
153 mObserver(NULL),
154 mCblk(NULL)
Ethan Chen8d420ed2013-11-14 00:40:54 -0800155#else
156 mPreviousSchedulingGroup(SP_DEFAULT)
157#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700159 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800160 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000161 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800162}
163
164AudioTrack::~AudioTrack()
165{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 if (mStatus == NO_ERROR) {
167 // Make sure that callback function exits in the case where
168 // it is looping on buffer full condition in obtainBuffer().
169 // Otherwise the callback thread will never exit.
170 stop();
171 if (mAudioTrackThread != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100172 mProxy->interrupt();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800173 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174 mAudioTrackThread->requestExitAndWait();
175 mAudioTrackThread.clear();
176 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800177#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530178 if (mAudioTrack != 0) {
179 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
180 mAudioTrack.clear();
181 AudioSystem::releaseAudioSessionId(mSessionId);
182 }
183
184 if (mDirectTrack != 0) {
185 mDirectTrack.clear();
186 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 IPCThreadState::self()->flushCommands();
Ethan Chen8d420ed2013-11-14 00:40:54 -0800188#else
189 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
190 mAudioTrack.clear();
191 IPCThreadState::self()->flushCommands();
192 AudioSystem::releaseAudioSessionId(mSessionId);
193#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 }
195}
196
197status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800198 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800200 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700201 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800202 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700203 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 callback_t cbf,
205 void* user,
206 int notificationFrames,
207 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700208 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800209 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000210 transfer_type transferType,
211 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800213 switch (transferType) {
214 case TRANSFER_DEFAULT:
215 if (sharedBuffer != 0) {
216 transferType = TRANSFER_SHARED;
217 } else if (cbf == NULL || threadCanCallJava) {
218 transferType = TRANSFER_SYNC;
219 } else {
220 transferType = TRANSFER_CALLBACK;
221 }
222 break;
223 case TRANSFER_CALLBACK:
224 if (cbf == NULL || sharedBuffer != 0) {
225 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
226 return BAD_VALUE;
227 }
228 break;
229 case TRANSFER_OBTAIN:
230 case TRANSFER_SYNC:
231 if (sharedBuffer != 0) {
232 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
233 return BAD_VALUE;
234 }
235 break;
236 case TRANSFER_SHARED:
237 if (sharedBuffer == 0) {
238 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
239 return BAD_VALUE;
240 }
241 break;
242 default:
243 ALOGE("Invalid transfer type %d", transferType);
244 return BAD_VALUE;
245 }
246 mTransfer = transferType;
247
Glenn Kastene33054e2012-11-14 12:54:39 -0800248 // FIXME "int" here is legacy and will be replaced by size_t later
249 if (frameCountInt < 0) {
250 ALOGE("Invalid frame count %d", frameCountInt);
251 return BAD_VALUE;
252 }
253 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700255 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
256 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257
Glenn Kastene33054e2012-11-14 12:54:39 -0800258 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700259
Eric Laurent1703cdf2011-03-07 14:52:59 -0800260 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800261
Glenn Kasten53cec222013-08-29 09:01:02 -0700262 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Eric Laurent1dd70b92009-04-21 07:56:33 -0700263 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000264 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 return INVALID_OPERATION;
266 }
267
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100268 mOutput = 0;
269
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700271 if (streamType == AUDIO_STREAM_DEFAULT) {
272 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700274
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800276 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700277 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
278 return NO_INIT;
279 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280 sampleRate = afSampleRate;
281 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800282 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700283
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800284 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800285 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700286 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700288 if (channelMask == 0) {
289 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800290 }
291
292 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700293 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800294 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 return BAD_VALUE;
296 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700297
Glenn Kastene0fa4672012-04-24 14:35:14 -0700298 // AudioFlinger does not currently support 8-bit data in shared memory
299 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
300 ALOGE("8-bit data in shared memory is not supported");
301 return BAD_VALUE;
302 }
303
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304 // force direct flag if format is not linear PCM
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100305 // or offload was requested
306 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
307 || !audio_is_linear_pcm(format)) {
308 ALOGV( (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)
309 ? "Offload request, forcing to Direct Output"
310 : "Not linear PCM, forcing to Direct Output");
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700311 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800312 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700313 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700315 // only allow deep buffering for music stream type
316 if (streamType != AUDIO_STREAM_MUSIC) {
317 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
318 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700319
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700320 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700321 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700322 return BAD_VALUE;
323 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800324 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700325 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800326 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700327
Ethan Chen8d420ed2013-11-14 00:40:54 -0800328#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530329 if ((streamType == AUDIO_STREAM_VOICE_CALL)
330 && (channelCount == 1)
331 && ((sampleRate == 8000 || sampleRate == 16000)))
332 {
333 ALOGD("Turn on Direct Output for VOIP RX");
334 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_VOIP_RX|AUDIO_OUTPUT_FLAG_DIRECT);
335 }
336
337 if ((audio_stream_type_t)streamType == AUDIO_STREAM_VOICE_CALL) {
338 if (audio_is_linear_pcm(format)) {
339 mFrameSize = channelCount * audio_bytes_per_sample(format);
340 mFrameSizeAF = channelCount * sizeof(int16_t);
341 } else {
342 mFrameSize = sizeof(uint16_t);
343 mFrameSizeAF = sizeof(uint16_t);
344 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800345 } else {
Alok Kediya601e9b52013-09-27 11:55:10 +0530346 if (audio_is_linear_pcm(format)) {
347 mFrameSize = channelCount * audio_bytes_per_sample(format);
348 mFrameSizeAF = channelCount * sizeof(int16_t);
349 } else {
350 mFrameSize = sizeof(uint8_t);
351 mFrameSizeAF = sizeof(uint8_t);
352 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800353 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800354#else
355 if (audio_is_linear_pcm(format)) {
356 mFrameSize = channelCount * audio_bytes_per_sample(format);
357 mFrameSizeAF = channelCount * sizeof(int16_t);
358 } else {
359 mFrameSize = sizeof(uint8_t);
360 mFrameSizeAF = sizeof(uint8_t);
361 }
362#endif
Glenn Kastene3aa6592012-12-04 12:22:46 -0800363
Dima Zavinfce7a472011-04-19 22:30:36 -0700364 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800365 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800366 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000367 flags,
368 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700369
370 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000371 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800372 return BAD_VALUE;
373 }
374
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800375 mVolume[LEFT] = 1.0f;
376 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800377 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700378 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800379 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700380 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800381 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700382 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700383 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700384 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700385 mCbf = cbf;
Ethan Chen8d420ed2013-11-14 00:40:54 -0800386#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530387 mSampleRate = sampleRate;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700388
Alok Kediya601e9b52013-09-27 11:55:10 +0530389 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL) {
390 ALOGV("Creating Direct Track");
391 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
392 if (audioFlinger == 0) {
393 ALOGE("Could not get audioflinger");
394 return NO_INIT;
Glenn Kastena997e7a2012-08-07 09:44:19 -0700395 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530396 mAudioFlinger = audioFlinger;
397 status_t status = NO_ERROR;
398 mAudioDirectOutput = output;
399 mDirectTrack = audioFlinger->createDirectTrack( getpid(),
400 sampleRate,
401 channelMask,
402 mAudioDirectOutput,
403 &mSessionId,
404 this,
405 streamType,
406 &status);
407 if(status != NO_ERROR) {
408 ALOGE("createDirectTrack returned with status %d", status);
409 return status;
410 }
411 mAudioTrack = NULL;
412 mSharedBuffer = NULL;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700413 }
Alok Kediya601e9b52013-09-27 11:55:10 +0530414 else {
Ethan Chen8d420ed2013-11-14 00:40:54 -0800415#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530416 if (cbf != NULL) {
417 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
418 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
419 }
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700420
Alok Kediya601e9b52013-09-27 11:55:10 +0530421 // create the IAudioTrack
422 status_t status = createTrack_l(streamType,
423 sampleRate,
424 format,
425 frameCount,
426 flags,
427 sharedBuffer,
428 output,
429 0 /*epoch*/);
430
431 if (status != NO_ERROR) {
432 if (mAudioTrackThread != 0) {
433 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
434 mAudioTrackThread->requestExitAndWait();
435 mAudioTrackThread.clear();
436 }
437 //Use of direct and offloaded output streams is ref counted by audio policy manager.
438 // As getOutput was called above and resulted in an output stream to be opened,
439 // we need to release it.
440 AudioSystem::releaseOutput(output);
441 return status;
442 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800443#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530444 AudioSystem::acquireAudioSessionId(mSessionId);
445 mAudioDirectOutput = -1;
446 mDirectTrack = NULL;
447 mSharedBuffer = sharedBuffer;
448 }
449 mUserData = user;
Ethan Chen8d420ed2013-11-14 00:40:54 -0800450#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800453 mFormat = format;
Ethan Chen8d420ed2013-11-14 00:40:54 -0800454#ifndef QCOM_HARDWARE
455 mSharedBuffer = sharedBuffer;
456#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800457 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800459 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800460 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700461 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800462 mNewPosition = 0;
463 mUpdatePeriod = 0;
Ethan Chen8d420ed2013-11-14 00:40:54 -0800464#ifndef QCOM_HARDWARE
465 AudioSystem::acquireAudioSessionId(mSessionId);
466#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800467 mSequence = 1;
468 mObservedSequence = mSequence;
469 mInUnderrun = false;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100470 mOutput = output;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800471
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800472 return NO_ERROR;
473}
474
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475// -------------------------------------------------------------------------
476
Ethan Chen8d420ed2013-11-14 00:40:54 -0800477#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530478uint32_t AudioTrack::latency() const
479{
480 if (mAudioDirectOutput != -1) {
481 return mAudioFlinger->latency(mAudioDirectOutput);
482 } else if (mOutput != 0) {
483 uint32_t afLatency = 0;
484 uint32_t newLatency = 0;
485 AudioSystem::getLatency(mOutput, mStreamType, &afLatency);
486 if (0 != mSampleRate){
487 newLatency = afLatency + (1000*mCblk->frameCount_) / mSampleRate;
488 }
489 ALOGD("latency() mLatency = %d, newLatency = %d", mLatency, newLatency);
490 return newLatency;
491 }
492 return mLatency;
493}
Ethan Chen8d420ed2013-11-14 00:40:54 -0800494#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530495
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100496status_t AudioTrack::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497{
Alok Kediya601e9b52013-09-27 11:55:10 +0530498 status_t status = NO_ERROR;
Eric Laurentf5aafb22010-11-18 08:40:16 -0800499 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800500 if (mState == STATE_ACTIVE) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100501 return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 }
503
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800504 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800506 State previousState = mState;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100507 if (previousState == STATE_PAUSED_STOPPING) {
508 mState = STATE_STOPPING;
509 } else {
510 mState = STATE_ACTIVE;
511 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800512#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530513 if (mDirectTrack != NULL) {
514 mDirectTrack->start();
515 return status;
516 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800517#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800518 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
519 // reset current position as seen by client to 0
520 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
Eric Laurentec9a0322013-08-28 10:23:01 -0700521 // force refresh of remaining frames by processAudioBuffer() as last
522 // write before stop could be partial.
523 mRefreshRemaining = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800524 }
525 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
Glenn Kasten96f60d82013-07-12 10:21:18 -0700526 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800529 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100530 if (previousState == STATE_STOPPING) {
531 mProxy->interrupt();
532 } else {
533 t->resume();
534 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800535 } else {
536 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
537 get_sched_policy(0, &mPreviousSchedulingGroup);
538 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
539 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800541 if (!(flags & CBLK_INVALID)) {
542 status = mAudioTrack->start();
543 if (status == DEAD_OBJECT) {
544 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800546 }
547 if (flags & CBLK_INVALID) {
548 status = restoreTrack_l("start");
549 }
550
551 if (status != NO_ERROR) {
552 ALOGE("start() status %d", status);
553 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 if (t != 0) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100555 if (previousState != STATE_STOPPING) {
556 t->pause();
557 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700559 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700560 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561 }
562 }
563
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100564 return status;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800565}
566
567void AudioTrack::stop()
568{
569 AutoMutex lock(mLock);
570 // FIXME pause then stop should not be a nop
571 if (mState != STATE_ACTIVE) {
572 return;
573 }
574
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100575 if (isOffloaded()) {
576 mState = STATE_STOPPING;
577 } else {
578 mState = STATE_STOPPED;
579 }
580
Ethan Chen8d420ed2013-11-14 00:40:54 -0800581#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530582 if(mDirectTrack != NULL) {
583 mDirectTrack->stop();
584 } else if (mAudioTrack != NULL) {
Ethan Chen8d420ed2013-11-14 00:40:54 -0800585#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530586 mProxy->interrupt();
587 mAudioTrack->stop();
588 // the playback head position will reset to 0, so if a marker is set, we need
589 // to activate it again
590 mMarkerReached = false;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800591#if 0
Alok Kediya601e9b52013-09-27 11:55:10 +0530592 // Force flush if a shared buffer is used otherwise audioflinger
593 // will not stop before end of buffer is reached.
594 // It may be needed to make sure that we stop playback, likely in case looping is on.
595 if (mSharedBuffer != 0) {
596 flush_l();
597 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800598#endif
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100599
Alok Kediya601e9b52013-09-27 11:55:10 +0530600 sp<AudioTrackThread> t = mAudioTrackThread;
601 if (t != 0) {
602 if (!isOffloaded()) {
603 t->pause();
604 }
605 } else {
606 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
607 set_sched_policy(0, mPreviousSchedulingGroup);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100608 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800609#ifdef QCOM_HARDWARE
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800610 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800611#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612}
613
614bool AudioTrack::stopped() const
615{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800616 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800617 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618}
619
620void AudioTrack::flush()
621{
Ethan Chen8d420ed2013-11-14 00:40:54 -0800622#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530623 if(mDirectTrack != NULL) {
624 ALOGE("mdirect track flush");
625 mDirectTrack->flush();
626 return ;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800627 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800628#else
629 if (mSharedBuffer != 0) {
630 return;
631 }
632 AutoMutex lock(mLock);
633#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800634 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
635 return;
636 }
637 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800638}
639
Eric Laurent1703cdf2011-03-07 14:52:59 -0800640void AudioTrack::flush_l()
641{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800642 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700643
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700644 // clear playback marker and periodic update counter
645 mMarkerPosition = 0;
646 mMarkerReached = false;
647 mUpdatePeriod = 0;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100648 mRefreshRemaining = true;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700649
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800650 mState = STATE_FLUSHED;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100651 if (isOffloaded()) {
652 mProxy->interrupt();
653 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800654 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800655 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656}
657
658void AudioTrack::pause()
659{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800660 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100661 if (mState == STATE_ACTIVE) {
662 mState = STATE_PAUSED;
663 } else if (mState == STATE_STOPPING) {
664 mState = STATE_PAUSED_STOPPING;
665 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800666 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800668#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530669 if(mDirectTrack != NULL) {
670 ALOGV("mDirectTrack pause");
671 mDirectTrack->pause();
672 } else {
Ethan Chen8d420ed2013-11-14 00:40:54 -0800673#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530674 mProxy->interrupt();
675 mAudioTrack->pause();
Ethan Chen8d420ed2013-11-14 00:40:54 -0800676#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530677 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800678#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679}
680
Eric Laurentbe916aa2010-06-01 23:49:17 -0700681status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800683 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700684 return BAD_VALUE;
685 }
686
Eric Laurent1703cdf2011-03-07 14:52:59 -0800687 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688 mVolume[LEFT] = left;
689 mVolume[RIGHT] = right;
Ethan Chen8d420ed2013-11-14 00:40:54 -0800690#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530691 if(mDirectTrack != NULL) {
692 ALOGV("mDirectTrack->setVolume(left = %f , right = %f)", left,right);
693 mDirectTrack->setVolume(left, right);
694 } else {
Ethan Chen8d420ed2013-11-14 00:40:54 -0800695#endif
Alok Kediya601e9b52013-09-27 11:55:10 +0530696 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Ethan Chen8d420ed2013-11-14 00:40:54 -0800697#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530698 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800699#endif
Eric Laurentbe916aa2010-06-01 23:49:17 -0700700
Eric Laurent59fe0102013-09-27 18:48:26 -0700701 if (isOffloaded()) {
702 mAudioTrack->signal();
703 }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700704 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705}
706
Glenn Kastenb1c09932012-02-27 16:21:04 -0800707status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800709 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700710}
711
Eric Laurent2beeb502010-07-16 07:43:46 -0700712status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700713{
Ethan Chen8d420ed2013-11-14 00:40:54 -0800714#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530715 if (mDirectTrack != NULL) {
716 return NO_ERROR;
717 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800718#endif
Glenn Kasten05632a52012-01-03 14:22:33 -0800719 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700720 return BAD_VALUE;
721 }
722
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800723 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700724 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800725 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700726
727 return NO_ERROR;
728}
729
Glenn Kastena5224f32012-01-04 12:41:44 -0800730void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700731{
732 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800733 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700734 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735}
736
Glenn Kasten3b16c762012-11-14 08:44:39 -0800737status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100739 if (mIsTimed || isOffloaded()) {
John Grossman4ff14ba2012-02-08 16:37:41 -0800740 return INVALID_OPERATION;
741 }
742
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800743 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800744 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700745 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746 }
747 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700748 if (rate == 0 || rate > afSamplingRate*2 ) {
749 return BAD_VALUE;
750 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751
Eric Laurent1703cdf2011-03-07 14:52:59 -0800752 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800753 mSampleRate = rate;
754 mProxy->setSampleRate(rate);
755
Eric Laurent57326622009-07-07 07:10:45 -0700756 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800757}
758
Glenn Kastena5224f32012-01-04 12:41:44 -0800759uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760{
John Grossman4ff14ba2012-02-08 16:37:41 -0800761 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800762 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800763 }
764
Eric Laurent1703cdf2011-03-07 14:52:59 -0800765 AutoMutex lock(mLock);
Ethan Chen8d420ed2013-11-14 00:40:54 -0800766#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +0530767 if(mAudioDirectOutput != -1) {
768 return mAudioFlinger->sampleRate(mAudioDirectOutput);
769 }
Ethan Chen8d420ed2013-11-14 00:40:54 -0800770#endif
Glenn Kastene3aa6592012-12-04 12:22:46 -0800771 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800772}
773
774status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
775{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100776 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800777 return INVALID_OPERATION;
778 }
779
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800780 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800781 ;
782 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
783 loopEnd - loopStart >= MIN_LOOP) {
784 ;
785 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800786 return BAD_VALUE;
787 }
788
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800789 AutoMutex lock(mLock);
790 // See setPosition() regarding setting parameters such as loop points or position while active
791 if (mState == STATE_ACTIVE) {
792 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700793 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800794 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800795 return NO_ERROR;
796}
797
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800798void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
799{
800 // FIXME If setting a loop also sets position to start of loop, then
801 // this is correct. Otherwise it should be removed.
802 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
803 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
804 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
805}
806
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800807status_t AudioTrack::setMarkerPosition(uint32_t marker)
808{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700809 // The only purpose of setting marker position is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100810 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700811 return INVALID_OPERATION;
812 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800813
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800814 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800815 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700816 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800817
818 return NO_ERROR;
819}
820
Glenn Kastena5224f32012-01-04 12:41:44 -0800821status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800822{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100823 if (isOffloaded()) {
824 return INVALID_OPERATION;
825 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700826 if (marker == NULL) {
827 return BAD_VALUE;
828 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800829
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800830 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831 *marker = mMarkerPosition;
832
833 return NO_ERROR;
834}
835
836status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
837{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -0700838 // The only purpose of setting position update period is to get a callback
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100839 if (mCbf == NULL || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700840 return INVALID_OPERATION;
841 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800842
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800843 AutoMutex lock(mLock);
844 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800845 mUpdatePeriod = updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800846 return NO_ERROR;
847}
848
Glenn Kastena5224f32012-01-04 12:41:44 -0800849status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800850{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100851 if (isOffloaded()) {
852 return INVALID_OPERATION;
853 }
Glenn Kastend65d73c2012-06-22 17:21:07 -0700854 if (updatePeriod == NULL) {
855 return BAD_VALUE;
856 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800857
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800858 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800859 *updatePeriod = mUpdatePeriod;
860
861 return NO_ERROR;
862}
863
864status_t AudioTrack::setPosition(uint32_t position)
865{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100866 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700867 return INVALID_OPERATION;
868 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800869 if (position > mFrameCount) {
870 return BAD_VALUE;
871 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800872
Eric Laurent1703cdf2011-03-07 14:52:59 -0800873 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800874 // Currently we require that the player is inactive before setting parameters such as position
875 // or loop points. Otherwise, there could be a race condition: the application could read the
876 // current position, compute a new position or loop parameters, and then set that position or
877 // loop parameters but it would do the "wrong" thing since the position has continued to advance
878 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
879 // to specify how it wants to handle such scenarios.
880 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700881 return INVALID_OPERATION;
882 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800883 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
884 mLoopPeriod = 0;
885 // FIXME Check whether loops and setting position are incompatible in old code.
886 // If we use setLoop for both purposes we lose the capability to set the position while looping.
887 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700888
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800889 return NO_ERROR;
890}
891
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800892status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800893{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700894 if (position == NULL) {
895 return BAD_VALUE;
896 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800897
Eric Laurent1703cdf2011-03-07 14:52:59 -0800898 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100899 if (isOffloaded()) {
900 uint32_t dspFrames = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800901
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100902 if (mOutput != 0) {
903 uint32_t halFrames;
904 AudioSystem::getRenderPosition(mOutput, &halFrames, &dspFrames);
905 }
906 *position = dspFrames;
907 } else {
908 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
909 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
910 mProxy->getPosition();
911 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800912 return NO_ERROR;
913}
914
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800915status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800916{
917 if (mSharedBuffer == 0 || mIsTimed) {
918 return INVALID_OPERATION;
919 }
920 if (position == NULL) {
921 return BAD_VALUE;
922 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800923
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800924 AutoMutex lock(mLock);
925 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800926 return NO_ERROR;
927}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800928
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929status_t AudioTrack::reload()
930{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100931 if (mSharedBuffer == 0 || mIsTimed || isOffloaded()) {
Glenn Kasten083d1c12012-11-30 15:00:36 -0800932 return INVALID_OPERATION;
933 }
934
Eric Laurent1703cdf2011-03-07 14:52:59 -0800935 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800936 // See setPosition() regarding setting parameters such as loop points or position while active
937 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700938 return INVALID_OPERATION;
939 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800940 mNewPosition = mUpdatePeriod;
941 mLoopPeriod = 0;
942 // FIXME The new code cannot reload while keeping a loop specified.
943 // Need to check how the old code handled this, and whether it's a significant change.
944 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 return NO_ERROR;
946}
947
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948audio_io_handle_t AudioTrack::getOutput()
949{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800950 AutoMutex lock(mLock);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100951 return mOutput;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800952}
953
954// must be called with mLock held
955audio_io_handle_t AudioTrack::getOutput_l()
956{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100957 if (mOutput) {
958 return mOutput;
959 } else {
960 return AudioSystem::getOutput(mStreamType,
961 mSampleRate, mFormat, mChannelMask, mFlags);
962 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700963}
964
Eric Laurentbe916aa2010-06-01 23:49:17 -0700965status_t AudioTrack::attachAuxEffect(int effectId)
966{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800967 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700968 status_t status = mAudioTrack->attachAuxEffect(effectId);
969 if (status == NO_ERROR) {
970 mAuxEffectId = effectId;
971 }
972 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700973}
974
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975// -------------------------------------------------------------------------
976
Eric Laurent1703cdf2011-03-07 14:52:59 -0800977// must be called with mLock held
978status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800979 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800980 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800981 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800982 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700983 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800984 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800985 audio_io_handle_t output,
986 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800987{
988 status_t status;
989 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
990 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700991 ALOGE("Could not get audioflinger");
992 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800993 }
994
Glenn Kastence8828a2013-09-16 18:07:38 -0700995 // Not all of these values are needed under all conditions, but it is easier to get them all
996
Alok Kediya601e9b52013-09-27 11:55:10 +0530997 uint32_t afLatency = 0;
Glenn Kastence8828a2013-09-16 18:07:38 -0700998 status = AudioSystem::getLatency(output, streamType, &afLatency);
999 if (status != NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001000 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001001 return NO_INIT;
1002 }
1003
Alok Kediya601e9b52013-09-27 11:55:10 +05301004 size_t afFrameCount = 0;
Glenn Kastence8828a2013-09-16 18:07:38 -07001005 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
1006 if (status != NO_ERROR) {
1007 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
1008 return NO_INIT;
1009 }
1010
Alok Kediya601e9b52013-09-27 11:55:10 +05301011 uint32_t afSampleRate = 0;
Glenn Kastence8828a2013-09-16 18:07:38 -07001012 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
1013 if (status != NO_ERROR) {
1014 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType, status);
1015 return NO_INIT;
1016 }
1017
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001018 // Client decides whether the track is TIMED (see below), but can only express a preference
1019 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001020 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001021 // either of these use cases:
1022 // use case 1: shared buffer
1023 (sharedBuffer != 0) ||
1024 // use case 2: callback handler
1025 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001026 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -07001027 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001028 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -07001029 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001030 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001031 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001032
Glenn Kastence8828a2013-09-16 18:07:38 -07001033 // The client's AudioTrack buffer is divided into n parts for purpose of wakeup by server, where
1034 // n = 1 fast track; nBuffering is ignored
1035 // n = 2 normal track, no sample rate conversion
1036 // n = 3 normal track, with sample rate conversion
1037 // (pessimistic; some non-1:1 conversion ratios don't actually need triple-buffering)
1038 // n > 3 very high latency or very small notification interval; nBuffering is ignored
1039 const uint32_t nBuffering = (sampleRate == afSampleRate) ? 2 : 3;
1040
Eric Laurentd1b449a2010-05-14 03:26:45 -07001041 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -07001042
Dima Zavinfce7a472011-04-19 22:30:36 -07001043 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001044
Eric Laurentd1b449a2010-05-14 03:26:45 -07001045 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001046 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -07001047 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -07001048 } else if (frameCount == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001049 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001050 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001051 if (mNotificationFramesAct != frameCount) {
1052 mNotificationFramesAct = frameCount;
1053 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001054 } else if (sharedBuffer != 0) {
1055
Glenn Kastena42ff002012-11-14 12:47:55 -08001056 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -07001057 // 8-bit data in shared memory is not currently supported by AudioFlinger
1058 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -08001059 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001060 // More than 2 channels does not require stronger alignment than stereo
1061 alignment <<= 1;
1062 }
Glenn Kastena42ff002012-11-14 12:47:55 -08001063 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
1064 ALOGE("Invalid buffer alignment: address %p, channel count %u",
1065 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001066 return BAD_VALUE;
1067 }
1068
1069 // When initializing a shared buffer AudioTrack via constructors,
1070 // there's no frameCount parameter.
1071 // But when initializing a shared buffer AudioTrack via set(),
1072 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -08001073 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001074
1075 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
1076
1077 // FIXME move these calculations and associated checks to server
Glenn Kastene0fa4672012-04-24 14:35:14 -07001078
Eric Laurentd1b449a2010-05-14 03:26:45 -07001079 // Ensure that buffer depth covers at least audio hardware latency
Alok Kediya601e9b52013-09-27 11:55:10 +05301080 if(!afSampleRate && !afFrameCount) {
1081 ALOGW("samplerate or framecount zero");
1082 return NO_INIT;
1083 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001084 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -07001085 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
1086 afFrameCount, minBufCount, afSampleRate, afLatency);
Glenn Kastence8828a2013-09-16 18:07:38 -07001087 if (minBufCount <= nBuffering) {
1088 minBufCount = nBuffering;
Glenn Kasten7c027242012-12-26 14:43:16 -08001089 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001090
Glenn Kastene33054e2012-11-14 12:54:39 -08001091 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
1092 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -08001093 ", afLatency=%d",
1094 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001095
1096 if (frameCount == 0) {
1097 frameCount = minFrameCount;
Glenn Kastence8828a2013-09-16 18:07:38 -07001098 } else if (frameCount < minFrameCount) {
Glenn Kastene0fa4672012-04-24 14:35:14 -07001099 // not ALOGW because it happens all the time when playing key clicks over A2DP
1100 ALOGV("Minimum buffer size corrected from %d to %d",
1101 frameCount, minFrameCount);
1102 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001103 }
Glenn Kastence8828a2013-09-16 18:07:38 -07001104 // Make sure that application is notified with sufficient margin before underrun
1105 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1106 mNotificationFramesAct = frameCount/nBuffering;
1107 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001108
Glenn Kastene0fa4672012-04-24 14:35:14 -07001109 } else {
1110 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -07001111 }
1112
Glenn Kastena075db42012-03-06 11:22:44 -08001113 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
1114 if (mIsTimed) {
1115 trackFlags |= IAudioFlinger::TRACK_TIMED;
1116 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001117
1118 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001119 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001120 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001121 if (mAudioTrackThread != 0) {
1122 tid = mAudioTrackThread->getTid();
1123 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001124 }
1125
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001126 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1127 trackFlags |= IAudioFlinger::TRACK_OFFLOAD;
1128 }
1129
Glenn Kasten8d6cc842012-02-03 11:06:53 -08001130 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001131 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -07001132 // AudioFlinger only sees 16-bit PCM
1133 format == AUDIO_FORMAT_PCM_8_BIT ?
1134 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -08001135 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001136 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -08001137 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001138 sharedBuffer,
1139 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001140 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001141 &mSessionId,
Glenn Kastend054c322013-07-12 12:59:20 -07001142 mName,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001143 &status);
1144
1145 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001146 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001147 return status;
1148 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001149 sp<IMemory> iMem = track->getCblk();
1150 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001151 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001152 return NO_INIT;
1153 }
Glenn Kasten53cec222013-08-29 09:01:02 -07001154 // invariant that mAudioTrack != 0 is true only after set() returns successfully
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001155 if (mAudioTrack != 0) {
1156 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
1157 mDeathNotifier.clear();
1158 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001159 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001160 mCblkMemory = iMem;
1161 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
1162 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001163 size_t temp = cblk->frameCount_;
1164 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
1165 // In current design, AudioTrack client checks and ensures frame count validity before
1166 // passing it to AudioFlinger so AudioFlinger should not return a different value except
1167 // for fast track as it uses a special method of assigning frame count.
1168 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
1169 }
1170 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001171 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001172 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -08001173 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001174 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -07001175 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001176 if (sharedBuffer == 0) {
1177 // double-buffering is not required for fast tracks, due to tighter scheduling
1178 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount) {
1179 mNotificationFramesAct = frameCount;
1180 }
1181 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001182 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -08001183 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001184 // once denied, do not request again if IAudioTrack is re-created
1185 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1186 mFlags = flags;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001187 if (sharedBuffer == 0) {
Glenn Kastence8828a2013-09-16 18:07:38 -07001188 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/nBuffering) {
1189 mNotificationFramesAct = frameCount/nBuffering;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001190 }
1191 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001192 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001193 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001194 if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1195 if (trackFlags & IAudioFlinger::TRACK_OFFLOAD) {
1196 ALOGV("AUDIO_OUTPUT_FLAG_OFFLOAD successful");
1197 } else {
1198 ALOGW("AUDIO_OUTPUT_FLAG_OFFLOAD denied by server");
1199 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1200 mFlags = flags;
1201 return NO_INIT;
1202 }
1203 }
1204
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001205 mRefreshRemaining = true;
1206
1207 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
1208 // is the value of pointer() for the shared buffer, otherwise buffers points
1209 // immediately after the control block. This address is for the mapping within client
1210 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
1211 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001212 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001213 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001214 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001215 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001216 }
1217
Eric Laurent2beeb502010-07-16 07:43:46 -07001218 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001219 // FIXME don't believe this lie
Alok Kediya601e9b52013-09-27 11:55:10 +05301220 if(sampleRate){
1221 mLatency = afLatency + (1000*frameCount) / sampleRate;
1222 }else{
1223 mLatency = afLatency;
1224 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001225 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001226 // If IAudioTrack is re-created, don't let the requested frameCount
1227 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -08001228 if (frameCount > mReqFrameCount) {
1229 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -07001230 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001231
1232 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001233 if (sharedBuffer == 0) {
1234 mStaticProxy.clear();
1235 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1236 } else {
1237 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
1238 mProxy = mStaticProxy;
1239 }
Glenn Kastene3aa6592012-12-04 12:22:46 -08001240 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
1241 uint16_t(mVolume[LEFT] * 0x1000));
1242 mProxy->setSendLevel(mSendLevel);
1243 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001244 mProxy->setEpoch(epoch);
1245 mProxy->setMinimum(mNotificationFramesAct);
1246
1247 mDeathNotifier = new DeathNotifier(this);
1248 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001249
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001250 return NO_ERROR;
1251}
1252
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1254{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001255 if (audioBuffer == NULL) {
1256 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001257 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001258 if (mTransfer != TRANSFER_OBTAIN) {
1259 audioBuffer->frameCount = 0;
1260 audioBuffer->size = 0;
1261 audioBuffer->raw = NULL;
1262 return INVALID_OPERATION;
1263 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001264
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001265 const struct timespec *requested;
1266 if (waitCount == -1) {
1267 requested = &ClientProxy::kForever;
1268 } else if (waitCount == 0) {
1269 requested = &ClientProxy::kNonBlocking;
1270 } else if (waitCount > 0) {
1271 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1272 struct timespec timeout;
1273 timeout.tv_sec = ms / 1000;
1274 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1275 requested = &timeout;
1276 } else {
1277 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1278 requested = NULL;
1279 }
1280 return obtainBuffer(audioBuffer, requested);
1281}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001282
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001283status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1284 struct timespec *elapsed, size_t *nonContig)
1285{
1286 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1287 uint32_t oldSequence = 0;
1288 uint32_t newSequence;
1289
1290 Proxy::Buffer buffer;
1291 status_t status = NO_ERROR;
1292
1293 static const int32_t kMaxTries = 5;
1294 int32_t tryCounter = kMaxTries;
1295
1296 do {
1297 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1298 // keep them from going away if another thread re-creates the track during obtainBuffer()
1299 sp<AudioTrackClientProxy> proxy;
1300 sp<IMemory> iMem;
1301
1302 { // start of lock scope
1303 AutoMutex lock(mLock);
1304
1305 newSequence = mSequence;
1306 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1307 if (status == DEAD_OBJECT) {
1308 // re-create track, unless someone else has already done so
1309 if (newSequence == oldSequence) {
1310 status = restoreTrack_l("obtainBuffer");
1311 if (status != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001312 buffer.mFrameCount = 0;
1313 buffer.mRaw = NULL;
1314 buffer.mNonContig = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001315 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001317 }
1318 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001319 oldSequence = newSequence;
1320
1321 // Keep the extra references
1322 proxy = mProxy;
1323 iMem = mCblkMemory;
1324
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001325 if (mState == STATE_STOPPING) {
1326 status = -EINTR;
1327 buffer.mFrameCount = 0;
1328 buffer.mRaw = NULL;
1329 buffer.mNonContig = 0;
1330 break;
1331 }
1332
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001333 // Non-blocking if track is stopped or paused
1334 if (mState != STATE_ACTIVE) {
1335 requested = &ClientProxy::kNonBlocking;
1336 }
1337
1338 } // end of lock scope
1339
1340 buffer.mFrameCount = audioBuffer->frameCount;
1341 // FIXME starts the requested timeout and elapsed over from scratch
1342 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1343
1344 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1345
1346 audioBuffer->frameCount = buffer.mFrameCount;
1347 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1348 audioBuffer->raw = buffer.mRaw;
1349 if (nonContig != NULL) {
1350 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001351 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001352 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001353}
1354
1355void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1356{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001357 if (mTransfer == TRANSFER_SHARED) {
1358 return;
1359 }
1360
1361 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1362 if (stepCount == 0) {
1363 return;
1364 }
1365
1366 Proxy::Buffer buffer;
1367 buffer.mFrameCount = stepCount;
1368 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001369
Eric Laurent1703cdf2011-03-07 14:52:59 -08001370 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001371 mInUnderrun = false;
1372 mProxy->releaseBuffer(&buffer);
1373
1374 // restart track if it was disabled by audioflinger due to previous underrun
1375 if (mState == STATE_ACTIVE) {
1376 audio_track_cblk_t* cblk = mCblk;
Alok Kediya601e9b52013-09-27 11:55:10 +05301377 if (cblk->mFlags & CBLK_DISABLED) {
1378 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Glenn Kastend054c322013-07-12 12:59:20 -07001379 ALOGW("releaseBuffer() track %p name=%s disabled due to previous underrun, restarting",
1380 this, mName.string());
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001381 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001382 mAudioTrack->start();
1383 }
1384 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001385}
1386
1387// -------------------------------------------------------------------------
1388
1389ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1390{
Ethan Chen8d420ed2013-11-14 00:40:54 -08001391#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301392 if (mDirectTrack != NULL) {
1393 ssize_t written = 0;
1394 written = mDirectTrack->write(buffer,userSize);
1395 return written;
1396 }
Ethan Chen8d420ed2013-11-14 00:40:54 -08001397#endif
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001398 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001399 return INVALID_OPERATION;
1400 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001401
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001402 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001403 // Sanity-check: user is most-likely passing an error code, and it would
1404 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001405 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001406 return BAD_VALUE;
1407 }
1408
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001409 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410 Buffer audioBuffer;
1411
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001412 while (userSize >= mFrameSize) {
1413 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001414
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001415 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001416 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001417 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001418 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001419 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 return ssize_t(err);
1421 }
1422
1423 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001424 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001426 toWrite = audioBuffer.size >> 1;
1427 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001428 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001430 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001431 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001432 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001433 userSize -= toWrite;
1434 written += toWrite;
1435
1436 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001437 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438
1439 return written;
1440}
1441
1442// -------------------------------------------------------------------------
1443
John Grossman4ff14ba2012-02-08 16:37:41 -08001444TimedAudioTrack::TimedAudioTrack() {
1445 mIsTimed = true;
1446}
1447
1448status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1449{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001450 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001451 status_t result = UNKNOWN_ERROR;
1452
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001453#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001454 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1455 // while we are accessing the cblk
1456 sp<IAudioTrack> audioTrack = mAudioTrack;
1457 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001458#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001459
John Grossman4ff14ba2012-02-08 16:37:41 -08001460 // If the track is not invalid already, try to allocate a buffer. alloc
1461 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001462 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001463 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001464 if (!(cblk->mFlags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001465 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1466 if (result == DEAD_OBJECT) {
Glenn Kasten96f60d82013-07-12 10:21:18 -07001467 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001468 }
1469 }
1470
1471 // If the track is invalid at this point, attempt to restore it. and try the
1472 // allocation one more time.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001473 if (cblk->mFlags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001474 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001475
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001476 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001477 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001478 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001479 }
1480
1481 return result;
1482}
1483
1484status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1485 int64_t pts)
1486{
Eric Laurentdf839842012-05-31 14:27:14 -07001487 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1488 {
1489 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001490 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001491 // restart track if it was disabled by audioflinger due to previous underrun
1492 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten96f60d82013-07-12 10:21:18 -07001493 (mState == STATE_ACTIVE) && (cblk->mFlags & CBLK_DISABLED)) {
1494 android_atomic_and(~CBLK_DISABLED, &cblk->mFlags);
Eric Laurentdf839842012-05-31 14:27:14 -07001495 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001496 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001497 mAudioTrack->start();
1498 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001499 }
Eric Laurentdf839842012-05-31 14:27:14 -07001500 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001501}
1502
1503status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1504 TargetTimeline target)
1505{
1506 return mAudioTrack->setMediaTimeTransform(xform, target);
1507}
1508
1509// -------------------------------------------------------------------------
1510
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001511nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001512{
Glenn Kastenfb1fdc92013-07-10 17:03:19 -07001513 // Currently the AudioTrack thread is not created if there are no callbacks.
1514 // Would it ever make sense to run the thread, even without callbacks?
1515 // If so, then replace this by checks at each use for mCbf != NULL.
1516 LOG_ALWAYS_FATAL_IF(mCblk == NULL);
1517
Eric Laurent1703cdf2011-03-07 14:52:59 -08001518 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001519 if (mAwaitBoost) {
1520 mAwaitBoost = false;
1521 mLock.unlock();
1522 static const int32_t kMaxTries = 5;
1523 int32_t tryCounter = kMaxTries;
1524 uint32_t pollUs = 10000;
1525 do {
1526 int policy = sched_getscheduler(0);
1527 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1528 break;
1529 }
1530 usleep(pollUs);
1531 pollUs <<= 1;
1532 } while (tryCounter-- > 0);
1533 if (tryCounter < 0) {
1534 ALOGE("did not receive expected priority boost on time");
1535 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001536 // Run again immediately
1537 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001538 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001539
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001540 // Can only reference mCblk while locked
1541 int32_t flags = android_atomic_and(
Glenn Kasten96f60d82013-07-12 10:21:18 -07001542 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->mFlags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001543
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001544 // Check for track invalidation
1545 if (flags & CBLK_INVALID) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001546 // for offloaded tracks restoreTrack_l() will just update the sequence and clear
1547 // AudioSystem cache. We should not exit here but after calling the callback so
1548 // that the upper layers can recreate the track
1549 if (!isOffloaded() || (mSequence == mObservedSequence)) {
1550 status_t status = restoreTrack_l("processAudioBuffer");
1551 mLock.unlock();
1552 // Run again immediately, but with a new IAudioTrack
1553 return 0;
1554 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001555 }
1556
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001557 bool waitStreamEnd = mState == STATE_STOPPING;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001558 bool active = mState == STATE_ACTIVE;
1559
1560 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1561 bool newUnderrun = false;
1562 if (flags & CBLK_UNDERRUN) {
1563#if 0
1564 // Currently in shared buffer mode, when the server reaches the end of buffer,
1565 // the track stays active in continuous underrun state. It's up to the application
1566 // to pause or stop the track, or set the position to a new offset within buffer.
1567 // This was some experimental code to auto-pause on underrun. Keeping it here
1568 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1569 if (mTransfer == TRANSFER_SHARED) {
1570 mState = STATE_PAUSED;
1571 active = false;
1572 }
1573#endif
1574 if (!mInUnderrun) {
1575 mInUnderrun = true;
1576 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577 }
1578 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001579
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001580 // Get current position of server
1581 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582
1583 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001584 bool markerReached = false;
1585 size_t markerPosition = mMarkerPosition;
1586 // FIXME fails for wraparound, need 64 bits
1587 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1588 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001589 }
1590
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001591 // Determine number of new position callback(s) that will be needed, while locked
1592 size_t newPosCount = 0;
1593 size_t newPosition = mNewPosition;
1594 size_t updatePeriod = mUpdatePeriod;
1595 // FIXME fails for wraparound, need 64 bits
1596 if (updatePeriod > 0 && position >= newPosition) {
1597 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1598 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 }
1600
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001601 // Cache other fields that will be needed soon
1602 uint32_t loopPeriod = mLoopPeriod;
1603 uint32_t sampleRate = mSampleRate;
1604 size_t notificationFrames = mNotificationFramesAct;
1605 if (mRefreshRemaining) {
1606 mRefreshRemaining = false;
1607 mRemainingFrames = notificationFrames;
1608 mRetryOnPartialBuffer = false;
1609 }
1610 size_t misalignment = mProxy->getMisalignment();
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001611 uint32_t sequence = mSequence;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001612
1613 // These fields don't need to be cached, because they are assigned only by set():
1614 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1615 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1616
1617 mLock.unlock();
1618
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001619 if (waitStreamEnd) {
1620 AutoMutex lock(mLock);
1621
1622 sp<AudioTrackClientProxy> proxy = mProxy;
1623 sp<IMemory> iMem = mCblkMemory;
1624
1625 struct timespec timeout;
1626 timeout.tv_sec = WAIT_STREAM_END_TIMEOUT_SEC;
1627 timeout.tv_nsec = 0;
1628
1629 mLock.unlock();
1630 status_t status = mProxy->waitStreamEndDone(&timeout);
1631 mLock.lock();
1632 switch (status) {
1633 case NO_ERROR:
1634 case DEAD_OBJECT:
1635 case TIMED_OUT:
1636 mLock.unlock();
1637 mCbf(EVENT_STREAM_END, mUserData, NULL);
1638 mLock.lock();
1639 if (mState == STATE_STOPPING) {
1640 mState = STATE_STOPPED;
1641 if (status != DEAD_OBJECT) {
1642 return NS_INACTIVE;
1643 }
1644 }
1645 return 0;
1646 default:
1647 return 0;
1648 }
1649 }
1650
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001651 // perform callbacks while unlocked
1652 if (newUnderrun) {
1653 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1654 }
1655 // FIXME we will miss loops if loop cycle was signaled several times since last call
1656 // to processAudioBuffer()
1657 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1658 mCbf(EVENT_LOOP_END, mUserData, NULL);
1659 }
1660 if (flags & CBLK_BUFFER_END) {
1661 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1662 }
1663 if (markerReached) {
1664 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1665 }
1666 while (newPosCount > 0) {
1667 size_t temp = newPosition;
1668 mCbf(EVENT_NEW_POS, mUserData, &temp);
1669 newPosition += updatePeriod;
1670 newPosCount--;
1671 }
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001672
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001673 if (mObservedSequence != sequence) {
1674 mObservedSequence = sequence;
1675 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001676 // for offloaded tracks, just wait for the upper layers to recreate the track
1677 if (isOffloaded()) {
1678 return NS_INACTIVE;
1679 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001680 }
1681
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001682 // if inactive, then don't run me again until re-started
1683 if (!active) {
1684 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001685 }
1686
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001687 // Compute the estimated time until the next timed event (position, markers, loops)
1688 // FIXME only for non-compressed audio
1689 uint32_t minFrames = ~0;
1690 if (!markerReached && position < markerPosition) {
1691 minFrames = markerPosition - position;
1692 }
1693 if (loopPeriod > 0 && loopPeriod < minFrames) {
1694 minFrames = loopPeriod;
1695 }
1696 if (updatePeriod > 0 && updatePeriod < minFrames) {
1697 minFrames = updatePeriod;
1698 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001699
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001700 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1701 static const uint32_t kPoll = 0;
1702 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1703 minFrames = kPoll * notificationFrames;
1704 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001705
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001706 // Convert frame units to time units
1707 nsecs_t ns = NS_WHENEVER;
1708 if (minFrames != (uint32_t) ~0) {
1709 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1710 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1711 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1712 }
1713
1714 // If not supplying data by EVENT_MORE_DATA, then we're done
1715 if (mTransfer != TRANSFER_CALLBACK) {
1716 return ns;
1717 }
1718
1719 struct timespec timeout;
1720 const struct timespec *requested = &ClientProxy::kForever;
1721 if (ns != NS_WHENEVER) {
1722 timeout.tv_sec = ns / 1000000000LL;
1723 timeout.tv_nsec = ns % 1000000000LL;
1724 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1725 requested = &timeout;
1726 }
1727
1728 while (mRemainingFrames > 0) {
1729
1730 Buffer audioBuffer;
1731 audioBuffer.frameCount = mRemainingFrames;
1732 size_t nonContig;
1733 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1734 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1735 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1736 requested = &ClientProxy::kNonBlocking;
1737 size_t avail = audioBuffer.frameCount + nonContig;
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001738 ALOGV("obtainBuffer(%u) returned %u = %u + %u err %d",
1739 mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001740 if (err != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001741 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR ||
1742 (isOffloaded() && (err == DEAD_OBJECT))) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001743 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001744 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001745 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1746 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001747 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001748
Eric Laurent42a6f422013-08-29 14:35:05 -07001749 if (mRetryOnPartialBuffer && !isOffloaded()) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001750 mRetryOnPartialBuffer = false;
1751 if (avail < mRemainingFrames) {
1752 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1753 if (ns < 0 || myns < ns) {
1754 ns = myns;
1755 }
1756 return ns;
1757 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001758 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001759
1760 // Divide buffer size by 2 to take into account the expansion
1761 // due to 8 to 16 bit conversion: the callback must fill only half
1762 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001763 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001764 audioBuffer.size >>= 1;
1765 }
1766
1767 size_t reqSize = audioBuffer.size;
1768 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001769 size_t writtenSize = audioBuffer.size;
1770 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001771
1772 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001773 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1774 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1775 reqSize, (int) writtenSize);
1776 return NS_NEVER;
1777 }
1778
1779 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001780 // The callback is done filling buffers
1781 // Keep this thread going to handle timed events and
1782 // still try to get more data in intervals of WAIT_PERIOD_MS
1783 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001784 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001785 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001786
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001787 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001788 // 8 to 16 bit conversion, note that source and destination are the same address
1789 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001790 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001791 }
1792
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001793 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1794 audioBuffer.frameCount = releasedFrames;
1795 mRemainingFrames -= releasedFrames;
1796 if (misalignment >= releasedFrames) {
1797 misalignment -= releasedFrames;
1798 } else {
1799 misalignment = 0;
1800 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001801
1802 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001803
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001804 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1805 // if callback doesn't like to accept the full chunk
1806 if (writtenSize < reqSize) {
1807 continue;
1808 }
1809
1810 // There could be enough non-contiguous frames available to satisfy the remaining request
1811 if (mRemainingFrames <= nonContig) {
1812 continue;
1813 }
1814
1815#if 0
1816 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1817 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1818 // that total to a sum == notificationFrames.
1819 if (0 < misalignment && misalignment <= mRemainingFrames) {
1820 mRemainingFrames = misalignment;
1821 return (mRemainingFrames * 1100000000LL) / sampleRate;
1822 }
1823#endif
1824
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001825 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001826 mRemainingFrames = notificationFrames;
1827 mRetryOnPartialBuffer = true;
1828
1829 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1830 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001831}
1832
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001833status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001834{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001835 ALOGW("dead IAudioTrack, %s, creating a new one from %s()",
1836 isOffloaded() ? "Offloaded" : "PCM", from);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001837 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001838 status_t result;
1839
Glenn Kastena47f3162012-11-07 10:13:08 -08001840 // refresh the audio configuration cache in this process to make sure we get new
1841 // output parameters in getOutput_l() and createTrack_l()
1842 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001843
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001844 if (isOffloaded()) {
1845 return DEAD_OBJECT;
1846 }
1847
1848 // force new output query from audio policy manager;
1849 mOutput = 0;
1850 audio_io_handle_t output = getOutput_l();
1851
Glenn Kastena47f3162012-11-07 10:13:08 -08001852 // if the new IAudioTrack is created, createTrack_l() will modify the
1853 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1854 // It will also delete the strong references on previous IAudioTrack and IMemory
Eric Laurentcc21e4f2013-10-16 15:12:32 -07001855
1856 // take the frames that will be lost by track recreation into account in saved position
1857 size_t position = mProxy->getPosition() + mProxy->getFramesFilled();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001858 mNewPosition = position + mUpdatePeriod;
1859 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001860 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001861 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001862 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001863 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001864 mFlags,
1865 mSharedBuffer,
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001866 output,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001867 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001868
Glenn Kastena47f3162012-11-07 10:13:08 -08001869 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001870 // continue playback from last known position, but
1871 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1872 if (mStaticProxy != NULL) {
1873 mLoopPeriod = 0;
1874 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1875 }
1876 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1877 // track destruction have been played? This is critical for SoundPool implementation
1878 // This must be broken, and needs to be tested/debugged.
1879#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001880 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001881 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001882 // Make sure that a client relying on callback events indicating underrun or
1883 // the actual amount of audio frames played (e.g SoundPool) receives them.
1884 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001885 // restart playback even if buffer is not completely filled.
Glenn Kasten96f60d82013-07-12 10:21:18 -07001886 android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001887 }
1888 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001889#endif
1890 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001891 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001892 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001893 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001894 if (result != NO_ERROR) {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001895 //Use of direct and offloaded output streams is ref counted by audio policy manager.
1896 // As getOutput was called above and resulted in an output stream to be opened,
1897 // we need to release it.
1898 AudioSystem::releaseOutput(output);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001899 ALOGW("restoreTrack_l() failed status %d", result);
1900 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001901 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001902
1903 return result;
1904}
1905
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001906status_t AudioTrack::setParameters(const String8& keyValuePairs)
1907{
1908 AutoMutex lock(mLock);
Glenn Kasten53cec222013-08-29 09:01:02 -07001909 return mAudioTrack->setParameters(keyValuePairs);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001910}
1911
Glenn Kastence703742013-07-19 16:33:58 -07001912status_t AudioTrack::getTimestamp(AudioTimestamp& timestamp)
1913{
Glenn Kasten53cec222013-08-29 09:01:02 -07001914 AutoMutex lock(mLock);
Glenn Kastenfe346c72013-08-30 13:28:22 -07001915 // FIXME not implemented for fast tracks; should use proxy and SSQ
1916 if (mFlags & AUDIO_OUTPUT_FLAG_FAST) {
1917 return INVALID_OPERATION;
1918 }
1919 if (mState != STATE_ACTIVE && mState != STATE_PAUSED) {
1920 return INVALID_OPERATION;
1921 }
1922 status_t status = mAudioTrack->getTimestamp(timestamp);
1923 if (status == NO_ERROR) {
1924 timestamp.mPosition += mProxy->getEpoch();
1925 }
1926 return status;
Glenn Kastence703742013-07-19 16:33:58 -07001927}
1928
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001929String8 AudioTrack::getParameters(const String8& keys)
1930{
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001931 if (mOutput) {
1932 return AudioSystem::getParameters(mOutput, keys);
1933 } else {
1934 return String8::empty();
1935 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001936}
1937
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001938status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1939{
1940
1941 const size_t SIZE = 256;
1942 char buffer[SIZE];
1943 String8 result;
1944
1945 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001946 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1947 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001948 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001949 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001950 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001951 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001952 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001953 result.append(buffer);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001954#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301955 uint32_t afLatency = 0;
1956 AudioSystem::getLatency(mOutput, mStreamType, &afLatency);
1957 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, afLatency + (1000*mCblk->frameCount_) / mSampleRate);
Ethan Chen8d420ed2013-11-14 00:40:54 -08001958#else
1959 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
1960#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001961 result.append(buffer);
1962 ::write(fd, result.string(), result.size());
1963 return NO_ERROR;
1964}
1965
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001966uint32_t AudioTrack::getUnderrunFrames() const
1967{
1968 AutoMutex lock(mLock);
1969 return mProxy->getUnderrunFrames();
1970}
1971
1972// =========================================================================
1973
1974void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1975{
1976 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1977 if (audioTrack != 0) {
1978 AutoMutex lock(audioTrack->mLock);
1979 audioTrack->mProxy->binderDied();
1980 }
1981}
1982
Ethan Chen8d420ed2013-11-14 00:40:54 -08001983#ifdef QCOM_HARDWARE
Alok Kediya601e9b52013-09-27 11:55:10 +05301984void AudioTrack::notify(int msg) {
1985 if (msg == EVENT_UNDERRUN) {
1986 ALOGV("Posting event underrun to Audio Sink.");
1987 mCbf(EVENT_UNDERRUN, mUserData, 0);
1988 }
1989 if (msg == EVENT_HW_FAIL) {
1990 ALOGV("Posting event HW fail to Audio Sink.");
1991 mCbf(EVENT_HW_FAIL, mUserData, 0);
1992 }
1993}
1994
1995status_t AudioTrack::getTimeStamp(uint64_t *tstamp) {
1996 if (mDirectTrack != NULL) {
1997 *tstamp = mDirectTrack->getTimeStamp();
1998 ALOGE("Timestamp %lld ", *tstamp);
1999 }
2000 return NO_ERROR;
2001}
Ethan Chen8d420ed2013-11-14 00:40:54 -08002002#endif
Alok Kediya601e9b52013-09-27 11:55:10 +05302003
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002004// =========================================================================
2005
2006AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten598de6c2013-10-16 17:02:13 -07002007 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
2008 mIgnoreNextPausedInt(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08002009{
2010}
2011
2012AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002013{
2014}
2015
2016bool AudioTrack::AudioTrackThread::threadLoop()
2017{
Glenn Kasten3acbd052012-02-28 10:39:56 -08002018 {
2019 AutoMutex _l(mMyLock);
2020 if (mPaused) {
2021 mMyCond.wait(mMyLock);
2022 // caller will check for exitPending()
2023 return true;
2024 }
Glenn Kasten598de6c2013-10-16 17:02:13 -07002025 if (mIgnoreNextPausedInt) {
2026 mIgnoreNextPausedInt = false;
2027 mPausedInt = false;
2028 }
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002029 if (mPausedInt) {
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002030 if (mPausedNs > 0) {
2031 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
2032 } else {
2033 mMyCond.wait(mMyLock);
2034 }
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002035 mPausedInt = false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002036 return true;
2037 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08002038 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002039 nsecs_t ns = mReceiver.processAudioBuffer(this);
2040 switch (ns) {
2041 case 0:
2042 return true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002043 case NS_INACTIVE:
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002044 pauseInternal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002045 return true;
2046 case NS_NEVER:
2047 return false;
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002048 case NS_WHENEVER:
2049 // FIXME increase poll interval, or make event-driven
2050 ns = 1000000000LL;
2051 // fall through
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002052 default:
2053 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002054 pauseInternal(ns);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002055 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07002056 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002057}
2058
Glenn Kasten3acbd052012-02-28 10:39:56 -08002059void AudioTrack::AudioTrackThread::requestExit()
2060{
2061 // must be in this order to avoid a race condition
2062 Thread::requestExit();
Glenn Kasten598de6c2013-10-16 17:02:13 -07002063 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08002064}
2065
2066void AudioTrack::AudioTrackThread::pause()
2067{
2068 AutoMutex _l(mMyLock);
2069 mPaused = true;
2070}
2071
2072void AudioTrack::AudioTrackThread::resume()
2073{
2074 AutoMutex _l(mMyLock);
Glenn Kasten598de6c2013-10-16 17:02:13 -07002075 mIgnoreNextPausedInt = true;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002076 if (mPaused || mPausedInt) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08002077 mPaused = false;
Eric Laurent9d2c78c2013-09-23 12:29:42 -07002078 mPausedInt = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08002079 mMyCond.signal();
2080 }
2081}
2082
Glenn Kasten5a6cd222013-09-20 09:20:45 -07002083void AudioTrack::AudioTrackThread::pauseInternal(nsecs_t ns)
2084{
2085 AutoMutex _l(mMyLock);
2086 mPausedInt = true;
2087 mPausedNs = ns;
2088}
2089
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002090}; // namespace android