Skip to content

Commit

Permalink
Bug 1851803 - Introduce SourceMediaTrack::mDirectDisabledMode. r=karlt
Browse files Browse the repository at this point in the history
Similar to MediaTrack::mDisabledMode, but this is for uses on the
SourceMediaTrack producer thread. It is still signaled via a control message
from the control thread to maintain order of operations, and is protected by the
SourceMediaTrack mutex.

Differential Revision: https://phabricator.services.mozilla.com/D187554
  • Loading branch information
Pehrsons authored and Richard Pospesel committed Oct 23, 2023
1 parent bc7d695 commit 3263856
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
56 changes: 35 additions & 21 deletions dom/media/MediaTrackGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ class GraphKey final {
nsTHashMap<nsGenericHashKey<GraphKey>, MediaTrackGraphImpl*> gGraphs;
} // anonymous namespace

static void ApplyTrackDisabling(DisabledTrackMode aDisabledMode,
MediaSegment* aSegment,
MediaSegment* aRawSegment) {
if (aDisabledMode == DisabledTrackMode::ENABLED) {
return;
}
if (aDisabledMode == DisabledTrackMode::SILENCE_BLACK) {
aSegment->ReplaceWithDisabled();
if (aRawSegment) {
aRawSegment->ReplaceWithDisabled();
}
} else if (aDisabledMode == DisabledTrackMode::SILENCE_FREEZE) {
aSegment->ReplaceWithNull();
if (aRawSegment) {
aRawSegment->ReplaceWithNull();
}
} else {
MOZ_CRASH("Unsupported mode");
}
}

MediaTrackGraphImpl::~MediaTrackGraphImpl() {
MOZ_ASSERT(mTracks.IsEmpty() && mSuspendedTracks.IsEmpty(),
"All tracks should have been destroyed by messages from the main "
Expand Down Expand Up @@ -2421,6 +2442,7 @@ RefPtr<GenericPromise> MediaTrack::RemoveListener(

void MediaTrack::AddDirectListenerImpl(
already_AddRefed<DirectMediaTrackListener> aListener) {
MOZ_ASSERT(mGraph->OnGraphThread());
// Base implementation, for tracks that don't support direct track listeners.
RefPtr<DirectMediaTrackListener> listener = aListener;
listener->NotifyDirectListenerInstalled(
Expand Down Expand Up @@ -2503,6 +2525,7 @@ void MediaTrack::RunAfterPendingUpdates(
}

void MediaTrack::SetDisabledTrackModeImpl(DisabledTrackMode aMode) {
MOZ_ASSERT(mGraph->OnGraphThread());
MOZ_DIAGNOSTIC_ASSERT(
aMode == DisabledTrackMode::ENABLED ||
mDisabledMode == DisabledTrackMode::ENABLED,
Expand Down Expand Up @@ -2531,22 +2554,8 @@ void MediaTrack::SetDisabledTrackMode(DisabledTrackMode aMode) {

void MediaTrack::ApplyTrackDisabling(MediaSegment* aSegment,
MediaSegment* aRawSegment) {
if (mDisabledMode == DisabledTrackMode::ENABLED) {
return;
}
if (mDisabledMode == DisabledTrackMode::SILENCE_BLACK) {
aSegment->ReplaceWithDisabled();
if (aRawSegment) {
aRawSegment->ReplaceWithDisabled();
}
} else if (mDisabledMode == DisabledTrackMode::SILENCE_FREEZE) {
aSegment->ReplaceWithNull();
if (aRawSegment) {
aRawSegment->ReplaceWithNull();
}
} else {
MOZ_CRASH("Unsupported mode");
}
MOZ_ASSERT(mGraph->OnGraphThread());
mozilla::ApplyTrackDisabling(mDisabledMode, aSegment, aRawSegment);
}

void MediaTrack::AddMainThreadListener(
Expand Down Expand Up @@ -2866,7 +2875,7 @@ TrackTime SourceMediaTrack::AppendData(MediaSegment* aSegment,

// Apply track disabling before notifying any consumers directly
// or inserting into the graph
ApplyTrackDisabling(aSegment, aRawSegment);
mozilla::ApplyTrackDisabling(mDirectDisabledMode, aSegment, aRawSegment);

ResampleAudioToGraphSampleRate(aSegment);

Expand Down Expand Up @@ -2910,6 +2919,7 @@ void SourceMediaTrack::NotifyDirectConsumers(MediaSegment* aSegment) {

void SourceMediaTrack::AddDirectListenerImpl(
already_AddRefed<DirectMediaTrackListener> aListener) {
MOZ_ASSERT(mGraph->OnGraphThread());
MutexAutoLock lock(mMutex);

RefPtr<DirectMediaTrackListener> listener = aListener;
Expand Down Expand Up @@ -2979,6 +2989,7 @@ void SourceMediaTrack::AddDirectListenerImpl(

void SourceMediaTrack::RemoveDirectListenerImpl(
DirectMediaTrackListener* aListener) {
mGraph->AssertOnGraphThreadOrNotRunning();
MutexAutoLock lock(mMutex);
for (int32_t i = mDirectTrackListeners.Length() - 1; i >= 0; --i) {
const RefPtr<DirectMediaTrackListener>& l = mDirectTrackListeners[i];
Expand Down Expand Up @@ -3008,17 +3019,20 @@ void SourceMediaTrack::End() {
}

void SourceMediaTrack::SetDisabledTrackModeImpl(DisabledTrackMode aMode) {
MOZ_ASSERT(mGraph->OnGraphThread());
{
MutexAutoLock lock(mMutex);
const DisabledTrackMode oldMode = mDirectDisabledMode;
const bool oldEnabled = oldMode == DisabledTrackMode::ENABLED;
const bool enabled = aMode == DisabledTrackMode::ENABLED;
mDirectDisabledMode = aMode;
for (const auto& l : mDirectTrackListeners) {
DisabledTrackMode oldMode = mDisabledMode;
bool oldEnabled = oldMode == DisabledTrackMode::ENABLED;
if (!oldEnabled && aMode == DisabledTrackMode::ENABLED) {
if (!oldEnabled && enabled) {
LOG(LogLevel::Debug, ("%p: SourceMediaTrack %p setting "
"direct listener enabled",
GraphImpl(), this));
l->DecreaseDisabled(oldMode);
} else if (oldEnabled && aMode != DisabledTrackMode::ENABLED) {
} else if (oldEnabled && !enabled) {
LOG(LogLevel::Debug, ("%p: SourceMediaTrack %p setting "
"direct listener disabled",
GraphImpl(), this));
Expand Down
15 changes: 5 additions & 10 deletions dom/media/MediaTrackGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,18 +652,8 @@ class SourceMediaTrack : public MediaTrack {
*/
void End();

// Overriding allows us to hold the mMutex lock while changing the track
// enable status
void SetDisabledTrackModeImpl(DisabledTrackMode aMode) override;

// Overriding allows us to ensure mMutex is locked while changing the track
// enable status
void ApplyTrackDisabling(MediaSegment* aSegment,
MediaSegment* aRawSegment = nullptr) override {
mMutex.AssertCurrentThreadOwns();
MediaTrack::ApplyTrackDisabling(aSegment, aRawSegment);
}

uint32_t NumberOfChannels() const override;

void RemoveAllDirectListenersImpl() override;
Expand Down Expand Up @@ -742,6 +732,11 @@ class SourceMediaTrack : public MediaTrack {
// protected by mMutex
float mVolume MOZ_GUARDED_BY(mMutex) = 1.0;
UniquePtr<TrackData> mUpdateTrack MOZ_GUARDED_BY(mMutex);
// This track's associated disabled mode for uses on the producing thread.
// It can either by disabled by frames being replaced by black, or by
// retaining the previous frame.
DisabledTrackMode mDirectDisabledMode MOZ_GUARDED_BY(mMutex) =
DisabledTrackMode::ENABLED;
nsTArray<RefPtr<DirectMediaTrackListener>> mDirectTrackListeners
MOZ_GUARDED_BY(mMutex);
};
Expand Down

0 comments on commit 3263856

Please sign in to comment.