Truly independent web browser
LibWeb: Render MediaStream destinations without an output bus
Shannon Boothf160d522b02f
diff --git a/Libraries/LibWeb/Rust/src/web_audio/rendering/media_stream_destination_render_node.rs b/Libraries/LibWeb/Rust/src/web_audio/rendering/media_stream_destination_render_node.rsindex 9bcac11e708..66b290a5b54 100644--- a/Libraries/LibWeb/Rust/src/web_audio/rendering/media_stream_destination_render_node.rs+++ b/Libraries/LibWeb/Rust/src/web_audio/rendering/media_stream_destination_render_node.rs@@ -14,7 +14,7 @@ use super::audio_frame_ring::{AudioFrameRing, AudioFrameRingCallbacks};use super::pipe_callbacks::{CallbackService, PipeCallbacks};use super::render_graph::RenderGraph;use super::render_node::*;-use super::render_nodes::{BusRef, NodeProcessor, node_handle};+use super::render_nodes::{NodeProcessor, node_handle};use crate::web_audio::control_message::FfiSharedCppObject;// The render thread only touches the ring and pending flag; the control thread owns the drain@@ -39,20 +39,14 @@ struct MediaStreamDestinationRenderNode {impl NodeProcessor for MediaStreamDestinationRenderNode {unsafe fn process(&mut self, data: &RenderNodeData, graph: &RenderGraph, context: &RenderContext) {unsafe {- let input = BusRef(data.pull_input(graph, context, 0).cast_mut());-- // AD-HOC: The spec gives this node zero outputs, but RenderGraph only pulls nodes that- // have at least one, so the render node exposes its input as a passthrough- // output. This keeps the node processing every quantum and also makes it audible- // when a page connects it onward.- BusRef(data.output(0)).copy_from(&input);+ let input = &*data.pull_input(graph, context, 0);// Mix the input to the track's channel count and interleave it into the shared ring. If// the control thread has stalled and the ring is full, the newest frames are dropped; the// consumer resynchronizes when it drains.self.mix_bus.zero();self.mix_bus.sum_from(- &*input.0,+ input,data.channel_interpretation.get() == ChannelInterpretation::Speakers,);let channel_count = self.mix_bus.channel_count();@@ -91,7 +85,7 @@ pub(crate) unsafe extern "C" fn web_audio_node_create_media_stream_destination(ring_callbacks: AudioFrameRingCallbacks,callbacks: PipeCallbacks,) -> *mut NodeHandle {- let data = RenderNodeData::new(node_id, 1, 1, quantum_size, channel_count);+ let data = RenderNodeData::new(node_id, 1, 0, quantum_size, channel_count);// NB: The control thread queues SetChannelConfig before AddNode and messages addressed to// unknown nodes are dropped, so the constructor must establish the node's channeldiff --git a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rs b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rsindex 9c28dd8a758..d39e51db502 100644--- a/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rs+++ b/Libraries/LibWeb/Rust/src/web_audio/rendering/render_graph.rs@@ -91,15 +91,13 @@ impl RenderGraph {self.incoming_connections_dirty.set(false);}- // Pull the destination first so the main graph is processed in dependency order; then process any remaining nodes- // so unconnected sources still advance their playback state and report ended events.+ // Process the destination first so the main graph is processed in dependency order.if let Some(destination_node) = self.destination() {- self.pull_output(destination_node.data().node_id, 0, context);+ self.process_node(destination_node, context);}- for (&node_id, graph_node) in self.nodes.iter() {- if graph_node.data().output_count() > 0 {- self.pull_output(node_id, 0, context);- }+ // Advance unconnected sources and process sinks with no outputs.+ for graph_node in self.nodes.values() {+ self.process_node(graph_node, context);}}@@ -113,22 +111,21 @@ impl RenderGraph {if output_index >= data.output_count() {return std::ptr::null_mut();}+ self.process_node(source_node, context);+ unsafe { data.output(output_index) }+ }- if data.processed_quantum.get() != self.current_quantum.get() {- // A re-entrant pull means this node is part of a cycle; its output bus still holds the previous quantum's- // samples, which effectively inserts one render quantum of delay into the cycle.- if data.is_processing.get() {- return unsafe { data.output(output_index) };- }-- data.is_processing.set(true);- unsafe {- source_node.node.process(self, context);- }- data.is_processing.set(false);- data.processed_quantum.set(self.current_quantum.get());+ fn process_node(&self, node: &NodeHandle, context: &RenderContext) {+ let data = node.data();+ if data.processed_quantum.get() == self.current_quantum.get() || data.is_processing.get() {+ return;}- unsafe { data.output(output_index) }+ data.is_processing.set(true);+ unsafe {+ node.node.process(self, context);+ }+ data.is_processing.set(false);+ data.processed_quantum.set(self.current_quantum.get());}pub fn node(&self, node_id: u64) -> Option<&NodeHandle> {diff --git a/Tests/LibWeb/Text/expected/WebAudio/MediaStreamAudioSourceNode-resampling.txt b/Tests/LibWeb/Text/expected/WebAudio/MediaStreamAudioSourceNode-resampling.txtindex 9f36bdf5bce..e483453b096 100644--- a/Tests/LibWeb/Text/expected/WebAudio/MediaStreamAudioSourceNode-resampling.txt+++ b/Tests/LibWeb/Text/expected/WebAudio/MediaStreamAudioSourceNode-resampling.txt@@ -1 +1,3 @@+MediaStream destination outputs: 0+IndexSizeErrorPASS: mono MediaStream preserves tone frequency from 32000 Hz to 44100 Hzdiff --git a/Tests/LibWeb/Text/input/WebAudio/MediaStreamAudioSourceNode-resampling.html b/Tests/LibWeb/Text/input/WebAudio/MediaStreamAudioSourceNode-resampling.htmlindex 8c369a6a8df..b42c095811f 100644--- a/Tests/LibWeb/Text/input/WebAudio/MediaStreamAudioSourceNode-resampling.html+++ b/Tests/LibWeb/Text/input/WebAudio/MediaStreamAudioSourceNode-resampling.html@@ -7,6 +7,13 @@ asyncTest(async done => {try {const oscillator = new OscillatorNode(sourceContext, { frequency: 1000 });const destination = new MediaStreamAudioDestinationNode(sourceContext, { channelCount: 1 });+ println(`MediaStream destination outputs: ${destination.numberOfOutputs}`);+ try {+ destination.connect(sourceContext.destination);+ println("Connected a MediaStream destination output");+ } catch (error) {+ println(error.name);+ }oscillator.connect(destination);const source = sinkContext.createMediaStreamSource(destination.stream);const analyser = new AnalyserNode(sinkContext, { fftSize: 2048, smoothingTimeConstant: 0 });