Add thread safe annotation in audio flinger effects
Bug: 315995877
Test: atest AudioTrackTest AudioRecordTest audioeffect_tests
Change-Id: I90406c48660aef25ab4402a70a2a45deb6651335
diff --git a/services/audioflinger/Effects.cpp b/services/audioflinger/Effects.cpp
index a348b42..a02657e 100644
--- a/services/audioflinger/Effects.cpp
+++ b/services/audioflinger/Effects.cpp
@@ -570,10 +570,10 @@
: EffectBase(callback, desc, id, sessionId, pinned),
// clear mConfig to ensure consistent initial value of buffer framecount
// in case buffers are associated by setInBuffer() or setOutBuffer()
- // prior to configure().
+ // prior to configure_l().
mConfig{{}, {}},
mStatus(NO_INIT),
- mMaxDisableWaitCnt(1), // set by configure(), should be >= 1
+ mMaxDisableWaitCnt(1), // set by configure_l(), should be >= 1
mDisableWaitCnt(0), // set by process() and updateState()
mOffloaded(false),
mIsOutput(false)
@@ -588,13 +588,13 @@
if (mStatus != NO_ERROR) {
return;
}
- lStatus = init();
+ lStatus = init_l();
if (lStatus < 0) {
mStatus = lStatus;
goto Error;
}
- setOffloaded(callback->isOffload(), callback->io());
+ setOffloaded_l(callback->isOffload(), callback->io());
ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface.get());
return;
@@ -616,7 +616,7 @@
}
-bool EffectModule::updateState() {
+bool EffectModule::updateState_l() {
audio_utils::lock_guard _l(mutex());
bool started = false;
@@ -632,7 +632,7 @@
0,
mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
}
- if (start_l() == NO_ERROR) {
+ if (start_ll() == NO_ERROR) {
mState = ACTIVE;
started = true;
} else {
@@ -641,8 +641,8 @@
break;
case STOPPING:
// volume control for offload and direct threads must take effect immediately.
- if (stop_l() == NO_ERROR
- && !(isVolumeControl() && isOffloadedOrDirect())) {
+ if (stop_ll() == NO_ERROR
+ && !(isVolumeControl() && isOffloadedOrDirect_l())) {
mDisableWaitCnt = mMaxDisableWaitCnt;
} else {
mDisableWaitCnt = 1; // will cause immediate transition to IDLE
@@ -836,9 +836,9 @@
mEffectInterface->command(EFFECT_CMD_RESET, 0, NULL, &replySize, &reply);
}
-status_t EffectModule::configure()
+status_t EffectModule::configure_l()
{
- ALOGVV("configure() started");
+ ALOGVV("%s started", __func__);
status_t status;
uint32_t size;
audio_channel_mask_t channelMask;
@@ -879,7 +879,7 @@
mConfig.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
// Don't use sample rate for thread if effect isn't offloadable.
- if (callback->isOffloadOrDirect() && !isOffloaded()) {
+ if (callback->isOffloadOrDirect() && !isOffloaded_l()) {
mConfig.inputCfg.samplingRate = DEFAULT_OUTPUT_SAMPLE_RATE;
ALOGV("Overriding effect input as 48kHz");
} else {
@@ -909,9 +909,9 @@
mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
mIsOutput = callback->isOutput();
- ALOGV("configure() %p chain %p buffer %p framecount %zu",
- this, callback->chain().promote().get(),
- mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
+ ALOGV("%s %p chain %p buffer %p framecount %zu", __func__, this,
+ callback->chain().promote().get(), mConfig.inputCfg.buffer.raw,
+ mConfig.inputCfg.buffer.frameCount);
status_t cmdStatus;
size = sizeof(int);
@@ -1012,11 +1012,11 @@
exit:
// TODO: consider clearing mConfig on error.
mStatus = status;
- ALOGVV("configure ended");
+ ALOGVV("%s ended", __func__);
return status;
}
-status_t EffectModule::init()
+status_t EffectModule::init_l()
{
audio_utils::lock_guard _l(mutex());
if (mEffectInterface == 0) {
@@ -1048,21 +1048,21 @@
}
}
-// start() must be called with PlaybackThread::mutex() or EffectChain::mutex() held
-status_t EffectModule::start()
+// start_l() must be called with EffectChain::mutex() held
+status_t EffectModule::start_l()
{
status_t status;
{
audio_utils::lock_guard _l(mutex());
- status = start_l();
+ status = start_ll();
}
if (status == NO_ERROR) {
- getCallback()->resetVolume();
+ getCallback()->resetVolume_l();
}
return status;
}
-status_t EffectModule::start_l()
+status_t EffectModule::start_ll()
{
if (mEffectInterface == 0) {
return NO_INIT;
@@ -1086,13 +1086,13 @@
return status;
}
-status_t EffectModule::stop()
+status_t EffectModule::stop_l()
{
audio_utils::lock_guard _l(mutex());
- return stop_l();
+ return stop_ll();
}
-status_t EffectModule::stop_l()
+status_t EffectModule::stop_ll()
{
if (mEffectInterface == 0) {
return NO_INIT;
@@ -1103,11 +1103,11 @@
status_t cmdStatus = NO_ERROR;
uint32_t size = sizeof(status_t);
- if (isVolumeControl() && isOffloadedOrDirect()) {
+ if (isVolumeControl() && isOffloadedOrDirect_l()) {
// We have the EffectChain and EffectModule lock, permit a reentrant call to setVolume:
// resetVolume_l --> setVolume_l --> EffectModule::setVolume
mSetVolumeReentrantTid = gettid();
- getCallback()->resetVolume();
+ getCallback()->resetVolume_l();
mSetVolumeReentrantTid = INVALID_PID;
}
@@ -1162,7 +1162,7 @@
std::vector<uint8_t>* reply)
{
audio_utils::lock_guard _l(mutex());
- ALOGVV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface.get());
+ ALOGVV("%s, cmdCode: %d, mEffectInterface: %p", __func__, cmdCode, mEffectInterface.get());
if (mState == DESTROYED || mEffectInterface == 0) {
return NO_INIT;
@@ -1258,20 +1258,20 @@
}
}
-bool EffectModule::isOffloadedOrDirect() const
+bool EffectModule::isOffloadedOrDirect_l() const
{
return getCallback()->isOffloadOrDirect();
}
-bool EffectModule::isVolumeControlEnabled() const
+bool EffectModule::isVolumeControlEnabled_l() const
{
- return (isVolumeControl() && (isOffloadedOrDirect() ? isEnabled() : isProcessEnabled()));
+ return (isVolumeControl() && (isOffloadedOrDirect_l() ? isEnabled() : isProcessEnabled()));
}
void EffectModule::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
ALOGVV("setInBuffer %p",(&buffer));
- // mConfig.inputCfg.buffer.frameCount may be zero if configure() is not called yet.
+ // mConfig.inputCfg.buffer.frameCount may be zero if configure_l() is not called yet.
if (buffer != 0) {
mConfig.inputCfg.buffer.raw = buffer->audioBuffer()->raw;
buffer->setFrameCount(mConfig.inputCfg.buffer.frameCount);
@@ -1317,7 +1317,7 @@
void EffectModule::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
ALOGVV("setOutBuffer %p",(&buffer));
- // mConfig.outputCfg.buffer.frameCount may be zero if configure() is not called yet.
+ // mConfig.outputCfg.buffer.frameCount may be zero if configure_l() is not called yet.
if (buffer != 0) {
mConfig.outputCfg.buffer.raw = buffer->audioBuffer()->raw;
buffer->setFrameCount(mConfig.outputCfg.buffer.frameCount);
@@ -1479,7 +1479,7 @@
return status;
}
-status_t EffectModule::setOffloaded(bool offloaded, audio_io_handle_t io)
+status_t EffectModule::setOffloaded_l(bool offloaded, audio_io_handle_t io)
{
audio_utils::lock_guard _l(mutex());
if (mStatus != NO_ERROR) {
@@ -1508,11 +1508,11 @@
}
mOffloaded = false;
}
- ALOGV("setOffloaded() offloaded %d io %d status %d", offloaded, io, status);
+ ALOGV("%s offloaded %d io %d status %d", __func__, offloaded, io, status);
return status;
}
-bool EffectModule::isOffloaded() const
+bool EffectModule::isOffloaded_l() const
{
audio_utils::lock_guard _l(mutex());
return mOffloaded;
@@ -1536,8 +1536,7 @@
return IAfEffectModule::isSpatializer(&mDescriptor.type);
}
-status_t EffectModule::setHapticIntensity(int id, os::HapticScale intensity)
-{
+status_t EffectModule::setHapticIntensity_l(int id, os::HapticScale intensity) {
if (mStatus != NO_ERROR) {
return mStatus;
}
@@ -1562,8 +1561,7 @@
return status;
}
-status_t EffectModule::setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo)
-{
+status_t EffectModule::setVibratorInfo_l(const media::AudioVibratorInfo& vibratorInfo) {
if (mStatus != NO_ERROR) {
return mStatus;
}
@@ -1592,8 +1590,8 @@
return status;
}
-status_t EffectModule::getConfigs(
- audio_config_base_t* inputCfg, audio_config_base_t* outputCfg, bool* isOutput) const {
+status_t EffectModule::getConfigs_l(audio_config_base_t* inputCfg, audio_config_base_t* outputCfg,
+ bool* isOutput) const {
audio_utils::lock_guard _l(mutex());
if (mConfig.inputCfg.mask == 0 || mConfig.outputCfg.mask == 0) {
return NO_INIT;
@@ -1608,7 +1606,7 @@
return NO_ERROR;
}
-status_t EffectModule::sendMetadata(const std::vector<playback_track_metadata_v7_t>& metadata) {
+status_t EffectModule::sendMetadata_ll(const std::vector<playback_track_metadata_v7_t>& metadata) {
if (mStatus != NO_ERROR) {
return mStatus;
}
@@ -1947,7 +1945,7 @@
audio_config_base_t inputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
audio_config_base_t outputCfg = AUDIO_CONFIG_BASE_INITIALIZER;
bool isOutput;
- status_t status = effectModule->getConfigs(&inputCfg, &outputCfg, &isOutput);
+ status_t status = effectModule->getConfigs_l(&inputCfg, &outputCfg, &isOutput);
if (status == NO_ERROR) {
constexpr bool isInput = false; // effects always use 'OUT' channel masks.
_config->inputCfg = VALUE_OR_RETURN_STATUS_AS_OUT(
@@ -2213,7 +2211,7 @@
return 0;
}
-std::vector<int> EffectChain::getEffectIds() const
+std::vector<int> EffectChain::getEffectIds_l() const
{
std::vector<int> ids;
audio_utils::lock_guard _l(mutex());
@@ -2243,8 +2241,7 @@
}
// Must be called with EffectChain::mutex() locked
-void EffectChain::process_l()
-{
+void EffectChain::process_l() {
// never process effects when:
// - on an OFFLOAD thread
// - no more tracks are on the session and the effect tail has been rendered
@@ -2287,7 +2284,7 @@
}
bool doResetVolume = false;
for (size_t i = 0; i < size; i++) {
- doResetVolume = mEffects[i]->updateState() || doResetVolume;
+ doResetVolume = mEffects[i]->updateState_l() || doResetVolume;
}
if (doResetVolume) {
resetVolume_l();
@@ -2341,14 +2338,14 @@
numSamples * sizeof(float), &halBuffer);
if (result != OK) return result;
- effect->configure();
+ effect->configure_l();
effect->setInBuffer(halBuffer);
// auxiliary effects output samples to chain input buffer for further processing
// by insert effects
effect->setOutBuffer(mInBuffer);
} else {
- ssize_t idx_insert = getInsertIndex(desc);
+ ssize_t idx_insert = getInsertIndex_ll(desc);
if (idx_insert < 0) {
return INVALID_OPERATION;
}
@@ -2356,7 +2353,7 @@
size_t previousSize = mEffects.size();
mEffects.insertAt(effect, idx_insert);
- effect->configure();
+ effect->configure_l();
// - By default:
// All effects read samples from chain input buffer.
@@ -2371,9 +2368,9 @@
effect->setOutBuffer(mOutBuffer);
if (idx_insert == 0) {
if (previousSize != 0) {
- mEffects[1]->configure();
+ mEffects[1]->configure_l();
mEffects[1]->setInBuffer(mOutBuffer);
- mEffects[1]->updateAccessMode(); // reconfig if neeeded.
+ mEffects[1]->updateAccessMode_l(); // reconfig if needed.
}
effect->setInBuffer(mInBuffer);
} else {
@@ -2383,9 +2380,9 @@
effect->setInBuffer(mInBuffer);
if (idx_insert == static_cast<ssize_t>(previousSize)) {
if (idx_insert != 0) {
- mEffects[idx_insert-1]->configure();
+ mEffects[idx_insert-1]->configure_l();
mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
- mEffects[idx_insert - 1]->updateAccessMode(); // reconfig if neeeded.
+ mEffects[idx_insert - 1]->updateAccessMode_l(); // reconfig if needed.
}
effect->setOutBuffer(mOutBuffer);
} else {
@@ -2395,21 +2392,21 @@
ALOGV("%s effect %p, added in chain %p at rank %zu",
__func__, effect.get(), this, idx_insert);
}
- effect->configure();
+ effect->configure_l();
return NO_ERROR;
}
std::optional<size_t> EffectChain::findVolumeControl_l(size_t from, size_t to) const {
for (size_t i = std::min(to, mEffects.size()); i > from; i--) {
- if (mEffects[i - 1]->isVolumeControlEnabled()) {
+ if (mEffects[i - 1]->isVolumeControlEnabled_l()) {
return i - 1;
}
}
return std::nullopt;
}
-ssize_t EffectChain::getInsertIndex(const effect_descriptor_t& desc) {
+ssize_t EffectChain::getInsertIndex_ll(const effect_descriptor_t& desc) {
// Insert effects are inserted at the end of mEffects vector as they are processed
// after track and auxiliary effects.
// Insert effect order as a function of indicated preference:
@@ -2497,7 +2494,7 @@
// the middle of a read from audio HAL
if (mEffects[i]->state() == EffectModule::ACTIVE ||
mEffects[i]->state() == EffectModule::STOPPING) {
- mEffects[i]->stop();
+ mEffects[i]->stop_l();
}
if (release) {
mEffects[i]->release_l();
@@ -2505,9 +2502,9 @@
if (type != EFFECT_FLAG_TYPE_AUXILIARY) {
if (i == size - 1 && i != 0) {
- mEffects[i - 1]->configure();
+ mEffects[i - 1]->configure_l();
mEffects[i - 1]->setOutBuffer(mOutBuffer);
- mEffects[i - 1]->updateAccessMode(); // reconfig if neeeded.
+ mEffects[i - 1]->updateAccessMode_l(); // reconfig if needed.
}
}
mEffects.removeAt(i);
@@ -2516,9 +2513,9 @@
// is updated if needed (can switch from HAL channel mask to mixer channel mask)
if (type != EFFECT_FLAG_TYPE_AUXILIARY // TODO(b/284522658) breaks for aux FX, why?
&& i == 0 && size > 1) {
- mEffects[0]->configure();
+ mEffects[0]->configure_l();
mEffects[0]->setInBuffer(mInBuffer);
- mEffects[0]->updateAccessMode(); // reconfig if neeeded.
+ mEffects[0]->updateAccessMode_l(); // reconfig if needed.
}
ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %zu", effect.get(),
@@ -2568,7 +2565,7 @@
bool EffectChain::hasVolumeControlEnabled_l() const {
for (const auto &effect : mEffects) {
- if (effect->isVolumeControlEnabled()) return true;
+ if (effect->isVolumeControlEnabled_l()) return true;
}
return false;
}
@@ -2681,11 +2678,11 @@
{
audio_utils::lock_guard _l(mutex());
for (size_t i = 0; i < mEffects.size(); ++i) {
- mEffects[i]->setHapticIntensity(id, intensity);
+ mEffects[i]->setHapticIntensity_l(id, intensity);
}
}
-void EffectChain::syncHalEffectsState()
+void EffectChain::syncHalEffectsState_l()
{
audio_utils::lock_guard _l(mutex());
for (size_t i = 0; i < mEffects.size(); i++) {
@@ -2754,7 +2751,7 @@
}
if (desc->mRefCount++ == 0) {
- sp<IAfEffectModule> effect = getEffectIfEnabled(type);
+ sp<IAfEffectModule> effect = getEffectIfEnabled_l(type);
if (effect != 0) {
desc->mEffect = effect;
effect->setSuspended(true);
@@ -2807,7 +2804,7 @@
}
if (desc->mRefCount++ == 0) {
Vector< sp<IAfEffectModule> > effects;
- getSuspendEligibleEffects(effects);
+ getSuspendEligibleEffects_l(effects);
for (size_t i = 0; i < effects.size(); i++) {
setEffectSuspended_l(&effects[i]->desc().type, true);
}
@@ -2848,8 +2845,7 @@
#endif //OPENSL_ES_H_
/* static */
-bool EffectChain::isEffectEligibleForBtNrecSuspend(const effect_uuid_t *type)
-{
+bool EffectChain::isEffectEligibleForBtNrecSuspend_l(const effect_uuid_t* type) {
// Only NS and AEC are suspended when BtNRec is off
if ((memcmp(type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) ||
(memcmp(type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
@@ -2858,7 +2854,7 @@
return false;
}
-bool EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
+bool EffectChain::isEffectEligibleForSuspend_l(const effect_descriptor_t& desc)
{
// auxiliary effects and visualizer are never suspended on output mix
if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
@@ -2871,26 +2867,24 @@
return true;
}
-void EffectChain::getSuspendEligibleEffects(
+void EffectChain::getSuspendEligibleEffects_l(
Vector< sp<IAfEffectModule> > &effects)
{
effects.clear();
for (size_t i = 0; i < mEffects.size(); i++) {
- if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
+ if (isEffectEligibleForSuspend_l(mEffects[i]->desc())) {
effects.add(mEffects[i]);
}
}
}
-sp<IAfEffectModule> EffectChain::getEffectIfEnabled(const effect_uuid_t *type)
+sp<IAfEffectModule> EffectChain::getEffectIfEnabled_l(const effect_uuid_t *type)
{
sp<IAfEffectModule> effect = getEffectFromType_l(type);
return effect != 0 && effect->isEnabled() ? effect : 0;
}
-void EffectChain::checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect,
- bool enabled)
-{
+void EffectChain::checkSuspendOnEffectEnabled_l(const sp<IAfEffectModule>& effect, bool enabled) {
ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
if (enabled) {
if (index < 0) {
@@ -2899,18 +2893,17 @@
if (index < 0) {
return;
}
- if (!isEffectEligibleForSuspend(effect->desc())) {
+ if (!isEffectEligibleForSuspend_l(effect->desc())) {
return;
}
setEffectSuspended_l(&effect->desc().type, enabled);
index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
if (index < 0) {
- ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
+ ALOGW("%s Fx should be suspended here!", __func__);
return;
}
}
- ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
- effect->desc().type.timeLow);
+ ALOGV("%s enable suspending fx %08x", __func__, effect->desc().type.timeLow);
sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
// if effect is requested to suspended but was not yet enabled, suspend it now.
if (desc->mEffect == 0) {
@@ -2922,8 +2915,7 @@
if (index < 0) {
return;
}
- ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
- effect->desc().type.timeLow);
+ ALOGV("%s disable restoring fx %08x", __func__, effect->desc().type.timeLow);
sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
desc->mEffect.clear();
effect->setSuspended(false);
@@ -3032,9 +3024,9 @@
for (const auto& effect : mEffects) {
if (spatializedMetadata.has_value()
&& IAfEffectModule::isSpatializer(&effect->desc().type)) {
- effect->sendMetadata(spatializedMetadata.value());
+ effect->sendMetadata_ll(spatializedMetadata.value());
} else {
- effect->sendMetadata(allMetadata);
+ effect->sendMetadata_ll(allMetadata);
}
}
}
@@ -3252,8 +3244,9 @@
t->setVolumeForOutput_l(left, right);
}
-void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(
- const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) {
+void EffectChain::EffectCallback::checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect,
+ bool enabled, bool threadLocked)
+ NO_THREAD_SAFETY_ANALYSIS {
const sp<IAfThreadBase> t = thread().promote();
if (t == nullptr) {
return;
@@ -3265,7 +3258,7 @@
return;
}
// in EffectChain context, an EffectBase is always from an EffectModule so static cast is safe
- c->checkSuspendOnEffectEnabled(effect->asEffectModule(), enabled);
+ c->checkSuspendOnEffectEnabled_l(effect->asEffectModule(), enabled);
}
void EffectChain::EffectCallback::onEffectEnable(const sp<IAfEffectBase>& effect) {
@@ -3297,7 +3290,7 @@
return true;
}
-void EffectChain::EffectCallback::resetVolume() NO_THREAD_SAFETY_ANALYSIS {
+void EffectChain::EffectCallback::resetVolume_l() {
sp<IAfEffectChain> c = chain().promote();
if (c == nullptr) {
return;
@@ -3358,7 +3351,7 @@
return status;
}
-status_t DeviceEffectProxy::init(
+status_t DeviceEffectProxy::init_l(
const std::map <audio_patch_handle_t, IAfPatchPanel::Patch>& patches) {
//For all audio patches
//If src or sink device match
@@ -3462,7 +3455,7 @@
} else {
mHalEffect->setDevices({mDevice});
}
- mHalEffect->configure();
+ mHalEffect->configure_l();
}
*handle = new EffectHandle(mHalEffect, nullptr, nullptr, 0 /*priority*/,
mNotifyFramesProcessed);
@@ -3751,7 +3744,7 @@
if (effect == nullptr) {
return;
}
- effect->start();
+ effect->start_l();
}
void DeviceEffectProxy::ProxyCallback::onEffectDisable(
@@ -3760,7 +3753,7 @@
if (effect == nullptr) {
return;
}
- effect->stop();
+ effect->stop_l();
}
} // namespace android