blob: a2172c9d6f89aa17b147fe3e3f460693b7845829 [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**
vivek mehta2192bc92012-12-04 11:46:08 -08005** Copyright (c) 2012, The Linux Foundation. All rights reserved.
6** Not a Contribution, Apache license notifications and license are retained
7** for attribution purposes only.
8
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08009** Licensed under the Apache License, Version 2.0 (the "License");
10** you may not use this file except in compliance with the License.
11** You may obtain a copy of the License at
12**
13** http://www.apache.org/licenses/LICENSE-2.0
14**
15** Unless required by applicable law or agreed to in writing, software
16** distributed under the License is distributed on an "AS IS" BASIS,
17** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18** See the License for the specific language governing permissions and
19** limitations under the License.
20*/
21
22
23//#define LOG_NDEBUG 0
24#define LOG_TAG "AudioTrack"
25
26#include <stdint.h>
27#include <sys/types.h>
28#include <limits.h>
29
30#include <sched.h>
31#include <sys/resource.h>
32
33#include <private/media/AudioTrackShared.h>
34
35#include <media/AudioSystem.h>
36#include <media/AudioTrack.h>
37
38#include <utils/Log.h>
Mathias Agopian75624082009-05-19 19:08:10 -070039#include <binder/Parcel.h>
40#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041#include <utils/Timers.h>
Eric Laurent38ccae22011-03-28 18:37:07 -070042#include <utils/Atomic.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043
Dima Zavinfce7a472011-04-19 22:30:36 -070044#include <cutils/bitops.h>
Glenn Kastenf6b16782011-12-15 09:51:17 -080045#include <cutils/compiler.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070046
Dima Zavin64760242011-05-11 14:15:23 -070047#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070048#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070049
Glenn Kasten511754b2012-01-11 09:52:19 -080050#include <audio_utils/primitives.h>
51
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052namespace android {
Chia-chi Yeh33005a92010-06-16 06:33:13 +080053// ---------------------------------------------------------------------------
54
55// static
56status_t AudioTrack::getMinFrameCount(
57 int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -080058 audio_stream_type_t streamType,
Chia-chi Yeh33005a92010-06-16 06:33:13 +080059 uint32_t sampleRate)
60{
Glenn Kasten04cd0182012-06-25 11:49:27 -070061 if (frameCount == NULL) return BAD_VALUE;
62
63 // default to 0 in case of error
64 *frameCount = 0;
65
Glenn Kastene0fa4672012-04-24 14:35:14 -070066 // FIXME merge with similar code in createTrack_l(), except we're missing
67 // some information here that is available in createTrack_l():
68 // audio_io_handle_t output
69 // audio_format_t format
70 // audio_channel_mask_t channelMask
71 // audio_output_flags_t flags
Chia-chi Yeh33005a92010-06-16 06:33:13 +080072 int afSampleRate;
73 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
74 return NO_INIT;
75 }
76 int afFrameCount;
77 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
78 return NO_INIT;
79 }
80 uint32_t afLatency;
81 if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) {
82 return NO_INIT;
83 }
84
85 // Ensure that buffer depth covers at least audio hardware latency
86 uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
87 if (minBufCount < 2) minBufCount = 2;
88
89 *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount :
Glenn Kastene53b9ea2012-03-12 16:29:55 -070090 afFrameCount * minBufCount * sampleRate / afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -080091 ALOGV("getMinFrameCount=%d: afFrameCount=%d, minBufCount=%d, afSampleRate=%d, afLatency=%d",
92 *frameCount, afFrameCount, minBufCount, afSampleRate, afLatency);
Chia-chi Yeh33005a92010-06-16 06:33:13 +080093 return NO_ERROR;
94}
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080095
96// ---------------------------------------------------------------------------
97
98AudioTrack::AudioTrack()
Glenn Kasten87913512011-06-22 16:15:25 -070099 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800100 mIsTimed(false),
101 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700102 mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800103#ifdef QCOM_HARDWARE
104 ,mAudioFlinger(NULL),
105 mObserver(NULL)
106#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107{
108}
109
110AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800111 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800112 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800113 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700114 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700116 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117 callback_t cbf,
118 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700119 int notificationFrames,
120 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700121 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800122 mIsTimed(false),
123 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700124 mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800125#ifdef QCOM_HARDWARE
126 ,mAudioFlinger(NULL),
127 mObserver(NULL)
128#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800129{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700130 mStatus = set(streamType, sampleRate, format, channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700131 frameCount, flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800132 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800133}
134
Glenn Kasten17a736c2012-02-14 08:52:15 -0800135// DEPRECATED
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136AudioTrack::AudioTrack(
Andreas Huberc8139852012-01-18 10:51:55 -0800137 int streamType,
138 uint32_t sampleRate,
139 int format,
140 int channelMask,
141 int frameCount,
142 uint32_t flags,
143 callback_t cbf,
144 void* user,
145 int notificationFrames,
146 int sessionId)
147 : mStatus(NO_INIT),
Glenn Kasten18868c52012-03-07 09:15:37 -0800148 mIsTimed(false),
Glenn Kastena6364332012-04-19 09:35:04 -0700149 mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800150#ifdef QCOM_HARDWARE
151 ,mAudioFlinger(NULL),
152 mObserver(NULL)
153#endif
Andreas Huberc8139852012-01-18 10:51:55 -0800154{
Glenn Kasten28b76b32012-07-03 17:24:41 -0700155 mStatus = set((audio_stream_type_t)streamType, sampleRate, (audio_format_t)format,
156 (audio_channel_mask_t) channelMask,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700157 frameCount, (audio_output_flags_t)flags, cbf, user, notificationFrames,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800158 0 /*sharedBuffer*/, false /*threadCanCallJava*/, sessionId);
Andreas Huberc8139852012-01-18 10:51:55 -0800159}
160
161AudioTrack::AudioTrack(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800162 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800164 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700165 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 const sp<IMemory>& sharedBuffer,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700167 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 callback_t cbf,
169 void* user,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700170 int notificationFrames,
171 int sessionId)
Glenn Kasten87913512011-06-22 16:15:25 -0700172 : mStatus(NO_INIT),
John Grossman4ff14ba2012-02-08 16:37:41 -0800173 mIsTimed(false),
174 mPreviousPriority(ANDROID_PRIORITY_NORMAL),
Glenn Kastena6364332012-04-19 09:35:04 -0700175 mPreviousSchedulingGroup(SP_DEFAULT)
vivek mehta2192bc92012-12-04 11:46:08 -0800176#ifdef QCOM_HARDWARE
177 ,mAudioFlinger(NULL),
178 mObserver(NULL)
179#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180{
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700181 mStatus = set(streamType, sampleRate, format, channelMask,
Glenn Kasten17a736c2012-02-14 08:52:15 -0800182 0 /*frameCount*/, flags, cbf, user, notificationFrames,
183 sharedBuffer, false /*threadCanCallJava*/, sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184}
185
186AudioTrack::~AudioTrack()
187{
Steve Block3856b092011-10-20 11:56:00 +0100188 ALOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189
190 if (mStatus == NO_ERROR) {
191 // Make sure that callback function exits in the case where
192 // it is looping on buffer full condition in obtainBuffer().
193 // Otherwise the callback thread will never exit.
194 stop();
195 if (mAudioTrackThread != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800196 mAudioTrackThread->requestExit(); // see comment in AudioTrack.h
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197 mAudioTrackThread->requestExitAndWait();
198 mAudioTrackThread.clear();
199 }
vivek mehta2192bc92012-12-04 11:46:08 -0800200#ifdef QCOM_HARDWARE
201 if (mAudioTrack != 0) {
202 mAudioTrack.clear();
203 AudioSystem::releaseAudioSessionId(mSessionId);
204 }
205
206 if (mDirectTrack != 0) {
207 mDirectTrack.clear();
208 }
209#else
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210 mAudioTrack.clear();
vivek mehta2192bc92012-12-04 11:46:08 -0800211#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 IPCThreadState::self()->flushCommands();
vivek mehta2192bc92012-12-04 11:46:08 -0800213#ifndef QCOM_HARDWARE
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700214 AudioSystem::releaseAudioSessionId(mSessionId);
vivek mehta2192bc92012-12-04 11:46:08 -0800215#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 }
217}
218
219status_t AudioTrack::set(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800220 audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800222 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700223 audio_channel_mask_t channelMask,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700225 audio_output_flags_t flags,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800226 callback_t cbf,
227 void* user,
228 int notificationFrames,
229 const sp<IMemory>& sharedBuffer,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700230 bool threadCanCallJava,
231 int sessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232{
233
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235
Eric Laurent1a9ed112012-03-20 18:36:01 -0700236 ALOGV("set() streamType %d frameCount %d flags %04x", streamType, frameCount, flags);
237
Eric Laurent1703cdf2011-03-07 14:52:59 -0800238 AutoMutex lock(mLock);
Eric Laurent1dd70b92009-04-21 07:56:33 -0700239 if (mAudioTrack != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000240 ALOGE("Track already in use");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241 return INVALID_OPERATION;
242 }
243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 // handle default values first.
Dima Zavinfce7a472011-04-19 22:30:36 -0700245 if (streamType == AUDIO_STREAM_DEFAULT) {
246 streamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 if (sampleRate == 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700250 int afSampleRate;
251 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
252 return NO_INIT;
253 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254 sampleRate = afSampleRate;
255 }
Glenn Kastenea7939a2012-03-14 12:56:26 -0700256
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257 // these below should probably come from the audioFlinger too...
Glenn Kastene1c39622012-01-04 09:36:37 -0800258 if (format == AUDIO_FORMAT_DEFAULT) {
Dima Zavinfce7a472011-04-19 22:30:36 -0700259 format = AUDIO_FORMAT_PCM_16_BIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700261 if (channelMask == 0) {
262 channelMask = AUDIO_CHANNEL_OUT_STEREO;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263 }
264
265 // validate parameters
Dima Zavinfce7a472011-04-19 22:30:36 -0700266 if (!audio_is_valid_format(format)) {
Steve Block29357bc2012-01-06 19:20:56 +0000267 ALOGE("Invalid format");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 return BAD_VALUE;
269 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700270
Glenn Kastene0fa4672012-04-24 14:35:14 -0700271 // AudioFlinger does not currently support 8-bit data in shared memory
272 if (format == AUDIO_FORMAT_PCM_8_BIT && sharedBuffer != 0) {
273 ALOGE("8-bit data in shared memory is not supported");
274 return BAD_VALUE;
275 }
276
Eric Laurentc2f1f072009-07-17 12:17:14 -0700277 // force direct flag if format is not linear PCM
Dima Zavinfce7a472011-04-19 22:30:36 -0700278 if (!audio_is_linear_pcm(format)) {
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700279 flags = (audio_output_flags_t)
Glenn Kasten3acbd052012-02-28 10:39:56 -0800280 // FIXME why can't we allow direct AND fast?
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700281 ((flags | AUDIO_OUTPUT_FLAG_DIRECT) & ~AUDIO_OUTPUT_FLAG_FAST);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700282 }
Eric Laurent1948eb32012-04-13 16:50:19 -0700283 // only allow deep buffering for music stream type
284 if (streamType != AUDIO_STREAM_MUSIC) {
285 flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER);
286 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287
vivek mehta2192bc92012-12-04 11:46:08 -0800288#ifdef QCOM_ENHANCED_AUDIO
289 if ((streamType == AUDIO_STREAM_VOICE_CALL)
290 && (channelMask == AUDIO_CHANNEL_OUT_MONO)
291 && ((sampleRate == 8000 || sampleRate == 16000)))
292 {
293 ALOGD("Turn on Direct Output for VOIP RX");
294 flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_VOIP_RX|AUDIO_OUTPUT_FLAG_DIRECT);
295 }
296#endif
297
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700298 if (!audio_is_output_channel(channelMask)) {
Glenn Kasten28b76b32012-07-03 17:24:41 -0700299 ALOGE("Invalid channel mask %#x", channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700300 return BAD_VALUE;
301 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700302 uint32_t channelCount = popcount(channelMask);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700303
vivek mehta2192bc92012-12-04 11:46:08 -0800304 ALOGV("AudioTrack getOutput streamType %d, sampleRate %d, format %d, channelMask %d, flags %x",
305 streamType, sampleRate, format, channelMask, flags);
Dima Zavinfce7a472011-04-19 22:30:36 -0700306 audio_io_handle_t output = AudioSystem::getOutput(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800307 streamType,
Glenn Kastene1c39622012-01-04 09:36:37 -0800308 sampleRate, format, channelMask,
Glenn Kasten18868c52012-03-07 09:15:37 -0800309 flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700310
311 if (output == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000312 ALOGE("Could not get audio output for stream type %d", streamType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800313 return BAD_VALUE;
314 }
315
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800316 mVolume[LEFT] = 1.0f;
317 mVolume[RIGHT] = 1.0f;
Glenn Kasten05632a52012-01-03 14:22:33 -0800318 mSendLevel = 0.0f;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700319 mFrameCount = frameCount;
320 mNotificationFramesReq = notificationFrames;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700321 mSessionId = sessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700322 mAuxEffectId = 0;
Glenn Kasten093000f2012-05-03 09:35:36 -0700323 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700324 mCbf = cbf;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700325
vivek mehta2192bc92012-12-04 11:46:08 -0800326#ifdef QCOM_HARDWARE
327 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL) {
328 ALOGV("Creating Direct Track");
329 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
330 if (audioFlinger == 0) {
331 ALOGE("Could not get audioflinger");
332 return NO_INIT;
Glenn Kastena997e7a2012-08-07 09:44:19 -0700333 }
vivek mehta2192bc92012-12-04 11:46:08 -0800334 mAudioFlinger = audioFlinger;
335 status_t status = NO_ERROR;
336 mAudioDirectOutput = output;
337 mDirectTrack = audioFlinger->createDirectTrack( getpid(),
338 sampleRate,
339 channelMask,
340 mAudioDirectOutput,
341 &mSessionId,
342 this,
343 streamType,
344 &status);
345 if(status != NO_ERROR) {
346 ALOGE("createDirectTrack returned with status %d", status);
347 return status;
348 }
349 mAudioTrack = NULL;
350 mSharedBuffer = NULL;
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700351 }
vivek mehta2192bc92012-12-04 11:46:08 -0800352 else {
353#endif
354 if (cbf != NULL) {
355 mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava);
356 mAudioTrackThread->run("AudioTrack", ANDROID_PRIORITY_AUDIO, 0 /*stack*/);
357 }
358 // create the IAudioTrack
359 status_t status = createTrack_l(streamType,
360 sampleRate,
361 format,
362 channelMask,
363 frameCount,
364 flags,
365 sharedBuffer,
366 output);
Glenn Kasten5d464eb2012-06-22 17:19:53 -0700367
vivek mehta2192bc92012-12-04 11:46:08 -0800368 if (status != NO_ERROR) {
369 if (mAudioTrackThread != 0) {
370 mAudioTrackThread->requestExit();
371 mAudioTrackThread.clear();
372 }
373 return status;
374 }
375#ifdef QCOM_HARDWARE
376 AudioSystem::acquireAudioSessionId(mSessionId);
377 mAudioDirectOutput = -1;
378 mDirectTrack = NULL;
379 mSharedBuffer = sharedBuffer;
380 }
381 mUserData = user;
382#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800383 mStatus = NO_ERROR;
384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385 mStreamType = streamType;
Glenn Kastene1c39622012-01-04 09:36:37 -0800386 mFormat = format;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700387 mChannelMask = channelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 mChannelCount = channelCount;
vivek mehta2192bc92012-12-04 11:46:08 -0800389
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 mMuted = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800391 mActive = false;
vivek mehta2192bc92012-12-04 11:46:08 -0800392
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 mLoopCount = 0;
394 mMarkerPosition = 0;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700395 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800396 mNewPosition = 0;
397 mUpdatePeriod = 0;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700398 mFlushed = false;
vivek mehta2192bc92012-12-04 11:46:08 -0800399
400#ifndef QCOM_HARDWARE
401 mSharedBuffer = sharedBuffer;
402 mUserData = user;
Marco Nelissen3a34bef2011-08-02 13:33:41 -0700403 AudioSystem::acquireAudioSessionId(mSessionId);
vivek mehta2192bc92012-12-04 11:46:08 -0800404#endif
405
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700406 mRestoreStatus = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 return NO_ERROR;
408}
409
410status_t AudioTrack::initCheck() const
411{
412 return mStatus;
413}
414
415// -------------------------------------------------------------------------
416
417uint32_t AudioTrack::latency() const
418{
vivek mehta2192bc92012-12-04 11:46:08 -0800419#ifdef QCOM_HARDWARE
420 if(mAudioDirectOutput != -1) {
421 return mAudioFlinger->latency(mAudioDirectOutput);
422 }
423#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 return mLatency;
425}
426
Glenn Kastenfff6d712012-01-12 16:38:12 -0800427audio_stream_type_t AudioTrack::streamType() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428{
429 return mStreamType;
430}
431
Glenn Kastene1c39622012-01-04 09:36:37 -0800432audio_format_t AudioTrack::format() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433{
434 return mFormat;
435}
436
437int AudioTrack::channelCount() const
438{
439 return mChannelCount;
440}
441
442uint32_t AudioTrack::frameCount() const
443{
vivek mehta2192bc92012-12-04 11:46:08 -0800444#ifdef QCOM_HARDWARE
445 if(mAudioDirectOutput != -1) {
446 return mAudioFlinger->frameCount(mAudioDirectOutput);
447 }
448#endif
Eric Laurentd1b449a2010-05-14 03:26:45 -0700449 return mCblk->frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450}
451
Glenn Kastenb9980652012-01-11 09:48:27 -0800452size_t AudioTrack::frameSize() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453{
Dima Zavinfce7a472011-04-19 22:30:36 -0700454 if (audio_is_linear_pcm(mFormat)) {
Eric Laurent671a6362011-06-16 21:30:45 -0700455 return channelCount()*audio_bytes_per_sample(mFormat);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700456 } else {
457 return sizeof(uint8_t);
458 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459}
460
461sp<IMemory>& AudioTrack::sharedBuffer()
462{
463 return mSharedBuffer;
464}
465
466// -------------------------------------------------------------------------
467
468void AudioTrack::start()
469{
vivek mehta2192bc92012-12-04 11:46:08 -0800470#ifdef QCOM_HARDWARE
471 if (mDirectTrack != NULL) {
472 if(mActive == 0) {
473 mActive = 1;
474 mDirectTrack->start();
475 }
476 return;
477 }
478#endif
479
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 sp<AudioTrackThread> t = mAudioTrackThread;
481
Steve Block3856b092011-10-20 11:56:00 +0100482 ALOGV("start %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483
Eric Laurentf5aafb22010-11-18 08:40:16 -0800484 AutoMutex lock(mLock);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800485 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
486 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700487 sp<IAudioTrack> audioTrack = mAudioTrack;
488 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800489 audio_track_cblk_t* cblk = mCblk;
490
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800491 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700492 mFlushed = false;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800493 mActive = true;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800494 mNewPosition = cblk->server + mUpdatePeriod;
Eric Laurent33797ea2011-03-17 09:36:51 -0700495 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800496 cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
497 cblk->waitTimeMs = 0;
Eric Laurent38ccae22011-03-28 18:37:07 -0700498 android_atomic_and(~CBLK_DISABLED_ON, &cblk->flags);
Eric Laurent2b584242009-11-09 23:32:22 -0800499 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800500 t->resume();
Eric Laurent2b584242009-11-09 23:32:22 -0800501 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700502 mPreviousPriority = getpriority(PRIO_PROCESS, 0);
Glenn Kastena6364332012-04-19 09:35:04 -0700503 get_sched_policy(0, &mPreviousSchedulingGroup);
Glenn Kasten87913512011-06-22 16:15:25 -0700504 androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
Eric Laurent2b584242009-11-09 23:32:22 -0800505 }
506
Steve Block3856b092011-10-20 11:56:00 +0100507 ALOGV("start %p before lock cblk %p", this, mCblk);
Glenn Kastend3a9ff42012-06-21 12:11:38 -0700508 status_t status = NO_ERROR;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800509 if (!(cblk->flags & CBLK_INVALID_MSK)) {
510 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800511 ALOGV("mAudioTrack->start()");
512 status = mAudioTrack->start();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800513 cblk->lock.lock();
514 if (status == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -0700515 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent6100d2d2009-11-19 09:00:56 -0800516 }
Eric Laurent2b584242009-11-09 23:32:22 -0800517 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800518 if (cblk->flags & CBLK_INVALID_MSK) {
519 status = restoreTrack_l(cblk, true);
520 }
521 cblk->lock.unlock();
Eric Laurent2b584242009-11-09 23:32:22 -0800522 if (status != NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100523 ALOGV("start() failed");
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800524 mActive = false;
Eric Laurent2b584242009-11-09 23:32:22 -0800525 if (t != 0) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800526 t->pause();
Eric Laurent2b584242009-11-09 23:32:22 -0800527 } else {
Glenn Kasten87913512011-06-22 16:15:25 -0700528 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
Glenn Kastena6364332012-04-19 09:35:04 -0700529 set_sched_policy(0, mPreviousSchedulingGroup);
Eric Laurent2b584242009-11-09 23:32:22 -0800530 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800531 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 }
533
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534}
535
536void AudioTrack::stop()
537{
538 sp<AudioTrackThread> t = mAudioTrackThread;
539
Steve Block3856b092011-10-20 11:56:00 +0100540 ALOGV("stop %p", this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541
Eric Laurentf5aafb22010-11-18 08:40:16 -0800542 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800543 if (mActive) {
vivek mehta2192bc92012-12-04 11:46:08 -0800544#ifdef QCOM_HARDWARE
545 if(mDirectTrack != NULL) {
546 mActive = false;
547 mDirectTrack->stop();
548 } else if (mAudioTrack != NULL) {
549#endif
550 mActive = false;
551 mCblk->cv.signal();
552 mAudioTrack->stop();
553 // Cancel loops (If we are in the middle of a loop, playback
554 // would not stop until loopCount reaches 0).
555 setLoop_l(0, 0, 0);
556 // the playback head position will reset to 0, so if a marker is set, we need
557 // to activate it again
558 mMarkerReached = false;
559 // Force flush if a shared buffer is used otherwise audioflinger
560 // will not stop before end of buffer is reached.
561 if (mSharedBuffer != 0) {
562 flush_l();
563 }
564 if (t != 0) {
565 t->pause();
566 } else {
567 setpriority(PRIO_PROCESS, 0, mPreviousPriority);
568 set_sched_policy(0, mPreviousSchedulingGroup);
569 }
570#ifdef QCOM_HARDWARE
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571 }
vivek mehta2192bc92012-12-04 11:46:08 -0800572#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573 }
574
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575}
576
577bool AudioTrack::stopped() const
578{
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800579 AutoMutex lock(mLock);
580 return stopped_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581}
582
583void AudioTrack::flush()
584{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800585 AutoMutex lock(mLock);
vivek mehta2192bc92012-12-04 11:46:08 -0800586#ifdef QCOM_HARDWARE
587 if(mDirectTrack != NULL) {
588 mDirectTrack->flush();
589 } else
590#endif
591 flush_l();
Eric Laurent1703cdf2011-03-07 14:52:59 -0800592}
593
594// must be called with mLock held
595void AudioTrack::flush_l()
596{
Steve Block3856b092011-10-20 11:56:00 +0100597 ALOGV("flush");
Eric Laurentc2f1f072009-07-17 12:17:14 -0700598
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700599 // clear playback marker and periodic update counter
600 mMarkerPosition = 0;
601 mMarkerReached = false;
602 mUpdatePeriod = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700603
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 if (!mActive) {
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700605 mFlushed = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 mAudioTrack->flush();
607 // Release AudioTrack callback thread in case it was waiting for new buffers
608 // in AudioTrack::obtainBuffer()
609 mCblk->cv.signal();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610 }
611}
612
613void AudioTrack::pause()
614{
Steve Block3856b092011-10-20 11:56:00 +0100615 ALOGV("pause");
Eric Laurentf5aafb22010-11-18 08:40:16 -0800616 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800617 if (mActive) {
618 mActive = false;
vivek mehta2192bc92012-12-04 11:46:08 -0800619#ifdef QCOM_HARDWARE
620 if(mDirectTrack != NULL) {
621 ALOGV("mDirectTrack pause");
622 mDirectTrack->pause();
623 } else {
624#endif
625 mCblk->cv.signal();
626 mAudioTrack->pause();
627#ifdef QCOM_HARDWARE
628 }
629#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630 }
631}
632
633void AudioTrack::mute(bool e)
634{
vivek mehta2192bc92012-12-04 11:46:08 -0800635#ifdef QCOM_HARDWARE
636 if(mDirectTrack != NULL) {
637 mDirectTrack->mute(e);
638 } else
639#endif
640 mAudioTrack->mute(e);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 mMuted = e;
642}
643
644bool AudioTrack::muted() const
645{
646 return mMuted;
647}
648
Eric Laurentbe916aa2010-06-01 23:49:17 -0700649status_t AudioTrack::setVolume(float left, float right)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650{
Glenn Kastenf0c49502011-11-30 09:46:04 -0800651 if (left < 0.0f || left > 1.0f || right < 0.0f || right > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700652 return BAD_VALUE;
653 }
654
Eric Laurent1703cdf2011-03-07 14:52:59 -0800655 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 mVolume[LEFT] = left;
657 mVolume[RIGHT] = right;
vivek mehta2192bc92012-12-04 11:46:08 -0800658#ifdef QCOM_HARDWARE
659 if(mDirectTrack != NULL) {
660 ALOGV("mDirectTrack->setVolume(left = %f , right = %f)", left,right);
661 mDirectTrack->setVolume(left, right);
662 } else
663#endif
664 mCblk->setVolumeLR((uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000));
Eric Laurentbe916aa2010-06-01 23:49:17 -0700665
666 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667}
668
Glenn Kastena5224f32012-01-04 12:41:44 -0800669void AudioTrack::getVolume(float* left, float* right) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670{
Eric Laurentbe916aa2010-06-01 23:49:17 -0700671 if (left != NULL) {
672 *left = mVolume[LEFT];
673 }
674 if (right != NULL) {
675 *right = mVolume[RIGHT];
676 }
677}
678
Eric Laurent2beeb502010-07-16 07:43:46 -0700679status_t AudioTrack::setAuxEffectSendLevel(float level)
Eric Laurentbe916aa2010-06-01 23:49:17 -0700680{
vivek mehta2192bc92012-12-04 11:46:08 -0800681#ifdef QCOM_HARDWARE
682 if (mDirectTrack != NULL) {
683 return NO_ERROR;
684 }
685#endif
Steve Block3856b092011-10-20 11:56:00 +0100686 ALOGV("setAuxEffectSendLevel(%f)", level);
Glenn Kasten05632a52012-01-03 14:22:33 -0800687 if (level < 0.0f || level > 1.0f) {
Eric Laurentbe916aa2010-06-01 23:49:17 -0700688 return BAD_VALUE;
689 }
Eric Laurent1703cdf2011-03-07 14:52:59 -0800690 AutoMutex lock(mLock);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700691
692 mSendLevel = level;
693
Glenn Kasten05632a52012-01-03 14:22:33 -0800694 mCblk->setSendLevel(level);
Eric Laurentbe916aa2010-06-01 23:49:17 -0700695
696 return NO_ERROR;
697}
698
Glenn Kastena5224f32012-01-04 12:41:44 -0800699void AudioTrack::getAuxEffectSendLevel(float* level) const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700700{
701 if (level != NULL) {
702 *level = mSendLevel;
703 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704}
705
Eric Laurent57326622009-07-07 07:10:45 -0700706status_t AudioTrack::setSampleRate(int rate)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707{
708 int afSamplingRate;
709
John Grossman4ff14ba2012-02-08 16:37:41 -0800710 if (mIsTimed) {
711 return INVALID_OPERATION;
712 }
713
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800714 if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) {
Eric Laurent57326622009-07-07 07:10:45 -0700715 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716 }
717 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
Eric Laurent57326622009-07-07 07:10:45 -0700718 if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800719
Eric Laurent1703cdf2011-03-07 14:52:59 -0800720 AutoMutex lock(mLock);
Eric Laurent57326622009-07-07 07:10:45 -0700721 mCblk->sampleRate = rate;
722 return NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800723}
724
Glenn Kastena5224f32012-01-04 12:41:44 -0800725uint32_t AudioTrack::getSampleRate() const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800726{
John Grossman4ff14ba2012-02-08 16:37:41 -0800727 if (mIsTimed) {
728 return INVALID_OPERATION;
729 }
730
Eric Laurent1703cdf2011-03-07 14:52:59 -0800731 AutoMutex lock(mLock);
vivek mehta2192bc92012-12-04 11:46:08 -0800732#ifdef QCOM_HARDWARE
733 if(mAudioDirectOutput != -1) {
734 return mAudioFlinger->sampleRate(mAudioDirectOutput);
735 }
736#endif
Eric Laurent57326622009-07-07 07:10:45 -0700737 return mCblk->sampleRate;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738}
739
740status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount)
741{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800742 AutoMutex lock(mLock);
743 return setLoop_l(loopStart, loopEnd, loopCount);
744}
745
746// must be called with mLock held
747status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount)
748{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749 audio_track_cblk_t* cblk = mCblk;
750
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800751 Mutex::Autolock _l(cblk->lock);
752
753 if (loopCount == 0) {
754 cblk->loopStart = UINT_MAX;
755 cblk->loopEnd = UINT_MAX;
756 cblk->loopCount = 0;
757 mLoopCount = 0;
758 return NO_ERROR;
759 }
760
John Grossman4ff14ba2012-02-08 16:37:41 -0800761 if (mIsTimed) {
762 return INVALID_OPERATION;
763 }
764
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800765 if (loopStart >= loopEnd ||
Eric Laurent9b7d9502011-03-21 11:49:00 -0700766 loopEnd - loopStart > cblk->frameCount ||
767 cblk->server > loopStart) {
Steve Block29357bc2012-01-06 19:20:56 +0000768 ALOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 return BAD_VALUE;
770 }
771
Eric Laurent9b7d9502011-03-21 11:49:00 -0700772 if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) {
Steve Block29357bc2012-01-06 19:20:56 +0000773 ALOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d",
Eric Laurentd1b449a2010-05-14 03:26:45 -0700774 loopStart, loopEnd, cblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800775 return BAD_VALUE;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700776 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800777
778 cblk->loopStart = loopStart;
779 cblk->loopEnd = loopEnd;
780 cblk->loopCount = loopCount;
781 mLoopCount = loopCount;
782
783 return NO_ERROR;
784}
785
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800786status_t AudioTrack::setMarkerPosition(uint32_t marker)
787{
Glenn Kastena0d68332012-01-27 16:47:15 -0800788 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800789
790 mMarkerPosition = marker;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700791 mMarkerReached = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800792
793 return NO_ERROR;
794}
795
Glenn Kastena5224f32012-01-04 12:41:44 -0800796status_t AudioTrack::getMarkerPosition(uint32_t *marker) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800797{
Glenn Kastena0d68332012-01-27 16:47:15 -0800798 if (marker == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800799
800 *marker = mMarkerPosition;
801
802 return NO_ERROR;
803}
804
805status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod)
806{
Glenn Kastena0d68332012-01-27 16:47:15 -0800807 if (mCbf == NULL) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800808
809 uint32_t curPosition;
810 getPosition(&curPosition);
811 mNewPosition = curPosition + updatePeriod;
812 mUpdatePeriod = updatePeriod;
813
814 return NO_ERROR;
815}
816
Glenn Kastena5224f32012-01-04 12:41:44 -0800817status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800818{
Glenn Kastena0d68332012-01-27 16:47:15 -0800819 if (updatePeriod == NULL) return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800820
821 *updatePeriod = mUpdatePeriod;
822
823 return NO_ERROR;
824}
825
826status_t AudioTrack::setPosition(uint32_t position)
827{
John Grossman4ff14ba2012-02-08 16:37:41 -0800828 if (mIsTimed) return INVALID_OPERATION;
829
Eric Laurent1703cdf2011-03-07 14:52:59 -0800830 AutoMutex lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800832 if (!stopped_l()) return INVALID_OPERATION;
833
834 Mutex::Autolock _l(mCblk->lock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800835
836 if (position > mCblk->user) return BAD_VALUE;
837
838 mCblk->server = position;
Eric Laurent38ccae22011-03-28 18:37:07 -0700839 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700840
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800841 return NO_ERROR;
842}
843
844status_t AudioTrack::getPosition(uint32_t *position)
845{
Glenn Kastena0d68332012-01-27 16:47:15 -0800846 if (position == NULL) return BAD_VALUE;
Eric Laurent1703cdf2011-03-07 14:52:59 -0800847 AutoMutex lock(mLock);
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700848 *position = mFlushed ? 0 : mCblk->server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800849
850 return NO_ERROR;
851}
852
853status_t AudioTrack::reload()
854{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800855 AutoMutex lock(mLock);
856
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800857 if (!stopped_l()) return INVALID_OPERATION;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700858
Eric Laurent1703cdf2011-03-07 14:52:59 -0800859 flush_l();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800860
Eric Laurentd1b449a2010-05-14 03:26:45 -0700861 mCblk->stepUser(mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800862
863 return NO_ERROR;
864}
865
Eric Laurentc2f1f072009-07-17 12:17:14 -0700866audio_io_handle_t AudioTrack::getOutput()
867{
Eric Laurent1703cdf2011-03-07 14:52:59 -0800868 AutoMutex lock(mLock);
869 return getOutput_l();
870}
871
872// must be called with mLock held
873audio_io_handle_t AudioTrack::getOutput_l()
874{
Glenn Kastenfff6d712012-01-12 16:38:12 -0800875 return AudioSystem::getOutput(mStreamType,
Glenn Kasten18868c52012-03-07 09:15:37 -0800876 mCblk->sampleRate, mFormat, mChannelMask, mFlags);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700877}
878
Glenn Kastena5224f32012-01-04 12:41:44 -0800879int AudioTrack::getSessionId() const
Eric Laurentbe916aa2010-06-01 23:49:17 -0700880{
881 return mSessionId;
882}
883
Ricardo Cerqueira54d90af2012-09-02 04:33:40 +0100884extern "C" int _ZNK7android10AudioTrack12getSessionIdEv();
885extern "C" int _ZN7android10AudioTrack12getSessionIdEv() {
886 return _ZNK7android10AudioTrack12getSessionIdEv();
887}
888
Eric Laurentbe916aa2010-06-01 23:49:17 -0700889status_t AudioTrack::attachAuxEffect(int effectId)
890{
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700892 status_t status = mAudioTrack->attachAuxEffect(effectId);
893 if (status == NO_ERROR) {
894 mAuxEffectId = effectId;
895 }
896 return status;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700897}
898
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800899// -------------------------------------------------------------------------
900
Eric Laurent1703cdf2011-03-07 14:52:59 -0800901// must be called with mLock held
902status_t AudioTrack::createTrack_l(
Glenn Kastenfff6d712012-01-12 16:38:12 -0800903 audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800904 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800905 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700906 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800907 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700908 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800909 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700910 audio_io_handle_t output)
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800911{
912 status_t status;
913 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
914 if (audioFlinger == 0) {
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700915 ALOGE("Could not get audioflinger");
916 return NO_INIT;
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800917 }
918
Eric Laurentd1b449a2010-05-14 03:26:45 -0700919 uint32_t afLatency;
Eric Laurent1a9ed112012-03-20 18:36:01 -0700920 if (AudioSystem::getLatency(output, streamType, &afLatency) != NO_ERROR) {
Eric Laurentd1b449a2010-05-14 03:26:45 -0700921 return NO_INIT;
922 }
923
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700924 // Client decides whether the track is TIMED (see below), but can only express a preference
925 // for FAST. Server will perform additional tests.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700926 if ((flags & AUDIO_OUTPUT_FLAG_FAST) && !(
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700927 // either of these use cases:
928 // use case 1: shared buffer
929 (sharedBuffer != 0) ||
930 // use case 2: callback handler
931 (mCbf != NULL))) {
Glenn Kasten3acbd052012-02-28 10:39:56 -0800932 ALOGW("AUDIO_OUTPUT_FLAG_FAST denied by client");
Glenn Kasten093000f2012-05-03 09:35:36 -0700933 // once denied, do not request again if IAudioTrack is re-created
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700934 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
Glenn Kasten093000f2012-05-03 09:35:36 -0700935 mFlags = flags;
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700936 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700937 ALOGV("createTrack_l() output %d afLatency %d", output, afLatency);
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700938
Eric Laurentd1b449a2010-05-14 03:26:45 -0700939 mNotificationFramesAct = mNotificationFramesReq;
Glenn Kastene0fa4672012-04-24 14:35:14 -0700940
Dima Zavinfce7a472011-04-19 22:30:36 -0700941 if (!audio_is_linear_pcm(format)) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700942
Eric Laurentd1b449a2010-05-14 03:26:45 -0700943 if (sharedBuffer != 0) {
Glenn Kastene0fa4672012-04-24 14:35:14 -0700944 // Same comment as below about ignoring frameCount parameter for set()
Eric Laurentd1b449a2010-05-14 03:26:45 -0700945 frameCount = sharedBuffer->size();
Glenn Kastene0fa4672012-04-24 14:35:14 -0700946 } else if (frameCount == 0) {
947 int afFrameCount;
948 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
949 return NO_INIT;
950 }
951 frameCount = afFrameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700952 }
Glenn Kastene0fa4672012-04-24 14:35:14 -0700953
954 } else if (sharedBuffer != 0) {
955
956 // Ensure that buffer alignment matches channelCount
957 int channelCount = popcount(channelMask);
958 // 8-bit data in shared memory is not currently supported by AudioFlinger
959 size_t alignment = /* format == AUDIO_FORMAT_PCM_8_BIT ? 1 : */ 2;
960 if (channelCount > 1) {
961 // More than 2 channels does not require stronger alignment than stereo
962 alignment <<= 1;
963 }
964 if (((uint32_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
965 ALOGE("Invalid buffer alignment: address %p, channelCount %d",
966 sharedBuffer->pointer(), channelCount);
967 return BAD_VALUE;
968 }
969
970 // When initializing a shared buffer AudioTrack via constructors,
971 // there's no frameCount parameter.
972 // But when initializing a shared buffer AudioTrack via set(),
973 // there _is_ a frameCount parameter. We silently ignore it.
974 frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t);
975
976 } else if (!(flags & AUDIO_OUTPUT_FLAG_FAST)) {
977
978 // FIXME move these calculations and associated checks to server
979 int afSampleRate;
980 if (AudioSystem::getSamplingRate(output, streamType, &afSampleRate) != NO_ERROR) {
981 return NO_INIT;
982 }
983 int afFrameCount;
984 if (AudioSystem::getFrameCount(output, streamType, &afFrameCount) != NO_ERROR) {
985 return NO_INIT;
986 }
987
Eric Laurentd1b449a2010-05-14 03:26:45 -0700988 // Ensure that buffer depth covers at least audio hardware latency
989 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
990 if (minBufCount < 2) minBufCount = 2;
991
992 int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate;
Glenn Kasten3acbd052012-02-28 10:39:56 -0800993 ALOGV("minFrameCount: %d, afFrameCount=%d, minBufCount=%d, sampleRate=%d, afSampleRate=%d"
994 ", afLatency=%d",
995 minFrameCount, afFrameCount, minBufCount, sampleRate, afSampleRate, afLatency);
Glenn Kastene0fa4672012-04-24 14:35:14 -0700996
997 if (frameCount == 0) {
998 frameCount = minFrameCount;
999 }
1000 if (mNotificationFramesAct == 0) {
1001 mNotificationFramesAct = frameCount/2;
1002 }
1003 // Make sure that application is notified with sufficient margin
1004 // before underrun
1005 if (mNotificationFramesAct > (uint32_t)frameCount/2) {
1006 mNotificationFramesAct = frameCount/2;
1007 }
1008 if (frameCount < minFrameCount) {
1009 // not ALOGW because it happens all the time when playing key clicks over A2DP
1010 ALOGV("Minimum buffer size corrected from %d to %d",
1011 frameCount, minFrameCount);
1012 frameCount = minFrameCount;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001013 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001014
Glenn Kastene0fa4672012-04-24 14:35:14 -07001015 } else {
1016 // For fast tracks, the frame count calculations and checks are done by server
Eric Laurentd1b449a2010-05-14 03:26:45 -07001017 }
1018
Glenn Kastena075db42012-03-06 11:22:44 -08001019 IAudioFlinger::track_flags_t trackFlags = IAudioFlinger::TRACK_DEFAULT;
1020 if (mIsTimed) {
1021 trackFlags |= IAudioFlinger::TRACK_TIMED;
1022 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001023
1024 pid_t tid = -1;
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001025 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001026 trackFlags |= IAudioFlinger::TRACK_FAST;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001027 if (mAudioTrackThread != 0) {
1028 tid = mAudioTrackThread->getTid();
1029 }
Glenn Kasten4a4a0952012-03-19 11:38:14 -07001030 }
1031
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001032 sp<IAudioTrack> track = audioFlinger->createTrack(getpid(),
1033 streamType,
1034 sampleRate,
1035 format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001036 channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001037 frameCount,
Glenn Kastena075db42012-03-06 11:22:44 -08001038 trackFlags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001039 sharedBuffer,
1040 output,
Glenn Kasten3acbd052012-02-28 10:39:56 -08001041 tid,
Eric Laurentbe916aa2010-06-01 23:49:17 -07001042 &mSessionId,
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001043 &status);
1044
1045 if (track == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001046 ALOGE("AudioFlinger could not create track, status: %d", status);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001047 return status;
1048 }
1049 sp<IMemory> cblk = track->getCblk();
1050 if (cblk == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001051 ALOGE("Could not get control block");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001052 return NO_INIT;
1053 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001054 mAudioTrack = track;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001055 mCblkMemory = cblk;
1056 mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer());
Glenn Kasten3acbd052012-02-28 10:39:56 -08001057 // old has the previous value of mCblk->flags before the "or" operation
1058 int32_t old = android_atomic_or(CBLK_DIRECTION_OUT, &mCblk->flags);
1059 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
1060 if (old & CBLK_FAST) {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -07001061 ALOGV("AUDIO_OUTPUT_FLAG_FAST successful; frameCount %u", mCblk->frameCount);
Glenn Kasten3acbd052012-02-28 10:39:56 -08001062 } else {
Glenn Kasten31dfd1d2012-05-01 11:07:08 -07001063 ALOGV("AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount %u", mCblk->frameCount);
Glenn Kasten093000f2012-05-03 09:35:36 -07001064 // once denied, do not request again if IAudioTrack is re-created
1065 flags = (audio_output_flags_t) (flags & ~AUDIO_OUTPUT_FLAG_FAST);
1066 mFlags = flags;
Glenn Kasten3acbd052012-02-28 10:39:56 -08001067 }
Glenn Kastene0fa4672012-04-24 14:35:14 -07001068 if (sharedBuffer == 0) {
1069 mNotificationFramesAct = mCblk->frameCount/2;
1070 }
Glenn Kasten3acbd052012-02-28 10:39:56 -08001071 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001072 if (sharedBuffer == 0) {
1073 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
1074 } else {
1075 mCblk->buffers = sharedBuffer->pointer();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001076 // Force buffer full condition as data is already present in shared memory
Eric Laurentd1b449a2010-05-14 03:26:45 -07001077 mCblk->stepUser(mCblk->frameCount);
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001078 }
1079
Glenn Kasten83d86532012-01-17 14:39:34 -08001080 mCblk->setVolumeLR((uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000));
Glenn Kasten05632a52012-01-03 14:22:33 -08001081 mCblk->setSendLevel(mSendLevel);
Eric Laurent2beeb502010-07-16 07:43:46 -07001082 mAudioTrack->attachAuxEffect(mAuxEffectId);
Eric Laurent6100d2d2009-11-19 09:00:56 -08001083 mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS;
1084 mCblk->waitTimeMs = 0;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001085 mRemainingFrames = mNotificationFramesAct;
Glenn Kastene0fa4672012-04-24 14:35:14 -07001086 // FIXME don't believe this lie
Eric Laurentd1b449a2010-05-14 03:26:45 -07001087 mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate;
Glenn Kasten093000f2012-05-03 09:35:36 -07001088 // If IAudioTrack is re-created, don't let the requested frameCount
1089 // decrease. This can confuse clients that cache frameCount().
1090 if (mCblk->frameCount > mFrameCount) {
1091 mFrameCount = mCblk->frameCount;
1092 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001093 return NO_ERROR;
1094}
1095
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001096status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount)
1097{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001098 AutoMutex lock(mLock);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001099 bool active;
Glenn Kastend0965dd2011-06-22 16:18:04 -07001100 status_t result = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001101 audio_track_cblk_t* cblk = mCblk;
1102 uint32_t framesReq = audioBuffer->frameCount;
Eric Laurent1dd70b92009-04-21 07:56:33 -07001103 uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104
1105 audioBuffer->frameCount = 0;
1106 audioBuffer->size = 0;
1107
1108 uint32_t framesAvail = cblk->framesAvailable();
1109
Eric Laurent9b7d9502011-03-21 11:49:00 -07001110 cblk->lock.lock();
1111 if (cblk->flags & CBLK_INVALID_MSK) {
1112 goto create_new_track;
1113 }
1114 cblk->lock.unlock();
1115
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116 if (framesAvail == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001117 cblk->lock.lock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001118 goto start_loop_here;
1119 while (framesAvail == 0) {
1120 active = mActive;
Glenn Kastenf6b16782011-12-15 09:51:17 -08001121 if (CC_UNLIKELY(!active)) {
Steve Block3856b092011-10-20 11:56:00 +01001122 ALOGV("Not active and NO_MORE_BUFFERS");
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001123 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001124 return NO_MORE_BUFFERS;
1125 }
Glenn Kastenf6b16782011-12-15 09:51:17 -08001126 if (CC_UNLIKELY(!waitCount)) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001127 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128 return WOULD_BLOCK;
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001129 }
Eric Laurentd1b449a2010-05-14 03:26:45 -07001130 if (!(cblk->flags & CBLK_INVALID_MSK)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001131 mLock.unlock();
Eric Laurentd1b449a2010-05-14 03:26:45 -07001132 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
Eric Laurentd1b449a2010-05-14 03:26:45 -07001133 cblk->lock.unlock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001134 mLock.lock();
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001135 if (!mActive) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001136 return status_t(STOPPED);
1137 }
1138 cblk->lock.lock();
1139 }
1140
1141 if (cblk->flags & CBLK_INVALID_MSK) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001142 goto create_new_track;
1143 }
Glenn Kastenf6b16782011-12-15 09:51:17 -08001144 if (CC_UNLIKELY(result != NO_ERROR)) {
Eric Laurent1dd70b92009-04-21 07:56:33 -07001145 cblk->waitTimeMs += waitTimeMs;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001146 if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) {
1147 // timing out when a loop has been set and we have already written upto loop end
1148 // is a normal condition: no need to wake AudioFlinger up.
1149 if (cblk->user < cblk->loopEnd) {
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001150 ALOGW( "obtainBuffer timed out (is the CPU pegged?) %p name=%#x"
1151 "user=%08x, server=%08x", this, cblk->mName, cblk->user, cblk->server);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001152 //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001153 cblk->lock.unlock();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001154 result = mAudioTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001155 cblk->lock.lock();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001156 if (result == DEAD_OBJECT) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001157 android_atomic_or(CBLK_INVALID_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001158create_new_track:
1159 result = restoreTrack_l(cblk, false);
1160 }
1161 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001162 ALOGW("obtainBuffer create Track error %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001163 cblk->lock.unlock();
1164 return result;
1165 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001166 }
1167 cblk->waitTimeMs = 0;
1168 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001169
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001170 if (--waitCount == 0) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001171 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 return TIMED_OUT;
1173 }
1174 }
1175 // read the server count again
1176 start_loop_here:
1177 framesAvail = cblk->framesAvailable_l();
1178 }
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001179 cblk->lock.unlock();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001180 }
1181
1182 cblk->waitTimeMs = 0;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001183
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001184 if (framesReq > framesAvail) {
1185 framesReq = framesAvail;
1186 }
1187
1188 uint32_t u = cblk->user;
1189 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
1190
Marco Nelissena1472d92012-03-30 14:36:54 -07001191 if (framesReq > bufferEnd - u) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001192 framesReq = bufferEnd - u;
1193 }
1194
Eric Laurentc2f1f072009-07-17 12:17:14 -07001195 audioBuffer->flags = mMuted ? Buffer::MUTE : 0;
1196 audioBuffer->channelCount = mChannelCount;
1197 audioBuffer->frameCount = framesReq;
1198 audioBuffer->size = framesReq * cblk->frameSize;
Dima Zavinfce7a472011-04-19 22:30:36 -07001199 if (audio_is_linear_pcm(mFormat)) {
1200 audioBuffer->format = AUDIO_FORMAT_PCM_16_BIT;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001201 } else {
1202 audioBuffer->format = mFormat;
1203 }
1204 audioBuffer->raw = (int8_t *)cblk->buffer(u);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 active = mActive;
1206 return active ? status_t(NO_ERROR) : status_t(STOPPED);
1207}
1208
1209void AudioTrack::releaseBuffer(Buffer* audioBuffer)
1210{
Eric Laurent1703cdf2011-03-07 14:52:59 -08001211 AutoMutex lock(mLock);
1212 mCblk->stepUser(audioBuffer->frameCount);
Eric Laurentdf839842012-05-31 14:27:14 -07001213 if (audioBuffer->frameCount > 0) {
1214 // restart track if it was disabled by audioflinger due to previous underrun
1215 if (mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1216 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
Glenn Kasten0c9d26d2012-05-31 14:35:01 -07001217 ALOGW("releaseBuffer() track %p name=%#x disabled, restarting", this, mCblk->mName);
Eric Laurentdf839842012-05-31 14:27:14 -07001218 mAudioTrack->start();
1219 }
1220 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001221}
1222
1223// -------------------------------------------------------------------------
1224
1225ssize_t AudioTrack::write(const void* buffer, size_t userSize)
1226{
vivek mehta2192bc92012-12-04 11:46:08 -08001227#ifdef QCOM_HARDWARE
1228 if (mDirectTrack != NULL) {
1229 mDirectTrack->write(buffer,userSize);
1230 return userSize;
1231 }
1232#endif
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001233 if (mSharedBuffer != 0) return INVALID_OPERATION;
John Grossman4ff14ba2012-02-08 16:37:41 -08001234 if (mIsTimed) return INVALID_OPERATION;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235
1236 if (ssize_t(userSize) < 0) {
Glenn Kasten99e53b82012-01-19 08:59:58 -08001237 // Sanity-check: user is most-likely passing an error code, and it would
1238 // make the return value ambiguous (actualSize vs error).
Steve Block29357bc2012-01-06 19:20:56 +00001239 ALOGE("AudioTrack::write(buffer=%p, size=%u (%d)",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240 buffer, userSize, userSize);
1241 return BAD_VALUE;
1242 }
1243
Steve Block3856b092011-10-20 11:56:00 +01001244 ALOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245
Eric Laurentdf839842012-05-31 14:27:14 -07001246 if (userSize == 0) {
1247 return 0;
1248 }
1249
Eric Laurent1703cdf2011-03-07 14:52:59 -08001250 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1251 // while we are accessing the cblk
1252 mLock.lock();
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001253 sp<IAudioTrack> audioTrack = mAudioTrack;
1254 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001255 mLock.unlock();
1256
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001257 ssize_t written = 0;
1258 const int8_t *src = (const int8_t *)buffer;
1259 Buffer audioBuffer;
Glenn Kastenb9980652012-01-11 09:48:27 -08001260 size_t frameSz = frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261
1262 do {
Eric Laurent33797ea2011-03-17 09:36:51 -07001263 audioBuffer.frameCount = userSize/frameSz;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 status_t err = obtainBuffer(&audioBuffer, -1);
1266 if (err < 0) {
1267 // out of buffers, return #bytes written
1268 if (err == status_t(NO_MORE_BUFFERS))
1269 break;
1270 return ssize_t(err);
1271 }
1272
1273 size_t toWrite;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001274
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001275 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276 // Divide capacity by 2 to take expansion into account
1277 toWrite = audioBuffer.size>>1;
Glenn Kasten511754b2012-01-11 09:52:19 -08001278 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) src, toWrite);
Eric Laurent33025262009-08-04 10:42:26 -07001279 } else {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 toWrite = audioBuffer.size;
1281 memcpy(audioBuffer.i8, src, toWrite);
1282 src += toWrite;
1283 }
1284 userSize -= toWrite;
1285 written += toWrite;
1286
1287 releaseBuffer(&audioBuffer);
Eric Laurent33797ea2011-03-17 09:36:51 -07001288 } while (userSize >= frameSz);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001289
1290 return written;
1291}
1292
1293// -------------------------------------------------------------------------
1294
John Grossman4ff14ba2012-02-08 16:37:41 -08001295TimedAudioTrack::TimedAudioTrack() {
1296 mIsTimed = true;
1297}
1298
1299status_t TimedAudioTrack::allocateTimedBuffer(size_t size, sp<IMemory>* buffer)
1300{
1301 status_t result = UNKNOWN_ERROR;
1302
1303 // If the track is not invalid already, try to allocate a buffer. alloc
1304 // fails indicating that the server is dead, flag the track as invalid so
Glenn Kastenc3ae93f2012-07-30 10:59:30 -07001305 // we can attempt to restore in just a bit.
John Grossman4ff14ba2012-02-08 16:37:41 -08001306 if (!(mCblk->flags & CBLK_INVALID_MSK)) {
1307 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1308 if (result == DEAD_OBJECT) {
1309 android_atomic_or(CBLK_INVALID_ON, &mCblk->flags);
1310 }
1311 }
1312
1313 // If the track is invalid at this point, attempt to restore it. and try the
1314 // allocation one more time.
1315 if (mCblk->flags & CBLK_INVALID_MSK) {
1316 mCblk->lock.lock();
1317 result = restoreTrack_l(mCblk, false);
1318 mCblk->lock.unlock();
1319
1320 if (result == OK)
1321 result = mAudioTrack->allocateTimedBuffer(size, buffer);
1322 }
1323
1324 return result;
1325}
1326
1327status_t TimedAudioTrack::queueTimedBuffer(const sp<IMemory>& buffer,
1328 int64_t pts)
1329{
Eric Laurentdf839842012-05-31 14:27:14 -07001330 status_t status = mAudioTrack->queueTimedBuffer(buffer, pts);
1331 {
1332 AutoMutex lock(mLock);
1333 // restart track if it was disabled by audioflinger due to previous underrun
1334 if (buffer->size() != 0 && status == NO_ERROR &&
1335 mActive && (mCblk->flags & CBLK_DISABLED_MSK)) {
1336 android_atomic_and(~CBLK_DISABLED_ON, &mCblk->flags);
1337 ALOGW("queueTimedBuffer() track %p disabled, restarting", this);
1338 mAudioTrack->start();
1339 }
John Grossman4ff14ba2012-02-08 16:37:41 -08001340 }
Eric Laurentdf839842012-05-31 14:27:14 -07001341 return status;
John Grossman4ff14ba2012-02-08 16:37:41 -08001342}
1343
1344status_t TimedAudioTrack::setMediaTimeTransform(const LinearTransform& xform,
1345 TargetTimeline target)
1346{
1347 return mAudioTrack->setMediaTimeTransform(xform, target);
1348}
1349
1350// -------------------------------------------------------------------------
1351
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001352bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread)
1353{
1354 Buffer audioBuffer;
1355 uint32_t frames;
1356 size_t writtenSize;
1357
Eric Laurent1703cdf2011-03-07 14:52:59 -08001358 mLock.lock();
1359 // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed
1360 // while we are accessing the cblk
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001361 sp<IAudioTrack> audioTrack = mAudioTrack;
1362 sp<IMemory> iMem = mCblkMemory;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001363 audio_track_cblk_t* cblk = mCblk;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001364 bool active = mActive;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001365 mLock.unlock();
1366
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001367 // Manage underrun callback
Glenn Kasten9a2aaf92012-01-03 09:42:47 -08001368 if (active && (cblk->framesAvailable() == cblk->frameCount)) {
Steve Block3856b092011-10-20 11:56:00 +01001369 ALOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags);
Eric Laurent38ccae22011-03-28 18:37:07 -07001370 if (!(android_atomic_or(CBLK_UNDERRUN_ON, &cblk->flags) & CBLK_UNDERRUN_MSK)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001371 mCbf(EVENT_UNDERRUN, mUserData, 0);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001372 if (cblk->server == cblk->frameCount) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001373 mCbf(EVENT_BUFFER_END, mUserData, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001374 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001375 if (mSharedBuffer != 0) return false;
1376 }
1377 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001378
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001379 // Manage loop end callback
Eric Laurent1703cdf2011-03-07 14:52:59 -08001380 while (mLoopCount > cblk->loopCount) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381 int loopCount = -1;
1382 mLoopCount--;
1383 if (mLoopCount >= 0) loopCount = mLoopCount;
1384
1385 mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount);
1386 }
1387
1388 // Manage marker callback
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001389 if (!mMarkerReached && (mMarkerPosition > 0)) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001390 if (cblk->server >= mMarkerPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001391 mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition);
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -07001392 mMarkerReached = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001393 }
1394 }
1395
1396 // Manage new position callback
Eric Laurentc2f1f072009-07-17 12:17:14 -07001397 if (mUpdatePeriod > 0) {
Eric Laurent1703cdf2011-03-07 14:52:59 -08001398 while (cblk->server >= mNewPosition) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001399 mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition);
1400 mNewPosition += mUpdatePeriod;
1401 }
1402 }
1403
1404 // If Shared buffer is used, no data is requested from client.
1405 if (mSharedBuffer != 0) {
1406 frames = 0;
1407 } else {
1408 frames = mRemainingFrames;
1409 }
1410
Glenn Kasten99e53b82012-01-19 08:59:58 -08001411 // See description of waitCount parameter at declaration of obtainBuffer().
1412 // The logic below prevents us from being stuck below at obtainBuffer()
1413 // not being able to handle timed events (position, markers, loops).
Eric Laurent2267ba12011-09-07 11:13:23 -07001414 int32_t waitCount = -1;
1415 if (mUpdatePeriod || (!mMarkerReached && mMarkerPosition) || mLoopCount) {
1416 waitCount = 1;
1417 }
1418
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419 do {
1420
1421 audioBuffer.frameCount = frames;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001422
Eric Laurent2267ba12011-09-07 11:13:23 -07001423 status_t err = obtainBuffer(&audioBuffer, waitCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424 if (err < NO_ERROR) {
1425 if (err != TIMED_OUT) {
Steve Block29357bc2012-01-06 19:20:56 +00001426 ALOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up.");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427 return false;
1428 }
1429 break;
1430 }
1431 if (err == status_t(STOPPED)) return false;
1432
1433 // Divide buffer size by 2 to take into account the expansion
1434 // due to 8 to 16 bit conversion: the callback must fill only half
1435 // of the destination buffer
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001436 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001437 audioBuffer.size >>= 1;
1438 }
1439
1440 size_t reqSize = audioBuffer.size;
1441 mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1442 writtenSize = audioBuffer.size;
1443
1444 // Sanity check on returned size
The Android Open Source Project8555d082009-03-05 14:34:35 -08001445 if (ssize_t(writtenSize) <= 0) {
1446 // The callback is done filling buffers
1447 // Keep this thread going to handle timed events and
1448 // still try to get more data in intervals of WAIT_PERIOD_MS
1449 // but don't just loop and block the CPU, so wait
1450 usleep(WAIT_PERIOD_MS*1000);
1451 break;
1452 }
Eric Laurentdf839842012-05-31 14:27:14 -07001453
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 if (writtenSize > reqSize) writtenSize = reqSize;
1455
Eric Laurent0ca3cf92012-04-18 09:24:29 -07001456 if (mFormat == AUDIO_FORMAT_PCM_8_BIT && !(mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
Glenn Kasten511754b2012-01-11 09:52:19 -08001457 // 8 to 16 bit conversion, note that source and destination are the same address
1458 memcpy_to_i16_from_u8(audioBuffer.i16, (const uint8_t *) audioBuffer.i8, writtenSize);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001459 writtenSize <<= 1;
1460 }
1461
1462 audioBuffer.size = writtenSize;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001463 // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for
Glenn Kastenb9980652012-01-11 09:48:27 -08001464 // 8 bit PCM data: in this case, mCblk->frameSize is based on a sample size of
Eric Laurentc2f1f072009-07-17 12:17:14 -07001465 // 16 bit.
1466 audioBuffer.frameCount = writtenSize/mCblk->frameSize;
1467
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001468 frames -= audioBuffer.frameCount;
1469
1470 releaseBuffer(&audioBuffer);
1471 }
1472 while (frames);
1473
1474 if (frames == 0) {
Eric Laurentd1b449a2010-05-14 03:26:45 -07001475 mRemainingFrames = mNotificationFramesAct;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001476 } else {
1477 mRemainingFrames = frames;
1478 }
1479 return true;
1480}
1481
Eric Laurent1703cdf2011-03-07 14:52:59 -08001482// must be called with mLock and cblk.lock held. Callers must also hold strong references on
1483// the IAudioTrack and IMemory in case they are recreated here.
1484// If the IAudioTrack is successfully restored, the cblk pointer is updated
1485status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart)
1486{
1487 status_t result;
1488
Eric Laurent38ccae22011-03-28 18:37:07 -07001489 if (!(android_atomic_or(CBLK_RESTORING_ON, &cblk->flags) & CBLK_RESTORING_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001490 ALOGW("dead IAudioTrack, creating a new one from %s TID %d",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001491 fromStart ? "start()" : "obtainBuffer()", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001492
Eric Laurent1703cdf2011-03-07 14:52:59 -08001493 // signal old cblk condition so that other threads waiting for available buffers stop
1494 // waiting now
1495 cblk->cv.broadcast();
1496 cblk->lock.unlock();
1497
Eric Laurent9f6530f2011-08-30 10:18:54 -07001498 // refresh the audio configuration cache in this process to make sure we get new
1499 // output parameters in getOutput_l() and createTrack_l()
1500 AudioSystem::clearAudioConfigCache();
1501
Eric Laurent1703cdf2011-03-07 14:52:59 -08001502 // if the new IAudioTrack is created, createTrack_l() will modify the
1503 // following member variables: mAudioTrack, mCblkMemory and mCblk.
1504 // It will also delete the strong references on previous IAudioTrack and IMemory
1505 result = createTrack_l(mStreamType,
1506 cblk->sampleRate,
1507 mFormat,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -07001508 mChannelMask,
Eric Laurent1703cdf2011-03-07 14:52:59 -08001509 mFrameCount,
1510 mFlags,
1511 mSharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -07001512 getOutput_l());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001513
1514 if (result == NO_ERROR) {
Eric Laurent408b8dc2011-09-06 12:36:15 -07001515 uint32_t user = cblk->user;
1516 uint32_t server = cblk->server;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001517 // restore write index and set other indexes to reflect empty buffer status
Eric Laurent408b8dc2011-09-06 12:36:15 -07001518 mCblk->user = user;
1519 mCblk->server = user;
1520 mCblk->userBase = user;
1521 mCblk->serverBase = user;
Eric Laurent9b7d9502011-03-21 11:49:00 -07001522 // restore loop: this is not guaranteed to succeed if new frame count is not
1523 // compatible with loop length
1524 setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001525 if (!fromStart) {
1526 mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
Eric Laurent408b8dc2011-09-06 12:36:15 -07001527 // Make sure that a client relying on callback events indicating underrun or
1528 // the actual amount of audio frames played (e.g SoundPool) receives them.
1529 if (mSharedBuffer == 0) {
1530 uint32_t frames = 0;
1531 if (user > server) {
1532 frames = ((user - server) > mCblk->frameCount) ?
1533 mCblk->frameCount : (user - server);
1534 memset(mCblk->buffers, 0, frames * mCblk->frameSize);
1535 }
1536 // restart playback even if buffer is not completely filled.
1537 android_atomic_or(CBLK_FORCEREADY_ON, &mCblk->flags);
1538 // stepUser() clears CBLK_UNDERRUN_ON flag enabling underrun callbacks to
1539 // the client
1540 mCblk->stepUser(frames);
1541 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001542 }
Eric Laurent29864602012-05-08 18:57:51 -07001543 if (mSharedBuffer != 0) {
1544 mCblk->stepUser(mCblk->frameCount);
1545 }
Eric Laurent9b7d9502011-03-21 11:49:00 -07001546 if (mActive) {
Glenn Kasten3acbd052012-02-28 10:39:56 -08001547 result = mAudioTrack->start();
Steve Block5ff1dd52012-01-05 23:22:43 +00001548 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() start() failed status %d", result);
Eric Laurent9b7d9502011-03-21 11:49:00 -07001549 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001550 if (fromStart && result == NO_ERROR) {
1551 mNewPosition = mCblk->server + mUpdatePeriod;
1552 }
1553 }
1554 if (result != NO_ERROR) {
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001555 android_atomic_and(~CBLK_RESTORING_ON, &cblk->flags);
Steve Block5ff1dd52012-01-05 23:22:43 +00001556 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() failed status %d", result);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001557 }
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001558 mRestoreStatus = result;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001559 // signal old cblk condition for other threads waiting for restore completion
Eric Laurent38ccae22011-03-28 18:37:07 -07001560 android_atomic_or(CBLK_RESTORED_ON, &cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001561 cblk->cv.broadcast();
Eric Laurent1703cdf2011-03-07 14:52:59 -08001562 } else {
1563 if (!(cblk->flags & CBLK_RESTORED_MSK)) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001564 ALOGW("dead IAudioTrack, waiting for a new one TID %d", gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001565 mLock.unlock();
1566 result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS));
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001567 if (result == NO_ERROR) {
1568 result = mRestoreStatus;
1569 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001570 cblk->lock.unlock();
1571 mLock.lock();
1572 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +00001573 ALOGW("dead IAudioTrack, already restored TID %d", gettid());
Eric Laurentcfe2ba62011-09-13 15:04:17 -07001574 result = mRestoreStatus;
Eric Laurent1703cdf2011-03-07 14:52:59 -08001575 cblk->lock.unlock();
1576 }
Eric Laurent1703cdf2011-03-07 14:52:59 -08001577 }
Steve Block3856b092011-10-20 11:56:00 +01001578 ALOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x",
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001579 result, mActive, mCblk, cblk, mCblk->flags, cblk->flags);
Eric Laurent1703cdf2011-03-07 14:52:59 -08001580
1581 if (result == NO_ERROR) {
1582 // from now on we switch to the newly created cblk
1583 cblk = mCblk;
1584 }
1585 cblk->lock.lock();
1586
Steve Block5ff1dd52012-01-05 23:22:43 +00001587 ALOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d TID %d", result, gettid());
Eric Laurent1703cdf2011-03-07 14:52:59 -08001588
1589 return result;
1590}
1591
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001592status_t AudioTrack::dump(int fd, const Vector<String16>& args) const
1593{
1594
1595 const size_t SIZE = 256;
1596 char buffer[SIZE];
1597 String8 result;
1598
1599 result.append(" AudioTrack::dump\n");
1600 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]);
1601 result.append(buffer);
Eric Laurentd1b449a2010-05-14 03:26:45 -07001602 snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001603 result.append(buffer);
Eric Laurent57326622009-07-07 07:10:45 -07001604 snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605 result.append(buffer);
1606 snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency);
1607 result.append(buffer);
1608 ::write(fd, result.string(), result.size());
1609 return NO_ERROR;
1610}
1611
vivek mehta2192bc92012-12-04 11:46:08 -08001612#ifdef QCOM_HARDWARE
1613void AudioTrack::notify(int msg) {
1614 if (msg == EVENT_UNDERRUN) {
1615 ALOGV("Posting event underrun to Audio Sink.");
1616 mCbf(EVENT_UNDERRUN, mUserData, 0);
1617 }
1618}
1619
1620status_t AudioTrack::getTimeStamp(uint64_t *tstamp) {
1621 if (mDirectTrack != NULL) {
1622 *tstamp = mDirectTrack->getTimeStamp();
1623 ALOGV("Timestamp %lld ", *tstamp);
1624 }
1625 return NO_ERROR;
1626}
1627#endif
1628
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001629// =========================================================================
1630
1631AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava)
Glenn Kasten3acbd052012-02-28 10:39:56 -08001632 : Thread(bCanCallJava), mReceiver(receiver), mPaused(true)
1633{
1634}
1635
1636AudioTrack::AudioTrackThread::~AudioTrackThread()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001637{
1638}
1639
1640bool AudioTrack::AudioTrackThread::threadLoop()
1641{
Glenn Kasten3acbd052012-02-28 10:39:56 -08001642 {
1643 AutoMutex _l(mMyLock);
1644 if (mPaused) {
1645 mMyCond.wait(mMyLock);
1646 // caller will check for exitPending()
1647 return true;
1648 }
1649 }
Glenn Kastenca8b2802012-04-23 13:58:16 -07001650 if (!mReceiver.processAudioBuffer(this)) {
1651 pause();
1652 }
1653 return true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001654}
1655
Glenn Kasten3acbd052012-02-28 10:39:56 -08001656void AudioTrack::AudioTrackThread::requestExit()
1657{
1658 // must be in this order to avoid a race condition
1659 Thread::requestExit();
Glenn Kastenf4022f92012-05-02 10:34:59 -07001660 resume();
Glenn Kasten3acbd052012-02-28 10:39:56 -08001661}
1662
1663void AudioTrack::AudioTrackThread::pause()
1664{
1665 AutoMutex _l(mMyLock);
1666 mPaused = true;
1667}
1668
1669void AudioTrack::AudioTrackThread::resume()
1670{
1671 AutoMutex _l(mMyLock);
1672 if (mPaused) {
1673 mPaused = false;
1674 mMyCond.signal();
1675 }
1676}
1677
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001678// =========================================================================
1679
Eric Laurent38ccae22011-03-28 18:37:07 -07001680
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681audio_track_cblk_t::audio_track_cblk_t()
Mathias Agopian54b1a052010-03-19 16:14:13 -07001682 : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0),
Glenn Kastena0d68332012-01-27 16:47:15 -08001683 userBase(0), serverBase(0), buffers(NULL), frameCount(0),
Glenn Kasten83d86532012-01-17 14:39:34 -08001684 loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), mVolumeLR(0x10001000),
Glenn Kasten05632a52012-01-03 14:22:33 -08001685 mSendLevel(0), flags(0)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001686{
1687}
1688
1689uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount)
1690{
Marco Nelissena1472d92012-03-30 14:36:54 -07001691 ALOGV("stepuser %08x %08x %d", user, server, frameCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001692
Marco Nelissena1472d92012-03-30 14:36:54 -07001693 uint32_t u = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001694 u += frameCount;
1695 // Ensure that user is never ahead of server for AudioRecord
Eric Laurentd1b449a2010-05-14 03:26:45 -07001696 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001697 // If stepServer() has been called once, switch to normal obtainBuffer() timeout period
1698 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) {
1699 bufferTimeoutMs = MAX_RUN_TIMEOUT_MS;
1700 }
Glenn Kasten90548972011-12-13 11:45:07 -08001701 } else if (u > server) {
Marco Nelissena1472d92012-03-30 14:36:54 -07001702 ALOGW("stepUser occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001703 u = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001704 }
1705
Marco Nelissena1472d92012-03-30 14:36:54 -07001706 uint32_t fc = this->frameCount;
1707 if (u >= fc) {
1708 // common case, user didn't just wrap
1709 if (u - fc >= userBase ) {
1710 userBase += fc;
1711 }
1712 } else if (u >= userBase + fc) {
1713 // user just wrapped
1714 userBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001715 }
1716
Glenn Kasten90548972011-12-13 11:45:07 -08001717 user = u;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001718
1719 // Clear flow control error condition as new data has been written/read to/from buffer.
Eric Laurent33797ea2011-03-17 09:36:51 -07001720 if (flags & CBLK_UNDERRUN_MSK) {
Eric Laurent38ccae22011-03-28 18:37:07 -07001721 android_atomic_and(~CBLK_UNDERRUN_MSK, &flags);
Eric Laurent33797ea2011-03-17 09:36:51 -07001722 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001723
1724 return u;
1725}
1726
1727bool audio_track_cblk_t::stepServer(uint32_t frameCount)
1728{
Marco Nelissena1472d92012-03-30 14:36:54 -07001729 ALOGV("stepserver %08x %08x %d", user, server, frameCount);
1730
Eric Laurent38ccae22011-03-28 18:37:07 -07001731 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001732 ALOGW("stepServer() could not lock cblk");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001733 return false;
1734 }
1735
Glenn Kasten90548972011-12-13 11:45:07 -08001736 uint32_t s = server;
Marco Nelissena1472d92012-03-30 14:36:54 -07001737 bool flushed = (s == user);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001738
1739 s += frameCount;
Eric Laurentd1b449a2010-05-14 03:26:45 -07001740 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001741 // Mark that we have read the first buffer so that next time stepUser() is called
1742 // we switch to normal obtainBuffer() timeout period
1743 if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) {
Eric Laurent34f1d8e2009-11-04 08:27:26 -08001744 bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1;
Eric Laurentc2f1f072009-07-17 12:17:14 -07001745 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001746 // It is possible that we receive a flush()
1747 // while the mixer is processing a block: in this case,
1748 // stepServer() is called After the flush() has reset u & s and
1749 // we have s > u
Marco Nelissena1472d92012-03-30 14:36:54 -07001750 if (flushed) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001751 ALOGW("stepServer occurred after track reset");
Glenn Kasten90548972011-12-13 11:45:07 -08001752 s = user;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001753 }
1754 }
1755
1756 if (s >= loopEnd) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001757 ALOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001758 s = loopStart;
1759 if (--loopCount == 0) {
1760 loopEnd = UINT_MAX;
1761 loopStart = UINT_MAX;
1762 }
1763 }
Marco Nelissena1472d92012-03-30 14:36:54 -07001764
1765 uint32_t fc = this->frameCount;
1766 if (s >= fc) {
1767 // common case, server didn't just wrap
1768 if (s - fc >= serverBase ) {
1769 serverBase += fc;
1770 }
1771 } else if (s >= serverBase + fc) {
1772 // server just wrapped
1773 serverBase += fc;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001774 }
1775
Glenn Kasten90548972011-12-13 11:45:07 -08001776 server = s;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001777
Eric Laurent1703cdf2011-03-07 14:52:59 -08001778 if (!(flags & CBLK_INVALID_MSK)) {
1779 cv.signal();
1780 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001781 lock.unlock();
1782 return true;
1783}
1784
1785void* audio_track_cblk_t::buffer(uint32_t offset) const
1786{
Glenn Kasten90548972011-12-13 11:45:07 -08001787 return (int8_t *)buffers + (offset - userBase) * frameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001788}
1789
1790uint32_t audio_track_cblk_t::framesAvailable()
1791{
1792 Mutex::Autolock _l(lock);
1793 return framesAvailable_l();
1794}
1795
1796uint32_t audio_track_cblk_t::framesAvailable_l()
1797{
Glenn Kasten90548972011-12-13 11:45:07 -08001798 uint32_t u = user;
1799 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001800
Eric Laurentd1b449a2010-05-14 03:26:45 -07001801 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001802 uint32_t limit = (s < loopStart) ? s : loopStart;
1803 return limit + frameCount - u;
1804 } else {
1805 return frameCount + u - s;
1806 }
1807}
1808
1809uint32_t audio_track_cblk_t::framesReady()
1810{
Glenn Kasten90548972011-12-13 11:45:07 -08001811 uint32_t u = user;
1812 uint32_t s = server;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001813
Eric Laurentd1b449a2010-05-14 03:26:45 -07001814 if (flags & CBLK_DIRECTION_MSK) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001815 if (u < loopEnd) {
1816 return u - s;
1817 } else {
Eric Laurent38ccae22011-03-28 18:37:07 -07001818 // do not block on mutex shared with client on AudioFlinger side
1819 if (!tryLock()) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001820 ALOGW("framesReady() could not lock cblk");
Eric Laurent38ccae22011-03-28 18:37:07 -07001821 return 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822 }
Eric Laurent38ccae22011-03-28 18:37:07 -07001823 uint32_t frames = UINT_MAX;
1824 if (loopCount >= 0) {
1825 frames = (loopEnd - loopStart)*loopCount + u - s;
1826 }
1827 lock.unlock();
1828 return frames;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829 }
1830 } else {
1831 return s - u;
1832 }
1833}
1834
Eric Laurent38ccae22011-03-28 18:37:07 -07001835bool audio_track_cblk_t::tryLock()
1836{
1837 // the code below simulates lock-with-timeout
1838 // we MUST do this to protect the AudioFlinger server
1839 // as this lock is shared with the client.
1840 status_t err;
1841
1842 err = lock.tryLock();
1843 if (err == -EBUSY) { // just wait a bit
1844 usleep(1000);
1845 err = lock.tryLock();
1846 }
1847 if (err != NO_ERROR) {
1848 // probably, the client just died.
1849 return false;
1850 }
1851 return true;
1852}
1853
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001854// -------------------------------------------------------------------------
1855
1856}; // namespace android