audioflinger: Fix reference counting protocol in OpPlayAudioMonitor
OpPlayAudioMonitor was constructing a weak pointer to itself
in the constructor. This practice can lead to crashes due to
race conditions vs object destruction. This code is now moved
to onFirstRef method which is called when at least one strong
reference exists.
This change also reduces the number of created OpPlayAudioMonitor
objects by using a factory method.
Bug: 130038586
Test: enable / disable DND mode
Change-Id: I22e63a883ebaa25b9c96e79271bb9693b5ed75cd
diff --git a/services/audioflinger/PlaybackTracks.h b/services/audioflinger/PlaybackTracks.h
index 4fd72a7..56be433 100644
--- a/services/audioflinger/PlaybackTracks.h
+++ b/services/audioflinger/PlaybackTracks.h
@@ -22,11 +22,16 @@
// Checks and monitors OP_PLAY_AUDIO
class OpPlayAudioMonitor : public RefBase {
public:
- OpPlayAudioMonitor(uid_t uid, audio_usage_t usage, int id, audio_stream_type_t streamType);
~OpPlayAudioMonitor() override;
bool hasOpPlayAudio() const;
+ static sp<OpPlayAudioMonitor> createIfNeeded(
+ uid_t uid, audio_usage_t usage, int id, audio_stream_type_t streamType);
+
private:
+ OpPlayAudioMonitor(uid_t uid, audio_usage_t usage, int id);
+ void onFirstRef() override;
+
AppOpsManager mAppOpsManager;
class PlayAudioOpCallback : public BnAppOpsCallback {
@@ -209,7 +214,9 @@
int fastIndex() const { return mFastIndex; }
- bool isPlaybackRestricted() const { return !mOpPlayAudioMonitor->hasOpPlayAudio(); }
+ bool isPlaybackRestricted() const {
+ // The monitor is only created for tracks that can be silenced.
+ return mOpPlayAudioMonitor ? !mOpPlayAudioMonitor->hasOpPlayAudio() : false; }
protected: