blob: e40895a16b64886a01e6fc9e4bad191a5858c1c0 [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
320status_t AudioTrack::initCheck() const
321{
322 return mStatus;
323}
324
325// -------------------------------------------------------------------------
326
327uint32_t AudioTrack::latency() const
328{
329 return mLatency;
330}
331
Glenn Kastenfff6d712012-01-12 16:38:12 -0800332audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800333{
334 return mStreamType;
335}
336
Glenn Kastene1c39622012-01-04 09:36:37 -0800337audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338{
339 return mFormat;
340}
341
Glenn Kastena42ff002012-11-14 12:47:55 -0800342uint32_t AudioTrack::channelCount() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343{
344 return mChannelCount;
345}
346
Glenn Kastene33054e2012-11-14 12:54:39 -0800347size_t AudioTrack::frameCount() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348{
Glenn Kastenb6037442012-11-14 13:42:25 -0800349 return mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350}
351
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352sp<IMemory>& AudioTrack::sharedBuffer()
353{
354 return mSharedBuffer;
355}
356
357// -------------------------------------------------------------------------
358
359void AudioTrack::start()
360{
361 sp<AudioTrackThread> t = mAudioTrackThread;
362
Steve Block3856b092011-10-20 11:56:00 +0100363 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364
Eric Laurentf5aafb22010-11-18 08:40:16 -0800365 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800366 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
367 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700368 sp<IAudioTrack> audioTrack = mAudioTrack;
369 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800370 audio_track_cblk_t* cblk = mCblk;
371
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800372 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700373 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800374 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800375 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700376 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800377 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
378 cblk->waitTimeMs = 0;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800379 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800380 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800381 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800382 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700383 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700384 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700385 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800386 }
387
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700388 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700389 status_t status = NO_ERROR;
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800390 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800391 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800392 ALOGV("mAudioTrack->start()");
393 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800394 cblk->lock.lock();
395 if (status == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800396 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800397 }
Eric Laurent2b584242009-11-09 23:32:22 -0800398 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800399 if (cblk->flags & CBLK_INVALID) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700400 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800401 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700402 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800403 }
404 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800405 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100406 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800407 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800408 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800409 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800410 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700411 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700412 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800413 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800414 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 }
416
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417}
418
419void AudioTrack::stop()
420{
421 sp<AudioTrackThread> t = mAudioTrackThread;
422
Steve Block3856b092011-10-20 11:56:00 +0100423 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424
Eric Laurentf5aafb22010-11-18 08:40:16 -0800425 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800426 if (mActive) {
427 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700428 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 mAudioTrack->stop();
430 // Cancel loops (If we are in the middle of a loop, playback
431 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800432 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700433 // the playback head position will reset to 0, so if a marker is set, we need
434 // to activate it again
435 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 // Force flush if a shared buffer is used otherwise audioflinger
437 // will not stop before end of buffer is reached.
438 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800439 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440 }
441 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800442 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700444 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700445 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446 }
447 }
448
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449}
450
451bool AudioTrack::stopped() const
452{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800453 AutoMutex lock(mLock);
454 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455}
456
457void AudioTrack::flush()
458{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800459 AutoMutex lock(mLock);
460 flush_l();
461}
462
463// must be called with mLock held
464void AudioTrack::flush_l()
465{
Steve Block3856b092011-10-20 11:56:00 +0100466 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700467
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700468 // clear playback marker and periodic update counter
469 mMarkerPosition = 0;
470 mMarkerReached = false;
471 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700472
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700474 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475 mAudioTrack->flush();
476 // Release AudioTrack callback thread in case it was waiting for new buffers
477 // in AudioTrack::obtainBuffer()
478 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 }
480}
481
482void AudioTrack::pause()
483{
Steve Block3856b092011-10-20 11:56:00 +0100484 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800485 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800486 if (mActive) {
487 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700488 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489 mAudioTrack->pause();
490 }
491}
492
493void AudioTrack::mute(bool e)
494{
495 mAudioTrack->mute(e);
496 mMuted = e;
497}
498
499bool AudioTrack::muted() const
500{
501 return mMuted;
502}
503
Eric Laurentbe916aa2010-06-01 23:49:17 -0700504status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800506 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700507 return BAD_VALUE;
508 }
509
Eric Laurent1703cdf2011-03-07 14:52:59 -0800510 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 mVolume[LEFT] = left;
512 mVolume[RIGHT] = right;
513
Glenn Kasten83d86532012-01-17 14:39:34 -0800514 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700515
516 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800517}
518
Glenn Kastenb1c09932012-02-27 16:21:04 -0800519status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800521 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700522}
523
Eric Laurent2beeb502010-07-16 07:43:46 -0700524status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700525{
Steve Block3856b092011-10-20 11:56:00 +0100526 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800527 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700528 return BAD_VALUE;
529 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800530 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700531
532 mSendLevel = level;
533
Glenn Kasten05632a52012-01-03 14:22:33 -0800534 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700535
536 return NO_ERROR;
537}
538
Glenn Kastena5224f32012-01-04 12:41:44 -0800539void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700540{
541 if (level != NULL) {
542 *level = mSendLevel;
543 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544}
545
Glenn Kasten3b16c762012-11-14 08:44:39 -0800546status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547{
Glenn Kasten3b16c762012-11-14 08:44:39 -0800548 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549
John Grossman4ff14ba2012-02-08 16:37:41 -0800550 if (mIsTimed) {
551 return INVALID_OPERATION;
552 }
553
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700555 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 }
557 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700558 if (rate == 0 || rate > afSamplingRate*2 ) {
559 return BAD_VALUE;
560 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561
Eric Laurent1703cdf2011-03-07 14:52:59 -0800562 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700563 mCblk->sampleRate = rate;
564 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565}
566
Glenn Kastena5224f32012-01-04 12:41:44 -0800567uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568{
John Grossman4ff14ba2012-02-08 16:37:41 -0800569 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800570 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800571 }
572
Eric Laurent1703cdf2011-03-07 14:52:59 -0800573 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700574 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575}
576
577status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
578{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800579 AutoMutex lock(mLock);
580 return setLoop_l(loopStart, loopEnd, loopCount);
581}
582
583// must be called with mLock held
584status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
585{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 audio_track_cblk_t* cblk = mCblk;
587
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800588 Mutex::Autolock _l(cblk->lock);
589
590 if (loopCount == 0) {
591 cblk->loopStart = UINT_MAX;
592 cblk->loopEnd = UINT_MAX;
593 cblk->loopCount = 0;
594 mLoopCount = 0;
595 return NO_ERROR;
596 }
597
John Grossman4ff14ba2012-02-08 16:37:41 -0800598 if (mIsTimed) {
599 return INVALID_OPERATION;
600 }
601
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800602 if (loopStart >= loopEnd ||
Glenn Kastenb6037442012-11-14 13:42:25 -0800603 loopEnd - loopStart > mFrameCount ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700604 cblk->server > loopStart) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700605 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
Glenn Kastenb6037442012-11-14 13:42:25 -0800606 "user %d", loopStart, loopEnd, loopCount, mFrameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607 return BAD_VALUE;
608 }
609
Glenn Kastenb6037442012-11-14 13:42:25 -0800610 if ((mSharedBuffer != 0) && (loopEnd > mFrameCount)) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700611 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
612 "framecount %d",
Glenn Kastenb6037442012-11-14 13:42:25 -0800613 loopStart, loopEnd, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800614 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700615 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800616
617 cblk->loopStart = loopStart;
618 cblk->loopEnd = loopEnd;
619 cblk->loopCount = loopCount;
620 mLoopCount = loopCount;
621
622 return NO_ERROR;
623}
624
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625status_t AudioTrack::setMarkerPosition(uint32_t marker)
626{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700627 if (mCbf == NULL) {
628 return INVALID_OPERATION;
629 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630
631 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700632 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633
634 return NO_ERROR;
635}
636
Glenn Kastena5224f32012-01-04 12:41:44 -0800637status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700639 if (marker == NULL) {
640 return BAD_VALUE;
641 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642
643 *marker = mMarkerPosition;
644
645 return NO_ERROR;
646}
647
648status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
649{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700650 if (mCbf == NULL) {
651 return INVALID_OPERATION;
652 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653
654 uint32_t curPosition;
655 getPosition(&curPosition);
656 mNewPosition = curPosition + updatePeriod;
657 mUpdatePeriod = updatePeriod;
658
659 return NO_ERROR;
660}
661
Glenn Kastena5224f32012-01-04 12:41:44 -0800662status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700664 if (updatePeriod == NULL) {
665 return BAD_VALUE;
666 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667
668 *updatePeriod = mUpdatePeriod;
669
670 return NO_ERROR;
671}
672
673status_t AudioTrack::setPosition(uint32_t position)
674{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700675 if (mIsTimed) {
676 return INVALID_OPERATION;
677 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800678
Eric Laurent1703cdf2011-03-07 14:52:59 -0800679 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800680
Glenn Kastend65d73c2012-06-22 17:21:07 -0700681 if (!stopped_l()) {
682 return INVALID_OPERATION;
683 }
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800684
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700685 audio_track_cblk_t* cblk = mCblk;
686 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687
Glenn Kastend65d73c2012-06-22 17:21:07 -0700688 if (position > cblk->user) {
689 return BAD_VALUE;
690 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700692 cblk->server = position;
693 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700694
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695 return NO_ERROR;
696}
697
698status_t AudioTrack::getPosition(uint32_t *position)
699{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700700 if (position == NULL) {
701 return BAD_VALUE;
702 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800703 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700704 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705
706 return NO_ERROR;
707}
708
709status_t AudioTrack::reload()
710{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800711 AutoMutex lock(mLock);
712
Glenn Kastend65d73c2012-06-22 17:21:07 -0700713 if (!stopped_l()) {
714 return INVALID_OPERATION;
715 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700716
Eric Laurent1703cdf2011-03-07 14:52:59 -0800717 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800718
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700719 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800720 cblk->stepUserOut(mFrameCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800721
722 return NO_ERROR;
723}
724
Eric Laurentc2f1f072009-07-17 12:17:14 -0700725audio_io_handle_t AudioTrack::getOutput()
726{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800727 AutoMutex lock(mLock);
728 return getOutput_l();
729}
730
731// must be called with mLock held
732audio_io_handle_t AudioTrack::getOutput_l()
733{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800734 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800735 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700736}
737
Glenn Kastena5224f32012-01-04 12:41:44 -0800738int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700739{
740 return mSessionId;
741}
742
743status_t AudioTrack::attachAuxEffect(int effectId)
744{
Steve Block3856b092011-10-20 11:56:00 +0100745 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700746 status_t status = mAudioTrack->attachAuxEffect(effectId);
747 if (status == NO_ERROR) {
748 mAuxEffectId = effectId;
749 }
750 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700751}
752
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800753// -------------------------------------------------------------------------
754
Eric Laurent1703cdf2011-03-07 14:52:59 -0800755// must be called with mLock held
756status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800757 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800758 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800759 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800760 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700761 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800762 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700763 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800764{
765 status_t status;
766 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
767 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700768 ALOGE("Could not get audioflinger");
769 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800770 }
771
Eric Laurentd1b449a2010-05-14 03:26:45 -0700772 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700773 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700774 return NO_INIT;
775 }
776
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700777 // Client decides whether the track is TIMED (see below), but can only express a preference
778 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700779 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700780 // either of these use cases:
781 // use case 1: shared buffer
782 (sharedBuffer != 0) ||
783 // use case 2: callback handler
784 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800785 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700786 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700787 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700788 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700789 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700790 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700791
Eric Laurentd1b449a2010-05-14 03:26:45 -0700792 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700793
Dima Zavinfce7a472011-04-19 22:30:36 -0700794 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700795
Eric Laurentd1b449a2010-05-14 03:26:45 -0700796 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700797 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700798 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700799 } else if (frameCount == 0) {
Glenn Kastene33054e2012-11-14 12:54:39 -0800800 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700801 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
802 return NO_INIT;
803 }
804 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700805 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700806
807 } else if (sharedBuffer != 0) {
808
Glenn Kastena42ff002012-11-14 12:47:55 -0800809 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700810 // 8-bit data in shared memory is not currently supported by AudioFlinger
811 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800812 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700813 // More than 2 channels does not require stronger alignment than stereo
814 alignment <<= 1;
815 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800816 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
817 ALOGE("Invalid buffer alignment: address %p, channel count %u",
818 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700819 return BAD_VALUE;
820 }
821
822 // When initializing a shared buffer AudioTrack via constructors,
823 // there's no frameCount parameter.
824 // But when initializing a shared buffer AudioTrack via set(),
825 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800826 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700827
828 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
829
830 // FIXME move these calculations and associated checks to server
Glenn Kasten3b16c762012-11-14 08:44:39 -0800831 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700832 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
833 return NO_INIT;
834 }
Glenn Kastene33054e2012-11-14 12:54:39 -0800835 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700836 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
837 return NO_INIT;
838 }
839
Eric Laurentd1b449a2010-05-14 03:26:45 -0700840 // Ensure that buffer depth covers at least audio hardware latency
841 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
842 if (minBufCount < 2) minBufCount = 2;
843
Glenn Kastene33054e2012-11-14 12:54:39 -0800844 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
845 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800846 ", afLatency=%d",
847 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700848
849 if (frameCount == 0) {
850 frameCount = minFrameCount;
851 }
852 if (mNotificationFramesAct == 0) {
853 mNotificationFramesAct = frameCount/2;
854 }
855 // Make sure that application is notified with sufficient margin
856 // before underrun
Glenn Kastene33054e2012-11-14 12:54:39 -0800857 if (mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700858 mNotificationFramesAct = frameCount/2;
859 }
860 if (frameCount < minFrameCount) {
861 // not ALOGW because it happens all the time when playing key clicks over A2DP
862 ALOGV("Minimum buffer size corrected from %d to %d",
863 frameCount, minFrameCount);
864 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800865 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700866
Glenn Kastene0fa4672012-04-24 14:35:14 -0700867 } else {
868 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700869 }
870
Glenn Kastena075db42012-03-06 11:22:44 -0800871 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
872 if (mIsTimed) {
873 trackFlags |= IAudioFlinger::TRACK_TIMED;
874 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800875
876 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700877 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700878 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800879 if (mAudioTrackThread != 0) {
880 tid = mAudioTrackThread->getTid();
881 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700882 }
883
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
885 streamType,
886 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700887 // AudioFlinger only sees 16-bit PCM
888 format == AUDIO_FORMAT_PCM_8_BIT ?
889 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800890 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800891 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800892 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800893 sharedBuffer,
894 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800895 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700896 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800897 &status);
898
899 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000900 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800901 return status;
902 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700903 sp<IMemory> iMem = track->getCblk();
904 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000905 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800906 return NO_INIT;
907 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800908 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700909 mCblkMemory = iMem;
910 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
911 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800912 size_t temp = cblk->frameCount_;
913 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
914 // In current design, AudioTrack client checks and ensures frame count validity before
915 // passing it to AudioFlinger so AudioFlinger should not return a different value except
916 // for fast track as it uses a special method of assigning frame count.
917 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
918 }
919 frameCount = temp;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800920 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800921 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800922 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800923 } else {
Glenn Kastenb6037442012-11-14 13:42:25 -0800924 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700925 // once denied, do not request again if IAudioTrack is re-created
926 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
927 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800928 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700929 if (sharedBuffer == 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800930 mNotificationFramesAct = frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700931 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800932 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800933 if (sharedBuffer == 0) {
Glenn Kastenb929e412012-11-08 12:13:58 -0800934 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800935 } else {
Glenn Kastenb929e412012-11-08 12:13:58 -0800936 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700937 // Force buffer full condition as data is already present in shared memory
Glenn Kastenb6037442012-11-14 13:42:25 -0800938 cblk->stepUserOut(frameCount, frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800939 }
940
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700941 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700942 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700943 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700944 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700945 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
946 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700947 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700948 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -0800949 mLatency = afLatency + (1000*frameCount) / sampleRate;
950 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700951 // If IAudioTrack is re-created, don't let the requested frameCount
952 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -0800953 if (frameCount > mReqFrameCount) {
954 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700955 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800956 return NO_ERROR;
957}
958
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800959status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
960{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800961 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800962 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700963 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 audio_track_cblk_t* cblk = mCblk;
965 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700966 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967
968 audioBuffer->frameCount = 0;
969 audioBuffer->size = 0;
970
Glenn Kastenb6037442012-11-14 13:42:25 -0800971 uint32_t framesAvail = cblk->framesAvailableOut(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972
Eric Laurent9b7d9502011-03-21 11:49:00 -0700973 cblk->lock.lock();
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800974 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700975 goto create_new_track;
976 }
977 cblk->lock.unlock();
978
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800980 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800981 goto start_loop_here;
982 while (framesAvail == 0) {
983 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800984 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800986 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800987 return NO_MORE_BUFFERS;
988 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800989 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800990 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800992 }
Glenn Kasten9c5fdd82012-11-05 13:38:15 -0800993 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800994 mLock.unlock();
Glenn Kastena47f3162012-11-07 10:13:08 -0800995 // this condition is in shared memory, so if IAudioTrack and control block
996 // are replaced due to mediaserver death or IAudioTrack invalidation then
997 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -0700998 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -0700999 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001000 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001001 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001002 return status_t(STOPPED);
1003 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001004 // IAudioTrack may have been re-created while mLock was unlocked
1005 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001006 cblk->lock.lock();
1007 }
1008
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001009 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001010 goto create_new_track;
1011 }
Glenn Kastenf6b16782011-12-15 09:51:17 -08001012 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -07001013 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001014 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
1015 // timing out when a loop has been set and we have already written upto loop end
1016 // is a normal condition: no need to wake AudioFlinger up.
1017 if (cblk->user < cblk->loopEnd) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001018 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
1019 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001020 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001022 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001023 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001024 if (result == DEAD_OBJECT) {
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001025 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001026create_new_track:
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001027 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001028 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001029 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001030 }
1031 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001032 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001033 cblk->lock.unlock();
1034 return result;
1035 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001036 }
1037 cblk->waitTimeMs = 0;
1038 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001039
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001040 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001041 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001042 return TIMED_OUT;
1043 }
1044 }
1045 // read the server count again
1046 start_loop_here:
Glenn Kastenb6037442012-11-14 13:42:25 -08001047 framesAvail = cblk->framesAvailableOut_l(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001048 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001049 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001050 }
1051
1052 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001053
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001054 if (framesReq > framesAvail) {
1055 framesReq = framesAvail;
1056 }
1057
1058 uint32_t u = cblk->user;
Glenn Kastenb6037442012-11-14 13:42:25 -08001059 uint32_t bufferEnd = cblk->userBase + mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001060
Marco Nelissena1472d92012-03-30 14:36:54 -07001061 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001062 framesReq = bufferEnd - u;
1063 }
1064
Eric Laurentc2f1f072009-07-17 12:17:14 -07001065 audioBuffer->frameCount = framesReq;
Glenn Kasten83a03822012-11-12 07:58:20 -08001066 audioBuffer->size = framesReq * mFrameSizeAF;
1067 audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001068 active = mActive;
1069 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1070}
1071
1072void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1073{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001074 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001075 audio_track_cblk_t* cblk = mCblk;
Glenn Kastenb6037442012-11-14 13:42:25 -08001076 cblk->stepUserOut(audioBuffer->frameCount, mFrameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001077 if (audioBuffer->frameCount > 0) {
1078 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001079 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1080 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1081 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001082 mAudioTrack->start();
1083 }
1084 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001085}
1086
1087// -------------------------------------------------------------------------
1088
1089ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1090{
1091
Glenn Kastend65d73c2012-06-22 17:21:07 -07001092 if (mSharedBuffer != 0) {
1093 return INVALID_OPERATION;
1094 }
1095 if (mIsTimed) {
1096 return INVALID_OPERATION;
1097 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001098
1099 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001100 // Sanity-check: user is most-likely passing an error code, and it would
1101 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001102 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001103 buffer, userSize, userSize);
1104 return BAD_VALUE;
1105 }
1106
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001108
Eric Laurentdf839842012-05-31 14:27:14 -07001109 if (userSize == 0) {
1110 return 0;
1111 }
1112
Eric Laurent1703cdf2011-03-07 14:52:59 -08001113 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1114 // while we are accessing the cblk
1115 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001116 sp<IAudioTrack> audioTrack = mAudioTrack;
1117 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001118 mLock.unlock();
1119
Glenn Kastena47f3162012-11-07 10:13:08 -08001120 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1121 // so all cblk references might still refer to old shared memory, but that should be benign
1122
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001123 ssize_t written = 0;
1124 const int8_t *src = (const int8_t *)buffer;
1125 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001126 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001127
1128 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001129 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001130
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001131 status_t err = obtainBuffer(&audioBuffer, -1);
1132 if (err < 0) {
1133 // out of buffers, return #bytes written
Glenn Kastend65d73c2012-06-22 17:21:07 -07001134 if (err == status_t(NO_MORE_BUFFERS)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001135 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001136 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001137 return ssize_t(err);
1138 }
1139
1140 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001141
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001142 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001143 // Divide capacity by 2 to take expansion into account
1144 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001145 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001146 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001147 toWrite = audioBuffer.size;
1148 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001149 }
Glenn Kastenbc0f6b92012-11-12 14:32:06 -08001150 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001151 userSize -= toWrite;
1152 written += toWrite;
1153
1154 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001155 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001156
1157 return written;
1158}
1159
1160// -------------------------------------------------------------------------
1161
John Grossman4ff14ba2012-02-08 16:37:41 -08001162TimedAudioTrack::TimedAudioTrack() {
1163 mIsTimed = true;
1164}
1165
1166status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1167{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001168 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001169 status_t result = UNKNOWN_ERROR;
1170
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001171 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1172 // while we are accessing the cblk
1173 sp<IAudioTrack> audioTrack = mAudioTrack;
1174 sp<IMemory> iMem = mCblkMemory;
1175
John Grossman4ff14ba2012-02-08 16:37:41 -08001176 // If the track is not invalid already, try to allocate a buffer. alloc
1177 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001178 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001179 audio_track_cblk_t* cblk = mCblk;
1180 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001181 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1182 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001183 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001184 }
1185 }
1186
1187 // If the track is invalid at this point, attempt to restore it. and try the
1188 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001189 if (cblk->flags & CBLK_INVALID) {
1190 cblk->lock.lock();
1191 audio_track_cblk_t* temp = cblk;
Glenn Kasten22eb4e22012-11-07 14:03:00 -08001192 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001193 cblk = temp;
1194 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001195
Glenn Kastend65d73c2012-06-22 17:21:07 -07001196 if (result == OK) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001197 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001198 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001199 }
1200
1201 return result;
1202}
1203
1204status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1205 int64_t pts)
1206{
Eric Laurentdf839842012-05-31 14:27:14 -07001207 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1208 {
1209 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001210 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001211 // restart track if it was disabled by audioflinger due to previous underrun
1212 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001213 mActive && (cblk->flags & CBLK_DISABLED)) {
1214 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001215 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1216 mAudioTrack->start();
1217 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001218 }
Eric Laurentdf839842012-05-31 14:27:14 -07001219 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001220}
1221
1222status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1223 TargetTimeline target)
1224{
1225 return mAudioTrack->setMediaTimeTransform(xform, target);
1226}
1227
1228// -------------------------------------------------------------------------
1229
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1231{
1232 Buffer audioBuffer;
1233 uint32_t frames;
1234 size_t writtenSize;
1235
Eric Laurent1703cdf2011-03-07 14:52:59 -08001236 mLock.lock();
1237 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1238 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001239 sp<IAudioTrack> audioTrack = mAudioTrack;
1240 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001241 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001242 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001243 mLock.unlock();
1244
Glenn Kastena47f3162012-11-07 10:13:08 -08001245 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1246 // so all cblk references might still refer to old shared memory, but that should be benign
1247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248 // Manage underrun callback
Glenn Kastenb6037442012-11-14 13:42:25 -08001249 if (active && (cblk->framesAvailableOut(mFrameCount) == mFrameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001251 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252 mCbf(EVENT_UNDERRUN, mUserData, 0);
Glenn Kastenb6037442012-11-14 13:42:25 -08001253 if (cblk->server == mFrameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001254 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001256 if (mSharedBuffer != 0) {
1257 return false;
1258 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 }
1260 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001261
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001263 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 int loopCount = -1;
1265 mLoopCount--;
1266 if (mLoopCount >= 0) loopCount = mLoopCount;
1267
1268 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1269 }
1270
1271 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001272 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001273 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001275 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276 }
1277 }
1278
1279 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001280 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001281 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001282 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1283 mNewPosition += mUpdatePeriod;
1284 }
1285 }
1286
1287 // If Shared buffer is used, no data is requested from client.
1288 if (mSharedBuffer != 0) {
1289 frames = 0;
1290 } else {
1291 frames = mRemainingFrames;
1292 }
1293
Glenn Kasten99e53b82012-01-19 08:59:58 -08001294 // See description of waitCount parameter at declaration of obtainBuffer().
1295 // The logic below prevents us from being stuck below at obtainBuffer()
1296 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001297 int32_t waitCount = -1;
1298 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1299 waitCount = 1;
1300 }
1301
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001302 do {
1303
1304 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001305
Eric Laurent2267ba12011-09-07 11:13:23 -07001306 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307 if (err < NO_ERROR) {
1308 if (err != TIMED_OUT) {
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001309 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1310 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001311 return false;
1312 }
1313 break;
1314 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001315 if (err == status_t(STOPPED)) {
1316 return false;
1317 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001318
1319 // Divide buffer size by 2 to take into account the expansion
1320 // due to 8 to 16 bit conversion: the callback must fill only half
1321 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001322 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001323 audioBuffer.size >>= 1;
1324 }
1325
1326 size_t reqSize = audioBuffer.size;
1327 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1328 writtenSize = audioBuffer.size;
1329
1330 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001331 if (ssize_t(writtenSize) <= 0) {
1332 // The callback is done filling buffers
1333 // Keep this thread going to handle timed events and
1334 // still try to get more data in intervals of WAIT_PERIOD_MS
1335 // but don't just loop and block the CPU, so wait
1336 usleep(WAIT_PERIOD_MS*1000);
1337 break;
1338 }
Eric Laurentdf839842012-05-31 14:27:14 -07001339
Glenn Kastend65d73c2012-06-22 17:21:07 -07001340 if (writtenSize > reqSize) {
1341 writtenSize = reqSize;
1342 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001343
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001344 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001345 // 8 to 16 bit conversion, note that source and destination are the same address
1346 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001347 writtenSize <<= 1;
1348 }
1349
1350 audioBuffer.size = writtenSize;
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001351 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1352 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001353 // 16 bit.
Glenn Kasten83a03822012-11-12 07:58:20 -08001354 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001355
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001356 frames -= audioBuffer.frameCount;
1357
1358 releaseBuffer(&audioBuffer);
1359 }
1360 while (frames);
1361
1362 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001363 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364 } else {
1365 mRemainingFrames = frames;
1366 }
1367 return true;
1368}
1369
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001370// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001371// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001372// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1373// FIXME Don't depend on caller to hold strong references.
1374status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001375{
1376 status_t result;
1377
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001378 audio_track_cblk_t* cblk = refCblk;
1379 audio_track_cblk_t* newCblk = cblk;
Glenn Kasten827e5f12012-11-02 10:00:06 -07001380 ALOGW("dead IAudioTrack, creating a new one from %s",
1381 fromStart ? "start()" : "obtainBuffer()");
Eric Laurent1703cdf2011-03-07 14:52:59 -08001382
Glenn Kastena47f3162012-11-07 10:13:08 -08001383 // signal old cblk condition so that other threads waiting for available buffers stop
1384 // waiting now
1385 cblk->cv.broadcast();
1386 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001387
Glenn Kastena47f3162012-11-07 10:13:08 -08001388 // refresh the audio configuration cache in this process to make sure we get new
1389 // output parameters in getOutput_l() and createTrack_l()
1390 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001391
Glenn Kastena47f3162012-11-07 10:13:08 -08001392 // if the new IAudioTrack is created, createTrack_l() will modify the
1393 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1394 // It will also delete the strong references on previous IAudioTrack and IMemory
1395 result = createTrack_l(mStreamType,
1396 cblk->sampleRate,
1397 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001398 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001399 mFlags,
1400 mSharedBuffer,
1401 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001402
Glenn Kastena47f3162012-11-07 10:13:08 -08001403 if (result == NO_ERROR) {
1404 uint32_t user = cblk->user;
1405 uint32_t server = cblk->server;
1406 // restore write index and set other indexes to reflect empty buffer status
1407 newCblk = mCblk;
1408 newCblk->user = user;
1409 newCblk->server = user;
1410 newCblk->userBase = user;
1411 newCblk->serverBase = user;
1412 // restore loop: this is not guaranteed to succeed if new frame count is not
1413 // compatible with loop length
1414 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1415 if (!fromStart) {
1416 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1417 // Make sure that a client relying on callback events indicating underrun or
1418 // the actual amount of audio frames played (e.g SoundPool) receives them.
1419 if (mSharedBuffer == 0) {
1420 uint32_t frames = 0;
1421 if (user > server) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001422 frames = ((user - server) > mFrameCount) ?
1423 mFrameCount : (user - server);
Glenn Kasten83a03822012-11-12 07:58:20 -08001424 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001425 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001426 // restart playback even if buffer is not completely filled.
1427 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1428 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1429 // the client
Glenn Kastenb6037442012-11-14 13:42:25 -08001430 newCblk->stepUserOut(frames, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001431 }
1432 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001433 if (mSharedBuffer != 0) {
Glenn Kastenb6037442012-11-14 13:42:25 -08001434 newCblk->stepUserOut(mFrameCount, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001435 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001436 if (mActive) {
1437 result = mAudioTrack->start();
1438 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1439 }
1440 if (fromStart && result == NO_ERROR) {
1441 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001442 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001443 }
Glenn Kastena47f3162012-11-07 10:13:08 -08001444 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001445 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001446 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001447
1448 if (result == NO_ERROR) {
1449 // from now on we switch to the newly created cblk
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001450 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001451 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001452 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001453
Glenn Kasten827e5f12012-11-02 10:00:06 -07001454 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001455
1456 return result;
1457}
1458
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001459status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1460{
1461
1462 const size_t SIZE = 256;
1463 char buffer[SIZE];
1464 String8 result;
1465
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001466 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001467 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001468 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1469 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001470 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001471 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001472 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473 result.append(buffer);
Glenn Kasten3b16c762012-11-14 08:44:39 -08001474 snprintf(buffer, 255, " sample rate(%u), status(%d), muted(%d)\n",
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001475 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001476 result.append(buffer);
1477 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1478 result.append(buffer);
1479 ::write(fd, result.string(), result.size());
1480 return NO_ERROR;
1481}
1482
1483// =========================================================================
1484
1485AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001486 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1487{
1488}
1489
1490AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001491{
1492}
1493
1494bool AudioTrack::AudioTrackThread::threadLoop()
1495{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001496 {
1497 AutoMutex _l(mMyLock);
1498 if (mPaused) {
1499 mMyCond.wait(mMyLock);
1500 // caller will check for exitPending()
1501 return true;
1502 }
1503 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001504 if (!mReceiver.processAudioBuffer(this)) {
1505 pause();
1506 }
1507 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001508}
1509
Glenn Kasten3acbd052012-02-28 10:39:56 -08001510void AudioTrack::AudioTrackThread::requestExit()
1511{
1512 // must be in this order to avoid a race condition
1513 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001514 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001515}
1516
1517void AudioTrack::AudioTrackThread::pause()
1518{
1519 AutoMutex _l(mMyLock);
1520 mPaused = true;
1521}
1522
1523void AudioTrack::AudioTrackThread::resume()
1524{
1525 AutoMutex _l(mMyLock);
1526 if (mPaused) {
1527 mPaused = false;
1528 mMyCond.signal();
1529 }
1530}
1531
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001532// =========================================================================
1533
Eric Laurent38ccae22011-03-28 18:37:07 -07001534
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001535audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001536 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastenb6037442012-11-14 13:42:25 -08001537 userBase(0), serverBase(0), frameCount_(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001538 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001539 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001540{
1541}
1542
Glenn Kastenb6037442012-11-14 13:42:25 -08001543uint32_t audio_track_cblk_t::stepUser(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001544{
Glenn Kastenb6037442012-11-14 13:42:25 -08001545 ALOGV("stepuser %08x %08x %d", user, server, stepCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001546
Marco Nelissena1472d92012-03-30 14:36:54 -07001547 uint32_t u = user;
Glenn Kastenb6037442012-11-14 13:42:25 -08001548 u += stepCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001549 // Ensure that user is never ahead of server for AudioRecord
Glenn Kasten864585d2012-11-06 16:15:41 -08001550 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1552 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1553 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1554 }
Glenn Kasten90548972011-12-13 11:45:07 -08001555 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001556 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001557 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001558 }
1559
Glenn Kastenb6037442012-11-14 13:42:25 -08001560 if (u >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001561 // common case, user didn't just wrap
Glenn Kastenb6037442012-11-14 13:42:25 -08001562 if (u - frameCount >= userBase ) {
1563 userBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001564 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001565 } else if (u >= userBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001566 // user just wrapped
Glenn Kastenb6037442012-11-14 13:42:25 -08001567 userBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001568 }
1569
Glenn Kasten90548972011-12-13 11:45:07 -08001570 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001571
1572 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001573 if (flags & CBLK_UNDERRUN) {
1574 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001575 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001576
1577 return u;
1578}
1579
Glenn Kastenb6037442012-11-14 13:42:25 -08001580bool audio_track_cblk_t::stepServer(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001581{
Glenn Kastenb6037442012-11-14 13:42:25 -08001582 ALOGV("stepserver %08x %08x %d", user, server, stepCount);
Marco Nelissena1472d92012-03-30 14:36:54 -07001583
Eric Laurent38ccae22011-03-28 18:37:07 -07001584 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001585 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001586 return false;
1587 }
1588
Glenn Kasten90548972011-12-13 11:45:07 -08001589 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001590 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001591
Glenn Kastenb6037442012-11-14 13:42:25 -08001592 s += stepCount;
Glenn Kasten864585d2012-11-06 16:15:41 -08001593 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594 // Mark that we have read the first buffer so that next time stepUser() is called
1595 // we switch to normal obtainBuffer() timeout period
1596 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001597 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001598 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 // It is possible that we receive a flush()
1600 // while the mixer is processing a block: in this case,
1601 // stepServer() is called After the flush() has reset u & s and
1602 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001603 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001604 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001605 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 }
1607 }
1608
1609 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001610 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001611 s = loopStart;
1612 if (--loopCount == 0) {
1613 loopEnd = UINT_MAX;
1614 loopStart = UINT_MAX;
1615 }
1616 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001617
Glenn Kastenb6037442012-11-14 13:42:25 -08001618 if (s >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001619 // common case, server didn't just wrap
Glenn Kastenb6037442012-11-14 13:42:25 -08001620 if (s - frameCount >= serverBase ) {
1621 serverBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001622 }
Glenn Kastenb6037442012-11-14 13:42:25 -08001623 } else if (s >= serverBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001624 // server just wrapped
Glenn Kastenb6037442012-11-14 13:42:25 -08001625 serverBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001626 }
1627
Glenn Kasten90548972011-12-13 11:45:07 -08001628 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001629
Glenn Kasten9c5fdd82012-11-05 13:38:15 -08001630 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001631 cv.signal();
1632 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001633 lock.unlock();
1634 return true;
1635}
1636
Glenn Kasten83a03822012-11-12 07:58:20 -08001637void* audio_track_cblk_t::buffer(void *buffers, size_t frameSize, uint32_t offset) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001638{
Glenn Kasten90548972011-12-13 11:45:07 -08001639 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001640}
1641
Glenn Kastenb6037442012-11-14 13:42:25 -08001642uint32_t audio_track_cblk_t::framesAvailable(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643{
1644 Mutex::Autolock _l(lock);
Glenn Kastenb6037442012-11-14 13:42:25 -08001645 return framesAvailable_l(frameCount, isOut);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001646}
1647
Glenn Kastenb6037442012-11-14 13:42:25 -08001648uint32_t audio_track_cblk_t::framesAvailable_l(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649{
Glenn Kasten90548972011-12-13 11:45:07 -08001650 uint32_t u = user;
1651 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001652
Glenn Kasten864585d2012-11-06 16:15:41 -08001653 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001654 uint32_t limit = (s < loopStart) ? s : loopStart;
1655 return limit + frameCount - u;
1656 } else {
1657 return frameCount + u - s;
1658 }
1659}
1660
Glenn Kasten864585d2012-11-06 16:15:41 -08001661uint32_t audio_track_cblk_t::framesReady(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001662{
Glenn Kasten90548972011-12-13 11:45:07 -08001663 uint32_t u = user;
1664 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001665
Glenn Kasten864585d2012-11-06 16:15:41 -08001666 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001667 if (u < loopEnd) {
1668 return u - s;
1669 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001670 // do not block on mutex shared with client on AudioFlinger side
1671 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001672 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001673 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001674 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001675 uint32_t frames = UINT_MAX;
1676 if (loopCount >= 0) {
1677 frames = (loopEnd - loopStart)*loopCount + u - s;
1678 }
1679 lock.unlock();
1680 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681 }
1682 } else {
1683 return s - u;
1684 }
1685}
1686
Eric Laurent38ccae22011-03-28 18:37:07 -07001687bool audio_track_cblk_t::tryLock()
1688{
1689 // the code below simulates lock-with-timeout
1690 // we MUST do this to protect the AudioFlinger server
1691 // as this lock is shared with the client.
1692 status_t err;
1693
1694 err = lock.tryLock();
1695 if (err == -EBUSY) { // just wait a bit
1696 usleep(1000);
1697 err = lock.tryLock();
1698 }
1699 if (err != NO_ERROR) {
1700 // probably, the client just died.
1701 return false;
1702 }
1703 return true;
1704}
1705
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001706// -------------------------------------------------------------------------
1707
1708}; // namespace android