Skip to content

Truly independent web browser

LibWeb: Keep media stream sources alive while their track is live

Ali Mohammad Purdf2bc28611b3
diff --git a/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp b/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp
index 1603be83b39..81351be77f9 100644
--- a/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp
+++ b/Libraries/LibWeb/WebAudio/BaseAudioContext.cpp
@@ -36,6 +36,7 @@
#include <LibWeb/WebAudio/ControlMessageQueue.h>
#include <LibWeb/WebAudio/DynamicsCompressorNode.h>
#include <LibWeb/WebAudio/GainNode.h>
+#include <LibWeb/WebAudio/MediaStreamAudioSourceNode.h>
#include <LibWeb/WebAudio/OscillatorNode.h>
#include <LibWeb/WebAudio/PannerNode.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
@@ -99,14 +100,23 @@ void BaseAudioContext::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_audio_worklet);
visitor.visit(m_playing_sources);
+ if (state() == AudioContextState::Closed)
+ return;
+
// INTEROP: Blink keeps a ScriptProcessorNode with audioprocess listeners alive until its context is cleared
// (ScriptProcessorNode::HasPendingActivity()), whether it is connected or not, so that a node script only
// reaches through its listener keeps producing audio.
- if (state() != AudioContextState::Closed) {
- for (auto& node : m_script_processor_nodes) {
- if (node.has_event_listener(HTML::EventNames::audioprocess))
- visitor.visit(node);
- }
+ for (auto& node : m_script_processor_nodes) {
+ if (node.has_event_listener(HTML::EventNames::audioprocess))
+ visitor.visit(node);
+ }
+
+ // Keep a source and the nodes it feeds alive while its track is live, even after script drops the source.
+ // https://webaudio.github.io/web-audio-api/#AudioNode-actively-processing
+ // NB: A muted or disabled track only pauses its source, so it stays alive as well.
+ for (auto& source : m_media_stream_sources) {
+ if (source.has_live_input_track())
+ visitor.visit(source);
}
}
@@ -120,6 +130,11 @@ void BaseAudioContext::add_script_processor_node(ScriptProcessorNode& node)
m_script_processor_nodes.set(node);
}
+void BaseAudioContext::add_media_stream_source(MediaStreamAudioSourceNode& source)
+{
+ m_media_stream_sources.set(source);
+}
+
void BaseAudioContext::handle_ended_sources(ReadonlySpan<NodeID> ended_nodes)
{
for (auto node_id : ended_nodes) {
diff --git a/Libraries/LibWeb/WebAudio/BaseAudioContext.h b/Libraries/LibWeb/WebAudio/BaseAudioContext.h
index 3c551219470..d21c2a886c5 100644
--- a/Libraries/LibWeb/WebAudio/BaseAudioContext.h
+++ b/Libraries/LibWeb/WebAudio/BaseAudioContext.h
@@ -130,6 +130,7 @@ public:
void add_playing_source(GC::Ref<AudioNode>);
void add_script_processor_node(ScriptProcessorNode&);
+ void add_media_stream_source(MediaStreamAudioSourceNode&);
void handle_ended_sources(ReadonlySpan<NodeID>);
NodeID next_node_id(Badge<AudioNode>) { return ++m_next_node_id; }
@@ -174,6 +175,8 @@ private:
HashMap<NodeID, GC::Ref<AudioNode>> m_playing_sources;
GC::WeakHashSet<ScriptProcessorNode> m_script_processor_nodes;
+ // Kept alive only while they can still produce audio; see visit_edges().
+ GC::WeakHashSet<MediaStreamAudioSourceNode> m_media_stream_sources;
};
void resolve_audio_buffer_promise(JS::Realm&, WebIDL::Promise const&, AudioBuffer&);
diff --git a/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.cpp b/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.cpp
index 2bf345e2508..5a8d3b7b426 100644
--- a/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.cpp
+++ b/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.cpp
@@ -70,6 +70,7 @@ WebIDL::ExceptionOr<GC::Ref<MediaStreamAudioSourceNode>> MediaStreamAudioSourceN
node->queue_render_node_creation(make<Rendering::MediaStreamSourceRenderNode>(node->node_id(), BaseAudioContext::render_quantum_size()));
node->attach_input_track();
+ context->add_media_stream_source(*node);
return node;
}
@@ -134,6 +135,11 @@ void MediaStreamAudioSourceNode::attach_input_track()
m_input_track->add_audio_sink(*m_sink);
}
+bool MediaStreamAudioSourceNode::has_live_input_track() const
+{
+ return m_input_track && m_input_track->track_ready_state() == MediaCapture::MediaStreamTrackState::Live;
+}
+
void MediaStreamAudioSourceNode::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
diff --git a/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.h b/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.h
index 849eb71d0b5..c5a9ec09a46 100644
--- a/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.h
+++ b/Libraries/LibWeb/WebAudio/MediaStreamAudioSourceNode.h
@@ -44,6 +44,8 @@ public:
// https://webaudio.github.io/web-audio-api/#dom-mediastreamaudiosourcenode-mediastream
GC::Ref<MediaCapture::MediaStream> media_stream() const { return m_media_stream; }
+ bool has_live_input_track() const;
+
private:
MediaStreamAudioSourceNode(GC::Ref<AudioContext>, MediaStreamAudioSourceOptions const&);
diff --git a/Tests/LibWeb/Text/expected/WebAudio/MediaStreamAudioSourceNode-survives-gc.txt b/Tests/LibWeb/Text/expected/WebAudio/MediaStreamAudioSourceNode-survives-gc.txt
new file mode 100644
index 00000000000..a80e5b8c3db
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebAudio/MediaStreamAudioSourceNode-survives-gc.txt
@@ -0,0 +1 @@
+audio still arrives after garbage collection: true
diff --git a/Tests/LibWeb/Text/input/WebAudio/MediaStreamAudioSourceNode-survives-gc.html b/Tests/LibWeb/Text/input/WebAudio/MediaStreamAudioSourceNode-survives-gc.html
new file mode 100644
index 00000000000..4ce258b457d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebAudio/MediaStreamAudioSourceNode-survives-gc.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+asyncTest(async done => {
+ // The page drops its source node right after connecting it, as Discord does with the microphone's.
+ const producer = new AudioContext();
+ const oscillator = producer.createOscillator();
+ const destination = producer.createMediaStreamDestination();
+ oscillator.connect(destination);
+ oscillator.start();
+
+ const consumer = new AudioContext();
+ const analyser = consumer.createAnalyser();
+ consumer.createMediaStreamSource(destination.stream).connect(analyser);
+
+ internals.gc();
+ internals.gc();
+
+ const samples = new Float32Array(analyser.fftSize);
+ let peak = 0;
+ for (let attempt = 0; attempt < 100 && peak < 0.1; ++attempt) {
+ await new Promise(resolve => setTimeout(resolve, 50));
+ internals.gc();
+ analyser.getFloatTimeDomainData(samples);
+ peak = samples.reduce((max, sample) => Math.max(max, Math.abs(sample)), 0);
+ }
+ println(`audio still arrives after garbage collection: ${peak >= 0.1}`);
+
+ oscillator.stop();
+ await producer.close();
+ await consumer.close();
+ done();
+});
+</script>