blob: 8e967fb5d256d5b8ebbee428fe74385b36a40391 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/* //device/include/server/AudioFlinger/AudioFlinger.cpp
2**
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_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
22#include <math.h>
23#include <signal.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070027#include <binder/IServiceManager.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070028#include <utils/Log.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/Parcel.h>
30#include <binder/IPCThreadState.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070031#include <utils/String16.h>
32#include <utils/threads.h>
33
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080034#include <cutils/properties.h>
35
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070036#include <media/AudioTrack.h>
37#include <media/AudioRecord.h>
38
39#include <private/media/AudioTrackShared.h>
40
The Android Open Source Project8a7a6752009-01-15 16:12:10 -080041#include <hardware_legacy/AudioHardwareInterface.h>
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070042
43#include "AudioMixer.h"
44#include "AudioFlinger.h"
45
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080046#ifdef WITH_A2DP
47#include "A2dpAudioInterface.h"
48#endif
49
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050// ----------------------------------------------------------------------------
51// the sim build doesn't have gettid
52
53#ifndef HAVE_GETTID
54# define gettid getpid
55#endif
56
57// ----------------------------------------------------------------------------
58
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070059namespace android {
60
The Android Open Source Project4f68be12009-03-18 17:39:46 -070061static const char* kDeadlockedString = "AudioFlinger may be deadlocked\n";
62static const char* kHardwareLockedString = "Hardware lock is taken\n";
63
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -080064//static const nsecs_t kStandbyTimeInNsecs = seconds(3);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070065static const unsigned long kBufferRecoveryInUsecs = 2000;
66static const unsigned long kMaxBufferRecoveryInUsecs = 20000;
67static const float MAX_GAIN = 4096.0f;
68
69// retry counts for buffer fill timeout
70// 50 * ~20msecs = 1 second
71static const int8_t kMaxTrackRetries = 50;
72static const int8_t kMaxTrackStartupRetries = 50;
73
The Android Open Source Project22f8def2009-03-09 11:52:12 -070074static const int kDumpLockRetries = 50;
75static const int kDumpLockSleep = 20000;
76
Dave Sparks8a95a452009-09-30 03:09:03 -070077static const nsecs_t kWarningThrottle = seconds(5);
78
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -070080#define AUDIOFLINGER_SECURITY_ENABLED 1
81
82// ----------------------------------------------------------------------------
83
84static bool recordingAllowed() {
85#ifndef HAVE_ANDROID_OS
86 return true;
87#endif
88#if AUDIOFLINGER_SECURITY_ENABLED
89 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
90 bool ok = checkCallingPermission(String16("android.permission.RECORD_AUDIO"));
91 if (!ok) LOGE("Request requires android.permission.RECORD_AUDIO");
92 return ok;
93#else
94 if (!checkCallingPermission(String16("android.permission.RECORD_AUDIO")))
95 LOGW("WARNING: Need to add android.permission.RECORD_AUDIO to manifest");
96 return true;
97#endif
98}
99
100static bool settingsAllowed() {
101#ifndef HAVE_ANDROID_OS
102 return true;
103#endif
104#if AUDIOFLINGER_SECURITY_ENABLED
105 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
106 bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"));
107 if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
108 return ok;
109#else
110 if (!checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")))
111 LOGW("WARNING: Need to add android.permission.MODIFY_AUDIO_SETTINGS to manifest");
112 return true;
113#endif
114}
115
116// ----------------------------------------------------------------------------
117
118AudioFlinger::AudioFlinger()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 : BnAudioFlinger(),
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700120 mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextThreadId(0)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700121{
122 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700123
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700124 mAudioHardware = AudioHardwareInterface::create();
Eric Laurent9d91ad52009-07-17 12:17:14 -0700125
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700126 mHardwareStatus = AUDIO_HW_INIT;
127 if (mAudioHardware->initCheck() == NO_ERROR) {
128 // open 16-bit output stream for s/w mixer
Eric Laurent9d91ad52009-07-17 12:17:14 -0700129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 setMode(AudioSystem::MODE_NORMAL);
131
132 setMasterVolume(1.0f);
133 setMasterMute(false);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700134 } else {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700135 LOGE("Couldn't even initialize the stubbed audio hardware!");
136 }
137}
138
139AudioFlinger::~AudioFlinger()
140{
Eric Laurentc80b1a02009-08-28 10:39:03 -0700141 while (!mRecordThreads.isEmpty()) {
142 // closeInput() will remove first entry from mRecordThreads
143 closeInput(mRecordThreads.keyAt(0));
144 }
145 while (!mPlaybackThreads.isEmpty()) {
146 // closeOutput() will remove first entry from mPlaybackThreads
147 closeOutput(mPlaybackThreads.keyAt(0));
148 }
149 if (mAudioHardware) {
150 delete mAudioHardware;
151 }
The Android Open Source Project27629322009-01-09 17:51:23 -0800152}
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800153
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700154
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700155
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700156status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
157{
158 const size_t SIZE = 256;
159 char buffer[SIZE];
160 String8 result;
161
162 result.append("Clients:\n");
163 for (size_t i = 0; i < mClients.size(); ++i) {
164 wp<Client> wClient = mClients.valueAt(i);
165 if (wClient != 0) {
166 sp<Client> client = wClient.promote();
167 if (client != 0) {
168 snprintf(buffer, SIZE, " pid: %d\n", client->pid());
169 result.append(buffer);
170 }
171 }
172 }
173 write(fd, result.string(), result.size());
174 return NO_ERROR;
175}
176
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700177
178status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
179{
180 const size_t SIZE = 256;
181 char buffer[SIZE];
182 String8 result;
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700183 int hardwareStatus = mHardwareStatus;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700184
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700185 snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700186 result.append(buffer);
187 write(fd, result.string(), result.size());
188 return NO_ERROR;
189}
190
191status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
192{
193 const size_t SIZE = 256;
194 char buffer[SIZE];
195 String8 result;
196 snprintf(buffer, SIZE, "Permission Denial: "
197 "can't dump AudioFlinger from pid=%d, uid=%d\n",
198 IPCThreadState::self()->getCallingPid(),
199 IPCThreadState::self()->getCallingUid());
200 result.append(buffer);
201 write(fd, result.string(), result.size());
202 return NO_ERROR;
203}
204
The Android Open Source Project4f68be12009-03-18 17:39:46 -0700205static bool tryLock(Mutex& mutex)
206{
207 bool locked = false;
208 for (int i = 0; i < kDumpLockRetries; ++i) {
209 if (mutex.tryLock() == NO_ERROR) {
210 locked = true;
211 break;
212 }
213 usleep(kDumpLockSleep);
214 }
215 return locked;
216}
217
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700218status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
219{
220 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
221 dumpPermissionDenial(fd, args);
222 } else {
The Android Open Source Project4f68be12009-03-18 17:39:46 -0700223 // get state of hardware lock
224 bool hardwareLocked = tryLock(mHardwareLock);
225 if (!hardwareLocked) {
226 String8 result(kHardwareLockedString);
227 write(fd, result.string(), result.size());
228 } else {
229 mHardwareLock.unlock();
230 }
231
232 bool locked = tryLock(mLock);
233
234 // failed to lock - AudioFlinger is probably deadlocked
235 if (!locked) {
236 String8 result(kDeadlockedString);
237 write(fd, result.string(), result.size());
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700238 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700239
240 dumpClients(fd, args);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700241 dumpInternals(fd, args);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242
Eric Laurent9d91ad52009-07-17 12:17:14 -0700243 // dump playback threads
244 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700245 mPlaybackThreads.valueAt(i)->dump(fd, args);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700246 }
247
248 // dump record threads
Eric Laurentfd558a92009-07-23 13:35:01 -0700249 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700250 mRecordThreads.valueAt(i)->dump(fd, args);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700251 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700253 if (mAudioHardware) {
254 mAudioHardware->dumpState(fd, args);
255 }
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700256 if (locked) mLock.unlock();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700257 }
258 return NO_ERROR;
259}
260
Eric Laurent9d91ad52009-07-17 12:17:14 -0700261
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700262// IAudioFlinger interface
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263
264
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700265sp<IAudioTrack> AudioFlinger::createTrack(
266 pid_t pid,
267 int streamType,
268 uint32_t sampleRate,
269 int format,
270 int channelCount,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800271 int frameCount,
272 uint32_t flags,
273 const sp<IMemory>& sharedBuffer,
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700274 int output,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800275 status_t *status)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700276{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700277 sp<PlaybackThread::Track> track;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700278 sp<TrackHandle> trackHandle;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700279 sp<Client> client;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800280 wp<Client> wclient;
281 status_t lStatus;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700282
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 if (streamType >= AudioSystem::NUM_STREAM_TYPES) {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800284 LOGE("invalid stream type");
285 lStatus = BAD_VALUE;
286 goto Exit;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700287 }
288
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800289 {
290 Mutex::Autolock _l(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700291 PlaybackThread *thread = checkPlaybackThread_l(output);
292 if (thread == NULL) {
293 LOGE("unknown output thread");
294 lStatus = BAD_VALUE;
295 goto Exit;
296 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800297
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800298 wclient = mClients.valueFor(pid);
299
300 if (wclient != NULL) {
301 client = wclient.promote();
302 } else {
303 client = new Client(this, pid);
304 mClients.add(pid, client);
305 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700306 track = thread->createTrack_l(client, streamType, sampleRate, format,
307 channelCount, frameCount, sharedBuffer, &lStatus);
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700308 }
309 if (lStatus == NO_ERROR) {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800310 trackHandle = new TrackHandle(track);
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700311 } else {
Eric Laurent0f8ab672009-09-17 05:12:56 -0700312 // remove local strong reference to Client before deleting the Track so that the Client
313 // destructor is called by the TrackBase destructor with mLock held
314 client.clear();
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700315 track.clear();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800316 }
317
318Exit:
319 if(status) {
320 *status = lStatus;
321 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700322 return trackHandle;
323}
324
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700325uint32_t AudioFlinger::sampleRate(int output) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700326{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700327 Mutex::Autolock _l(mLock);
328 PlaybackThread *thread = checkPlaybackThread_l(output);
329 if (thread == NULL) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700330 LOGW("sampleRate() unknown thread %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700331 return 0;
332 }
333 return thread->sampleRate();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700334}
335
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700336int AudioFlinger::channelCount(int output) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700337{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700338 Mutex::Autolock _l(mLock);
339 PlaybackThread *thread = checkPlaybackThread_l(output);
340 if (thread == NULL) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700341 LOGW("channelCount() unknown thread %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700342 return 0;
343 }
344 return thread->channelCount();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700345}
346
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700347int AudioFlinger::format(int output) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700348{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700349 Mutex::Autolock _l(mLock);
350 PlaybackThread *thread = checkPlaybackThread_l(output);
351 if (thread == NULL) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700352 LOGW("format() unknown thread %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700353 return 0;
354 }
355 return thread->format();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700356}
357
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700358size_t AudioFlinger::frameCount(int output) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700359{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700360 Mutex::Autolock _l(mLock);
361 PlaybackThread *thread = checkPlaybackThread_l(output);
362 if (thread == NULL) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700363 LOGW("frameCount() unknown thread %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700364 return 0;
365 }
366 return thread->frameCount();
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700367}
368
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700369uint32_t AudioFlinger::latency(int output) const
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800370{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700371 Mutex::Autolock _l(mLock);
372 PlaybackThread *thread = checkPlaybackThread_l(output);
373 if (thread == NULL) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700374 LOGW("latency() unknown thread %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700375 return 0;
376 }
377 return thread->latency();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800378}
379
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700380status_t AudioFlinger::setMasterVolume(float value)
381{
382 // check calling permissions
383 if (!settingsAllowed()) {
384 return PERMISSION_DENIED;
385 }
386
387 // when hw supports master volume, don't scale in sw mixer
388 AutoMutex lock(mHardwareLock);
389 mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
390 if (mAudioHardware->setMasterVolume(value) == NO_ERROR) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391 value = 1.0f;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700392 }
393 mHardwareStatus = AUDIO_HW_IDLE;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700394
395 mMasterVolume = value;
396 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700397 mPlaybackThreads.valueAt(i)->setMasterVolume(value);
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700398
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700399 return NO_ERROR;
400}
401
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700402status_t AudioFlinger::setMode(int mode)
403{
404 // check calling permissions
405 if (!settingsAllowed()) {
406 return PERMISSION_DENIED;
407 }
408 if ((mode < 0) || (mode >= AudioSystem::NUM_MODES)) {
409 LOGW("Illegal value: setMode(%d)", mode);
410 return BAD_VALUE;
411 }
412
413 AutoMutex lock(mHardwareLock);
414 mHardwareStatus = AUDIO_HW_SET_MODE;
415 status_t ret = mAudioHardware->setMode(mode);
416 mHardwareStatus = AUDIO_HW_IDLE;
417 return ret;
418}
419
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700420status_t AudioFlinger::setMicMute(bool state)
421{
422 // check calling permissions
423 if (!settingsAllowed()) {
424 return PERMISSION_DENIED;
425 }
426
427 AutoMutex lock(mHardwareLock);
428 mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
429 status_t ret = mAudioHardware->setMicMute(state);
430 mHardwareStatus = AUDIO_HW_IDLE;
431 return ret;
432}
433
434bool AudioFlinger::getMicMute() const
435{
436 bool state = AudioSystem::MODE_INVALID;
437 mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
438 mAudioHardware->getMicMute(&state);
439 mHardwareStatus = AUDIO_HW_IDLE;
440 return state;
441}
442
443status_t AudioFlinger::setMasterMute(bool muted)
444{
445 // check calling permissions
446 if (!settingsAllowed()) {
447 return PERMISSION_DENIED;
448 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700449
450 mMasterMute = muted;
451 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700452 mPlaybackThreads.valueAt(i)->setMasterMute(muted);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700453
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700454 return NO_ERROR;
455}
456
457float AudioFlinger::masterVolume() const
458{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700459 return mMasterVolume;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700460}
461
462bool AudioFlinger::masterMute() const
463{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700464 return mMasterMute;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700465}
466
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700467status_t AudioFlinger::setStreamVolume(int stream, float value, int output)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700468{
469 // check calling permissions
470 if (!settingsAllowed()) {
471 return PERMISSION_DENIED;
472 }
473
Eric Laurent9d91ad52009-07-17 12:17:14 -0700474 if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700475 return BAD_VALUE;
476 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -0800477
Eric Laurent9d91ad52009-07-17 12:17:14 -0700478 AutoMutex lock(mLock);
479 PlaybackThread *thread = NULL;
480 if (output) {
481 thread = checkPlaybackThread_l(output);
482 if (thread == NULL) {
483 return BAD_VALUE;
484 }
485 }
486
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700487 status_t ret = NO_ERROR;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700488
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489 if (stream == AudioSystem::VOICE_CALL ||
490 stream == AudioSystem::BLUETOOTH_SCO) {
Eric Laurent4dd495b2009-04-21 07:56:33 -0700491 float hwValue;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492 if (stream == AudioSystem::VOICE_CALL) {
Jean-Baptiste Queru732ca392009-03-18 11:33:14 -0700493 hwValue = (float)AudioSystem::logToLinear(value)/100.0f;
Eric Laurent4dd495b2009-04-21 07:56:33 -0700494 // offset value to reflect actual hardware volume that never reaches 0
495 // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
496 value = 0.01 + 0.99 * value;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800497 } else { // (type == AudioSystem::BLUETOOTH_SCO)
Jean-Baptiste Queru732ca392009-03-18 11:33:14 -0700498 hwValue = 1.0f;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800499 }
500
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700501 AutoMutex lock(mHardwareLock);
502 mHardwareStatus = AUDIO_SET_VOICE_VOLUME;
Jean-Baptiste Queru732ca392009-03-18 11:33:14 -0700503 ret = mAudioHardware->setVoiceVolume(hwValue);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700504 mHardwareStatus = AUDIO_HW_IDLE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800505
Eric Laurent9d91ad52009-07-17 12:17:14 -0700506 }
507
508 mStreamTypes[stream].volume = value;
509
510 if (thread == NULL) {
511 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700512 mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700513
514 } else {
515 thread->setStreamVolume(stream, value);
516 }
Eric Laurent4dd495b2009-04-21 07:56:33 -0700517
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700518 return ret;
519}
520
521status_t AudioFlinger::setStreamMute(int stream, bool muted)
522{
523 // check calling permissions
524 if (!settingsAllowed()) {
525 return PERMISSION_DENIED;
526 }
527
Eric Laurent9d91ad52009-07-17 12:17:14 -0700528 if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES ||
Eric Laurentb1596ee2009-03-26 01:57:59 -0700529 uint32_t(stream) == AudioSystem::ENFORCED_AUDIBLE) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700530 return BAD_VALUE;
531 }
The Android Open Source Project4f68be12009-03-18 17:39:46 -0700532
Eric Laurent9d91ad52009-07-17 12:17:14 -0700533 mStreamTypes[stream].mute = muted;
534 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700535 mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800536
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700537 return NO_ERROR;
538}
539
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700540float AudioFlinger::streamVolume(int stream, int output) const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700541{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700542 if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700543 return 0.0f;
544 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700545
546 AutoMutex lock(mLock);
547 float volume;
548 if (output) {
549 PlaybackThread *thread = checkPlaybackThread_l(output);
550 if (thread == NULL) {
551 return 0.0f;
552 }
553 volume = thread->streamVolume(stream);
554 } else {
555 volume = mStreamTypes[stream].volume;
556 }
557
Eric Laurent4dd495b2009-04-21 07:56:33 -0700558 // remove correction applied by setStreamVolume()
Jean-Baptiste Queru732ca392009-03-18 11:33:14 -0700559 if (stream == AudioSystem::VOICE_CALL) {
Eric Laurent4dd495b2009-04-21 07:56:33 -0700560 volume = (volume - 0.01) / 0.99 ;
James E. Blair6015dfc2009-01-17 13:30:20 -0800561 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700562
Eric Laurent4dd495b2009-04-21 07:56:33 -0700563 return volume;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700564}
565
566bool AudioFlinger::streamMute(int stream) const
567{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700568 if (stream < 0 || stream >= (int)AudioSystem::NUM_STREAM_TYPES) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700569 return true;
570 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700571
572 return mStreamTypes[stream].mute;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700573}
574
575bool AudioFlinger::isMusicActive() const
576{
Eric Laurentb025ca02009-07-09 03:20:57 -0700577 Mutex::Autolock _l(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700578 for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700579 if (mPlaybackThreads.valueAt(i)->isMusicActive()) {
Eric Laurent9d91ad52009-07-17 12:17:14 -0700580 return true;
581 }
582 }
583 return false;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700584}
585
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700586status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700587{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700588 status_t result;
589
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700590 LOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d",
Eric Laurent9d91ad52009-07-17 12:17:14 -0700591 ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
592 // check calling permissions
593 if (!settingsAllowed()) {
594 return PERMISSION_DENIED;
The Android Open Source Project8a7a6752009-01-15 16:12:10 -0800595 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700596
597 // ioHandle == 0 means the parameters are global to the audio hardware interface
598 if (ioHandle == 0) {
599 AutoMutex lock(mHardwareLock);
600 mHardwareStatus = AUDIO_SET_PARAMETER;
601 result = mAudioHardware->setParameters(keyValuePairs);
602 mHardwareStatus = AUDIO_HW_IDLE;
603 return result;
604 }
605
Eric Laurent2d70c802009-09-29 11:12:57 -0700606 // hold a strong ref on thread in case closeOutput() or closeInput() is called
607 // and the thread is exited once the lock is released
608 sp<ThreadBase> thread;
609 {
610 Mutex::Autolock _l(mLock);
611 thread = checkPlaybackThread_l(ioHandle);
612 if (thread == NULL) {
613 thread = checkRecordThread_l(ioHandle);
614 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700615 }
Eric Laurent2d70c802009-09-29 11:12:57 -0700616 if (thread != NULL) {
617 return thread->setParameters(keyValuePairs);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700618 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700619 return BAD_VALUE;
620}
621
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700622String8 AudioFlinger::getParameters(int ioHandle, const String8& keys)
Eric Laurent9d91ad52009-07-17 12:17:14 -0700623{
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700624// LOGV("getParameters() io %d, keys %s, tid %d, calling tid %d",
Eric Laurent9d91ad52009-07-17 12:17:14 -0700625// ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
626
627 if (ioHandle == 0) {
628 return mAudioHardware->getParameters(keys);
629 }
Eric Laurent2d70c802009-09-29 11:12:57 -0700630
631 Mutex::Autolock _l(mLock);
632
Eric Laurent9d91ad52009-07-17 12:17:14 -0700633 PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
634 if (playbackThread != NULL) {
635 return playbackThread->getParameters(keys);
636 }
637 RecordThread *recordThread = checkRecordThread_l(ioHandle);
638 if (recordThread != NULL) {
639 return recordThread->getParameters(keys);
640 }
641 return String8("");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700642}
643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
645{
646 return mAudioHardware->getInputBufferSize(sampleRate, format, channelCount);
647}
648
649void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
650{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 LOGV("registerClient() %p, tid %d, calling tid %d", client.get(), gettid(), IPCThreadState::self()->getCallingPid());
653 Mutex::Autolock _l(mLock);
654
655 sp<IBinder> binder = client->asBinder();
656 if (mNotificationClients.indexOf(binder) < 0) {
657 LOGV("Adding notification client %p", binder.get());
658 binder->linkToDeath(this);
659 mNotificationClients.add(binder);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700660 }
661
662 // the config change is always sent from playback or record threads to avoid deadlock
663 // with AudioSystem::gLock
664 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700665 mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700666 }
667
668 for (size_t i = 0; i < mRecordThreads.size(); i++) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700669 mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670 }
671}
672
673void AudioFlinger::binderDied(const wp<IBinder>& who) {
Eric Laurent9d91ad52009-07-17 12:17:14 -0700674
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675 LOGV("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), IPCThreadState::self()->getCallingPid());
676 Mutex::Autolock _l(mLock);
677
678 IBinder *binder = who.unsafe_get();
679
680 if (binder != NULL) {
681 int index = mNotificationClients.indexOf(binder);
682 if (index >= 0) {
683 LOGV("Removing notification client %p", binder);
684 mNotificationClients.removeAt(index);
685 }
686 }
687}
688
Eric Laurentb3687ae2009-09-15 07:10:12 -0700689// audioConfigChanged_l() must be called with AudioFlinger::mLock held
690void AudioFlinger::audioConfigChanged_l(int event, const sp<ThreadBase>& thread, void *param2) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700691 int ioHandle = 0;
692
693 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
694 if (mPlaybackThreads.valueAt(i) == thread) {
695 ioHandle = mPlaybackThreads.keyAt(i);
696 break;
697 }
698 }
699 if (ioHandle == 0) {
700 for (size_t i = 0; i < mRecordThreads.size(); i++) {
701 if (mRecordThreads.valueAt(i) == thread) {
702 ioHandle = mRecordThreads.keyAt(i);
703 break;
704 }
705 }
706 }
707
708 if (ioHandle != 0) {
709 size_t size = mNotificationClients.size();
710 for (size_t i = 0; i < size; i++) {
711 sp<IBinder> binder = mNotificationClients.itemAt(i);
Eric Laurentb3687ae2009-09-15 07:10:12 -0700712 LOGV("audioConfigChanged_l() Notifying change to client %p", binder.get());
Eric Laurente0e9ecc2009-07-28 08:44:33 -0700713 sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient> (binder);
714 client->ioConfigChanged(event, ioHandle, param2);
715 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700716 }
717}
718
Eric Laurent0f8ab672009-09-17 05:12:56 -0700719// removeClient_l() must be called with AudioFlinger::mLock held
720void AudioFlinger::removeClient_l(pid_t pid)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700721{
Eric Laurent0f8ab672009-09-17 05:12:56 -0700722 LOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700723 mClients.removeItem(pid);
724}
725
Eric Laurent9d91ad52009-07-17 12:17:14 -0700726// ----------------------------------------------------------------------------
727
728AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger)
729 : Thread(false),
730 mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0),
Eric Laurent3464c012009-08-04 09:45:33 -0700731 mFormat(0), mFrameSize(1), mStandby(false)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -0700732{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733}
734
Eric Laurent9d91ad52009-07-17 12:17:14 -0700735AudioFlinger::ThreadBase::~ThreadBase()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800736{
Eric Laurent3464c012009-08-04 09:45:33 -0700737 mParamCond.broadcast();
738 mNewParameters.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739}
740
Eric Laurent9d91ad52009-07-17 12:17:14 -0700741void AudioFlinger::ThreadBase::exit()
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700742{
Eric Laurent2d70c802009-09-29 11:12:57 -0700743 // keep a strong ref on ourself so that we wont get
Eric Laurent9d91ad52009-07-17 12:17:14 -0700744 // destroyed in the middle of requestExitAndWait()
745 sp <ThreadBase> strongMe = this;
746
747 LOGV("ThreadBase::exit");
748 {
749 AutoMutex lock(&mLock);
750 requestExit();
751 mWaitWorkCV.signal();
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700752 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700753 requestExitAndWait();
The Android Open Source Projectbcef13b2009-03-11 12:11:56 -0700754}
Eric Laurent9d91ad52009-07-17 12:17:14 -0700755
756uint32_t AudioFlinger::ThreadBase::sampleRate() const
757{
758 return mSampleRate;
759}
760
761int AudioFlinger::ThreadBase::channelCount() const
762{
763 return mChannelCount;
764}
765
766int AudioFlinger::ThreadBase::format() const
767{
768 return mFormat;
769}
770
771size_t AudioFlinger::ThreadBase::frameCount() const
772{
773 return mFrameCount;
774}
775
776status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
777{
Eric Laurent3464c012009-08-04 09:45:33 -0700778 status_t status;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700779
Eric Laurent3464c012009-08-04 09:45:33 -0700780 LOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
Eric Laurent9d91ad52009-07-17 12:17:14 -0700781 Mutex::Autolock _l(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700782
Eric Laurent3464c012009-08-04 09:45:33 -0700783 mNewParameters.add(keyValuePairs);
Eric Laurent9d91ad52009-07-17 12:17:14 -0700784 mWaitWorkCV.signal();
Eric Laurent2d70c802009-09-29 11:12:57 -0700785 // wait condition with timeout in case the thread loop has exited
786 // before the request could be processed
787 if (mParamCond.waitRelative(mLock, seconds(2)) == NO_ERROR) {
788 status = mParamStatus;
789 mWaitWorkCV.signal();
790 } else {
791 status = TIMED_OUT;
792 }
Eric Laurent3464c012009-08-04 09:45:33 -0700793 return status;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700794}
795
796void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param)
797{
798 Mutex::Autolock _l(mLock);
Eric Laurent3464c012009-08-04 09:45:33 -0700799 sendConfigEvent_l(event, param);
800}
801
802// sendConfigEvent_l() must be called with ThreadBase::mLock held
803void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param)
804{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700805 ConfigEvent *configEvent = new ConfigEvent();
806 configEvent->mEvent = event;
807 configEvent->mParam = param;
808 mConfigEvents.add(configEvent);
809 LOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param);
810 mWaitWorkCV.signal();
811}
812
813void AudioFlinger::ThreadBase::processConfigEvents()
814{
815 mLock.lock();
816 while(!mConfigEvents.isEmpty()) {
817 LOGV("processConfigEvents() remaining events %d", mConfigEvents.size());
818 ConfigEvent *configEvent = mConfigEvents[0];
819 mConfigEvents.removeAt(0);
Eric Laurentb3687ae2009-09-15 07:10:12 -0700820 // release mLock because audioConfigChanged() will lock AudioFlinger mLock
821 // before calling Audioflinger::audioConfigChanged_l() thus creating
Eric Laurent9d91ad52009-07-17 12:17:14 -0700822 // potential cross deadlock between AudioFlinger::mLock and mLock
823 mLock.unlock();
824 audioConfigChanged(configEvent->mEvent, configEvent->mParam);
825 delete configEvent;
826 mLock.lock();
827 }
828 mLock.unlock();
829}
830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831
832// ----------------------------------------------------------------------------
833
Eric Laurent9d91ad52009-07-17 12:17:14 -0700834AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output)
835 : ThreadBase(audioFlinger),
Eric Laurentf9df2492009-08-06 08:49:39 -0700836 mMixBuffer(0), mSuspended(0), mBytesWritten(0), mOutput(output),
Eric Laurent9395d9b2009-07-23 13:17:39 -0700837 mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838{
Eric Laurent9d91ad52009-07-17 12:17:14 -0700839 readOutputParameters();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800840
Eric Laurent9d91ad52009-07-17 12:17:14 -0700841 mMasterVolume = mAudioFlinger->masterVolume();
842 mMasterMute = mAudioFlinger->masterMute();
843
844 for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
845 mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream);
846 mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700848 // notify client processes that a new input has been opened
849 sendConfigEvent(AudioSystem::OUTPUT_OPENED);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850}
851
Eric Laurent9d91ad52009-07-17 12:17:14 -0700852AudioFlinger::PlaybackThread::~PlaybackThread()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800853{
854 delete [] mMixBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800855}
856
Eric Laurent9d91ad52009-07-17 12:17:14 -0700857status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858{
859 dumpInternals(fd, args);
860 dumpTracks(fd, args);
861 return NO_ERROR;
862}
863
Eric Laurent9d91ad52009-07-17 12:17:14 -0700864status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800865{
866 const size_t SIZE = 256;
867 char buffer[SIZE];
868 String8 result;
869
Eric Laurent9d91ad52009-07-17 12:17:14 -0700870 snprintf(buffer, SIZE, "Output thread %p tracks\n", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871 result.append(buffer);
872 result.append(" Name Clien Typ Fmt Chn Buf S M F SRate LeftV RighV Serv User\n");
873 for (size_t i = 0; i < mTracks.size(); ++i) {
Eric Laurentc828f6a2009-03-31 14:34:35 -0700874 sp<Track> track = mTracks[i];
875 if (track != 0) {
876 track->dump(buffer, SIZE);
877 result.append(buffer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800878 }
879 }
880
Eric Laurent9d91ad52009-07-17 12:17:14 -0700881 snprintf(buffer, SIZE, "Output thread %p active tracks\n", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 result.append(buffer);
883 result.append(" Name Clien Typ Fmt Chn Buf S M F SRate LeftV RighV Serv User\n");
884 for (size_t i = 0; i < mActiveTracks.size(); ++i) {
Eric Laurentc828f6a2009-03-31 14:34:35 -0700885 wp<Track> wTrack = mActiveTracks[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886 if (wTrack != 0) {
887 sp<Track> track = wTrack.promote();
888 if (track != 0) {
889 track->dump(buffer, SIZE);
890 result.append(buffer);
891 }
892 }
893 }
894 write(fd, result.string(), result.size());
895 return NO_ERROR;
896}
897
Eric Laurent9d91ad52009-07-17 12:17:14 -0700898status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800899{
900 const size_t SIZE = 256;
901 char buffer[SIZE];
902 String8 result;
903
Eric Laurent9d91ad52009-07-17 12:17:14 -0700904 snprintf(buffer, SIZE, "Output thread %p internals\n", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905 result.append(buffer);
906 snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
907 result.append(buffer);
908 snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites);
909 result.append(buffer);
910 snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites);
911 result.append(buffer);
912 snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite);
913 result.append(buffer);
914 snprintf(buffer, SIZE, "standby: %d\n", mStandby);
915 result.append(buffer);
916 write(fd, result.string(), result.size());
917 return NO_ERROR;
918}
919
920// Thread virtuals
Eric Laurent9d91ad52009-07-17 12:17:14 -0700921status_t AudioFlinger::PlaybackThread::readyToRun()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800922{
923 if (mSampleRate == 0) {
924 LOGE("No working audio driver found.");
925 return NO_INIT;
926 }
Eric Laurent9d91ad52009-07-17 12:17:14 -0700927 LOGI("AudioFlinger's thread %p ready to run", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928 return NO_ERROR;
929}
930
Eric Laurent9d91ad52009-07-17 12:17:14 -0700931void AudioFlinger::PlaybackThread::onFirstRef()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932{
933 const size_t SIZE = 256;
934 char buffer[SIZE];
935
Eric Laurent9d91ad52009-07-17 12:17:14 -0700936 snprintf(buffer, SIZE, "Playback Thread %p", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937
938 run(buffer, ANDROID_PRIORITY_URGENT_AUDIO);
939}
940
Eric Laurent9d91ad52009-07-17 12:17:14 -0700941// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
942sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800943 const sp<AudioFlinger::Client>& client,
944 int streamType,
945 uint32_t sampleRate,
946 int format,
947 int channelCount,
948 int frameCount,
949 const sp<IMemory>& sharedBuffer,
950 status_t *status)
951{
952 sp<Track> track;
953 status_t lStatus;
Eric Laurent9d91ad52009-07-17 12:17:14 -0700954
955 if (mType == DIRECT) {
956 if (sampleRate != mSampleRate || format != mFormat || channelCount != mChannelCount) {
957 LOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelCount %d for output %p",
958 sampleRate, format, channelCount, mOutput);
959 lStatus = BAD_VALUE;
960 goto Exit;
961 }
962 } else {
963 // Resampler implementation limits input sampling rate to 2 x output sampling rate.
964 if (sampleRate > mSampleRate*2) {
965 LOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate);
966 lStatus = BAD_VALUE;
967 goto Exit;
968 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969 }
970
Eric Laurent9d91ad52009-07-17 12:17:14 -0700971 if (mOutput == 0) {
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700972 LOGE("Audio driver not initialized.");
973 lStatus = NO_INIT;
974 goto Exit;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 }
976
Eric Laurent9d91ad52009-07-17 12:17:14 -0700977 { // scope for mLock
978 Mutex::Autolock _l(mLock);
979 track = new Track(this, client, streamType, sampleRate, format,
980 channelCount, frameCount, sharedBuffer);
981 if (track->getCblk() == NULL) {
982 lStatus = NO_MEMORY;
983 goto Exit;
984 }
985 mTracks.add(track);
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700986 }
The Android Open Source Project22f8def2009-03-09 11:52:12 -0700987 lStatus = NO_ERROR;
988
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989Exit:
990 if(status) {
991 *status = lStatus;
992 }
993 return track;
994}
995
Eric Laurent9d91ad52009-07-17 12:17:14 -0700996uint32_t AudioFlinger::PlaybackThread::latency() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997{
998 if (mOutput) {
999 return mOutput->latency();
1000 }
1001 else {
1002 return 0;
1003 }
1004}
1005
Eric Laurent9d91ad52009-07-17 12:17:14 -07001006status_t AudioFlinger::PlaybackThread::setMasterVolume(float value)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007{
1008 mMasterVolume = value;
1009 return NO_ERROR;
1010}
1011
Eric Laurent9d91ad52009-07-17 12:17:14 -07001012status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013{
1014 mMasterMute = muted;
1015 return NO_ERROR;
1016}
1017
Eric Laurent9d91ad52009-07-17 12:17:14 -07001018float AudioFlinger::PlaybackThread::masterVolume() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019{
1020 return mMasterVolume;
1021}
1022
Eric Laurent9d91ad52009-07-17 12:17:14 -07001023bool AudioFlinger::PlaybackThread::masterMute() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024{
1025 return mMasterMute;
1026}
1027
Eric Laurent9d91ad52009-07-17 12:17:14 -07001028status_t AudioFlinger::PlaybackThread::setStreamVolume(int stream, float value)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029{
1030 mStreamTypes[stream].volume = value;
1031 return NO_ERROR;
1032}
1033
Eric Laurent9d91ad52009-07-17 12:17:14 -07001034status_t AudioFlinger::PlaybackThread::setStreamMute(int stream, bool muted)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035{
1036 mStreamTypes[stream].mute = muted;
1037 return NO_ERROR;
1038}
1039
Eric Laurent9d91ad52009-07-17 12:17:14 -07001040float AudioFlinger::PlaybackThread::streamVolume(int stream) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041{
1042 return mStreamTypes[stream].volume;
1043}
1044
Eric Laurent9d91ad52009-07-17 12:17:14 -07001045bool AudioFlinger::PlaybackThread::streamMute(int stream) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046{
1047 return mStreamTypes[stream].mute;
1048}
1049
Eric Laurent9d91ad52009-07-17 12:17:14 -07001050bool AudioFlinger::PlaybackThread::isMusicActive() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051{
Eric Laurent9d91ad52009-07-17 12:17:14 -07001052 Mutex::Autolock _l(mLock);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 size_t count = mActiveTracks.size();
1054 for (size_t i = 0 ; i < count ; ++i) {
1055 sp<Track> t = mActiveTracks[i].promote();
1056 if (t == 0) continue;
1057 Track* const track = t.get();
Eric Laurent9395d9b2009-07-23 13:17:39 -07001058 if (t->type() == AudioSystem::MUSIC)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 return true;
1060 }
1061 return false;
1062}
1063
Eric Laurent9d91ad52009-07-17 12:17:14 -07001064// addTrack_l() must be called with ThreadBase::mLock held
1065status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066{
1067 status_t status = ALREADY_EXISTS;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001068
1069 // here the track could be either new, or restarted
1070 // in both cases "unstop" the track
1071 if (track->isPaused()) {
1072 track->mState = TrackBase::RESUMING;
1073 LOGV("PAUSED => RESUMING (%d)", track->name());
1074 } else {
1075 track->mState = TrackBase::ACTIVE;
1076 LOGV("? => ACTIVE (%d)", track->name());
1077 }
1078 // set retry count for buffer fill
1079 track->mRetryCount = kMaxTrackStartupRetries;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001080 if (mActiveTracks.indexOf(track) < 0) {
1081 // the track is newly added, make sure it fills up all its
1082 // buffers before playing. This is to ensure the client will
1083 // effectively get the latency it requested.
1084 track->mFillingUpStatus = Track::FS_FILLING;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08001085 track->mResetDone = false;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001086 mActiveTracks.add(track);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087 status = NO_ERROR;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001088 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07001089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090 LOGV("mWaitWorkCV.broadcast");
Eric Laurent9d91ad52009-07-17 12:17:14 -07001091 mWaitWorkCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092
1093 return status;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001094}
1095
Eric Laurent9d91ad52009-07-17 12:17:14 -07001096// destroyTrack_l() must be called with ThreadBase::mLock held
1097void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001098{
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001099 track->mState = TrackBase::TERMINATED;
1100 if (mActiveTracks.indexOf(track) < 0) {
1101 LOGV("remove track (%d) and delete from mixer", track->name());
1102 mTracks.remove(track);
The Android Open Source Project22f8def2009-03-09 11:52:12 -07001103 deleteTrackName_l(track->name());
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001104 }
1105}
1106
Eric Laurent9d91ad52009-07-17 12:17:14 -07001107String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
The Android Open Source Projecte41dd752009-01-22 00:13:42 -08001108{
Eric Laurent9d91ad52009-07-17 12:17:14 -07001109 return mOutput->getParameters(keys);
1110}
The Android Open Source Projecte41dd752009-01-22 00:13:42 -08001111
Eric Laurent9d91ad52009-07-17 12:17:14 -07001112void AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) {
1113 AudioSystem::OutputDescriptor desc;
1114 void *param2 = 0;
1115
1116 LOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event, param);
1117
1118 switch (event) {
1119 case AudioSystem::OUTPUT_OPENED:
1120 case AudioSystem::OUTPUT_CONFIG_CHANGED:
1121 desc.channels = mChannelCount;
1122 desc.samplingRate = mSampleRate;
1123 desc.format = mFormat;
1124 desc.frameCount = mFrameCount;
1125 desc.latency = latency();
1126 param2 = &desc;
1127 break;
1128
1129 case AudioSystem::STREAM_CONFIG_CHANGED:
1130 param2 = &param;
1131 case AudioSystem::OUTPUT_CLOSED:
1132 default:
1133 break;
1134 }
Eric Laurentb3687ae2009-09-15 07:10:12 -07001135 Mutex::Autolock _l(mAudioFlinger->mLock);
1136 mAudioFlinger->audioConfigChanged_l(event, this, param2);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001137}
1138
1139void AudioFlinger::PlaybackThread::readOutputParameters()
1140{
1141 mSampleRate = mOutput->sampleRate();
1142 mChannelCount = AudioSystem::popCount(mOutput->channels());
1143
1144 mFormat = mOutput->format();
1145 mFrameSize = mOutput->frameSize();
1146 mFrameCount = mOutput->bufferSize() / mFrameSize;
1147
1148 mMinBytesToWrite = (mOutput->latency() * mSampleRate * mFrameSize) / 1000;
1149 // FIXME - Current mixer implementation only supports stereo output: Always
1150 // Allocate a stereo buffer even if HW output is mono.
1151 if (mMixBuffer != NULL) delete mMixBuffer;
1152 mMixBuffer = new int16_t[mFrameCount * 2];
1153 memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t));
1154}
1155
1156// ----------------------------------------------------------------------------
1157
1158AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output)
1159 : PlaybackThread(audioFlinger, output),
1160 mAudioMixer(0)
1161{
1162 mType = PlaybackThread::MIXER;
1163 mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
1164
1165 // FIXME - Current mixer implementation only supports stereo output
1166 if (mChannelCount == 1) {
1167 LOGE("Invalid audio hardware channel count");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 }
The Android Open Source Projecte41dd752009-01-22 00:13:42 -08001169}
1170
Eric Laurent9d91ad52009-07-17 12:17:14 -07001171AudioFlinger::MixerThread::~MixerThread()
The Android Open Source Projecte41dd752009-01-22 00:13:42 -08001172{
Eric Laurent9d91ad52009-07-17 12:17:14 -07001173 delete mAudioMixer;
1174}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001175
Eric Laurent9d91ad52009-07-17 12:17:14 -07001176bool AudioFlinger::MixerThread::threadLoop()
1177{
Eric Laurent3522c802009-09-07 08:38:38 -07001178 unsigned long sleepTime = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001179 int16_t* curBuf = mMixBuffer;
1180 Vector< sp<Track> > tracksToRemove;
1181 size_t enabledTracks = 0;
1182 nsecs_t standbyTime = systemTime();
1183 size_t mixBufferSize = mFrameCount * mFrameSize;
Dave Sparks8a95a452009-09-30 03:09:03 -07001184 // FIXME: Relaxed timing because of a certain device that can't meet latency
1185 // Should be reduced to 2x after the vendor fixes the driver issue
1186 nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 3;
1187 nsecs_t lastWarning = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188
Eric Laurent9d91ad52009-07-17 12:17:14 -07001189 while (!exitPending())
1190 {
1191 processConfigEvents();
1192
1193 enabledTracks = 0;
1194 { // scope for mLock
1195
1196 Mutex::Autolock _l(mLock);
1197
1198 if (checkForNewParameters_l()) {
1199 mixBufferSize = mFrameCount * mFrameSize;
Dave Sparks8a95a452009-09-30 03:09:03 -07001200 // FIXME: Relaxed timing because of a certain device that can't meet latency
1201 // Should be reduced to 2x after the vendor fixes the driver issue
1202 maxPeriod = seconds(mFrameCount) / mSampleRate * 3;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001203 }
1204
1205 const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
1206
1207 // put audio hardware into standby after short delay
1208 if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
1209 mSuspended) {
1210 if (!mStandby) {
1211 LOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended);
1212 mOutput->standby();
1213 mStandby = true;
1214 mBytesWritten = 0;
1215 }
1216
1217 if (!activeTracks.size() && mConfigEvents.isEmpty()) {
1218 // we're about to wait, flush the binder command buffer
1219 IPCThreadState::self()->flushCommands();
1220
1221 if (exitPending()) break;
1222
1223 // wait until we have something to do...
1224 LOGV("MixerThread %p TID %d going to sleep\n", this, gettid());
1225 mWaitWorkCV.wait(mLock);
1226 LOGV("MixerThread %p TID %d waking up\n", this, gettid());
1227
1228 if (mMasterMute == false) {
1229 char value[PROPERTY_VALUE_MAX];
1230 property_get("ro.audio.silent", value, "0");
1231 if (atoi(value)) {
1232 LOGD("Silence is golden");
1233 setMasterMute(true);
1234 }
1235 }
1236
1237 standbyTime = systemTime() + kStandbyTimeInNsecs;
Eric Laurent3522c802009-09-07 08:38:38 -07001238 sleepTime = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001239 continue;
1240 }
1241 }
1242
1243 enabledTracks = prepareTracks_l(activeTracks, &tracksToRemove);
1244 }
1245
Eric Laurentaef692f2009-09-22 00:35:48 -07001246 if (LIKELY(enabledTracks)) {
1247 // mix buffers...
1248 mAudioMixer->process(curBuf);
1249 sleepTime = 0;
1250 standbyTime = systemTime() + kStandbyTimeInNsecs;
Eric Laurent3522c802009-09-07 08:38:38 -07001251 } else {
Eric Laurentaef692f2009-09-22 00:35:48 -07001252 sleepTime += kBufferRecoveryInUsecs;
1253 if (sleepTime > kMaxBufferRecoveryInUsecs) {
1254 sleepTime = kMaxBufferRecoveryInUsecs;
1255 }
1256 // There was nothing to mix this round, which means all
1257 // active tracks were late. Sleep a little bit to give
1258 // them another chance. If we're too late, write 0s to audio
1259 // hardware to avoid underrun.
1260 if (mBytesWritten != 0 && sleepTime >= kMaxBufferRecoveryInUsecs) {
1261 memset (curBuf, 0, mixBufferSize);
Eric Laurent3522c802009-09-07 08:38:38 -07001262 sleepTime = 0;
Eric Laurent3522c802009-09-07 08:38:38 -07001263 }
Eric Laurentaef692f2009-09-22 00:35:48 -07001264 }
1265
1266 if (mSuspended) {
1267 sleepTime = kMaxBufferRecoveryInUsecs;
1268 }
1269 // sleepTime == 0 means we must write to audio hardware
1270 if (sleepTime == 0) {
1271 mLastWriteTime = systemTime();
1272 mInWrite = true;
1273 LOGV("mOutput->write() thread %p frames %d", this, mFrameCount);
1274 int bytesWritten = (int)mOutput->write(curBuf, mixBufferSize);
1275 if (bytesWritten > 0) mBytesWritten += bytesWritten;
1276 mNumWrites++;
1277 mInWrite = false;
1278 mStandby = false;
Dave Sparks8a95a452009-09-30 03:09:03 -07001279 nsecs_t now = systemTime();
1280 nsecs_t delta = now - mLastWriteTime;
Eric Laurentaef692f2009-09-22 00:35:48 -07001281 if (delta > maxPeriod) {
Eric Laurentaef692f2009-09-22 00:35:48 -07001282 mNumDelayedWrites++;
Dave Sparks8a95a452009-09-30 03:09:03 -07001283 if ((now - lastWarning) > kWarningThrottle) {
1284 LOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
1285 ns2ms(delta), mNumDelayedWrites, this);
1286 lastWarning = now;
1287 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07001288 }
Eric Laurentaef692f2009-09-22 00:35:48 -07001289 } else {
1290 usleep(sleepTime);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001291 }
1292
1293 // finally let go of all our tracks, without the lock held
1294 // since we can't guarantee the destructors won't acquire that
1295 // same lock.
1296 tracksToRemove.clear();
1297 }
1298
1299 if (!mStandby) {
1300 mOutput->standby();
1301 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07001302
1303 LOGV("MixerThread %p exiting", this);
1304 return false;
1305}
1306
1307// prepareTracks_l() must be called with ThreadBase::mLock held
1308size_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove)
1309{
1310
1311 size_t enabledTracks = 0;
1312 // find out which tracks need to be processed
1313 size_t count = activeTracks.size();
1314 for (size_t i=0 ; i<count ; i++) {
1315 sp<Track> t = activeTracks[i].promote();
1316 if (t == 0) continue;
1317
1318 Track* const track = t.get();
1319 audio_track_cblk_t* cblk = track->cblk();
1320
1321 // The first time a track is added we wait
1322 // for all its buffers to be filled before processing it
1323 mAudioMixer->setActiveTrack(track->name());
1324 if (cblk->framesReady() && (track->isReady() || track->isStopped()) &&
1325 !track->isPaused())
1326 {
1327 //LOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
1328
1329 // compute volume for this track
1330 int16_t left, right;
1331 if (track->isMuted() || mMasterMute || track->isPausing() ||
1332 mStreamTypes[track->type()].mute) {
1333 left = right = 0;
1334 if (track->isPausing()) {
1335 track->setPaused();
1336 }
1337 } else {
1338 float typeVolume = mStreamTypes[track->type()].volume;
1339 float v = mMasterVolume * typeVolume;
1340 float v_clamped = v * cblk->volume[0];
1341 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
1342 left = int16_t(v_clamped);
1343 v_clamped = v * cblk->volume[1];
1344 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
1345 right = int16_t(v_clamped);
1346 }
1347
1348 // XXX: these things DON'T need to be done each time
1349 mAudioMixer->setBufferProvider(track);
1350 mAudioMixer->enable(AudioMixer::MIXING);
1351
Eric Laurent08d3d1d2009-09-03 03:45:52 -07001352 int param = AudioMixer::VOLUME;
1353 if (track->mFillingUpStatus == Track::FS_FILLED) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07001354 // no ramp for the first volume setting
1355 track->mFillingUpStatus = Track::FS_ACTIVE;
1356 if (track->mState == TrackBase::RESUMING) {
1357 track->mState = TrackBase::ACTIVE;
1358 param = AudioMixer::RAMP_VOLUME;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001359 }
Eric Laurent08d3d1d2009-09-03 03:45:52 -07001360 } else if (cblk->server != 0) {
1361 // If the track is stopped before the first frame was mixed,
1362 // do not apply ramp
Eric Laurent9d91ad52009-07-17 12:17:14 -07001363 param = AudioMixer::RAMP_VOLUME;
1364 }
Eric Laurent08d3d1d2009-09-03 03:45:52 -07001365
Eric Laurent9d91ad52009-07-17 12:17:14 -07001366 mAudioMixer->setParameter(param, AudioMixer::VOLUME0, left);
1367 mAudioMixer->setParameter(param, AudioMixer::VOLUME1, right);
1368 mAudioMixer->setParameter(
1369 AudioMixer::TRACK,
1370 AudioMixer::FORMAT, track->format());
1371 mAudioMixer->setParameter(
1372 AudioMixer::TRACK,
1373 AudioMixer::CHANNEL_COUNT, track->channelCount());
1374 mAudioMixer->setParameter(
1375 AudioMixer::RESAMPLE,
1376 AudioMixer::SAMPLE_RATE,
1377 int(cblk->sampleRate));
1378
1379 // reset retry count
1380 track->mRetryCount = kMaxTrackRetries;
1381 enabledTracks++;
1382 } else {
1383 //LOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
1384 if (track->isStopped()) {
1385 track->reset();
1386 }
1387 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
1388 // We have consumed all the buffers of this track.
1389 // Remove it from the list of active tracks.
1390 tracksToRemove->add(track);
1391 mAudioMixer->disable(AudioMixer::MIXING);
1392 } else {
1393 // No buffers for this track. Give it a few chances to
1394 // fill a buffer, then remove it from active list.
1395 if (--(track->mRetryCount) <= 0) {
1396 LOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
1397 tracksToRemove->add(track);
1398 }
Eric Laurent08d3d1d2009-09-03 03:45:52 -07001399 // For tracks using static shared memory buffer, make sure that we have
Eric Laurent9d91ad52009-07-17 12:17:14 -07001400 // written enough data to audio hardware before disabling the track
1401 // NOTE: this condition with arrive before track->mRetryCount <= 0 so we
1402 // don't care about code removing track from active list above.
1403 if ((track->mSharedBuffer == 0) || (mBytesWritten >= mMinBytesToWrite)) {
1404 mAudioMixer->disable(AudioMixer::MIXING);
1405 } else {
1406 enabledTracks++;
1407 }
1408 }
1409 }
1410 }
1411
1412 // remove all the tracks that need to be...
1413 count = tracksToRemove->size();
1414 if (UNLIKELY(count)) {
1415 for (size_t i=0 ; i<count ; i++) {
1416 const sp<Track>& track = tracksToRemove->itemAt(i);
1417 mActiveTracks.remove(track);
1418 if (track->isTerminated()) {
1419 mTracks.remove(track);
1420 deleteTrackName_l(track->mName);
1421 }
1422 }
1423 }
1424
1425 return enabledTracks;
1426}
1427
1428void AudioFlinger::MixerThread::getTracks(
1429 SortedVector < sp<Track> >& tracks,
1430 SortedVector < wp<Track> >& activeTracks,
1431 int streamType)
1432{
1433 LOGV ("MixerThread::getTracks() mixer %p, mTracks.size %d, mActiveTracks.size %d", this, mTracks.size(), mActiveTracks.size());
1434 Mutex::Autolock _l(mLock);
1435 size_t size = mTracks.size();
1436 for (size_t i = 0; i < size; i++) {
1437 sp<Track> t = mTracks[i];
1438 if (t->type() == streamType) {
1439 tracks.add(t);
1440 int j = mActiveTracks.indexOf(t);
1441 if (j >= 0) {
1442 t = mActiveTracks[j].promote();
1443 if (t != NULL) {
1444 activeTracks.add(t);
1445 }
1446 }
1447 }
1448 }
1449
1450 size = activeTracks.size();
1451 for (size_t i = 0; i < size; i++) {
1452 mActiveTracks.remove(activeTracks[i]);
1453 }
1454
1455 size = tracks.size();
1456 for (size_t i = 0; i < size; i++) {
1457 sp<Track> t = tracks[i];
1458 mTracks.remove(t);
1459 deleteTrackName_l(t->name());
1460 }
1461}
1462
1463void AudioFlinger::MixerThread::putTracks(
1464 SortedVector < sp<Track> >& tracks,
1465 SortedVector < wp<Track> >& activeTracks)
1466{
1467 LOGV ("MixerThread::putTracks() mixer %p, tracks.size %d, activeTracks.size %d", this, tracks.size(), activeTracks.size());
1468 Mutex::Autolock _l(mLock);
1469 size_t size = tracks.size();
1470 for (size_t i = 0; i < size ; i++) {
1471 sp<Track> t = tracks[i];
1472 int name = getTrackName_l();
1473
1474 if (name < 0) return;
1475
1476 t->mName = name;
1477 t->mThread = this;
1478 mTracks.add(t);
1479
1480 int j = activeTracks.indexOf(t);
1481 if (j >= 0) {
1482 mActiveTracks.add(t);
Eric Laurent06437712009-09-01 05:56:26 -07001483 // force buffer refilling and no ramp volume when the track is mixed for the first time
1484 t->mFillingUpStatus = Track::FS_FILLING;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485 }
1486 }
1487}
1488
Eric Laurent9d91ad52009-07-17 12:17:14 -07001489// getTrackName_l() must be called with ThreadBase::mLock held
The Android Open Source Project22f8def2009-03-09 11:52:12 -07001490int AudioFlinger::MixerThread::getTrackName_l()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491{
1492 return mAudioMixer->getTrackName();
1493}
1494
Eric Laurent9d91ad52009-07-17 12:17:14 -07001495// deleteTrackName_l() must be called with ThreadBase::mLock held
The Android Open Source Project22f8def2009-03-09 11:52:12 -07001496void AudioFlinger::MixerThread::deleteTrackName_l(int name)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497{
1498 mAudioMixer->deleteTrackName(name);
1499}
1500
Eric Laurent9d91ad52009-07-17 12:17:14 -07001501// checkForNewParameters_l() must be called with ThreadBase::mLock held
1502bool AudioFlinger::MixerThread::checkForNewParameters_l()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503{
Eric Laurent9d91ad52009-07-17 12:17:14 -07001504 bool reconfig = false;
1505
Eric Laurent3464c012009-08-04 09:45:33 -07001506 while (!mNewParameters.isEmpty()) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07001507 status_t status = NO_ERROR;
Eric Laurent3464c012009-08-04 09:45:33 -07001508 String8 keyValuePair = mNewParameters[0];
1509 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001510 int value;
Eric Laurent3464c012009-08-04 09:45:33 -07001511
1512 mNewParameters.removeAt(0);
1513
Eric Laurent9d91ad52009-07-17 12:17:14 -07001514 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
1515 reconfig = true;
1516 }
1517 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
1518 if (value != AudioSystem::PCM_16_BIT) {
1519 status = BAD_VALUE;
1520 } else {
1521 reconfig = true;
1522 }
1523 }
1524 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
1525 if (value != AudioSystem::CHANNEL_OUT_STEREO) {
1526 status = BAD_VALUE;
1527 } else {
1528 reconfig = true;
1529 }
1530 }
1531 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
1532 // do not accept frame count changes if tracks are open as the track buffer
1533 // size depends on frame count and correct behavior would not be garantied
1534 // if frame count is changed after track creation
1535 if (!mTracks.isEmpty()) {
1536 status = INVALID_OPERATION;
1537 } else {
1538 reconfig = true;
1539 }
1540 }
1541 if (status == NO_ERROR) {
Eric Laurent3464c012009-08-04 09:45:33 -07001542 status = mOutput->setParameters(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001543 if (!mStandby && status == INVALID_OPERATION) {
1544 mOutput->standby();
1545 mStandby = true;
1546 mBytesWritten = 0;
Eric Laurent3464c012009-08-04 09:45:33 -07001547 status = mOutput->setParameters(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001548 }
1549 if (status == NO_ERROR && reconfig) {
1550 delete mAudioMixer;
1551 readOutputParameters();
1552 mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
1553 for (size_t i = 0; i < mTracks.size() ; i++) {
1554 int name = getTrackName_l();
1555 if (name < 0) break;
1556 mTracks[i]->mName = name;
Eric Laurent878c0e12009-08-10 08:15:12 -07001557 // limit track sample rate to 2 x new output sample rate
1558 if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) {
1559 mTracks[i]->mCblk->sampleRate = 2 * sampleRate();
1560 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07001561 }
Eric Laurent3464c012009-08-04 09:45:33 -07001562 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001563 }
1564 }
1565 mParamStatus = status;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001566 mParamCond.signal();
Eric Laurent3464c012009-08-04 09:45:33 -07001567 mWaitWorkCV.wait(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001568 }
1569 return reconfig;
1570}
1571
1572status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
1573{
1574 const size_t SIZE = 256;
1575 char buffer[SIZE];
1576 String8 result;
1577
1578 PlaybackThread::dumpInternals(fd, args);
1579
1580 snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames());
1581 result.append(buffer);
1582 write(fd, result.string(), result.size());
1583 return NO_ERROR;
1584}
1585
1586// ----------------------------------------------------------------------------
1587AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output)
1588 : PlaybackThread(audioFlinger, output),
1589 mLeftVolume (1.0), mRightVolume(1.0)
1590{
1591 mType = PlaybackThread::DIRECT;
1592}
1593
1594AudioFlinger::DirectOutputThread::~DirectOutputThread()
1595{
1596}
1597
1598
1599bool AudioFlinger::DirectOutputThread::threadLoop()
1600{
Eric Laurent3522c802009-09-07 08:38:38 -07001601 unsigned long sleepTime = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001602 sp<Track> trackToRemove;
1603 sp<Track> activeTrack;
1604 nsecs_t standbyTime = systemTime();
1605 int8_t *curBuf;
1606 size_t mixBufferSize = mFrameCount*mFrameSize;
1607
1608 while (!exitPending())
1609 {
1610 processConfigEvents();
1611
1612 { // scope for the mLock
1613
1614 Mutex::Autolock _l(mLock);
1615
1616 if (checkForNewParameters_l()) {
1617 mixBufferSize = mFrameCount*mFrameSize;
1618 }
1619
1620 // put audio hardware into standby after short delay
1621 if UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) ||
1622 mSuspended) {
1623 // wait until we have something to do...
1624 if (!mStandby) {
1625 LOGV("Audio hardware entering standby, mixer %p\n", this);
1626 mOutput->standby();
1627 mStandby = true;
1628 mBytesWritten = 0;
1629 }
1630
1631 if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
1632 // we're about to wait, flush the binder command buffer
1633 IPCThreadState::self()->flushCommands();
1634
1635 if (exitPending()) break;
1636
1637 LOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid());
1638 mWaitWorkCV.wait(mLock);
1639 LOGV("DirectOutputThread %p TID %d waking up in active mode\n", this, gettid());
1640
1641 if (mMasterMute == false) {
1642 char value[PROPERTY_VALUE_MAX];
1643 property_get("ro.audio.silent", value, "0");
1644 if (atoi(value)) {
1645 LOGD("Silence is golden");
1646 setMasterMute(true);
1647 }
1648 }
1649
1650 standbyTime = systemTime() + kStandbyTimeInNsecs;
Eric Laurent3522c802009-09-07 08:38:38 -07001651 sleepTime = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001652 continue;
1653 }
1654 }
1655
1656 // find out which tracks need to be processed
1657 if (mActiveTracks.size() != 0) {
1658 sp<Track> t = mActiveTracks[0].promote();
1659 if (t == 0) continue;
1660
1661 Track* const track = t.get();
1662 audio_track_cblk_t* cblk = track->cblk();
1663
1664 // The first time a track is added we wait
1665 // for all its buffers to be filled before processing it
1666 if (cblk->framesReady() && (track->isReady() || track->isStopped()) &&
1667 !track->isPaused())
1668 {
1669 //LOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
1670
1671 // compute volume for this track
1672 float left, right;
1673 if (track->isMuted() || mMasterMute || track->isPausing() ||
1674 mStreamTypes[track->type()].mute) {
1675 left = right = 0;
1676 if (track->isPausing()) {
1677 track->setPaused();
1678 }
1679 } else {
1680 float typeVolume = mStreamTypes[track->type()].volume;
1681 float v = mMasterVolume * typeVolume;
1682 float v_clamped = v * cblk->volume[0];
1683 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
1684 left = v_clamped/MAX_GAIN;
1685 v_clamped = v * cblk->volume[1];
1686 if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
1687 right = v_clamped/MAX_GAIN;
1688 }
1689
1690 if (left != mLeftVolume || right != mRightVolume) {
1691 mOutput->setVolume(left, right);
1692 left = mLeftVolume;
1693 right = mRightVolume;
1694 }
1695
1696 if (track->mFillingUpStatus == Track::FS_FILLED) {
1697 track->mFillingUpStatus = Track::FS_ACTIVE;
1698 if (track->mState == TrackBase::RESUMING) {
1699 track->mState = TrackBase::ACTIVE;
1700 }
1701 }
1702
1703 // reset retry count
1704 track->mRetryCount = kMaxTrackRetries;
1705 activeTrack = t;
1706 } else {
1707 //LOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
1708 if (track->isStopped()) {
1709 track->reset();
1710 }
1711 if (track->isTerminated() || track->isStopped() || track->isPaused()) {
1712 // We have consumed all the buffers of this track.
1713 // Remove it from the list of active tracks.
1714 trackToRemove = track;
1715 } else {
1716 // No buffers for this track. Give it a few chances to
1717 // fill a buffer, then remove it from active list.
1718 if (--(track->mRetryCount) <= 0) {
1719 LOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
1720 trackToRemove = track;
1721 }
1722
1723 // For tracks using static shared memry buffer, make sure that we have
1724 // written enough data to audio hardware before disabling the track
1725 // NOTE: this condition with arrive before track->mRetryCount <= 0 so we
1726 // don't care about code removing track from active list above.
1727 if ((track->mSharedBuffer != 0) && (mBytesWritten < mMinBytesToWrite)) {
1728 activeTrack = t;
1729 }
1730 }
1731 }
1732 }
1733
1734 // remove all the tracks that need to be...
1735 if (UNLIKELY(trackToRemove != 0)) {
1736 mActiveTracks.remove(trackToRemove);
1737 if (trackToRemove->isTerminated()) {
1738 mTracks.remove(trackToRemove);
1739 deleteTrackName_l(trackToRemove->mName);
1740 }
1741 }
1742 }
1743
Eric Laurentaef692f2009-09-22 00:35:48 -07001744 if (activeTrack != 0) {
1745 AudioBufferProvider::Buffer buffer;
1746 size_t frameCount = mFrameCount;
1747 curBuf = (int8_t *)mMixBuffer;
1748 // output audio to hardware
1749 while(frameCount) {
1750 buffer.frameCount = frameCount;
1751 activeTrack->getNextBuffer(&buffer);
1752 if (UNLIKELY(buffer.raw == 0)) {
1753 memset(curBuf, 0, frameCount * mFrameSize);
1754 break;
1755 }
1756 memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
1757 frameCount -= buffer.frameCount;
1758 curBuf += buffer.frameCount * mFrameSize;
1759 activeTrack->releaseBuffer(&buffer);
1760 }
1761 sleepTime = 0;
1762 standbyTime = systemTime() + kStandbyTimeInNsecs;
Eric Laurent3522c802009-09-07 08:38:38 -07001763 } else {
Eric Laurentaef692f2009-09-22 00:35:48 -07001764 sleepTime += kBufferRecoveryInUsecs;
1765 if (sleepTime > kMaxBufferRecoveryInUsecs) {
1766 sleepTime = kMaxBufferRecoveryInUsecs;
1767 }
1768 // There was nothing to mix this round, which means all
1769 // active tracks were late. Sleep a little bit to give
1770 // them another chance. If we're too late, write 0s to audio
1771 // hardware to avoid underrun.
1772 if (mBytesWritten != 0 && sleepTime >= kMaxBufferRecoveryInUsecs &&
1773 AudioSystem::isLinearPCM(mFormat)) {
1774 memset (mMixBuffer, 0, mFrameCount * mFrameSize);
Eric Laurent3522c802009-09-07 08:38:38 -07001775 sleepTime = 0;
Eric Laurent3522c802009-09-07 08:38:38 -07001776 }
Eric Laurentaef692f2009-09-22 00:35:48 -07001777 }
Eric Laurent3522c802009-09-07 08:38:38 -07001778
Eric Laurentaef692f2009-09-22 00:35:48 -07001779 if (mSuspended) {
1780 sleepTime = kMaxBufferRecoveryInUsecs;
1781 }
1782 // sleepTime == 0 means we must write to audio hardware
1783 if (sleepTime == 0) {
1784 mLastWriteTime = systemTime();
1785 mInWrite = true;
1786 int bytesWritten = (int)mOutput->write(mMixBuffer, mixBufferSize);
1787 if (bytesWritten) mBytesWritten += bytesWritten;
1788 mNumWrites++;
1789 mInWrite = false;
1790 mStandby = false;
1791 } else {
1792 usleep(sleepTime);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001793 }
1794
1795 // finally let go of removed track, without the lock held
1796 // since we can't guarantee the destructors won't acquire that
1797 // same lock.
1798 trackToRemove.clear();
1799 activeTrack.clear();
1800 }
1801
1802 if (!mStandby) {
1803 mOutput->standby();
1804 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07001805
1806 LOGV("DirectOutputThread %p exiting", this);
1807 return false;
1808}
1809
1810// getTrackName_l() must be called with ThreadBase::mLock held
1811int AudioFlinger::DirectOutputThread::getTrackName_l()
1812{
1813 return 0;
1814}
1815
1816// deleteTrackName_l() must be called with ThreadBase::mLock held
1817void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name)
1818{
1819}
1820
1821// checkForNewParameters_l() must be called with ThreadBase::mLock held
1822bool AudioFlinger::DirectOutputThread::checkForNewParameters_l()
1823{
1824 bool reconfig = false;
1825
Eric Laurent3464c012009-08-04 09:45:33 -07001826 while (!mNewParameters.isEmpty()) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07001827 status_t status = NO_ERROR;
Eric Laurent3464c012009-08-04 09:45:33 -07001828 String8 keyValuePair = mNewParameters[0];
1829 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001830 int value;
Eric Laurent3464c012009-08-04 09:45:33 -07001831
1832 mNewParameters.removeAt(0);
1833
Eric Laurent9d91ad52009-07-17 12:17:14 -07001834 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
1835 // do not accept frame count changes if tracks are open as the track buffer
1836 // size depends on frame count and correct behavior would not be garantied
1837 // if frame count is changed after track creation
1838 if (!mTracks.isEmpty()) {
1839 status = INVALID_OPERATION;
1840 } else {
1841 reconfig = true;
1842 }
1843 }
1844 if (status == NO_ERROR) {
Eric Laurent3464c012009-08-04 09:45:33 -07001845 status = mOutput->setParameters(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001846 if (!mStandby && status == INVALID_OPERATION) {
1847 mOutput->standby();
1848 mStandby = true;
1849 mBytesWritten = 0;
Eric Laurent3464c012009-08-04 09:45:33 -07001850 status = mOutput->setParameters(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001851 }
1852 if (status == NO_ERROR && reconfig) {
1853 readOutputParameters();
Eric Laurent3464c012009-08-04 09:45:33 -07001854 sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001855 }
1856 }
1857 mParamStatus = status;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001858 mParamCond.signal();
Eric Laurent3464c012009-08-04 09:45:33 -07001859 mWaitWorkCV.wait(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001860 }
1861 return reconfig;
The Android Open Source Projecte41dd752009-01-22 00:13:42 -08001862}
1863
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001864// ----------------------------------------------------------------------------
1865
Eric Laurent9d91ad52009-07-17 12:17:14 -07001866AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread)
1867 : MixerThread(audioFlinger, mainThread->getOutput())
1868{
1869 mType = PlaybackThread::DUPLICATING;
1870 addOutputTrack(mainThread);
1871}
1872
1873AudioFlinger::DuplicatingThread::~DuplicatingThread()
1874{
1875 mOutputTracks.clear();
1876}
1877
1878bool AudioFlinger::DuplicatingThread::threadLoop()
1879{
1880 unsigned long sleepTime = kBufferRecoveryInUsecs;
1881 int16_t* curBuf = mMixBuffer;
1882 Vector< sp<Track> > tracksToRemove;
1883 size_t enabledTracks = 0;
1884 nsecs_t standbyTime = systemTime();
1885 size_t mixBufferSize = mFrameCount*mFrameSize;
1886 SortedVector< sp<OutputTrack> > outputTracks;
1887
1888 while (!exitPending())
1889 {
1890 processConfigEvents();
1891
1892 enabledTracks = 0;
1893 { // scope for the mLock
1894
1895 Mutex::Autolock _l(mLock);
1896
1897 if (checkForNewParameters_l()) {
1898 mixBufferSize = mFrameCount*mFrameSize;
1899 }
1900
1901 const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
1902
1903 for (size_t i = 0; i < mOutputTracks.size(); i++) {
1904 outputTracks.add(mOutputTracks[i]);
1905 }
1906
1907 // put audio hardware into standby after short delay
1908 if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
1909 mSuspended) {
1910 if (!mStandby) {
1911 for (size_t i = 0; i < outputTracks.size(); i++) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07001912 outputTracks[i]->stop();
Eric Laurent9d91ad52009-07-17 12:17:14 -07001913 }
1914 mStandby = true;
1915 mBytesWritten = 0;
1916 }
1917
1918 if (!activeTracks.size() && mConfigEvents.isEmpty()) {
1919 // we're about to wait, flush the binder command buffer
1920 IPCThreadState::self()->flushCommands();
1921 outputTracks.clear();
1922
1923 if (exitPending()) break;
1924
1925 LOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid());
1926 mWaitWorkCV.wait(mLock);
1927 LOGV("DuplicatingThread %p TID %d waking up\n", this, gettid());
1928 if (mMasterMute == false) {
1929 char value[PROPERTY_VALUE_MAX];
1930 property_get("ro.audio.silent", value, "0");
1931 if (atoi(value)) {
1932 LOGD("Silence is golden");
1933 setMasterMute(true);
1934 }
1935 }
1936
1937 standbyTime = systemTime() + kStandbyTimeInNsecs;
1938 sleepTime = kBufferRecoveryInUsecs;
1939 continue;
1940 }
1941 }
1942
1943 enabledTracks = prepareTracks_l(activeTracks, &tracksToRemove);
Eric Laurentaef692f2009-09-22 00:35:48 -07001944 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07001945
Eric Laurent9d91ad52009-07-17 12:17:14 -07001946 if (LIKELY(enabledTracks)) {
1947 // mix buffers...
1948 mAudioMixer->process(curBuf);
Eric Laurentaef692f2009-09-22 00:35:48 -07001949 sleepTime = 0;
1950 standbyTime = systemTime() + kStandbyTimeInNsecs;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001951 } else {
Eric Laurentaef692f2009-09-22 00:35:48 -07001952 sleepTime += kBufferRecoveryInUsecs;
1953 if (sleepTime > kMaxBufferRecoveryInUsecs) {
1954 sleepTime = kMaxBufferRecoveryInUsecs;
1955 }
1956 // There was nothing to mix this round, which means all
1957 // active tracks were late. Sleep a little bit to give
1958 // them another chance. If we're too late, write 0s to audio
1959 // hardware to avoid underrun.
1960 if (mBytesWritten != 0 && sleepTime >= kMaxBufferRecoveryInUsecs) {
1961 memset (curBuf, 0, mixBufferSize);
1962 sleepTime = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001963 }
1964 }
Eric Laurentaef692f2009-09-22 00:35:48 -07001965
1966 if (mSuspended) {
1967 sleepTime = kMaxBufferRecoveryInUsecs;
1968 }
1969 // sleepTime == 0 means we must write to audio hardware
1970 if (sleepTime == 0) {
1971 for (size_t i = 0; i < outputTracks.size(); i++) {
1972 outputTracks[i]->write(curBuf, mFrameCount);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001973 }
Eric Laurentaef692f2009-09-22 00:35:48 -07001974 mStandby = false;
1975 mBytesWritten += mixBufferSize;
Eric Laurent9d91ad52009-07-17 12:17:14 -07001976 } else {
Eric Laurentaef692f2009-09-22 00:35:48 -07001977 usleep(sleepTime);
Eric Laurent9d91ad52009-07-17 12:17:14 -07001978 }
1979
1980 // finally let go of all our tracks, without the lock held
1981 // since we can't guarantee the destructors won't acquire that
1982 // same lock.
1983 tracksToRemove.clear();
1984 outputTracks.clear();
1985 }
1986
Eric Laurentf5aba822009-08-10 23:22:32 -07001987 { // scope for the mLock
1988
1989 Mutex::Autolock _l(mLock);
1990 if (!mStandby) {
1991 LOGV("DuplicatingThread() exiting out of standby");
1992 for (size_t i = 0; i < mOutputTracks.size(); i++) {
1993 mOutputTracks[i]->destroy();
1994 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07001995 }
1996 }
1997
Eric Laurent9d91ad52009-07-17 12:17:14 -07001998 return false;
1999}
2000
2001void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
2002{
2003 int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate();
2004 OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread,
2005 mSampleRate,
2006 mFormat,
2007 mChannelCount,
2008 frameCount);
Eric Laurentf5aba822009-08-10 23:22:32 -07002009 if (outputTrack->cblk() != NULL) {
2010 thread->setStreamVolume(AudioSystem::NUM_STREAM_TYPES, 1.0f);
2011 mOutputTracks.add(outputTrack);
2012 LOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
2013 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002014}
2015
2016void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
2017{
2018 Mutex::Autolock _l(mLock);
2019 for (size_t i = 0; i < mOutputTracks.size(); i++) {
2020 if (mOutputTracks[i]->thread() == (ThreadBase *)thread) {
Eric Laurentf5aba822009-08-10 23:22:32 -07002021 mOutputTracks[i]->destroy();
Eric Laurent9d91ad52009-07-17 12:17:14 -07002022 mOutputTracks.removeAt(i);
2023 return;
2024 }
2025 }
2026 LOGV("removeOutputTrack(): unkonwn thread: %p", thread);
2027}
2028
2029
2030// ----------------------------------------------------------------------------
2031
The Android Open Source Project22f8def2009-03-09 11:52:12 -07002032// TrackBase constructor must be called with AudioFlinger::mLock held
Eric Laurent9d91ad52009-07-17 12:17:14 -07002033AudioFlinger::ThreadBase::TrackBase::TrackBase(
2034 const wp<ThreadBase>& thread,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002035 const sp<Client>& client,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002036 uint32_t sampleRate,
2037 int format,
2038 int channelCount,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002039 int frameCount,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002040 uint32_t flags,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002041 const sp<IMemory>& sharedBuffer)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002042 : RefBase(),
Eric Laurent9d91ad52009-07-17 12:17:14 -07002043 mThread(thread),
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002044 mClient(client),
Eric Laurent6ad8c642009-09-09 05:16:08 -07002045 mCblk(0),
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002046 mFrameCount(0),
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002047 mState(IDLE),
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002048 mClientTid(-1),
2049 mFormat(format),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002050 mFlags(flags & ~SYSTEM_FLAGS_MASK)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002051{
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002052 LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
2053
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002054 // LOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002055 size_t size = sizeof(audio_track_cblk_t);
2056 size_t bufferSize = frameCount*channelCount*sizeof(int16_t);
2057 if (sharedBuffer == 0) {
2058 size += bufferSize;
2059 }
2060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002061 if (client != NULL) {
2062 mCblkMemory = client->heap()->allocate(size);
2063 if (mCblkMemory != 0) {
2064 mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
2065 if (mCblk) { // construct the shared structure in-place.
2066 new(mCblk) audio_track_cblk_t();
2067 // clear all buffers
2068 mCblk->frameCount = frameCount;
Eric Laurent0bac5382009-07-07 07:10:45 -07002069 mCblk->sampleRate = sampleRate;
2070 mCblk->channels = (uint8_t)channelCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002071 if (sharedBuffer == 0) {
2072 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
2073 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
2074 // Force underrun condition to avoid false underrun callback until first data is
2075 // written to buffer
2076 mCblk->flowControlFlag = 1;
2077 } else {
2078 mBuffer = sharedBuffer->pointer();
2079 }
2080 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002081 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082 } else {
2083 LOGE("not enough memory for AudioTrack size=%u", size);
2084 client->heap()->dump("AudioTrack");
2085 return;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002086 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002087 } else {
2088 mCblk = (audio_track_cblk_t *)(new uint8_t[size]);
2089 if (mCblk) { // construct the shared structure in-place.
2090 new(mCblk) audio_track_cblk_t();
2091 // clear all buffers
2092 mCblk->frameCount = frameCount;
Eric Laurent0bac5382009-07-07 07:10:45 -07002093 mCblk->sampleRate = sampleRate;
2094 mCblk->channels = (uint8_t)channelCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
2096 memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
2097 // Force underrun condition to avoid false underrun callback until first data is
2098 // written to buffer
2099 mCblk->flowControlFlag = 1;
2100 mBufferEnd = (uint8_t *)mBuffer + bufferSize;
2101 }
2102 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002103}
2104
Eric Laurentbdc0f842009-09-16 06:02:45 -07002105AudioFlinger::ThreadBase::TrackBase::~TrackBase()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002106{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107 if (mCblk) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07002108 mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
2109 if (mClient == NULL) {
2110 delete mCblk;
2111 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002112 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002113 mCblkMemory.clear(); // and free the shared memory
Eric Laurent0f8ab672009-09-17 05:12:56 -07002114 if (mClient != NULL) {
2115 Mutex::Autolock _l(mClient->audioFlinger()->mLock);
2116 mClient.clear();
2117 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002118}
2119
Eric Laurentbdc0f842009-09-16 06:02:45 -07002120void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002121{
2122 buffer->raw = 0;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002123 mFrameCount = buffer->frameCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002124 step();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002125 buffer->frameCount = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002126}
2127
Eric Laurentbdc0f842009-09-16 06:02:45 -07002128bool AudioFlinger::ThreadBase::TrackBase::step() {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002129 bool result;
2130 audio_track_cblk_t* cblk = this->cblk();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002131
2132 result = cblk->stepServer(mFrameCount);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002133 if (!result) {
2134 LOGV("stepServer failed acquiring cblk mutex");
2135 mFlags |= STEPSERVER_FAILED;
2136 }
2137 return result;
2138}
2139
Eric Laurentbdc0f842009-09-16 06:02:45 -07002140void AudioFlinger::ThreadBase::TrackBase::reset() {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002141 audio_track_cblk_t* cblk = this->cblk();
2142
2143 cblk->user = 0;
2144 cblk->server = 0;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002145 cblk->userBase = 0;
2146 cblk->serverBase = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002147 mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002148 LOGV("TrackBase::reset");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002149}
2150
Eric Laurentbdc0f842009-09-16 06:02:45 -07002151sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002152{
2153 return mCblkMemory;
2154}
2155
Eric Laurentbdc0f842009-09-16 06:02:45 -07002156int AudioFlinger::ThreadBase::TrackBase::sampleRate() const {
The Android Open Source Project4f68be12009-03-18 17:39:46 -07002157 return (int)mCblk->sampleRate;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002158}
2159
Eric Laurentbdc0f842009-09-16 06:02:45 -07002160int AudioFlinger::ThreadBase::TrackBase::channelCount() const {
Eric Laurent0bac5382009-07-07 07:10:45 -07002161 return (int)mCblk->channels;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002162}
2163
Eric Laurentbdc0f842009-09-16 06:02:45 -07002164void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002165 audio_track_cblk_t* cblk = this->cblk();
Eric Laurent9d91ad52009-07-17 12:17:14 -07002166 int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*cblk->frameSize;
2167 int8_t *bufferEnd = bufferStart + frames * cblk->frameSize;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002168
2169 // Check validity of returned pointer in case the track control block would have been corrupted.
Eric Laurent9d91ad52009-07-17 12:17:14 -07002170 if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd ||
2171 ((unsigned long)bufferStart & (unsigned long)(cblk->frameSize - 1))) {
The Android Open Source Project4f68be12009-03-18 17:39:46 -07002172 LOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \
2173 server %d, serverBase %d, user %d, userBase %d, channels %d",
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002174 bufferStart, bufferEnd, mBuffer, mBufferEnd,
The Android Open Source Project4f68be12009-03-18 17:39:46 -07002175 cblk->server, cblk->serverBase, cblk->user, cblk->userBase, cblk->channels);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002176 return 0;
2177 }
2178
2179 return bufferStart;
2180}
2181
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002182// ----------------------------------------------------------------------------
2183
Eric Laurent9d91ad52009-07-17 12:17:14 -07002184// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
2185AudioFlinger::PlaybackThread::Track::Track(
2186 const wp<ThreadBase>& thread,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002187 const sp<Client>& client,
2188 int streamType,
2189 uint32_t sampleRate,
2190 int format,
2191 int channelCount,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002192 int frameCount,
2193 const sp<IMemory>& sharedBuffer)
Eric Laurent9d91ad52009-07-17 12:17:14 -07002194 : TrackBase(thread, client, sampleRate, format, channelCount, frameCount, 0, sharedBuffer),
2195 mMute(false), mSharedBuffer(sharedBuffer), mName(-1)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002196{
Eric Laurent6ad8c642009-09-09 05:16:08 -07002197 if (mCblk != NULL) {
2198 sp<ThreadBase> baseThread = thread.promote();
2199 if (baseThread != 0) {
2200 PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get();
2201 mName = playbackThread->getTrackName_l();
2202 }
2203 LOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid());
2204 if (mName < 0) {
2205 LOGE("no more track names available");
2206 }
2207 mVolume[0] = 1.0f;
2208 mVolume[1] = 1.0f;
2209 mStreamType = streamType;
2210 // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
2211 // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
2212 mCblk->frameSize = AudioSystem::isLinearPCM(format) ? channelCount * sizeof(int16_t) : sizeof(int8_t);
Eric Laurent9d91ad52009-07-17 12:17:14 -07002213 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002214}
2215
Eric Laurent9d91ad52009-07-17 12:17:14 -07002216AudioFlinger::PlaybackThread::Track::~Track()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002217{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002218 LOGV("PlaybackThread::Track destructor");
2219 sp<ThreadBase> thread = mThread.promote();
2220 if (thread != 0) {
2221 Mutex::Autolock _l(thread->mLock);
2222 mState = TERMINATED;
The Android Open Source Project22f8def2009-03-09 11:52:12 -07002223 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002224}
2225
Eric Laurent9d91ad52009-07-17 12:17:14 -07002226void AudioFlinger::PlaybackThread::Track::destroy()
2227{
2228 // NOTE: destroyTrack_l() can remove a strong reference to this Track
2229 // by removing it from mTracks vector, so there is a risk that this Tracks's
2230 // desctructor is called. As the destructor needs to lock mLock,
2231 // we must acquire a strong reference on this Track before locking mLock
2232 // here so that the destructor is called only when exiting this function.
2233 // On the other hand, as long as Track::destroy() is only called by
2234 // TrackHandle destructor, the TrackHandle still holds a strong ref on
2235 // this Track with its member mTrack.
2236 sp<Track> keep(this);
2237 { // scope for mLock
2238 sp<ThreadBase> thread = mThread.promote();
2239 if (thread != 0) {
2240 Mutex::Autolock _l(thread->mLock);
2241 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2242 playbackThread->destroyTrack_l(this);
2243 }
2244 }
2245}
2246
2247void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002248{
2249 snprintf(buffer, size, " %5d %5d %3u %3u %3u %3u %1d %1d %1d %5u %5u %5u %04x %04x\n",
2250 mName - AudioMixer::TRACK0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002251 (mClient == NULL) ? getpid() : mClient->pid(),
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002252 mStreamType,
2253 mFormat,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002254 mCblk->channels,
2255 mFrameCount,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002256 mState,
2257 mMute,
2258 mFillingUpStatus,
2259 mCblk->sampleRate,
2260 mCblk->volume[0],
2261 mCblk->volume[1],
2262 mCblk->server,
2263 mCblk->user);
2264}
2265
Eric Laurent9d91ad52009-07-17 12:17:14 -07002266status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002267{
2268 audio_track_cblk_t* cblk = this->cblk();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002269 uint32_t framesReady;
2270 uint32_t framesReq = buffer->frameCount;
2271
2272 // Check if last stepServer failed, try to step now
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002273 if (mFlags & TrackBase::STEPSERVER_FAILED) {
2274 if (!step()) goto getNextBuffer_exit;
2275 LOGV("stepServer recovered");
2276 mFlags &= ~TrackBase::STEPSERVER_FAILED;
2277 }
2278
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002279 framesReady = cblk->framesReady();
2280
2281 if (LIKELY(framesReady)) {
2282 uint32_t s = cblk->server;
2283 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
2284
2285 bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd;
2286 if (framesReq > framesReady) {
2287 framesReq = framesReady;
2288 }
2289 if (s + framesReq > bufferEnd) {
2290 framesReq = bufferEnd - s;
2291 }
2292
2293 buffer->raw = getBuffer(s, framesReq);
2294 if (buffer->raw == 0) goto getNextBuffer_exit;
2295
2296 buffer->frameCount = framesReq;
2297 return NO_ERROR;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002298 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002299
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002300getNextBuffer_exit:
2301 buffer->raw = 0;
2302 buffer->frameCount = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002303 LOGV("getNextBuffer() no more data");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002304 return NOT_ENOUGH_DATA;
2305}
2306
Eric Laurent9d91ad52009-07-17 12:17:14 -07002307bool AudioFlinger::PlaybackThread::Track::isReady() const {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002308 if (mFillingUpStatus != FS_FILLING) return true;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002309
2310 if (mCblk->framesReady() >= mCblk->frameCount ||
2311 mCblk->forceReady) {
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002312 mFillingUpStatus = FS_FILLED;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002313 mCblk->forceReady = 0;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002314 return true;
2315 }
2316 return false;
2317}
2318
Eric Laurent9d91ad52009-07-17 12:17:14 -07002319status_t AudioFlinger::PlaybackThread::Track::start()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002320{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002321 LOGV("start(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid());
2322 sp<ThreadBase> thread = mThread.promote();
2323 if (thread != 0) {
2324 Mutex::Autolock _l(thread->mLock);
2325 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2326 playbackThread->addTrack_l(this);
2327 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002328 return NO_ERROR;
2329}
2330
Eric Laurent9d91ad52009-07-17 12:17:14 -07002331void AudioFlinger::PlaybackThread::Track::stop()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002332{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002333 LOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid());
2334 sp<ThreadBase> thread = mThread.promote();
2335 if (thread != 0) {
2336 Mutex::Autolock _l(thread->mLock);
2337 if (mState > STOPPED) {
2338 mState = STOPPED;
2339 // If the track is not active (PAUSED and buffers full), flush buffers
2340 PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
2341 if (playbackThread->mActiveTracks.indexOf(this) < 0) {
2342 reset();
2343 }
2344 LOGV("(> STOPPED) => STOPPED (%d)", mName);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002345 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002346 }
2347}
2348
Eric Laurent9d91ad52009-07-17 12:17:14 -07002349void AudioFlinger::PlaybackThread::Track::pause()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002350{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002351 LOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid());
Eric Laurent9d91ad52009-07-17 12:17:14 -07002352 sp<ThreadBase> thread = mThread.promote();
2353 if (thread != 0) {
2354 Mutex::Autolock _l(thread->mLock);
2355 if (mState == ACTIVE || mState == RESUMING) {
2356 mState = PAUSING;
2357 LOGV("ACTIVE/RESUMING => PAUSING (%d)", mName);
2358 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002359 }
2360}
2361
Eric Laurent9d91ad52009-07-17 12:17:14 -07002362void AudioFlinger::PlaybackThread::Track::flush()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002363{
2364 LOGV("flush(%d)", mName);
Eric Laurent9d91ad52009-07-17 12:17:14 -07002365 sp<ThreadBase> thread = mThread.promote();
2366 if (thread != 0) {
2367 Mutex::Autolock _l(thread->mLock);
2368 if (mState != STOPPED && mState != PAUSED && mState != PAUSING) {
2369 return;
2370 }
2371 // No point remaining in PAUSED state after a flush => go to
2372 // STOPPED state
2373 mState = STOPPED;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002374
Eric Laurent9d91ad52009-07-17 12:17:14 -07002375 mCblk->lock.lock();
2376 // NOTE: reset() will reset cblk->user and cblk->server with
2377 // the risk that at the same time, the AudioMixer is trying to read
2378 // data. In this case, getNextBuffer() would return a NULL pointer
2379 // as audio buffer => the AudioMixer code MUST always test that pointer
2380 // returned by getNextBuffer() is not NULL!
2381 reset();
2382 mCblk->lock.unlock();
2383 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002384}
2385
Eric Laurent9d91ad52009-07-17 12:17:14 -07002386void AudioFlinger::PlaybackThread::Track::reset()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002387{
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002388 // Do not reset twice to avoid discarding data written just after a flush and before
2389 // the audioflinger thread detects the track is stopped.
2390 if (!mResetDone) {
2391 TrackBase::reset();
2392 // Force underrun condition to avoid false underrun callback until first data is
2393 // written to buffer
2394 mCblk->flowControlFlag = 1;
2395 mCblk->forceReady = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002396 mFillingUpStatus = FS_FILLING;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002397 mResetDone = true;
2398 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002399}
2400
Eric Laurent9d91ad52009-07-17 12:17:14 -07002401void AudioFlinger::PlaybackThread::Track::mute(bool muted)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002402{
2403 mMute = muted;
2404}
2405
Eric Laurent9d91ad52009-07-17 12:17:14 -07002406void AudioFlinger::PlaybackThread::Track::setVolume(float left, float right)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002407{
2408 mVolume[0] = left;
2409 mVolume[1] = right;
2410}
2411
2412// ----------------------------------------------------------------------------
2413
The Android Open Source Project22f8def2009-03-09 11:52:12 -07002414// RecordTrack constructor must be called with AudioFlinger::mLock held
Eric Laurent9d91ad52009-07-17 12:17:14 -07002415AudioFlinger::RecordThread::RecordTrack::RecordTrack(
2416 const wp<ThreadBase>& thread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002417 const sp<Client>& client,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002418 uint32_t sampleRate,
2419 int format,
2420 int channelCount,
2421 int frameCount,
2422 uint32_t flags)
Eric Laurent9d91ad52009-07-17 12:17:14 -07002423 : TrackBase(thread, client, sampleRate, format,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002424 channelCount, frameCount, flags, 0),
Eric Laurent9d91ad52009-07-17 12:17:14 -07002425 mOverflow(false)
2426{
Eric Laurent6ad8c642009-09-09 05:16:08 -07002427 if (mCblk != NULL) {
2428 LOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
2429 if (format == AudioSystem::PCM_16_BIT) {
2430 mCblk->frameSize = channelCount * sizeof(int16_t);
2431 } else if (format == AudioSystem::PCM_8_BIT) {
2432 mCblk->frameSize = channelCount * sizeof(int8_t);
2433 } else {
2434 mCblk->frameSize = sizeof(int8_t);
2435 }
2436 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002437}
2438
2439AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002440{
2441}
2442
Eric Laurent9d91ad52009-07-17 12:17:14 -07002443status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444{
2445 audio_track_cblk_t* cblk = this->cblk();
2446 uint32_t framesAvail;
2447 uint32_t framesReq = buffer->frameCount;
2448
2449 // Check if last stepServer failed, try to step now
2450 if (mFlags & TrackBase::STEPSERVER_FAILED) {
2451 if (!step()) goto getNextBuffer_exit;
2452 LOGV("stepServer recovered");
2453 mFlags &= ~TrackBase::STEPSERVER_FAILED;
2454 }
2455
2456 framesAvail = cblk->framesAvailable_l();
2457
2458 if (LIKELY(framesAvail)) {
2459 uint32_t s = cblk->server;
2460 uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
2461
2462 if (framesReq > framesAvail) {
2463 framesReq = framesAvail;
2464 }
2465 if (s + framesReq > bufferEnd) {
2466 framesReq = bufferEnd - s;
2467 }
2468
2469 buffer->raw = getBuffer(s, framesReq);
2470 if (buffer->raw == 0) goto getNextBuffer_exit;
2471
2472 buffer->frameCount = framesReq;
2473 return NO_ERROR;
2474 }
2475
2476getNextBuffer_exit:
2477 buffer->raw = 0;
2478 buffer->frameCount = 0;
2479 return NOT_ENOUGH_DATA;
2480}
2481
Eric Laurent9d91ad52009-07-17 12:17:14 -07002482status_t AudioFlinger::RecordThread::RecordTrack::start()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002484 sp<ThreadBase> thread = mThread.promote();
2485 if (thread != 0) {
2486 RecordThread *recordThread = (RecordThread *)thread.get();
2487 return recordThread->start(this);
2488 }
2489 return NO_INIT;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002490}
2491
Eric Laurent9d91ad52009-07-17 12:17:14 -07002492void AudioFlinger::RecordThread::RecordTrack::stop()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002493{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002494 sp<ThreadBase> thread = mThread.promote();
2495 if (thread != 0) {
2496 RecordThread *recordThread = (RecordThread *)thread.get();
2497 recordThread->stop(this);
2498 TrackBase::reset();
2499 // Force overerrun condition to avoid false overrun callback until first data is
2500 // read from buffer
2501 mCblk->flowControlFlag = 1;
2502 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002503}
2504
2505
2506// ----------------------------------------------------------------------------
2507
Eric Laurent9d91ad52009-07-17 12:17:14 -07002508AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
2509 const wp<ThreadBase>& thread,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510 uint32_t sampleRate,
2511 int format,
2512 int channelCount,
2513 int frameCount)
Eric Laurent9d91ad52009-07-17 12:17:14 -07002514 : Track(thread, NULL, AudioSystem::NUM_STREAM_TYPES, sampleRate, format, channelCount, frameCount, NULL),
2515 mActive(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002517
2518 PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get();
Eric Laurentf5aba822009-08-10 23:22:32 -07002519 if (mCblk != NULL) {
2520 mCblk->out = 1;
2521 mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
2522 mCblk->volume[0] = mCblk->volume[1] = 0x1000;
2523 mOutBuffer.frameCount = 0;
2524 mWaitTimeMs = (playbackThread->frameCount() * 2 * 1000) / playbackThread->sampleRate();
2525 playbackThread->mTracks.add(this);
2526 LOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, mCblk->frameCount %d, mCblk->sampleRate %d, mCblk->channels %d mBufferEnd %p mWaitTimeMs %d",
2527 mCblk, mBuffer, mCblk->buffers, mCblk->frameCount, mCblk->sampleRate, mCblk->channels, mBufferEnd, mWaitTimeMs);
2528 } else {
2529 LOGW("Error creating output track on thread %p", playbackThread);
2530 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002531}
2532
Eric Laurent9d91ad52009-07-17 12:17:14 -07002533AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534{
Eric Laurentf5aba822009-08-10 23:22:32 -07002535 clearBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536}
2537
Eric Laurent9d91ad52009-07-17 12:17:14 -07002538status_t AudioFlinger::PlaybackThread::OutputTrack::start()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002539{
2540 status_t status = Track::start();
Eric Laurent9d91ad52009-07-17 12:17:14 -07002541 if (status != NO_ERROR) {
2542 return status;
2543 }
2544
2545 mActive = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546 mRetryCount = 127;
2547 return status;
2548}
2549
Eric Laurent9d91ad52009-07-17 12:17:14 -07002550void AudioFlinger::PlaybackThread::OutputTrack::stop()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551{
2552 Track::stop();
2553 clearBufferQueue();
2554 mOutBuffer.frameCount = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002555 mActive = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556}
2557
Eric Laurent9d91ad52009-07-17 12:17:14 -07002558bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559{
2560 Buffer *pInBuffer;
2561 Buffer inBuffer;
2562 uint32_t channels = mCblk->channels;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002563 bool outputBufferFull = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002564 inBuffer.frameCount = frames;
2565 inBuffer.i16 = data;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002566
2567 uint32_t waitTimeLeftMs = mWaitTimeMs;
2568
2569 if (!mActive) {
2570 start();
2571 sp<ThreadBase> thread = mThread.promote();
2572 if (thread != 0) {
2573 MixerThread *mixerThread = (MixerThread *)thread.get();
2574 if (mCblk->frameCount > frames){
2575 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
2576 uint32_t startFrames = (mCblk->frameCount - frames);
2577 pInBuffer = new Buffer;
2578 pInBuffer->mBuffer = new int16_t[startFrames * channels];
2579 pInBuffer->frameCount = startFrames;
2580 pInBuffer->i16 = pInBuffer->mBuffer;
2581 memset(pInBuffer->raw, 0, startFrames * channels * sizeof(int16_t));
2582 mBufferQueue.add(pInBuffer);
2583 } else {
2584 LOGW ("OutputTrack::write() %p no more buffers in queue", this);
2585 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002586 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002587 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002588 }
2589
Eric Laurent9d91ad52009-07-17 12:17:14 -07002590 while (waitTimeLeftMs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591 // First write pending buffers, then new data
2592 if (mBufferQueue.size()) {
2593 pInBuffer = mBufferQueue.itemAt(0);
2594 } else {
2595 pInBuffer = &inBuffer;
2596 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002597
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002598 if (pInBuffer->frameCount == 0) {
2599 break;
2600 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002601
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002602 if (mOutBuffer.frameCount == 0) {
2603 mOutBuffer.frameCount = pInBuffer->frameCount;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002604 nsecs_t startTime = systemTime();
2605 if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)AudioTrack::NO_MORE_BUFFERS) {
2606 LOGV ("OutputTrack::write() %p no more output buffers", this);
2607 outputBufferFull = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002608 break;
2609 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002610 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
2611// LOGV("OutputTrack::write() waitTimeMs %d waitTimeLeftMs %d", waitTimeMs, waitTimeLeftMs)
2612 if (waitTimeLeftMs >= waitTimeMs) {
2613 waitTimeLeftMs -= waitTimeMs;
2614 } else {
2615 waitTimeLeftMs = 0;
2616 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002617 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002619 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount;
2620 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channels * sizeof(int16_t));
2621 mCblk->stepUser(outFrames);
2622 pInBuffer->frameCount -= outFrames;
2623 pInBuffer->i16 += outFrames * channels;
2624 mOutBuffer.frameCount -= outFrames;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002625 mOutBuffer.i16 += outFrames * channels;
2626
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627 if (pInBuffer->frameCount == 0) {
2628 if (mBufferQueue.size()) {
2629 mBufferQueue.removeAt(0);
2630 delete [] pInBuffer->mBuffer;
2631 delete pInBuffer;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002632 LOGV("OutputTrack::write() %p released overflow buffer %d", this, mBufferQueue.size());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633 } else {
2634 break;
2635 }
2636 }
2637 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002639 // If we could not write all frames, allocate a buffer and queue it for next time.
2640 if (inBuffer.frameCount) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07002641 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002642 pInBuffer = new Buffer;
2643 pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channels];
2644 pInBuffer->frameCount = inBuffer.frameCount;
2645 pInBuffer->i16 = pInBuffer->mBuffer;
2646 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channels * sizeof(int16_t));
2647 mBufferQueue.add(pInBuffer);
Eric Laurent9d91ad52009-07-17 12:17:14 -07002648 LOGV("OutputTrack::write() %p adding overflow buffer %d", this, mBufferQueue.size());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002649 } else {
Eric Laurent9d91ad52009-07-17 12:17:14 -07002650 LOGW("OutputTrack::write() %p no more overflow buffers", this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651 }
2652 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07002653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002654 // Calling write() with a 0 length buffer, means that no more data will be written:
Eric Laurent9d91ad52009-07-17 12:17:14 -07002655 // If no more buffers are pending, fill output track buffer to make sure it is started
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656 // by output mixer.
Eric Laurent9d91ad52009-07-17 12:17:14 -07002657 if (frames == 0 && mBufferQueue.size() == 0) {
2658 if (mCblk->user < mCblk->frameCount) {
2659 frames = mCblk->frameCount - mCblk->user;
2660 pInBuffer = new Buffer;
2661 pInBuffer->mBuffer = new int16_t[frames * channels];
2662 pInBuffer->frameCount = frames;
2663 pInBuffer->i16 = pInBuffer->mBuffer;
2664 memset(pInBuffer->raw, 0, frames * channels * sizeof(int16_t));
2665 mBufferQueue.add(pInBuffer);
2666 } else {
2667 stop();
2668 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002669 }
2670
Eric Laurent9d91ad52009-07-17 12:17:14 -07002671 return outputBufferFull;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002672}
2673
Eric Laurent9d91ad52009-07-17 12:17:14 -07002674status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002675{
2676 int active;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002677 status_t result;
2678 audio_track_cblk_t* cblk = mCblk;
2679 uint32_t framesReq = buffer->frameCount;
2680
Eric Laurent9d91ad52009-07-17 12:17:14 -07002681// LOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002682 buffer->frameCount = 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002683
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002684 uint32_t framesAvail = cblk->framesAvailable();
2685
Eric Laurent9d91ad52009-07-17 12:17:14 -07002686
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002687 if (framesAvail == 0) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07002688 Mutex::Autolock _l(cblk->lock);
2689 goto start_loop_here;
2690 while (framesAvail == 0) {
2691 active = mActive;
2692 if (UNLIKELY(!active)) {
2693 LOGV("Not active and NO_MORE_BUFFERS");
2694 return AudioTrack::NO_MORE_BUFFERS;
2695 }
2696 result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
2697 if (result != NO_ERROR) {
2698 return AudioTrack::NO_MORE_BUFFERS;
2699 }
2700 // read the server count again
2701 start_loop_here:
2702 framesAvail = cblk->framesAvailable_l();
2703 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002704 }
2705
Eric Laurent9d91ad52009-07-17 12:17:14 -07002706// if (framesAvail < framesReq) {
2707// return AudioTrack::NO_MORE_BUFFERS;
2708// }
2709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002710 if (framesReq > framesAvail) {
2711 framesReq = framesAvail;
2712 }
2713
2714 uint32_t u = cblk->user;
2715 uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
2716
2717 if (u + framesReq > bufferEnd) {
2718 framesReq = bufferEnd - u;
2719 }
2720
2721 buffer->frameCount = framesReq;
2722 buffer->raw = (void *)cblk->buffer(u);
2723 return NO_ERROR;
2724}
2725
2726
Eric Laurent9d91ad52009-07-17 12:17:14 -07002727void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002728{
2729 size_t size = mBufferQueue.size();
2730 Buffer *pBuffer;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002731
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002732 for (size_t i = 0; i < size; i++) {
2733 pBuffer = mBufferQueue.itemAt(i);
2734 delete [] pBuffer->mBuffer;
2735 delete pBuffer;
2736 }
2737 mBufferQueue.clear();
2738}
2739
2740// ----------------------------------------------------------------------------
2741
2742AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
2743 : RefBase(),
2744 mAudioFlinger(audioFlinger),
2745 mMemoryDealer(new MemoryDealer(1024*1024)),
2746 mPid(pid)
2747{
2748 // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
2749}
2750
Eric Laurent0f8ab672009-09-17 05:12:56 -07002751// Client destructor must be called with AudioFlinger::mLock held
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002752AudioFlinger::Client::~Client()
2753{
Eric Laurent0f8ab672009-09-17 05:12:56 -07002754 mAudioFlinger->removeClient_l(mPid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002755}
2756
2757const sp<MemoryDealer>& AudioFlinger::Client::heap() const
2758{
2759 return mMemoryDealer;
2760}
2761
2762// ----------------------------------------------------------------------------
2763
Eric Laurent9d91ad52009-07-17 12:17:14 -07002764AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002765 : BnAudioTrack(),
2766 mTrack(track)
2767{
2768}
2769
2770AudioFlinger::TrackHandle::~TrackHandle() {
2771 // just stop the track on deletion, associated resources
2772 // will be freed from the main thread once all pending buffers have
2773 // been played. Unless it's not in the active track list, in which
2774 // case we free everything now...
2775 mTrack->destroy();
2776}
2777
2778status_t AudioFlinger::TrackHandle::start() {
2779 return mTrack->start();
2780}
2781
2782void AudioFlinger::TrackHandle::stop() {
2783 mTrack->stop();
2784}
2785
2786void AudioFlinger::TrackHandle::flush() {
2787 mTrack->flush();
2788}
2789
2790void AudioFlinger::TrackHandle::mute(bool e) {
2791 mTrack->mute(e);
2792}
2793
2794void AudioFlinger::TrackHandle::pause() {
2795 mTrack->pause();
2796}
2797
2798void AudioFlinger::TrackHandle::setVolume(float left, float right) {
2799 mTrack->setVolume(left, right);
2800}
2801
2802sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
2803 return mTrack->getCblk();
2804}
2805
2806status_t AudioFlinger::TrackHandle::onTransact(
2807 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2808{
2809 return BnAudioTrack::onTransact(code, data, reply, flags);
2810}
2811
2812// ----------------------------------------------------------------------------
2813
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002814sp<IAudioRecord> AudioFlinger::openRecord(
2815 pid_t pid,
Eric Laurente0e9ecc2009-07-28 08:44:33 -07002816 int input,
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002817 uint32_t sampleRate,
2818 int format,
2819 int channelCount,
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002820 int frameCount,
2821 uint32_t flags,
2822 status_t *status)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002823{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002824 sp<RecordThread::RecordTrack> recordTrack;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002825 sp<RecordHandle> recordHandle;
2826 sp<Client> client;
2827 wp<Client> wclient;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002828 status_t lStatus;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002829 RecordThread *thread;
2830 size_t inFrameCount;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002831
2832 // check calling permissions
2833 if (!recordingAllowed()) {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002834 lStatus = PERMISSION_DENIED;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002835 goto Exit;
2836 }
2837
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002838 // add client to list
The Android Open Source Project22f8def2009-03-09 11:52:12 -07002839 { // scope for mLock
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002840 Mutex::Autolock _l(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -07002841 thread = checkRecordThread_l(input);
2842 if (thread == NULL) {
2843 lStatus = BAD_VALUE;
2844 goto Exit;
2845 }
2846
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002847 wclient = mClients.valueFor(pid);
2848 if (wclient != NULL) {
2849 client = wclient.promote();
2850 } else {
2851 client = new Client(this, pid);
2852 mClients.add(pid, client);
2853 }
The Android Open Source Project22f8def2009-03-09 11:52:12 -07002854
The Android Open Source Project22f8def2009-03-09 11:52:12 -07002855 // create new record track. The record track uses one track in mHardwareMixerThread by convention.
Eric Laurent9d91ad52009-07-17 12:17:14 -07002856 recordTrack = new RecordThread::RecordTrack(thread, client, sampleRate,
The Android Open Source Project22f8def2009-03-09 11:52:12 -07002857 format, channelCount, frameCount, flags);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002858 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002859 if (recordTrack->getCblk() == NULL) {
Eric Laurent0f8ab672009-09-17 05:12:56 -07002860 // remove local strong reference to Client before deleting the RecordTrack so that the Client
2861 // destructor is called by the TrackBase destructor with mLock held
2862 client.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002863 recordTrack.clear();
2864 lStatus = NO_MEMORY;
2865 goto Exit;
2866 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002867
2868 // return to handle to client
2869 recordHandle = new RecordHandle(recordTrack);
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002870 lStatus = NO_ERROR;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002871
2872Exit:
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002873 if (status) {
2874 *status = lStatus;
2875 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002876 return recordHandle;
2877}
2878
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002879// ----------------------------------------------------------------------------
2880
Eric Laurent9d91ad52009-07-17 12:17:14 -07002881AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002882 : BnAudioRecord(),
2883 mRecordTrack(recordTrack)
2884{
2885}
2886
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002887AudioFlinger::RecordHandle::~RecordHandle() {
2888 stop();
2889}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002890
2891status_t AudioFlinger::RecordHandle::start() {
2892 LOGV("RecordHandle::start()");
2893 return mRecordTrack->start();
2894}
2895
2896void AudioFlinger::RecordHandle::stop() {
2897 LOGV("RecordHandle::stop()");
2898 mRecordTrack->stop();
2899}
2900
2901sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
2902 return mRecordTrack->getCblk();
2903}
2904
2905status_t AudioFlinger::RecordHandle::onTransact(
2906 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2907{
2908 return BnAudioRecord::onTransact(code, data, reply, flags);
2909}
2910
2911// ----------------------------------------------------------------------------
2912
Eric Laurent9d91ad52009-07-17 12:17:14 -07002913AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, AudioStreamIn *input, uint32_t sampleRate, uint32_t channels) :
2914 ThreadBase(audioFlinger),
2915 mInput(input), mResampler(0), mRsmpOutBuffer(0), mRsmpInBuffer(0)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002916{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002917 mReqChannelCount = AudioSystem::popCount(channels);
2918 mReqSampleRate = sampleRate;
2919 readInputParameters();
2920 sendConfigEvent(AudioSystem::INPUT_OPENED);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002921}
2922
Eric Laurent9d91ad52009-07-17 12:17:14 -07002923
2924AudioFlinger::RecordThread::~RecordThread()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002925{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002926 delete[] mRsmpInBuffer;
2927 if (mResampler != 0) {
2928 delete mResampler;
2929 delete[] mRsmpOutBuffer;
2930 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002931}
2932
Eric Laurent9d91ad52009-07-17 12:17:14 -07002933void AudioFlinger::RecordThread::onFirstRef()
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002934{
Eric Laurent9d91ad52009-07-17 12:17:14 -07002935 const size_t SIZE = 256;
2936 char buffer[SIZE];
2937
2938 snprintf(buffer, SIZE, "Record Thread %p", this);
2939
2940 run(buffer, PRIORITY_URGENT_AUDIO);
2941}
2942bool AudioFlinger::RecordThread::threadLoop()
2943{
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002944 AudioBufferProvider::Buffer buffer;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002945 sp<RecordTrack> activeTrack;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002946
2947 // start recording
2948 while (!exitPending()) {
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08002949
Eric Laurent9d91ad52009-07-17 12:17:14 -07002950 processConfigEvents();
2951
2952 { // scope for mLock
2953 Mutex::Autolock _l(mLock);
2954 checkForNewParameters_l();
2955 if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
2956 if (!mStandby) {
2957 mInput->standby();
2958 mStandby = true;
2959 }
2960
2961 if (exitPending()) break;
2962
2963 LOGV("RecordThread: loop stopping");
2964 // go to sleep
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07002965 mWaitWorkCV.wait(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -07002966 LOGV("RecordThread: loop starting");
2967 continue;
2968 }
2969 if (mActiveTrack != 0) {
2970 if (mActiveTrack->mState == TrackBase::PAUSING) {
2971 mActiveTrack.clear();
2972 mStartStopCond.broadcast();
2973 } else if (mActiveTrack->mState == TrackBase::RESUMING) {
2974 mRsmpInIndex = mFrameCount;
2975 if (mReqChannelCount != mActiveTrack->channelCount()) {
2976 mActiveTrack.clear();
2977 } else {
Eric Laurent9e7b8192009-08-10 02:41:54 -07002978 mActiveTrack->mState = TrackBase::ACTIVE;
Eric Laurent9d91ad52009-07-17 12:17:14 -07002979 }
2980 mStartStopCond.broadcast();
2981 }
2982 mStandby = false;
2983 }
2984 }
2985
2986 if (mActiveTrack != 0) {
2987 buffer.frameCount = mFrameCount;
2988 if (LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) {
2989 size_t framesOut = buffer.frameCount;
2990 if (mResampler == 0) {
2991 // no resampling
2992 while (framesOut) {
2993 size_t framesIn = mFrameCount - mRsmpInIndex;
2994 if (framesIn) {
2995 int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
2996 int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize;
2997 if (framesIn > framesOut)
2998 framesIn = framesOut;
2999 mRsmpInIndex += framesIn;
3000 framesOut -= framesIn;
3001 if (mChannelCount == mReqChannelCount ||
3002 mFormat != AudioSystem::PCM_16_BIT) {
3003 memcpy(dst, src, framesIn * mFrameSize);
3004 } else {
3005 int16_t *src16 = (int16_t *)src;
3006 int16_t *dst16 = (int16_t *)dst;
3007 if (mChannelCount == 1) {
3008 while (framesIn--) {
3009 *dst16++ = *src16;
3010 *dst16++ = *src16++;
3011 }
3012 } else {
3013 while (framesIn--) {
3014 *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
3015 src16 += 2;
3016 }
3017 }
3018 }
3019 }
3020 if (framesOut && mFrameCount == mRsmpInIndex) {
3021 ssize_t bytesRead;
3022 if (framesOut == mFrameCount &&
3023 (mChannelCount == mReqChannelCount || mFormat != AudioSystem::PCM_16_BIT)) {
3024 bytesRead = mInput->read(buffer.raw, mInputBytes);
3025 framesOut = 0;
3026 } else {
3027 bytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
3028 mRsmpInIndex = 0;
3029 }
3030 if (bytesRead < 0) {
3031 LOGE("Error reading audio input");
3032 sleep(1);
3033 mRsmpInIndex = mFrameCount;
3034 framesOut = 0;
3035 buffer.frameCount = 0;
3036 }
3037 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08003038 }
3039 } else {
Eric Laurent9d91ad52009-07-17 12:17:14 -07003040 // resampling
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003041
Eric Laurent9d91ad52009-07-17 12:17:14 -07003042 memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t));
3043 // alter output frame count as if we were expecting stereo samples
3044 if (mChannelCount == 1 && mReqChannelCount == 1) {
3045 framesOut >>= 1;
3046 }
3047 mResampler->resample(mRsmpOutBuffer, framesOut, this);
3048 // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer()
3049 // are 32 bit aligned which should be always true.
3050 if (mChannelCount == 2 && mReqChannelCount == 1) {
3051 AudioMixer::ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut);
3052 // the resampler always outputs stereo samples: do post stereo to mono conversion
3053 int16_t *src = (int16_t *)mRsmpOutBuffer;
3054 int16_t *dst = buffer.i16;
3055 while (framesOut--) {
3056 *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1);
3057 src += 2;
3058 }
3059 } else {
3060 AudioMixer::ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut);
3061 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003062
Eric Laurent9d91ad52009-07-17 12:17:14 -07003063 }
3064 mActiveTrack->releaseBuffer(&buffer);
3065 mActiveTrack->overflow();
3066 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003067 // client isn't retrieving buffers fast enough
3068 else {
Eric Laurent9d91ad52009-07-17 12:17:14 -07003069 if (!mActiveTrack->setOverflow())
3070 LOGW("RecordThread: buffer overflow");
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08003071 // Release the processor for a while before asking for a new buffer.
3072 // This will give the application more chance to read from the buffer and
3073 // clear the overflow.
3074 usleep(5000);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003075 }
3076 }
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08003077 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003078
Eric Laurent9d91ad52009-07-17 12:17:14 -07003079 if (!mStandby) {
3080 mInput->standby();
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08003081 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07003082 mActiveTrack.clear();
3083
Eric Laurent9d91ad52009-07-17 12:17:14 -07003084 LOGV("RecordThread %p exiting", this);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003085 return false;
3086}
3087
Eric Laurent9d91ad52009-07-17 12:17:14 -07003088status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack)
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003089{
Eric Laurent9d91ad52009-07-17 12:17:14 -07003090 LOGV("RecordThread::start");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003091 AutoMutex lock(&mLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003092
Eric Laurent9d91ad52009-07-17 12:17:14 -07003093 if (mActiveTrack != 0) {
3094 if (recordTrack != mActiveTrack.get()) return -EBUSY;
The Android Open Source Projecte09fd9e2008-12-17 18:05:43 -08003095
Eric Laurent9d91ad52009-07-17 12:17:14 -07003096 if (mActiveTrack->mState == TrackBase::PAUSING) mActiveTrack->mState = TrackBase::RESUMING;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003097
Eric Laurent9d91ad52009-07-17 12:17:14 -07003098 return NO_ERROR;
3099 }
3100
3101 mActiveTrack = recordTrack;
3102 mActiveTrack->mState = TrackBase::RESUMING;
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003103 // signal thread to start
3104 LOGV("Signal record thread");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003105 mWaitWorkCV.signal();
Eric Laurent9d91ad52009-07-17 12:17:14 -07003106 mStartStopCond.wait(mLock);
3107 if (mActiveTrack != 0) {
3108 LOGV("Record started OK");
3109 return NO_ERROR;
3110 } else {
3111 LOGV("Record failed to start");
3112 return BAD_VALUE;
3113 }
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003114}
3115
Eric Laurent9d91ad52009-07-17 12:17:14 -07003116void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
3117 LOGV("RecordThread::stop");
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003118 AutoMutex lock(&mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003119 if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) {
3120 mActiveTrack->mState = TrackBase::PAUSING;
3121 mStartStopCond.wait(mLock);
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003122 }
3123}
3124
Eric Laurent9d91ad52009-07-17 12:17:14 -07003125status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003126{
3127 const size_t SIZE = 256;
3128 char buffer[SIZE];
3129 String8 result;
3130 pid_t pid = 0;
3131
Eric Laurent9d91ad52009-07-17 12:17:14 -07003132 if (mActiveTrack != 0 && mActiveTrack->mClient != 0) {
3133 snprintf(buffer, SIZE, "Record client pid: %d\n", mActiveTrack->mClient->pid());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003134 result.append(buffer);
3135 } else {
3136 result.append("No record client\n");
3137 }
3138 write(fd, result.string(), result.size());
3139 return NO_ERROR;
3140}
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003141
Eric Laurent9d91ad52009-07-17 12:17:14 -07003142status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer)
3143{
3144 size_t framesReq = buffer->frameCount;
3145 size_t framesReady = mFrameCount - mRsmpInIndex;
3146 int channelCount;
3147
3148 if (framesReady == 0) {
3149 ssize_t bytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
3150 if (bytesRead < 0) {
3151 LOGE("RecordThread::getNextBuffer() Error reading audio input");
3152 sleep(1);
3153 buffer->raw = 0;
3154 buffer->frameCount = 0;
3155 return NOT_ENOUGH_DATA;
3156 }
3157 mRsmpInIndex = 0;
3158 framesReady = mFrameCount;
3159 }
3160
3161 if (framesReq > framesReady) {
3162 framesReq = framesReady;
3163 }
3164
3165 if (mChannelCount == 1 && mReqChannelCount == 2) {
3166 channelCount = 1;
3167 } else {
3168 channelCount = 2;
3169 }
3170 buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount;
3171 buffer->frameCount = framesReq;
3172 return NO_ERROR;
3173}
3174
3175void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer)
3176{
3177 mRsmpInIndex += buffer->frameCount;
3178 buffer->frameCount = 0;
3179}
3180
3181bool AudioFlinger::RecordThread::checkForNewParameters_l()
3182{
3183 bool reconfig = false;
3184
Eric Laurent3464c012009-08-04 09:45:33 -07003185 while (!mNewParameters.isEmpty()) {
Eric Laurent9d91ad52009-07-17 12:17:14 -07003186 status_t status = NO_ERROR;
Eric Laurent3464c012009-08-04 09:45:33 -07003187 String8 keyValuePair = mNewParameters[0];
3188 AudioParameter param = AudioParameter(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003189 int value;
3190 int reqFormat = mFormat;
3191 int reqSamplingRate = mReqSampleRate;
3192 int reqChannelCount = mReqChannelCount;
Eric Laurent3464c012009-08-04 09:45:33 -07003193
3194 mNewParameters.removeAt(0);
3195
Eric Laurent9d91ad52009-07-17 12:17:14 -07003196 if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
3197 reqSamplingRate = value;
3198 reconfig = true;
3199 }
3200 if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
3201 reqFormat = value;
3202 reconfig = true;
3203 }
3204 if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
3205 reqChannelCount = AudioSystem::popCount(value);
3206 reconfig = true;
3207 }
3208 if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
3209 // do not accept frame count changes if tracks are open as the track buffer
3210 // size depends on frame count and correct behavior would not be garantied
3211 // if frame count is changed after track creation
3212 if (mActiveTrack != 0) {
3213 status = INVALID_OPERATION;
3214 } else {
3215 reconfig = true;
3216 }
3217 }
3218 if (status == NO_ERROR) {
Eric Laurent3464c012009-08-04 09:45:33 -07003219 status = mInput->setParameters(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003220 if (status == INVALID_OPERATION) {
3221 mInput->standby();
Eric Laurent3464c012009-08-04 09:45:33 -07003222 status = mInput->setParameters(keyValuePair);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003223 }
3224 if (reconfig) {
3225 if (status == BAD_VALUE &&
3226 reqFormat == mInput->format() && reqFormat == AudioSystem::PCM_16_BIT &&
3227 ((int)mInput->sampleRate() <= 2 * reqSamplingRate) &&
3228 (AudioSystem::popCount(mInput->channels()) < 3) && (reqChannelCount < 3)) {
3229 status = NO_ERROR;
3230 }
3231 if (status == NO_ERROR) {
3232 readInputParameters();
Eric Laurent3464c012009-08-04 09:45:33 -07003233 sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003234 }
3235 }
3236 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07003237 mParamStatus = status;
3238 mParamCond.signal();
Eric Laurent3464c012009-08-04 09:45:33 -07003239 mWaitWorkCV.wait(mLock);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003240 }
3241 return reconfig;
3242}
3243
3244String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
3245{
3246 return mInput->getParameters(keys);
3247}
3248
3249void AudioFlinger::RecordThread::audioConfigChanged(int event, int param) {
3250 AudioSystem::OutputDescriptor desc;
3251 void *param2 = 0;
3252
3253 switch (event) {
3254 case AudioSystem::INPUT_OPENED:
3255 case AudioSystem::INPUT_CONFIG_CHANGED:
3256 desc.channels = mChannelCount;
3257 desc.samplingRate = mSampleRate;
3258 desc.format = mFormat;
3259 desc.frameCount = mFrameCount;
3260 desc.latency = 0;
3261 param2 = &desc;
3262 break;
3263
3264 case AudioSystem::INPUT_CLOSED:
3265 default:
3266 break;
3267 }
Eric Laurentb3687ae2009-09-15 07:10:12 -07003268 Mutex::Autolock _l(mAudioFlinger->mLock);
3269 mAudioFlinger->audioConfigChanged_l(event, this, param2);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003270}
3271
3272void AudioFlinger::RecordThread::readInputParameters()
3273{
3274 if (mRsmpInBuffer) delete mRsmpInBuffer;
3275 if (mRsmpOutBuffer) delete mRsmpOutBuffer;
3276 if (mResampler) delete mResampler;
3277 mResampler = 0;
3278
3279 mSampleRate = mInput->sampleRate();
3280 mChannelCount = AudioSystem::popCount(mInput->channels());
3281 mFormat = mInput->format();
3282 mFrameSize = mInput->frameSize();
3283 mInputBytes = mInput->bufferSize();
3284 mFrameCount = mInputBytes / mFrameSize;
3285 mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
3286
3287 if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3)
3288 {
3289 int channelCount;
3290 // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid
3291 // stereo to mono post process as the resampler always outputs stereo.
3292 if (mChannelCount == 1 && mReqChannelCount == 2) {
3293 channelCount = 1;
3294 } else {
3295 channelCount = 2;
3296 }
3297 mResampler = AudioResampler::create(16, channelCount, mReqSampleRate);
3298 mResampler->setSampleRate(mSampleRate);
3299 mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
3300 mRsmpOutBuffer = new int32_t[mFrameCount * 2];
3301
3302 // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples
3303 if (mChannelCount == 1 && mReqChannelCount == 1) {
3304 mFrameCount >>= 1;
3305 }
3306
3307 }
3308 mRsmpInIndex = mFrameCount;
3309}
3310
3311// ----------------------------------------------------------------------------
3312
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003313int AudioFlinger::openOutput(uint32_t *pDevices,
Eric Laurent9d91ad52009-07-17 12:17:14 -07003314 uint32_t *pSamplingRate,
3315 uint32_t *pFormat,
3316 uint32_t *pChannels,
3317 uint32_t *pLatencyMs,
3318 uint32_t flags)
3319{
3320 status_t status;
3321 PlaybackThread *thread = NULL;
3322 mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
3323 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
3324 uint32_t format = pFormat ? *pFormat : 0;
3325 uint32_t channels = pChannels ? *pChannels : 0;
3326 uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
3327
3328 LOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
3329 pDevices ? *pDevices : 0,
3330 samplingRate,
3331 format,
3332 channels,
3333 flags);
3334
3335 if (pDevices == NULL || *pDevices == 0) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003336 return 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003337 }
3338 Mutex::Autolock _l(mLock);
3339
3340 AudioStreamOut *output = mAudioHardware->openOutputStream(*pDevices,
3341 (int *)&format,
3342 &channels,
3343 &samplingRate,
3344 &status);
3345 LOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
3346 output,
3347 samplingRate,
3348 format,
3349 channels,
3350 status);
3351
3352 mHardwareStatus = AUDIO_HW_IDLE;
3353 if (output != 0) {
3354 if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
3355 (format != AudioSystem::PCM_16_BIT) ||
3356 (channels != AudioSystem::CHANNEL_OUT_STEREO)) {
3357 thread = new DirectOutputThread(this, output);
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003358 LOGV("openOutput() created direct output: ID %d thread %p", (mNextThreadId + 1), thread);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003359 } else {
3360 thread = new MixerThread(this, output);
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003361 LOGV("openOutput() created mixer output: ID %d thread %p", (mNextThreadId + 1), thread);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003362 }
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003363 mPlaybackThreads.add(++mNextThreadId, thread);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003364
3365 if (pSamplingRate) *pSamplingRate = samplingRate;
3366 if (pFormat) *pFormat = format;
3367 if (pChannels) *pChannels = channels;
3368 if (pLatencyMs) *pLatencyMs = thread->latency();
3369 }
3370
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003371 return mNextThreadId;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003372}
3373
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003374int AudioFlinger::openDuplicateOutput(int output1, int output2)
Eric Laurent9d91ad52009-07-17 12:17:14 -07003375{
3376 Mutex::Autolock _l(mLock);
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003377 MixerThread *thread1 = checkMixerThread_l(output1);
3378 MixerThread *thread2 = checkMixerThread_l(output2);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003379
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003380 if (thread1 == NULL || thread2 == NULL) {
3381 LOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
3382 return 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003383 }
3384
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003385
3386 DuplicatingThread *thread = new DuplicatingThread(this, thread1);
3387 thread->addOutputTrack(thread2);
3388 mPlaybackThreads.add(++mNextThreadId, thread);
3389 return mNextThreadId;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003390}
3391
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003392status_t AudioFlinger::closeOutput(int output)
Eric Laurent9d91ad52009-07-17 12:17:14 -07003393{
Eric Laurentdae20d92009-08-04 08:37:05 -07003394 // keep strong reference on the playback thread so that
3395 // it is not destroyed while exit() is executed
3396 sp <PlaybackThread> thread;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003397 {
3398 Mutex::Autolock _l(mLock);
3399 thread = checkPlaybackThread_l(output);
3400 if (thread == NULL) {
3401 return BAD_VALUE;
3402 }
3403
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003404 LOGV("closeOutput() %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003405
3406 if (thread->type() == PlaybackThread::MIXER) {
3407 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003408 if (mPlaybackThreads.valueAt(i)->type() == PlaybackThread::DUPLICATING) {
3409 DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
Eric Laurentdae20d92009-08-04 08:37:05 -07003410 dupThread->removeOutputTrack((MixerThread *)thread.get());
Eric Laurent9d91ad52009-07-17 12:17:14 -07003411 }
3412 }
3413 }
Eric Laurentb3687ae2009-09-15 07:10:12 -07003414 void *param2 = 0;
3415 audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, thread, param2);
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003416 mPlaybackThreads.removeItem(output);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003417 }
3418 thread->exit();
3419
Eric Laurentdae20d92009-08-04 08:37:05 -07003420 if (thread->type() != PlaybackThread::DUPLICATING) {
3421 mAudioHardware->closeOutputStream(thread->getOutput());
3422 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07003423 return NO_ERROR;
3424}
3425
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003426status_t AudioFlinger::suspendOutput(int output)
Eric Laurent9d91ad52009-07-17 12:17:14 -07003427{
3428 Mutex::Autolock _l(mLock);
3429 PlaybackThread *thread = checkPlaybackThread_l(output);
3430
3431 if (thread == NULL) {
3432 return BAD_VALUE;
3433 }
3434
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003435 LOGV("suspendOutput() %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003436 thread->suspend();
3437
3438 return NO_ERROR;
3439}
3440
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003441status_t AudioFlinger::restoreOutput(int output)
Eric Laurent9d91ad52009-07-17 12:17:14 -07003442{
3443 Mutex::Autolock _l(mLock);
3444 PlaybackThread *thread = checkPlaybackThread_l(output);
3445
3446 if (thread == NULL) {
3447 return BAD_VALUE;
3448 }
3449
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003450 LOGV("restoreOutput() %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003451
3452 thread->restore();
3453
3454 return NO_ERROR;
3455}
3456
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003457int AudioFlinger::openInput(uint32_t *pDevices,
Eric Laurent9d91ad52009-07-17 12:17:14 -07003458 uint32_t *pSamplingRate,
3459 uint32_t *pFormat,
3460 uint32_t *pChannels,
3461 uint32_t acoustics)
3462{
3463 status_t status;
3464 RecordThread *thread = NULL;
3465 uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
3466 uint32_t format = pFormat ? *pFormat : 0;
3467 uint32_t channels = pChannels ? *pChannels : 0;
3468 uint32_t reqSamplingRate = samplingRate;
3469 uint32_t reqFormat = format;
3470 uint32_t reqChannels = channels;
3471
3472 if (pDevices == NULL || *pDevices == 0) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003473 return 0;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003474 }
3475 Mutex::Autolock _l(mLock);
3476
3477 AudioStreamIn *input = mAudioHardware->openInputStream(*pDevices,
3478 (int *)&format,
3479 &channels,
3480 &samplingRate,
3481 &status,
3482 (AudioSystem::audio_in_acoustics)acoustics);
3483 LOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d",
3484 input,
3485 samplingRate,
3486 format,
3487 channels,
3488 acoustics,
3489 status);
3490
3491 // If the input could not be opened with the requested parameters and we can handle the conversion internally,
3492 // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
3493 // or stereo to mono conversions on 16 bit PCM inputs.
3494 if (input == 0 && status == BAD_VALUE &&
3495 reqFormat == format && format == AudioSystem::PCM_16_BIT &&
3496 (samplingRate <= 2 * reqSamplingRate) &&
3497 (AudioSystem::popCount(channels) < 3) && (AudioSystem::popCount(reqChannels) < 3)) {
3498 LOGV("openInput() reopening with proposed sampling rate and channels");
3499 input = mAudioHardware->openInputStream(*pDevices,
3500 (int *)&format,
3501 &channels,
3502 &samplingRate,
3503 &status,
3504 (AudioSystem::audio_in_acoustics)acoustics);
3505 }
3506
3507 if (input != 0) {
3508 // Start record thread
3509 thread = new RecordThread(this, input, reqSamplingRate, reqChannels);
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003510 mRecordThreads.add(++mNextThreadId, thread);
3511 LOGV("openInput() created record thread: ID %d thread %p", mNextThreadId, thread);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003512 if (pSamplingRate) *pSamplingRate = reqSamplingRate;
3513 if (pFormat) *pFormat = format;
3514 if (pChannels) *pChannels = reqChannels;
3515
3516 input->standby();
3517 }
3518
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003519 return mNextThreadId;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003520}
3521
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003522status_t AudioFlinger::closeInput(int input)
Eric Laurent9d91ad52009-07-17 12:17:14 -07003523{
Eric Laurentdae20d92009-08-04 08:37:05 -07003524 // keep strong reference on the record thread so that
3525 // it is not destroyed while exit() is executed
3526 sp <RecordThread> thread;
Eric Laurent9d91ad52009-07-17 12:17:14 -07003527 {
3528 Mutex::Autolock _l(mLock);
3529 thread = checkRecordThread_l(input);
3530 if (thread == NULL) {
3531 return BAD_VALUE;
3532 }
3533
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003534 LOGV("closeInput() %d", input);
Eric Laurentb3687ae2009-09-15 07:10:12 -07003535 void *param2 = 0;
3536 audioConfigChanged_l(AudioSystem::INPUT_CLOSED, thread, param2);
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003537 mRecordThreads.removeItem(input);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003538 }
3539 thread->exit();
3540
Eric Laurentdae20d92009-08-04 08:37:05 -07003541 mAudioHardware->closeInputStream(thread->getInput());
3542
Eric Laurent9d91ad52009-07-17 12:17:14 -07003543 return NO_ERROR;
3544}
3545
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003546status_t AudioFlinger::setStreamOutput(uint32_t stream, int output)
Eric Laurent9d91ad52009-07-17 12:17:14 -07003547{
3548 Mutex::Autolock _l(mLock);
3549 MixerThread *dstThread = checkMixerThread_l(output);
3550 if (dstThread == NULL) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003551 LOGW("setStreamOutput() bad output id %d", output);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003552 return BAD_VALUE;
3553 }
3554
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003555 LOGV("setStreamOutput() stream %d to output %d", stream, output);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003556
3557 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003558 PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
Eric Laurent9d91ad52009-07-17 12:17:14 -07003559 if (thread != dstThread &&
3560 thread->type() != PlaybackThread::DIRECT) {
3561 MixerThread *srcThread = (MixerThread *)thread;
3562 SortedVector < sp<MixerThread::Track> > tracks;
3563 SortedVector < wp<MixerThread::Track> > activeTracks;
3564 srcThread->getTracks(tracks, activeTracks, stream);
3565 if (tracks.size()) {
3566 dstThread->putTracks(tracks, activeTracks);
Eric Laurent9d91ad52009-07-17 12:17:14 -07003567 }
3568 }
3569 }
3570
Eric Laurent06437712009-09-01 05:56:26 -07003571 dstThread->sendConfigEvent(AudioSystem::STREAM_CONFIG_CHANGED, stream);
3572
Eric Laurent9d91ad52009-07-17 12:17:14 -07003573 return NO_ERROR;
3574}
3575
3576// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003577AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const
Eric Laurent9d91ad52009-07-17 12:17:14 -07003578{
3579 PlaybackThread *thread = NULL;
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003580 if (mPlaybackThreads.indexOfKey(output) >= 0) {
3581 thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get();
Eric Laurent9d91ad52009-07-17 12:17:14 -07003582 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07003583 return thread;
3584}
3585
3586// checkMixerThread_l() must be called with AudioFlinger::mLock held
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003587AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const
Eric Laurent9d91ad52009-07-17 12:17:14 -07003588{
3589 PlaybackThread *thread = checkPlaybackThread_l(output);
3590 if (thread != NULL) {
3591 if (thread->type() == PlaybackThread::DIRECT) {
3592 thread = NULL;
3593 }
3594 }
3595 return (MixerThread *)thread;
3596}
3597
3598// checkRecordThread_l() must be called with AudioFlinger::mLock held
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003599AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const
Eric Laurent9d91ad52009-07-17 12:17:14 -07003600{
3601 RecordThread *thread = NULL;
Eric Laurente0e9ecc2009-07-28 08:44:33 -07003602 if (mRecordThreads.indexOfKey(input) >= 0) {
3603 thread = (RecordThread *)mRecordThreads.valueFor(input).get();
Eric Laurent9d91ad52009-07-17 12:17:14 -07003604 }
Eric Laurent9d91ad52009-07-17 12:17:14 -07003605 return thread;
3606}
3607
3608// ----------------------------------------------------------------------------
3609
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003610status_t AudioFlinger::onTransact(
3611 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3612{
3613 return BnAudioFlinger::onTransact(code, data, reply, flags);
3614}
3615
3616// ----------------------------------------------------------------------------
Eric Laurent9d91ad52009-07-17 12:17:14 -07003617
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07003618void AudioFlinger::instantiate() {
3619 defaultServiceManager()->addService(
3620 String16("media.audio_flinger"), new AudioFlinger());
3621}
3622
3623}; // namespace android