blob: ff1b21bc5842d1817c91d8b44f9b6699e1a3e3bd [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 Kasten7da35f22012-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 Kastena2049222012-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 Kasten1127d652012-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 Kasten7da35f22012-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 Kasten7da35f22012-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 Kasten7da35f22012-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 Kasten8af901c2012-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 Kasten7da35f22012-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 Kasten1127d652012-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 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700246 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700247
Dima Zavinfce7a472011-04-19 22:30:36 -0700248 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800249 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800250 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800251 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700252
253 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000254 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 return BAD_VALUE;
256 }
257
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800258 mVolume[LEFT] = 1.0f;
259 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800260 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700261 mFrameCount = frameCount;
Glenn Kastend7101432012-11-14 13:42:25 -0800262 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700263 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700264 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700265 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700266 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700267 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700268
Glenn Kastena997e7a2012-08-07 09:44:19 -0700269 if (cbf != NULL) {
Eric Laurentef6be0b2012-09-13 11:18:23 -0700270 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700271 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
272 }
273
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800274 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800275 status_t status = createTrack_l(streamType,
276 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800277 format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700278 channelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800279 frameCount,
280 flags,
281 sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700282 output);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800283
Glenn Kastena997e7a2012-08-07 09:44:19 -0700284 if (status != NO_ERROR) {
285 if (mAudioTrackThread != 0) {
286 mAudioTrackThread->requestExit();
287 mAudioTrackThread.clear();
288 }
289 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700290 }
291
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292 mStatus = NO_ERROR;
293
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800295 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700296 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800297 mChannelCount = channelCount;
Glenn Kasten5aab59a2012-11-12 07:58:20 -0800298
299 if (audio_is_linear_pcm(format)) {
300 mFrameSize = channelCount * audio_bytes_per_sample(format);
301 mFrameSizeAF = channelCount * sizeof(int16_t);
302 } else {
303 mFrameSize = sizeof(uint8_t);
304 mFrameSizeAF = sizeof(uint8_t);
305 }
306
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307 mSharedBuffer = sharedBuffer;
308 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800309 mActive = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 mUserData = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 mLoopCount = 0;
312 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700313 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 mNewPosition = 0;
315 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700316 mFlushed = false;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700317 AudioSystem::acquireAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318 return NO_ERROR;
319}
320
321status_t AudioTrack::initCheck() const
322{
323 return mStatus;
324}
325
326// -------------------------------------------------------------------------
327
328uint32_t AudioTrack::latency() const
329{
330 return mLatency;
331}
332
Glenn Kastenfff6d712012-01-12 16:38:12 -0800333audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334{
335 return mStreamType;
336}
337
Glenn Kastene1c39622012-01-04 09:36:37 -0800338audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339{
340 return mFormat;
341}
342
343int AudioTrack::channelCount() const
344{
345 return mChannelCount;
346}
347
Glenn Kasten7da35f22012-11-14 12:54:39 -0800348size_t AudioTrack::frameCount() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349{
Glenn Kastend7101432012-11-14 13:42:25 -0800350 return mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351}
352
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353sp<IMemory>& AudioTrack::sharedBuffer()
354{
355 return mSharedBuffer;
356}
357
358// -------------------------------------------------------------------------
359
360void AudioTrack::start()
361{
362 sp<AudioTrackThread> t = mAudioTrackThread;
363
Steve Block3856b092011-10-20 11:56:00 +0100364 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800365
Eric Laurentf5aafb22010-11-18 08:40:16 -0800366 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800367 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
368 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700369 sp<IAudioTrack> audioTrack = mAudioTrack;
370 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800371 audio_track_cblk_t* cblk = mCblk;
372
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800373 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700374 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800375 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800376 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700377 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800378 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
379 cblk->waitTimeMs = 0;
Glenn Kastend12b0332012-11-05 13:38:15 -0800380 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800381 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800382 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800383 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700384 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700385 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700386 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800387 }
388
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700389 ALOGV("start %p before lock cblk %p", this, cblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700390 status_t status = NO_ERROR;
Glenn Kastend12b0332012-11-05 13:38:15 -0800391 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800392 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800393 ALOGV("mAudioTrack->start()");
394 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800395 cblk->lock.lock();
396 if (status == DEAD_OBJECT) {
Glenn Kastend12b0332012-11-05 13:38:15 -0800397 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800398 }
Eric Laurent2b584242009-11-09 23:32:22 -0800399 }
Glenn Kastend12b0332012-11-05 13:38:15 -0800400 if (cblk->flags & CBLK_INVALID) {
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700401 audio_track_cblk_t* temp = cblk;
Glenn Kasten020f79f2012-11-07 14:03:00 -0800402 status = restoreTrack_l(temp, true /*fromStart*/);
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700403 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800404 }
405 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800406 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100407 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800408 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800409 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800410 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800411 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700412 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700413 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800414 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800415 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416 }
417
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418}
419
420void AudioTrack::stop()
421{
422 sp<AudioTrackThread> t = mAudioTrackThread;
423
Steve Block3856b092011-10-20 11:56:00 +0100424 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425
Eric Laurentf5aafb22010-11-18 08:40:16 -0800426 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800427 if (mActive) {
428 mActive = false;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700429 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 mAudioTrack->stop();
431 // Cancel loops (If we are in the middle of a loop, playback
432 // would not stop until loopCount reaches 0).
Eric Laurent1703cdf2011-03-07 14:52:59 -0800433 setLoop_l(0, 0, 0);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700434 // the playback head position will reset to 0, so if a marker is set, we need
435 // to activate it again
436 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437 // Force flush if a shared buffer is used otherwise audioflinger
438 // will not stop before end of buffer is reached.
439 if (mSharedBuffer != 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800440 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800441 }
442 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800443 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700445 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700446 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 }
448 }
449
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450}
451
452bool AudioTrack::stopped() const
453{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800454 AutoMutex lock(mLock);
455 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800456}
457
458void AudioTrack::flush()
459{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800460 AutoMutex lock(mLock);
461 flush_l();
462}
463
464// must be called with mLock held
465void AudioTrack::flush_l()
466{
Steve Block3856b092011-10-20 11:56:00 +0100467 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700468
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700469 // clear playback marker and periodic update counter
470 mMarkerPosition = 0;
471 mMarkerReached = false;
472 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700473
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800474 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700475 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 mAudioTrack->flush();
477 // Release AudioTrack callback thread in case it was waiting for new buffers
478 // in AudioTrack::obtainBuffer()
479 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 }
481}
482
483void AudioTrack::pause()
484{
Steve Block3856b092011-10-20 11:56:00 +0100485 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800486 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800487 if (mActive) {
488 mActive = false;
Eric Laurent192cbba2012-06-13 08:38:36 -0700489 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490 mAudioTrack->pause();
491 }
492}
493
494void AudioTrack::mute(bool e)
495{
496 mAudioTrack->mute(e);
497 mMuted = e;
498}
499
500bool AudioTrack::muted() const
501{
502 return mMuted;
503}
504
Eric Laurentbe916aa2010-06-01 23:49:17 -0700505status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800507 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700508 return BAD_VALUE;
509 }
510
Eric Laurent1703cdf2011-03-07 14:52:59 -0800511 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512 mVolume[LEFT] = left;
513 mVolume[RIGHT] = right;
514
Glenn Kasten83d86532012-01-17 14:39:34 -0800515 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700516
517 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518}
519
Glenn Kasten164d6532012-02-27 16:21:04 -0800520status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521{
Glenn Kasten164d6532012-02-27 16:21:04 -0800522 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700523}
524
Eric Laurent2beeb502010-07-16 07:43:46 -0700525status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700526{
Steve Block3856b092011-10-20 11:56:00 +0100527 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800528 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700529 return BAD_VALUE;
530 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800531 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700532
533 mSendLevel = level;
534
Glenn Kasten05632a52012-01-03 14:22:33 -0800535 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700536
537 return NO_ERROR;
538}
539
Glenn Kastena5224f32012-01-04 12:41:44 -0800540void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700541{
542 if (level != NULL) {
543 *level = mSendLevel;
544 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545}
546
Glenn Kasten1127d652012-11-14 08:44:39 -0800547status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548{
Glenn Kasten1127d652012-11-14 08:44:39 -0800549 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550
John Grossman4ff14ba2012-02-08 16:37:41 -0800551 if (mIsTimed) {
552 return INVALID_OPERATION;
553 }
554
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700556 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 }
558 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastena2049222012-06-22 17:21:07 -0700559 if (rate == 0 || rate > afSamplingRate*2 ) {
560 return BAD_VALUE;
561 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800562
Eric Laurent1703cdf2011-03-07 14:52:59 -0800563 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700564 mCblk->sampleRate = rate;
565 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566}
567
Glenn Kastena5224f32012-01-04 12:41:44 -0800568uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800569{
John Grossman4ff14ba2012-02-08 16:37:41 -0800570 if (mIsTimed) {
Glenn Kasten1127d652012-11-14 08:44:39 -0800571 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800572 }
573
Eric Laurent1703cdf2011-03-07 14:52:59 -0800574 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700575 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576}
577
578status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
579{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800580 AutoMutex lock(mLock);
581 return setLoop_l(loopStart, loopEnd, loopCount);
582}
583
584// must be called with mLock held
585status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
586{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 audio_track_cblk_t* cblk = mCblk;
588
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 Mutex::Autolock _l(cblk->lock);
590
591 if (loopCount == 0) {
592 cblk->loopStart = UINT_MAX;
593 cblk->loopEnd = UINT_MAX;
594 cblk->loopCount = 0;
595 mLoopCount = 0;
596 return NO_ERROR;
597 }
598
John Grossman4ff14ba2012-02-08 16:37:41 -0800599 if (mIsTimed) {
600 return INVALID_OPERATION;
601 }
602
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603 if (loopStart >= loopEnd ||
Glenn Kastend7101432012-11-14 13:42:25 -0800604 loopEnd - loopStart > mFrameCount ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700605 cblk->server > loopStart) {
Glenn Kasten8af901c2012-11-01 11:11:38 -0700606 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, "
Glenn Kastend7101432012-11-14 13:42:25 -0800607 "user %d", loopStart, loopEnd, loopCount, mFrameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608 return BAD_VALUE;
609 }
610
Glenn Kastend7101432012-11-14 13:42:25 -0800611 if ((mSharedBuffer != 0) && (loopEnd > mFrameCount)) {
Glenn Kasten8af901c2012-11-01 11:11:38 -0700612 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, "
613 "framecount %d",
Glenn Kastend7101432012-11-14 13:42:25 -0800614 loopStart, loopEnd, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700616 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617
618 cblk->loopStart = loopStart;
619 cblk->loopEnd = loopEnd;
620 cblk->loopCount = loopCount;
621 mLoopCount = loopCount;
622
623 return NO_ERROR;
624}
625
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626status_t AudioTrack::setMarkerPosition(uint32_t marker)
627{
Glenn Kastena2049222012-06-22 17:21:07 -0700628 if (mCbf == NULL) {
629 return INVALID_OPERATION;
630 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631
632 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700633 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634
635 return NO_ERROR;
636}
637
Glenn Kastena5224f32012-01-04 12:41:44 -0800638status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639{
Glenn Kastena2049222012-06-22 17:21:07 -0700640 if (marker == NULL) {
641 return BAD_VALUE;
642 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643
644 *marker = mMarkerPosition;
645
646 return NO_ERROR;
647}
648
649status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
650{
Glenn Kastena2049222012-06-22 17:21:07 -0700651 if (mCbf == NULL) {
652 return INVALID_OPERATION;
653 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654
655 uint32_t curPosition;
656 getPosition(&curPosition);
657 mNewPosition = curPosition + updatePeriod;
658 mUpdatePeriod = updatePeriod;
659
660 return NO_ERROR;
661}
662
Glenn Kastena5224f32012-01-04 12:41:44 -0800663status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664{
Glenn Kastena2049222012-06-22 17:21:07 -0700665 if (updatePeriod == NULL) {
666 return BAD_VALUE;
667 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668
669 *updatePeriod = mUpdatePeriod;
670
671 return NO_ERROR;
672}
673
674status_t AudioTrack::setPosition(uint32_t position)
675{
Glenn Kastena2049222012-06-22 17:21:07 -0700676 if (mIsTimed) {
677 return INVALID_OPERATION;
678 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800679
Eric Laurent1703cdf2011-03-07 14:52:59 -0800680 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681
Glenn Kastena2049222012-06-22 17:21:07 -0700682 if (!stopped_l()) {
683 return INVALID_OPERATION;
684 }
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800685
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700686 audio_track_cblk_t* cblk = mCblk;
687 Mutex::Autolock _l(cblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688
Glenn Kastena2049222012-06-22 17:21:07 -0700689 if (position > cblk->user) {
690 return BAD_VALUE;
691 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700693 cblk->server = position;
694 android_atomic_or(CBLK_FORCEREADY, &cblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700695
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696 return NO_ERROR;
697}
698
699status_t AudioTrack::getPosition(uint32_t *position)
700{
Glenn Kastena2049222012-06-22 17:21:07 -0700701 if (position == NULL) {
702 return BAD_VALUE;
703 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800704 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700705 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706
707 return NO_ERROR;
708}
709
710status_t AudioTrack::reload()
711{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800712 AutoMutex lock(mLock);
713
Glenn Kastena2049222012-06-22 17:21:07 -0700714 if (!stopped_l()) {
715 return INVALID_OPERATION;
716 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700717
Eric Laurent1703cdf2011-03-07 14:52:59 -0800718 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700720 audio_track_cblk_t* cblk = mCblk;
Glenn Kastend7101432012-11-14 13:42:25 -0800721 cblk->stepUserOut(mFrameCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800722
723 return NO_ERROR;
724}
725
Eric Laurentc2f1f072009-07-17 12:17:14 -0700726audio_io_handle_t AudioTrack::getOutput()
727{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800728 AutoMutex lock(mLock);
729 return getOutput_l();
730}
731
732// must be called with mLock held
733audio_io_handle_t AudioTrack::getOutput_l()
734{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800735 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800736 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700737}
738
Glenn Kastena5224f32012-01-04 12:41:44 -0800739int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700740{
741 return mSessionId;
742}
743
744status_t AudioTrack::attachAuxEffect(int effectId)
745{
Steve Block3856b092011-10-20 11:56:00 +0100746 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700747 status_t status = mAudioTrack->attachAuxEffect(effectId);
748 if (status == NO_ERROR) {
749 mAuxEffectId = effectId;
750 }
751 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700752}
753
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800754// -------------------------------------------------------------------------
755
Eric Laurent1703cdf2011-03-07 14:52:59 -0800756// must be called with mLock held
757status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800758 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800759 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800760 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700761 audio_channel_mask_t channelMask,
Glenn Kasten7da35f22012-11-14 12:54:39 -0800762 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700763 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800764 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700765 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800766{
767 status_t status;
768 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
769 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700770 ALOGE("Could not get audioflinger");
771 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800772 }
773
Eric Laurentd1b449a2010-05-14 03:26:45 -0700774 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700775 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700776 return NO_INIT;
777 }
778
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700779 // Client decides whether the track is TIMED (see below), but can only express a preference
780 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700781 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700782 // either of these use cases:
783 // use case 1: shared buffer
784 (sharedBuffer != 0) ||
785 // use case 2: callback handler
786 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800787 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700788 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700789 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700790 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700791 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700792 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700793
Eric Laurentd1b449a2010-05-14 03:26:45 -0700794 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700795
Dima Zavinfce7a472011-04-19 22:30:36 -0700796 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700797
Eric Laurentd1b449a2010-05-14 03:26:45 -0700798 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700799 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700800 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700801 } else if (frameCount == 0) {
Glenn Kasten7da35f22012-11-14 12:54:39 -0800802 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700803 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
804 return NO_INIT;
805 }
806 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700807 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700808
809 } else if (sharedBuffer != 0) {
810
811 // Ensure that buffer alignment matches channelCount
812 int channelCount = popcount(channelMask);
813 // 8-bit data in shared memory is not currently supported by AudioFlinger
814 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
815 if (channelCount > 1) {
816 // More than 2 channels does not require stronger alignment than stereo
817 alignment <<= 1;
818 }
819 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
820 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
821 sharedBuffer->pointer(), channelCount);
822 return BAD_VALUE;
823 }
824
825 // When initializing a shared buffer AudioTrack via constructors,
826 // there's no frameCount parameter.
827 // But when initializing a shared buffer AudioTrack via set(),
828 // there _is_ a frameCount parameter. We silently ignore it.
829 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
830
831 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
832
833 // FIXME move these calculations and associated checks to server
Glenn Kasten1127d652012-11-14 08:44:39 -0800834 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700835 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
836 return NO_INIT;
837 }
Glenn Kasten7da35f22012-11-14 12:54:39 -0800838 size_t afFrameCount;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700839 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
840 return NO_INIT;
841 }
842
Eric Laurentd1b449a2010-05-14 03:26:45 -0700843 // Ensure that buffer depth covers at least audio hardware latency
844 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
845 if (minBufCount < 2) minBufCount = 2;
846
Glenn Kasten7da35f22012-11-14 12:54:39 -0800847 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
848 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800849 ", afLatency=%d",
850 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700851
852 if (frameCount == 0) {
853 frameCount = minFrameCount;
854 }
855 if (mNotificationFramesAct == 0) {
856 mNotificationFramesAct = frameCount/2;
857 }
858 // Make sure that application is notified with sufficient margin
859 // before underrun
Glenn Kasten7da35f22012-11-14 12:54:39 -0800860 if (mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700861 mNotificationFramesAct = frameCount/2;
862 }
863 if (frameCount < minFrameCount) {
864 // not ALOGW because it happens all the time when playing key clicks over A2DP
865 ALOGV("Minimum buffer size corrected from %d to %d",
866 frameCount, minFrameCount);
867 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800868 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700869
Glenn Kastene0fa4672012-04-24 14:35:14 -0700870 } else {
871 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700872 }
873
Glenn Kastena075db42012-03-06 11:22:44 -0800874 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
875 if (mIsTimed) {
876 trackFlags |= IAudioFlinger::TRACK_TIMED;
877 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800878
879 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700880 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700881 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800882 if (mAudioTrackThread != 0) {
883 tid = mAudioTrackThread->getTid();
884 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700885 }
886
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800887 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
888 streamType,
889 sampleRate,
Glenn Kasten520a9af2012-06-21 12:56:37 -0700890 // AudioFlinger only sees 16-bit PCM
891 format == AUDIO_FORMAT_PCM_8_BIT ?
892 AUDIO_FORMAT_PCM_16_BIT : format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700893 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800894 frameCount,
Glenn Kastenc2674152012-11-06 15:03:34 -0800895 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800896 sharedBuffer,
897 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800898 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700899 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800900 &status);
901
902 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000903 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800904 return status;
905 }
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700906 sp<IMemory> iMem = track->getCblk();
907 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000908 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800909 return NO_INIT;
910 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800911 mAudioTrack = track;
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700912 mCblkMemory = iMem;
913 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
914 mCblk = cblk;
Glenn Kastend7101432012-11-14 13:42:25 -0800915 size_t temp = cblk->frameCount_;
916 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
917 // In current design, AudioTrack client checks and ensures frame count validity before
918 // passing it to AudioFlinger so AudioFlinger should not return a different value except
919 // for fast track as it uses a special method of assigning frame count.
920 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
921 }
922 frameCount = temp;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800923 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastenc2674152012-11-06 15:03:34 -0800924 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastend7101432012-11-14 13:42:25 -0800925 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800926 } else {
Glenn Kastend7101432012-11-14 13:42:25 -0800927 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -0700928 // once denied, do not request again if IAudioTrack is re-created
929 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
930 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800931 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700932 if (sharedBuffer == 0) {
Glenn Kastend7101432012-11-14 13:42:25 -0800933 mNotificationFramesAct = frameCount/2;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700934 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800935 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800936 if (sharedBuffer == 0) {
Glenn Kasten2f6226a2012-11-08 12:13:58 -0800937 mBuffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800938 } else {
Glenn Kasten2f6226a2012-11-08 12:13:58 -0800939 mBuffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700940 // Force buffer full condition as data is already present in shared memory
Glenn Kastend7101432012-11-14 13:42:25 -0800941 cblk->stepUserOut(frameCount, frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800942 }
943
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700944 cblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
Glenn Kasten8af901c2012-11-01 11:11:38 -0700945 uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700946 cblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -0700947 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastenc9f872e2012-11-01 14:58:02 -0700948 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
949 cblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700950 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700951 // FIXME don't believe this lie
Glenn Kastend7101432012-11-14 13:42:25 -0800952 mLatency = afLatency + (1000*frameCount) / sampleRate;
953 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700954 // If IAudioTrack is re-created, don't let the requested frameCount
955 // decrease. This can confuse clients that cache frameCount().
Glenn Kastend7101432012-11-14 13:42:25 -0800956 if (frameCount > mReqFrameCount) {
957 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700958 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800959 return NO_ERROR;
960}
961
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
963{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800964 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800965 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -0700966 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 audio_track_cblk_t* cblk = mCblk;
968 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -0700969 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970
971 audioBuffer->frameCount = 0;
972 audioBuffer->size = 0;
973
Glenn Kastend7101432012-11-14 13:42:25 -0800974 uint32_t framesAvail = cblk->framesAvailableOut(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975
Eric Laurent9b7d9502011-03-21 11:49:00 -0700976 cblk->lock.lock();
Glenn Kastend12b0332012-11-05 13:38:15 -0800977 if (cblk->flags & CBLK_INVALID) {
Eric Laurent9b7d9502011-03-21 11:49:00 -0700978 goto create_new_track;
979 }
980 cblk->lock.unlock();
981
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800983 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800984 goto start_loop_here;
985 while (framesAvail == 0) {
986 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -0800987 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +0100988 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800989 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800990 return NO_MORE_BUFFERS;
991 }
Glenn Kastenf6b16782011-12-15 09:51:17 -0800992 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800993 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800994 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800995 }
Glenn Kastend12b0332012-11-05 13:38:15 -0800996 if (!(cblk->flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -0800997 mLock.unlock();
Glenn Kastene0461d12012-11-07 10:13:08 -0800998 // this condition is in shared memory, so if IAudioTrack and control block
999 // are replaced due to mediaserver death or IAudioTrack invalidation then
1000 // cv won't be signalled, but fortunately the timeout will limit the wait
Eric Laurentd1b449a2010-05-14 03:26:45 -07001001 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -07001002 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001003 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001004 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001005 return status_t(STOPPED);
1006 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001007 // IAudioTrack may have been re-created while mLock was unlocked
1008 cblk = mCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001009 cblk->lock.lock();
1010 }
1011
Glenn Kastend12b0332012-11-05 13:38:15 -08001012 if (cblk->flags & CBLK_INVALID) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001013 goto create_new_track;
1014 }
Glenn Kastenf6b16782011-12-15 09:51:17 -08001015 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -07001016 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001017 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
1018 // timing out when a loop has been set and we have already written upto loop end
1019 // is a normal condition: no need to wake AudioFlinger up.
1020 if (cblk->user < cblk->loopEnd) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001021 ALOGW("obtainBuffer timed out (is the CPU pegged?) %p name=%#x user=%08x, "
1022 "server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001023 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001025 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001027 if (result == DEAD_OBJECT) {
Glenn Kastend12b0332012-11-05 13:38:15 -08001028 android_atomic_or(CBLK_INVALID, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001029create_new_track:
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001030 audio_track_cblk_t* temp = cblk;
Glenn Kasten020f79f2012-11-07 14:03:00 -08001031 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001032 cblk = temp;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001033 }
1034 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001035 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001036 cblk->lock.unlock();
1037 return result;
1038 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001039 }
1040 cblk->waitTimeMs = 0;
1041 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001042
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001043 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001044 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001045 return TIMED_OUT;
1046 }
1047 }
1048 // read the server count again
1049 start_loop_here:
Glenn Kastend7101432012-11-14 13:42:25 -08001050 framesAvail = cblk->framesAvailableOut_l(mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001051 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001052 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001053 }
1054
1055 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001056
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001057 if (framesReq > framesAvail) {
1058 framesReq = framesAvail;
1059 }
1060
1061 uint32_t u = cblk->user;
Glenn Kastend7101432012-11-14 13:42:25 -08001062 uint32_t bufferEnd = cblk->userBase + mFrameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001063
Marco Nelissena1472d92012-03-30 14:36:54 -07001064 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001065 framesReq = bufferEnd - u;
1066 }
1067
Eric Laurentc2f1f072009-07-17 12:17:14 -07001068 audioBuffer->frameCount = framesReq;
Glenn Kasten5aab59a2012-11-12 07:58:20 -08001069 audioBuffer->size = framesReq * mFrameSizeAF;
1070 audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001071 active = mActive;
1072 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1073}
1074
1075void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1076{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001077 AutoMutex lock(mLock);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001078 audio_track_cblk_t* cblk = mCblk;
Glenn Kastend7101432012-11-14 13:42:25 -08001079 cblk->stepUserOut(audioBuffer->frameCount, mFrameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001080 if (audioBuffer->frameCount > 0) {
1081 // restart track if it was disabled by audioflinger due to previous underrun
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001082 if (mActive && (cblk->flags & CBLK_DISABLED)) {
1083 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
1084 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, cblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001085 mAudioTrack->start();
1086 }
1087 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001088}
1089
1090// -------------------------------------------------------------------------
1091
1092ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1093{
1094
Glenn Kastena2049222012-06-22 17:21:07 -07001095 if (mSharedBuffer != 0) {
1096 return INVALID_OPERATION;
1097 }
1098 if (mIsTimed) {
1099 return INVALID_OPERATION;
1100 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001101
1102 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001103 // Sanity-check: user is most-likely passing an error code, and it would
1104 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001105 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001106 buffer, userSize, userSize);
1107 return BAD_VALUE;
1108 }
1109
Steve Block3856b092011-10-20 11:56:00 +01001110 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001111
Eric Laurentdf839842012-05-31 14:27:14 -07001112 if (userSize == 0) {
1113 return 0;
1114 }
1115
Eric Laurent1703cdf2011-03-07 14:52:59 -08001116 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1117 // while we are accessing the cblk
1118 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001119 sp<IAudioTrack> audioTrack = mAudioTrack;
1120 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001121 mLock.unlock();
1122
Glenn Kastene0461d12012-11-07 10:13:08 -08001123 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1124 // so all cblk references might still refer to old shared memory, but that should be benign
1125
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001126 ssize_t written = 0;
1127 const int8_t *src = (const int8_t *)buffer;
1128 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001129 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001130
1131 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001132 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001133
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001134 status_t err = obtainBuffer(&audioBuffer, -1);
1135 if (err < 0) {
1136 // out of buffers, return #bytes written
Glenn Kastena2049222012-06-22 17:21:07 -07001137 if (err == status_t(NO_MORE_BUFFERS)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001138 break;
Glenn Kastena2049222012-06-22 17:21:07 -07001139 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001140 return ssize_t(err);
1141 }
1142
1143 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001144
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001145 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001146 // Divide capacity by 2 to take expansion into account
1147 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001148 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001149 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001150 toWrite = audioBuffer.size;
1151 memcpy(audioBuffer.i8, src, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001152 }
Glenn Kasten9c6c02e2012-11-12 14:32:06 -08001153 src += toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001154 userSize -= toWrite;
1155 written += toWrite;
1156
1157 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001158 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001159
1160 return written;
1161}
1162
1163// -------------------------------------------------------------------------
1164
John Grossman4ff14ba2012-02-08 16:37:41 -08001165TimedAudioTrack::TimedAudioTrack() {
1166 mIsTimed = true;
1167}
1168
1169status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1170{
Glenn Kastena96bd952012-11-02 13:05:14 -07001171 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001172 status_t result = UNKNOWN_ERROR;
1173
Glenn Kastena96bd952012-11-02 13:05:14 -07001174 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1175 // while we are accessing the cblk
1176 sp<IAudioTrack> audioTrack = mAudioTrack;
1177 sp<IMemory> iMem = mCblkMemory;
1178
John Grossman4ff14ba2012-02-08 16:37:41 -08001179 // If the track is not invalid already, try to allocate a buffer. alloc
1180 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kasten2662ac92012-07-30 10:59:30 -07001181 // we can attempt to restore in just a bit.
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001182 audio_track_cblk_t* cblk = mCblk;
1183 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001184 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1185 if (result == DEAD_OBJECT) {
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001186 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001187 }
1188 }
1189
1190 // If the track is invalid at this point, attempt to restore it. and try the
1191 // allocation one more time.
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001192 if (cblk->flags & CBLK_INVALID) {
1193 cblk->lock.lock();
1194 audio_track_cblk_t* temp = cblk;
Glenn Kasten020f79f2012-11-07 14:03:00 -08001195 result = restoreTrack_l(temp, false /*fromStart*/);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001196 cblk = temp;
1197 cblk->lock.unlock();
John Grossman4ff14ba2012-02-08 16:37:41 -08001198
Glenn Kastena2049222012-06-22 17:21:07 -07001199 if (result == OK) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001200 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastena2049222012-06-22 17:21:07 -07001201 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001202 }
1203
1204 return result;
1205}
1206
1207status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1208 int64_t pts)
1209{
Eric Laurentdf839842012-05-31 14:27:14 -07001210 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1211 {
1212 AutoMutex lock(mLock);
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001213 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001214 // restart track if it was disabled by audioflinger due to previous underrun
1215 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001216 mActive && (cblk->flags & CBLK_DISABLED)) {
1217 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001218 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1219 mAudioTrack->start();
1220 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001221 }
Eric Laurentdf839842012-05-31 14:27:14 -07001222 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001223}
1224
1225status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1226 TargetTimeline target)
1227{
1228 return mAudioTrack->setMediaTimeTransform(xform, target);
1229}
1230
1231// -------------------------------------------------------------------------
1232
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001233bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1234{
1235 Buffer audioBuffer;
1236 uint32_t frames;
1237 size_t writtenSize;
1238
Eric Laurent1703cdf2011-03-07 14:52:59 -08001239 mLock.lock();
1240 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1241 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001242 sp<IAudioTrack> audioTrack = mAudioTrack;
1243 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001244 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001245 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001246 mLock.unlock();
1247
Glenn Kastene0461d12012-11-07 10:13:08 -08001248 // since mLock is unlocked the IAudioTrack and shared memory may be re-created,
1249 // so all cblk references might still refer to old shared memory, but that should be benign
1250
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 // Manage underrun callback
Glenn Kastend7101432012-11-14 13:42:25 -08001252 if (active && (cblk->framesAvailableOut(mFrameCount) == mFrameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Glenn Kastend12b0332012-11-05 13:38:15 -08001254 if (!(android_atomic_or(CBLK_UNDERRUN, &cblk->flags) & CBLK_UNDERRUN)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 mCbf(EVENT_UNDERRUN, mUserData, 0);
Glenn Kastend7101432012-11-14 13:42:25 -08001256 if (cblk->server == mFrameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001257 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258 }
Glenn Kastena2049222012-06-22 17:21:07 -07001259 if (mSharedBuffer != 0) {
1260 return false;
1261 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 }
1263 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001266 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267 int loopCount = -1;
1268 mLoopCount--;
1269 if (mLoopCount >= 0) loopCount = mLoopCount;
1270
1271 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1272 }
1273
1274 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001275 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001276 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001278 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279 }
1280 }
1281
1282 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001283 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001284 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1286 mNewPosition += mUpdatePeriod;
1287 }
1288 }
1289
1290 // If Shared buffer is used, no data is requested from client.
1291 if (mSharedBuffer != 0) {
1292 frames = 0;
1293 } else {
1294 frames = mRemainingFrames;
1295 }
1296
Glenn Kasten99e53b82012-01-19 08:59:58 -08001297 // See description of waitCount parameter at declaration of obtainBuffer().
1298 // The logic below prevents us from being stuck below at obtainBuffer()
1299 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001300 int32_t waitCount = -1;
1301 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1302 waitCount = 1;
1303 }
1304
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 do {
1306
1307 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001308
Eric Laurent2267ba12011-09-07 11:13:23 -07001309 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001310 if (err < NO_ERROR) {
1311 if (err != TIMED_OUT) {
Glenn Kasten8af901c2012-11-01 11:11:38 -07001312 ALOGE_IF(err != status_t(NO_MORE_BUFFERS),
1313 "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001314 return false;
1315 }
1316 break;
1317 }
Glenn Kastena2049222012-06-22 17:21:07 -07001318 if (err == status_t(STOPPED)) {
1319 return false;
1320 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321
1322 // Divide buffer size by 2 to take into account the expansion
1323 // due to 8 to 16 bit conversion: the callback must fill only half
1324 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001325 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326 audioBuffer.size >>= 1;
1327 }
1328
1329 size_t reqSize = audioBuffer.size;
1330 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1331 writtenSize = audioBuffer.size;
1332
1333 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001334 if (ssize_t(writtenSize) <= 0) {
1335 // The callback is done filling buffers
1336 // Keep this thread going to handle timed events and
1337 // still try to get more data in intervals of WAIT_PERIOD_MS
1338 // but don't just loop and block the CPU, so wait
1339 usleep(WAIT_PERIOD_MS*1000);
1340 break;
1341 }
Eric Laurentdf839842012-05-31 14:27:14 -07001342
Glenn Kastena2049222012-06-22 17:21:07 -07001343 if (writtenSize > reqSize) {
1344 writtenSize = reqSize;
1345 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001346
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001347 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001348 // 8 to 16 bit conversion, note that source and destination are the same address
1349 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001350 writtenSize <<= 1;
1351 }
1352
1353 audioBuffer.size = writtenSize;
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001354 // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
1355 // 8 bit PCM data: in this case, cblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001356 // 16 bit.
Glenn Kasten5aab59a2012-11-12 07:58:20 -08001357 audioBuffer.frameCount = writtenSize / mFrameSizeAF;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001358
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359 frames -= audioBuffer.frameCount;
1360
1361 releaseBuffer(&audioBuffer);
1362 }
1363 while (frames);
1364
1365 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001366 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001367 } else {
1368 mRemainingFrames = frames;
1369 }
1370 return true;
1371}
1372
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001373// must be called with mLock and refCblk.lock held. Callers must also hold strong references on
Eric Laurent1703cdf2011-03-07 14:52:59 -08001374// the IAudioTrack and IMemory in case they are recreated here.
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001375// If the IAudioTrack is successfully restored, the refCblk pointer is updated
1376// FIXME Don't depend on caller to hold strong references.
1377status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& refCblk, bool fromStart)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001378{
1379 status_t result;
1380
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001381 audio_track_cblk_t* cblk = refCblk;
1382 audio_track_cblk_t* newCblk = cblk;
Glenn Kasten411e4472012-11-02 10:00:06 -07001383 ALOGW("dead IAudioTrack, creating a new one from %s",
1384 fromStart ? "start()" : "obtainBuffer()");
Eric Laurent1703cdf2011-03-07 14:52:59 -08001385
Glenn Kastene0461d12012-11-07 10:13:08 -08001386 // signal old cblk condition so that other threads waiting for available buffers stop
1387 // waiting now
1388 cblk->cv.broadcast();
1389 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001390
Glenn Kastene0461d12012-11-07 10:13:08 -08001391 // refresh the audio configuration cache in this process to make sure we get new
1392 // output parameters in getOutput_l() and createTrack_l()
1393 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001394
Glenn Kastene0461d12012-11-07 10:13:08 -08001395 // if the new IAudioTrack is created, createTrack_l() will modify the
1396 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1397 // It will also delete the strong references on previous IAudioTrack and IMemory
1398 result = createTrack_l(mStreamType,
1399 cblk->sampleRate,
1400 mFormat,
1401 mChannelMask,
Glenn Kastend7101432012-11-14 13:42:25 -08001402 mReqFrameCount, // so that frame count never goes down
Glenn Kastene0461d12012-11-07 10:13:08 -08001403 mFlags,
1404 mSharedBuffer,
1405 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001406
Glenn Kastene0461d12012-11-07 10:13:08 -08001407 if (result == NO_ERROR) {
1408 uint32_t user = cblk->user;
1409 uint32_t server = cblk->server;
1410 // restore write index and set other indexes to reflect empty buffer status
1411 newCblk = mCblk;
1412 newCblk->user = user;
1413 newCblk->server = user;
1414 newCblk->userBase = user;
1415 newCblk->serverBase = user;
1416 // restore loop: this is not guaranteed to succeed if new frame count is not
1417 // compatible with loop length
1418 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
1419 if (!fromStart) {
1420 newCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1421 // Make sure that a client relying on callback events indicating underrun or
1422 // the actual amount of audio frames played (e.g SoundPool) receives them.
1423 if (mSharedBuffer == 0) {
1424 uint32_t frames = 0;
1425 if (user > server) {
Glenn Kastend7101432012-11-14 13:42:25 -08001426 frames = ((user - server) > mFrameCount) ?
1427 mFrameCount : (user - server);
Glenn Kasten5aab59a2012-11-12 07:58:20 -08001428 memset(mBuffers, 0, frames * mFrameSizeAF);
Eric Laurent408b8dc2011-09-06 12:36:15 -07001429 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001430 // restart playback even if buffer is not completely filled.
1431 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
1432 // stepUser() clears CBLK_UNDERRUN flag enabling underrun callbacks to
1433 // the client
Glenn Kastend7101432012-11-14 13:42:25 -08001434 newCblk->stepUserOut(frames, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001435 }
1436 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001437 if (mSharedBuffer != 0) {
Glenn Kastend7101432012-11-14 13:42:25 -08001438 newCblk->stepUserOut(mFrameCount, mFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001439 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001440 if (mActive) {
1441 result = mAudioTrack->start();
1442 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
1443 }
1444 if (fromStart && result == NO_ERROR) {
1445 mNewPosition = newCblk->server + mUpdatePeriod;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001446 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001447 }
Glenn Kastene0461d12012-11-07 10:13:08 -08001448 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Steve Block3856b092011-10-20 11:56:00 +01001449 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001450 result, mActive, newCblk, cblk, newCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001451
1452 if (result == NO_ERROR) {
1453 // from now on we switch to the newly created cblk
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001454 refCblk = newCblk;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001455 }
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001456 newCblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001457
Glenn Kasten411e4472012-11-02 10:00:06 -07001458 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001459
1460 return result;
1461}
1462
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001463status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1464{
1465
1466 const size_t SIZE = 256;
1467 char buffer[SIZE];
1468 String8 result;
1469
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001470 audio_track_cblk_t* cblk = mCblk;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001471 result.append(" AudioTrack::dump\n");
Glenn Kasten8af901c2012-11-01 11:11:38 -07001472 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1473 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001474 result.append(buffer);
Glenn Kasten8af901c2012-11-01 11:11:38 -07001475 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastend7101432012-11-14 13:42:25 -08001476 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001477 result.append(buffer);
Glenn Kasten1127d652012-11-14 08:44:39 -08001478 snprintf(buffer, 255, " sample rate(%u), status(%d), muted(%d)\n",
Glenn Kastenc9f872e2012-11-01 14:58:02 -07001479 (cblk == 0) ? 0 : cblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480 result.append(buffer);
1481 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1482 result.append(buffer);
1483 ::write(fd, result.string(), result.size());
1484 return NO_ERROR;
1485}
1486
1487// =========================================================================
1488
1489AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001490 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1491{
1492}
1493
1494AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001495{
1496}
1497
1498bool AudioTrack::AudioTrackThread::threadLoop()
1499{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001500 {
1501 AutoMutex _l(mMyLock);
1502 if (mPaused) {
1503 mMyCond.wait(mMyLock);
1504 // caller will check for exitPending()
1505 return true;
1506 }
1507 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001508 if (!mReceiver.processAudioBuffer(this)) {
1509 pause();
1510 }
1511 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001512}
1513
Glenn Kasten3acbd052012-02-28 10:39:56 -08001514void AudioTrack::AudioTrackThread::requestExit()
1515{
1516 // must be in this order to avoid a race condition
1517 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001518 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001519}
1520
1521void AudioTrack::AudioTrackThread::pause()
1522{
1523 AutoMutex _l(mMyLock);
1524 mPaused = true;
1525}
1526
1527void AudioTrack::AudioTrackThread::resume()
1528{
1529 AutoMutex _l(mMyLock);
1530 if (mPaused) {
1531 mPaused = false;
1532 mMyCond.signal();
1533 }
1534}
1535
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001536// =========================================================================
1537
Eric Laurent38ccae22011-03-28 18:37:07 -07001538
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001539audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001540 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastend7101432012-11-14 13:42:25 -08001541 userBase(0), serverBase(0), frameCount_(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001542 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001543 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001544{
1545}
1546
Glenn Kastend7101432012-11-14 13:42:25 -08001547uint32_t audio_track_cblk_t::stepUser(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001548{
Glenn Kastend7101432012-11-14 13:42:25 -08001549 ALOGV("stepuser %08x %08x %d", user, server, stepCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001550
Marco Nelissena1472d92012-03-30 14:36:54 -07001551 uint32_t u = user;
Glenn Kastend7101432012-11-14 13:42:25 -08001552 u += stepCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001553 // Ensure that user is never ahead of server for AudioRecord
Glenn Kastenba850982012-11-06 16:15:41 -08001554 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001555 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1556 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1557 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1558 }
Glenn Kasten90548972011-12-13 11:45:07 -08001559 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001560 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001561 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001562 }
1563
Glenn Kastend7101432012-11-14 13:42:25 -08001564 if (u >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001565 // common case, user didn't just wrap
Glenn Kastend7101432012-11-14 13:42:25 -08001566 if (u - frameCount >= userBase ) {
1567 userBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001568 }
Glenn Kastend7101432012-11-14 13:42:25 -08001569 } else if (u >= userBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001570 // user just wrapped
Glenn Kastend7101432012-11-14 13:42:25 -08001571 userBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001572 }
1573
Glenn Kasten90548972011-12-13 11:45:07 -08001574 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001575
1576 // Clear flow control error condition as new data has been written/read to/from buffer.
Glenn Kastend12b0332012-11-05 13:38:15 -08001577 if (flags & CBLK_UNDERRUN) {
1578 android_atomic_and(~CBLK_UNDERRUN, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001579 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001580
1581 return u;
1582}
1583
Glenn Kastend7101432012-11-14 13:42:25 -08001584bool audio_track_cblk_t::stepServer(size_t stepCount, size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001585{
Glenn Kastend7101432012-11-14 13:42:25 -08001586 ALOGV("stepserver %08x %08x %d", user, server, stepCount);
Marco Nelissena1472d92012-03-30 14:36:54 -07001587
Eric Laurent38ccae22011-03-28 18:37:07 -07001588 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001589 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001590 return false;
1591 }
1592
Glenn Kasten90548972011-12-13 11:45:07 -08001593 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001594 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001595
Glenn Kastend7101432012-11-14 13:42:25 -08001596 s += stepCount;
Glenn Kastenba850982012-11-06 16:15:41 -08001597 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001598 // Mark that we have read the first buffer so that next time stepUser() is called
1599 // we switch to normal obtainBuffer() timeout period
1600 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001601 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001602 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603 // It is possible that we receive a flush()
1604 // while the mixer is processing a block: in this case,
1605 // stepServer() is called After the flush() has reset u & s and
1606 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001607 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001608 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001609 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001610 }
1611 }
1612
1613 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001614 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001615 s = loopStart;
1616 if (--loopCount == 0) {
1617 loopEnd = UINT_MAX;
1618 loopStart = UINT_MAX;
1619 }
1620 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001621
Glenn Kastend7101432012-11-14 13:42:25 -08001622 if (s >= frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001623 // common case, server didn't just wrap
Glenn Kastend7101432012-11-14 13:42:25 -08001624 if (s - frameCount >= serverBase ) {
1625 serverBase += frameCount;
Marco Nelissena1472d92012-03-30 14:36:54 -07001626 }
Glenn Kastend7101432012-11-14 13:42:25 -08001627 } else if (s >= serverBase + frameCount) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001628 // server just wrapped
Glenn Kastend7101432012-11-14 13:42:25 -08001629 serverBase += frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001630 }
1631
Glenn Kasten90548972011-12-13 11:45:07 -08001632 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001633
Glenn Kastend12b0332012-11-05 13:38:15 -08001634 if (!(flags & CBLK_INVALID)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001635 cv.signal();
1636 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001637 lock.unlock();
1638 return true;
1639}
1640
Glenn Kasten5aab59a2012-11-12 07:58:20 -08001641void* audio_track_cblk_t::buffer(void *buffers, size_t frameSize, uint32_t offset) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001642{
Glenn Kasten90548972011-12-13 11:45:07 -08001643 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001644}
1645
Glenn Kastend7101432012-11-14 13:42:25 -08001646uint32_t audio_track_cblk_t::framesAvailable(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001647{
1648 Mutex::Autolock _l(lock);
Glenn Kastend7101432012-11-14 13:42:25 -08001649 return framesAvailable_l(frameCount, isOut);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001650}
1651
Glenn Kastend7101432012-11-14 13:42:25 -08001652uint32_t audio_track_cblk_t::framesAvailable_l(size_t frameCount, bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653{
Glenn Kasten90548972011-12-13 11:45:07 -08001654 uint32_t u = user;
1655 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001656
Glenn Kastenba850982012-11-06 16:15:41 -08001657 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001658 uint32_t limit = (s < loopStart) ? s : loopStart;
1659 return limit + frameCount - u;
1660 } else {
1661 return frameCount + u - s;
1662 }
1663}
1664
Glenn Kastenba850982012-11-06 16:15:41 -08001665uint32_t audio_track_cblk_t::framesReady(bool isOut)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001666{
Glenn Kasten90548972011-12-13 11:45:07 -08001667 uint32_t u = user;
1668 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001669
Glenn Kastenba850982012-11-06 16:15:41 -08001670 if (isOut) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001671 if (u < loopEnd) {
1672 return u - s;
1673 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001674 // do not block on mutex shared with client on AudioFlinger side
1675 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001676 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001677 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001678 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001679 uint32_t frames = UINT_MAX;
1680 if (loopCount >= 0) {
1681 frames = (loopEnd - loopStart)*loopCount + u - s;
1682 }
1683 lock.unlock();
1684 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001685 }
1686 } else {
1687 return s - u;
1688 }
1689}
1690
Eric Laurent38ccae22011-03-28 18:37:07 -07001691bool audio_track_cblk_t::tryLock()
1692{
1693 // the code below simulates lock-with-timeout
1694 // we MUST do this to protect the AudioFlinger server
1695 // as this lock is shared with the client.
1696 status_t err;
1697
1698 err = lock.tryLock();
1699 if (err == -EBUSY) { // just wait a bit
1700 usleep(1000);
1701 err = lock.tryLock();
1702 }
1703 if (err != NO_ERROR) {
1704 // probably, the client just died.
1705 return false;
1706 }
1707 return true;
1708}
1709
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001710// -------------------------------------------------------------------------
1711
1712}; // namespace android