Concurrent playback behavior when bit-perfect client is active.
1. Mute system sound on bit-perfect output thread. System sound such as
touch sound can be triggered often. To provide better bit-perfect
playback experience, mute system sound on bit-perfect output thread.
2. Mute bit-perfect track when there are other clients playing. When
there are other clients, such as notification, playing, the
bit-perfect client can no longer be played bit-perfectly. As the
bit-perfect client can turn out to be noise if it cannot be played
bit-perfectly, mute bit-perfect client when there are other clients
playing.
3. Do not allow creating bit-perfect client if there is any other high
priority use case, such as ringtone, alarm, active. The reason is
that the high priority clients will always take the audio focus for
the playback and there is no need for those clients to be played over
bit-perfect path.
Bug: 339515899
Test: atest audiopolicy_tests
Test: Manually
Change-Id: I8483d08085fd4076fa64ed4de278e45e6f6c0af0
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index d5d778f..46c85af 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -4915,6 +4915,23 @@
return mPatchPanel->getAudioMixPort_l(devicePort, mixPort);
}
+status_t AudioFlinger::setTracksInternalMute(
+ const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) {
+ audio_utils::lock_guard _l(mutex());
+ ALOGV("%s", __func__);
+
+ std::map<audio_port_handle_t, bool> tracksInternalMuteMap;
+ for (const auto& trackInternalMute : tracksInternalMute) {
+ audio_port_handle_t portId = VALUE_OR_RETURN_STATUS(
+ aidl2legacy_int32_t_audio_port_handle_t(trackInternalMute.portId));
+ tracksInternalMuteMap.emplace(portId, trackInternalMute.muted);
+ }
+ for (size_t i = 0; i < mPlaybackThreads.size() && !tracksInternalMuteMap.empty(); i++) {
+ mPlaybackThreads.valueAt(i)->setTracksInternalMute(&tracksInternalMuteMap);
+ }
+ return NO_ERROR;
+}
+
// ----------------------------------------------------------------------------
status_t AudioFlinger::onTransactWrapper(TransactionCode code,
@@ -4949,6 +4966,7 @@
case TransactionCode::INVALIDATE_TRACKS:
case TransactionCode::GET_AUDIO_POLICY_CONFIG:
case TransactionCode::GET_AUDIO_MIX_PORT:
+ case TransactionCode::SET_TRACKS_INTERNAL_MUTE:
ALOGW("%s: transaction %d received from PID %d",
__func__, static_cast<int>(code), IPCThreadState::self()->getCallingPid());
// return status only for non void methods
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index 4e46bea..0adfc8c 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -259,6 +259,10 @@
status_t getAudioMixPort(const struct audio_port_v7* devicePort,
struct audio_port_v7* mixPort) const final EXCLUDES_AudioFlinger_Mutex;
+ status_t setTracksInternalMute(
+ const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) final
+ EXCLUDES_AudioFlinger_Mutex;
+
status_t onTransactWrapper(TransactionCode code, const Parcel& data, uint32_t flags,
const std::function<status_t()>& delegate) final EXCLUDES_AudioFlinger_Mutex;
diff --git a/services/audioflinger/IAfThread.h b/services/audioflinger/IAfThread.h
index 4b6ab89..204cc63 100644
--- a/services/audioflinger/IAfThread.h
+++ b/services/audioflinger/IAfThread.h
@@ -536,6 +536,9 @@
virtual const std::atomic<int64_t>& framesWritten() const = 0;
virtual bool usesHwAvSync() const = 0;
+
+ virtual void setTracksInternalMute(std::map<audio_port_handle_t, bool>* tracksInternalMute)
+ EXCLUDES_ThreadBase_Mutex = 0;
};
class IAfDirectOutputThread : public virtual IAfPlaybackThread {
diff --git a/services/audioflinger/IAfTrack.h b/services/audioflinger/IAfTrack.h
index a0d20ac..168bcca 100644
--- a/services/audioflinger/IAfTrack.h
+++ b/services/audioflinger/IAfTrack.h
@@ -427,6 +427,10 @@
virtual FillingStatus& fillingStatus() = 0;
virtual int8_t& retryCount() = 0;
virtual FastTrackUnderruns& fastTrackUnderruns() = 0;
+
+ // Internal mute, this is currently only used for bit-perfect playback
+ virtual bool getInternalMute() const = 0;
+ virtual void setInternalMute(bool muted) = 0;
};
// playback track, used by DuplicatingThread
diff --git a/services/audioflinger/PlaybackTracks.h b/services/audioflinger/PlaybackTracks.h
index 89ee5c0..1e1b6d2 100644
--- a/services/audioflinger/PlaybackTracks.h
+++ b/services/audioflinger/PlaybackTracks.h
@@ -220,6 +220,8 @@
*/
void processMuteEvent_l(const sp<IAudioManager>& audioManager, mute_state_t muteState) final;
+ bool getInternalMute() const final { return mInternalMute; }
+ void setInternalMute(bool muted) final { mInternalMute = muted; }
protected:
DISALLOW_COPY_AND_ASSIGN(Track);
@@ -401,6 +403,8 @@
// access these two variables only when holding player thread lock.
std::unique_ptr<os::PersistableBundle> mMuteEventExtras;
mute_state_t mMuteState;
+
+ bool mInternalMute = false;
}; // end of Track
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 7c248dc..2a1b7dc 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -33,6 +33,7 @@
#include <afutils/Vibrator.h>
#include <audio_utils/MelProcessor.h>
#include <audio_utils/Metadata.h>
+#include <com_android_media_audioserver.h>
#ifdef DEBUG_CPU_USAGE
#include <audio_utils/Statistics.h>
#include <cpustats/ThreadCpuUsage.h>
@@ -5771,6 +5772,11 @@
vlf *= volume;
vrf *= volume;
+ if (track->getInternalMute()) {
+ vlf = 0.f;
+ vrf = 0.f;
+ }
+
track->setFinalVolume(vlf, vrf);
++fastTracks;
} else {
@@ -5970,6 +5976,11 @@
vaf = v * sendLevel * (1. / MAX_GAIN_INT);
}
+ if (track->getInternalMute()) {
+ vrf = 0.f;
+ vlf = 0.f;
+ }
+
track->setFinalVolume(vlf, vrf);
// Delegate volume control to effect in track effect chain if needed
@@ -11353,14 +11364,15 @@
// If there is only one active track and it is bit-perfect, enable tee buffer.
float volumeLeft = 1.0f;
float volumeRight = 1.0f;
- if (mActiveTracks.size() == 1 && mActiveTracks[0]->isBitPerfect()) {
- const int trackId = mActiveTracks[0]->id();
+ if (sp<IAfTrack> bitPerfectTrack = getTrackToStreamBitPerfectly_l();
+ bitPerfectTrack != nullptr) {
+ const int trackId = bitPerfectTrack->id();
mAudioMixer->setParameter(
trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER, (void *)mSinkBuffer);
mAudioMixer->setParameter(
trackId, AudioMixer::TRACK, AudioMixer::TEE_BUFFER_FRAME_COUNT,
(void *)(uintptr_t)mNormalFrameCount);
- mActiveTracks[0]->getFinalVolume(&volumeLeft, &volumeRight);
+ bitPerfectTrack->getFinalVolume(&volumeLeft, &volumeRight);
mIsBitPerfect = true;
} else {
mIsBitPerfect = false;
@@ -11385,4 +11397,39 @@
mHasDataCopiedToSinkBuffer = mIsBitPerfect;
}
+void BitPerfectThread::setTracksInternalMute(
+ std::map<audio_port_handle_t, bool>* tracksInternalMute) {
+ for (auto& track : mTracks) {
+ if (auto it = tracksInternalMute->find(track->portId()); it != tracksInternalMute->end()) {
+ track->setInternalMute(it->second);
+ tracksInternalMute->erase(it);
+ }
+ }
+}
+
+sp<IAfTrack> BitPerfectThread::getTrackToStreamBitPerfectly_l() {
+ if (com::android::media::audioserver::
+ fix_concurrent_playback_behavior_with_bit_perfect_client()) {
+ sp<IAfTrack> bitPerfectTrack = nullptr;
+ bool allOtherTracksMuted = true;
+ // Return the bit perfect track if all other tracks are muted
+ for (const auto& track : mActiveTracks) {
+ if (track->isBitPerfect()) {
+ bitPerfectTrack = track;
+ } else if (track->getFinalVolume() != 0.f) {
+ allOtherTracksMuted = false;
+ if (bitPerfectTrack != nullptr) {
+ break;
+ }
+ }
+ }
+ return allOtherTracksMuted ? bitPerfectTrack : nullptr;
+ } else {
+ if (mActiveTracks.size() == 1 && mActiveTracks[0]->isBitPerfect()) {
+ return mActiveTracks[0];
+ }
+ }
+ return nullptr;
+}
+
} // namespace android
diff --git a/services/audioflinger/Threads.h b/services/audioflinger/Threads.h
index 86e1894..7e9bef1 100644
--- a/services/audioflinger/Threads.h
+++ b/services/audioflinger/Threads.h
@@ -1200,6 +1200,11 @@
}
return mHalStarted;
}
+
+ void setTracksInternalMute(std::map<audio_port_handle_t, bool>* /* tracksInternalMute */)
+ override EXCLUDES_ThreadBase_Mutex {
+ // Do nothing. It is only used for bit perfect thread
+ }
protected:
// updated by readOutputParameters_l()
size_t mNormalFrameCount; // normal mixer and effects
@@ -2449,12 +2454,17 @@
BitPerfectThread(const sp<IAfThreadCallback>& afThreadCallback, AudioStreamOut *output,
audio_io_handle_t id, bool systemReady);
+ void setTracksInternalMute(std::map<audio_port_handle_t, bool>* tracksInternalMuted)
+ final EXCLUDES_ThreadBase_Mutex;
+
protected:
mixer_state prepareTracks_l(Vector<sp<IAfTrack>>* tracksToRemove) final
REQUIRES(mutex(), ThreadBase_ThreadLoop);
void threadLoop_mix() final REQUIRES(ThreadBase_ThreadLoop);
private:
+ sp<IAfTrack> getTrackToStreamBitPerfectly_l() REQUIRES(mutex());
+
// These variables are only accessed on the threadLoop; hence need no mutex.
bool mIsBitPerfect GUARDED_BY(ThreadBase_ThreadLoop) = false;
float mVolumeLeft GUARDED_BY(ThreadBase_ThreadLoop) = 0.f;
diff --git a/services/audioflinger/Tracks.cpp b/services/audioflinger/Tracks.cpp
index c40f795..f8cfc12 100644
--- a/services/audioflinger/Tracks.cpp
+++ b/services/audioflinger/Tracks.cpp
@@ -913,7 +913,7 @@
" Format Chn mask SRate "
"ST Usg CT "
" G db L dB R dB VS dB "
- " Server FrmCnt FrmRdy F Underruns Flushed BitPerfect"
+ " Server FrmCnt FrmRdy F Underruns Flushed BitPerfect InternalMute"
"%s\n",
isServerLatencySupported() ? " Latency" : "");
}
@@ -999,7 +999,7 @@
"%08X %08X %6u "
"%2u %3x %2x "
"%5.2g %5.2g %5.2g %5.2g%c "
- "%08X %6zu%c %6zu %c %9u%c %7u %10s",
+ "%08X %6zu%c %6zu %c %9u%c %7u %10s %12s",
active ? "yes" : "no",
(mClient == 0) ? getpid() : mClient->pid(),
mSessionId,
@@ -1029,7 +1029,8 @@
mAudioTrackServerProxy->getUnderrunFrames(),
nowInUnderrun,
(unsigned)mAudioTrackServerProxy->framesFlushed() % 10000000,
- isBitPerfect() ? "true" : "false"
+ isBitPerfect() ? "true" : "false",
+ getInternalMute() ? "true" : "false"
);
if (isServerLatencySupported()) {