blob: 0d7d7332f901daed4386c8b2c11bae3bacb392ee [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
Ravishankar Sarawadi20017912013-02-27 18:47:54 +01005** Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
vivek mehta2192bc92012-12-04 11:46:08 -08006** Not a Contribution, Apache license notifications and license are retained
7** for attribution purposes only.
8
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08009** Licensed under the Apache License, Version 2.0 (the "License");
10** you may not use this file except in compliance with the License.
11** You may obtain a copy of the License at
12**
13** http://www.apache.org/licenses/LICENSE-2.0
14**
15** Unless required by applicable law or agreed to in writing, software
16** distributed under the License is distributed on an "AS IS" BASIS,
17** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18** See the License for the specific language governing permissions and
19** limitations under the License.
20*/
21
22
23//#define LOG_NDEBUG 0
24#define LOG_TAG "AudioTrack"
25
26#include <stdint.h>
27#include <sys/types.h>
28#include <limits.h>
29
30#include <sched.h>
31#include <sys/resource.h>
32
33#include <private/media/AudioTrackShared.h>
34
35#include <media/AudioSystem.h>
36#include <media/AudioTrack.h>
37
38#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070039#include <binder/Parcel.h>
40#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070042#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043
Dima Zavinfce7a472011-04-19 22:30:36 -070044#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080045#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070046
Dima Zavin64760242011-05-11 14:15:23 -070047#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070048#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070049
Glenn Kasten511754b2012-01-11 09:52:19 -080050#include <audio_utils/primitives.h>
51
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080053// ---------------------------------------------------------------------------
54
55// static
56status_t AudioTrack::getMinFrameCount(
57 int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080058 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080059 uint32_t sampleRate)
60{
Glenn Kasten04cd0182012-06-25 11:49:27 -070061 if (frameCount == NULL) return BAD_VALUE;
62
63 // default to 0 in case of error
64 *frameCount = 0;
65
Glenn Kastene0fa4672012-04-24 14:35:14 -070066 // FIXME merge with similar code in createTrack_l(), except we're missing
67 // some information here that is available in createTrack_l():
68 // audio_io_handle_t output
69 // audio_format_t format
70 // audio_channel_mask_t channelMask
71 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080072 int afSampleRate;
73 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 int afFrameCount;
77 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80 uint32_t afLatency;
81 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
82 return NO_INIT;
83 }
84
85 // Ensure that buffer depth covers at least audio hardware latency
86 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
87 if (minBufCount < 2) minBufCount = 2;
88
89 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070090 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080091 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
92 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080093 return NO_ERROR;
94}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095
96// ---------------------------------------------------------------------------
97
98AudioTrack::AudioTrack()
Mingming Yin9b3dd172013-01-10 18:42:38 -080099 : mCblk(NULL),
100 mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800101 mIsTimed(false),
102 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700103 mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800104#ifdef QCOM_HARDWARE
105 ,mAudioFlinger(NULL),
106 mObserver(NULL)
107#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108{
109}
110
111AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800112 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800114 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700115 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700117 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 callback_t cbf,
119 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700120 int notificationFrames,
121 int sessionId)
Mingming Yin9b3dd172013-01-10 18:42:38 -0800122 : mCblk(NULL),
123 mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800124 mIsTimed(false),
125 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700126 mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800127#ifdef QCOM_HARDWARE
128 ,mAudioFlinger(NULL),
129 mObserver(NULL)
130#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800131{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700132 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700133 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800134 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800135}
136
Glenn Kasten17a736c2012-02-14 08:52:15 -0800137// DEPRECATED
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800139 int streamType,
140 uint32_t sampleRate,
141 int format,
142 int channelMask,
143 int frameCount,
144 uint32_t flags,
145 callback_t cbf,
146 void* user,
147 int notificationFrames,
148 int sessionId)
Mingming Yin9b3dd172013-01-10 18:42:38 -0800149 : mCblk(NULL),
150 mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800151 mIsTimed(false),
Glenn Kastena6364332012-04-19 09:35:04 -0700152 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800153#ifdef QCOM_HARDWARE
154 ,mAudioFlinger(NULL),
155 mObserver(NULL)
156#endif
Andreas Huberc8139852012-01-18 10:51:55 -0800157{
Glenn Kasten28b76b32012-07-03 17:24:41 -0700158 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format,
159 (audio_channel_mask_t) channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700160 frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800161 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800162}
163
164AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800165 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800167 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700168 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700170 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800171 callback_t cbf,
172 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700173 int notificationFrames,
174 int sessionId)
Mingming Yin9b3dd172013-01-10 18:42:38 -0800175 : mCblk(NULL),
176 mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800177 mIsTimed(false),
178 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700179 mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800180#ifdef QCOM_HARDWARE
181 ,mAudioFlinger(NULL),
182 mObserver(NULL)
183#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700185 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800186 0 /*frameCount*/, flags, cbf, user, notificationFrames,
187 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188}
189
190AudioTrack::~AudioTrack()
191{
Steve Block3856b092011-10-20 11:56:00 +0100192 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193
194 if (mStatus == NO_ERROR) {
195 // Make sure that callback function exits in the case where
196 // it is looping on buffer full condition in obtainBuffer().
197 // Otherwise the callback thread will never exit.
198 stop();
199 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800200 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201 mAudioTrackThread->requestExitAndWait();
202 mAudioTrackThread.clear();
203 }
vivek mehta2192bc92012-12-04 11:46:08 -0800204#ifdef QCOM_HARDWARE
205 if (mAudioTrack != 0) {
206 mAudioTrack.clear();
207 AudioSystem::releaseAudioSessionId(mSessionId);
208 }
209
210 if (mDirectTrack != 0) {
211 mDirectTrack.clear();
212 }
213#else
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214 mAudioTrack.clear();
vivek mehta2192bc92012-12-04 11:46:08 -0800215#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 IPCThreadState::self()->flushCommands();
vivek mehta2192bc92012-12-04 11:46:08 -0800217#ifndef QCOM_HARDWARE
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700218 AudioSystem::releaseAudioSessionId(mSessionId);
vivek mehta2192bc92012-12-04 11:46:08 -0800219#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 }
221}
222
223status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800224 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800226 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700227 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700229 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 callback_t cbf,
231 void* user,
232 int notificationFrames,
233 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700234 bool threadCanCallJava,
235 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236{
237
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800239
Eric Laurent1a9ed112012-03-20 18:36:01 -0700240 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
241
Eric Laurent1703cdf2011-03-07 14:52:59 -0800242 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700243 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000244 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 return INVALID_OPERATION;
246 }
247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700249 if (streamType == AUDIO_STREAM_DEFAULT) {
250 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700252
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700254 int afSampleRate;
255 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
256 return NO_INIT;
257 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 sampleRate = afSampleRate;
259 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700260
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800262 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700263 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700265 if (channelMask == 0) {
266 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267 }
268
269 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700270 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000271 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 return BAD_VALUE;
273 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700274
Glenn Kastene0fa4672012-04-24 14:35:14 -0700275 // AudioFlinger does not currently support 8-bit data in shared memory
276 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
277 ALOGE("8-bit data in shared memory is not supported");
278 return BAD_VALUE;
279 }
280
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700282 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700283 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800284 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700285 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700286 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700287 // only allow deep buffering for music stream type
288 if (streamType != AUDIO_STREAM_MUSIC) {
289 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
290 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700291
vivek mehta2192bc92012-12-04 11:46:08 -0800292#ifdef QCOM_ENHANCED_AUDIO
293 if ((streamType == AUDIO_STREAM_VOICE_CALL)
294 && (channelMask == AUDIO_CHANNEL_OUT_MONO)
295 && ((sampleRate == 8000 || sampleRate == 16000)))
296 {
297 ALOGD("Turn on Direct Output for VOIP RX");
298 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_VOIP_RX|AUDIO_OUTPUT_FLAG_DIRECT);
299 }
300#endif
301
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700302 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700303 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700304 return BAD_VALUE;
305 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700306 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307
vivek mehta2192bc92012-12-04 11:46:08 -0800308 ALOGV("AudioTrack getOutput streamType %d, sampleRate %d, format %d, channelMask %d, flags %x",
309 streamType, sampleRate, format, channelMask, flags);
Dima Zavinfce7a472011-04-19 22:30:36 -0700310 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800311 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800312 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800313 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700314
315 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000316 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 return BAD_VALUE;
318 }
319
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800320 mVolume[LEFT] = 1.0f;
321 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800322 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700323 mFrameCount = frameCount;
324 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700325 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700326 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700327 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700328 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700329
vivek mehta2192bc92012-12-04 11:46:08 -0800330#ifdef QCOM_HARDWARE
331 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL) {
332 ALOGV("Creating Direct Track");
333 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
334 if (audioFlinger == 0) {
335 ALOGE("Could not get audioflinger");
336 return NO_INIT;
Glenn Kastena997e7a2012-08-07 09:44:19 -0700337 }
vivek mehta2192bc92012-12-04 11:46:08 -0800338 mAudioFlinger = audioFlinger;
339 status_t status = NO_ERROR;
340 mAudioDirectOutput = output;
341 mDirectTrack = audioFlinger->createDirectTrack( getpid(),
342 sampleRate,
343 channelMask,
344 mAudioDirectOutput,
345 &mSessionId,
346 this,
347 streamType,
348 &status);
349 if(status != NO_ERROR) {
350 ALOGE("createDirectTrack returned with status %d", status);
351 return status;
352 }
353 mAudioTrack = NULL;
354 mSharedBuffer = NULL;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700355 }
vivek mehta2192bc92012-12-04 11:46:08 -0800356 else {
357#endif
358 if (cbf != NULL) {
359 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
360 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
361 }
362 // create the IAudioTrack
363 status_t status = createTrack_l(streamType,
364 sampleRate,
365 format,
366 channelMask,
367 frameCount,
368 flags,
369 sharedBuffer,
370 output);
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700371
vivek mehta2192bc92012-12-04 11:46:08 -0800372 if (status != NO_ERROR) {
373 if (mAudioTrackThread != 0) {
374 mAudioTrackThread->requestExit();
375 mAudioTrackThread.clear();
376 }
377 return status;
378 }
379#ifdef QCOM_HARDWARE
380 AudioSystem::acquireAudioSessionId(mSessionId);
381 mAudioDirectOutput = -1;
382 mDirectTrack = NULL;
383 mSharedBuffer = sharedBuffer;
384 }
385 mUserData = user;
386#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 mStatus = NO_ERROR;
388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800390 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700391 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 mChannelCount = channelCount;
vivek mehta2192bc92012-12-04 11:46:08 -0800393
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800395 mActive = false;
vivek mehta2192bc92012-12-04 11:46:08 -0800396
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800397 mLoopCount = 0;
398 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700399 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400 mNewPosition = 0;
401 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700402 mFlushed = false;
vivek mehta2192bc92012-12-04 11:46:08 -0800403
404#ifndef QCOM_HARDWARE
405 mSharedBuffer = sharedBuffer;
406 mUserData = user;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700407 AudioSystem::acquireAudioSessionId(mSessionId);
vivek mehta2192bc92012-12-04 11:46:08 -0800408#endif
409
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700410 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 return NO_ERROR;
412}
413
414status_t AudioTrack::initCheck() const
415{
416 return mStatus;
417}
418
419// -------------------------------------------------------------------------
420
421uint32_t AudioTrack::latency() const
422{
vivek mehta2192bc92012-12-04 11:46:08 -0800423#ifdef QCOM_HARDWARE
424 if(mAudioDirectOutput != -1) {
425 return mAudioFlinger->latency(mAudioDirectOutput);
426 }
427#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 return mLatency;
429}
430
Glenn Kastenfff6d712012-01-12 16:38:12 -0800431audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432{
433 return mStreamType;
434}
435
Glenn Kastene1c39622012-01-04 09:36:37 -0800436audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437{
438 return mFormat;
439}
440
441int AudioTrack::channelCount() const
442{
443 return mChannelCount;
444}
445
446uint32_t AudioTrack::frameCount() const
447{
vivek mehta2192bc92012-12-04 11:46:08 -0800448#ifdef QCOM_HARDWARE
449 if(mAudioDirectOutput != -1) {
450 return mAudioFlinger->frameCount(mAudioDirectOutput);
451 }
452#endif
Eric Laurentd1b449a2010-05-14 03:26:45 -0700453 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454}
455
Glenn Kastenb9980652012-01-11 09:48:27 -0800456size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457{
Dima Zavinfce7a472011-04-19 22:30:36 -0700458 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700459 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700460 } else {
461 return sizeof(uint8_t);
462 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463}
464
465sp<IMemory>& AudioTrack::sharedBuffer()
466{
467 return mSharedBuffer;
468}
469
470// -------------------------------------------------------------------------
471
472void AudioTrack::start()
473{
vivek mehta2192bc92012-12-04 11:46:08 -0800474#ifdef QCOM_HARDWARE
475 if (mDirectTrack != NULL) {
476 if(mActive == 0) {
477 mActive = 1;
478 mDirectTrack->start();
479 }
480 return;
481 }
482#endif
483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 sp<AudioTrackThread> t = mAudioTrackThread;
485
Steve Block3856b092011-10-20 11:56:00 +0100486 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487
Eric Laurentf5aafb22010-11-18 08:40:16 -0800488 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800489 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
490 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700491 sp<IAudioTrack> audioTrack = mAudioTrack;
492 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800493 audio_track_cblk_t* cblk = mCblk;
494
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800495 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700496 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800497 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800498 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700499 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800500 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
501 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700502 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800503 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800504 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800505 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700506 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700507 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700508 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800509 }
510
Steve Block3856b092011-10-20 11:56:00 +0100511 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700512 status_t status = NO_ERROR;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800513 if (!(cblk->flags & CBLK_INVALID_MSK)) {
514 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800515 ALOGV("mAudioTrack->start()");
516 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800517 cblk->lock.lock();
518 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700519 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800520 }
Eric Laurent2b584242009-11-09 23:32:22 -0800521 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800522 if (cblk->flags & CBLK_INVALID_MSK) {
523 status = restoreTrack_l(cblk, true);
524 }
525 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800526 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800528 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800529 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800530 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800531 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700532 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700533 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800534 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800535 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800536 }
537
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538}
539
540void AudioTrack::stop()
541{
542 sp<AudioTrackThread> t = mAudioTrackThread;
543
Steve Block3856b092011-10-20 11:56:00 +0100544 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545
Eric Laurentf5aafb22010-11-18 08:40:16 -0800546 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800547 if (mActive) {
vivek mehta2192bc92012-12-04 11:46:08 -0800548#ifdef QCOM_HARDWARE
549 if(mDirectTrack != NULL) {
550 mActive = false;
551 mDirectTrack->stop();
552 } else if (mAudioTrack != NULL) {
553#endif
554 mActive = false;
555 mCblk->cv.signal();
556 mAudioTrack->stop();
557 // Cancel loops (If we are in the middle of a loop, playback
558 // would not stop until loopCount reaches 0).
559 setLoop_l(0, 0, 0);
560 // the playback head position will reset to 0, so if a marker is set, we need
561 // to activate it again
562 mMarkerReached = false;
563 // Force flush if a shared buffer is used otherwise audioflinger
564 // will not stop before end of buffer is reached.
565 if (mSharedBuffer != 0) {
566 flush_l();
567 }
568 if (t != 0) {
569 t->pause();
570 } else {
571 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
572 set_sched_policy(0, mPreviousSchedulingGroup);
573 }
574#ifdef QCOM_HARDWARE
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575 }
vivek mehta2192bc92012-12-04 11:46:08 -0800576#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 }
578
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800579}
580
581bool AudioTrack::stopped() const
582{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800583 AutoMutex lock(mLock);
584 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800585}
586
587void AudioTrack::flush()
588{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800589 AutoMutex lock(mLock);
vivek mehta2192bc92012-12-04 11:46:08 -0800590#ifdef QCOM_HARDWARE
591 if(mDirectTrack != NULL) {
592 mDirectTrack->flush();
593 } else
594#endif
595 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800596}
597
598// must be called with mLock held
599void AudioTrack::flush_l()
600{
Steve Block3856b092011-10-20 11:56:00 +0100601 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700602
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700603 // clear playback marker and periodic update counter
604 mMarkerPosition = 0;
605 mMarkerReached = false;
606 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700607
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700609 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610 mAudioTrack->flush();
611 // Release AudioTrack callback thread in case it was waiting for new buffers
612 // in AudioTrack::obtainBuffer()
613 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614 }
615}
616
617void AudioTrack::pause()
618{
Steve Block3856b092011-10-20 11:56:00 +0100619 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800620 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800621 if (mActive) {
622 mActive = false;
vivek mehta2192bc92012-12-04 11:46:08 -0800623#ifdef QCOM_HARDWARE
624 if(mDirectTrack != NULL) {
625 ALOGV("mDirectTrack pause");
626 mDirectTrack->pause();
627 } else {
628#endif
629 mCblk->cv.signal();
630 mAudioTrack->pause();
631#ifdef QCOM_HARDWARE
632 }
633#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 }
635}
636
637void AudioTrack::mute(bool e)
638{
vivek mehta2192bc92012-12-04 11:46:08 -0800639#ifdef QCOM_HARDWARE
640 if(mDirectTrack != NULL) {
641 mDirectTrack->mute(e);
642 } else
643#endif
644 mAudioTrack->mute(e);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 mMuted = e;
646}
647
648bool AudioTrack::muted() const
649{
650 return mMuted;
651}
652
Eric Laurentbe916aa2010-06-01 23:49:17 -0700653status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800655 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700656 return BAD_VALUE;
657 }
658
Eric Laurent1703cdf2011-03-07 14:52:59 -0800659 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660 mVolume[LEFT] = left;
661 mVolume[RIGHT] = right;
vivek mehta2192bc92012-12-04 11:46:08 -0800662#ifdef QCOM_HARDWARE
663 if(mDirectTrack != NULL) {
664 ALOGV("mDirectTrack->setVolume(left = %f , right = %f)", left,right);
665 mDirectTrack->setVolume(left, right);
666 } else
667#endif
668 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700669
670 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671}
672
Glenn Kastena5224f32012-01-04 12:41:44 -0800673void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700675 if (left != NULL) {
676 *left = mVolume[LEFT];
677 }
678 if (right != NULL) {
679 *right = mVolume[RIGHT];
680 }
681}
682
Eric Laurent2beeb502010-07-16 07:43:46 -0700683status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700684{
vivek mehta2192bc92012-12-04 11:46:08 -0800685#ifdef QCOM_HARDWARE
686 if (mDirectTrack != NULL) {
687 return NO_ERROR;
688 }
689#endif
Steve Block3856b092011-10-20 11:56:00 +0100690 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800691 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700692 return BAD_VALUE;
693 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800694 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700695
696 mSendLevel = level;
697
Glenn Kasten05632a52012-01-03 14:22:33 -0800698 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700699
700 return NO_ERROR;
701}
702
Glenn Kastena5224f32012-01-04 12:41:44 -0800703void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700704{
705 if (level != NULL) {
706 *level = mSendLevel;
707 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708}
709
Eric Laurent57326622009-07-07 07:10:45 -0700710status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711{
712 int afSamplingRate;
713
John Grossman4ff14ba2012-02-08 16:37:41 -0800714 if (mIsTimed) {
715 return INVALID_OPERATION;
716 }
717
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700719 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 }
721 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700722 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800723
Eric Laurent1703cdf2011-03-07 14:52:59 -0800724 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700725 mCblk->sampleRate = rate;
726 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800727}
728
Glenn Kastena5224f32012-01-04 12:41:44 -0800729uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730{
John Grossman4ff14ba2012-02-08 16:37:41 -0800731 if (mIsTimed) {
732 return INVALID_OPERATION;
733 }
734
Eric Laurent1703cdf2011-03-07 14:52:59 -0800735 AutoMutex lock(mLock);
vivek mehta2192bc92012-12-04 11:46:08 -0800736#ifdef QCOM_HARDWARE
737 if(mAudioDirectOutput != -1) {
738 return mAudioFlinger->sampleRate(mAudioDirectOutput);
739 }
740#endif
Eric Laurent57326622009-07-07 07:10:45 -0700741 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742}
743
744status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
745{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800746 AutoMutex lock(mLock);
747 return setLoop_l(loopStart, loopEnd, loopCount);
748}
749
750// must be called with mLock held
751status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
752{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800753 audio_track_cblk_t* cblk = mCblk;
754
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800755 Mutex::Autolock _l(cblk->lock);
756
757 if (loopCount == 0) {
758 cblk->loopStart = UINT_MAX;
759 cblk->loopEnd = UINT_MAX;
760 cblk->loopCount = 0;
761 mLoopCount = 0;
762 return NO_ERROR;
763 }
764
John Grossman4ff14ba2012-02-08 16:37:41 -0800765 if (mIsTimed) {
766 return INVALID_OPERATION;
767 }
768
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700770 loopEnd - loopStart > cblk->frameCount ||
771 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000772 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800773 return BAD_VALUE;
774 }
775
Eric Laurent9b7d9502011-03-21 11:49:00 -0700776 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000777 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700778 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700780 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800781
782 cblk->loopStart = loopStart;
783 cblk->loopEnd = loopEnd;
784 cblk->loopCount = loopCount;
785 mLoopCount = loopCount;
786
787 return NO_ERROR;
788}
789
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800790status_t AudioTrack::setMarkerPosition(uint32_t marker)
791{
Glenn Kastena0d68332012-01-27 16:47:15 -0800792 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800793
794 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700795 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800796
797 return NO_ERROR;
798}
799
Glenn Kastena5224f32012-01-04 12:41:44 -0800800status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800801{
Glenn Kastena0d68332012-01-27 16:47:15 -0800802 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800803
804 *marker = mMarkerPosition;
805
806 return NO_ERROR;
807}
808
809status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
810{
Glenn Kastena0d68332012-01-27 16:47:15 -0800811 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812
813 uint32_t curPosition;
814 getPosition(&curPosition);
815 mNewPosition = curPosition + updatePeriod;
816 mUpdatePeriod = updatePeriod;
817
818 return NO_ERROR;
819}
820
Glenn Kastena5224f32012-01-04 12:41:44 -0800821status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800822{
Glenn Kastena0d68332012-01-27 16:47:15 -0800823 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800824
825 *updatePeriod = mUpdatePeriod;
826
827 return NO_ERROR;
828}
829
830status_t AudioTrack::setPosition(uint32_t position)
831{
John Grossman4ff14ba2012-02-08 16:37:41 -0800832 if (mIsTimed) return INVALID_OPERATION;
833
Eric Laurent1703cdf2011-03-07 14:52:59 -0800834 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800835
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800836 if (!stopped_l()) return INVALID_OPERATION;
837
838 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800839
840 if (position > mCblk->user) return BAD_VALUE;
841
842 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700843 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700844
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800845 return NO_ERROR;
846}
847
848status_t AudioTrack::getPosition(uint32_t *position)
849{
Glenn Kastena0d68332012-01-27 16:47:15 -0800850 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800851 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700852 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853
854 return NO_ERROR;
855}
856
857status_t AudioTrack::reload()
858{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800859 AutoMutex lock(mLock);
860
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800861 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700862
Eric Laurent1703cdf2011-03-07 14:52:59 -0800863 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800864
Eric Laurentd1b449a2010-05-14 03:26:45 -0700865 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800866
867 return NO_ERROR;
868}
869
Eric Laurentc2f1f072009-07-17 12:17:14 -0700870audio_io_handle_t AudioTrack::getOutput()
871{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800872 AutoMutex lock(mLock);
873 return getOutput_l();
874}
875
876// must be called with mLock held
877audio_io_handle_t AudioTrack::getOutput_l()
878{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800879 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800880 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700881}
882
Glenn Kastena5224f32012-01-04 12:41:44 -0800883int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700884{
885 return mSessionId;
886}
887
Ricardo Cerqueira54d90af2012-09-02 04:33:40 +0100888extern "C" int _ZNK7android10AudioTrack12getSessionIdEv();
889extern "C" int _ZN7android10AudioTrack12getSessionIdEv() {
890 return _ZNK7android10AudioTrack12getSessionIdEv();
891}
892
Eric Laurentbe916aa2010-06-01 23:49:17 -0700893status_t AudioTrack::attachAuxEffect(int effectId)
894{
Steve Block3856b092011-10-20 11:56:00 +0100895 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700896 status_t status = mAudioTrack->attachAuxEffect(effectId);
897 if (status == NO_ERROR) {
898 mAuxEffectId = effectId;
899 }
900 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700901}
902
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903// -------------------------------------------------------------------------
904
Eric Laurent1703cdf2011-03-07 14:52:59 -0800905// must be called with mLock held
906status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800907 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800908 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800909 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700910 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800911 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700912 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800913 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700914 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800915{
916 status_t status;
917 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
918 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700919 ALOGE("Could not get audioflinger");
920 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800921 }
922
Eric Laurentd1b449a2010-05-14 03:26:45 -0700923 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700924 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700925 return NO_INIT;
926 }
927
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700928 // Client decides whether the track is TIMED (see below), but can only express a preference
929 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700930 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700931 // either of these use cases:
932 // use case 1: shared buffer
933 (sharedBuffer != 0) ||
934 // use case 2: callback handler
935 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800936 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700937 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700938 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700939 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700940 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700941 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700942
Eric Laurentd1b449a2010-05-14 03:26:45 -0700943 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700944
Dima Zavinfce7a472011-04-19 22:30:36 -0700945 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700946
Eric Laurentd1b449a2010-05-14 03:26:45 -0700947 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700948 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700949 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700950 } else if (frameCount == 0) {
951 int afFrameCount;
952 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
953 return NO_INIT;
954 }
955 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700956 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700957
958 } else if (sharedBuffer != 0) {
959
960 // Ensure that buffer alignment matches channelCount
961 int channelCount = popcount(channelMask);
962 // 8-bit data in shared memory is not currently supported by AudioFlinger
963 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
964 if (channelCount > 1) {
965 // More than 2 channels does not require stronger alignment than stereo
966 alignment <<= 1;
967 }
968 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
969 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
970 sharedBuffer->pointer(), channelCount);
971 return BAD_VALUE;
972 }
973
974 // When initializing a shared buffer AudioTrack via constructors,
975 // there's no frameCount parameter.
976 // But when initializing a shared buffer AudioTrack via set(),
977 // there _is_ a frameCount parameter. We silently ignore it.
978 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
979
980 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
981
982 // FIXME move these calculations and associated checks to server
983 int afSampleRate;
984 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
985 return NO_INIT;
986 }
987 int afFrameCount;
988 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
989 return NO_INIT;
990 }
991
Eric Laurentd1b449a2010-05-14 03:26:45 -0700992 // Ensure that buffer depth covers at least audio hardware latency
993 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
994 if (minBufCount < 2) minBufCount = 2;
995
996 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800997 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
998 ", afLatency=%d",
999 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -07001000
1001 if (frameCount == 0) {
1002 frameCount = minFrameCount;
1003 }
1004 if (mNotificationFramesAct == 0) {
1005 mNotificationFramesAct = frameCount/2;
1006 }
1007 // Make sure that application is notified with sufficient margin
1008 // before underrun
1009 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
1010 mNotificationFramesAct = frameCount/2;
1011 }
1012 if (frameCount < minFrameCount) {
1013 // not ALOGW because it happens all the time when playing key clicks over A2DP
1014 ALOGV("Minimum buffer size corrected from %d to %d",
1015 frameCount, minFrameCount);
1016 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001017 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001018
Glenn Kastene0fa4672012-04-24 14:35:14 -07001019 } else {
1020 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -07001021 }
1022
Glenn Kastena075db42012-03-06 11:22:44 -08001023 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
1024 if (mIsTimed) {
1025 trackFlags |= IAudioFlinger::TRACK_TIMED;
1026 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001027
1028 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001029 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001030 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001031 if (mAudioTrackThread != 0) {
1032 tid = mAudioTrackThread->getTid();
1033 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001034 }
1035
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001036 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
1037 streamType,
1038 sampleRate,
1039 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001040 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001041 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001042 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001043 sharedBuffer,
1044 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001045 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001046 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001047 &status);
1048
1049 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001050 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001051 return status;
1052 }
1053 sp<IMemory> cblk = track->getCblk();
1054 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001055 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001056 return NO_INIT;
1057 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001058 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001059 mCblkMemory = cblk;
1060 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -08001061 // old has the previous value of mCblk->flags before the "or" operation
1062 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
1063 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
1064 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -07001065 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -08001066 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -07001067 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001068 // once denied, do not request again if IAudioTrack is re-created
1069 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1070 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001071 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001072 if (sharedBuffer == 0) {
1073 mNotificationFramesAct = mCblk->frameCount/2;
1074 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001075 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001076 if (sharedBuffer == 0) {
1077 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
1078 } else {
1079 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001080 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -07001081 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001082 }
1083
Glenn Kasten83d86532012-01-17 14:39:34 -08001084 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -08001085 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -07001086 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -08001087 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
1088 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001089 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -07001090 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -07001091 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -07001092 // If IAudioTrack is re-created, don't let the requested frameCount
1093 // decrease. This can confuse clients that cache frameCount().
1094 if (mCblk->frameCount > mFrameCount) {
1095 mFrameCount = mCblk->frameCount;
1096 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001097 return NO_ERROR;
1098}
1099
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001100status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1101{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001102 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001103 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -07001104 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001105 audio_track_cblk_t* cblk = mCblk;
1106 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -07001107 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001108
1109 audioBuffer->frameCount = 0;
1110 audioBuffer->size = 0;
1111
1112 uint32_t framesAvail = cblk->framesAvailable();
1113
Eric Laurent9b7d9502011-03-21 11:49:00 -07001114 cblk->lock.lock();
1115 if (cblk->flags & CBLK_INVALID_MSK) {
1116 goto create_new_track;
1117 }
1118 cblk->lock.unlock();
1119
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001120 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001121 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122 goto start_loop_here;
1123 while (framesAvail == 0) {
1124 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -08001125 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001127 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128 return NO_MORE_BUFFERS;
1129 }
Glenn Kastenf6b16782011-12-15 09:51:17 -08001130 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001131 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001132 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001133 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001134 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001135 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -07001136 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -07001137 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001138 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001139 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001140 return status_t(STOPPED);
1141 }
1142 cblk->lock.lock();
1143 }
1144
1145 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001146 goto create_new_track;
1147 }
Glenn Kastenf6b16782011-12-15 09:51:17 -08001148 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -07001149 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001150 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
1151 // timing out when a loop has been set and we have already written upto loop end
1152 // is a normal condition: no need to wake AudioFlinger up.
1153 if (cblk->user < cblk->loopEnd) {
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001154 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p name=%#x"
1155 "user=%08x, server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001156 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001157 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001158 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001159 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001160 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001161 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001162create_new_track:
1163 result = restoreTrack_l(cblk, false);
1164 }
1165 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001166 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001167 cblk->lock.unlock();
1168 return result;
1169 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001170 }
1171 cblk->waitTimeMs = 0;
1172 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001173
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001174 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001175 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001176 return TIMED_OUT;
1177 }
1178 }
1179 // read the server count again
1180 start_loop_here:
1181 framesAvail = cblk->framesAvailable_l();
1182 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001183 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001184 }
1185
1186 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001187
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188 if (framesReq > framesAvail) {
1189 framesReq = framesAvail;
1190 }
1191
1192 uint32_t u = cblk->user;
1193 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1194
Marco Nelissena1472d92012-03-30 14:36:54 -07001195 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001196 framesReq = bufferEnd - u;
1197 }
1198
Eric Laurentc2f1f072009-07-17 12:17:14 -07001199 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1200 audioBuffer->channelCount = mChannelCount;
1201 audioBuffer->frameCount = framesReq;
1202 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001203 if (audio_is_linear_pcm(mFormat)) {
1204 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001205 } else {
1206 audioBuffer->format = mFormat;
1207 }
1208 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209 active = mActive;
1210 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1211}
1212
1213void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1214{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001215 AutoMutex lock(mLock);
1216 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001217 if (audioBuffer->frameCount > 0) {
1218 // restart track if it was disabled by audioflinger due to previous underrun
1219 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1220 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001221 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001222 mAudioTrack->start();
1223 }
1224 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225}
1226
1227// -------------------------------------------------------------------------
1228
1229ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1230{
vivek mehta2192bc92012-12-04 11:46:08 -08001231#ifdef QCOM_HARDWARE
1232 if (mDirectTrack != NULL) {
1233 mDirectTrack->write(buffer,userSize);
1234 return userSize;
1235 }
1236#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001237 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001238 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001239
1240 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001241 // Sanity-check: user is most-likely passing an error code, and it would
1242 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001243 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001244 buffer, userSize, userSize);
1245 return BAD_VALUE;
1246 }
1247
Steve Block3856b092011-10-20 11:56:00 +01001248 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249
Eric Laurentdf839842012-05-31 14:27:14 -07001250 if (userSize == 0) {
1251 return 0;
1252 }
1253
Eric Laurent1703cdf2011-03-07 14:52:59 -08001254 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1255 // while we are accessing the cblk
1256 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001257 sp<IAudioTrack> audioTrack = mAudioTrack;
1258 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001259 mLock.unlock();
1260
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261 ssize_t written = 0;
1262 const int8_t *src = (const int8_t *)buffer;
1263 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001264 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265
1266 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001267 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001268
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269 status_t err = obtainBuffer(&audioBuffer, -1);
1270 if (err < 0) {
1271 // out of buffers, return #bytes written
1272 if (err == status_t(NO_MORE_BUFFERS))
1273 break;
1274 return ssize_t(err);
1275 }
1276
1277 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001278
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001279 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 // Divide capacity by 2 to take expansion into account
1281 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001282 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001283 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 toWrite = audioBuffer.size;
1285 memcpy(audioBuffer.i8, src, toWrite);
1286 src += toWrite;
1287 }
1288 userSize -= toWrite;
1289 written += toWrite;
1290
1291 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001292 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293
1294 return written;
1295}
1296
1297// -------------------------------------------------------------------------
1298
John Grossman4ff14ba2012-02-08 16:37:41 -08001299TimedAudioTrack::TimedAudioTrack() {
1300 mIsTimed = true;
1301}
1302
1303status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1304{
1305 status_t result = UNKNOWN_ERROR;
1306
1307 // If the track is not invalid already, try to allocate a buffer. alloc
1308 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001309 // we can attempt to restore in just a bit.
John Grossman4ff14ba2012-02-08 16:37:41 -08001310 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1311 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1312 if (result == DEAD_OBJECT) {
1313 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1314 }
1315 }
1316
1317 // If the track is invalid at this point, attempt to restore it. and try the
1318 // allocation one more time.
1319 if (mCblk->flags & CBLK_INVALID_MSK) {
1320 mCblk->lock.lock();
1321 result = restoreTrack_l(mCblk, false);
1322 mCblk->lock.unlock();
1323
1324 if (result == OK)
1325 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1326 }
1327
1328 return result;
1329}
1330
1331status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1332 int64_t pts)
1333{
Eric Laurentdf839842012-05-31 14:27:14 -07001334 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1335 {
1336 AutoMutex lock(mLock);
1337 // restart track if it was disabled by audioflinger due to previous underrun
1338 if (buffer->size() != 0 && status == NO_ERROR &&
1339 mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1340 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1341 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1342 mAudioTrack->start();
1343 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001344 }
Eric Laurentdf839842012-05-31 14:27:14 -07001345 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001346}
1347
1348status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1349 TargetTimeline target)
1350{
1351 return mAudioTrack->setMediaTimeTransform(xform, target);
1352}
1353
1354// -------------------------------------------------------------------------
1355
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001356bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1357{
1358 Buffer audioBuffer;
1359 uint32_t frames;
1360 size_t writtenSize;
1361
Eric Laurent1703cdf2011-03-07 14:52:59 -08001362 mLock.lock();
1363 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1364 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001365 sp<IAudioTrack> audioTrack = mAudioTrack;
1366 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001367 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001368 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001369 mLock.unlock();
1370
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001371 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001372 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001373 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001374 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001375 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001376 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001377 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001378 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001379 if (mSharedBuffer != 0) return false;
1380 }
1381 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001382
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001383 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001384 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001385 int loopCount = -1;
1386 mLoopCount--;
1387 if (mLoopCount >= 0) loopCount = mLoopCount;
1388
1389 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1390 }
1391
1392 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001393 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001394 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001396 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001397 }
1398 }
1399
1400 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001401 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001402 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001403 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1404 mNewPosition += mUpdatePeriod;
1405 }
1406 }
1407
1408 // If Shared buffer is used, no data is requested from client.
1409 if (mSharedBuffer != 0) {
1410 frames = 0;
1411 } else {
1412 frames = mRemainingFrames;
1413 }
1414
Glenn Kasten99e53b82012-01-19 08:59:58 -08001415 // See description of waitCount parameter at declaration of obtainBuffer().
1416 // The logic below prevents us from being stuck below at obtainBuffer()
1417 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001418 int32_t waitCount = -1;
1419 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1420 waitCount = 1;
1421 }
1422
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001423 do {
1424
1425 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001426
Eric Laurent2267ba12011-09-07 11:13:23 -07001427 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001428 if (err < NO_ERROR) {
1429 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001430 ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001431 return false;
1432 }
1433 break;
1434 }
1435 if (err == status_t(STOPPED)) return false;
1436
1437 // Divide buffer size by 2 to take into account the expansion
1438 // due to 8 to 16 bit conversion: the callback must fill only half
1439 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001440 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 audioBuffer.size >>= 1;
1442 }
1443
1444 size_t reqSize = audioBuffer.size;
1445 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1446 writtenSize = audioBuffer.size;
1447
1448 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001449 if (ssize_t(writtenSize) <= 0) {
1450 // The callback is done filling buffers
1451 // Keep this thread going to handle timed events and
1452 // still try to get more data in intervals of WAIT_PERIOD_MS
1453 // but don't just loop and block the CPU, so wait
1454 usleep(WAIT_PERIOD_MS*1000);
1455 break;
1456 }
Eric Laurentdf839842012-05-31 14:27:14 -07001457
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458 if (writtenSize > reqSize) writtenSize = reqSize;
1459
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001460 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001461 // 8 to 16 bit conversion, note that source and destination are the same address
1462 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001463 writtenSize <<= 1;
1464 }
1465
1466 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001467 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001468 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001469 // 16 bit.
1470 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1471
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001472 frames -= audioBuffer.frameCount;
1473
1474 releaseBuffer(&audioBuffer);
1475 }
1476 while (frames);
1477
1478 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001479 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480 } else {
1481 mRemainingFrames = frames;
1482 }
1483 return true;
1484}
1485
Eric Laurent1703cdf2011-03-07 14:52:59 -08001486// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1487// the IAudioTrack and IMemory in case they are recreated here.
1488// If the IAudioTrack is successfully restored, the cblk pointer is updated
1489status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1490{
1491 status_t result;
1492
Eric Laurent38ccae22011-03-28 18:37:07 -07001493 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001494 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001495 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001496
Eric Laurent1703cdf2011-03-07 14:52:59 -08001497 // signal old cblk condition so that other threads waiting for available buffers stop
1498 // waiting now
1499 cblk->cv.broadcast();
1500 cblk->lock.unlock();
1501
Eric Laurent9f6530f2011-08-30 10:18:54 -07001502 // refresh the audio configuration cache in this process to make sure we get new
1503 // output parameters in getOutput_l() and createTrack_l()
1504 AudioSystem::clearAudioConfigCache();
1505
Eric Laurent1703cdf2011-03-07 14:52:59 -08001506 // if the new IAudioTrack is created, createTrack_l() will modify the
1507 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1508 // It will also delete the strong references on previous IAudioTrack and IMemory
1509 result = createTrack_l(mStreamType,
1510 cblk->sampleRate,
1511 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001512 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001513 mFrameCount,
1514 mFlags,
1515 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001516 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001517
1518 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001519 uint32_t user = cblk->user;
1520 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001521 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001522 mCblk->user = user;
1523 mCblk->server = user;
1524 mCblk->userBase = user;
1525 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001526 // restore loop: this is not guaranteed to succeed if new frame count is not
1527 // compatible with loop length
1528 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001529 if (!fromStart) {
1530 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001531 // Make sure that a client relying on callback events indicating underrun or
1532 // the actual amount of audio frames played (e.g SoundPool) receives them.
1533 if (mSharedBuffer == 0) {
1534 uint32_t frames = 0;
1535 if (user > server) {
1536 frames = ((user - server) > mCblk->frameCount) ?
1537 mCblk->frameCount : (user - server);
1538 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1539 }
1540 // restart playback even if buffer is not completely filled.
1541 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1542 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1543 // the client
1544 mCblk->stepUser(frames);
1545 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001546 }
Eric Laurent29864602012-05-08 18:57:51 -07001547 if (mSharedBuffer != 0) {
1548 mCblk->stepUser(mCblk->frameCount);
1549 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001550 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001551 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001552 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001553 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001554 if (fromStart && result == NO_ERROR) {
1555 mNewPosition = mCblk->server + mUpdatePeriod;
1556 }
1557 }
1558 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001559 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001560 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001561 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001562 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001563 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001564 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001565 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001566 } else {
1567 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001568 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001569 mLock.unlock();
1570 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001571 if (result == NO_ERROR) {
1572 result = mRestoreStatus;
1573 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001574 cblk->lock.unlock();
1575 mLock.lock();
1576 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001577 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001578 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001579 cblk->lock.unlock();
1580 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001581 }
Steve Block3856b092011-10-20 11:56:00 +01001582 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001583 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001584
1585 if (result == NO_ERROR) {
1586 // from now on we switch to the newly created cblk
1587 cblk = mCblk;
1588 }
1589 cblk->lock.lock();
1590
Steve Block5ff1dd52012-01-05 23:22:43 +00001591 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001592
1593 return result;
1594}
1595
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1597{
1598
1599 const size_t SIZE = 256;
1600 char buffer[SIZE];
1601 String8 result;
1602
1603 result.append(" AudioTrack::dump\n");
1604 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1605 result.append(buffer);
Mingming Yin9b3dd172013-01-10 18:42:38 -08001606 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount,
1607 (mCblk == 0) ? 0 : mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001609 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001610 result.append(buffer);
1611 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1612 result.append(buffer);
1613 ::write(fd, result.string(), result.size());
1614 return NO_ERROR;
1615}
1616
vivek mehta2192bc92012-12-04 11:46:08 -08001617#ifdef QCOM_HARDWARE
1618void AudioTrack::notify(int msg) {
1619 if (msg == EVENT_UNDERRUN) {
1620 ALOGV("Posting event underrun to Audio Sink.");
1621 mCbf(EVENT_UNDERRUN, mUserData, 0);
1622 }
Ravishankar Sarawadi20017912013-02-27 18:47:54 +01001623 if (msg == EVENT_HW_FAIL) {
1624 ALOGV("Posting event HW fail to Audio Sink.");
1625 mCbf(EVENT_HW_FAIL, mUserData, 0);
1626 }
vivek mehta2192bc92012-12-04 11:46:08 -08001627}
1628
1629status_t AudioTrack::getTimeStamp(uint64_t *tstamp) {
1630 if (mDirectTrack != NULL) {
1631 *tstamp = mDirectTrack->getTimeStamp();
1632 ALOGV("Timestamp %lld ", *tstamp);
1633 }
1634 return NO_ERROR;
1635}
1636#endif
1637
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001638// =========================================================================
1639
1640AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001641 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1642{
1643}
1644
1645AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001646{
1647}
1648
1649bool AudioTrack::AudioTrackThread::threadLoop()
1650{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001651 {
1652 AutoMutex _l(mMyLock);
1653 if (mPaused) {
1654 mMyCond.wait(mMyLock);
1655 // caller will check for exitPending()
1656 return true;
1657 }
1658 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001659 if (!mReceiver.processAudioBuffer(this)) {
1660 pause();
1661 }
1662 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001663}
1664
Glenn Kasten3acbd052012-02-28 10:39:56 -08001665void AudioTrack::AudioTrackThread::requestExit()
1666{
1667 // must be in this order to avoid a race condition
1668 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001669 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001670}
1671
1672void AudioTrack::AudioTrackThread::pause()
1673{
1674 AutoMutex _l(mMyLock);
1675 mPaused = true;
1676}
1677
1678void AudioTrack::AudioTrackThread::resume()
1679{
1680 AutoMutex _l(mMyLock);
1681 if (mPaused) {
1682 mPaused = false;
1683 mMyCond.signal();
1684 }
1685}
1686
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001687// =========================================================================
1688
Eric Laurent38ccae22011-03-28 18:37:07 -07001689
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001690audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001691 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001692 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001693 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001694 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001695{
1696}
1697
1698uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1699{
Marco Nelissena1472d92012-03-30 14:36:54 -07001700 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001701
Marco Nelissena1472d92012-03-30 14:36:54 -07001702 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001703 u += frameCount;
1704 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001705 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001706 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1707 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1708 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1709 }
Glenn Kasten90548972011-12-13 11:45:07 -08001710 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001711 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001712 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001713 }
1714
Marco Nelissena1472d92012-03-30 14:36:54 -07001715 uint32_t fc = this->frameCount;
1716 if (u >= fc) {
1717 // common case, user didn't just wrap
1718 if (u - fc >= userBase ) {
1719 userBase += fc;
1720 }
1721 } else if (u >= userBase + fc) {
1722 // user just wrapped
1723 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001724 }
1725
Glenn Kasten90548972011-12-13 11:45:07 -08001726 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001727
1728 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001729 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001730 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001731 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001732
1733 return u;
1734}
1735
1736bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1737{
Marco Nelissena1472d92012-03-30 14:36:54 -07001738 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1739
Eric Laurent38ccae22011-03-28 18:37:07 -07001740 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001741 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001742 return false;
1743 }
1744
Glenn Kasten90548972011-12-13 11:45:07 -08001745 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001746 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001747
1748 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001749 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001750 // Mark that we have read the first buffer so that next time stepUser() is called
1751 // we switch to normal obtainBuffer() timeout period
1752 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001753 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001754 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001755 // It is possible that we receive a flush()
1756 // while the mixer is processing a block: in this case,
1757 // stepServer() is called After the flush() has reset u & s and
1758 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001759 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001760 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001761 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001762 }
1763 }
1764
1765 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001766 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001767 s = loopStart;
1768 if (--loopCount == 0) {
1769 loopEnd = UINT_MAX;
1770 loopStart = UINT_MAX;
1771 }
1772 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001773
1774 uint32_t fc = this->frameCount;
1775 if (s >= fc) {
1776 // common case, server didn't just wrap
1777 if (s - fc >= serverBase ) {
1778 serverBase += fc;
1779 }
1780 } else if (s >= serverBase + fc) {
1781 // server just wrapped
1782 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001783 }
1784
Glenn Kasten90548972011-12-13 11:45:07 -08001785 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001786
Eric Laurent1703cdf2011-03-07 14:52:59 -08001787 if (!(flags & CBLK_INVALID_MSK)) {
1788 cv.signal();
1789 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001790 lock.unlock();
1791 return true;
1792}
1793
1794void* audio_track_cblk_t::buffer(uint32_t offset) const
1795{
Glenn Kasten90548972011-12-13 11:45:07 -08001796 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001797}
1798
1799uint32_t audio_track_cblk_t::framesAvailable()
1800{
1801 Mutex::Autolock _l(lock);
1802 return framesAvailable_l();
1803}
1804
1805uint32_t audio_track_cblk_t::framesAvailable_l()
1806{
Glenn Kasten90548972011-12-13 11:45:07 -08001807 uint32_t u = user;
1808 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809
Eric Laurentd1b449a2010-05-14 03:26:45 -07001810 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811 uint32_t limit = (s < loopStart) ? s : loopStart;
1812 return limit + frameCount - u;
1813 } else {
1814 return frameCount + u - s;
1815 }
1816}
1817
1818uint32_t audio_track_cblk_t::framesReady()
1819{
Glenn Kasten90548972011-12-13 11:45:07 -08001820 uint32_t u = user;
1821 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822
Eric Laurentd1b449a2010-05-14 03:26:45 -07001823 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824 if (u < loopEnd) {
1825 return u - s;
1826 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001827 // do not block on mutex shared with client on AudioFlinger side
1828 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001829 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001830 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001831 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001832 uint32_t frames = UINT_MAX;
1833 if (loopCount >= 0) {
1834 frames = (loopEnd - loopStart)*loopCount + u - s;
1835 }
1836 lock.unlock();
1837 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001838 }
1839 } else {
1840 return s - u;
1841 }
1842}
1843
Eric Laurent38ccae22011-03-28 18:37:07 -07001844bool audio_track_cblk_t::tryLock()
1845{
1846 // the code below simulates lock-with-timeout
1847 // we MUST do this to protect the AudioFlinger server
1848 // as this lock is shared with the client.
1849 status_t err;
1850
1851 err = lock.tryLock();
1852 if (err == -EBUSY) { // just wait a bit
1853 usleep(1000);
1854 err = lock.tryLock();
1855 }
1856 if (err != NO_ERROR) {
1857 // probably, the client just died.
1858 return false;
1859 }
1860 return true;
1861}
1862
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001863// -------------------------------------------------------------------------
1864
1865}; // namespace android