blob: 00f46404b18b780d33ee1f3c06f2fdc64d3f7e48 [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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080022#include <sys/resource.h>
Glenn Kasten9f80dd22012-12-18 15:57:32 -080023#include <audio_utils/primitives.h>
24#include <binder/IPCThreadState.h>
25#include <media/AudioTrack.h>
26#include <utils/Log.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <private/media/AudioTrackShared.h>
Glenn Kasten1ab85ec2013-05-31 09:18:43 -070028#include <media/IAudioFlinger.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080029
Glenn Kasten9f80dd22012-12-18 15:57:32 -080030#define WAIT_PERIOD_MS 10
Glenn Kasten511754b2012-01-11 09:52:19 -080031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080033// ---------------------------------------------------------------------------
34
35// static
36status_t AudioTrack::getMinFrameCount(
Glenn Kastene33054e2012-11-14 12:54:39 -080037 size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080038 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080039 uint32_t sampleRate)
40{
Glenn Kastend65d73c2012-06-22 17:21:07 -070041 if (frameCount == NULL) {
42 return BAD_VALUE;
43 }
Glenn Kasten04cd0182012-06-25 11:49:27 -070044
45 // default to 0 in case of error
46 *frameCount = 0;
47
Glenn Kastene0fa4672012-04-24 14:35:14 -070048 // FIXME merge with similar code in createTrack_l(), except we're missing
49 // some information here that is available in createTrack_l():
50 // audio_io_handle_t output
51 // audio_format_t format
52 // audio_channel_mask_t channelMask
53 // audio_output_flags_t flags
Glenn Kasten3b16c762012-11-14 08:44:39 -080054 uint32_t afSampleRate;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080055 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
56 return NO_INIT;
57 }
Glenn Kastene33054e2012-11-14 12:54:39 -080058 size_t afFrameCount;
Chia-chi Yeh33005a92010-06-16 06:33:13 +080059 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
60 return NO_INIT;
61 }
62 uint32_t afLatency;
63 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
64 return NO_INIT;
65 }
66
67 // Ensure that buffer depth covers at least audio hardware latency
68 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -080069 if (minBufCount < 2) {
70 minBufCount = 2;
71 }
Chia-chi Yeh33005a92010-06-16 06:33:13 +080072
73 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070074 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080075 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
76 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080077 return NO_ERROR;
78}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080079
80// ---------------------------------------------------------------------------
81
82AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070083 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -080084 mIsTimed(false),
85 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -080086 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080087{
88}
89
90AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -080091 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -080093 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -070094 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070096 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097 callback_t cbf,
98 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -070099 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800100 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000101 transfer_type transferType,
102 const audio_offload_info_t *offloadInfo)
Glenn Kasten87913512011-06-22 16:15:25 -0700103 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800104 mIsTimed(false),
105 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800106 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700108 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700109 frameCount, flags, cbf, user, notificationFrames,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000110 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111}
112
Andreas Huberc8139852012-01-18 10:51:55 -0800113AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800114 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800116 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700117 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700119 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800120 callback_t cbf,
121 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700122 int notificationFrames,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800123 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000124 transfer_type transferType,
125 const audio_offload_info_t *offloadInfo)
Glenn Kasten87913512011-06-22 16:15:25 -0700126 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800127 mIsTimed(false),
128 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800129 mPreviousSchedulingGroup(SP_DEFAULT)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800130{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700131 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800132 0 /*frameCount*/, flags, cbf, user, notificationFrames,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000133 sharedBuffer, false /*threadCanCallJava*/, sessionId, transferType, offloadInfo);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134}
135
136AudioTrack::~AudioTrack()
137{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 if (mStatus == NO_ERROR) {
139 // Make sure that callback function exits in the case where
140 // it is looping on buffer full condition in obtainBuffer().
141 // Otherwise the callback thread will never exit.
142 stop();
143 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800144 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 mAudioTrackThread->requestExitAndWait();
146 mAudioTrackThread.clear();
147 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800148 if (mAudioTrack != 0) {
149 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
150 mAudioTrack.clear();
151 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 IPCThreadState::self()->flushCommands();
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700153 AudioSystem::releaseAudioSessionId(mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 }
155}
156
157status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800158 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800160 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700161 audio_channel_mask_t channelMask,
Glenn Kastene33054e2012-11-14 12:54:39 -0800162 int frameCountInt,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700163 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 callback_t cbf,
165 void* user,
166 int notificationFrames,
167 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700168 bool threadCanCallJava,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800169 int sessionId,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000170 transfer_type transferType,
171 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800173 switch (transferType) {
174 case TRANSFER_DEFAULT:
175 if (sharedBuffer != 0) {
176 transferType = TRANSFER_SHARED;
177 } else if (cbf == NULL || threadCanCallJava) {
178 transferType = TRANSFER_SYNC;
179 } else {
180 transferType = TRANSFER_CALLBACK;
181 }
182 break;
183 case TRANSFER_CALLBACK:
184 if (cbf == NULL || sharedBuffer != 0) {
185 ALOGE("Transfer type TRANSFER_CALLBACK but cbf == NULL || sharedBuffer != 0");
186 return BAD_VALUE;
187 }
188 break;
189 case TRANSFER_OBTAIN:
190 case TRANSFER_SYNC:
191 if (sharedBuffer != 0) {
192 ALOGE("Transfer type TRANSFER_OBTAIN but sharedBuffer != 0");
193 return BAD_VALUE;
194 }
195 break;
196 case TRANSFER_SHARED:
197 if (sharedBuffer == 0) {
198 ALOGE("Transfer type TRANSFER_SHARED but sharedBuffer == 0");
199 return BAD_VALUE;
200 }
201 break;
202 default:
203 ALOGE("Invalid transfer type %d", transferType);
204 return BAD_VALUE;
205 }
206 mTransfer = transferType;
207
Glenn Kastene33054e2012-11-14 12:54:39 -0800208 // FIXME "int" here is legacy and will be replaced by size_t later
209 if (frameCountInt < 0) {
210 ALOGE("Invalid frame count %d", frameCountInt);
211 return BAD_VALUE;
212 }
213 size_t frameCount = frameCountInt;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800214
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700215 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(),
216 sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217
Glenn Kastene33054e2012-11-14 12:54:39 -0800218 ALOGV("set() streamType %d frameCount %u flags %04x", streamType, frameCount, flags);
Eric Laurent1a9ed112012-03-20 18:36:01 -0700219
Eric Laurent1703cdf2011-03-07 14:52:59 -0800220 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800221
Eric Laurent1dd70b92009-04-21 07:56:33 -0700222 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000223 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 return INVALID_OPERATION;
225 }
226
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700228 if (streamType == AUDIO_STREAM_DEFAULT) {
229 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700231
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232 if (sampleRate == 0) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800233 uint32_t afSampleRate;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700234 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
235 return NO_INIT;
236 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237 sampleRate = afSampleRate;
238 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800239 mSampleRate = sampleRate;
Glenn Kastenea7939a2012-03-14 12:56:26 -0700240
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800242 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700243 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700245 if (channelMask == 0) {
246 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 }
248
249 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700250 if (!audio_is_valid_format(format)) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800251 ALOGE("Invalid format %d", format);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 return BAD_VALUE;
253 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254
Glenn Kastene0fa4672012-04-24 14:35:14 -0700255 // AudioFlinger does not currently support 8-bit data in shared memory
256 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
257 ALOGE("8-bit data in shared memory is not supported");
258 return BAD_VALUE;
259 }
260
Eric Laurentc2f1f072009-07-17 12:17:14 -0700261 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700263 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800264 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700265 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700266 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700267 // only allow deep buffering for music stream type
268 if (streamType != AUDIO_STREAM_MUSIC) {
269 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
270 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700271
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700272 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700273 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700274 return BAD_VALUE;
275 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800276 mChannelMask = channelMask;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700277 uint32_t channelCount = popcount(channelMask);
Glenn Kastena42ff002012-11-14 12:47:55 -0800278 mChannelCount = channelCount;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700279
Glenn Kastene3aa6592012-12-04 12:22:46 -0800280 if (audio_is_linear_pcm(format)) {
281 mFrameSize = channelCount * audio_bytes_per_sample(format);
282 mFrameSizeAF = channelCount * sizeof(int16_t);
283 } else {
284 mFrameSize = sizeof(uint8_t);
285 mFrameSizeAF = sizeof(uint8_t);
286 }
287
Dima Zavinfce7a472011-04-19 22:30:36 -0700288 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800289 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800290 sampleRate, format, channelMask,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000291 flags,
292 offloadInfo);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700293
294 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000295 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296 return BAD_VALUE;
297 }
298
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800299 mVolume[LEFT] = 1.0f;
300 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800301 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700302 mFrameCount = frameCount;
Glenn Kastenb6037442012-11-14 13:42:25 -0800303 mReqFrameCount = frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700304 mNotificationFramesReq = notificationFrames;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800305 mNotificationFramesAct = 0;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700306 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700307 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700308 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700309 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700310
Glenn Kastena997e7a2012-08-07 09:44:19 -0700311 if (cbf != NULL) {
Eric Laurent896adcd2012-09-13 11:18:23 -0700312 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
Glenn Kastena997e7a2012-08-07 09:44:19 -0700313 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
314 }
315
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800316 // create the IAudioTrack
Eric Laurent1703cdf2011-03-07 14:52:59 -0800317 status_t status = createTrack_l(streamType,
318 sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800319 format,
Eric Laurent1703cdf2011-03-07 14:52:59 -0800320 frameCount,
321 flags,
322 sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800323 output,
324 0 /*epoch*/);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800325
Glenn Kastena997e7a2012-08-07 09:44:19 -0700326 if (status != NO_ERROR) {
327 if (mAudioTrackThread != 0) {
328 mAudioTrackThread->requestExit();
329 mAudioTrackThread.clear();
330 }
331 return status;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700332 }
333
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 mStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800336 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800337 mSharedBuffer = sharedBuffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800338 mState = STATE_STOPPED;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800339 mUserData = user;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800340 mLoopPeriod = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800341 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700342 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 mNewPosition = 0;
344 mUpdatePeriod = 0;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700345 AudioSystem::acquireAudioSessionId(mSessionId);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800346 mSequence = 1;
347 mObservedSequence = mSequence;
348 mInUnderrun = false;
349
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800350 return NO_ERROR;
351}
352
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353// -------------------------------------------------------------------------
354
355void AudioTrack::start()
356{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800357 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800358 if (mState == STATE_ACTIVE) {
359 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 }
361
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800362 mInUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800364 State previousState = mState;
365 mState = STATE_ACTIVE;
366 if (previousState == STATE_STOPPED || previousState == STATE_FLUSHED) {
367 // reset current position as seen by client to 0
368 mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
369 }
370 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
371 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->flags);
372
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 sp<AudioTrackThread> t = mAudioTrackThread;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800374 if (t != 0) {
375 t->resume();
376 } else {
377 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
378 get_sched_policy(0, &mPreviousSchedulingGroup);
379 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
380 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800382 status_t status = NO_ERROR;
383 if (!(flags & CBLK_INVALID)) {
384 status = mAudioTrack->start();
385 if (status == DEAD_OBJECT) {
386 flags |= CBLK_INVALID;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800388 }
389 if (flags & CBLK_INVALID) {
390 status = restoreTrack_l("start");
391 }
392
393 if (status != NO_ERROR) {
394 ALOGE("start() status %d", status);
395 mState = previousState;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800397 t->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800398 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700399 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700400 set_sched_policy(0, mPreviousSchedulingGroup);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 }
402 }
403
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800404 // FIXME discarding status
405}
406
407void AudioTrack::stop()
408{
409 AutoMutex lock(mLock);
410 // FIXME pause then stop should not be a nop
411 if (mState != STATE_ACTIVE) {
412 return;
413 }
414
415 mState = STATE_STOPPED;
416 mProxy->interrupt();
417 mAudioTrack->stop();
418 // the playback head position will reset to 0, so if a marker is set, we need
419 // to activate it again
420 mMarkerReached = false;
421#if 0
422 // Force flush if a shared buffer is used otherwise audioflinger
423 // will not stop before end of buffer is reached.
424 // It may be needed to make sure that we stop playback, likely in case looping is on.
425 if (mSharedBuffer != 0) {
426 flush_l();
427 }
428#endif
429 sp<AudioTrackThread> t = mAudioTrackThread;
430 if (t != 0) {
431 t->pause();
432 } else {
433 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
434 set_sched_policy(0, mPreviousSchedulingGroup);
435 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436}
437
438bool AudioTrack::stopped() const
439{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800440 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800441 return mState != STATE_ACTIVE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442}
443
444void AudioTrack::flush()
445{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800446 if (mSharedBuffer != 0) {
447 return;
Glenn Kasten4bae3642012-11-30 13:41:12 -0800448 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800449 AutoMutex lock(mLock);
450 if (mState == STATE_ACTIVE || mState == STATE_FLUSHED) {
451 return;
452 }
453 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800454}
455
Eric Laurent1703cdf2011-03-07 14:52:59 -0800456void AudioTrack::flush_l()
457{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800458 ALOG_ASSERT(mState != STATE_ACTIVE);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700459
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700460 // clear playback marker and periodic update counter
461 mMarkerPosition = 0;
462 mMarkerReached = false;
463 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700464
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800465 mState = STATE_FLUSHED;
466 mProxy->flush();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800467 mAudioTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468}
469
470void AudioTrack::pause()
471{
Eric Laurentf5aafb22010-11-18 08:40:16 -0800472 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800473 if (mState != STATE_ACTIVE) {
474 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800476 mState = STATE_PAUSED;
477 mProxy->interrupt();
478 mAudioTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479}
480
Eric Laurentbe916aa2010-06-01 23:49:17 -0700481status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800483 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700484 return BAD_VALUE;
485 }
486
Eric Laurent1703cdf2011-03-07 14:52:59 -0800487 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 mVolume[LEFT] = left;
489 mVolume[RIGHT] = right;
490
Glenn Kastene3aa6592012-12-04 12:22:46 -0800491 mProxy->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700492
493 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494}
495
Glenn Kastenb1c09932012-02-27 16:21:04 -0800496status_t AudioTrack::setVolume(float volume)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497{
Glenn Kastenb1c09932012-02-27 16:21:04 -0800498 return setVolume(volume, volume);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700499}
500
Eric Laurent2beeb502010-07-16 07:43:46 -0700501status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700502{
Glenn Kasten05632a52012-01-03 14:22:33 -0800503 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700504 return BAD_VALUE;
505 }
506
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800507 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700508 mSendLevel = level;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800509 mProxy->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700510
511 return NO_ERROR;
512}
513
Glenn Kastena5224f32012-01-04 12:41:44 -0800514void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700515{
516 if (level != NULL) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800517 *level = mSendLevel;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700518 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519}
520
Glenn Kasten3b16c762012-11-14 08:44:39 -0800521status_t AudioTrack::setSampleRate(uint32_t rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522{
John Grossman4ff14ba2012-02-08 16:37:41 -0800523 if (mIsTimed) {
524 return INVALID_OPERATION;
525 }
526
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800527 uint32_t afSamplingRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700529 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530 }
531 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Glenn Kastend65d73c2012-06-22 17:21:07 -0700532 if (rate == 0 || rate > afSamplingRate*2 ) {
533 return BAD_VALUE;
534 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535
Eric Laurent1703cdf2011-03-07 14:52:59 -0800536 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800537 mSampleRate = rate;
538 mProxy->setSampleRate(rate);
539
Eric Laurent57326622009-07-07 07:10:45 -0700540 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541}
542
Glenn Kastena5224f32012-01-04 12:41:44 -0800543uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544{
John Grossman4ff14ba2012-02-08 16:37:41 -0800545 if (mIsTimed) {
Glenn Kasten3b16c762012-11-14 08:44:39 -0800546 return 0;
John Grossman4ff14ba2012-02-08 16:37:41 -0800547 }
548
Eric Laurent1703cdf2011-03-07 14:52:59 -0800549 AutoMutex lock(mLock);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800550 return mSampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800551}
552
553status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
554{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800555 if (mSharedBuffer == 0 || mIsTimed) {
556 return INVALID_OPERATION;
557 }
558
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559 if (loopCount == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800560 ;
561 } else if (loopCount >= -1 && loopStart < loopEnd && loopEnd <= mFrameCount &&
562 loopEnd - loopStart >= MIN_LOOP) {
563 ;
564 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 return BAD_VALUE;
566 }
567
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800568 AutoMutex lock(mLock);
569 // See setPosition() regarding setting parameters such as loop points or position while active
570 if (mState == STATE_ACTIVE) {
571 return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700572 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800573 setLoop_l(loopStart, loopEnd, loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 return NO_ERROR;
575}
576
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800577void AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
578{
579 // FIXME If setting a loop also sets position to start of loop, then
580 // this is correct. Otherwise it should be removed.
581 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
582 mLoopPeriod = loopCount != 0 ? loopEnd - loopStart : 0;
583 mStaticProxy->setLoop(loopStart, loopEnd, loopCount);
584}
585
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586status_t AudioTrack::setMarkerPosition(uint32_t marker)
587{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700588 if (mCbf == NULL) {
589 return INVALID_OPERATION;
590 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800592 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700594 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595
596 return NO_ERROR;
597}
598
Glenn Kastena5224f32012-01-04 12:41:44 -0800599status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800600{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700601 if (marker == NULL) {
602 return BAD_VALUE;
603 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800605 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 *marker = mMarkerPosition;
607
608 return NO_ERROR;
609}
610
611status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
612{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700613 if (mCbf == NULL) {
614 return INVALID_OPERATION;
615 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800616
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800617 AutoMutex lock(mLock);
618 mNewPosition = mProxy->getPosition() + updatePeriod;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800619 mUpdatePeriod = updatePeriod;
620
621 return NO_ERROR;
622}
623
Glenn Kastena5224f32012-01-04 12:41:44 -0800624status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700626 if (updatePeriod == NULL) {
627 return BAD_VALUE;
628 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800630 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 *updatePeriod = mUpdatePeriod;
632
633 return NO_ERROR;
634}
635
636status_t AudioTrack::setPosition(uint32_t position)
637{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800638 if (mSharedBuffer == 0 || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700639 return INVALID_OPERATION;
640 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800641 if (position > mFrameCount) {
642 return BAD_VALUE;
643 }
John Grossman4ff14ba2012-02-08 16:37:41 -0800644
Eric Laurent1703cdf2011-03-07 14:52:59 -0800645 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800646 // Currently we require that the player is inactive before setting parameters such as position
647 // or loop points. Otherwise, there could be a race condition: the application could read the
648 // current position, compute a new position or loop parameters, and then set that position or
649 // loop parameters but it would do the "wrong" thing since the position has continued to advance
650 // in the mean time. If we ever provide a sequencer in server, we could allow a way for the app
651 // to specify how it wants to handle such scenarios.
652 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700653 return INVALID_OPERATION;
654 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800655 mNewPosition = mProxy->getPosition() + mUpdatePeriod;
656 mLoopPeriod = 0;
657 // FIXME Check whether loops and setting position are incompatible in old code.
658 // If we use setLoop for both purposes we lose the capability to set the position while looping.
659 mStaticProxy->setLoop(position, mFrameCount, 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700660
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661 return NO_ERROR;
662}
663
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800664status_t AudioTrack::getPosition(uint32_t *position) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665{
Glenn Kastend65d73c2012-06-22 17:21:07 -0700666 if (position == NULL) {
667 return BAD_VALUE;
668 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800669
Eric Laurent1703cdf2011-03-07 14:52:59 -0800670 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800671 // IAudioTrack::stop() isn't synchronous; we don't know when presentation completes
672 *position = (mState == STATE_STOPPED || mState == STATE_FLUSHED) ? 0 :
673 mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674
675 return NO_ERROR;
676}
677
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800678status_t AudioTrack::getBufferPosition(size_t *position)
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800679{
680 if (mSharedBuffer == 0 || mIsTimed) {
681 return INVALID_OPERATION;
682 }
683 if (position == NULL) {
684 return BAD_VALUE;
685 }
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800686
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800687 AutoMutex lock(mLock);
688 *position = mStaticProxy->getBufferPosition();
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800689 return NO_ERROR;
690}
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800691
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692status_t AudioTrack::reload()
693{
Glenn Kasten083d1c12012-11-30 15:00:36 -0800694 if (mSharedBuffer == 0 || mIsTimed) {
695 return INVALID_OPERATION;
696 }
697
Eric Laurent1703cdf2011-03-07 14:52:59 -0800698 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800699 // See setPosition() regarding setting parameters such as loop points or position while active
700 if (mState == STATE_ACTIVE) {
Glenn Kastend65d73c2012-06-22 17:21:07 -0700701 return INVALID_OPERATION;
702 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800703 mNewPosition = mUpdatePeriod;
704 mLoopPeriod = 0;
705 // FIXME The new code cannot reload while keeping a loop specified.
706 // Need to check how the old code handled this, and whether it's a significant change.
707 mStaticProxy->setLoop(0, mFrameCount, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 return NO_ERROR;
709}
710
Eric Laurentc2f1f072009-07-17 12:17:14 -0700711audio_io_handle_t AudioTrack::getOutput()
712{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800713 AutoMutex lock(mLock);
714 return getOutput_l();
715}
716
717// must be called with mLock held
718audio_io_handle_t AudioTrack::getOutput_l()
719{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800720 return AudioSystem::getOutput(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -0800721 mSampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700722}
723
Eric Laurentbe916aa2010-06-01 23:49:17 -0700724status_t AudioTrack::attachAuxEffect(int effectId)
725{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800726 AutoMutex lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -0700727 status_t status = mAudioTrack->attachAuxEffect(effectId);
728 if (status == NO_ERROR) {
729 mAuxEffectId = effectId;
730 }
731 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700732}
733
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800734// -------------------------------------------------------------------------
735
Eric Laurent1703cdf2011-03-07 14:52:59 -0800736// must be called with mLock held
737status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800738 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800739 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800740 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800741 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700742 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800743 const sp<IMemory>& sharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800744 audio_io_handle_t output,
745 size_t epoch)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800746{
747 status_t status;
748 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
749 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700750 ALOGE("Could not get audioflinger");
751 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800752 }
753
Eric Laurentd1b449a2010-05-14 03:26:45 -0700754 uint32_t afLatency;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800755 if ((status = AudioSystem::getLatency(output, streamType, &afLatency)) != NO_ERROR) {
756 ALOGE("getLatency(%d) failed status %d", output, status);
Eric Laurentd1b449a2010-05-14 03:26:45 -0700757 return NO_INIT;
758 }
759
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700760 // Client decides whether the track is TIMED (see below), but can only express a preference
761 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700762 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700763 // either of these use cases:
764 // use case 1: shared buffer
765 (sharedBuffer != 0) ||
766 // use case 2: callback handler
767 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800768 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700769 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700770 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700771 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700772 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700773 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700774
Eric Laurentd1b449a2010-05-14 03:26:45 -0700775 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700776
Dima Zavinfce7a472011-04-19 22:30:36 -0700777 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700778
Eric Laurentd1b449a2010-05-14 03:26:45 -0700779 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700780 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700781 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700782 } else if (frameCount == 0) {
Glenn Kastene33054e2012-11-14 12:54:39 -0800783 size_t afFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800784 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
785 if (status != NO_ERROR) {
786 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType,
787 status);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700788 return NO_INIT;
789 }
790 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700791 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700792
793 } else if (sharedBuffer != 0) {
794
Glenn Kastena42ff002012-11-14 12:47:55 -0800795 // Ensure that buffer alignment matches channel count
Glenn Kastene0fa4672012-04-24 14:35:14 -0700796 // 8-bit data in shared memory is not currently supported by AudioFlinger
797 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
Glenn Kastena42ff002012-11-14 12:47:55 -0800798 if (mChannelCount > 1) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700799 // More than 2 channels does not require stronger alignment than stereo
800 alignment <<= 1;
801 }
Glenn Kastena42ff002012-11-14 12:47:55 -0800802 if (((size_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
803 ALOGE("Invalid buffer alignment: address %p, channel count %u",
804 sharedBuffer->pointer(), mChannelCount);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700805 return BAD_VALUE;
806 }
807
808 // When initializing a shared buffer AudioTrack via constructors,
809 // there's no frameCount parameter.
810 // But when initializing a shared buffer AudioTrack via set(),
811 // there _is_ a frameCount parameter. We silently ignore it.
Glenn Kastena42ff002012-11-14 12:47:55 -0800812 frameCount = sharedBuffer->size()/mChannelCount/sizeof(int16_t);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700813
814 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
815
816 // FIXME move these calculations and associated checks to server
Glenn Kasten3b16c762012-11-14 08:44:39 -0800817 uint32_t afSampleRate;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800818 status = AudioSystem::getSamplingRate(output, streamType, &afSampleRate);
819 if (status != NO_ERROR) {
820 ALOGE("getSamplingRate(output=%d, streamType=%d) status %d", output, streamType,
821 status);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700822 return NO_INIT;
823 }
Glenn Kastene33054e2012-11-14 12:54:39 -0800824 size_t afFrameCount;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800825 status = AudioSystem::getFrameCount(output, streamType, &afFrameCount);
826 if (status != NO_ERROR) {
827 ALOGE("getFrameCount(output=%d, streamType=%d) status %d", output, streamType, status);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700828 return NO_INIT;
829 }
830
Eric Laurentd1b449a2010-05-14 03:26:45 -0700831 // Ensure that buffer depth covers at least audio hardware latency
832 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
Glenn Kastenbb6f0a02013-06-03 15:00:29 -0700833 ALOGV("afFrameCount=%d, minBufCount=%d, afSampleRate=%u, afLatency=%d",
834 afFrameCount, minBufCount, afSampleRate, afLatency);
835 if (minBufCount <= 2) {
836 minBufCount = sampleRate == afSampleRate ? 2 : 3;
Glenn Kasten7c027242012-12-26 14:43:16 -0800837 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700838
Glenn Kastene33054e2012-11-14 12:54:39 -0800839 size_t minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
840 ALOGV("minFrameCount: %u, afFrameCount=%d, minBufCount=%d, sampleRate=%u, afSampleRate=%u"
Glenn Kasten3acbd052012-02-28 10:39:56 -0800841 ", afLatency=%d",
842 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700843
844 if (frameCount == 0) {
845 frameCount = minFrameCount;
846 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700847 // Make sure that application is notified with sufficient margin
848 // before underrun
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800849 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700850 mNotificationFramesAct = frameCount/2;
851 }
852 if (frameCount < minFrameCount) {
853 // not ALOGW because it happens all the time when playing key clicks over A2DP
854 ALOGV("Minimum buffer size corrected from %d to %d",
855 frameCount, minFrameCount);
856 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800857 }
Eric Laurentd1b449a2010-05-14 03:26:45 -0700858
Glenn Kastene0fa4672012-04-24 14:35:14 -0700859 } else {
860 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -0700861 }
862
Glenn Kastena075db42012-03-06 11:22:44 -0800863 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
864 if (mIsTimed) {
865 trackFlags |= IAudioFlinger::TRACK_TIMED;
866 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800867
868 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700869 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700870 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800871 if (mAudioTrackThread != 0) {
872 tid = mAudioTrackThread->getTid();
873 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700874 }
875
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800876 sp<IAudioTrack> track = audioFlinger->createTrack(streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800877 sampleRate,
Glenn Kasten60a83922012-06-21 12:56:37 -0700878 // AudioFlinger only sees 16-bit PCM
879 format == AUDIO_FORMAT_PCM_8_BIT ?
880 AUDIO_FORMAT_PCM_16_BIT : format,
Glenn Kastena42ff002012-11-14 12:47:55 -0800881 mChannelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800882 frameCount,
Glenn Kastene0b07172012-11-06 15:03:34 -0800883 &trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800884 sharedBuffer,
885 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -0800886 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700887 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800888 &status);
889
890 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000891 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800892 return status;
893 }
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700894 sp<IMemory> iMem = track->getCblk();
895 if (iMem == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000896 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800897 return NO_INIT;
898 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800899 if (mAudioTrack != 0) {
900 mAudioTrack->asBinder()->unlinkToDeath(mDeathNotifier, this);
901 mDeathNotifier.clear();
902 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800903 mAudioTrack = track;
Glenn Kastend2c38fc2012-11-01 14:58:02 -0700904 mCblkMemory = iMem;
905 audio_track_cblk_t* cblk = static_cast<audio_track_cblk_t*>(iMem->pointer());
906 mCblk = cblk;
Glenn Kastenb6037442012-11-14 13:42:25 -0800907 size_t temp = cblk->frameCount_;
908 if (temp < frameCount || (frameCount == 0 && temp == 0)) {
909 // In current design, AudioTrack client checks and ensures frame count validity before
910 // passing it to AudioFlinger so AudioFlinger should not return a different value except
911 // for fast track as it uses a special method of assigning frame count.
912 ALOGW("Requested frameCount %u but received frameCount %u", frameCount, temp);
913 }
914 frameCount = temp;
Glenn Kastena07f17c2013-04-23 12:39:37 -0700915 mAwaitBoost = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800916 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kastene0b07172012-11-06 15:03:34 -0800917 if (trackFlags & IAudioFlinger::TRACK_FAST) {
Glenn Kastenb6037442012-11-14 13:42:25 -0800918 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", frameCount);
Glenn Kastena07f17c2013-04-23 12:39:37 -0700919 mAwaitBoost = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800920 if (sharedBuffer == 0) {
921 // double-buffering is not required for fast tracks, due to tighter scheduling
922 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount) {
923 mNotificationFramesAct = frameCount;
924 }
925 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800926 } else {
Glenn Kastenb6037442012-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 Kasten9f80dd22012-12-18 15:57:32 -0800931 if (sharedBuffer == 0) {
932 if (mNotificationFramesAct == 0 || mNotificationFramesAct > frameCount/2) {
933 mNotificationFramesAct = frameCount/2;
934 }
935 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700936 }
Glenn Kasten3acbd052012-02-28 10:39:56 -0800937 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800938 mRefreshRemaining = true;
939
940 // Starting address of buffers in shared memory. If there is a shared buffer, buffers
941 // is the value of pointer() for the shared buffer, otherwise buffers points
942 // immediately after the control block. This address is for the mapping within client
943 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
944 void* buffers;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800945 if (sharedBuffer == 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800946 buffers = (char*)cblk + sizeof(audio_track_cblk_t);
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800947 } else {
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800948 buffers = sharedBuffer->pointer();
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800949 }
950
Eric Laurent2beeb502010-07-16 07:43:46 -0700951 mAudioTrack->attachAuxEffect(mAuxEffectId);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700952 // FIXME don't believe this lie
Glenn Kastenb6037442012-11-14 13:42:25 -0800953 mLatency = afLatency + (1000*frameCount) / sampleRate;
954 mFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700955 // If IAudioTrack is re-created, don't let the requested frameCount
956 // decrease. This can confuse clients that cache frameCount().
Glenn Kastenb6037442012-11-14 13:42:25 -0800957 if (frameCount > mReqFrameCount) {
958 mReqFrameCount = frameCount;
Glenn Kasten093000f2012-05-03 09:35:36 -0700959 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800960
961 // update proxy
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800962 if (sharedBuffer == 0) {
963 mStaticProxy.clear();
964 mProxy = new AudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
965 } else {
966 mStaticProxy = new StaticAudioTrackClientProxy(cblk, buffers, frameCount, mFrameSizeAF);
967 mProxy = mStaticProxy;
968 }
Glenn Kastene3aa6592012-12-04 12:22:46 -0800969 mProxy->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) |
970 uint16_t(mVolume[LEFT] * 0x1000));
971 mProxy->setSendLevel(mSendLevel);
972 mProxy->setSampleRate(mSampleRate);
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800973 mProxy->setEpoch(epoch);
974 mProxy->setMinimum(mNotificationFramesAct);
975
976 mDeathNotifier = new DeathNotifier(this);
977 mAudioTrack->asBinder()->linkToDeath(mDeathNotifier, this);
Glenn Kastene3aa6592012-12-04 12:22:46 -0800978
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800979 return NO_ERROR;
980}
981
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
983{
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800984 if (audioBuffer == NULL) {
985 return BAD_VALUE;
Eric Laurent9b7d9502011-03-21 11:49:00 -0700986 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800987 if (mTransfer != TRANSFER_OBTAIN) {
988 audioBuffer->frameCount = 0;
989 audioBuffer->size = 0;
990 audioBuffer->raw = NULL;
991 return INVALID_OPERATION;
992 }
Eric Laurent9b7d9502011-03-21 11:49:00 -0700993
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800994 const struct timespec *requested;
995 if (waitCount == -1) {
996 requested = &ClientProxy::kForever;
997 } else if (waitCount == 0) {
998 requested = &ClientProxy::kNonBlocking;
999 } else if (waitCount > 0) {
1000 long long ms = WAIT_PERIOD_MS * (long long) waitCount;
1001 struct timespec timeout;
1002 timeout.tv_sec = ms / 1000;
1003 timeout.tv_nsec = (int) (ms % 1000) * 1000000;
1004 requested = &timeout;
1005 } else {
1006 ALOGE("%s invalid waitCount %d", __func__, waitCount);
1007 requested = NULL;
1008 }
1009 return obtainBuffer(audioBuffer, requested);
1010}
Eric Laurent1703cdf2011-03-07 14:52:59 -08001011
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001012status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
1013 struct timespec *elapsed, size_t *nonContig)
1014{
1015 // previous and new IAudioTrack sequence numbers are used to detect track re-creation
1016 uint32_t oldSequence = 0;
1017 uint32_t newSequence;
1018
1019 Proxy::Buffer buffer;
1020 status_t status = NO_ERROR;
1021
1022 static const int32_t kMaxTries = 5;
1023 int32_t tryCounter = kMaxTries;
1024
1025 do {
1026 // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1027 // keep them from going away if another thread re-creates the track during obtainBuffer()
1028 sp<AudioTrackClientProxy> proxy;
1029 sp<IMemory> iMem;
1030
1031 { // start of lock scope
1032 AutoMutex lock(mLock);
1033
1034 newSequence = mSequence;
1035 // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1036 if (status == DEAD_OBJECT) {
1037 // re-create track, unless someone else has already done so
1038 if (newSequence == oldSequence) {
1039 status = restoreTrack_l("obtainBuffer");
1040 if (status != NO_ERROR) {
1041 break;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001042 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001043 }
1044 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001045 oldSequence = newSequence;
1046
1047 // Keep the extra references
1048 proxy = mProxy;
1049 iMem = mCblkMemory;
1050
1051 // Non-blocking if track is stopped or paused
1052 if (mState != STATE_ACTIVE) {
1053 requested = &ClientProxy::kNonBlocking;
1054 }
1055
1056 } // end of lock scope
1057
1058 buffer.mFrameCount = audioBuffer->frameCount;
1059 // FIXME starts the requested timeout and elapsed over from scratch
1060 status = proxy->obtainBuffer(&buffer, requested, elapsed);
1061
1062 } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1063
1064 audioBuffer->frameCount = buffer.mFrameCount;
1065 audioBuffer->size = buffer.mFrameCount * mFrameSizeAF;
1066 audioBuffer->raw = buffer.mRaw;
1067 if (nonContig != NULL) {
1068 *nonContig = buffer.mNonContig;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001069 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001070 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001071}
1072
1073void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1074{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001075 if (mTransfer == TRANSFER_SHARED) {
1076 return;
1077 }
1078
1079 size_t stepCount = audioBuffer->size / mFrameSizeAF;
1080 if (stepCount == 0) {
1081 return;
1082 }
1083
1084 Proxy::Buffer buffer;
1085 buffer.mFrameCount = stepCount;
1086 buffer.mRaw = audioBuffer->raw;
Glenn Kastene3aa6592012-12-04 12:22:46 -08001087
Eric Laurent1703cdf2011-03-07 14:52:59 -08001088 AutoMutex lock(mLock);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001089 mInUnderrun = false;
1090 mProxy->releaseBuffer(&buffer);
1091
1092 // restart track if it was disabled by audioflinger due to previous underrun
1093 if (mState == STATE_ACTIVE) {
1094 audio_track_cblk_t* cblk = mCblk;
1095 if (android_atomic_and(~CBLK_DISABLED, &cblk->flags) & CBLK_DISABLED) {
1096 ALOGW("releaseBuffer() track %p name=%#x disabled due to previous underrun, restarting",
1097 this, cblk->mName);
1098 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001099 mAudioTrack->start();
1100 }
1101 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001102}
1103
1104// -------------------------------------------------------------------------
1105
1106ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1107{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001108 if (mTransfer != TRANSFER_SYNC || mIsTimed) {
Glenn Kastend65d73c2012-06-22 17:21:07 -07001109 return INVALID_OPERATION;
1110 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001111
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001112 if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001113 // Sanity-check: user is most-likely passing an error code, and it would
1114 // make the return value ambiguous (actualSize vs error).
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001115 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)", buffer, userSize, userSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116 return BAD_VALUE;
1117 }
1118
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001119 size_t written = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001120 Buffer audioBuffer;
1121
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001122 while (userSize >= mFrameSize) {
1123 audioBuffer.frameCount = userSize / mFrameSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001124
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001125 status_t err = obtainBuffer(&audioBuffer, &ClientProxy::kForever);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001126 if (err < 0) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001127 if (written > 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128 break;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001129 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001130 return ssize_t(err);
1131 }
1132
1133 size_t toWrite;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001134 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001135 // Divide capacity by 2 to take expansion into account
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001136 toWrite = audioBuffer.size >> 1;
1137 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) buffer, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001138 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001139 toWrite = audioBuffer.size;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001140 memcpy(audioBuffer.i8, buffer, toWrite);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001141 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001142 buffer = ((const char *) buffer) + toWrite;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001143 userSize -= toWrite;
1144 written += toWrite;
1145
1146 releaseBuffer(&audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001147 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001148
1149 return written;
1150}
1151
1152// -------------------------------------------------------------------------
1153
John Grossman4ff14ba2012-02-08 16:37:41 -08001154TimedAudioTrack::TimedAudioTrack() {
1155 mIsTimed = true;
1156}
1157
1158status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1159{
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001160 AutoMutex lock(mLock);
John Grossman4ff14ba2012-02-08 16:37:41 -08001161 status_t result = UNKNOWN_ERROR;
1162
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001163#if 1
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001164 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1165 // while we are accessing the cblk
1166 sp<IAudioTrack> audioTrack = mAudioTrack;
1167 sp<IMemory> iMem = mCblkMemory;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001168#endif
Glenn Kastend5ed6e82012-11-02 13:05:14 -07001169
John Grossman4ff14ba2012-02-08 16:37:41 -08001170 // If the track is not invalid already, try to allocate a buffer. alloc
1171 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001172 // we can attempt to restore in just a bit.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001173 audio_track_cblk_t* cblk = mCblk;
1174 if (!(cblk->flags & CBLK_INVALID)) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001175 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1176 if (result == DEAD_OBJECT) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001177 android_atomic_or(CBLK_INVALID, &cblk->flags);
John Grossman4ff14ba2012-02-08 16:37:41 -08001178 }
1179 }
1180
1181 // If the track is invalid at this point, attempt to restore it. and try the
1182 // allocation one more time.
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001183 if (cblk->flags & CBLK_INVALID) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001184 result = restoreTrack_l("allocateTimedBuffer");
John Grossman4ff14ba2012-02-08 16:37:41 -08001185
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001186 if (result == NO_ERROR) {
John Grossman4ff14ba2012-02-08 16:37:41 -08001187 result = mAudioTrack->allocateTimedBuffer(size, buffer);
Glenn Kastend65d73c2012-06-22 17:21:07 -07001188 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001189 }
1190
1191 return result;
1192}
1193
1194status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1195 int64_t pts)
1196{
Eric Laurentdf839842012-05-31 14:27:14 -07001197 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1198 {
1199 AutoMutex lock(mLock);
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001200 audio_track_cblk_t* cblk = mCblk;
Eric Laurentdf839842012-05-31 14:27:14 -07001201 // restart track if it was disabled by audioflinger due to previous underrun
1202 if (buffer->size() != 0 && status == NO_ERROR &&
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001203 (mState == STATE_ACTIVE) && (cblk->flags & CBLK_DISABLED)) {
Glenn Kastend2c38fc2012-11-01 14:58:02 -07001204 android_atomic_and(~CBLK_DISABLED, &cblk->flags);
Eric Laurentdf839842012-05-31 14:27:14 -07001205 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001206 // FIXME ignoring status
Eric Laurentdf839842012-05-31 14:27:14 -07001207 mAudioTrack->start();
1208 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001209 }
Eric Laurentdf839842012-05-31 14:27:14 -07001210 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001211}
1212
1213status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1214 TargetTimeline target)
1215{
1216 return mAudioTrack->setMediaTimeTransform(xform, target);
1217}
1218
1219// -------------------------------------------------------------------------
1220
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001221nsecs_t AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001223 mLock.lock();
Glenn Kastena07f17c2013-04-23 12:39:37 -07001224 if (mAwaitBoost) {
1225 mAwaitBoost = false;
1226 mLock.unlock();
1227 static const int32_t kMaxTries = 5;
1228 int32_t tryCounter = kMaxTries;
1229 uint32_t pollUs = 10000;
1230 do {
1231 int policy = sched_getscheduler(0);
1232 if (policy == SCHED_FIFO || policy == SCHED_RR) {
1233 break;
1234 }
1235 usleep(pollUs);
1236 pollUs <<= 1;
1237 } while (tryCounter-- > 0);
1238 if (tryCounter < 0) {
1239 ALOGE("did not receive expected priority boost on time");
1240 }
Glenn Kastenb0dfd462013-07-10 16:52:47 -07001241 // Run again immediately
1242 return 0;
Glenn Kastena07f17c2013-04-23 12:39:37 -07001243 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001244
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001245 // Can only reference mCblk while locked
1246 int32_t flags = android_atomic_and(
1247 ~(CBLK_UNDERRUN | CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL | CBLK_BUFFER_END), &mCblk->flags);
Glenn Kastena47f3162012-11-07 10:13:08 -08001248
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001249 // Check for track invalidation
1250 if (flags & CBLK_INVALID) {
1251 (void) restoreTrack_l("processAudioBuffer");
1252 mLock.unlock();
1253 // Run again immediately, but with a new IAudioTrack
1254 return 0;
1255 }
1256
1257 bool active = mState == STATE_ACTIVE;
1258
1259 // Manage underrun callback, must be done under lock to avoid race with releaseBuffer()
1260 bool newUnderrun = false;
1261 if (flags & CBLK_UNDERRUN) {
1262#if 0
1263 // Currently in shared buffer mode, when the server reaches the end of buffer,
1264 // the track stays active in continuous underrun state. It's up to the application
1265 // to pause or stop the track, or set the position to a new offset within buffer.
1266 // This was some experimental code to auto-pause on underrun. Keeping it here
1267 // in "if 0" so we can re-visit this if we add a real sequencer for shared memory content.
1268 if (mTransfer == TRANSFER_SHARED) {
1269 mState = STATE_PAUSED;
1270 active = false;
1271 }
1272#endif
1273 if (!mInUnderrun) {
1274 mInUnderrun = true;
1275 newUnderrun = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276 }
1277 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001278
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001279 // Get current position of server
1280 size_t position = mProxy->getPosition();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001281
1282 // Manage marker callback
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001283 bool markerReached = false;
1284 size_t markerPosition = mMarkerPosition;
1285 // FIXME fails for wraparound, need 64 bits
1286 if (!mMarkerReached && (markerPosition > 0) && (position >= markerPosition)) {
1287 mMarkerReached = markerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288 }
1289
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001290 // Determine number of new position callback(s) that will be needed, while locked
1291 size_t newPosCount = 0;
1292 size_t newPosition = mNewPosition;
1293 size_t updatePeriod = mUpdatePeriod;
1294 // FIXME fails for wraparound, need 64 bits
1295 if (updatePeriod > 0 && position >= newPosition) {
1296 newPosCount = ((position - newPosition) / updatePeriod) + 1;
1297 mNewPosition += updatePeriod * newPosCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298 }
1299
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001300 // Cache other fields that will be needed soon
1301 uint32_t loopPeriod = mLoopPeriod;
1302 uint32_t sampleRate = mSampleRate;
1303 size_t notificationFrames = mNotificationFramesAct;
1304 if (mRefreshRemaining) {
1305 mRefreshRemaining = false;
1306 mRemainingFrames = notificationFrames;
1307 mRetryOnPartialBuffer = false;
1308 }
1309 size_t misalignment = mProxy->getMisalignment();
1310 int32_t sequence = mSequence;
1311
1312 // These fields don't need to be cached, because they are assigned only by set():
1313 // mTransfer, mCbf, mUserData, mFormat, mFrameSize, mFrameSizeAF, mFlags
1314 // mFlags is also assigned by createTrack_l(), but not the bit we care about.
1315
1316 mLock.unlock();
1317
1318 // perform callbacks while unlocked
1319 if (newUnderrun) {
1320 mCbf(EVENT_UNDERRUN, mUserData, NULL);
1321 }
1322 // FIXME we will miss loops if loop cycle was signaled several times since last call
1323 // to processAudioBuffer()
1324 if (flags & (CBLK_LOOP_CYCLE | CBLK_LOOP_FINAL)) {
1325 mCbf(EVENT_LOOP_END, mUserData, NULL);
1326 }
1327 if (flags & CBLK_BUFFER_END) {
1328 mCbf(EVENT_BUFFER_END, mUserData, NULL);
1329 }
1330 if (markerReached) {
1331 mCbf(EVENT_MARKER, mUserData, &markerPosition);
1332 }
1333 while (newPosCount > 0) {
1334 size_t temp = newPosition;
1335 mCbf(EVENT_NEW_POS, mUserData, &temp);
1336 newPosition += updatePeriod;
1337 newPosCount--;
1338 }
1339 if (mObservedSequence != sequence) {
1340 mObservedSequence = sequence;
1341 mCbf(EVENT_NEW_IAUDIOTRACK, mUserData, NULL);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001342 }
1343
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001344 // if inactive, then don't run me again until re-started
1345 if (!active) {
1346 return NS_INACTIVE;
Eric Laurent2267ba12011-09-07 11:13:23 -07001347 }
1348
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001349 // Compute the estimated time until the next timed event (position, markers, loops)
1350 // FIXME only for non-compressed audio
1351 uint32_t minFrames = ~0;
1352 if (!markerReached && position < markerPosition) {
1353 minFrames = markerPosition - position;
1354 }
1355 if (loopPeriod > 0 && loopPeriod < minFrames) {
1356 minFrames = loopPeriod;
1357 }
1358 if (updatePeriod > 0 && updatePeriod < minFrames) {
1359 minFrames = updatePeriod;
1360 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001361
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001362 // If > 0, poll periodically to recover from a stuck server. A good value is 2.
1363 static const uint32_t kPoll = 0;
1364 if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1365 minFrames = kPoll * notificationFrames;
1366 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001367
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001368 // Convert frame units to time units
1369 nsecs_t ns = NS_WHENEVER;
1370 if (minFrames != (uint32_t) ~0) {
1371 // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1372 static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1373 ns = ((minFrames * 1000000000LL) / sampleRate) + kFudgeNs;
1374 }
1375
1376 // If not supplying data by EVENT_MORE_DATA, then we're done
1377 if (mTransfer != TRANSFER_CALLBACK) {
1378 return ns;
1379 }
1380
1381 struct timespec timeout;
1382 const struct timespec *requested = &ClientProxy::kForever;
1383 if (ns != NS_WHENEVER) {
1384 timeout.tv_sec = ns / 1000000000LL;
1385 timeout.tv_nsec = ns % 1000000000LL;
1386 ALOGV("timeout %ld.%03d", timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1387 requested = &timeout;
1388 }
1389
1390 while (mRemainingFrames > 0) {
1391
1392 Buffer audioBuffer;
1393 audioBuffer.frameCount = mRemainingFrames;
1394 size_t nonContig;
1395 status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1396 LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1397 "obtainBuffer() err=%d frameCount=%u", err, audioBuffer.frameCount);
1398 requested = &ClientProxy::kNonBlocking;
1399 size_t avail = audioBuffer.frameCount + nonContig;
1400 ALOGV("obtainBuffer(%u) returned %u = %u + %u",
1401 mRemainingFrames, avail, audioBuffer.frameCount, nonContig);
1402 if (err != NO_ERROR) {
1403 if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR) {
1404 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001406 ALOGE("Error %d obtaining an audio buffer, giving up.", err);
1407 return NS_NEVER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001408 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001409
1410 if (mRetryOnPartialBuffer) {
1411 mRetryOnPartialBuffer = false;
1412 if (avail < mRemainingFrames) {
1413 int64_t myns = ((mRemainingFrames - avail) * 1100000000LL) / sampleRate;
1414 if (ns < 0 || myns < ns) {
1415 ns = myns;
1416 }
1417 return ns;
1418 }
Glenn Kastend65d73c2012-06-22 17:21:07 -07001419 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420
1421 // Divide buffer size by 2 to take into account the expansion
1422 // due to 8 to 16 bit conversion: the callback must fill only half
1423 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001424 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001425 audioBuffer.size >>= 1;
1426 }
1427
1428 size_t reqSize = audioBuffer.size;
1429 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001430 size_t writtenSize = audioBuffer.size;
1431 size_t writtenFrames = writtenSize / mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432
1433 // Sanity check on returned size
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001434 if (ssize_t(writtenSize) < 0 || writtenSize > reqSize) {
1435 ALOGE("EVENT_MORE_DATA requested %u bytes but callback returned %d bytes",
1436 reqSize, (int) writtenSize);
1437 return NS_NEVER;
1438 }
1439
1440 if (writtenSize == 0) {
The Android Open Source Project8555d082009-03-05 14:34:35 -08001441 // The callback is done filling buffers
1442 // Keep this thread going to handle timed events and
1443 // still try to get more data in intervals of WAIT_PERIOD_MS
1444 // but don't just loop and block the CPU, so wait
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001445 return WAIT_PERIOD_MS * 1000000LL;
Glenn Kastend65d73c2012-06-22 17:21:07 -07001446 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001448 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001449 // 8 to 16 bit conversion, note that source and destination are the same address
1450 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001451 audioBuffer.size <<= 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 }
1453
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001454 size_t releasedFrames = audioBuffer.size / mFrameSizeAF;
1455 audioBuffer.frameCount = releasedFrames;
1456 mRemainingFrames -= releasedFrames;
1457 if (misalignment >= releasedFrames) {
1458 misalignment -= releasedFrames;
1459 } else {
1460 misalignment = 0;
1461 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001462
1463 releaseBuffer(&audioBuffer);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001464
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001465 // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1466 // if callback doesn't like to accept the full chunk
1467 if (writtenSize < reqSize) {
1468 continue;
1469 }
1470
1471 // There could be enough non-contiguous frames available to satisfy the remaining request
1472 if (mRemainingFrames <= nonContig) {
1473 continue;
1474 }
1475
1476#if 0
1477 // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1478 // sum <= notificationFrames. It replaces that series by at most two EVENT_MORE_DATA
1479 // that total to a sum == notificationFrames.
1480 if (0 < misalignment && misalignment <= mRemainingFrames) {
1481 mRemainingFrames = misalignment;
1482 return (mRemainingFrames * 1100000000LL) / sampleRate;
1483 }
1484#endif
1485
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001487 mRemainingFrames = notificationFrames;
1488 mRetryOnPartialBuffer = true;
1489
1490 // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1491 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492}
1493
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001494status_t AudioTrack::restoreTrack_l(const char *from)
Eric Laurent1703cdf2011-03-07 14:52:59 -08001495{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001496 ALOGW("dead IAudioTrack, creating a new one from %s()", from);
1497 ++mSequence;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001498 status_t result;
1499
Glenn Kastena47f3162012-11-07 10:13:08 -08001500 // refresh the audio configuration cache in this process to make sure we get new
1501 // output parameters in getOutput_l() and createTrack_l()
1502 AudioSystem::clearAudioConfigCache();
Eric Laurent9f6530f2011-08-30 10:18:54 -07001503
Glenn Kastena47f3162012-11-07 10:13:08 -08001504 // if the new IAudioTrack is created, createTrack_l() will modify the
1505 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1506 // It will also delete the strong references on previous IAudioTrack and IMemory
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001507 size_t position = mProxy->getPosition();
1508 mNewPosition = position + mUpdatePeriod;
1509 size_t bufferPosition = mStaticProxy != NULL ? mStaticProxy->getBufferPosition() : 0;
Glenn Kastena47f3162012-11-07 10:13:08 -08001510 result = createTrack_l(mStreamType,
Glenn Kastene3aa6592012-12-04 12:22:46 -08001511 mSampleRate,
Glenn Kastena47f3162012-11-07 10:13:08 -08001512 mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001513 mReqFrameCount, // so that frame count never goes down
Glenn Kastena47f3162012-11-07 10:13:08 -08001514 mFlags,
1515 mSharedBuffer,
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001516 getOutput_l(),
1517 position /*epoch*/);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001518
Glenn Kastena47f3162012-11-07 10:13:08 -08001519 if (result == NO_ERROR) {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001520 // continue playback from last known position, but
1521 // don't attempt to restore loop after invalidation; it's difficult and not worthwhile
1522 if (mStaticProxy != NULL) {
1523 mLoopPeriod = 0;
1524 mStaticProxy->setLoop(bufferPosition, mFrameCount, 0);
1525 }
1526 // FIXME How do we simulate the fact that all frames present in the buffer at the time of
1527 // track destruction have been played? This is critical for SoundPool implementation
1528 // This must be broken, and needs to be tested/debugged.
1529#if 0
Glenn Kastena47f3162012-11-07 10:13:08 -08001530 // restore write index and set other indexes to reflect empty buffer status
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001531 if (!strcmp(from, "start")) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001532 // Make sure that a client relying on callback events indicating underrun or
1533 // the actual amount of audio frames played (e.g SoundPool) receives them.
1534 if (mSharedBuffer == 0) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001535 // restart playback even if buffer is not completely filled.
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001536 android_atomic_or(CBLK_FORCEREADY, &mCblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001537 }
1538 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001539#endif
1540 if (mState == STATE_ACTIVE) {
Glenn Kastena47f3162012-11-07 10:13:08 -08001541 result = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001542 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001543 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001544 if (result != NO_ERROR) {
1545 ALOGW("restoreTrack_l() failed status %d", result);
1546 mState = STATE_STOPPED;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001547 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001548
1549 return result;
1550}
1551
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001552status_t AudioTrack::setParameters(const String8& keyValuePairs)
1553{
1554 AutoMutex lock(mLock);
1555 if (mAudioTrack != 0) {
1556 return mAudioTrack->setParameters(keyValuePairs);
1557 } else {
1558 return NO_INIT;
1559 }
1560}
1561
1562String8 AudioTrack::getParameters(const String8& keys)
1563{
1564 return String8::empty();
1565}
1566
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1568{
1569
1570 const size_t SIZE = 256;
1571 char buffer[SIZE];
1572 String8 result;
1573
1574 result.append(" AudioTrack::dump\n");
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001575 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType,
1576 mVolume[0], mVolume[1]);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577 result.append(buffer);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001578 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat,
Glenn Kastenb6037442012-11-14 13:42:25 -08001579 mChannelCount, mFrameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001580 result.append(buffer);
Glenn Kastene3aa6592012-12-04 12:22:46 -08001581 snprintf(buffer, 255, " sample rate(%u), status(%d)\n", mSampleRate, mStatus);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 result.append(buffer);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001583 snprintf(buffer, 255, " state(%d), latency (%d)\n", mState, mLatency);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001584 result.append(buffer);
1585 ::write(fd, result.string(), result.size());
1586 return NO_ERROR;
1587}
1588
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001589uint32_t AudioTrack::getUnderrunFrames() const
1590{
1591 AutoMutex lock(mLock);
1592 return mProxy->getUnderrunFrames();
1593}
1594
1595// =========================================================================
1596
1597void AudioTrack::DeathNotifier::binderDied(const wp<IBinder>& who)
1598{
1599 sp<AudioTrack> audioTrack = mAudioTrack.promote();
1600 if (audioTrack != 0) {
1601 AutoMutex lock(audioTrack->mLock);
1602 audioTrack->mProxy->binderDied();
1603 }
1604}
1605
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606// =========================================================================
1607
1608AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001609 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true), mResumeLatch(false)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001610{
1611}
1612
1613AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001614{
1615}
1616
1617bool AudioTrack::AudioTrackThread::threadLoop()
1618{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001619 {
1620 AutoMutex _l(mMyLock);
1621 if (mPaused) {
1622 mMyCond.wait(mMyLock);
1623 // caller will check for exitPending()
1624 return true;
1625 }
1626 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001627 nsecs_t ns = mReceiver.processAudioBuffer(this);
1628 switch (ns) {
1629 case 0:
1630 return true;
1631 case NS_WHENEVER:
1632 sleep(1);
1633 return true;
1634 case NS_INACTIVE:
1635 pauseConditional();
1636 return true;
1637 case NS_NEVER:
1638 return false;
1639 default:
1640 LOG_ALWAYS_FATAL_IF(ns < 0, "processAudioBuffer() returned %lld", ns);
1641 struct timespec req;
1642 req.tv_sec = ns / 1000000000LL;
1643 req.tv_nsec = ns % 1000000000LL;
1644 nanosleep(&req, NULL /*rem*/);
1645 return true;
Glenn Kastenca8b2802012-04-23 13:58:16 -07001646 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001647}
1648
Glenn Kasten3acbd052012-02-28 10:39:56 -08001649void AudioTrack::AudioTrackThread::requestExit()
1650{
1651 // must be in this order to avoid a race condition
1652 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001653 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001654}
1655
1656void AudioTrack::AudioTrackThread::pause()
1657{
1658 AutoMutex _l(mMyLock);
1659 mPaused = true;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001660 mResumeLatch = false;
1661}
1662
1663void AudioTrack::AudioTrackThread::pauseConditional()
1664{
1665 AutoMutex _l(mMyLock);
1666 if (mResumeLatch) {
1667 mResumeLatch = false;
1668 } else {
1669 mPaused = true;
1670 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001671}
1672
1673void AudioTrack::AudioTrackThread::resume()
1674{
1675 AutoMutex _l(mMyLock);
1676 if (mPaused) {
1677 mPaused = false;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001678 mResumeLatch = false;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001679 mMyCond.signal();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001680 } else {
1681 mResumeLatch = true;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001682 }
1683}
1684
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001685}; // namespace android