blob: 907f7e6017bfd9a55dcc8185499aad7651a3d859 [file] [log] [blame]
Glenn Kasten99e53b82012-01-19 08:59:58 -08001/*
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioTrack"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
26#include <sched.h>
27#include <sys/resource.h>
28
29#include <private/media/AudioTrackShared.h>
30
31#include <media/AudioSystem.h>
32#include <media/AudioTrack.h>
33
34#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070035#include <binder/Parcel.h>
36#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070038#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Dima Zavinfce7a472011-04-19 22:30:36 -070040#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080041#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070042
Dima Zavin64760242011-05-11 14:15:23 -070043#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070044#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070045
Glenn Kasten511754b2012-01-11 09:52:19 -080046#include <audio_utils/primitives.h>
47
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080049// ---------------------------------------------------------------------------
50
51// static
52status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080053 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080054 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 uint32_t sampleRate)
56{
Glenn Kasten04cd0182012-06-25 11:49:27 -070057 if (frameCount == NULL) return BAD_VALUE;
58
59 // default to 0 in case of error
60 *frameCount = 0;
61
Glenn Kastene0fa4672012-04-24 14:35:14 -070062 // FIXME merge with similar code in createTrack_l(), except we're missing
63 // some information here that is available in createTrack_l():
64 // audio_io_handle_t output
65 // audio_format_t format
66 // audio_channel_mask_t channelMask
67 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080068 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080069 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
70 return NO_INIT;
71 }
Glenn Kastene33054e2012-11-14 12:54:39 -080072 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080073 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 uint32_t afLatency;
77 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80
81 // Ensure that buffer depth covers at least audio hardware latency
82 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
83 if (minBufCount < 2) minBufCount = 2;
84
85 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070086 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080087 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
88 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080089 return NO_ERROR;
90}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080091
92// ---------------------------------------------------------------------------
93
94AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070095 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080096 mIsTimed(false),
97 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -070098 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080099{
100}
101
102AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800103 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800105 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700106 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700108 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 callback_t cbf,
110 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700111 int notificationFrames,
112 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700113 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800114 mIsTimed(false),
115 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700116 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700118 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700119 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800120 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121}
122
Andreas Huberc8139852012-01-18 10:51:55 -0800123AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800124 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800126 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700127 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800128 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700129 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 callback_t cbf,
131 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700132 int notificationFrames,
133 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700134 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800135 mIsTimed(false),
136 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700137 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700139 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800140 0 /*frameCount*/, flags, cbf, user, notificationFrames,
141 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142}
143
144AudioTrack::~AudioTrack()
145{
Steve Block3856b092011-10-20 11:56:00 +0100146 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147
148 if (mStatus == NO_ERROR) {
149 // Make sure that callback function exits in the case where
150 // it is looping on buffer full condition in obtainBuffer().
151 // Otherwise the callback thread will never exit.
152 stop();
153 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800154 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 mAudioTrackThread->requestExitAndWait();
156 mAudioTrackThread.clear();
157 }
158 mAudioTrack.clear();
159 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700160 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 }
162}
163
164status_t AudioTrack::set(
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,
Glenn Kastene33054e2012-11-14 12:54:39 -0800169 int frameCountInt,
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,
173 int notificationFrames,
174 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700175 bool threadCanCallJava,
176 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177{
Glenn Kastene33054e2012-11-14 12:54:39 -0800178 // FIXME "int" here is legacy and will be replaced by size_t later
179 if (frameCountInt < 0) {
180 ALOGE("Invalid frame count %d", frameCountInt);
181 return BAD_VALUE;
182 }
183 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700185 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
186 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187
Glenn Kastene33054e2012-11-14 12:54:39 -0800188 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700189
Eric Laurent1703cdf2011-03-07 14:52:59 -0800190 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700191 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000192 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 return INVALID_OPERATION;
194 }
195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700197 if (streamType == AUDIO_STREAM_DEFAULT) {
198 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700200
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800202 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700203 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
204 return NO_INIT;
205 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 sampleRate = afSampleRate;
207 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700208
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800210 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700211 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700213 if (channelMask == 0) {
214 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 }
216
217 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700218 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000219 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 return BAD_VALUE;
221 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700222
Glenn Kastene0fa4672012-04-24 14:35:14 -0700223 // AudioFlinger does not currently support 8-bit data in shared memory
224 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
225 ALOGE("8-bit data in shared memory is not supported");
226 return BAD_VALUE;
227 }
228
Eric Laurentc2f1f072009-07-17 12:17:14 -0700229 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700230 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700231 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800232 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700233 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700234 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700235 // only allow deep buffering for music stream type
236 if (streamType != AUDIO_STREAM_MUSIC) {
237 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
238 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700239
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700240 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700241 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700242 return BAD_VALUE;
243 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700244 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700245
Dima Zavinfce7a472011-04-19 22:30:36 -0700246 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800247 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800248 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800249 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700250
251 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000252 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 return BAD_VALUE;
254 }
255
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800256 mVolume[LEFT] = 1.0f;
257 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800258 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700259 mFrameCount = frameCount;
260 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700261 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700262 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700263 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700264 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700265
Glenn Kastena997e7a2012-08-07 09:44:19 -0700266 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700267 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700268 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
269 }
270
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800271 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800272 status_t status = createTrack_l(streamType,
273 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800274 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700275 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800276 frameCount,
277 flags,
278 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700279 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800280
Glenn Kastena997e7a2012-08-07 09:44:19 -0700281 if (status != NO_ERROR) {
282 if (mAudioTrackThread != 0) {
283 mAudioTrackThread->requestExit();
284 mAudioTrackThread.clear();
285 }
286 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700287 }
288
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289 mStatus = NO_ERROR;
290
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800291 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800292 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700293 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294 mChannelCount = channelCount;
Glenn Kasten83a03822012-11-12 07:58:20 -0800295
296 if (audio_is_linear_pcm(format)) {
297 mFrameSize = channelCount * audio_bytes_per_sample(format);
298 mFrameSizeAF = channelCount * sizeof(int16_t);
299 } else {
300 mFrameSize = sizeof(uint8_t);
301 mFrameSizeAF = sizeof(uint8_t);
302 }
303
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304 mSharedBuffer = sharedBuffer;
305 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800306 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 mLoopCount = 0;
309 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700310 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 mNewPosition = 0;
312 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700313 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700314 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315 return NO_ERROR;
316}
317
318status_t AudioTrack::initCheck() const
319{
320 return mStatus;
321}
322
323// -------------------------------------------------------------------------
324
325uint32_t AudioTrack::latency() const
326{
327 return mLatency;
328}
329
Glenn Kastenfff6d712012-01-12 16:38:12 -0800330audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331{
332 return mStreamType;
333}
334
Glenn Kastene1c39622012-01-04 09:36:37 -0800335audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336{
337 return mFormat;
338}
339
340int AudioTrack::channelCount() const
341{
342 return mChannelCount;
343}
344
Glenn Kastene33054e2012-11-14 12:54:39 -0800345size_t AudioTrack::frameCount() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346{
Eric Laurentd1b449a2010-05-14 03:26:45 -0700347 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348}
349
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350sp<IMemory>& AudioTrack::sharedBuffer()
351{
352 return mSharedBuffer;
353}
354
355// -------------------------------------------------------------------------
356
357void AudioTrack::start()
358{
359 sp<AudioTrackThread> t = mAudioTrackThread;
360
Steve Block3856b092011-10-20 11:56:00 +0100361 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362
Eric Laurentf5aafb22010-11-18 08:40:16 -0800363 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800364 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
365 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700366 sp<IAudioTrack> audioTrack = mAudioTrack;
367 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800368 audio_track_cblk_t* cblk = mCblk;
369
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800370 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700371 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800372 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800373 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700374 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800375 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
376 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800377 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800378 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800379 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800380 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700381 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700382 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700383 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800384 }
385
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700386 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700387 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800388 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800389 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800390 ALOGV("mAudioTrack->start()");
391 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800392 cblk->lock.lock();
393 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800394 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800395 }
Eric Laurent2b584242009-11-09 23:32:22 -0800396 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800397 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700398 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800399 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700400 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800401 }
402 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800403 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100404 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800405 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800406 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800407 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800408 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700409 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700410 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800411 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800412 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 }
414
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415}
416
417void AudioTrack::stop()
418{
419 sp<AudioTrackThread> t = mAudioTrackThread;
420
Steve Block3856b092011-10-20 11:56:00 +0100421 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422
Eric Laurentf5aafb22010-11-18 08:40:16 -0800423 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800424 if (mActive) {
425 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700426 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427 mAudioTrack->stop();
428 // Cancel loops (If we are in the middle of a loop, playback
429 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800430 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700431 // the playback head position will reset to 0, so if a marker is set, we need
432 // to activate it again
433 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434 // Force flush if a shared buffer is used otherwise audioflinger
435 // will not stop before end of buffer is reached.
436 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800437 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 }
439 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800440 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700442 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700443 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 }
445 }
446
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447}
448
449bool AudioTrack::stopped() const
450{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800451 AutoMutex lock(mLock);
452 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453}
454
455void AudioTrack::flush()
456{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800457 AutoMutex lock(mLock);
458 flush_l();
459}
460
461// must be called with mLock held
462void AudioTrack::flush_l()
463{
Steve Block3856b092011-10-20 11:56:00 +0100464 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700465
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700466 // clear playback marker and periodic update counter
467 mMarkerPosition = 0;
468 mMarkerReached = false;
469 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700470
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700472 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 mAudioTrack->flush();
474 // Release AudioTrack callback thread in case it was waiting for new buffers
475 // in AudioTrack::obtainBuffer()
476 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477 }
478}
479
480void AudioTrack::pause()
481{
Steve Block3856b092011-10-20 11:56:00 +0100482 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800483 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800484 if (mActive) {
485 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700486 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 mAudioTrack->pause();
488 }
489}
490
491void AudioTrack::mute(bool e)
492{
493 mAudioTrack->mute(e);
494 mMuted = e;
495}
496
497bool AudioTrack::muted() const
498{
499 return mMuted;
500}
501
Eric Laurentbe916aa2010-06-01 23:49:17 -0700502status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800504 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700505 return BAD_VALUE;
506 }
507
Eric Laurent1703cdf2011-03-07 14:52:59 -0800508 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509 mVolume[LEFT] = left;
510 mVolume[RIGHT] = right;
511
Glenn Kasten83d86532012-01-17 14:39:34 -0800512 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700513
514 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515}
516
Glenn Kastenb1c09932012-02-27 16:21:04 -0800517status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800519 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700520}
521
Eric Laurent2beeb502010-07-16 07:43:46 -0700522status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700523{
Steve Block3856b092011-10-20 11:56:00 +0100524 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800525 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700526 return BAD_VALUE;
527 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800528 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700529
530 mSendLevel = level;
531
Glenn Kasten05632a52012-01-03 14:22:33 -0800532 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700533
534 return NO_ERROR;
535}
536
Glenn Kastena5224f32012-01-04 12:41:44 -0800537void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700538{
539 if (level != NULL) {
540 *level = mSendLevel;
541 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542}
543
Glenn Kasten3b16c762012-11-14 08:44:39 -0800544status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545{
Glenn Kasten3b16c762012-11-14 08:44:39 -0800546 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547
John Grossman4ff14ba2012-02-08 16:37:41 -0800548 if (mIsTimed) {
549 return INVALID_OPERATION;
550 }
551
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700553 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 }
555 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kasten3b16c762012-11-14 08:44:39 -0800556 if (rate == 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557
Eric Laurent1703cdf2011-03-07 14:52:59 -0800558 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700559 mCblk->sampleRate = rate;
560 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561}
562
Glenn Kastena5224f32012-01-04 12:41:44 -0800563uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564{
John Grossman4ff14ba2012-02-08 16:37:41 -0800565 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800566 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800567 }
568
Eric Laurent1703cdf2011-03-07 14:52:59 -0800569 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700570 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571}
572
573status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
574{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800575 AutoMutex lock(mLock);
576 return setLoop_l(loopStart, loopEnd, loopCount);
577}
578
579// must be called with mLock held
580status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
581{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 audio_track_cblk_t* cblk = mCblk;
583
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 Mutex::Autolock _l(cblk->lock);
585
586 if (loopCount == 0) {
587 cblk->loopStart = UINT_MAX;
588 cblk->loopEnd = UINT_MAX;
589 cblk->loopCount = 0;
590 mLoopCount = 0;
591 return NO_ERROR;
592 }
593
John Grossman4ff14ba2012-02-08 16:37:41 -0800594 if (mIsTimed) {
595 return INVALID_OPERATION;
596 }
597
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700599 loopEnd - loopStart > cblk->frameCount ||
600 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700601 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
602 "user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603 return BAD_VALUE;
604 }
605
Eric Laurent9b7d9502011-03-21 11:49:00 -0700606 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700607 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
608 "framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700609 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700611 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612
613 cblk->loopStart = loopStart;
614 cblk->loopEnd = loopEnd;
615 cblk->loopCount = loopCount;
616 mLoopCount = loopCount;
617
618 return NO_ERROR;
619}
620
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621status_t AudioTrack::setMarkerPosition(uint32_t marker)
622{
Glenn Kastena0d68332012-01-27 16:47:15 -0800623 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624
625 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700626 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627
628 return NO_ERROR;
629}
630
Glenn Kastena5224f32012-01-04 12:41:44 -0800631status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632{
Glenn Kastena0d68332012-01-27 16:47:15 -0800633 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634
635 *marker = mMarkerPosition;
636
637 return NO_ERROR;
638}
639
640status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
641{
Glenn Kastena0d68332012-01-27 16:47:15 -0800642 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643
644 uint32_t curPosition;
645 getPosition(&curPosition);
646 mNewPosition = curPosition + updatePeriod;
647 mUpdatePeriod = updatePeriod;
648
649 return NO_ERROR;
650}
651
Glenn Kastena5224f32012-01-04 12:41:44 -0800652status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653{
Glenn Kastena0d68332012-01-27 16:47:15 -0800654 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655
656 *updatePeriod = mUpdatePeriod;
657
658 return NO_ERROR;
659}
660
661status_t AudioTrack::setPosition(uint32_t position)
662{
John Grossman4ff14ba2012-02-08 16:37:41 -0800663 if (mIsTimed) return INVALID_OPERATION;
664
Eric Laurent1703cdf2011-03-07 14:52:59 -0800665 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800667 if (!stopped_l()) return INVALID_OPERATION;
668
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700669 audio_track_cblk_t* cblk = mCblk;
670 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700672 if (position > cblk->user) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800673
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700674 cblk->server = position;
675 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700676
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677 return NO_ERROR;
678}
679
680status_t AudioTrack::getPosition(uint32_t *position)
681{
Glenn Kastena0d68332012-01-27 16:47:15 -0800682 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800683 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700684 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685
686 return NO_ERROR;
687}
688
689status_t AudioTrack::reload()
690{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800691 AutoMutex lock(mLock);
692
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800693 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694
Eric Laurent1703cdf2011-03-07 14:52:59 -0800695 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700697 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten864585d2012-11-06 16:15:41 -0800698 cblk->stepUserOut(cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800699
700 return NO_ERROR;
701}
702
Eric Laurentc2f1f072009-07-17 12:17:14 -0700703audio_io_handle_t AudioTrack::getOutput()
704{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800705 AutoMutex lock(mLock);
706 return getOutput_l();
707}
708
709// must be called with mLock held
710audio_io_handle_t AudioTrack::getOutput_l()
711{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800712 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800713 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700714}
715
Glenn Kastena5224f32012-01-04 12:41:44 -0800716int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700717{
718 return mSessionId;
719}
720
721status_t AudioTrack::attachAuxEffect(int effectId)
722{
Steve Block3856b092011-10-20 11:56:00 +0100723 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700724 status_t status = mAudioTrack->attachAuxEffect(effectId);
725 if (status == NO_ERROR) {
726 mAuxEffectId = effectId;
727 }
728 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700729}
730
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800731// -------------------------------------------------------------------------
732
Eric Laurent1703cdf2011-03-07 14:52:59 -0800733// must be called with mLock held
734status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800735 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800736 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800737 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700738 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800739 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700740 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800741 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700742 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800743{
744 status_t status;
745 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
746 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700747 ALOGE("Could not get audioflinger");
748 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800749 }
750
Eric Laurentd1b449a2010-05-14 03:26:45 -0700751 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700752 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700753 return NO_INIT;
754 }
755
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700756 // Client decides whether the track is TIMED (see below), but can only express a preference
757 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700758 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700759 // either of these use cases:
760 // use case 1: shared buffer
761 (sharedBuffer != 0) ||
762 // use case 2: callback handler
763 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800764 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700765 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700766 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700767 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700768 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700769 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700770
Eric Laurentd1b449a2010-05-14 03:26:45 -0700771 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700772
Dima Zavinfce7a472011-04-19 22:30:36 -0700773 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700774
Eric Laurentd1b449a2010-05-14 03:26:45 -0700775 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700776 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700777 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700778 } else if (frameCount == 0) {
Glenn Kastene33054e2012-11-14 12:54:39 -0800779 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700780 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
781 return NO_INIT;
782 }
783 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700784 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700785
786 } else if (sharedBuffer != 0) {
787
788 // Ensure that buffer alignment matches channelCount
789 int channelCount = popcount(channelMask);
790 // 8-bit data in shared memory is not currently supported by AudioFlinger
791 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
792 if (channelCount > 1) {
793 // More than 2 channels does not require stronger alignment than stereo
794 alignment <<= 1;
795 }
796 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
797 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
798 sharedBuffer->pointer(), channelCount);
799 return BAD_VALUE;
800 }
801
802 // When initializing a shared buffer AudioTrack via constructors,
803 // there's no frameCount parameter.
804 // But when initializing a shared buffer AudioTrack via set(),
805 // there _is_ a frameCount parameter. We silently ignore it.
806 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
807
808 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
809
810 // FIXME move these calculations and associated checks to server
Glenn Kasten3b16c762012-11-14 08:44:39 -0800811 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700812 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
813 return NO_INIT;
814 }
Glenn Kastene33054e2012-11-14 12:54:39 -0800815 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700816 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
817 return NO_INIT;
818 }
819
Eric Laurentd1b449a2010-05-14 03:26:45 -0700820 // Ensure that buffer depth covers at least audio hardware latency
821 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
822 if (minBufCount < 2) minBufCount = 2;
823
Glenn Kastene33054e2012-11-14 12:54:39 -0800824 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
825 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800826 ", afLatency=%d",
827 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700828
829 if (frameCount == 0) {
830 frameCount = minFrameCount;
831 }
832 if (mNotificationFramesAct == 0) {
833 mNotificationFramesAct = frameCount/2;
834 }
835 // Make sure that application is notified with sufficient margin
836 // before underrun
Glenn Kastene33054e2012-11-14 12:54:39 -0800837 if (mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700838 mNotificationFramesAct = frameCount/2;
839 }
840 if (frameCount < minFrameCount) {
841 // not ALOGW because it happens all the time when playing key clicks over A2DP
842 ALOGV("Minimum buffer size corrected from %d to %d",
843 frameCount, minFrameCount);
844 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800845 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700846
Glenn Kastene0fa4672012-04-24 14:35:14 -0700847 } else {
848 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700849 }
850
Glenn Kastena075db42012-03-06 11:22:44 -0800851 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
852 if (mIsTimed) {
853 trackFlags |= IAudioFlinger::TRACK_TIMED;
854 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800855
856 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700857 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700858 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800859 if (mAudioTrackThread != 0) {
860 tid = mAudioTrackThread->getTid();
861 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700862 }
863
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800864 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
865 streamType,
866 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700867 // AudioFlinger only sees 16-bit PCM
868 format == AUDIO_FORMAT_PCM_8_BIT ?
869 AUDIO_FORMAT_PCM_16_BIT : format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700870 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800871 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800872 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800873 sharedBuffer,
874 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800875 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700876 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800877 &status);
878
879 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000880 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800881 return status;
882 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700883 sp<IMemory> iMem = track->getCblk();
884 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000885 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800886 return NO_INIT;
887 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800888 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700889 mCblkMemory = iMem;
890 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
891 mCblk = cblk;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800892 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800893 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700894 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", cblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800895 } else {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700896 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", cblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700897 // once denied, do not request again if IAudioTrack is re-created
898 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
899 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800900 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700901 if (sharedBuffer == 0) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700902 mNotificationFramesAct = cblk->frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700903 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800904 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800905 if (sharedBuffer == 0) {
Glenn Kastenb929e412012-11-08 12:13:58 -0800906 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800907 } else {
Glenn Kastenb929e412012-11-08 12:13:58 -0800908 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700909 // Force buffer full condition as data is already present in shared memory
Glenn Kasten864585d2012-11-06 16:15:41 -0800910 cblk->stepUserOut(cblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800911 }
912
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700913 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700914 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700915 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700916 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700917 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
918 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700919 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700920 // FIXME don't believe this lie
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700921 mLatency = afLatency + (1000*cblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -0700922 // If IAudioTrack is re-created, don't let the requested frameCount
923 // decrease. This can confuse clients that cache frameCount().
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700924 if (cblk->frameCount > mFrameCount) {
925 mFrameCount = cblk->frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700926 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800927 return NO_ERROR;
928}
929
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800930status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
931{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800932 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800933 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700934 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935 audio_track_cblk_t* cblk = mCblk;
936 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700937 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938
939 audioBuffer->frameCount = 0;
940 audioBuffer->size = 0;
941
Glenn Kasten864585d2012-11-06 16:15:41 -0800942 uint32_t framesAvail = cblk->framesAvailableOut();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943
Eric Laurent9b7d9502011-03-21 11:49:00 -0700944 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800945 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700946 goto create_new_track;
947 }
948 cblk->lock.unlock();
949
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800951 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800952 goto start_loop_here;
953 while (framesAvail == 0) {
954 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800955 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100956 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800957 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800958 return NO_MORE_BUFFERS;
959 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800960 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800961 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800963 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800964 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800965 mLock.unlock();
Glenn Kastena47f3162012-11-07 10:13:08 -0800966 // this condition is in shared memory, so if IAudioTrack and control block
967 // are replaced due to mediaserver death or IAudioTrack invalidation then
968 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700969 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700970 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800971 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800972 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800973 return status_t(STOPPED);
974 }
Glenn Kastena47f3162012-11-07 10:13:08 -0800975 // IAudioTrack may have been re-created while mLock was unlocked
976 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800977 cblk->lock.lock();
978 }
979
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800980 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700981 goto create_new_track;
982 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800983 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700984 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800985 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
986 // timing out when a loop has been set and we have already written upto loop end
987 // is a normal condition: no need to wake AudioFlinger up.
988 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700989 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
990 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700991 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800992 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800993 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800994 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800995 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800996 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800997create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700998 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800999 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001000 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001001 }
1002 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001003 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001004 cblk->lock.unlock();
1005 return result;
1006 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 }
1008 cblk->waitTimeMs = 0;
1009 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001010
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001011 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001012 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 return TIMED_OUT;
1014 }
1015 }
1016 // read the server count again
1017 start_loop_here:
Glenn Kasten864585d2012-11-06 16:15:41 -08001018 framesAvail = cblk->framesAvailableOut_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001019 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001020 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 }
1022
1023 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001024
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001025 if (framesReq > framesAvail) {
1026 framesReq = framesAvail;
1027 }
1028
1029 uint32_t u = cblk->user;
1030 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1031
Marco Nelissena1472d92012-03-30 14:36:54 -07001032 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 framesReq = bufferEnd - u;
1034 }
1035
Eric Laurentc2f1f072009-07-17 12:17:14 -07001036 audioBuffer->frameCount = framesReq;
Glenn Kasten83a03822012-11-12 07:58:20 -08001037 audioBuffer->size = framesReq * mFrameSizeAF;
1038 audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001039 active = mActive;
1040 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1041}
1042
1043void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1044{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001045 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001046 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten864585d2012-11-06 16:15:41 -08001047 cblk->stepUserOut(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001048 if (audioBuffer->frameCount > 0) {
1049 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001050 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1051 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1052 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001053 mAudioTrack->start();
1054 }
1055 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001056}
1057
1058// -------------------------------------------------------------------------
1059
1060ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1061{
1062
1063 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001064 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001065
1066 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001067 // Sanity-check: user is most-likely passing an error code, and it would
1068 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001069 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001070 buffer, userSize, userSize);
1071 return BAD_VALUE;
1072 }
1073
Steve Block3856b092011-10-20 11:56:00 +01001074 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001075
Eric Laurentdf839842012-05-31 14:27:14 -07001076 if (userSize == 0) {
1077 return 0;
1078 }
1079
Eric Laurent1703cdf2011-03-07 14:52:59 -08001080 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1081 // while we are accessing the cblk
1082 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001083 sp<IAudioTrack> audioTrack = mAudioTrack;
1084 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001085 mLock.unlock();
1086
Glenn Kastena47f3162012-11-07 10:13:08 -08001087 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1088 // so all cblk references might still refer to old shared memory, but that should be benign
1089
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001090 ssize_t written = 0;
1091 const int8_t *src = (const int8_t *)buffer;
1092 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001093 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001094
1095 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001096 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001097
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001098 status_t err = obtainBuffer(&audioBuffer, -1);
1099 if (err < 0) {
1100 // out of buffers, return #bytes written
1101 if (err == status_t(NO_MORE_BUFFERS))
1102 break;
1103 return ssize_t(err);
1104 }
1105
1106 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001107
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001108 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001109 // Divide capacity by 2 to take expansion into account
1110 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001111 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001112 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001113 toWrite = audioBuffer.size;
1114 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115 }
Glenn Kastenbc0f6b92012-11-12 14:32:06 -08001116 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001117 userSize -= toWrite;
1118 written += toWrite;
1119
1120 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001121 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122
1123 return written;
1124}
1125
1126// -------------------------------------------------------------------------
1127
John Grossman4ff14ba2012-02-08 16:37:41 -08001128TimedAudioTrack::TimedAudioTrack() {
1129 mIsTimed = true;
1130}
1131
1132status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1133{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001134 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001135 status_t result = UNKNOWN_ERROR;
1136
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001137 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1138 // while we are accessing the cblk
1139 sp<IAudioTrack> audioTrack = mAudioTrack;
1140 sp<IMemory> iMem = mCblkMemory;
1141
John Grossman4ff14ba2012-02-08 16:37:41 -08001142 // If the track is not invalid already, try to allocate a buffer. alloc
1143 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001144 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001145 audio_track_cblk_t* cblk = mCblk;
1146 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001147 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1148 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001149 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001150 }
1151 }
1152
1153 // If the track is invalid at this point, attempt to restore it. and try the
1154 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001155 if (cblk->flags & CBLK_INVALID) {
1156 cblk->lock.lock();
1157 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001158 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001159 cblk = temp;
1160 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001161
1162 if (result == OK)
1163 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1164 }
1165
1166 return result;
1167}
1168
1169status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1170 int64_t pts)
1171{
Eric Laurentdf839842012-05-31 14:27:14 -07001172 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1173 {
1174 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001175 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001176 // restart track if it was disabled by audioflinger due to previous underrun
1177 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001178 mActive && (cblk->flags & CBLK_DISABLED)) {
1179 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001180 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1181 mAudioTrack->start();
1182 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001183 }
Eric Laurentdf839842012-05-31 14:27:14 -07001184 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001185}
1186
1187status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1188 TargetTimeline target)
1189{
1190 return mAudioTrack->setMediaTimeTransform(xform, target);
1191}
1192
1193// -------------------------------------------------------------------------
1194
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001195bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1196{
1197 Buffer audioBuffer;
1198 uint32_t frames;
1199 size_t writtenSize;
1200
Eric Laurent1703cdf2011-03-07 14:52:59 -08001201 mLock.lock();
1202 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1203 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001204 sp<IAudioTrack> audioTrack = mAudioTrack;
1205 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001206 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001207 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001208 mLock.unlock();
1209
Glenn Kastena47f3162012-11-07 10:13:08 -08001210 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1211 // so all cblk references might still refer to old shared memory, but that should be benign
1212
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 // Manage underrun callback
Glenn Kasten864585d2012-11-06 16:15:41 -08001214 if (active && (cblk->framesAvailableOut() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001216 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001218 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001219 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001220 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001221 if (mSharedBuffer != 0) return false;
1222 }
1223 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001224
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001226 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 int loopCount = -1;
1228 mLoopCount--;
1229 if (mLoopCount >= 0) loopCount = mLoopCount;
1230
1231 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1232 }
1233
1234 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001235 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001236 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001237 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001238 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001239 }
1240 }
1241
1242 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001243 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001244 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1246 mNewPosition += mUpdatePeriod;
1247 }
1248 }
1249
1250 // If Shared buffer is used, no data is requested from client.
1251 if (mSharedBuffer != 0) {
1252 frames = 0;
1253 } else {
1254 frames = mRemainingFrames;
1255 }
1256
Glenn Kasten99e53b82012-01-19 08:59:58 -08001257 // See description of waitCount parameter at declaration of obtainBuffer().
1258 // The logic below prevents us from being stuck below at obtainBuffer()
1259 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001260 int32_t waitCount = -1;
1261 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1262 waitCount = 1;
1263 }
1264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 do {
1266
1267 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001268
Eric Laurent2267ba12011-09-07 11:13:23 -07001269 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 if (err < NO_ERROR) {
1271 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001272 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1273 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 return false;
1275 }
1276 break;
1277 }
1278 if (err == status_t(STOPPED)) return false;
1279
1280 // Divide buffer size by 2 to take into account the expansion
1281 // due to 8 to 16 bit conversion: the callback must fill only half
1282 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001283 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001284 audioBuffer.size >>= 1;
1285 }
1286
1287 size_t reqSize = audioBuffer.size;
1288 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1289 writtenSize = audioBuffer.size;
1290
1291 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001292 if (ssize_t(writtenSize) <= 0) {
1293 // The callback is done filling buffers
1294 // Keep this thread going to handle timed events and
1295 // still try to get more data in intervals of WAIT_PERIOD_MS
1296 // but don't just loop and block the CPU, so wait
1297 usleep(WAIT_PERIOD_MS*1000);
1298 break;
1299 }
Eric Laurentdf839842012-05-31 14:27:14 -07001300
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301 if (writtenSize > reqSize) writtenSize = reqSize;
1302
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001303 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001304 // 8 to 16 bit conversion, note that source and destination are the same address
1305 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306 writtenSize <<= 1;
1307 }
1308
1309 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001310 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1311 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001312 // 16 bit.
Glenn Kasten83a03822012-11-12 07:58:20 -08001313 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001314
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315 frames -= audioBuffer.frameCount;
1316
1317 releaseBuffer(&audioBuffer);
1318 }
1319 while (frames);
1320
1321 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001322 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001323 } else {
1324 mRemainingFrames = frames;
1325 }
1326 return true;
1327}
1328
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001329// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001330// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001331// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1332// FIXME Don't depend on caller to hold strong references.
1333status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001334{
1335 status_t result;
1336
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001337 audio_track_cblk_t* cblk = refCblk;
1338 audio_track_cblk_t* newCblk = cblk;
Glenn Kasten827e5f12012-11-02 10:00:06 -07001339 ALOGW("dead IAudioTrack, creating a new one from %s",
1340 fromStart ? "start()" : "obtainBuffer()");
Eric Laurent1703cdf2011-03-07 14:52:59 -08001341
Glenn Kastena47f3162012-11-07 10:13:08 -08001342 // signal old cblk condition so that other threads waiting for available buffers stop
1343 // waiting now
1344 cblk->cv.broadcast();
1345 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001346
Glenn Kastena47f3162012-11-07 10:13:08 -08001347 // refresh the audio configuration cache in this process to make sure we get new
1348 // output parameters in getOutput_l() and createTrack_l()
1349 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001350
Glenn Kastena47f3162012-11-07 10:13:08 -08001351 // if the new IAudioTrack is created, createTrack_l() will modify the
1352 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1353 // It will also delete the strong references on previous IAudioTrack and IMemory
1354 result = createTrack_l(mStreamType,
1355 cblk->sampleRate,
1356 mFormat,
1357 mChannelMask,
1358 mFrameCount,
1359 mFlags,
1360 mSharedBuffer,
1361 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001362
Glenn Kastena47f3162012-11-07 10:13:08 -08001363 if (result == NO_ERROR) {
1364 uint32_t user = cblk->user;
1365 uint32_t server = cblk->server;
1366 // restore write index and set other indexes to reflect empty buffer status
1367 newCblk = mCblk;
1368 newCblk->user = user;
1369 newCblk->server = user;
1370 newCblk->userBase = user;
1371 newCblk->serverBase = user;
1372 // restore loop: this is not guaranteed to succeed if new frame count is not
1373 // compatible with loop length
1374 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1375 if (!fromStart) {
1376 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1377 // Make sure that a client relying on callback events indicating underrun or
1378 // the actual amount of audio frames played (e.g SoundPool) receives them.
1379 if (mSharedBuffer == 0) {
1380 uint32_t frames = 0;
1381 if (user > server) {
1382 frames = ((user - server) > newCblk->frameCount) ?
1383 newCblk->frameCount : (user - server);
Glenn Kasten83a03822012-11-12 07:58:20 -08001384 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001385 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001386 // restart playback even if buffer is not completely filled.
1387 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1388 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1389 // the client
1390 newCblk->stepUserOut(frames);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001391 }
1392 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001393 if (mSharedBuffer != 0) {
1394 newCblk->stepUserOut(newCblk->frameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001395 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001396 if (mActive) {
1397 result = mAudioTrack->start();
1398 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1399 }
1400 if (fromStart && result == NO_ERROR) {
1401 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001402 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001403 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001404 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001405 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001406 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001407
1408 if (result == NO_ERROR) {
1409 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001410 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001411 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001412 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001413
Glenn Kasten827e5f12012-11-02 10:00:06 -07001414 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001415
1416 return result;
1417}
1418
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1420{
1421
1422 const size_t SIZE = 256;
1423 char buffer[SIZE];
1424 String8 result;
1425
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001426 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001428 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1429 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001430 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001431 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001432 mChannelCount, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001433 result.append(buffer);
Glenn Kasten3b16c762012-11-14 08:44:39 -08001434 snprintf(buffer, 255, " sample rate(%u), status(%d), muted(%d)\n",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001435 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001436 result.append(buffer);
1437 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1438 result.append(buffer);
1439 ::write(fd, result.string(), result.size());
1440 return NO_ERROR;
1441}
1442
1443// =========================================================================
1444
1445AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001446 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1447{
1448}
1449
1450AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001451{
1452}
1453
1454bool AudioTrack::AudioTrackThread::threadLoop()
1455{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001456 {
1457 AutoMutex _l(mMyLock);
1458 if (mPaused) {
1459 mMyCond.wait(mMyLock);
1460 // caller will check for exitPending()
1461 return true;
1462 }
1463 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001464 if (!mReceiver.processAudioBuffer(this)) {
1465 pause();
1466 }
1467 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001468}
1469
Glenn Kasten3acbd052012-02-28 10:39:56 -08001470void AudioTrack::AudioTrackThread::requestExit()
1471{
1472 // must be in this order to avoid a race condition
1473 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001474 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001475}
1476
1477void AudioTrack::AudioTrackThread::pause()
1478{
1479 AutoMutex _l(mMyLock);
1480 mPaused = true;
1481}
1482
1483void AudioTrack::AudioTrackThread::resume()
1484{
1485 AutoMutex _l(mMyLock);
1486 if (mPaused) {
1487 mPaused = false;
1488 mMyCond.signal();
1489 }
1490}
1491
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492// =========================================================================
1493
Eric Laurent38ccae22011-03-28 18:37:07 -07001494
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001495audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001496 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastenb929e412012-11-08 12:13:58 -08001497 userBase(0), serverBase(0), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001498 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001499 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500{
1501}
1502
Glenn Kasten864585d2012-11-06 16:15:41 -08001503uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001504{
Marco Nelissena1472d92012-03-30 14:36:54 -07001505 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001506
Marco Nelissena1472d92012-03-30 14:36:54 -07001507 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001508 u += frameCount;
1509 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten864585d2012-11-06 16:15:41 -08001510 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001511 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1512 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1513 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1514 }
Glenn Kasten90548972011-12-13 11:45:07 -08001515 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001516 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001517 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001518 }
1519
Marco Nelissena1472d92012-03-30 14:36:54 -07001520 uint32_t fc = this->frameCount;
1521 if (u >= fc) {
1522 // common case, user didn't just wrap
1523 if (u - fc >= userBase ) {
1524 userBase += fc;
1525 }
1526 } else if (u >= userBase + fc) {
1527 // user just wrapped
1528 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001529 }
1530
Glenn Kasten90548972011-12-13 11:45:07 -08001531 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001532
1533 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001534 if (flags & CBLK_UNDERRUN) {
1535 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001536 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001537
1538 return u;
1539}
1540
Glenn Kasten864585d2012-11-06 16:15:41 -08001541bool audio_track_cblk_t::stepServer(uint32_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542{
Marco Nelissena1472d92012-03-30 14:36:54 -07001543 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1544
Eric Laurent38ccae22011-03-28 18:37:07 -07001545 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001546 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547 return false;
1548 }
1549
Glenn Kasten90548972011-12-13 11:45:07 -08001550 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001551 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552
1553 s += frameCount;
Glenn Kasten864585d2012-11-06 16:15:41 -08001554 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001555 // Mark that we have read the first buffer so that next time stepUser() is called
1556 // we switch to normal obtainBuffer() timeout period
1557 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001558 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001559 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001560 // It is possible that we receive a flush()
1561 // while the mixer is processing a block: in this case,
1562 // stepServer() is called After the flush() has reset u & s and
1563 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001564 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001565 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001566 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567 }
1568 }
1569
1570 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001571 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001572 s = loopStart;
1573 if (--loopCount == 0) {
1574 loopEnd = UINT_MAX;
1575 loopStart = UINT_MAX;
1576 }
1577 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001578
1579 uint32_t fc = this->frameCount;
1580 if (s >= fc) {
1581 // common case, server didn't just wrap
1582 if (s - fc >= serverBase ) {
1583 serverBase += fc;
1584 }
1585 } else if (s >= serverBase + fc) {
1586 // server just wrapped
1587 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001588 }
1589
Glenn Kasten90548972011-12-13 11:45:07 -08001590 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001591
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001592 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001593 cv.signal();
1594 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001595 lock.unlock();
1596 return true;
1597}
1598
Glenn Kasten83a03822012-11-12 07:58:20 -08001599void* audio_track_cblk_t::buffer(void *buffers, size_t frameSize, uint32_t offset) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001600{
Glenn Kasten90548972011-12-13 11:45:07 -08001601 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001602}
1603
Glenn Kasten864585d2012-11-06 16:15:41 -08001604uint32_t audio_track_cblk_t::framesAvailable(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605{
1606 Mutex::Autolock _l(lock);
Glenn Kasten864585d2012-11-06 16:15:41 -08001607 return framesAvailable_l(isOut);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001608}
1609
Glenn Kasten864585d2012-11-06 16:15:41 -08001610uint32_t audio_track_cblk_t::framesAvailable_l(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001611{
Glenn Kasten90548972011-12-13 11:45:07 -08001612 uint32_t u = user;
1613 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001614
Glenn Kasten864585d2012-11-06 16:15:41 -08001615 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001616 uint32_t limit = (s < loopStart) ? s : loopStart;
1617 return limit + frameCount - u;
1618 } else {
1619 return frameCount + u - s;
1620 }
1621}
1622
Glenn Kasten864585d2012-11-06 16:15:41 -08001623uint32_t audio_track_cblk_t::framesReady(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001624{
Glenn Kasten90548972011-12-13 11:45:07 -08001625 uint32_t u = user;
1626 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001627
Glenn Kasten864585d2012-11-06 16:15:41 -08001628 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001629 if (u < loopEnd) {
1630 return u - s;
1631 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001632 // do not block on mutex shared with client on AudioFlinger side
1633 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001634 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001635 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001636 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001637 uint32_t frames = UINT_MAX;
1638 if (loopCount >= 0) {
1639 frames = (loopEnd - loopStart)*loopCount + u - s;
1640 }
1641 lock.unlock();
1642 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643 }
1644 } else {
1645 return s - u;
1646 }
1647}
1648
Eric Laurent38ccae22011-03-28 18:37:07 -07001649bool audio_track_cblk_t::tryLock()
1650{
1651 // the code below simulates lock-with-timeout
1652 // we MUST do this to protect the AudioFlinger server
1653 // as this lock is shared with the client.
1654 status_t err;
1655
1656 err = lock.tryLock();
1657 if (err == -EBUSY) { // just wait a bit
1658 usleep(1000);
1659 err = lock.tryLock();
1660 }
1661 if (err != NO_ERROR) {
1662 // probably, the client just died.
1663 return false;
1664 }
1665 return true;
1666}
1667
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001668// -------------------------------------------------------------------------
1669
1670}; // namespace android