2008-10-21 14:00:00 +00:00
|
|
|
/* //device/include/server/AudioFlinger/AudioFlinger.cpp
|
|
|
|
**
|
|
|
|
** Copyright 2007, The Android Open Source Project
|
|
|
|
**
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
**
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
**
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define LOG_TAG "AudioFlinger"
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/IServiceManager.h>
|
2008-10-21 14:00:00 +00:00
|
|
|
#include <utils/Log.h>
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/Parcel.h>
|
|
|
|
#include <binder/IPCThreadState.h>
|
2008-10-21 14:00:00 +00:00
|
|
|
#include <utils/String16.h>
|
|
|
|
#include <utils/threads.h>
|
|
|
|
|
2008-12-18 02:05:43 +00:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
#include <media/AudioTrack.h>
|
|
|
|
#include <media/AudioRecord.h>
|
|
|
|
|
|
|
|
#include <private/media/AudioTrackShared.h>
|
|
|
|
|
2009-01-16 00:12:10 +00:00
|
|
|
#include <hardware_legacy/AudioHardwareInterface.h>
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
#include "AudioMixer.h"
|
|
|
|
#include "AudioFlinger.h"
|
|
|
|
|
2008-12-18 02:05:43 +00:00
|
|
|
#ifdef WITH_A2DP
|
|
|
|
#include "A2dpAudioInterface.h"
|
|
|
|
#endif
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the sim build doesn't have gettid
|
|
|
|
|
|
|
|
#ifndef HAVE_GETTID
|
|
|
|
# define gettid getpid
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
namespace android {
|
|
|
|
|
2009-03-19 00:39:46 +00:00
|
|
|
static const char* kDeadlockedString = "AudioFlinger may be deadlocked\n";
|
|
|
|
static const char* kHardwareLockedString = "Hardware lock is taken\n";
|
|
|
|
|
2008-12-18 02:05:43 +00:00
|
|
|
//static const nsecs_t kStandbyTimeInNsecs = seconds(3);
|
2008-10-21 14:00:00 +00:00
|
|
|
static const float MAX_GAIN = 4096.0f;
|
|
|
|
|
|
|
|
// retry counts for buffer fill timeout
|
|
|
|
// 50 * ~20msecs = 1 second
|
|
|
|
static const int8_t kMaxTrackRetries = 50;
|
|
|
|
static const int8_t kMaxTrackStartupRetries = 50;
|
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
static const int kDumpLockRetries = 50;
|
|
|
|
static const int kDumpLockSleep = 20000;
|
|
|
|
|
2009-09-30 10:09:03 +00:00
|
|
|
static const nsecs_t kWarningThrottle = seconds(5);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
#define AUDIOFLINGER_SECURITY_ENABLED 1
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool recordingAllowed() {
|
|
|
|
#ifndef HAVE_ANDROID_OS
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
#if AUDIOFLINGER_SECURITY_ENABLED
|
|
|
|
if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
|
|
|
|
bool ok = checkCallingPermission(String16("android.permission.RECORD_AUDIO"));
|
|
|
|
if (!ok) LOGE("Request requires android.permission.RECORD_AUDIO");
|
|
|
|
return ok;
|
|
|
|
#else
|
|
|
|
if (!checkCallingPermission(String16("android.permission.RECORD_AUDIO")))
|
|
|
|
LOGW("WARNING: Need to add android.permission.RECORD_AUDIO to manifest");
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool settingsAllowed() {
|
|
|
|
#ifndef HAVE_ANDROID_OS
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
#if AUDIOFLINGER_SECURITY_ENABLED
|
|
|
|
if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
|
|
|
|
bool ok = checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS"));
|
|
|
|
if (!ok) LOGE("Request requires android.permission.MODIFY_AUDIO_SETTINGS");
|
|
|
|
return ok;
|
|
|
|
#else
|
|
|
|
if (!checkCallingPermission(String16("android.permission.MODIFY_AUDIO_SETTINGS")))
|
|
|
|
LOGW("WARNING: Need to add android.permission.MODIFY_AUDIO_SETTINGS to manifest");
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
AudioFlinger::AudioFlinger()
|
2009-03-04 03:31:44 +00:00
|
|
|
: BnAudioFlinger(),
|
2009-07-28 15:44:33 +00:00
|
|
|
mAudioHardware(0), mMasterVolume(1.0f), mMasterMute(false), mNextThreadId(0)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
mAudioHardware = AudioHardwareInterface::create();
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
mHardwareStatus = AUDIO_HW_INIT;
|
|
|
|
if (mAudioHardware->initCheck() == NO_ERROR) {
|
|
|
|
// open 16-bit output stream for s/w mixer
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
setMode(AudioSystem::MODE_NORMAL);
|
|
|
|
|
|
|
|
setMasterVolume(1.0f);
|
|
|
|
setMasterMute(false);
|
2009-07-17 19:17:14 +00:00
|
|
|
} else {
|
2008-10-21 14:00:00 +00:00
|
|
|
LOGE("Couldn't even initialize the stubbed audio hardware!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioFlinger::~AudioFlinger()
|
|
|
|
{
|
2009-08-28 17:39:03 +00:00
|
|
|
while (!mRecordThreads.isEmpty()) {
|
|
|
|
// closeInput() will remove first entry from mRecordThreads
|
|
|
|
closeInput(mRecordThreads.keyAt(0));
|
|
|
|
}
|
|
|
|
while (!mPlaybackThreads.isEmpty()) {
|
|
|
|
// closeOutput() will remove first entry from mPlaybackThreads
|
|
|
|
closeOutput(mPlaybackThreads.keyAt(0));
|
|
|
|
}
|
|
|
|
if (mAudioHardware) {
|
|
|
|
delete mAudioHardware;
|
|
|
|
}
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-01-22 08:13:42 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
|
|
|
|
result.append("Clients:\n");
|
|
|
|
for (size_t i = 0; i < mClients.size(); ++i) {
|
|
|
|
wp<Client> wClient = mClients.valueAt(i);
|
|
|
|
if (wClient != 0) {
|
|
|
|
sp<Client> client = wClient.promote();
|
|
|
|
if (client != 0) {
|
|
|
|
snprintf(buffer, SIZE, " pid: %d\n", client->pid());
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
2009-03-09 18:52:12 +00:00
|
|
|
int hardwareStatus = mHardwareStatus;
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
snprintf(buffer, SIZE, "Hardware status: %d\n", hardwareStatus);
|
2008-10-21 14:00:00 +00:00
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
snprintf(buffer, SIZE, "Permission Denial: "
|
|
|
|
"can't dump AudioFlinger from pid=%d, uid=%d\n",
|
|
|
|
IPCThreadState::self()->getCallingPid(),
|
|
|
|
IPCThreadState::self()->getCallingUid());
|
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-03-19 00:39:46 +00:00
|
|
|
static bool tryLock(Mutex& mutex)
|
|
|
|
{
|
|
|
|
bool locked = false;
|
|
|
|
for (int i = 0; i < kDumpLockRetries; ++i) {
|
|
|
|
if (mutex.tryLock() == NO_ERROR) {
|
|
|
|
locked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
usleep(kDumpLockSleep);
|
|
|
|
}
|
|
|
|
return locked;
|
|
|
|
}
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
|
|
|
|
dumpPermissionDenial(fd, args);
|
|
|
|
} else {
|
2009-03-19 00:39:46 +00:00
|
|
|
// get state of hardware lock
|
|
|
|
bool hardwareLocked = tryLock(mHardwareLock);
|
|
|
|
if (!hardwareLocked) {
|
|
|
|
String8 result(kHardwareLockedString);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
} else {
|
|
|
|
mHardwareLock.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool locked = tryLock(mLock);
|
|
|
|
|
|
|
|
// failed to lock - AudioFlinger is probably deadlocked
|
|
|
|
if (!locked) {
|
|
|
|
String8 result(kDeadlockedString);
|
|
|
|
write(fd, result.string(), result.size());
|
2009-03-09 18:52:12 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
dumpClients(fd, args);
|
|
|
|
dumpInternals(fd, args);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// dump playback threads
|
|
|
|
for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
mPlaybackThreads.valueAt(i)->dump(fd, args);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dump record threads
|
2009-07-23 20:35:01 +00:00
|
|
|
for (size_t i = 0; i < mRecordThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
mRecordThreads.valueAt(i)->dump(fd, args);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
if (mAudioHardware) {
|
|
|
|
mAudioHardware->dumpState(fd, args);
|
|
|
|
}
|
2009-03-09 18:52:12 +00:00
|
|
|
if (locked) mLock.unlock();
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// IAudioFlinger interface
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
sp<IAudioTrack> AudioFlinger::createTrack(
|
|
|
|
pid_t pid,
|
|
|
|
int streamType,
|
|
|
|
uint32_t sampleRate,
|
|
|
|
int format,
|
|
|
|
int channelCount,
|
|
|
|
int frameCount,
|
|
|
|
uint32_t flags,
|
|
|
|
const sp<IMemory>& sharedBuffer,
|
2009-07-28 15:44:33 +00:00
|
|
|
int output,
|
2009-03-04 03:31:44 +00:00
|
|
|
status_t *status)
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<PlaybackThread::Track> track;
|
2009-03-04 03:31:44 +00:00
|
|
|
sp<TrackHandle> trackHandle;
|
|
|
|
sp<Client> client;
|
|
|
|
wp<Client> wclient;
|
|
|
|
status_t lStatus;
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (streamType >= AudioSystem::NUM_STREAM_TYPES) {
|
|
|
|
LOGE("invalid stream type");
|
|
|
|
lStatus = BAD_VALUE;
|
|
|
|
goto Exit;
|
|
|
|
}
|
2009-01-10 01:51:23 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
|
|
|
LOGE("unknown output thread");
|
|
|
|
lStatus = BAD_VALUE;
|
|
|
|
goto Exit;
|
|
|
|
}
|
2009-01-10 01:51:23 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
wclient = mClients.valueFor(pid);
|
2009-01-10 01:51:23 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (wclient != NULL) {
|
|
|
|
client = wclient.promote();
|
|
|
|
} else {
|
|
|
|
client = new Client(this, pid);
|
|
|
|
mClients.add(pid, client);
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
track = thread->createTrack_l(client, streamType, sampleRate, format,
|
|
|
|
channelCount, frameCount, sharedBuffer, &lStatus);
|
2009-03-09 18:52:12 +00:00
|
|
|
}
|
|
|
|
if (lStatus == NO_ERROR) {
|
|
|
|
trackHandle = new TrackHandle(track);
|
|
|
|
} else {
|
2009-09-17 12:12:56 +00:00
|
|
|
// remove local strong reference to Client before deleting the Track so that the Client
|
|
|
|
// destructor is called by the TrackBase destructor with mLock held
|
|
|
|
client.clear();
|
2009-03-09 18:52:12 +00:00
|
|
|
track.clear();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
Exit:
|
|
|
|
if(status) {
|
|
|
|
*status = lStatus;
|
|
|
|
}
|
|
|
|
return trackHandle;
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
uint32_t AudioFlinger::sampleRate(int output) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGW("sampleRate() unknown thread %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return thread->sampleRate();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
int AudioFlinger::channelCount(int output) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGW("channelCount() unknown thread %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return thread->channelCount();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
int AudioFlinger::format(int output) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGW("format() unknown thread %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return thread->format();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
size_t AudioFlinger::frameCount(int output) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGW("frameCount() unknown thread %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return thread->frameCount();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
uint32_t AudioFlinger::latency(int output) const
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGW("latency() unknown thread %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return thread->latency();
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
status_t AudioFlinger::setMasterVolume(float value)
|
|
|
|
{
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when hw supports master volume, don't scale in sw mixer
|
|
|
|
AutoMutex lock(mHardwareLock);
|
|
|
|
mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
|
|
|
|
if (mAudioHardware->setMasterVolume(value) == NO_ERROR) {
|
2009-03-04 03:31:44 +00:00
|
|
|
value = 1.0f;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
2008-12-18 02:05:43 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
mMasterVolume = value;
|
|
|
|
for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
|
2009-07-28 15:44:33 +00:00
|
|
|
mPlaybackThreads.valueAt(i)->setMasterVolume(value);
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
return NO_ERROR;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::setMode(int mode)
|
|
|
|
{
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
if ((mode < 0) || (mode >= AudioSystem::NUM_MODES)) {
|
|
|
|
LOGW("Illegal value: setMode(%d)", mode);
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoMutex lock(mHardwareLock);
|
|
|
|
mHardwareStatus = AUDIO_HW_SET_MODE;
|
|
|
|
status_t ret = mAudioHardware->setMode(mode);
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::setMicMute(bool state)
|
|
|
|
{
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoMutex lock(mHardwareLock);
|
|
|
|
mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
|
|
|
|
status_t ret = mAudioHardware->setMicMute(state);
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::getMicMute() const
|
|
|
|
{
|
|
|
|
bool state = AudioSystem::MODE_INVALID;
|
|
|
|
mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
|
|
|
|
mAudioHardware->getMicMute(&state);
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::setMasterMute(bool muted)
|
|
|
|
{
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
mMasterMute = muted;
|
|
|
|
for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
|
2009-07-28 15:44:33 +00:00
|
|
|
mPlaybackThreads.valueAt(i)->setMasterMute(muted);
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioFlinger::masterVolume() const
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mMasterVolume;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::masterMute() const
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mMasterMute;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
status_t AudioFlinger::setStreamVolume(int stream, float value, int output)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) {
|
2008-10-21 14:00:00 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2008-12-18 02:05:43 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AutoMutex lock(mLock);
|
|
|
|
PlaybackThread *thread = NULL;
|
|
|
|
if (output) {
|
|
|
|
thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mStreamTypes[stream].volume = value;
|
|
|
|
|
|
|
|
if (thread == NULL) {
|
2009-10-21 15:14:22 +00:00
|
|
|
for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
|
2009-10-21 15:14:22 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
} else {
|
|
|
|
thread->setStreamVolume(stream, value);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-10-21 15:14:22 +00:00
|
|
|
return NO_ERROR;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::setStreamMute(int stream, bool muted)
|
|
|
|
{
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES ||
|
2009-03-26 08:57:59 +00:00
|
|
|
uint32_t(stream) == AudioSystem::ENFORCED_AUDIBLE) {
|
2008-10-21 14:00:00 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2009-03-19 00:39:46 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
mStreamTypes[stream].mute = muted;
|
|
|
|
for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
|
2009-07-28 15:44:33 +00:00
|
|
|
mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
float AudioFlinger::streamVolume(int stream, int output) const
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
if (stream < 0 || uint32_t(stream) >= AudioSystem::NUM_STREAM_TYPES) {
|
2008-10-21 14:00:00 +00:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
AutoMutex lock(mLock);
|
|
|
|
float volume;
|
|
|
|
if (output) {
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
volume = thread->streamVolume(stream);
|
|
|
|
} else {
|
|
|
|
volume = mStreamTypes[stream].volume;
|
|
|
|
}
|
|
|
|
|
2009-04-21 14:56:33 +00:00
|
|
|
return volume;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::streamMute(int stream) const
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
if (stream < 0 || stream >= (int)AudioSystem::NUM_STREAM_TYPES) {
|
2008-10-21 14:00:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
return mStreamTypes[stream].mute;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::isMusicActive() const
|
|
|
|
{
|
2009-07-09 10:20:57 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
for (uint32_t i = 0; i < mPlaybackThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
if (mPlaybackThreads.valueAt(i)->isMusicActive()) {
|
2009-07-17 19:17:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
status_t AudioFlinger::setParameters(int ioHandle, const String8& keyValuePairs)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t result;
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGV("setParameters(): io %d, keyvalue %s, tid %d, calling tid %d",
|
2009-07-17 19:17:14 +00:00
|
|
|
ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
2009-01-16 00:12:10 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
// ioHandle == 0 means the parameters are global to the audio hardware interface
|
|
|
|
if (ioHandle == 0) {
|
|
|
|
AutoMutex lock(mHardwareLock);
|
|
|
|
mHardwareStatus = AUDIO_SET_PARAMETER;
|
|
|
|
result = mAudioHardware->setParameters(keyValuePairs);
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-29 18:12:57 +00:00
|
|
|
// hold a strong ref on thread in case closeOutput() or closeInput() is called
|
|
|
|
// and the thread is exited once the lock is released
|
|
|
|
sp<ThreadBase> thread;
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
thread = checkPlaybackThread_l(ioHandle);
|
|
|
|
if (thread == NULL) {
|
|
|
|
thread = checkRecordThread_l(ioHandle);
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-09-29 18:12:57 +00:00
|
|
|
if (thread != NULL) {
|
|
|
|
return thread->setParameters(keyValuePairs);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
String8 AudioFlinger::getParameters(int ioHandle, const String8& keys)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
2009-07-28 15:44:33 +00:00
|
|
|
// LOGV("getParameters() io %d, keys %s, tid %d, calling tid %d",
|
2009-07-17 19:17:14 +00:00
|
|
|
// ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
|
|
|
|
|
|
|
|
if (ioHandle == 0) {
|
|
|
|
return mAudioHardware->getParameters(keys);
|
|
|
|
}
|
2009-09-29 18:12:57 +00:00
|
|
|
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
|
|
|
|
if (playbackThread != NULL) {
|
|
|
|
return playbackThread->getParameters(keys);
|
|
|
|
}
|
|
|
|
RecordThread *recordThread = checkRecordThread_l(ioHandle);
|
|
|
|
if (recordThread != NULL) {
|
|
|
|
return recordThread->getParameters(keys);
|
|
|
|
}
|
|
|
|
return String8("");
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-03-04 03:31:44 +00:00
|
|
|
return mAudioHardware->getInputBufferSize(sampleRate, format, channelCount);
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-10-21 15:14:22 +00:00
|
|
|
status_t AudioFlinger::setVoiceVolume(float value)
|
|
|
|
{
|
|
|
|
// check calling permissions
|
|
|
|
if (!settingsAllowed()) {
|
|
|
|
return PERMISSION_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoMutex lock(mHardwareLock);
|
|
|
|
mHardwareStatus = AUDIO_SET_VOICE_VOLUME;
|
|
|
|
status_t ret = mAudioHardware->setVoiceVolume(value);
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
LOGV("registerClient() %p, tid %d, calling tid %d", client.get(), gettid(), IPCThreadState::self()->getCallingPid());
|
2008-10-21 14:00:00 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
sp<IBinder> binder = client->asBinder();
|
|
|
|
if (mNotificationClients.indexOf(binder) < 0) {
|
|
|
|
LOGV("Adding notification client %p", binder.get());
|
|
|
|
binder->linkToDeath(this);
|
|
|
|
mNotificationClients.add(binder);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the config change is always sent from playback or record threads to avoid deadlock
|
|
|
|
// with AudioSystem::gLock
|
|
|
|
for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mRecordThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
void AudioFlinger::binderDied(const wp<IBinder>& who) {
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
LOGV("binderDied() %p, tid %d, calling tid %d", who.unsafe_get(), gettid(), IPCThreadState::self()->getCallingPid());
|
2008-10-21 14:00:00 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
IBinder *binder = who.unsafe_get();
|
|
|
|
|
|
|
|
if (binder != NULL) {
|
|
|
|
int index = mNotificationClients.indexOf(binder);
|
|
|
|
if (index >= 0) {
|
|
|
|
LOGV("Removing notification client %p", binder);
|
|
|
|
mNotificationClients.removeAt(index);
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-15 14:10:12 +00:00
|
|
|
// audioConfigChanged_l() must be called with AudioFlinger::mLock held
|
2009-11-19 17:00:56 +00:00
|
|
|
void AudioFlinger::audioConfigChanged_l(int event, int ioHandle, void *param2) {
|
|
|
|
size_t size = mNotificationClients.size();
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
sp<IBinder> binder = mNotificationClients.itemAt(i);
|
|
|
|
LOGV("audioConfigChanged_l() Notifying change to client %p", binder.get());
|
|
|
|
sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient> (binder);
|
|
|
|
client->ioConfigChanged(event, ioHandle, param2);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-17 12:12:56 +00:00
|
|
|
// removeClient_l() must be called with AudioFlinger::mLock held
|
|
|
|
void AudioFlinger::removeClient_l(pid_t pid)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-09-17 12:12:56 +00:00
|
|
|
LOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid());
|
2009-03-04 03:31:44 +00:00
|
|
|
mClients.removeItem(pid);
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, int id)
|
2009-07-17 19:17:14 +00:00
|
|
|
: Thread(false),
|
|
|
|
mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0), mChannelCount(0),
|
2009-11-19 17:00:56 +00:00
|
|
|
mFormat(0), mFrameSize(1), mStandby(false), mId(id), mExiting(false)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
}
|
2009-01-22 08:13:42 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::ThreadBase::~ThreadBase()
|
|
|
|
{
|
2009-08-04 16:45:33 +00:00
|
|
|
mParamCond.broadcast();
|
|
|
|
mNewParameters.clear();
|
2009-03-11 19:11:56 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::ThreadBase::exit()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-09-29 18:12:57 +00:00
|
|
|
// keep a strong ref on ourself so that we wont get
|
2009-07-17 19:17:14 +00:00
|
|
|
// destroyed in the middle of requestExitAndWait()
|
|
|
|
sp <ThreadBase> strongMe = this;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("ThreadBase::exit");
|
|
|
|
{
|
|
|
|
AutoMutex lock(&mLock);
|
2009-11-19 17:00:56 +00:00
|
|
|
mExiting = true;
|
2009-07-17 19:17:14 +00:00
|
|
|
requestExit();
|
|
|
|
mWaitWorkCV.signal();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
requestExitAndWait();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
uint32_t AudioFlinger::ThreadBase::sampleRate() const
|
|
|
|
{
|
|
|
|
return mSampleRate;
|
2009-01-22 08:13:42 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
int AudioFlinger::ThreadBase::channelCount() const
|
2009-01-22 08:13:42 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mChannelCount;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
int AudioFlinger::ThreadBase::format() const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return mFormat;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
size_t AudioFlinger::ThreadBase::frameCount() const
|
|
|
|
{
|
|
|
|
return mFrameCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
|
|
|
|
{
|
2009-08-04 16:45:33 +00:00
|
|
|
status_t status;
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-08-04 16:45:33 +00:00
|
|
|
LOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
2009-08-04 16:45:33 +00:00
|
|
|
mNewParameters.add(keyValuePairs);
|
2009-07-17 19:17:14 +00:00
|
|
|
mWaitWorkCV.signal();
|
2009-09-29 18:12:57 +00:00
|
|
|
// wait condition with timeout in case the thread loop has exited
|
|
|
|
// before the request could be processed
|
|
|
|
if (mParamCond.waitRelative(mLock, seconds(2)) == NO_ERROR) {
|
|
|
|
status = mParamStatus;
|
|
|
|
mWaitWorkCV.signal();
|
|
|
|
} else {
|
|
|
|
status = TIMED_OUT;
|
|
|
|
}
|
2009-08-04 16:45:33 +00:00
|
|
|
return status;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
2009-08-04 16:45:33 +00:00
|
|
|
sendConfigEvent_l(event, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendConfigEvent_l() must be called with ThreadBase::mLock held
|
|
|
|
void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param)
|
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
ConfigEvent *configEvent = new ConfigEvent();
|
|
|
|
configEvent->mEvent = event;
|
|
|
|
configEvent->mParam = param;
|
|
|
|
mConfigEvents.add(configEvent);
|
|
|
|
LOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param);
|
|
|
|
mWaitWorkCV.signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::ThreadBase::processConfigEvents()
|
|
|
|
{
|
|
|
|
mLock.lock();
|
|
|
|
while(!mConfigEvents.isEmpty()) {
|
|
|
|
LOGV("processConfigEvents() remaining events %d", mConfigEvents.size());
|
|
|
|
ConfigEvent *configEvent = mConfigEvents[0];
|
|
|
|
mConfigEvents.removeAt(0);
|
2009-09-15 14:10:12 +00:00
|
|
|
// release mLock because audioConfigChanged() will lock AudioFlinger mLock
|
|
|
|
// before calling Audioflinger::audioConfigChanged_l() thus creating
|
2009-07-17 19:17:14 +00:00
|
|
|
// potential cross deadlock between AudioFlinger::mLock and mLock
|
|
|
|
mLock.unlock();
|
|
|
|
audioConfigChanged(configEvent->mEvent, configEvent->mParam);
|
|
|
|
delete configEvent;
|
|
|
|
mLock.lock();
|
|
|
|
}
|
|
|
|
mLock.unlock();
|
|
|
|
}
|
|
|
|
|
2009-11-07 08:01:32 +00:00
|
|
|
status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
|
|
|
|
bool locked = tryLock(mLock);
|
|
|
|
if (!locked) {
|
|
|
|
snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this);
|
|
|
|
write(fd, buffer, strlen(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buffer, SIZE, "standby: %d\n", mStandby);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Format: %d\n", mFormat);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Frame size: %d\n", mFrameSize);
|
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
snprintf(buffer, SIZE, "\nPending setParameters commands: \n");
|
|
|
|
result.append(buffer);
|
|
|
|
result.append(" Index Command");
|
|
|
|
for (size_t i = 0; i < mNewParameters.size(); ++i) {
|
|
|
|
snprintf(buffer, SIZE, "\n %02d ", i);
|
|
|
|
result.append(buffer);
|
|
|
|
result.append(mNewParameters[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buffer, SIZE, "\n\nPending config events: \n");
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, " Index event param\n");
|
|
|
|
result.append(buffer);
|
|
|
|
for (size_t i = 0; i < mConfigEvents.size(); i++) {
|
|
|
|
snprintf(buffer, SIZE, " %02d %02d %d\n", i, mConfigEvents[i]->mEvent, mConfigEvents[i]->mParam);
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
result.append("\n");
|
|
|
|
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
|
|
|
|
if (locked) {
|
|
|
|
mLock.unlock();
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id)
|
|
|
|
: ThreadBase(audioFlinger, id),
|
2009-08-06 15:49:39 +00:00
|
|
|
mMixBuffer(0), mSuspended(0), mBytesWritten(0), mOutput(output),
|
2009-07-23 20:17:39 +00:00
|
|
|
mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
readOutputParameters();
|
|
|
|
|
|
|
|
mMasterVolume = mAudioFlinger->masterVolume();
|
|
|
|
mMasterMute = mAudioFlinger->masterMute();
|
|
|
|
|
|
|
|
for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
|
|
|
|
mStreamTypes[stream].volume = mAudioFlinger->streamVolumeInternal(stream);
|
|
|
|
mStreamTypes[stream].mute = mAudioFlinger->streamMute(stream);
|
|
|
|
}
|
|
|
|
// notify client processes that a new input has been opened
|
|
|
|
sendConfigEvent(AudioSystem::OUTPUT_OPENED);
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioFlinger::PlaybackThread::~PlaybackThread()
|
|
|
|
{
|
|
|
|
delete [] mMixBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
dumpInternals(fd, args);
|
|
|
|
dumpTracks(fd, args);
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
snprintf(buffer, SIZE, "Output thread %p tracks\n", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append(buffer);
|
2009-11-07 08:01:32 +00:00
|
|
|
result.append(" Name Clien Typ Fmt Chn Buf S M F SRate LeftV RighV Serv User\n");
|
2009-03-04 03:31:44 +00:00
|
|
|
for (size_t i = 0; i < mTracks.size(); ++i) {
|
2009-03-31 21:34:35 +00:00
|
|
|
sp<Track> track = mTracks[i];
|
|
|
|
if (track != 0) {
|
|
|
|
track->dump(buffer, SIZE);
|
|
|
|
result.append(buffer);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
snprintf(buffer, SIZE, "Output thread %p active tracks\n", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append(buffer);
|
2009-11-07 08:01:32 +00:00
|
|
|
result.append(" Name Clien Typ Fmt Chn Buf S M F SRate LeftV RighV Serv User\n");
|
2009-03-04 03:31:44 +00:00
|
|
|
for (size_t i = 0; i < mActiveTracks.size(); ++i) {
|
2009-03-31 21:34:35 +00:00
|
|
|
wp<Track> wTrack = mActiveTracks[i];
|
2009-03-04 03:31:44 +00:00
|
|
|
if (wTrack != 0) {
|
|
|
|
sp<Track> track = wTrack.promote();
|
|
|
|
if (track != 0) {
|
|
|
|
track->dump(buffer, SIZE);
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
|
2009-11-07 08:01:32 +00:00
|
|
|
snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite);
|
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
2009-11-07 08:01:32 +00:00
|
|
|
|
|
|
|
dumpBase(fd, args);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thread virtuals
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::PlaybackThread::readyToRun()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
if (mSampleRate == 0) {
|
|
|
|
LOGE("No working audio driver found.");
|
|
|
|
return NO_INIT;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGI("AudioFlinger's thread %p ready to run", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::onFirstRef()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
snprintf(buffer, SIZE, "Playback Thread %p", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
run(buffer, ANDROID_PRIORITY_URGENT_AUDIO);
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
|
|
|
|
sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
|
2009-03-04 03:31:44 +00:00
|
|
|
const sp<AudioFlinger::Client>& client,
|
|
|
|
int streamType,
|
|
|
|
uint32_t sampleRate,
|
|
|
|
int format,
|
|
|
|
int channelCount,
|
|
|
|
int frameCount,
|
|
|
|
const sp<IMemory>& sharedBuffer,
|
|
|
|
status_t *status)
|
|
|
|
{
|
|
|
|
sp<Track> track;
|
|
|
|
status_t lStatus;
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (mType == DIRECT) {
|
|
|
|
if (sampleRate != mSampleRate || format != mFormat || channelCount != mChannelCount) {
|
|
|
|
LOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelCount %d for output %p",
|
|
|
|
sampleRate, format, channelCount, mOutput);
|
|
|
|
lStatus = BAD_VALUE;
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Resampler implementation limits input sampling rate to 2 x output sampling rate.
|
|
|
|
if (sampleRate > mSampleRate*2) {
|
|
|
|
LOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate);
|
|
|
|
lStatus = BAD_VALUE;
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (mOutput == 0) {
|
2009-03-09 18:52:12 +00:00
|
|
|
LOGE("Audio driver not initialized.");
|
|
|
|
lStatus = NO_INIT;
|
|
|
|
goto Exit;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
{ // scope for mLock
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
track = new Track(this, client, streamType, sampleRate, format,
|
|
|
|
channelCount, frameCount, sharedBuffer);
|
2009-11-09 12:45:39 +00:00
|
|
|
if (track->getCblk() == NULL || track->name() < 0) {
|
2009-07-17 19:17:14 +00:00
|
|
|
lStatus = NO_MEMORY;
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
mTracks.add(track);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-03-09 18:52:12 +00:00
|
|
|
lStatus = NO_ERROR;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
Exit:
|
|
|
|
if(status) {
|
|
|
|
*status = lStatus;
|
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AudioFlinger::PlaybackThread::latency() const
|
|
|
|
{
|
|
|
|
if (mOutput) {
|
|
|
|
return mOutput->latency();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::PlaybackThread::setMasterVolume(float value)
|
|
|
|
{
|
|
|
|
mMasterVolume = value;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::PlaybackThread::setMasterMute(bool muted)
|
|
|
|
{
|
|
|
|
mMasterMute = muted;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioFlinger::PlaybackThread::masterVolume() const
|
|
|
|
{
|
|
|
|
return mMasterVolume;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::PlaybackThread::masterMute() const
|
|
|
|
{
|
|
|
|
return mMasterMute;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::PlaybackThread::setStreamVolume(int stream, float value)
|
|
|
|
{
|
|
|
|
mStreamTypes[stream].volume = value;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::PlaybackThread::setStreamMute(int stream, bool muted)
|
|
|
|
{
|
|
|
|
mStreamTypes[stream].mute = muted;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AudioFlinger::PlaybackThread::streamVolume(int stream) const
|
|
|
|
{
|
|
|
|
return mStreamTypes[stream].volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::PlaybackThread::streamMute(int stream) const
|
|
|
|
{
|
|
|
|
return mStreamTypes[stream].mute;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::PlaybackThread::isMusicActive() const
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
size_t count = mActiveTracks.size();
|
|
|
|
for (size_t i = 0 ; i < count ; ++i) {
|
|
|
|
sp<Track> t = mActiveTracks[i].promote();
|
|
|
|
if (t == 0) continue;
|
|
|
|
Track* const track = t.get();
|
2009-07-23 20:17:39 +00:00
|
|
|
if (t->type() == AudioSystem::MUSIC)
|
2009-07-17 19:17:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// addTrack_l() must be called with ThreadBase::mLock held
|
|
|
|
status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
|
|
|
|
{
|
|
|
|
status_t status = ALREADY_EXISTS;
|
|
|
|
|
|
|
|
// set retry count for buffer fill
|
|
|
|
track->mRetryCount = kMaxTrackStartupRetries;
|
|
|
|
if (mActiveTracks.indexOf(track) < 0) {
|
|
|
|
// the track is newly added, make sure it fills up all its
|
|
|
|
// buffers before playing. This is to ensure the client will
|
|
|
|
// effectively get the latency it requested.
|
|
|
|
track->mFillingUpStatus = Track::FS_FILLING;
|
|
|
|
track->mResetDone = false;
|
|
|
|
mActiveTracks.add(track);
|
|
|
|
status = NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGV("mWaitWorkCV.broadcast");
|
|
|
|
mWaitWorkCV.broadcast();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroyTrack_l() must be called with ThreadBase::mLock held
|
|
|
|
void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
|
|
|
|
{
|
|
|
|
track->mState = TrackBase::TERMINATED;
|
|
|
|
if (mActiveTracks.indexOf(track) < 0) {
|
|
|
|
mTracks.remove(track);
|
|
|
|
deleteTrackName_l(track->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
|
|
|
|
{
|
|
|
|
return mOutput->getParameters(keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) {
|
|
|
|
AudioSystem::OutputDescriptor desc;
|
|
|
|
void *param2 = 0;
|
|
|
|
|
|
|
|
LOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event, param);
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case AudioSystem::OUTPUT_OPENED:
|
|
|
|
case AudioSystem::OUTPUT_CONFIG_CHANGED:
|
|
|
|
desc.channels = mChannelCount;
|
|
|
|
desc.samplingRate = mSampleRate;
|
|
|
|
desc.format = mFormat;
|
|
|
|
desc.frameCount = mFrameCount;
|
|
|
|
desc.latency = latency();
|
|
|
|
param2 = &desc;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AudioSystem::STREAM_CONFIG_CHANGED:
|
|
|
|
param2 = ¶m;
|
|
|
|
case AudioSystem::OUTPUT_CLOSED:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-09-15 14:10:12 +00:00
|
|
|
Mutex::Autolock _l(mAudioFlinger->mLock);
|
2009-11-19 17:00:56 +00:00
|
|
|
mAudioFlinger->audioConfigChanged_l(event, mId, param2);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::PlaybackThread::readOutputParameters()
|
|
|
|
{
|
|
|
|
mSampleRate = mOutput->sampleRate();
|
|
|
|
mChannelCount = AudioSystem::popCount(mOutput->channels());
|
|
|
|
|
|
|
|
mFormat = mOutput->format();
|
|
|
|
mFrameSize = mOutput->frameSize();
|
|
|
|
mFrameCount = mOutput->bufferSize() / mFrameSize;
|
|
|
|
|
|
|
|
// FIXME - Current mixer implementation only supports stereo output: Always
|
|
|
|
// Allocate a stereo buffer even if HW output is mono.
|
|
|
|
if (mMixBuffer != NULL) delete mMixBuffer;
|
|
|
|
mMixBuffer = new int16_t[mFrameCount * 2];
|
|
|
|
memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id)
|
|
|
|
: PlaybackThread(audioFlinger, output, id),
|
2009-07-17 19:17:14 +00:00
|
|
|
mAudioMixer(0)
|
|
|
|
{
|
|
|
|
mType = PlaybackThread::MIXER;
|
|
|
|
mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
|
|
|
|
|
|
|
|
// FIXME - Current mixer implementation only supports stereo output
|
|
|
|
if (mChannelCount == 1) {
|
|
|
|
LOGE("Invalid audio hardware channel count");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioFlinger::MixerThread::~MixerThread()
|
|
|
|
{
|
|
|
|
delete mAudioMixer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::MixerThread::threadLoop()
|
|
|
|
{
|
|
|
|
int16_t* curBuf = mMixBuffer;
|
|
|
|
Vector< sp<Track> > tracksToRemove;
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t mixerStatus = MIXER_IDLE;
|
2009-07-17 19:17:14 +00:00
|
|
|
nsecs_t standbyTime = systemTime();
|
|
|
|
size_t mixBufferSize = mFrameCount * mFrameSize;
|
2009-09-30 10:09:03 +00:00
|
|
|
// FIXME: Relaxed timing because of a certain device that can't meet latency
|
|
|
|
// Should be reduced to 2x after the vendor fixes the driver issue
|
|
|
|
nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 3;
|
|
|
|
nsecs_t lastWarning = 0;
|
2009-11-10 07:32:22 +00:00
|
|
|
bool longStandbyExit = false;
|
|
|
|
uint32_t activeSleepTime = activeSleepTimeUs();
|
|
|
|
uint32_t idleSleepTime = idleSleepTimeUs();
|
|
|
|
uint32_t sleepTime = idleSleepTime;
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
while (!exitPending())
|
|
|
|
{
|
|
|
|
processConfigEvents();
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
mixerStatus = MIXER_IDLE;
|
2009-07-17 19:17:14 +00:00
|
|
|
{ // scope for mLock
|
|
|
|
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
|
|
|
if (checkForNewParameters_l()) {
|
|
|
|
mixBufferSize = mFrameCount * mFrameSize;
|
2009-09-30 10:09:03 +00:00
|
|
|
// FIXME: Relaxed timing because of a certain device that can't meet latency
|
|
|
|
// Should be reduced to 2x after the vendor fixes the driver issue
|
|
|
|
maxPeriod = seconds(mFrameCount) / mSampleRate * 3;
|
2009-11-10 07:32:22 +00:00
|
|
|
activeSleepTime = activeSleepTimeUs();
|
|
|
|
idleSleepTime = idleSleepTimeUs();
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
|
|
|
|
|
|
|
|
// put audio hardware into standby after short delay
|
|
|
|
if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
|
|
|
|
mSuspended) {
|
|
|
|
if (!mStandby) {
|
|
|
|
LOGV("Audio hardware entering standby, mixer %p, mSuspended %d\n", this, mSuspended);
|
|
|
|
mOutput->standby();
|
|
|
|
mStandby = true;
|
|
|
|
mBytesWritten = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!activeTracks.size() && mConfigEvents.isEmpty()) {
|
|
|
|
// we're about to wait, flush the binder command buffer
|
|
|
|
IPCThreadState::self()->flushCommands();
|
|
|
|
|
|
|
|
if (exitPending()) break;
|
|
|
|
|
|
|
|
// wait until we have something to do...
|
|
|
|
LOGV("MixerThread %p TID %d going to sleep\n", this, gettid());
|
|
|
|
mWaitWorkCV.wait(mLock);
|
|
|
|
LOGV("MixerThread %p TID %d waking up\n", this, gettid());
|
|
|
|
|
|
|
|
if (mMasterMute == false) {
|
|
|
|
char value[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("ro.audio.silent", value, "0");
|
|
|
|
if (atoi(value)) {
|
|
|
|
LOGD("Silence is golden");
|
|
|
|
setMasterMute(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
standbyTime = systemTime() + kStandbyTimeInNsecs;
|
2009-11-10 07:32:22 +00:00
|
|
|
sleepTime = idleSleepTime;
|
2009-07-17 19:17:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
|
2009-09-22 07:35:48 +00:00
|
|
|
// mix buffers...
|
|
|
|
mAudioMixer->process(curBuf);
|
|
|
|
sleepTime = 0;
|
|
|
|
standbyTime = systemTime() + kStandbyTimeInNsecs;
|
2009-09-07 15:38:38 +00:00
|
|
|
} else {
|
2009-10-06 03:29:18 +00:00
|
|
|
// If no tracks are ready, sleep once for the duration of an output
|
|
|
|
// buffer size, then write 0s to the output
|
|
|
|
if (sleepTime == 0) {
|
2009-11-10 07:32:22 +00:00
|
|
|
if (mixerStatus == MIXER_TRACKS_ENABLED) {
|
|
|
|
sleepTime = activeSleepTime;
|
|
|
|
} else {
|
|
|
|
sleepTime = idleSleepTime;
|
|
|
|
}
|
|
|
|
} else if (mBytesWritten != 0 ||
|
|
|
|
(mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) {
|
2009-09-22 07:35:48 +00:00
|
|
|
memset (curBuf, 0, mixBufferSize);
|
2009-09-07 15:38:38 +00:00
|
|
|
sleepTime = 0;
|
2009-11-10 07:32:22 +00:00
|
|
|
LOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start");
|
2009-09-07 15:38:38 +00:00
|
|
|
}
|
2009-09-22 07:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mSuspended) {
|
2009-11-10 07:32:22 +00:00
|
|
|
sleepTime = idleSleepTime;
|
2009-09-22 07:35:48 +00:00
|
|
|
}
|
|
|
|
// sleepTime == 0 means we must write to audio hardware
|
|
|
|
if (sleepTime == 0) {
|
|
|
|
mLastWriteTime = systemTime();
|
|
|
|
mInWrite = true;
|
|
|
|
int bytesWritten = (int)mOutput->write(curBuf, mixBufferSize);
|
|
|
|
if (bytesWritten > 0) mBytesWritten += bytesWritten;
|
|
|
|
mNumWrites++;
|
|
|
|
mInWrite = false;
|
2009-09-30 10:09:03 +00:00
|
|
|
nsecs_t now = systemTime();
|
|
|
|
nsecs_t delta = now - mLastWriteTime;
|
2009-09-22 07:35:48 +00:00
|
|
|
if (delta > maxPeriod) {
|
|
|
|
mNumDelayedWrites++;
|
2009-09-30 10:09:03 +00:00
|
|
|
if ((now - lastWarning) > kWarningThrottle) {
|
|
|
|
LOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
|
|
|
|
ns2ms(delta), mNumDelayedWrites, this);
|
|
|
|
lastWarning = now;
|
|
|
|
}
|
2009-11-10 07:32:22 +00:00
|
|
|
if (mStandby) {
|
|
|
|
longStandbyExit = true;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-11-10 07:32:22 +00:00
|
|
|
mStandby = false;
|
2009-09-22 07:35:48 +00:00
|
|
|
} else {
|
|
|
|
usleep(sleepTime);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// finally let go of all our tracks, without the lock held
|
|
|
|
// since we can't guarantee the destructors won't acquire that
|
|
|
|
// same lock.
|
|
|
|
tracksToRemove.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mStandby) {
|
|
|
|
mOutput->standby();
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGV("MixerThread %p exiting", this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareTracks_l() must be called with ThreadBase::mLock held
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t AudioFlinger::MixerThread::prepareTracks_l(const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t mixerStatus = MIXER_IDLE;
|
2009-07-17 19:17:14 +00:00
|
|
|
// find out which tracks need to be processed
|
|
|
|
size_t count = activeTracks.size();
|
|
|
|
for (size_t i=0 ; i<count ; i++) {
|
|
|
|
sp<Track> t = activeTracks[i].promote();
|
|
|
|
if (t == 0) continue;
|
|
|
|
|
|
|
|
Track* const track = t.get();
|
|
|
|
audio_track_cblk_t* cblk = track->cblk();
|
|
|
|
|
|
|
|
// The first time a track is added we wait
|
|
|
|
// for all its buffers to be filled before processing it
|
|
|
|
mAudioMixer->setActiveTrack(track->name());
|
|
|
|
if (cblk->framesReady() && (track->isReady() || track->isStopped()) &&
|
|
|
|
!track->isPaused())
|
|
|
|
{
|
|
|
|
//LOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
|
|
|
|
|
|
|
|
// compute volume for this track
|
|
|
|
int16_t left, right;
|
|
|
|
if (track->isMuted() || mMasterMute || track->isPausing() ||
|
|
|
|
mStreamTypes[track->type()].mute) {
|
|
|
|
left = right = 0;
|
|
|
|
if (track->isPausing()) {
|
|
|
|
track->setPaused();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
float typeVolume = mStreamTypes[track->type()].volume;
|
|
|
|
float v = mMasterVolume * typeVolume;
|
|
|
|
float v_clamped = v * cblk->volume[0];
|
|
|
|
if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
|
|
|
|
left = int16_t(v_clamped);
|
|
|
|
v_clamped = v * cblk->volume[1];
|
|
|
|
if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
|
|
|
|
right = int16_t(v_clamped);
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: these things DON'T need to be done each time
|
|
|
|
mAudioMixer->setBufferProvider(track);
|
|
|
|
mAudioMixer->enable(AudioMixer::MIXING);
|
|
|
|
|
2009-09-03 10:45:52 +00:00
|
|
|
int param = AudioMixer::VOLUME;
|
|
|
|
if (track->mFillingUpStatus == Track::FS_FILLED) {
|
2009-07-17 19:17:14 +00:00
|
|
|
// no ramp for the first volume setting
|
|
|
|
track->mFillingUpStatus = Track::FS_ACTIVE;
|
|
|
|
if (track->mState == TrackBase::RESUMING) {
|
|
|
|
track->mState = TrackBase::ACTIVE;
|
|
|
|
param = AudioMixer::RAMP_VOLUME;
|
|
|
|
}
|
2009-09-03 10:45:52 +00:00
|
|
|
} else if (cblk->server != 0) {
|
|
|
|
// If the track is stopped before the first frame was mixed,
|
|
|
|
// do not apply ramp
|
2009-07-17 19:17:14 +00:00
|
|
|
param = AudioMixer::RAMP_VOLUME;
|
|
|
|
}
|
2009-09-03 10:45:52 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
mAudioMixer->setParameter(param, AudioMixer::VOLUME0, left);
|
|
|
|
mAudioMixer->setParameter(param, AudioMixer::VOLUME1, right);
|
|
|
|
mAudioMixer->setParameter(
|
|
|
|
AudioMixer::TRACK,
|
|
|
|
AudioMixer::FORMAT, track->format());
|
|
|
|
mAudioMixer->setParameter(
|
|
|
|
AudioMixer::TRACK,
|
|
|
|
AudioMixer::CHANNEL_COUNT, track->channelCount());
|
|
|
|
mAudioMixer->setParameter(
|
|
|
|
AudioMixer::RESAMPLE,
|
|
|
|
AudioMixer::SAMPLE_RATE,
|
|
|
|
int(cblk->sampleRate));
|
|
|
|
|
|
|
|
// reset retry count
|
|
|
|
track->mRetryCount = kMaxTrackRetries;
|
2009-11-10 07:32:22 +00:00
|
|
|
mixerStatus = MIXER_TRACKS_READY;
|
2009-07-17 19:17:14 +00:00
|
|
|
} else {
|
|
|
|
//LOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
|
|
|
|
if (track->isStopped()) {
|
|
|
|
track->reset();
|
|
|
|
}
|
|
|
|
if (track->isTerminated() || track->isStopped() || track->isPaused()) {
|
|
|
|
// We have consumed all the buffers of this track.
|
|
|
|
// Remove it from the list of active tracks.
|
|
|
|
tracksToRemove->add(track);
|
|
|
|
mAudioMixer->disable(AudioMixer::MIXING);
|
|
|
|
} else {
|
|
|
|
// No buffers for this track. Give it a few chances to
|
|
|
|
// fill a buffer, then remove it from active list.
|
|
|
|
if (--(track->mRetryCount) <= 0) {
|
2009-10-06 03:29:18 +00:00
|
|
|
LOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", track->name(), this);
|
2009-07-17 19:17:14 +00:00
|
|
|
tracksToRemove->add(track);
|
2009-11-10 07:32:22 +00:00
|
|
|
} else if (mixerStatus != MIXER_TRACKS_READY) {
|
|
|
|
mixerStatus = MIXER_TRACKS_ENABLED;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-11-10 07:32:22 +00:00
|
|
|
|
|
|
|
mAudioMixer->disable(AudioMixer::MIXING);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all the tracks that need to be...
|
|
|
|
count = tracksToRemove->size();
|
|
|
|
if (UNLIKELY(count)) {
|
|
|
|
for (size_t i=0 ; i<count ; i++) {
|
|
|
|
const sp<Track>& track = tracksToRemove->itemAt(i);
|
|
|
|
mActiveTracks.remove(track);
|
|
|
|
if (track->isTerminated()) {
|
|
|
|
mTracks.remove(track);
|
|
|
|
deleteTrackName_l(track->mName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
return mixerStatus;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::MixerThread::getTracks(
|
|
|
|
SortedVector < sp<Track> >& tracks,
|
|
|
|
SortedVector < wp<Track> >& activeTracks,
|
|
|
|
int streamType)
|
|
|
|
{
|
|
|
|
LOGV ("MixerThread::getTracks() mixer %p, mTracks.size %d, mActiveTracks.size %d", this, mTracks.size(), mActiveTracks.size());
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
size_t size = mTracks.size();
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
sp<Track> t = mTracks[i];
|
|
|
|
if (t->type() == streamType) {
|
|
|
|
tracks.add(t);
|
|
|
|
int j = mActiveTracks.indexOf(t);
|
|
|
|
if (j >= 0) {
|
|
|
|
t = mActiveTracks[j].promote();
|
|
|
|
if (t != NULL) {
|
|
|
|
activeTracks.add(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size = activeTracks.size();
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
mActiveTracks.remove(activeTracks[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
size = tracks.size();
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
sp<Track> t = tracks[i];
|
|
|
|
mTracks.remove(t);
|
|
|
|
deleteTrackName_l(t->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::MixerThread::putTracks(
|
|
|
|
SortedVector < sp<Track> >& tracks,
|
|
|
|
SortedVector < wp<Track> >& activeTracks)
|
|
|
|
{
|
|
|
|
LOGV ("MixerThread::putTracks() mixer %p, tracks.size %d, activeTracks.size %d", this, tracks.size(), activeTracks.size());
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
size_t size = tracks.size();
|
|
|
|
for (size_t i = 0; i < size ; i++) {
|
|
|
|
sp<Track> t = tracks[i];
|
|
|
|
int name = getTrackName_l();
|
|
|
|
|
|
|
|
if (name < 0) return;
|
|
|
|
|
|
|
|
t->mName = name;
|
|
|
|
t->mThread = this;
|
|
|
|
mTracks.add(t);
|
|
|
|
|
|
|
|
int j = activeTracks.indexOf(t);
|
|
|
|
if (j >= 0) {
|
|
|
|
mActiveTracks.add(t);
|
2009-09-01 12:56:26 +00:00
|
|
|
// force buffer refilling and no ramp volume when the track is mixed for the first time
|
|
|
|
t->mFillingUpStatus = Track::FS_FILLING;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTrackName_l() must be called with ThreadBase::mLock held
|
|
|
|
int AudioFlinger::MixerThread::getTrackName_l()
|
|
|
|
{
|
|
|
|
return mAudioMixer->getTrackName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteTrackName_l() must be called with ThreadBase::mLock held
|
|
|
|
void AudioFlinger::MixerThread::deleteTrackName_l(int name)
|
|
|
|
{
|
2009-12-07 18:53:10 +00:00
|
|
|
LOGV("remove track (%d) and delete from mixer", name);
|
2009-07-17 19:17:14 +00:00
|
|
|
mAudioMixer->deleteTrackName(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkForNewParameters_l() must be called with ThreadBase::mLock held
|
|
|
|
bool AudioFlinger::MixerThread::checkForNewParameters_l()
|
|
|
|
{
|
|
|
|
bool reconfig = false;
|
|
|
|
|
2009-08-04 16:45:33 +00:00
|
|
|
while (!mNewParameters.isEmpty()) {
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t status = NO_ERROR;
|
2009-08-04 16:45:33 +00:00
|
|
|
String8 keyValuePair = mNewParameters[0];
|
|
|
|
AudioParameter param = AudioParameter(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
int value;
|
2009-08-04 16:45:33 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
|
|
|
|
if (value != AudioSystem::PCM_16_BIT) {
|
|
|
|
status = BAD_VALUE;
|
|
|
|
} else {
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
|
|
|
|
if (value != AudioSystem::CHANNEL_OUT_STEREO) {
|
|
|
|
status = BAD_VALUE;
|
|
|
|
} else {
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
|
|
|
|
// do not accept frame count changes if tracks are open as the track buffer
|
|
|
|
// size depends on frame count and correct behavior would not be garantied
|
|
|
|
// if frame count is changed after track creation
|
|
|
|
if (!mTracks.isEmpty()) {
|
|
|
|
status = INVALID_OPERATION;
|
|
|
|
} else {
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status == NO_ERROR) {
|
2009-08-04 16:45:33 +00:00
|
|
|
status = mOutput->setParameters(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
if (!mStandby && status == INVALID_OPERATION) {
|
|
|
|
mOutput->standby();
|
|
|
|
mStandby = true;
|
|
|
|
mBytesWritten = 0;
|
2009-08-04 16:45:33 +00:00
|
|
|
status = mOutput->setParameters(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
if (status == NO_ERROR && reconfig) {
|
|
|
|
delete mAudioMixer;
|
|
|
|
readOutputParameters();
|
|
|
|
mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
|
|
|
|
for (size_t i = 0; i < mTracks.size() ; i++) {
|
|
|
|
int name = getTrackName_l();
|
|
|
|
if (name < 0) break;
|
|
|
|
mTracks[i]->mName = name;
|
2009-08-10 15:15:12 +00:00
|
|
|
// limit track sample rate to 2 x new output sample rate
|
|
|
|
if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) {
|
|
|
|
mTracks[i]->mCblk->sampleRate = 2 * sampleRate();
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-08-04 16:45:33 +00:00
|
|
|
sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-07 08:01:32 +00:00
|
|
|
|
|
|
|
mNewParameters.removeAt(0);
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
mParamStatus = status;
|
|
|
|
mParamCond.signal();
|
2009-08-04 16:45:33 +00:00
|
|
|
mWaitWorkCV.wait(mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
return reconfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
|
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
|
|
|
|
PlaybackThread::dumpInternals(fd, args);
|
|
|
|
|
|
|
|
snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames());
|
|
|
|
result.append(buffer);
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t AudioFlinger::MixerThread::activeSleepTimeUs()
|
2009-10-06 03:29:18 +00:00
|
|
|
{
|
2009-11-10 07:32:22 +00:00
|
|
|
return (uint32_t)(mOutput->latency() * 1000) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AudioFlinger::MixerThread::idleSleepTimeUs()
|
|
|
|
{
|
|
|
|
return (uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000;
|
2009-10-06 03:29:18 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-11-19 17:00:56 +00:00
|
|
|
AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id)
|
|
|
|
: PlaybackThread(audioFlinger, output, id),
|
2009-07-17 19:17:14 +00:00
|
|
|
mLeftVolume (1.0), mRightVolume(1.0)
|
|
|
|
{
|
|
|
|
mType = PlaybackThread::DIRECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioFlinger::DirectOutputThread::~DirectOutputThread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool AudioFlinger::DirectOutputThread::threadLoop()
|
|
|
|
{
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t mixerStatus = MIXER_IDLE;
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<Track> trackToRemove;
|
|
|
|
sp<Track> activeTrack;
|
|
|
|
nsecs_t standbyTime = systemTime();
|
|
|
|
int8_t *curBuf;
|
|
|
|
size_t mixBufferSize = mFrameCount*mFrameSize;
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t activeSleepTime = activeSleepTimeUs();
|
|
|
|
uint32_t idleSleepTime = idleSleepTimeUs();
|
|
|
|
uint32_t sleepTime = idleSleepTime;
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
while (!exitPending())
|
|
|
|
{
|
|
|
|
processConfigEvents();
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
mixerStatus = MIXER_IDLE;
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
{ // scope for the mLock
|
|
|
|
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
|
|
|
if (checkForNewParameters_l()) {
|
|
|
|
mixBufferSize = mFrameCount*mFrameSize;
|
2009-11-10 07:32:22 +00:00
|
|
|
activeSleepTime = activeSleepTimeUs();
|
|
|
|
idleSleepTime = idleSleepTimeUs();
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// put audio hardware into standby after short delay
|
|
|
|
if UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) ||
|
|
|
|
mSuspended) {
|
|
|
|
// wait until we have something to do...
|
|
|
|
if (!mStandby) {
|
|
|
|
LOGV("Audio hardware entering standby, mixer %p\n", this);
|
|
|
|
mOutput->standby();
|
|
|
|
mStandby = true;
|
|
|
|
mBytesWritten = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
|
|
|
|
// we're about to wait, flush the binder command buffer
|
|
|
|
IPCThreadState::self()->flushCommands();
|
|
|
|
|
|
|
|
if (exitPending()) break;
|
|
|
|
|
|
|
|
LOGV("DirectOutputThread %p TID %d going to sleep\n", this, gettid());
|
|
|
|
mWaitWorkCV.wait(mLock);
|
|
|
|
LOGV("DirectOutputThread %p TID %d waking up in active mode\n", this, gettid());
|
|
|
|
|
|
|
|
if (mMasterMute == false) {
|
|
|
|
char value[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("ro.audio.silent", value, "0");
|
|
|
|
if (atoi(value)) {
|
|
|
|
LOGD("Silence is golden");
|
|
|
|
setMasterMute(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
standbyTime = systemTime() + kStandbyTimeInNsecs;
|
2009-11-10 07:32:22 +00:00
|
|
|
sleepTime = idleSleepTime;
|
2009-07-17 19:17:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find out which tracks need to be processed
|
|
|
|
if (mActiveTracks.size() != 0) {
|
|
|
|
sp<Track> t = mActiveTracks[0].promote();
|
|
|
|
if (t == 0) continue;
|
|
|
|
|
|
|
|
Track* const track = t.get();
|
|
|
|
audio_track_cblk_t* cblk = track->cblk();
|
|
|
|
|
|
|
|
// The first time a track is added we wait
|
|
|
|
// for all its buffers to be filled before processing it
|
|
|
|
if (cblk->framesReady() && (track->isReady() || track->isStopped()) &&
|
|
|
|
!track->isPaused())
|
|
|
|
{
|
|
|
|
//LOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
|
|
|
|
|
|
|
|
// compute volume for this track
|
|
|
|
float left, right;
|
|
|
|
if (track->isMuted() || mMasterMute || track->isPausing() ||
|
|
|
|
mStreamTypes[track->type()].mute) {
|
|
|
|
left = right = 0;
|
|
|
|
if (track->isPausing()) {
|
|
|
|
track->setPaused();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
float typeVolume = mStreamTypes[track->type()].volume;
|
|
|
|
float v = mMasterVolume * typeVolume;
|
|
|
|
float v_clamped = v * cblk->volume[0];
|
|
|
|
if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
|
|
|
|
left = v_clamped/MAX_GAIN;
|
|
|
|
v_clamped = v * cblk->volume[1];
|
|
|
|
if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
|
|
|
|
right = v_clamped/MAX_GAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (left != mLeftVolume || right != mRightVolume) {
|
|
|
|
mOutput->setVolume(left, right);
|
|
|
|
left = mLeftVolume;
|
|
|
|
right = mRightVolume;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track->mFillingUpStatus == Track::FS_FILLED) {
|
|
|
|
track->mFillingUpStatus = Track::FS_ACTIVE;
|
|
|
|
if (track->mState == TrackBase::RESUMING) {
|
|
|
|
track->mState = TrackBase::ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset retry count
|
|
|
|
track->mRetryCount = kMaxTrackRetries;
|
|
|
|
activeTrack = t;
|
2009-11-10 07:32:22 +00:00
|
|
|
mixerStatus = MIXER_TRACKS_READY;
|
2009-07-17 19:17:14 +00:00
|
|
|
} else {
|
|
|
|
//LOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
|
|
|
|
if (track->isStopped()) {
|
|
|
|
track->reset();
|
|
|
|
}
|
|
|
|
if (track->isTerminated() || track->isStopped() || track->isPaused()) {
|
|
|
|
// We have consumed all the buffers of this track.
|
|
|
|
// Remove it from the list of active tracks.
|
|
|
|
trackToRemove = track;
|
|
|
|
} else {
|
|
|
|
// No buffers for this track. Give it a few chances to
|
|
|
|
// fill a buffer, then remove it from active list.
|
|
|
|
if (--(track->mRetryCount) <= 0) {
|
|
|
|
LOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
|
|
|
|
trackToRemove = track;
|
2009-11-10 07:32:22 +00:00
|
|
|
} else {
|
|
|
|
mixerStatus = MIXER_TRACKS_ENABLED;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-11-10 07:32:22 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// remove all the tracks that need to be...
|
|
|
|
if (UNLIKELY(trackToRemove != 0)) {
|
|
|
|
mActiveTracks.remove(trackToRemove);
|
|
|
|
if (trackToRemove->isTerminated()) {
|
|
|
|
mTracks.remove(trackToRemove);
|
|
|
|
deleteTrackName_l(trackToRemove->mName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
|
2009-09-22 07:35:48 +00:00
|
|
|
AudioBufferProvider::Buffer buffer;
|
|
|
|
size_t frameCount = mFrameCount;
|
|
|
|
curBuf = (int8_t *)mMixBuffer;
|
|
|
|
// output audio to hardware
|
|
|
|
while(frameCount) {
|
|
|
|
buffer.frameCount = frameCount;
|
|
|
|
activeTrack->getNextBuffer(&buffer);
|
|
|
|
if (UNLIKELY(buffer.raw == 0)) {
|
|
|
|
memset(curBuf, 0, frameCount * mFrameSize);
|
|
|
|
break;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-09-22 07:35:48 +00:00
|
|
|
memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
|
|
|
|
frameCount -= buffer.frameCount;
|
|
|
|
curBuf += buffer.frameCount * mFrameSize;
|
|
|
|
activeTrack->releaseBuffer(&buffer);
|
|
|
|
}
|
|
|
|
sleepTime = 0;
|
|
|
|
standbyTime = systemTime() + kStandbyTimeInNsecs;
|
|
|
|
} else {
|
2009-10-06 03:29:18 +00:00
|
|
|
if (sleepTime == 0) {
|
2009-11-10 07:32:22 +00:00
|
|
|
if (mixerStatus == MIXER_TRACKS_ENABLED) {
|
|
|
|
sleepTime = activeSleepTime;
|
|
|
|
} else {
|
|
|
|
sleepTime = idleSleepTime;
|
|
|
|
}
|
2009-10-06 03:29:18 +00:00
|
|
|
} else if (mBytesWritten != 0 && AudioSystem::isLinearPCM(mFormat)) {
|
2009-09-22 07:35:48 +00:00
|
|
|
memset (mMixBuffer, 0, mFrameCount * mFrameSize);
|
2009-09-07 15:38:38 +00:00
|
|
|
sleepTime = 0;
|
|
|
|
}
|
2009-09-22 07:35:48 +00:00
|
|
|
}
|
2009-09-07 15:38:38 +00:00
|
|
|
|
2009-09-22 07:35:48 +00:00
|
|
|
if (mSuspended) {
|
2009-11-10 07:32:22 +00:00
|
|
|
sleepTime = idleSleepTime;
|
2009-09-22 07:35:48 +00:00
|
|
|
}
|
|
|
|
// sleepTime == 0 means we must write to audio hardware
|
|
|
|
if (sleepTime == 0) {
|
|
|
|
mLastWriteTime = systemTime();
|
|
|
|
mInWrite = true;
|
|
|
|
int bytesWritten = (int)mOutput->write(mMixBuffer, mixBufferSize);
|
|
|
|
if (bytesWritten) mBytesWritten += bytesWritten;
|
|
|
|
mNumWrites++;
|
|
|
|
mInWrite = false;
|
|
|
|
mStandby = false;
|
|
|
|
} else {
|
|
|
|
usleep(sleepTime);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// finally let go of removed track, without the lock held
|
|
|
|
// since we can't guarantee the destructors won't acquire that
|
|
|
|
// same lock.
|
|
|
|
trackToRemove.clear();
|
|
|
|
activeTrack.clear();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (!mStandby) {
|
|
|
|
mOutput->standby();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("DirectOutputThread %p exiting", this);
|
|
|
|
return false;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// getTrackName_l() must be called with ThreadBase::mLock held
|
|
|
|
int AudioFlinger::DirectOutputThread::getTrackName_l()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
return 0;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// deleteTrackName_l() must be called with ThreadBase::mLock held
|
|
|
|
void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// checkForNewParameters_l() must be called with ThreadBase::mLock held
|
|
|
|
bool AudioFlinger::DirectOutputThread::checkForNewParameters_l()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
bool reconfig = false;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-08-04 16:45:33 +00:00
|
|
|
while (!mNewParameters.isEmpty()) {
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t status = NO_ERROR;
|
2009-08-04 16:45:33 +00:00
|
|
|
String8 keyValuePair = mNewParameters[0];
|
|
|
|
AudioParameter param = AudioParameter(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
int value;
|
2009-08-04 16:45:33 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
|
|
|
|
// do not accept frame count changes if tracks are open as the track buffer
|
|
|
|
// size depends on frame count and correct behavior would not be garantied
|
|
|
|
// if frame count is changed after track creation
|
|
|
|
if (!mTracks.isEmpty()) {
|
|
|
|
status = INVALID_OPERATION;
|
|
|
|
} else {
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status == NO_ERROR) {
|
2009-08-04 16:45:33 +00:00
|
|
|
status = mOutput->setParameters(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
if (!mStandby && status == INVALID_OPERATION) {
|
|
|
|
mOutput->standby();
|
|
|
|
mStandby = true;
|
|
|
|
mBytesWritten = 0;
|
2009-08-04 16:45:33 +00:00
|
|
|
status = mOutput->setParameters(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
if (status == NO_ERROR && reconfig) {
|
|
|
|
readOutputParameters();
|
2009-08-04 16:45:33 +00:00
|
|
|
sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-07 08:01:32 +00:00
|
|
|
|
|
|
|
mNewParameters.removeAt(0);
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
mParamStatus = status;
|
|
|
|
mParamCond.signal();
|
2009-08-04 16:45:33 +00:00
|
|
|
mWaitWorkCV.wait(mLock);
|
2009-01-22 08:13:42 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
return reconfig;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs()
|
2009-10-06 03:29:18 +00:00
|
|
|
{
|
|
|
|
uint32_t time;
|
|
|
|
if (AudioSystem::isLinearPCM(mFormat)) {
|
2009-11-10 07:32:22 +00:00
|
|
|
time = (uint32_t)(mOutput->latency() * 1000) / 2;
|
|
|
|
} else {
|
|
|
|
time = 10000;
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs()
|
|
|
|
{
|
|
|
|
uint32_t time;
|
|
|
|
if (AudioSystem::isLinearPCM(mFormat)) {
|
|
|
|
time = (uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000;
|
2009-10-06 03:29:18 +00:00
|
|
|
} else {
|
|
|
|
time = 10000;
|
|
|
|
}
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger, AudioFlinger::MixerThread* mainThread, int id)
|
|
|
|
: MixerThread(audioFlinger, mainThread->getOutput(), id)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
mType = PlaybackThread::DUPLICATING;
|
|
|
|
addOutputTrack(mainThread);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::DuplicatingThread::~DuplicatingThread()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-12-07 18:53:10 +00:00
|
|
|
for (size_t i = 0; i < mOutputTracks.size(); i++) {
|
|
|
|
mOutputTracks[i]->destroy();
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
mOutputTracks.clear();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
bool AudioFlinger::DuplicatingThread::threadLoop()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
int16_t* curBuf = mMixBuffer;
|
|
|
|
Vector< sp<Track> > tracksToRemove;
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t mixerStatus = MIXER_IDLE;
|
2009-07-17 19:17:14 +00:00
|
|
|
nsecs_t standbyTime = systemTime();
|
|
|
|
size_t mixBufferSize = mFrameCount*mFrameSize;
|
|
|
|
SortedVector< sp<OutputTrack> > outputTracks;
|
2009-10-06 03:29:18 +00:00
|
|
|
uint32_t writeFrames = 0;
|
2009-11-10 07:32:22 +00:00
|
|
|
uint32_t activeSleepTime = activeSleepTimeUs();
|
|
|
|
uint32_t idleSleepTime = idleSleepTimeUs();
|
|
|
|
uint32_t sleepTime = idleSleepTime;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
while (!exitPending())
|
|
|
|
{
|
|
|
|
processConfigEvents();
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
mixerStatus = MIXER_IDLE;
|
2009-07-17 19:17:14 +00:00
|
|
|
{ // scope for the mLock
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (checkForNewParameters_l()) {
|
|
|
|
mixBufferSize = mFrameCount*mFrameSize;
|
2009-11-10 07:32:22 +00:00
|
|
|
activeSleepTime = activeSleepTimeUs();
|
|
|
|
idleSleepTime = idleSleepTimeUs();
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
for (size_t i = 0; i < mOutputTracks.size(); i++) {
|
|
|
|
outputTracks.add(mOutputTracks[i]);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// put audio hardware into standby after short delay
|
|
|
|
if UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
|
|
|
|
mSuspended) {
|
|
|
|
if (!mStandby) {
|
|
|
|
for (size_t i = 0; i < outputTracks.size(); i++) {
|
|
|
|
outputTracks[i]->stop();
|
|
|
|
}
|
|
|
|
mStandby = true;
|
|
|
|
mBytesWritten = 0;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (!activeTracks.size() && mConfigEvents.isEmpty()) {
|
|
|
|
// we're about to wait, flush the binder command buffer
|
|
|
|
IPCThreadState::self()->flushCommands();
|
|
|
|
outputTracks.clear();
|
|
|
|
|
|
|
|
if (exitPending()) break;
|
|
|
|
|
|
|
|
LOGV("DuplicatingThread %p TID %d going to sleep\n", this, gettid());
|
|
|
|
mWaitWorkCV.wait(mLock);
|
|
|
|
LOGV("DuplicatingThread %p TID %d waking up\n", this, gettid());
|
|
|
|
if (mMasterMute == false) {
|
|
|
|
char value[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("ro.audio.silent", value, "0");
|
|
|
|
if (atoi(value)) {
|
|
|
|
LOGD("Silence is golden");
|
|
|
|
setMasterMute(true);
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
standbyTime = systemTime() + kStandbyTimeInNsecs;
|
2009-11-10 07:32:22 +00:00
|
|
|
sleepTime = idleSleepTime;
|
2009-07-17 19:17:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove);
|
2009-09-22 07:35:48 +00:00
|
|
|
}
|
2009-01-22 08:13:42 +00:00
|
|
|
|
2009-11-10 07:32:22 +00:00
|
|
|
if (LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
|
2009-07-17 19:17:14 +00:00
|
|
|
// mix buffers...
|
|
|
|
mAudioMixer->process(curBuf);
|
2009-09-22 07:35:48 +00:00
|
|
|
sleepTime = 0;
|
2009-10-06 03:29:18 +00:00
|
|
|
writeFrames = mFrameCount;
|
2009-07-17 19:17:14 +00:00
|
|
|
} else {
|
2009-10-06 03:29:18 +00:00
|
|
|
if (sleepTime == 0) {
|
2009-11-10 07:32:22 +00:00
|
|
|
if (mixerStatus == MIXER_TRACKS_ENABLED) {
|
|
|
|
sleepTime = activeSleepTime;
|
|
|
|
} else {
|
|
|
|
sleepTime = idleSleepTime;
|
|
|
|
}
|
2009-10-06 03:29:18 +00:00
|
|
|
} else if (mBytesWritten != 0) {
|
|
|
|
// flush remaining overflow buffers in output tracks
|
|
|
|
for (size_t i = 0; i < outputTracks.size(); i++) {
|
|
|
|
if (outputTracks[i]->isActive()) {
|
|
|
|
sleepTime = 0;
|
|
|
|
writeFrames = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-22 07:35:48 +00:00
|
|
|
|
|
|
|
if (mSuspended) {
|
2009-11-10 07:32:22 +00:00
|
|
|
sleepTime = idleSleepTime;
|
2009-09-22 07:35:48 +00:00
|
|
|
}
|
|
|
|
// sleepTime == 0 means we must write to audio hardware
|
|
|
|
if (sleepTime == 0) {
|
2009-10-06 03:29:18 +00:00
|
|
|
standbyTime = systemTime() + kStandbyTimeInNsecs;
|
2009-09-22 07:35:48 +00:00
|
|
|
for (size_t i = 0; i < outputTracks.size(); i++) {
|
2009-10-06 03:29:18 +00:00
|
|
|
outputTracks[i]->write(curBuf, writeFrames);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-09-22 07:35:48 +00:00
|
|
|
mStandby = false;
|
|
|
|
mBytesWritten += mixBufferSize;
|
2009-07-17 19:17:14 +00:00
|
|
|
} else {
|
2009-09-22 07:35:48 +00:00
|
|
|
usleep(sleepTime);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// finally let go of all our tracks, without the lock held
|
|
|
|
// since we can't guarantee the destructors won't acquire that
|
|
|
|
// same lock.
|
|
|
|
tracksToRemove.clear();
|
|
|
|
outputTracks.clear();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
return false;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate();
|
|
|
|
OutputTrack *outputTrack = new OutputTrack((ThreadBase *)thread,
|
|
|
|
mSampleRate,
|
|
|
|
mFormat,
|
|
|
|
mChannelCount,
|
|
|
|
frameCount);
|
2009-08-11 06:22:32 +00:00
|
|
|
if (outputTrack->cblk() != NULL) {
|
|
|
|
thread->setStreamVolume(AudioSystem::NUM_STREAM_TYPES, 1.0f);
|
|
|
|
mOutputTracks.add(outputTrack);
|
|
|
|
LOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
for (size_t i = 0; i < mOutputTracks.size(); i++) {
|
|
|
|
if (mOutputTracks[i]->thread() == (ThreadBase *)thread) {
|
2009-08-11 06:22:32 +00:00
|
|
|
mOutputTracks[i]->destroy();
|
2009-07-17 19:17:14 +00:00
|
|
|
mOutputTracks.removeAt(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOGV("removeOutputTrack(): unkonwn thread: %p", thread);
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
// TrackBase constructor must be called with AudioFlinger::mLock held
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::ThreadBase::TrackBase::TrackBase(
|
|
|
|
const wp<ThreadBase>& thread,
|
2008-10-21 14:00:00 +00:00
|
|
|
const sp<Client>& client,
|
|
|
|
uint32_t sampleRate,
|
|
|
|
int format,
|
|
|
|
int channelCount,
|
2008-12-18 02:05:43 +00:00
|
|
|
int frameCount,
|
2009-03-04 03:31:44 +00:00
|
|
|
uint32_t flags,
|
2008-12-18 02:05:43 +00:00
|
|
|
const sp<IMemory>& sharedBuffer)
|
2008-10-21 14:00:00 +00:00
|
|
|
: RefBase(),
|
2009-07-17 19:17:14 +00:00
|
|
|
mThread(thread),
|
2008-10-21 14:00:00 +00:00
|
|
|
mClient(client),
|
2009-09-09 12:16:08 +00:00
|
|
|
mCblk(0),
|
2008-12-18 02:05:43 +00:00
|
|
|
mFrameCount(0),
|
2008-10-21 14:00:00 +00:00
|
|
|
mState(IDLE),
|
2008-12-18 02:05:43 +00:00
|
|
|
mClientTid(-1),
|
|
|
|
mFormat(format),
|
2009-03-04 03:31:44 +00:00
|
|
|
mFlags(flags & ~SYSTEM_FLAGS_MASK)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2008-12-18 02:05:43 +00:00
|
|
|
LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
// LOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
|
2008-12-18 02:05:43 +00:00
|
|
|
size_t size = sizeof(audio_track_cblk_t);
|
|
|
|
size_t bufferSize = frameCount*channelCount*sizeof(int16_t);
|
|
|
|
if (sharedBuffer == 0) {
|
|
|
|
size += bufferSize;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (client != NULL) {
|
|
|
|
mCblkMemory = client->heap()->allocate(size);
|
|
|
|
if (mCblkMemory != 0) {
|
|
|
|
mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
|
|
|
|
if (mCblk) { // construct the shared structure in-place.
|
|
|
|
new(mCblk) audio_track_cblk_t();
|
|
|
|
// clear all buffers
|
|
|
|
mCblk->frameCount = frameCount;
|
2009-07-07 14:10:45 +00:00
|
|
|
mCblk->sampleRate = sampleRate;
|
|
|
|
mCblk->channels = (uint8_t)channelCount;
|
2009-03-04 03:31:44 +00:00
|
|
|
if (sharedBuffer == 0) {
|
|
|
|
mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
|
|
|
|
memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
|
|
|
|
// Force underrun condition to avoid false underrun callback until first data is
|
|
|
|
// written to buffer
|
|
|
|
mCblk->flowControlFlag = 1;
|
|
|
|
} else {
|
|
|
|
mBuffer = sharedBuffer->pointer();
|
|
|
|
}
|
|
|
|
mBufferEnd = (uint8_t *)mBuffer + bufferSize;
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
|
|
|
LOGE("not enough memory for AudioTrack size=%u", size);
|
|
|
|
client->heap()->dump("AudioTrack");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mCblk = (audio_track_cblk_t *)(new uint8_t[size]);
|
|
|
|
if (mCblk) { // construct the shared structure in-place.
|
|
|
|
new(mCblk) audio_track_cblk_t();
|
|
|
|
// clear all buffers
|
|
|
|
mCblk->frameCount = frameCount;
|
2009-07-07 14:10:45 +00:00
|
|
|
mCblk->sampleRate = sampleRate;
|
|
|
|
mCblk->channels = (uint8_t)channelCount;
|
2009-03-04 03:31:44 +00:00
|
|
|
mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
|
|
|
|
memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
|
|
|
|
// Force underrun condition to avoid false underrun callback until first data is
|
|
|
|
// written to buffer
|
|
|
|
mCblk->flowControlFlag = 1;
|
|
|
|
mBufferEnd = (uint8_t *)mBuffer + bufferSize;
|
|
|
|
}
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
AudioFlinger::ThreadBase::TrackBase::~TrackBase()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-03-04 03:31:44 +00:00
|
|
|
if (mCblk) {
|
2009-07-17 19:17:14 +00:00
|
|
|
mCblk->~audio_track_cblk_t(); // destroy our shared-structure.
|
|
|
|
if (mClient == NULL) {
|
|
|
|
delete mCblk;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
mCblkMemory.clear(); // and free the shared memory
|
2009-09-17 12:12:56 +00:00
|
|
|
if (mClient != NULL) {
|
|
|
|
Mutex::Autolock _l(mClient->audioFlinger()->mLock);
|
|
|
|
mClient.clear();
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
buffer->raw = 0;
|
2008-12-18 02:05:43 +00:00
|
|
|
mFrameCount = buffer->frameCount;
|
2008-10-21 14:00:00 +00:00
|
|
|
step();
|
2008-12-18 02:05:43 +00:00
|
|
|
buffer->frameCount = 0;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
bool AudioFlinger::ThreadBase::TrackBase::step() {
|
2008-10-21 14:00:00 +00:00
|
|
|
bool result;
|
|
|
|
audio_track_cblk_t* cblk = this->cblk();
|
2008-12-18 02:05:43 +00:00
|
|
|
|
|
|
|
result = cblk->stepServer(mFrameCount);
|
2008-10-21 14:00:00 +00:00
|
|
|
if (!result) {
|
|
|
|
LOGV("stepServer failed acquiring cblk mutex");
|
|
|
|
mFlags |= STEPSERVER_FAILED;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
void AudioFlinger::ThreadBase::TrackBase::reset() {
|
2008-10-21 14:00:00 +00:00
|
|
|
audio_track_cblk_t* cblk = this->cblk();
|
|
|
|
|
|
|
|
cblk->user = 0;
|
|
|
|
cblk->server = 0;
|
2008-12-18 02:05:43 +00:00
|
|
|
cblk->userBase = 0;
|
|
|
|
cblk->serverBase = 0;
|
2009-03-04 03:31:44 +00:00
|
|
|
mFlags &= (uint32_t)(~SYSTEM_FLAGS_MASK);
|
2008-12-18 02:05:43 +00:00
|
|
|
LOGV("TrackBase::reset");
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
sp<IMemory> AudioFlinger::ThreadBase::TrackBase::getCblk() const
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
return mCblkMemory;
|
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
int AudioFlinger::ThreadBase::TrackBase::sampleRate() const {
|
2009-03-19 00:39:46 +00:00
|
|
|
return (int)mCblk->sampleRate;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
int AudioFlinger::ThreadBase::TrackBase::channelCount() const {
|
2009-07-07 14:10:45 +00:00
|
|
|
return (int)mCblk->channels;
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
2009-09-16 13:02:45 +00:00
|
|
|
void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
|
2008-12-18 02:05:43 +00:00
|
|
|
audio_track_cblk_t* cblk = this->cblk();
|
2009-07-17 19:17:14 +00:00
|
|
|
int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*cblk->frameSize;
|
|
|
|
int8_t *bufferEnd = bufferStart + frames * cblk->frameSize;
|
2008-12-18 02:05:43 +00:00
|
|
|
|
|
|
|
// Check validity of returned pointer in case the track control block would have been corrupted.
|
2009-07-17 19:17:14 +00:00
|
|
|
if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd ||
|
|
|
|
((unsigned long)bufferStart & (unsigned long)(cblk->frameSize - 1))) {
|
2009-03-19 00:39:46 +00:00
|
|
|
LOGE("TrackBase::getBuffer buffer out of range:\n start: %p, end %p , mBuffer %p mBufferEnd %p\n \
|
|
|
|
server %d, serverBase %d, user %d, userBase %d, channels %d",
|
2008-12-18 02:05:43 +00:00
|
|
|
bufferStart, bufferEnd, mBuffer, mBufferEnd,
|
2009-03-19 00:39:46 +00:00
|
|
|
cblk->server, cblk->serverBase, cblk->user, cblk->userBase, cblk->channels);
|
2008-12-18 02:05:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bufferStart;
|
|
|
|
}
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
|
|
|
|
AudioFlinger::PlaybackThread::Track::Track(
|
|
|
|
const wp<ThreadBase>& thread,
|
2008-10-21 14:00:00 +00:00
|
|
|
const sp<Client>& client,
|
|
|
|
int streamType,
|
|
|
|
uint32_t sampleRate,
|
|
|
|
int format,
|
|
|
|
int channelCount,
|
2008-12-18 02:05:43 +00:00
|
|
|
int frameCount,
|
|
|
|
const sp<IMemory>& sharedBuffer)
|
2009-07-17 19:17:14 +00:00
|
|
|
: TrackBase(thread, client, sampleRate, format, channelCount, frameCount, 0, sharedBuffer),
|
|
|
|
mMute(false), mSharedBuffer(sharedBuffer), mName(-1)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-09-09 12:16:08 +00:00
|
|
|
if (mCblk != NULL) {
|
|
|
|
sp<ThreadBase> baseThread = thread.promote();
|
|
|
|
if (baseThread != 0) {
|
|
|
|
PlaybackThread *playbackThread = (PlaybackThread *)baseThread.get();
|
|
|
|
mName = playbackThread->getTrackName_l();
|
|
|
|
}
|
|
|
|
LOGV("Track constructor name %d, calling thread %d", mName, IPCThreadState::self()->getCallingPid());
|
|
|
|
if (mName < 0) {
|
|
|
|
LOGE("no more track names available");
|
|
|
|
}
|
|
|
|
mVolume[0] = 1.0f;
|
|
|
|
mVolume[1] = 1.0f;
|
|
|
|
mStreamType = streamType;
|
|
|
|
// NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
|
|
|
|
// 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
|
|
|
|
mCblk->frameSize = AudioSystem::isLinearPCM(format) ? channelCount * sizeof(int16_t) : sizeof(int8_t);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::PlaybackThread::Track::~Track()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("PlaybackThread::Track destructor");
|
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
2009-12-01 10:17:41 +00:00
|
|
|
Mutex::Autolock _l(thread->mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
mState = TERMINATED;
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::destroy()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
// NOTE: destroyTrack_l() can remove a strong reference to this Track
|
2009-03-09 18:52:12 +00:00
|
|
|
// by removing it from mTracks vector, so there is a risk that this Tracks's
|
2009-07-17 19:17:14 +00:00
|
|
|
// desctructor is called. As the destructor needs to lock mLock,
|
|
|
|
// we must acquire a strong reference on this Track before locking mLock
|
2009-03-09 18:52:12 +00:00
|
|
|
// here so that the destructor is called only when exiting this function.
|
2009-07-17 19:17:14 +00:00
|
|
|
// On the other hand, as long as Track::destroy() is only called by
|
|
|
|
// TrackHandle destructor, the TrackHandle still holds a strong ref on
|
2009-03-09 18:52:12 +00:00
|
|
|
// this Track with its member mTrack.
|
|
|
|
sp<Track> keep(this);
|
2009-07-17 19:17:14 +00:00
|
|
|
{ // scope for mLock
|
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
2009-12-01 10:17:41 +00:00
|
|
|
if (!isOutputTrack()) {
|
|
|
|
if (mState == ACTIVE || mState == RESUMING) {
|
|
|
|
AudioSystem::stopOutput(thread->id(), (AudioSystem::stream_type)mStreamType);
|
|
|
|
}
|
|
|
|
AudioSystem::releaseOutput(thread->id());
|
2009-11-19 17:00:56 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(thread->mLock);
|
|
|
|
PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
|
|
|
|
playbackThread->destroyTrack_l(this);
|
|
|
|
}
|
2009-03-09 18:52:12 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-11-07 08:01:32 +00:00
|
|
|
snprintf(buffer, size, " %5d %5d %3u %3u %3u %04u %1d %1d %1d %5u %5u %5u %08x %08x\n",
|
2008-10-21 14:00:00 +00:00
|
|
|
mName - AudioMixer::TRACK0,
|
2009-03-04 03:31:44 +00:00
|
|
|
(mClient == NULL) ? getpid() : mClient->pid(),
|
2008-10-21 14:00:00 +00:00
|
|
|
mStreamType,
|
|
|
|
mFormat,
|
2008-12-18 02:05:43 +00:00
|
|
|
mCblk->channels,
|
|
|
|
mFrameCount,
|
2008-10-21 14:00:00 +00:00
|
|
|
mState,
|
|
|
|
mMute,
|
|
|
|
mFillingUpStatus,
|
|
|
|
mCblk->sampleRate,
|
|
|
|
mCblk->volume[0],
|
|
|
|
mCblk->volume[1],
|
|
|
|
mCblk->server,
|
|
|
|
mCblk->user);
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
audio_track_cblk_t* cblk = this->cblk();
|
2008-12-18 02:05:43 +00:00
|
|
|
uint32_t framesReady;
|
|
|
|
uint32_t framesReq = buffer->frameCount;
|
|
|
|
|
|
|
|
// Check if last stepServer failed, try to step now
|
2008-10-21 14:00:00 +00:00
|
|
|
if (mFlags & TrackBase::STEPSERVER_FAILED) {
|
|
|
|
if (!step()) goto getNextBuffer_exit;
|
|
|
|
LOGV("stepServer recovered");
|
|
|
|
mFlags &= ~TrackBase::STEPSERVER_FAILED;
|
|
|
|
}
|
|
|
|
|
2008-12-18 02:05:43 +00:00
|
|
|
framesReady = cblk->framesReady();
|
|
|
|
|
|
|
|
if (LIKELY(framesReady)) {
|
|
|
|
uint32_t s = cblk->server;
|
|
|
|
uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
|
|
|
|
|
|
|
|
bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd;
|
|
|
|
if (framesReq > framesReady) {
|
|
|
|
framesReq = framesReady;
|
|
|
|
}
|
|
|
|
if (s + framesReq > bufferEnd) {
|
|
|
|
framesReq = bufferEnd - s;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->raw = getBuffer(s, framesReq);
|
|
|
|
if (buffer->raw == 0) goto getNextBuffer_exit;
|
|
|
|
|
|
|
|
buffer->frameCount = framesReq;
|
|
|
|
return NO_ERROR;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
2008-12-18 02:05:43 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
getNextBuffer_exit:
|
|
|
|
buffer->raw = 0;
|
|
|
|
buffer->frameCount = 0;
|
2009-10-06 03:29:18 +00:00
|
|
|
LOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get());
|
2008-10-21 14:00:00 +00:00
|
|
|
return NOT_ENOUGH_DATA;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
bool AudioFlinger::PlaybackThread::Track::isReady() const {
|
2008-10-21 14:00:00 +00:00
|
|
|
if (mFillingUpStatus != FS_FILLING) return true;
|
2008-12-18 02:05:43 +00:00
|
|
|
|
|
|
|
if (mCblk->framesReady() >= mCblk->frameCount ||
|
|
|
|
mCblk->forceReady) {
|
2008-10-21 14:00:00 +00:00
|
|
|
mFillingUpStatus = FS_FILLED;
|
2008-12-18 02:05:43 +00:00
|
|
|
mCblk->forceReady = 0;
|
2008-10-21 14:00:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::PlaybackThread::Track::start()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-11-19 17:00:56 +00:00
|
|
|
status_t status = NO_ERROR;
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("start(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid());
|
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
Mutex::Autolock _l(thread->mLock);
|
2009-11-19 17:00:56 +00:00
|
|
|
int state = mState;
|
|
|
|
// here the track could be either new, or restarted
|
|
|
|
// in both cases "unstop" the track
|
|
|
|
if (mState == PAUSED) {
|
|
|
|
mState = TrackBase::RESUMING;
|
|
|
|
LOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
|
|
|
|
} else {
|
|
|
|
mState = TrackBase::ACTIVE;
|
|
|
|
LOGV("? => ACTIVE (%d) on thread %p", mName, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isOutputTrack() && state != ACTIVE && state != RESUMING) {
|
|
|
|
thread->mLock.unlock();
|
|
|
|
status = AudioSystem::startOutput(thread->id(), (AudioSystem::stream_type)mStreamType);
|
|
|
|
thread->mLock.lock();
|
|
|
|
}
|
|
|
|
if (status == NO_ERROR) {
|
|
|
|
PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
|
|
|
|
playbackThread->addTrack_l(this);
|
|
|
|
} else {
|
|
|
|
mState = state;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = BAD_VALUE;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-11-19 17:00:56 +00:00
|
|
|
return status;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::stop()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("stop(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid());
|
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
Mutex::Autolock _l(thread->mLock);
|
2009-11-19 17:00:56 +00:00
|
|
|
int state = mState;
|
2009-07-17 19:17:14 +00:00
|
|
|
if (mState > STOPPED) {
|
|
|
|
mState = STOPPED;
|
|
|
|
// If the track is not active (PAUSED and buffers full), flush buffers
|
|
|
|
PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
|
|
|
|
if (playbackThread->mActiveTracks.indexOf(this) < 0) {
|
|
|
|
reset();
|
|
|
|
}
|
2009-10-06 03:29:18 +00:00
|
|
|
LOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread);
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
2009-11-19 17:00:56 +00:00
|
|
|
if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) {
|
|
|
|
thread->mLock.unlock();
|
|
|
|
AudioSystem::stopOutput(thread->id(), (AudioSystem::stream_type)mStreamType);
|
|
|
|
thread->mLock.lock();
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::pause()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-03-04 03:31:44 +00:00
|
|
|
LOGV("pause(%d), calling thread %d", mName, IPCThreadState::self()->getCallingPid());
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
Mutex::Autolock _l(thread->mLock);
|
|
|
|
if (mState == ACTIVE || mState == RESUMING) {
|
|
|
|
mState = PAUSING;
|
2009-10-06 03:29:18 +00:00
|
|
|
LOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
|
2009-11-19 17:00:56 +00:00
|
|
|
if (!isOutputTrack()) {
|
|
|
|
thread->mLock.unlock();
|
|
|
|
AudioSystem::stopOutput(thread->id(), (AudioSystem::stream_type)mStreamType);
|
|
|
|
thread->mLock.lock();
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::flush()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
LOGV("flush(%d)", mName);
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
Mutex::Autolock _l(thread->mLock);
|
|
|
|
if (mState != STOPPED && mState != PAUSED && mState != PAUSING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// No point remaining in PAUSED state after a flush => go to
|
|
|
|
// STOPPED state
|
|
|
|
mState = STOPPED;
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
mCblk->lock.lock();
|
|
|
|
// NOTE: reset() will reset cblk->user and cblk->server with
|
|
|
|
// the risk that at the same time, the AudioMixer is trying to read
|
|
|
|
// data. In this case, getNextBuffer() would return a NULL pointer
|
|
|
|
// as audio buffer => the AudioMixer code MUST always test that pointer
|
|
|
|
// returned by getNextBuffer() is not NULL!
|
|
|
|
reset();
|
|
|
|
mCblk->lock.unlock();
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::reset()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2008-12-18 02:05:43 +00:00
|
|
|
// Do not reset twice to avoid discarding data written just after a flush and before
|
|
|
|
// the audioflinger thread detects the track is stopped.
|
|
|
|
if (!mResetDone) {
|
|
|
|
TrackBase::reset();
|
|
|
|
// Force underrun condition to avoid false underrun callback until first data is
|
|
|
|
// written to buffer
|
|
|
|
mCblk->flowControlFlag = 1;
|
|
|
|
mCblk->forceReady = 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
mFillingUpStatus = FS_FILLING;
|
2008-12-18 02:05:43 +00:00
|
|
|
mResetDone = true;
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::mute(bool muted)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
mMute = muted;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::Track::setVolume(float left, float right)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
|
|
|
mVolume[0] = left;
|
|
|
|
mVolume[1] = right;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
// RecordTrack constructor must be called with AudioFlinger::mLock held
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::RecordThread::RecordTrack::RecordTrack(
|
|
|
|
const wp<ThreadBase>& thread,
|
2009-03-04 03:31:44 +00:00
|
|
|
const sp<Client>& client,
|
|
|
|
uint32_t sampleRate,
|
|
|
|
int format,
|
|
|
|
int channelCount,
|
|
|
|
int frameCount,
|
|
|
|
uint32_t flags)
|
2009-07-17 19:17:14 +00:00
|
|
|
: TrackBase(thread, client, sampleRate, format,
|
2009-03-04 03:31:44 +00:00
|
|
|
channelCount, frameCount, flags, 0),
|
2009-07-17 19:17:14 +00:00
|
|
|
mOverflow(false)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-09-09 12:16:08 +00:00
|
|
|
if (mCblk != NULL) {
|
|
|
|
LOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
|
|
|
|
if (format == AudioSystem::PCM_16_BIT) {
|
|
|
|
mCblk->frameSize = channelCount * sizeof(int16_t);
|
|
|
|
} else if (format == AudioSystem::PCM_8_BIT) {
|
|
|
|
mCblk->frameSize = channelCount * sizeof(int8_t);
|
|
|
|
} else {
|
|
|
|
mCblk->frameSize = sizeof(int8_t);
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-11-19 17:00:56 +00:00
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
AudioSystem::releaseInput(thread->id());
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
audio_track_cblk_t* cblk = this->cblk();
|
|
|
|
uint32_t framesAvail;
|
|
|
|
uint32_t framesReq = buffer->frameCount;
|
|
|
|
|
|
|
|
// Check if last stepServer failed, try to step now
|
|
|
|
if (mFlags & TrackBase::STEPSERVER_FAILED) {
|
|
|
|
if (!step()) goto getNextBuffer_exit;
|
|
|
|
LOGV("stepServer recovered");
|
|
|
|
mFlags &= ~TrackBase::STEPSERVER_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
framesAvail = cblk->framesAvailable_l();
|
|
|
|
|
|
|
|
if (LIKELY(framesAvail)) {
|
|
|
|
uint32_t s = cblk->server;
|
|
|
|
uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
|
|
|
|
|
|
|
|
if (framesReq > framesAvail) {
|
|
|
|
framesReq = framesAvail;
|
|
|
|
}
|
|
|
|
if (s + framesReq > bufferEnd) {
|
|
|
|
framesReq = bufferEnd - s;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->raw = getBuffer(s, framesReq);
|
|
|
|
if (buffer->raw == 0) goto getNextBuffer_exit;
|
|
|
|
|
|
|
|
buffer->frameCount = framesReq;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNextBuffer_exit:
|
|
|
|
buffer->raw = 0;
|
|
|
|
buffer->frameCount = 0;
|
|
|
|
return NOT_ENOUGH_DATA;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::RecordThread::RecordTrack::start()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
RecordThread *recordThread = (RecordThread *)thread.get();
|
|
|
|
return recordThread->start(this);
|
2009-11-19 17:00:56 +00:00
|
|
|
} else {
|
|
|
|
return BAD_VALUE;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::RecordThread::RecordTrack::stop()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
RecordThread *recordThread = (RecordThread *)thread.get();
|
|
|
|
recordThread->stop(this);
|
|
|
|
TrackBase::reset();
|
|
|
|
// Force overerrun condition to avoid false overrun callback until first data is
|
|
|
|
// read from buffer
|
|
|
|
mCblk->flowControlFlag = 1;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 08:01:32 +00:00
|
|
|
void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size)
|
|
|
|
{
|
|
|
|
snprintf(buffer, size, " %05d %03u %03u %04u %01d %05u %08x %08x\n",
|
|
|
|
(mClient == NULL) ? getpid() : mClient->pid(),
|
|
|
|
mFormat,
|
|
|
|
mCblk->channels,
|
|
|
|
mFrameCount,
|
|
|
|
mState,
|
|
|
|
mCblk->sampleRate,
|
|
|
|
mCblk->server,
|
|
|
|
mCblk->user);
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
|
|
|
|
const wp<ThreadBase>& thread,
|
2009-03-04 03:31:44 +00:00
|
|
|
uint32_t sampleRate,
|
|
|
|
int format,
|
|
|
|
int channelCount,
|
|
|
|
int frameCount)
|
2009-07-17 19:17:14 +00:00
|
|
|
: Track(thread, NULL, AudioSystem::NUM_STREAM_TYPES, sampleRate, format, channelCount, frameCount, NULL),
|
|
|
|
mActive(false)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
PlaybackThread *playbackThread = (PlaybackThread *)thread.unsafe_get();
|
2009-08-11 06:22:32 +00:00
|
|
|
if (mCblk != NULL) {
|
|
|
|
mCblk->out = 1;
|
|
|
|
mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
|
|
|
|
mCblk->volume[0] = mCblk->volume[1] = 0x1000;
|
|
|
|
mOutBuffer.frameCount = 0;
|
|
|
|
mWaitTimeMs = (playbackThread->frameCount() * 2 * 1000) / playbackThread->sampleRate();
|
|
|
|
playbackThread->mTracks.add(this);
|
|
|
|
LOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, mCblk->frameCount %d, mCblk->sampleRate %d, mCblk->channels %d mBufferEnd %p mWaitTimeMs %d",
|
|
|
|
mCblk, mBuffer, mCblk->buffers, mCblk->frameCount, mCblk->sampleRate, mCblk->channels, mBufferEnd, mWaitTimeMs);
|
|
|
|
} else {
|
|
|
|
LOGW("Error creating output track on thread %p", playbackThread);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2009-08-11 06:22:32 +00:00
|
|
|
clearBufferQueue();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::PlaybackThread::OutputTrack::start()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
status_t status = Track::start();
|
2009-07-17 19:17:14 +00:00
|
|
|
if (status != NO_ERROR) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
mActive = true;
|
2009-03-04 03:31:44 +00:00
|
|
|
mRetryCount = 127;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::OutputTrack::stop()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
Track::stop();
|
|
|
|
clearBufferQueue();
|
|
|
|
mOutBuffer.frameCount = 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
mActive = false;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
Buffer *pInBuffer;
|
|
|
|
Buffer inBuffer;
|
|
|
|
uint32_t channels = mCblk->channels;
|
2009-07-17 19:17:14 +00:00
|
|
|
bool outputBufferFull = false;
|
2009-03-04 03:31:44 +00:00
|
|
|
inBuffer.frameCount = frames;
|
|
|
|
inBuffer.i16 = data;
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
uint32_t waitTimeLeftMs = mWaitTimeMs;
|
|
|
|
|
2009-10-06 03:29:18 +00:00
|
|
|
if (!mActive && frames != 0) {
|
2009-07-17 19:17:14 +00:00
|
|
|
start();
|
|
|
|
sp<ThreadBase> thread = mThread.promote();
|
|
|
|
if (thread != 0) {
|
|
|
|
MixerThread *mixerThread = (MixerThread *)thread.get();
|
|
|
|
if (mCblk->frameCount > frames){
|
|
|
|
if (mBufferQueue.size() < kMaxOverFlowBuffers) {
|
|
|
|
uint32_t startFrames = (mCblk->frameCount - frames);
|
|
|
|
pInBuffer = new Buffer;
|
|
|
|
pInBuffer->mBuffer = new int16_t[startFrames * channels];
|
|
|
|
pInBuffer->frameCount = startFrames;
|
|
|
|
pInBuffer->i16 = pInBuffer->mBuffer;
|
|
|
|
memset(pInBuffer->raw, 0, startFrames * channels * sizeof(int16_t));
|
|
|
|
mBufferQueue.add(pInBuffer);
|
|
|
|
} else {
|
|
|
|
LOGW ("OutputTrack::write() %p no more buffers in queue", this);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
while (waitTimeLeftMs) {
|
2009-03-04 03:31:44 +00:00
|
|
|
// First write pending buffers, then new data
|
|
|
|
if (mBufferQueue.size()) {
|
|
|
|
pInBuffer = mBufferQueue.itemAt(0);
|
|
|
|
} else {
|
|
|
|
pInBuffer = &inBuffer;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (pInBuffer->frameCount == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (mOutBuffer.frameCount == 0) {
|
|
|
|
mOutBuffer.frameCount = pInBuffer->frameCount;
|
2009-07-17 19:17:14 +00:00
|
|
|
nsecs_t startTime = systemTime();
|
|
|
|
if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)AudioTrack::NO_MORE_BUFFERS) {
|
|
|
|
LOGV ("OutputTrack::write() %p no more output buffers", this);
|
|
|
|
outputBufferFull = true;
|
2009-03-04 03:31:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
|
2009-10-06 03:29:18 +00:00
|
|
|
LOGV("OutputTrack::write() to thread %p waitTimeMs %d waitTimeLeftMs %d", mThread.unsafe_get(), waitTimeMs, waitTimeLeftMs);
|
2009-07-17 19:17:14 +00:00
|
|
|
if (waitTimeLeftMs >= waitTimeMs) {
|
|
|
|
waitTimeLeftMs -= waitTimeMs;
|
|
|
|
} else {
|
|
|
|
waitTimeLeftMs = 0;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount;
|
|
|
|
memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channels * sizeof(int16_t));
|
|
|
|
mCblk->stepUser(outFrames);
|
|
|
|
pInBuffer->frameCount -= outFrames;
|
|
|
|
pInBuffer->i16 += outFrames * channels;
|
|
|
|
mOutBuffer.frameCount -= outFrames;
|
2009-07-17 19:17:14 +00:00
|
|
|
mOutBuffer.i16 += outFrames * channels;
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (pInBuffer->frameCount == 0) {
|
|
|
|
if (mBufferQueue.size()) {
|
|
|
|
mBufferQueue.removeAt(0);
|
|
|
|
delete [] pInBuffer->mBuffer;
|
|
|
|
delete pInBuffer;
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("OutputTrack::write() %p released overflow buffer %d", this, mBufferQueue.size());
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// If we could not write all frames, allocate a buffer and queue it for next time.
|
|
|
|
if (inBuffer.frameCount) {
|
2009-07-17 19:17:14 +00:00
|
|
|
if (mBufferQueue.size() < kMaxOverFlowBuffers) {
|
2009-03-04 03:31:44 +00:00
|
|
|
pInBuffer = new Buffer;
|
|
|
|
pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channels];
|
|
|
|
pInBuffer->frameCount = inBuffer.frameCount;
|
|
|
|
pInBuffer->i16 = pInBuffer->mBuffer;
|
|
|
|
memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channels * sizeof(int16_t));
|
|
|
|
mBufferQueue.add(pInBuffer);
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("OutputTrack::write() %p adding overflow buffer %d", this, mBufferQueue.size());
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGW("OutputTrack::write() %p no more overflow buffers", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// Calling write() with a 0 length buffer, means that no more data will be written:
|
2009-07-17 19:17:14 +00:00
|
|
|
// If no more buffers are pending, fill output track buffer to make sure it is started
|
2009-03-04 03:31:44 +00:00
|
|
|
// by output mixer.
|
2009-07-17 19:17:14 +00:00
|
|
|
if (frames == 0 && mBufferQueue.size() == 0) {
|
|
|
|
if (mCblk->user < mCblk->frameCount) {
|
|
|
|
frames = mCblk->frameCount - mCblk->user;
|
|
|
|
pInBuffer = new Buffer;
|
|
|
|
pInBuffer->mBuffer = new int16_t[frames * channels];
|
|
|
|
pInBuffer->frameCount = frames;
|
|
|
|
pInBuffer->i16 = pInBuffer->mBuffer;
|
|
|
|
memset(pInBuffer->raw, 0, frames * channels * sizeof(int16_t));
|
|
|
|
mBufferQueue.add(pInBuffer);
|
2009-10-06 03:29:18 +00:00
|
|
|
} else if (mActive) {
|
2009-07-17 19:17:14 +00:00
|
|
|
stop();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
return outputBufferFull;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
int active;
|
|
|
|
status_t result;
|
|
|
|
audio_track_cblk_t* cblk = mCblk;
|
|
|
|
uint32_t framesReq = buffer->frameCount;
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// LOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server);
|
2009-03-04 03:31:44 +00:00
|
|
|
buffer->frameCount = 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
uint32_t framesAvail = cblk->framesAvailable();
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (framesAvail == 0) {
|
2009-07-17 19:17:14 +00:00
|
|
|
Mutex::Autolock _l(cblk->lock);
|
|
|
|
goto start_loop_here;
|
|
|
|
while (framesAvail == 0) {
|
|
|
|
active = mActive;
|
|
|
|
if (UNLIKELY(!active)) {
|
|
|
|
LOGV("Not active and NO_MORE_BUFFERS");
|
|
|
|
return AudioTrack::NO_MORE_BUFFERS;
|
|
|
|
}
|
|
|
|
result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
|
|
|
|
if (result != NO_ERROR) {
|
|
|
|
return AudioTrack::NO_MORE_BUFFERS;
|
|
|
|
}
|
|
|
|
// read the server count again
|
|
|
|
start_loop_here:
|
|
|
|
framesAvail = cblk->framesAvailable_l();
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
// if (framesAvail < framesReq) {
|
|
|
|
// return AudioTrack::NO_MORE_BUFFERS;
|
|
|
|
// }
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
if (framesReq > framesAvail) {
|
|
|
|
framesReq = framesAvail;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t u = cblk->user;
|
|
|
|
uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
|
|
|
|
|
|
|
|
if (u + framesReq > bufferEnd) {
|
|
|
|
framesReq = bufferEnd - u;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->frameCount = framesReq;
|
|
|
|
buffer->raw = (void *)cblk->buffer(u);
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
size_t size = mBufferQueue.size();
|
|
|
|
Buffer *pBuffer;
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
pBuffer = mBufferQueue.itemAt(i);
|
|
|
|
delete [] pBuffer->mBuffer;
|
|
|
|
delete pBuffer;
|
|
|
|
}
|
|
|
|
mBufferQueue.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
|
|
|
|
: RefBase(),
|
|
|
|
mAudioFlinger(audioFlinger),
|
|
|
|
mMemoryDealer(new MemoryDealer(1024*1024)),
|
|
|
|
mPid(pid)
|
|
|
|
{
|
|
|
|
// 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
|
|
|
|
}
|
|
|
|
|
2009-09-17 12:12:56 +00:00
|
|
|
// Client destructor must be called with AudioFlinger::mLock held
|
2009-03-04 03:31:44 +00:00
|
|
|
AudioFlinger::Client::~Client()
|
|
|
|
{
|
2009-09-17 12:12:56 +00:00
|
|
|
mAudioFlinger->removeClient_l(mPid);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const sp<MemoryDealer>& AudioFlinger::Client::heap() const
|
|
|
|
{
|
|
|
|
return mMemoryDealer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
|
2008-10-21 14:00:00 +00:00
|
|
|
: BnAudioTrack(),
|
|
|
|
mTrack(track)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioFlinger::TrackHandle::~TrackHandle() {
|
|
|
|
// just stop the track on deletion, associated resources
|
|
|
|
// will be freed from the main thread once all pending buffers have
|
|
|
|
// been played. Unless it's not in the active track list, in which
|
|
|
|
// case we free everything now...
|
|
|
|
mTrack->destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::TrackHandle::start() {
|
|
|
|
return mTrack->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::TrackHandle::stop() {
|
|
|
|
mTrack->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::TrackHandle::flush() {
|
|
|
|
mTrack->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::TrackHandle::mute(bool e) {
|
|
|
|
mTrack->mute(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::TrackHandle::pause() {
|
|
|
|
mTrack->pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::TrackHandle::setVolume(float left, float right) {
|
|
|
|
mTrack->setVolume(left, right);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
|
|
|
|
return mTrack->getCblk();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::TrackHandle::onTransact(
|
|
|
|
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
|
|
|
|
{
|
|
|
|
return BnAudioTrack::onTransact(code, data, reply, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
sp<IAudioRecord> AudioFlinger::openRecord(
|
|
|
|
pid_t pid,
|
2009-07-28 15:44:33 +00:00
|
|
|
int input,
|
2008-10-21 14:00:00 +00:00
|
|
|
uint32_t sampleRate,
|
|
|
|
int format,
|
|
|
|
int channelCount,
|
2008-12-18 02:05:43 +00:00
|
|
|
int frameCount,
|
|
|
|
uint32_t flags,
|
|
|
|
status_t *status)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<RecordThread::RecordTrack> recordTrack;
|
2008-10-21 14:00:00 +00:00
|
|
|
sp<RecordHandle> recordHandle;
|
|
|
|
sp<Client> client;
|
|
|
|
wp<Client> wclient;
|
2008-12-18 02:05:43 +00:00
|
|
|
status_t lStatus;
|
2009-07-17 19:17:14 +00:00
|
|
|
RecordThread *thread;
|
|
|
|
size_t inFrameCount;
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
// check calling permissions
|
|
|
|
if (!recordingAllowed()) {
|
2008-12-18 02:05:43 +00:00
|
|
|
lStatus = PERMISSION_DENIED;
|
2008-10-21 14:00:00 +00:00
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add client to list
|
2009-03-09 18:52:12 +00:00
|
|
|
{ // scope for mLock
|
2008-10-21 14:00:00 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
thread = checkRecordThread_l(input);
|
|
|
|
if (thread == NULL) {
|
|
|
|
lStatus = BAD_VALUE;
|
|
|
|
goto Exit;
|
|
|
|
}
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
wclient = mClients.valueFor(pid);
|
|
|
|
if (wclient != NULL) {
|
|
|
|
client = wclient.promote();
|
|
|
|
} else {
|
|
|
|
client = new Client(this, pid);
|
|
|
|
mClients.add(pid, client);
|
|
|
|
}
|
2008-12-18 02:05:43 +00:00
|
|
|
|
2009-03-09 18:52:12 +00:00
|
|
|
// create new record track. The record track uses one track in mHardwareMixerThread by convention.
|
2009-07-17 19:17:14 +00:00
|
|
|
recordTrack = new RecordThread::RecordTrack(thread, client, sampleRate,
|
2009-03-09 18:52:12 +00:00
|
|
|
format, channelCount, frameCount, flags);
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
if (recordTrack->getCblk() == NULL) {
|
2009-09-17 12:12:56 +00:00
|
|
|
// remove local strong reference to Client before deleting the RecordTrack so that the Client
|
|
|
|
// destructor is called by the TrackBase destructor with mLock held
|
|
|
|
client.clear();
|
2009-03-04 03:31:44 +00:00
|
|
|
recordTrack.clear();
|
|
|
|
lStatus = NO_MEMORY;
|
|
|
|
goto Exit;
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
// return to handle to client
|
|
|
|
recordHandle = new RecordHandle(recordTrack);
|
2008-12-18 02:05:43 +00:00
|
|
|
lStatus = NO_ERROR;
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
Exit:
|
2008-12-18 02:05:43 +00:00
|
|
|
if (status) {
|
|
|
|
*status = lStatus;
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
return recordHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
|
2008-10-21 14:00:00 +00:00
|
|
|
: BnAudioRecord(),
|
|
|
|
mRecordTrack(recordTrack)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-12-18 02:05:43 +00:00
|
|
|
AudioFlinger::RecordHandle::~RecordHandle() {
|
|
|
|
stop();
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
status_t AudioFlinger::RecordHandle::start() {
|
|
|
|
LOGV("RecordHandle::start()");
|
|
|
|
return mRecordTrack->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::RecordHandle::stop() {
|
|
|
|
LOGV("RecordHandle::stop()");
|
|
|
|
mRecordTrack->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
|
|
|
|
return mRecordTrack->getCblk();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t AudioFlinger::RecordHandle::onTransact(
|
|
|
|
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
|
|
|
|
{
|
|
|
|
return BnAudioRecord::onTransact(code, data, reply, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger, AudioStreamIn *input, uint32_t sampleRate, uint32_t channels, int id) :
|
|
|
|
ThreadBase(audioFlinger, id),
|
2009-07-17 19:17:14 +00:00
|
|
|
mInput(input), mResampler(0), mRsmpOutBuffer(0), mRsmpInBuffer(0)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
mReqChannelCount = AudioSystem::popCount(channels);
|
|
|
|
mReqSampleRate = sampleRate;
|
|
|
|
readInputParameters();
|
|
|
|
sendConfigEvent(AudioSystem::INPUT_OPENED);
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
AudioFlinger::RecordThread::~RecordThread()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
delete[] mRsmpInBuffer;
|
|
|
|
if (mResampler != 0) {
|
|
|
|
delete mResampler;
|
|
|
|
delete[] mRsmpOutBuffer;
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::RecordThread::onFirstRef()
|
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
|
|
|
|
snprintf(buffer, SIZE, "Record Thread %p", this);
|
|
|
|
|
|
|
|
run(buffer, PRIORITY_URGENT_AUDIO);
|
|
|
|
}
|
2009-11-19 17:00:56 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
bool AudioFlinger::RecordThread::threadLoop()
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2008-12-18 02:05:43 +00:00
|
|
|
AudioBufferProvider::Buffer buffer;
|
2009-07-17 19:17:14 +00:00
|
|
|
sp<RecordTrack> activeTrack;
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
// start recording
|
|
|
|
while (!exitPending()) {
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
processConfigEvents();
|
|
|
|
|
|
|
|
{ // scope for mLock
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
checkForNewParameters_l();
|
|
|
|
if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
|
|
|
|
if (!mStandby) {
|
|
|
|
mInput->standby();
|
|
|
|
mStandby = true;
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (exitPending()) break;
|
|
|
|
|
|
|
|
LOGV("RecordThread: loop stopping");
|
|
|
|
// go to sleep
|
2008-10-21 14:00:00 +00:00
|
|
|
mWaitWorkCV.wait(mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("RecordThread: loop starting");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mActiveTrack != 0) {
|
|
|
|
if (mActiveTrack->mState == TrackBase::PAUSING) {
|
2009-12-05 13:20:01 +00:00
|
|
|
if (!mStandby) {
|
|
|
|
mInput->standby();
|
|
|
|
mStandby = true;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
mActiveTrack.clear();
|
|
|
|
mStartStopCond.broadcast();
|
|
|
|
} else if (mActiveTrack->mState == TrackBase::RESUMING) {
|
|
|
|
if (mReqChannelCount != mActiveTrack->channelCount()) {
|
|
|
|
mActiveTrack.clear();
|
2009-12-05 13:20:01 +00:00
|
|
|
mStartStopCond.broadcast();
|
|
|
|
} else if (mBytesRead != 0) {
|
|
|
|
// record start succeeds only if first read from audio input
|
|
|
|
// succeeds
|
|
|
|
if (mBytesRead > 0) {
|
|
|
|
mActiveTrack->mState = TrackBase::ACTIVE;
|
|
|
|
} else {
|
|
|
|
mActiveTrack.clear();
|
|
|
|
}
|
|
|
|
mStartStopCond.broadcast();
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
2009-12-05 13:20:01 +00:00
|
|
|
mStandby = false;
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mActiveTrack != 0) {
|
2009-12-05 13:20:01 +00:00
|
|
|
if (mActiveTrack->mState != TrackBase::ACTIVE &&
|
|
|
|
mActiveTrack->mState != TrackBase::RESUMING) {
|
2009-11-19 17:00:56 +00:00
|
|
|
usleep(5000);
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
buffer.frameCount = mFrameCount;
|
|
|
|
if (LIKELY(mActiveTrack->getNextBuffer(&buffer) == NO_ERROR)) {
|
|
|
|
size_t framesOut = buffer.frameCount;
|
|
|
|
if (mResampler == 0) {
|
|
|
|
// no resampling
|
|
|
|
while (framesOut) {
|
|
|
|
size_t framesIn = mFrameCount - mRsmpInIndex;
|
|
|
|
if (framesIn) {
|
|
|
|
int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
|
|
|
|
int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize;
|
|
|
|
if (framesIn > framesOut)
|
|
|
|
framesIn = framesOut;
|
|
|
|
mRsmpInIndex += framesIn;
|
|
|
|
framesOut -= framesIn;
|
|
|
|
if (mChannelCount == mReqChannelCount ||
|
|
|
|
mFormat != AudioSystem::PCM_16_BIT) {
|
|
|
|
memcpy(dst, src, framesIn * mFrameSize);
|
|
|
|
} else {
|
|
|
|
int16_t *src16 = (int16_t *)src;
|
|
|
|
int16_t *dst16 = (int16_t *)dst;
|
|
|
|
if (mChannelCount == 1) {
|
|
|
|
while (framesIn--) {
|
|
|
|
*dst16++ = *src16;
|
|
|
|
*dst16++ = *src16++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (framesIn--) {
|
|
|
|
*dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
|
|
|
|
src16 += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (framesOut && mFrameCount == mRsmpInIndex) {
|
|
|
|
if (framesOut == mFrameCount &&
|
|
|
|
(mChannelCount == mReqChannelCount || mFormat != AudioSystem::PCM_16_BIT)) {
|
2009-12-05 13:20:01 +00:00
|
|
|
mBytesRead = mInput->read(buffer.raw, mInputBytes);
|
2009-07-17 19:17:14 +00:00
|
|
|
framesOut = 0;
|
|
|
|
} else {
|
2009-12-05 13:20:01 +00:00
|
|
|
mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
|
2009-07-17 19:17:14 +00:00
|
|
|
mRsmpInIndex = 0;
|
|
|
|
}
|
2009-12-05 13:20:01 +00:00
|
|
|
if (mBytesRead < 0) {
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGE("Error reading audio input");
|
2009-12-05 13:20:01 +00:00
|
|
|
if (mActiveTrack->mState == TrackBase::ACTIVE) {
|
|
|
|
sleep(1);
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
mRsmpInIndex = mFrameCount;
|
|
|
|
framesOut = 0;
|
|
|
|
buffer.frameCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// resampling
|
|
|
|
|
|
|
|
memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t));
|
|
|
|
// alter output frame count as if we were expecting stereo samples
|
|
|
|
if (mChannelCount == 1 && mReqChannelCount == 1) {
|
|
|
|
framesOut >>= 1;
|
|
|
|
}
|
|
|
|
mResampler->resample(mRsmpOutBuffer, framesOut, this);
|
|
|
|
// ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer()
|
|
|
|
// are 32 bit aligned which should be always true.
|
|
|
|
if (mChannelCount == 2 && mReqChannelCount == 1) {
|
|
|
|
AudioMixer::ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut);
|
|
|
|
// the resampler always outputs stereo samples: do post stereo to mono conversion
|
|
|
|
int16_t *src = (int16_t *)mRsmpOutBuffer;
|
|
|
|
int16_t *dst = buffer.i16;
|
|
|
|
while (framesOut--) {
|
|
|
|
*dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1);
|
|
|
|
src += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AudioMixer::ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut);
|
|
|
|
}
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
mActiveTrack->releaseBuffer(&buffer);
|
|
|
|
mActiveTrack->overflow();
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
// client isn't retrieving buffers fast enough
|
|
|
|
else {
|
2009-07-17 19:17:14 +00:00
|
|
|
if (!mActiveTrack->setOverflow())
|
|
|
|
LOGW("RecordThread: buffer overflow");
|
2008-12-18 02:05:43 +00:00
|
|
|
// Release the processor for a while before asking for a new buffer.
|
|
|
|
// This will give the application more chance to read from the buffer and
|
|
|
|
// clear the overflow.
|
|
|
|
usleep(5000);
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (!mStandby) {
|
|
|
|
mInput->standby();
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
mActiveTrack.clear();
|
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
mStartStopCond.broadcast();
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("RecordThread %p exiting", this);
|
2008-12-18 02:05:43 +00:00
|
|
|
return false;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack)
|
2008-10-21 14:00:00 +00:00
|
|
|
{
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("RecordThread::start");
|
2009-11-19 17:00:56 +00:00
|
|
|
sp <ThreadBase> strongMe = this;
|
|
|
|
status_t status = NO_ERROR;
|
|
|
|
{
|
|
|
|
AutoMutex lock(&mLock);
|
|
|
|
if (mActiveTrack != 0) {
|
|
|
|
if (recordTrack != mActiveTrack.get()) {
|
|
|
|
status = -EBUSY;
|
|
|
|
} else if (mActiveTrack->mState == TrackBase::PAUSING) {
|
2009-12-05 13:20:01 +00:00
|
|
|
mActiveTrack->mState = TrackBase::ACTIVE;
|
2009-11-19 17:00:56 +00:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
recordTrack->mState = TrackBase::IDLE;
|
|
|
|
mActiveTrack = recordTrack;
|
|
|
|
mLock.unlock();
|
|
|
|
status_t status = AudioSystem::startInput(mId);
|
|
|
|
mLock.lock();
|
|
|
|
if (status != NO_ERROR) {
|
|
|
|
mActiveTrack.clear();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
mActiveTrack->mState = TrackBase::RESUMING;
|
2009-12-05 13:20:01 +00:00
|
|
|
mRsmpInIndex = mFrameCount;
|
|
|
|
mBytesRead = 0;
|
2009-11-19 17:00:56 +00:00
|
|
|
// signal thread to start
|
|
|
|
LOGV("Signal record thread");
|
|
|
|
mWaitWorkCV.signal();
|
|
|
|
// do not wait for mStartStopCond if exiting
|
|
|
|
if (mExiting) {
|
|
|
|
mActiveTrack.clear();
|
|
|
|
status = INVALID_OPERATION;
|
|
|
|
goto startError;
|
|
|
|
}
|
|
|
|
mStartStopCond.wait(mLock);
|
|
|
|
if (mActiveTrack == 0) {
|
|
|
|
LOGV("Record failed to start");
|
|
|
|
status = BAD_VALUE;
|
|
|
|
goto startError;
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGV("Record started OK");
|
2009-11-19 17:00:56 +00:00
|
|
|
return status;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
2009-11-19 17:00:56 +00:00
|
|
|
startError:
|
|
|
|
AudioSystem::stopInput(mId);
|
|
|
|
return status;
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
|
|
|
|
LOGV("RecordThread::stop");
|
2009-11-19 17:00:56 +00:00
|
|
|
sp <ThreadBase> strongMe = this;
|
|
|
|
{
|
|
|
|
AutoMutex lock(&mLock);
|
|
|
|
if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) {
|
|
|
|
mActiveTrack->mState = TrackBase::PAUSING;
|
|
|
|
// do not wait for mStartStopCond if exiting
|
|
|
|
if (mExiting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mStartStopCond.wait(mLock);
|
|
|
|
// if we have been restarted, recordTrack == mActiveTrack.get() here
|
|
|
|
if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) {
|
|
|
|
mLock.unlock();
|
|
|
|
AudioSystem::stopInput(mId);
|
|
|
|
mLock.lock();
|
2009-12-05 13:20:01 +00:00
|
|
|
LOGV("Record stopped OK");
|
2009-11-19 17:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-18 02:05:43 +00:00
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
String8 result;
|
|
|
|
pid_t pid = 0;
|
|
|
|
|
2009-11-07 08:01:32 +00:00
|
|
|
snprintf(buffer, SIZE, "\nInput thread %p internals\n", this);
|
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
if (mActiveTrack != 0) {
|
|
|
|
result.append("Active Track:\n");
|
|
|
|
result.append(" Clien Fmt Chn Buf S SRate Serv User\n");
|
|
|
|
mActiveTrack->dump(buffer, SIZE);
|
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "In size: %d\n", mInputBytes);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != 0));
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount);
|
|
|
|
result.append(buffer);
|
|
|
|
snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate);
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append(buffer);
|
2009-11-07 08:01:32 +00:00
|
|
|
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
|
|
|
result.append("No record client\n");
|
|
|
|
}
|
|
|
|
write(fd, result.string(), result.size());
|
2009-11-07 08:01:32 +00:00
|
|
|
|
|
|
|
dumpBase(fd, args);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer)
|
|
|
|
{
|
|
|
|
size_t framesReq = buffer->frameCount;
|
|
|
|
size_t framesReady = mFrameCount - mRsmpInIndex;
|
|
|
|
int channelCount;
|
|
|
|
|
|
|
|
if (framesReady == 0) {
|
2009-12-05 13:20:01 +00:00
|
|
|
mBytesRead = mInput->read(mRsmpInBuffer, mInputBytes);
|
|
|
|
if (mBytesRead < 0) {
|
2009-07-17 19:17:14 +00:00
|
|
|
LOGE("RecordThread::getNextBuffer() Error reading audio input");
|
2009-12-05 13:20:01 +00:00
|
|
|
if (mActiveTrack->mState == TrackBase::ACTIVE) {
|
|
|
|
sleep(1);
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
buffer->raw = 0;
|
|
|
|
buffer->frameCount = 0;
|
|
|
|
return NOT_ENOUGH_DATA;
|
|
|
|
}
|
|
|
|
mRsmpInIndex = 0;
|
|
|
|
framesReady = mFrameCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (framesReq > framesReady) {
|
|
|
|
framesReq = framesReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mChannelCount == 1 && mReqChannelCount == 2) {
|
|
|
|
channelCount = 1;
|
|
|
|
} else {
|
|
|
|
channelCount = 2;
|
|
|
|
}
|
|
|
|
buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount;
|
|
|
|
buffer->frameCount = framesReq;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer)
|
|
|
|
{
|
|
|
|
mRsmpInIndex += buffer->frameCount;
|
|
|
|
buffer->frameCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AudioFlinger::RecordThread::checkForNewParameters_l()
|
|
|
|
{
|
|
|
|
bool reconfig = false;
|
|
|
|
|
2009-08-04 16:45:33 +00:00
|
|
|
while (!mNewParameters.isEmpty()) {
|
2009-07-17 19:17:14 +00:00
|
|
|
status_t status = NO_ERROR;
|
2009-08-04 16:45:33 +00:00
|
|
|
String8 keyValuePair = mNewParameters[0];
|
|
|
|
AudioParameter param = AudioParameter(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
int value;
|
|
|
|
int reqFormat = mFormat;
|
|
|
|
int reqSamplingRate = mReqSampleRate;
|
|
|
|
int reqChannelCount = mReqChannelCount;
|
2009-08-04 16:45:33 +00:00
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
|
|
|
|
reqSamplingRate = value;
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
|
|
|
|
reqFormat = value;
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
|
|
|
|
reqChannelCount = AudioSystem::popCount(value);
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
|
|
|
|
// do not accept frame count changes if tracks are open as the track buffer
|
|
|
|
// size depends on frame count and correct behavior would not be garantied
|
|
|
|
// if frame count is changed after track creation
|
|
|
|
if (mActiveTrack != 0) {
|
|
|
|
status = INVALID_OPERATION;
|
|
|
|
} else {
|
|
|
|
reconfig = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status == NO_ERROR) {
|
2009-08-04 16:45:33 +00:00
|
|
|
status = mInput->setParameters(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
if (status == INVALID_OPERATION) {
|
|
|
|
mInput->standby();
|
2009-08-04 16:45:33 +00:00
|
|
|
status = mInput->setParameters(keyValuePair);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
if (reconfig) {
|
|
|
|
if (status == BAD_VALUE &&
|
|
|
|
reqFormat == mInput->format() && reqFormat == AudioSystem::PCM_16_BIT &&
|
|
|
|
((int)mInput->sampleRate() <= 2 * reqSamplingRate) &&
|
|
|
|
(AudioSystem::popCount(mInput->channels()) < 3) && (reqChannelCount < 3)) {
|
|
|
|
status = NO_ERROR;
|
|
|
|
}
|
|
|
|
if (status == NO_ERROR) {
|
|
|
|
readInputParameters();
|
2009-08-04 16:45:33 +00:00
|
|
|
sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-07 08:01:32 +00:00
|
|
|
|
|
|
|
mNewParameters.removeAt(0);
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
mParamStatus = status;
|
|
|
|
mParamCond.signal();
|
2009-08-04 16:45:33 +00:00
|
|
|
mWaitWorkCV.wait(mLock);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
return reconfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
|
|
|
|
{
|
|
|
|
return mInput->getParameters(keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::RecordThread::audioConfigChanged(int event, int param) {
|
|
|
|
AudioSystem::OutputDescriptor desc;
|
|
|
|
void *param2 = 0;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case AudioSystem::INPUT_OPENED:
|
|
|
|
case AudioSystem::INPUT_CONFIG_CHANGED:
|
|
|
|
desc.channels = mChannelCount;
|
|
|
|
desc.samplingRate = mSampleRate;
|
|
|
|
desc.format = mFormat;
|
|
|
|
desc.frameCount = mFrameCount;
|
|
|
|
desc.latency = 0;
|
|
|
|
param2 = &desc;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AudioSystem::INPUT_CLOSED:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-09-15 14:10:12 +00:00
|
|
|
Mutex::Autolock _l(mAudioFlinger->mLock);
|
2009-11-19 17:00:56 +00:00
|
|
|
mAudioFlinger->audioConfigChanged_l(event, mId, param2);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioFlinger::RecordThread::readInputParameters()
|
|
|
|
{
|
|
|
|
if (mRsmpInBuffer) delete mRsmpInBuffer;
|
|
|
|
if (mRsmpOutBuffer) delete mRsmpOutBuffer;
|
|
|
|
if (mResampler) delete mResampler;
|
|
|
|
mResampler = 0;
|
|
|
|
|
|
|
|
mSampleRate = mInput->sampleRate();
|
|
|
|
mChannelCount = AudioSystem::popCount(mInput->channels());
|
|
|
|
mFormat = mInput->format();
|
|
|
|
mFrameSize = mInput->frameSize();
|
|
|
|
mInputBytes = mInput->bufferSize();
|
|
|
|
mFrameCount = mInputBytes / mFrameSize;
|
|
|
|
mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
|
|
|
|
|
|
|
|
if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3)
|
|
|
|
{
|
|
|
|
int channelCount;
|
|
|
|
// optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid
|
|
|
|
// stereo to mono post process as the resampler always outputs stereo.
|
|
|
|
if (mChannelCount == 1 && mReqChannelCount == 2) {
|
|
|
|
channelCount = 1;
|
|
|
|
} else {
|
|
|
|
channelCount = 2;
|
|
|
|
}
|
|
|
|
mResampler = AudioResampler::create(16, channelCount, mReqSampleRate);
|
|
|
|
mResampler->setSampleRate(mSampleRate);
|
|
|
|
mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
|
|
|
|
mRsmpOutBuffer = new int32_t[mFrameCount * 2];
|
|
|
|
|
|
|
|
// optmization: if mono to mono, alter input frame count as if we were inputing stereo samples
|
|
|
|
if (mChannelCount == 1 && mReqChannelCount == 1) {
|
|
|
|
mFrameCount >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
mRsmpInIndex = mFrameCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
int AudioFlinger::openOutput(uint32_t *pDevices,
|
2009-07-17 19:17:14 +00:00
|
|
|
uint32_t *pSamplingRate,
|
|
|
|
uint32_t *pFormat,
|
|
|
|
uint32_t *pChannels,
|
|
|
|
uint32_t *pLatencyMs,
|
|
|
|
uint32_t flags)
|
|
|
|
{
|
|
|
|
status_t status;
|
|
|
|
PlaybackThread *thread = NULL;
|
|
|
|
mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
|
|
|
|
uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
|
|
|
|
uint32_t format = pFormat ? *pFormat : 0;
|
|
|
|
uint32_t channels = pChannels ? *pChannels : 0;
|
|
|
|
uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
|
|
|
|
|
|
|
|
LOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
|
|
|
|
pDevices ? *pDevices : 0,
|
|
|
|
samplingRate,
|
|
|
|
format,
|
|
|
|
channels,
|
|
|
|
flags);
|
|
|
|
|
|
|
|
if (pDevices == NULL || *pDevices == 0) {
|
2009-07-28 15:44:33 +00:00
|
|
|
return 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
|
|
|
AudioStreamOut *output = mAudioHardware->openOutputStream(*pDevices,
|
|
|
|
(int *)&format,
|
|
|
|
&channels,
|
|
|
|
&samplingRate,
|
|
|
|
&status);
|
|
|
|
LOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
|
|
|
|
output,
|
|
|
|
samplingRate,
|
|
|
|
format,
|
|
|
|
channels,
|
|
|
|
status);
|
|
|
|
|
|
|
|
mHardwareStatus = AUDIO_HW_IDLE;
|
|
|
|
if (output != 0) {
|
|
|
|
if ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
|
|
|
|
(format != AudioSystem::PCM_16_BIT) ||
|
|
|
|
(channels != AudioSystem::CHANNEL_OUT_STEREO)) {
|
2009-11-19 17:00:56 +00:00
|
|
|
thread = new DirectOutputThread(this, output, ++mNextThreadId);
|
|
|
|
LOGV("openOutput() created direct output: ID %d thread %p", mNextThreadId, thread);
|
2009-07-17 19:17:14 +00:00
|
|
|
} else {
|
2009-11-19 17:00:56 +00:00
|
|
|
thread = new MixerThread(this, output, ++mNextThreadId);
|
|
|
|
LOGV("openOutput() created mixer output: ID %d thread %p", mNextThreadId, thread);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
2009-11-19 17:00:56 +00:00
|
|
|
mPlaybackThreads.add(mNextThreadId, thread);
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
if (pSamplingRate) *pSamplingRate = samplingRate;
|
|
|
|
if (pFormat) *pFormat = format;
|
|
|
|
if (pChannels) *pChannels = channels;
|
|
|
|
if (pLatencyMs) *pLatencyMs = thread->latency();
|
2009-12-05 13:20:01 +00:00
|
|
|
|
|
|
|
return mNextThreadId;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2009-12-05 13:20:01 +00:00
|
|
|
return 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
int AudioFlinger::openDuplicateOutput(int output1, int output2)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
2009-07-28 15:44:33 +00:00
|
|
|
MixerThread *thread1 = checkMixerThread_l(output1);
|
|
|
|
MixerThread *thread2 = checkMixerThread_l(output2);
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
if (thread1 == NULL || thread2 == NULL) {
|
|
|
|
LOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
|
|
|
|
return 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
|
2009-11-19 17:00:56 +00:00
|
|
|
DuplicatingThread *thread = new DuplicatingThread(this, thread1, ++mNextThreadId);
|
2009-07-28 15:44:33 +00:00
|
|
|
thread->addOutputTrack(thread2);
|
2009-11-19 17:00:56 +00:00
|
|
|
mPlaybackThreads.add(mNextThreadId, thread);
|
2009-07-28 15:44:33 +00:00
|
|
|
return mNextThreadId;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
status_t AudioFlinger::closeOutput(int output)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
2009-08-04 15:37:05 +00:00
|
|
|
// keep strong reference on the playback thread so that
|
|
|
|
// it is not destroyed while exit() is executed
|
|
|
|
sp <PlaybackThread> thread;
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread == NULL) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGV("closeOutput() %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
if (thread->type() == PlaybackThread::MIXER) {
|
|
|
|
for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
if (mPlaybackThreads.valueAt(i)->type() == PlaybackThread::DUPLICATING) {
|
|
|
|
DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
|
2009-08-04 15:37:05 +00:00
|
|
|
dupThread->removeOutputTrack((MixerThread *)thread.get());
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-15 14:10:12 +00:00
|
|
|
void *param2 = 0;
|
2009-11-19 17:00:56 +00:00
|
|
|
audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, param2);
|
2009-07-28 15:44:33 +00:00
|
|
|
mPlaybackThreads.removeItem(output);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
thread->exit();
|
|
|
|
|
2009-08-04 15:37:05 +00:00
|
|
|
if (thread->type() != PlaybackThread::DUPLICATING) {
|
|
|
|
mAudioHardware->closeOutputStream(thread->getOutput());
|
|
|
|
}
|
2009-07-17 19:17:14 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
status_t AudioFlinger::suspendOutput(int output)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
|
|
|
|
if (thread == NULL) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGV("suspendOutput() %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
thread->suspend();
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
status_t AudioFlinger::restoreOutput(int output)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
|
|
|
|
if (thread == NULL) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGV("restoreOutput() %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
thread->restore();
|
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
int AudioFlinger::openInput(uint32_t *pDevices,
|
2009-07-17 19:17:14 +00:00
|
|
|
uint32_t *pSamplingRate,
|
|
|
|
uint32_t *pFormat,
|
|
|
|
uint32_t *pChannels,
|
|
|
|
uint32_t acoustics)
|
|
|
|
{
|
|
|
|
status_t status;
|
|
|
|
RecordThread *thread = NULL;
|
|
|
|
uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
|
|
|
|
uint32_t format = pFormat ? *pFormat : 0;
|
|
|
|
uint32_t channels = pChannels ? *pChannels : 0;
|
|
|
|
uint32_t reqSamplingRate = samplingRate;
|
|
|
|
uint32_t reqFormat = format;
|
|
|
|
uint32_t reqChannels = channels;
|
|
|
|
|
|
|
|
if (pDevices == NULL || *pDevices == 0) {
|
2009-07-28 15:44:33 +00:00
|
|
|
return 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
|
|
|
AudioStreamIn *input = mAudioHardware->openInputStream(*pDevices,
|
|
|
|
(int *)&format,
|
|
|
|
&channels,
|
|
|
|
&samplingRate,
|
|
|
|
&status,
|
|
|
|
(AudioSystem::audio_in_acoustics)acoustics);
|
|
|
|
LOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d",
|
|
|
|
input,
|
|
|
|
samplingRate,
|
|
|
|
format,
|
|
|
|
channels,
|
|
|
|
acoustics,
|
|
|
|
status);
|
|
|
|
|
|
|
|
// If the input could not be opened with the requested parameters and we can handle the conversion internally,
|
|
|
|
// try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
|
|
|
|
// or stereo to mono conversions on 16 bit PCM inputs.
|
|
|
|
if (input == 0 && status == BAD_VALUE &&
|
|
|
|
reqFormat == format && format == AudioSystem::PCM_16_BIT &&
|
|
|
|
(samplingRate <= 2 * reqSamplingRate) &&
|
|
|
|
(AudioSystem::popCount(channels) < 3) && (AudioSystem::popCount(reqChannels) < 3)) {
|
|
|
|
LOGV("openInput() reopening with proposed sampling rate and channels");
|
|
|
|
input = mAudioHardware->openInputStream(*pDevices,
|
|
|
|
(int *)&format,
|
|
|
|
&channels,
|
|
|
|
&samplingRate,
|
|
|
|
&status,
|
|
|
|
(AudioSystem::audio_in_acoustics)acoustics);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input != 0) {
|
|
|
|
// Start record thread
|
2009-11-19 17:00:56 +00:00
|
|
|
thread = new RecordThread(this, input, reqSamplingRate, reqChannels, ++mNextThreadId);
|
|
|
|
mRecordThreads.add(mNextThreadId, thread);
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGV("openInput() created record thread: ID %d thread %p", mNextThreadId, thread);
|
2009-07-17 19:17:14 +00:00
|
|
|
if (pSamplingRate) *pSamplingRate = reqSamplingRate;
|
|
|
|
if (pFormat) *pFormat = format;
|
|
|
|
if (pChannels) *pChannels = reqChannels;
|
|
|
|
|
|
|
|
input->standby();
|
2009-12-05 13:20:01 +00:00
|
|
|
|
|
|
|
return mNextThreadId;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2009-12-05 13:20:01 +00:00
|
|
|
return 0;
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
status_t AudioFlinger::closeInput(int input)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
2009-08-04 15:37:05 +00:00
|
|
|
// keep strong reference on the record thread so that
|
|
|
|
// it is not destroyed while exit() is executed
|
|
|
|
sp <RecordThread> thread;
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
thread = checkRecordThread_l(input);
|
|
|
|
if (thread == NULL) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGV("closeInput() %d", input);
|
2009-09-15 14:10:12 +00:00
|
|
|
void *param2 = 0;
|
2009-11-19 17:00:56 +00:00
|
|
|
audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, param2);
|
2009-07-28 15:44:33 +00:00
|
|
|
mRecordThreads.removeItem(input);
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
thread->exit();
|
|
|
|
|
2009-08-04 15:37:05 +00:00
|
|
|
mAudioHardware->closeInputStream(thread->getInput());
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
status_t AudioFlinger::setStreamOutput(uint32_t stream, int output)
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
MixerThread *dstThread = checkMixerThread_l(output);
|
|
|
|
if (dstThread == NULL) {
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGW("setStreamOutput() bad output id %d", output);
|
2009-07-17 19:17:14 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2009-07-28 15:44:33 +00:00
|
|
|
LOGV("setStreamOutput() stream %d to output %d", stream, output);
|
2009-07-17 19:17:14 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
|
2009-07-28 15:44:33 +00:00
|
|
|
PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
|
2009-07-17 19:17:14 +00:00
|
|
|
if (thread != dstThread &&
|
|
|
|
thread->type() != PlaybackThread::DIRECT) {
|
|
|
|
MixerThread *srcThread = (MixerThread *)thread;
|
|
|
|
SortedVector < sp<MixerThread::Track> > tracks;
|
|
|
|
SortedVector < wp<MixerThread::Track> > activeTracks;
|
|
|
|
srcThread->getTracks(tracks, activeTracks, stream);
|
|
|
|
if (tracks.size()) {
|
|
|
|
dstThread->putTracks(tracks, activeTracks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-01 12:56:26 +00:00
|
|
|
dstThread->sendConfigEvent(AudioSystem::STREAM_CONFIG_CHANGED, stream);
|
|
|
|
|
2009-07-17 19:17:14 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
|
2009-07-28 15:44:33 +00:00
|
|
|
AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(int output) const
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
PlaybackThread *thread = NULL;
|
2009-07-28 15:44:33 +00:00
|
|
|
if (mPlaybackThreads.indexOfKey(output) >= 0) {
|
|
|
|
thread = (PlaybackThread *)mPlaybackThreads.valueFor(output).get();
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkMixerThread_l() must be called with AudioFlinger::mLock held
|
2009-07-28 15:44:33 +00:00
|
|
|
AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(int output) const
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
PlaybackThread *thread = checkPlaybackThread_l(output);
|
|
|
|
if (thread != NULL) {
|
|
|
|
if (thread->type() == PlaybackThread::DIRECT) {
|
|
|
|
thread = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (MixerThread *)thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkRecordThread_l() must be called with AudioFlinger::mLock held
|
2009-07-28 15:44:33 +00:00
|
|
|
AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(int input) const
|
2009-07-17 19:17:14 +00:00
|
|
|
{
|
|
|
|
RecordThread *thread = NULL;
|
2009-07-28 15:44:33 +00:00
|
|
|
if (mRecordThreads.indexOfKey(input) >= 0) {
|
|
|
|
thread = (RecordThread *)mRecordThreads.valueFor(input).get();
|
2009-07-17 19:17:14 +00:00
|
|
|
}
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
status_t AudioFlinger::onTransact(
|
|
|
|
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
|
|
|
|
{
|
|
|
|
return BnAudioFlinger::onTransact(code, data, reply, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2009-07-17 19:17:14 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
void AudioFlinger::instantiate() {
|
|
|
|
defaultServiceManager()->addService(
|
|
|
|
String16("media.audio_flinger"), new AudioFlinger());
|
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|