blob: 04634336a0fd20a2374e42398372ebe10f755aec [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 Kastend65d73c2012-06-22 17:21:07 -070057 if (frameCount == NULL) {
58 return BAD_VALUE;
59 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070060
61 // default to 0 in case of error
62 *frameCount = 0;
63
Glenn Kastene0fa4672012-04-24 14:35:14 -070064 // FIXME merge with similar code in createTrack_l(), except we're missing
65 // some information here that is available in createTrack_l():
66 // audio_io_handle_t output
67 // audio_format_t format
68 // audio_channel_mask_t channelMask
69 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080070 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080071 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
72 return NO_INIT;
73 }
Glenn Kastene33054e2012-11-14 12:54:39 -080074 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080075 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
76 return NO_INIT;
77 }
78 uint32_t afLatency;
79 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
80 return NO_INIT;
81 }
82
83 // Ensure that buffer depth covers at least audio hardware latency
84 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
85 if (minBufCount < 2) minBufCount = 2;
86
87 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070088 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080089 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
90 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080091 return NO_ERROR;
92}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093
94// ---------------------------------------------------------------------------
95
96AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070097 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080098 mIsTimed(false),
99 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700100 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101{
102}
103
104AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800105 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800106 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800107 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700108 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700110 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 callback_t cbf,
112 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700113 int notificationFrames,
114 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700115 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800116 mIsTimed(false),
117 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700118 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700120 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700121 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800122 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123}
124
Andreas Huberc8139852012-01-18 10:51:55 -0800125AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800126 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800128 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700129 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700131 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 callback_t cbf,
133 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700134 int notificationFrames,
135 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700136 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800137 mIsTimed(false),
138 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700139 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800140{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700141 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800142 0 /*frameCount*/, flags, cbf, user, notificationFrames,
143 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144}
145
146AudioTrack::~AudioTrack()
147{
Steve Block3856b092011-10-20 11:56:00 +0100148 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149
150 if (mStatus == NO_ERROR) {
151 // Make sure that callback function exits in the case where
152 // it is looping on buffer full condition in obtainBuffer().
153 // Otherwise the callback thread will never exit.
154 stop();
155 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800156 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 mAudioTrackThread->requestExitAndWait();
158 mAudioTrackThread.clear();
159 }
160 mAudioTrack.clear();
161 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700162 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 }
164}
165
166status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800167 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800169 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700170 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800171 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700172 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800173 callback_t cbf,
174 void* user,
175 int notificationFrames,
176 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700177 bool threadCanCallJava,
178 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179{
Glenn Kastene33054e2012-11-14 12:54:39 -0800180 // FIXME "int" here is legacy and will be replaced by size_t later
181 if (frameCountInt < 0) {
182 ALOGE("Invalid frame count %d", frameCountInt);
183 return BAD_VALUE;
184 }
185 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700187 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
188 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189
Glenn Kastene33054e2012-11-14 12:54:39 -0800190 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700191
Eric Laurent1703cdf2011-03-07 14:52:59 -0800192 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700193 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000194 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 return INVALID_OPERATION;
196 }
197
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700199 if (streamType == AUDIO_STREAM_DEFAULT) {
200 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800204 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700205 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
206 return NO_INIT;
207 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 sampleRate = afSampleRate;
209 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700210
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800212 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700213 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700215 if (channelMask == 0) {
216 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217 }
218
219 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700220 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000221 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222 return BAD_VALUE;
223 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700224
Glenn Kastene0fa4672012-04-24 14:35:14 -0700225 // AudioFlinger does not currently support 8-bit data in shared memory
226 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
227 ALOGE("8-bit data in shared memory is not supported");
228 return BAD_VALUE;
229 }
230
Eric Laurentc2f1f072009-07-17 12:17:14 -0700231 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700232 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700233 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800234 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700235 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700236 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700237 // only allow deep buffering for music stream type
238 if (streamType != AUDIO_STREAM_MUSIC) {
239 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
240 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700241
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700242 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700243 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700244 return BAD_VALUE;
245 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800246 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700247 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800248 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800251 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800252 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800253 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254
255 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000256 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257 return BAD_VALUE;
258 }
259
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800260 mVolume[LEFT] = 1.0f;
261 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800262 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700263 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800264 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700265 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700266 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700267 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700268 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700269 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700270
Glenn Kastena997e7a2012-08-07 09:44:19 -0700271 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700272 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700273 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
274 }
275
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800276 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800277 status_t status = createTrack_l(streamType,
278 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800279 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800280 frameCount,
281 flags,
282 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700283 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800284
Glenn Kastena997e7a2012-08-07 09:44:19 -0700285 if (status != NO_ERROR) {
286 if (mAudioTrackThread != 0) {
287 mAudioTrackThread->requestExit();
288 mAudioTrackThread.clear();
289 }
290 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700291 }
292
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800293 mStatus = NO_ERROR;
294
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800296 mFormat = format;
Glenn Kasten83a03822012-11-12 07:58:20 -0800297
298 if (audio_is_linear_pcm(format)) {
299 mFrameSize = channelCount * audio_bytes_per_sample(format);
300 mFrameSizeAF = channelCount * sizeof(int16_t);
301 } else {
302 mFrameSize = sizeof(uint8_t);
303 mFrameSizeAF = sizeof(uint8_t);
304 }
305
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306 mSharedBuffer = sharedBuffer;
307 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800308 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 mLoopCount = 0;
311 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700312 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 mNewPosition = 0;
314 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700315 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700316 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317 return NO_ERROR;
318}
319
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320// -------------------------------------------------------------------------
321
322void AudioTrack::start()
323{
324 sp<AudioTrackThread> t = mAudioTrackThread;
325
Steve Block3856b092011-10-20 11:56:00 +0100326 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327
Eric Laurentf5aafb22010-11-18 08:40:16 -0800328 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800329 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
330 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700331 sp<IAudioTrack> audioTrack = mAudioTrack;
332 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800333 audio_track_cblk_t* cblk = mCblk;
334
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800335 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700336 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800337 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800338 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700339 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800340 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
341 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800342 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800343 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800344 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800345 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700346 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700347 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700348 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800349 }
350
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700351 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700352 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800353 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800354 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800355 ALOGV("mAudioTrack->start()");
356 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800357 cblk->lock.lock();
358 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800359 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800360 }
Eric Laurent2b584242009-11-09 23:32:22 -0800361 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800362 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700363 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800364 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700365 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800366 }
367 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800368 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100369 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800370 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800371 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800372 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800373 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700374 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700375 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800376 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800377 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378 }
379
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380}
381
382void AudioTrack::stop()
383{
384 sp<AudioTrackThread> t = mAudioTrackThread;
385
Steve Block3856b092011-10-20 11:56:00 +0100386 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387
Eric Laurentf5aafb22010-11-18 08:40:16 -0800388 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800389 if (mActive) {
390 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700391 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800392 mAudioTrack->stop();
393 // Cancel loops (If we are in the middle of a loop, playback
394 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800395 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700396 // the playback head position will reset to 0, so if a marker is set, we need
397 // to activate it again
398 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 // Force flush if a shared buffer is used otherwise audioflinger
400 // will not stop before end of buffer is reached.
401 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800402 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800403 }
404 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800405 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700407 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700408 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 }
410 }
411
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412}
413
414bool AudioTrack::stopped() const
415{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800416 AutoMutex lock(mLock);
417 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418}
419
420void AudioTrack::flush()
421{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800422 AutoMutex lock(mLock);
423 flush_l();
424}
425
426// must be called with mLock held
427void AudioTrack::flush_l()
428{
Steve Block3856b092011-10-20 11:56:00 +0100429 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700430
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700431 // clear playback marker and periodic update counter
432 mMarkerPosition = 0;
433 mMarkerReached = false;
434 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700435
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700437 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 mAudioTrack->flush();
439 // Release AudioTrack callback thread in case it was waiting for new buffers
440 // in AudioTrack::obtainBuffer()
441 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 }
443}
444
445void AudioTrack::pause()
446{
Steve Block3856b092011-10-20 11:56:00 +0100447 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800448 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800449 if (mActive) {
450 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700451 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 mAudioTrack->pause();
453 }
454}
455
456void AudioTrack::mute(bool e)
457{
458 mAudioTrack->mute(e);
459 mMuted = e;
460}
461
Eric Laurentbe916aa2010-06-01 23:49:17 -0700462status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800464 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700465 return BAD_VALUE;
466 }
467
Eric Laurent1703cdf2011-03-07 14:52:59 -0800468 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 mVolume[LEFT] = left;
470 mVolume[RIGHT] = right;
471
Glenn Kasten83d86532012-01-17 14:39:34 -0800472 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700473
474 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475}
476
Glenn Kastenb1c09932012-02-27 16:21:04 -0800477status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800479 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700480}
481
Eric Laurent2beeb502010-07-16 07:43:46 -0700482status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700483{
Steve Block3856b092011-10-20 11:56:00 +0100484 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800485 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700486 return BAD_VALUE;
487 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800488 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700489
490 mSendLevel = level;
491
Glenn Kasten05632a52012-01-03 14:22:33 -0800492 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700493
494 return NO_ERROR;
495}
496
Glenn Kastena5224f32012-01-04 12:41:44 -0800497void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700498{
499 if (level != NULL) {
500 *level = mSendLevel;
501 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502}
503
Glenn Kasten3b16c762012-11-14 08:44:39 -0800504status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505{
Glenn Kasten3b16c762012-11-14 08:44:39 -0800506 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800507
John Grossman4ff14ba2012-02-08 16:37:41 -0800508 if (mIsTimed) {
509 return INVALID_OPERATION;
510 }
511
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700513 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 }
515 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700516 if (rate == 0 || rate > afSamplingRate*2 ) {
517 return BAD_VALUE;
518 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519
Eric Laurent1703cdf2011-03-07 14:52:59 -0800520 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700521 mCblk->sampleRate = rate;
522 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523}
524
Glenn Kastena5224f32012-01-04 12:41:44 -0800525uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526{
John Grossman4ff14ba2012-02-08 16:37:41 -0800527 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800528 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800529 }
530
Eric Laurent1703cdf2011-03-07 14:52:59 -0800531 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700532 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800533}
534
535status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
536{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800537 AutoMutex lock(mLock);
538 return setLoop_l(loopStart, loopEnd, loopCount);
539}
540
541// must be called with mLock held
542status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
543{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544 audio_track_cblk_t* cblk = mCblk;
545
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 Mutex::Autolock _l(cblk->lock);
547
548 if (loopCount == 0) {
549 cblk->loopStart = UINT_MAX;
550 cblk->loopEnd = UINT_MAX;
551 cblk->loopCount = 0;
552 mLoopCount = 0;
553 return NO_ERROR;
554 }
555
John Grossman4ff14ba2012-02-08 16:37:41 -0800556 if (mIsTimed) {
557 return INVALID_OPERATION;
558 }
559
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560 if (loopStart >= loopEnd ||
Glenn Kastenb6037442012-11-14 13:42:25 -0800561 loopEnd - loopStart > mFrameCount ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700562 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700563 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
Glenn Kastenb6037442012-11-14 13:42:25 -0800564 "user %d", loopStart, loopEnd, loopCount, mFrameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 return BAD_VALUE;
566 }
567
Glenn Kastenb6037442012-11-14 13:42:25 -0800568 if ((mSharedBuffer != 0) && (loopEnd > mFrameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700569 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
570 "framecount %d",
Glenn Kastenb6037442012-11-14 13:42:25 -0800571 loopStart, loopEnd, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700573 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574
575 cblk->loopStart = loopStart;
576 cblk->loopEnd = loopEnd;
577 cblk->loopCount = loopCount;
578 mLoopCount = loopCount;
579
580 return NO_ERROR;
581}
582
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800583status_t AudioTrack::setMarkerPosition(uint32_t marker)
584{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700585 if (mCbf == NULL) {
586 return INVALID_OPERATION;
587 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588
589 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700590 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591
592 return NO_ERROR;
593}
594
Glenn Kastena5224f32012-01-04 12:41:44 -0800595status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700597 if (marker == NULL) {
598 return BAD_VALUE;
599 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600
601 *marker = mMarkerPosition;
602
603 return NO_ERROR;
604}
605
606status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
607{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700608 if (mCbf == NULL) {
609 return INVALID_OPERATION;
610 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611
612 uint32_t curPosition;
613 getPosition(&curPosition);
614 mNewPosition = curPosition + updatePeriod;
615 mUpdatePeriod = updatePeriod;
616
617 return NO_ERROR;
618}
619
Glenn Kastena5224f32012-01-04 12:41:44 -0800620status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700622 if (updatePeriod == NULL) {
623 return BAD_VALUE;
624 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625
626 *updatePeriod = mUpdatePeriod;
627
628 return NO_ERROR;
629}
630
631status_t AudioTrack::setPosition(uint32_t position)
632{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700633 if (mIsTimed) {
634 return INVALID_OPERATION;
635 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800636
Eric Laurent1703cdf2011-03-07 14:52:59 -0800637 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638
Glenn Kastend65d73c2012-06-22 17:21:07 -0700639 if (!stopped_l()) {
640 return INVALID_OPERATION;
641 }
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800642
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700643 audio_track_cblk_t* cblk = mCblk;
644 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645
Glenn Kastend65d73c2012-06-22 17:21:07 -0700646 if (position > cblk->user) {
647 return BAD_VALUE;
648 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700650 cblk->server = position;
651 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700652
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653 return NO_ERROR;
654}
655
656status_t AudioTrack::getPosition(uint32_t *position)
657{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700658 if (position == NULL) {
659 return BAD_VALUE;
660 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800661 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700662 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663
664 return NO_ERROR;
665}
666
667status_t AudioTrack::reload()
668{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800669 AutoMutex lock(mLock);
670
Glenn Kastend65d73c2012-06-22 17:21:07 -0700671 if (!stopped_l()) {
672 return INVALID_OPERATION;
673 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700674
Eric Laurent1703cdf2011-03-07 14:52:59 -0800675 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700677 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800678 cblk->stepUserOut(mFrameCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679
680 return NO_ERROR;
681}
682
Eric Laurentc2f1f072009-07-17 12:17:14 -0700683audio_io_handle_t AudioTrack::getOutput()
684{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800685 AutoMutex lock(mLock);
686 return getOutput_l();
687}
688
689// must be called with mLock held
690audio_io_handle_t AudioTrack::getOutput_l()
691{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800692 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800693 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694}
695
Eric Laurentbe916aa2010-06-01 23:49:17 -0700696status_t AudioTrack::attachAuxEffect(int effectId)
697{
Steve Block3856b092011-10-20 11:56:00 +0100698 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700699 status_t status = mAudioTrack->attachAuxEffect(effectId);
700 if (status == NO_ERROR) {
701 mAuxEffectId = effectId;
702 }
703 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700704}
705
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706// -------------------------------------------------------------------------
707
Eric Laurent1703cdf2011-03-07 14:52:59 -0800708// must be called with mLock held
709status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800710 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800711 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800712 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800713 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700714 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800715 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700716 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800717{
718 status_t status;
719 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
720 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700721 ALOGE("Could not get audioflinger");
722 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800723 }
724
Eric Laurentd1b449a2010-05-14 03:26:45 -0700725 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700726 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700727 return NO_INIT;
728 }
729
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700730 // Client decides whether the track is TIMED (see below), but can only express a preference
731 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700732 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700733 // either of these use cases:
734 // use case 1: shared buffer
735 (sharedBuffer != 0) ||
736 // use case 2: callback handler
737 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800738 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700739 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700740 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700741 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700742 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700743 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700744
Eric Laurentd1b449a2010-05-14 03:26:45 -0700745 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700746
Dima Zavinfce7a472011-04-19 22:30:36 -0700747 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700748
Eric Laurentd1b449a2010-05-14 03:26:45 -0700749 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700750 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700751 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700752 } else if (frameCount == 0) {
Glenn Kastene33054e2012-11-14 12:54:39 -0800753 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700754 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
755 return NO_INIT;
756 }
757 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700758 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700759
760 } else if (sharedBuffer != 0) {
761
Glenn Kastena42ff002012-11-14 12:47:55 -0800762 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700763 // 8-bit data in shared memory is not currently supported by AudioFlinger
764 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800765 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700766 // More than 2 channels does not require stronger alignment than stereo
767 alignment <<= 1;
768 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800769 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
770 ALOGE("Invalid buffer alignment: address %p, channel count %u",
771 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700772 return BAD_VALUE;
773 }
774
775 // When initializing a shared buffer AudioTrack via constructors,
776 // there's no frameCount parameter.
777 // But when initializing a shared buffer AudioTrack via set(),
778 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800779 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700780
781 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
782
783 // FIXME move these calculations and associated checks to server
Glenn Kasten3b16c762012-11-14 08:44:39 -0800784 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700785 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
786 return NO_INIT;
787 }
Glenn Kastene33054e2012-11-14 12:54:39 -0800788 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700789 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
790 return NO_INIT;
791 }
792
Eric Laurentd1b449a2010-05-14 03:26:45 -0700793 // Ensure that buffer depth covers at least audio hardware latency
794 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
795 if (minBufCount < 2) minBufCount = 2;
796
Glenn Kastene33054e2012-11-14 12:54:39 -0800797 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
798 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800799 ", afLatency=%d",
800 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700801
802 if (frameCount == 0) {
803 frameCount = minFrameCount;
804 }
805 if (mNotificationFramesAct == 0) {
806 mNotificationFramesAct = frameCount/2;
807 }
808 // Make sure that application is notified with sufficient margin
809 // before underrun
Glenn Kastene33054e2012-11-14 12:54:39 -0800810 if (mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700811 mNotificationFramesAct = frameCount/2;
812 }
813 if (frameCount < minFrameCount) {
814 // not ALOGW because it happens all the time when playing key clicks over A2DP
815 ALOGV("Minimum buffer size corrected from %d to %d",
816 frameCount, minFrameCount);
817 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800818 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700819
Glenn Kastene0fa4672012-04-24 14:35:14 -0700820 } else {
821 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700822 }
823
Glenn Kastena075db42012-03-06 11:22:44 -0800824 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
825 if (mIsTimed) {
826 trackFlags |= IAudioFlinger::TRACK_TIMED;
827 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800828
829 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700830 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700831 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800832 if (mAudioTrackThread != 0) {
833 tid = mAudioTrackThread->getTid();
834 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700835 }
836
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800837 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
838 streamType,
839 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700840 // AudioFlinger only sees 16-bit PCM
841 format == AUDIO_FORMAT_PCM_8_BIT ?
842 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800843 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800844 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800845 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800846 sharedBuffer,
847 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800848 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700849 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800850 &status);
851
852 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000853 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800854 return status;
855 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700856 sp<IMemory> iMem = track->getCblk();
857 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000858 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800859 return NO_INIT;
860 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800861 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700862 mCblkMemory = iMem;
863 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
864 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800865 size_t temp = cblk->frameCount_;
866 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
867 // In current design, AudioTrack client checks and ensures frame count validity before
868 // passing it to AudioFlinger so AudioFlinger should not return a different value except
869 // for fast track as it uses a special method of assigning frame count.
870 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
871 }
872 frameCount = temp;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800873 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800874 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800875 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800876 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -0800877 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700878 // once denied, do not request again if IAudioTrack is re-created
879 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
880 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800881 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700882 if (sharedBuffer == 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800883 mNotificationFramesAct = frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700884 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800885 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800886 if (sharedBuffer == 0) {
Glenn Kastenb929e412012-11-08 12:13:58 -0800887 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800888 } else {
Glenn Kastenb929e412012-11-08 12:13:58 -0800889 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700890 // Force buffer full condition as data is already present in shared memory
Glenn Kastenb6037442012-11-14 13:42:25 -0800891 cblk->stepUserOut(frameCount, frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800892 }
893
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700894 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700895 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700896 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700897 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700898 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
899 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700900 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700901 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -0800902 mLatency = afLatency + (1000*frameCount) / sampleRate;
903 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700904 // If IAudioTrack is re-created, don't let the requested frameCount
905 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -0800906 if (frameCount > mReqFrameCount) {
907 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700908 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800909 return NO_ERROR;
910}
911
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800912status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
913{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800914 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800915 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700916 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800917 audio_track_cblk_t* cblk = mCblk;
918 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700919 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800920
921 audioBuffer->frameCount = 0;
922 audioBuffer->size = 0;
923
Glenn Kastenb6037442012-11-14 13:42:25 -0800924 uint32_t framesAvail = cblk->framesAvailableOut(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925
Eric Laurent9b7d9502011-03-21 11:49:00 -0700926 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800927 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700928 goto create_new_track;
929 }
930 cblk->lock.unlock();
931
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800932 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800933 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934 goto start_loop_here;
935 while (framesAvail == 0) {
936 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800937 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100938 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800939 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800940 return NO_MORE_BUFFERS;
941 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800942 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800943 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800945 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800946 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800947 mLock.unlock();
Glenn Kastena47f3162012-11-07 10:13:08 -0800948 // this condition is in shared memory, so if IAudioTrack and control block
949 // are replaced due to mediaserver death or IAudioTrack invalidation then
950 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700951 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700952 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800953 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800954 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800955 return status_t(STOPPED);
956 }
Glenn Kastena47f3162012-11-07 10:13:08 -0800957 // IAudioTrack may have been re-created while mLock was unlocked
958 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800959 cblk->lock.lock();
960 }
961
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800962 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700963 goto create_new_track;
964 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800965 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -0700966 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
968 // timing out when a loop has been set and we have already written upto loop end
969 // is a normal condition: no need to wake AudioFlinger up.
970 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700971 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
972 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700973 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800975 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800977 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800978 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800979create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700980 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800981 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700982 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800983 }
984 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000985 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800986 cblk->lock.unlock();
987 return result;
988 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800989 }
990 cblk->waitTimeMs = 0;
991 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700992
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800993 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800994 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800995 return TIMED_OUT;
996 }
997 }
998 // read the server count again
999 start_loop_here:
Glenn Kastenb6037442012-11-14 13:42:25 -08001000 framesAvail = cblk->framesAvailableOut_l(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001001 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001002 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003 }
1004
1005 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001006
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 if (framesReq > framesAvail) {
1008 framesReq = framesAvail;
1009 }
1010
1011 uint32_t u = cblk->user;
Glenn Kastenb6037442012-11-14 13:42:25 -08001012 uint32_t bufferEnd = cblk->userBase + mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013
Marco Nelissena1472d92012-03-30 14:36:54 -07001014 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001015 framesReq = bufferEnd - u;
1016 }
1017
Eric Laurentc2f1f072009-07-17 12:17:14 -07001018 audioBuffer->frameCount = framesReq;
Glenn Kasten83a03822012-11-12 07:58:20 -08001019 audioBuffer->size = framesReq * mFrameSizeAF;
1020 audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 active = mActive;
1022 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1023}
1024
1025void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1026{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001027 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001028 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001029 cblk->stepUserOut(audioBuffer->frameCount, mFrameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001030 if (audioBuffer->frameCount > 0) {
1031 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001032 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1033 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1034 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001035 mAudioTrack->start();
1036 }
1037 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038}
1039
1040// -------------------------------------------------------------------------
1041
1042ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1043{
1044
Glenn Kastend65d73c2012-06-22 17:21:07 -07001045 if (mSharedBuffer != 0) {
1046 return INVALID_OPERATION;
1047 }
1048 if (mIsTimed) {
1049 return INVALID_OPERATION;
1050 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001051
1052 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001053 // Sanity-check: user is most-likely passing an error code, and it would
1054 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001055 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001056 buffer, userSize, userSize);
1057 return BAD_VALUE;
1058 }
1059
Steve Block3856b092011-10-20 11:56:00 +01001060 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001061
Eric Laurentdf839842012-05-31 14:27:14 -07001062 if (userSize == 0) {
1063 return 0;
1064 }
1065
Eric Laurent1703cdf2011-03-07 14:52:59 -08001066 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1067 // while we are accessing the cblk
1068 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001069 sp<IAudioTrack> audioTrack = mAudioTrack;
1070 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001071 mLock.unlock();
1072
Glenn Kastena47f3162012-11-07 10:13:08 -08001073 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1074 // so all cblk references might still refer to old shared memory, but that should be benign
1075
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001076 ssize_t written = 0;
1077 const int8_t *src = (const int8_t *)buffer;
1078 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001079 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001080
1081 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001082 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001083
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001084 status_t err = obtainBuffer(&audioBuffer, -1);
1085 if (err < 0) {
1086 // out of buffers, return #bytes written
Glenn Kastend65d73c2012-06-22 17:21:07 -07001087 if (err == status_t(NO_MORE_BUFFERS)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001088 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001089 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001090 return ssize_t(err);
1091 }
1092
1093 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001094
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001095 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001096 // Divide capacity by 2 to take expansion into account
1097 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001098 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001099 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001100 toWrite = audioBuffer.size;
1101 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001102 }
Glenn Kastenbc0f6b92012-11-12 14:32:06 -08001103 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104 userSize -= toWrite;
1105 written += toWrite;
1106
1107 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001108 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001109
1110 return written;
1111}
1112
1113// -------------------------------------------------------------------------
1114
John Grossman4ff14ba2012-02-08 16:37:41 -08001115TimedAudioTrack::TimedAudioTrack() {
1116 mIsTimed = true;
1117}
1118
1119status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1120{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001121 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001122 status_t result = UNKNOWN_ERROR;
1123
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001124 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1125 // while we are accessing the cblk
1126 sp<IAudioTrack> audioTrack = mAudioTrack;
1127 sp<IMemory> iMem = mCblkMemory;
1128
John Grossman4ff14ba2012-02-08 16:37:41 -08001129 // If the track is not invalid already, try to allocate a buffer. alloc
1130 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001131 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001132 audio_track_cblk_t* cblk = mCblk;
1133 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001134 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1135 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001136 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001137 }
1138 }
1139
1140 // If the track is invalid at this point, attempt to restore it. and try the
1141 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001142 if (cblk->flags & CBLK_INVALID) {
1143 cblk->lock.lock();
1144 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001145 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001146 cblk = temp;
1147 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001148
Glenn Kastend65d73c2012-06-22 17:21:07 -07001149 if (result == OK) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001150 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001151 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001152 }
1153
1154 return result;
1155}
1156
1157status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1158 int64_t pts)
1159{
Eric Laurentdf839842012-05-31 14:27:14 -07001160 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1161 {
1162 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001163 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001164 // restart track if it was disabled by audioflinger due to previous underrun
1165 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001166 mActive && (cblk->flags & CBLK_DISABLED)) {
1167 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001168 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1169 mAudioTrack->start();
1170 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001171 }
Eric Laurentdf839842012-05-31 14:27:14 -07001172 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001173}
1174
1175status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1176 TargetTimeline target)
1177{
1178 return mAudioTrack->setMediaTimeTransform(xform, target);
1179}
1180
1181// -------------------------------------------------------------------------
1182
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001183bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1184{
1185 Buffer audioBuffer;
1186 uint32_t frames;
1187 size_t writtenSize;
1188
Eric Laurent1703cdf2011-03-07 14:52:59 -08001189 mLock.lock();
1190 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1191 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001192 sp<IAudioTrack> audioTrack = mAudioTrack;
1193 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001194 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001195 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001196 mLock.unlock();
1197
Glenn Kastena47f3162012-11-07 10:13:08 -08001198 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1199 // so all cblk references might still refer to old shared memory, but that should be benign
1200
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 // Manage underrun callback
Glenn Kastenb6037442012-11-14 13:42:25 -08001202 if (active && (cblk->framesAvailableOut(mFrameCount) == mFrameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001204 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 mCbf(EVENT_UNDERRUN, mUserData, 0);
Glenn Kastenb6037442012-11-14 13:42:25 -08001206 if (cblk->server == mFrameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001207 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001209 if (mSharedBuffer != 0) {
1210 return false;
1211 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001212 }
1213 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001214
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001216 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217 int loopCount = -1;
1218 mLoopCount--;
1219 if (mLoopCount >= 0) loopCount = mLoopCount;
1220
1221 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1222 }
1223
1224 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001225 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001226 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001228 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229 }
1230 }
1231
1232 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001233 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001234 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1236 mNewPosition += mUpdatePeriod;
1237 }
1238 }
1239
1240 // If Shared buffer is used, no data is requested from client.
1241 if (mSharedBuffer != 0) {
1242 frames = 0;
1243 } else {
1244 frames = mRemainingFrames;
1245 }
1246
Glenn Kasten99e53b82012-01-19 08:59:58 -08001247 // See description of waitCount parameter at declaration of obtainBuffer().
1248 // The logic below prevents us from being stuck below at obtainBuffer()
1249 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001250 int32_t waitCount = -1;
1251 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1252 waitCount = 1;
1253 }
1254
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 do {
1256
1257 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001258
Eric Laurent2267ba12011-09-07 11:13:23 -07001259 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 if (err < NO_ERROR) {
1261 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001262 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1263 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 return false;
1265 }
1266 break;
1267 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001268 if (err == status_t(STOPPED)) {
1269 return false;
1270 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271
1272 // Divide buffer size by 2 to take into account the expansion
1273 // due to 8 to 16 bit conversion: the callback must fill only half
1274 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001275 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276 audioBuffer.size >>= 1;
1277 }
1278
1279 size_t reqSize = audioBuffer.size;
1280 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1281 writtenSize = audioBuffer.size;
1282
1283 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001284 if (ssize_t(writtenSize) <= 0) {
1285 // The callback is done filling buffers
1286 // Keep this thread going to handle timed events and
1287 // still try to get more data in intervals of WAIT_PERIOD_MS
1288 // but don't just loop and block the CPU, so wait
1289 usleep(WAIT_PERIOD_MS*1000);
1290 break;
1291 }
Eric Laurentdf839842012-05-31 14:27:14 -07001292
Glenn Kastend65d73c2012-06-22 17:21:07 -07001293 if (writtenSize > reqSize) {
1294 writtenSize = reqSize;
1295 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001296
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001297 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001298 // 8 to 16 bit conversion, note that source and destination are the same address
1299 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300 writtenSize <<= 1;
1301 }
1302
1303 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001304 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1305 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001306 // 16 bit.
Glenn Kasten83a03822012-11-12 07:58:20 -08001307 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001308
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001309 frames -= audioBuffer.frameCount;
1310
1311 releaseBuffer(&audioBuffer);
1312 }
1313 while (frames);
1314
1315 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001316 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001317 } else {
1318 mRemainingFrames = frames;
1319 }
1320 return true;
1321}
1322
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001323// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001324// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001325// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1326// FIXME Don't depend on caller to hold strong references.
1327status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001328{
1329 status_t result;
1330
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001331 audio_track_cblk_t* cblk = refCblk;
1332 audio_track_cblk_t* newCblk = cblk;
Glenn Kasten827e5f12012-11-02 10:00:06 -07001333 ALOGW("dead IAudioTrack, creating a new one from %s",
1334 fromStart ? "start()" : "obtainBuffer()");
Eric Laurent1703cdf2011-03-07 14:52:59 -08001335
Glenn Kastena47f3162012-11-07 10:13:08 -08001336 // signal old cblk condition so that other threads waiting for available buffers stop
1337 // waiting now
1338 cblk->cv.broadcast();
1339 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001340
Glenn Kastena47f3162012-11-07 10:13:08 -08001341 // refresh the audio configuration cache in this process to make sure we get new
1342 // output parameters in getOutput_l() and createTrack_l()
1343 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001344
Glenn Kastena47f3162012-11-07 10:13:08 -08001345 // if the new IAudioTrack is created, createTrack_l() will modify the
1346 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1347 // It will also delete the strong references on previous IAudioTrack and IMemory
1348 result = createTrack_l(mStreamType,
1349 cblk->sampleRate,
1350 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001351 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001352 mFlags,
1353 mSharedBuffer,
1354 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001355
Glenn Kastena47f3162012-11-07 10:13:08 -08001356 if (result == NO_ERROR) {
1357 uint32_t user = cblk->user;
1358 uint32_t server = cblk->server;
1359 // restore write index and set other indexes to reflect empty buffer status
1360 newCblk = mCblk;
1361 newCblk->user = user;
1362 newCblk->server = user;
1363 newCblk->userBase = user;
1364 newCblk->serverBase = user;
1365 // restore loop: this is not guaranteed to succeed if new frame count is not
1366 // compatible with loop length
1367 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1368 if (!fromStart) {
1369 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1370 // Make sure that a client relying on callback events indicating underrun or
1371 // the actual amount of audio frames played (e.g SoundPool) receives them.
1372 if (mSharedBuffer == 0) {
1373 uint32_t frames = 0;
1374 if (user > server) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001375 frames = ((user - server) > mFrameCount) ?
1376 mFrameCount : (user - server);
Glenn Kasten83a03822012-11-12 07:58:20 -08001377 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001378 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001379 // restart playback even if buffer is not completely filled.
1380 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1381 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1382 // the client
Glenn Kastenb6037442012-11-14 13:42:25 -08001383 newCblk->stepUserOut(frames, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001384 }
1385 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001386 if (mSharedBuffer != 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001387 newCblk->stepUserOut(mFrameCount, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001388 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001389 if (mActive) {
1390 result = mAudioTrack->start();
1391 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1392 }
1393 if (fromStart && result == NO_ERROR) {
1394 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001395 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001396 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001397 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001398 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001399 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001400
1401 if (result == NO_ERROR) {
1402 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001403 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001404 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001405 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001406
Glenn Kasten827e5f12012-11-02 10:00:06 -07001407 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001408
1409 return result;
1410}
1411
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001412status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1413{
1414
1415 const size_t SIZE = 256;
1416 char buffer[SIZE];
1417 String8 result;
1418
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001419 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001421 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1422 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001423 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001424 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001425 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426 result.append(buffer);
Glenn Kasten3b16c762012-11-14 08:44:39 -08001427 snprintf(buffer, 255, " sample rate(%u), status(%d), muted(%d)\n",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001428 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001429 result.append(buffer);
1430 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1431 result.append(buffer);
1432 ::write(fd, result.string(), result.size());
1433 return NO_ERROR;
1434}
1435
1436// =========================================================================
1437
1438AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001439 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1440{
1441}
1442
1443AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444{
1445}
1446
1447bool AudioTrack::AudioTrackThread::threadLoop()
1448{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001449 {
1450 AutoMutex _l(mMyLock);
1451 if (mPaused) {
1452 mMyCond.wait(mMyLock);
1453 // caller will check for exitPending()
1454 return true;
1455 }
1456 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001457 if (!mReceiver.processAudioBuffer(this)) {
1458 pause();
1459 }
1460 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461}
1462
Glenn Kasten3acbd052012-02-28 10:39:56 -08001463void AudioTrack::AudioTrackThread::requestExit()
1464{
1465 // must be in this order to avoid a race condition
1466 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001467 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001468}
1469
1470void AudioTrack::AudioTrackThread::pause()
1471{
1472 AutoMutex _l(mMyLock);
1473 mPaused = true;
1474}
1475
1476void AudioTrack::AudioTrackThread::resume()
1477{
1478 AutoMutex _l(mMyLock);
1479 if (mPaused) {
1480 mPaused = false;
1481 mMyCond.signal();
1482 }
1483}
1484
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001485// =========================================================================
1486
Eric Laurent38ccae22011-03-28 18:37:07 -07001487
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001488audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001489 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastenb6037442012-11-14 13:42:25 -08001490 userBase(0), serverBase(0), frameCount_(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001491 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001492 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001493{
1494}
1495
Glenn Kastenb6037442012-11-14 13:42:25 -08001496uint32_t audio_track_cblk_t::stepUser(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001497{
Glenn Kastenb6037442012-11-14 13:42:25 -08001498 ALOGV("stepuser %08x %08x %d", user, server, stepCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001499
Marco Nelissena1472d92012-03-30 14:36:54 -07001500 uint32_t u = user;
Glenn Kastenb6037442012-11-14 13:42:25 -08001501 u += stepCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten864585d2012-11-06 16:15:41 -08001503 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001504 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1505 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1506 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1507 }
Glenn Kasten90548972011-12-13 11:45:07 -08001508 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001509 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001510 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001511 }
1512
Glenn Kastenb6037442012-11-14 13:42:25 -08001513 if (u >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001514 // common case, user didn't just wrap
Glenn Kastenb6037442012-11-14 13:42:25 -08001515 if (u - frameCount >= userBase ) {
1516 userBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001517 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001518 } else if (u >= userBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001519 // user just wrapped
Glenn Kastenb6037442012-11-14 13:42:25 -08001520 userBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001521 }
1522
Glenn Kasten90548972011-12-13 11:45:07 -08001523 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524
1525 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001526 if (flags & CBLK_UNDERRUN) {
1527 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001528 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001529
1530 return u;
1531}
1532
Glenn Kastenb6037442012-11-14 13:42:25 -08001533bool audio_track_cblk_t::stepServer(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001534{
Glenn Kastenb6037442012-11-14 13:42:25 -08001535 ALOGV("stepserver %08x %08x %d", user, server, stepCount);
Marco Nelissena1472d92012-03-30 14:36:54 -07001536
Eric Laurent38ccae22011-03-28 18:37:07 -07001537 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001538 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001539 return false;
1540 }
1541
Glenn Kasten90548972011-12-13 11:45:07 -08001542 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001543 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001544
Glenn Kastenb6037442012-11-14 13:42:25 -08001545 s += stepCount;
Glenn Kasten864585d2012-11-06 16:15:41 -08001546 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547 // Mark that we have read the first buffer so that next time stepUser() is called
1548 // we switch to normal obtainBuffer() timeout period
1549 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001550 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001551 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552 // It is possible that we receive a flush()
1553 // while the mixer is processing a block: in this case,
1554 // stepServer() is called After the flush() has reset u & s and
1555 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001556 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001557 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001558 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559 }
1560 }
1561
1562 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001563 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001564 s = loopStart;
1565 if (--loopCount == 0) {
1566 loopEnd = UINT_MAX;
1567 loopStart = UINT_MAX;
1568 }
1569 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001570
Glenn Kastenb6037442012-11-14 13:42:25 -08001571 if (s >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001572 // common case, server didn't just wrap
Glenn Kastenb6037442012-11-14 13:42:25 -08001573 if (s - frameCount >= serverBase ) {
1574 serverBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001575 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001576 } else if (s >= serverBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001577 // server just wrapped
Glenn Kastenb6037442012-11-14 13:42:25 -08001578 serverBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001579 }
1580
Glenn Kasten90548972011-12-13 11:45:07 -08001581 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001583 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001584 cv.signal();
1585 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001586 lock.unlock();
1587 return true;
1588}
1589
Glenn Kasten83a03822012-11-12 07:58:20 -08001590void* audio_track_cblk_t::buffer(void *buffers, size_t frameSize, uint32_t offset) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001591{
Glenn Kasten90548972011-12-13 11:45:07 -08001592 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001593}
1594
Glenn Kastenb6037442012-11-14 13:42:25 -08001595uint32_t audio_track_cblk_t::framesAvailable(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596{
1597 Mutex::Autolock _l(lock);
Glenn Kastenb6037442012-11-14 13:42:25 -08001598 return framesAvailable_l(frameCount, isOut);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599}
1600
Glenn Kastenb6037442012-11-14 13:42:25 -08001601uint32_t audio_track_cblk_t::framesAvailable_l(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001602{
Glenn Kasten90548972011-12-13 11:45:07 -08001603 uint32_t u = user;
1604 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605
Glenn Kasten864585d2012-11-06 16:15:41 -08001606 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001607 uint32_t limit = (s < loopStart) ? s : loopStart;
1608 return limit + frameCount - u;
1609 } else {
1610 return frameCount + u - s;
1611 }
1612}
1613
Glenn Kasten864585d2012-11-06 16:15:41 -08001614uint32_t audio_track_cblk_t::framesReady(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001615{
Glenn Kasten90548972011-12-13 11:45:07 -08001616 uint32_t u = user;
1617 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001618
Glenn Kasten864585d2012-11-06 16:15:41 -08001619 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001620 if (u < loopEnd) {
1621 return u - s;
1622 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001623 // do not block on mutex shared with client on AudioFlinger side
1624 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001625 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001626 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001627 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001628 uint32_t frames = UINT_MAX;
1629 if (loopCount >= 0) {
1630 frames = (loopEnd - loopStart)*loopCount + u - s;
1631 }
1632 lock.unlock();
1633 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001634 }
1635 } else {
1636 return s - u;
1637 }
1638}
1639
Eric Laurent38ccae22011-03-28 18:37:07 -07001640bool audio_track_cblk_t::tryLock()
1641{
1642 // the code below simulates lock-with-timeout
1643 // we MUST do this to protect the AudioFlinger server
1644 // as this lock is shared with the client.
1645 status_t err;
1646
1647 err = lock.tryLock();
1648 if (err == -EBUSY) { // just wait a bit
1649 usleep(1000);
1650 err = lock.tryLock();
1651 }
1652 if (err != NO_ERROR) {
1653 // probably, the client just died.
1654 return false;
1655 }
1656 return true;
1657}
1658
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001659// -------------------------------------------------------------------------
1660
1661}; // namespace android