Truly independent web browser
AudioServer+LibWeb: Capture from a null input device when headless
Luke Wilde0b0c3561fade
diff --git a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp b/Libraries/LibWeb/MediaCapture/MediaDevices.cppindex b9923ee3036..25163fc2b65 100644--- a/Libraries/LibWeb/MediaCapture/MediaDevices.cpp+++ b/Libraries/LibWeb/MediaCapture/MediaDevices.cpp@@ -75,9 +75,6 @@ MediaDevices::MediaDevices(HTML::Window& window): DOM::EventTarget(), m_window(window){- if (HTML::Window::in_test_mode())- return;-m_audio_device_cache_listener_id = Audio::AudioDevices::the().add_devices_changed_listener([this] {did_observe_audio_device_cache_update();});@@ -250,7 +247,7 @@ void MediaDevices::enumerate_devices(GC::Ref<WebIDL::Promise> promise)return;}- if (!HTML::Window::in_test_mode() && !Audio::AudioDevices::the().has_device_list()) {+ if (!Audio::AudioDevices::the().has_device_list()) {m_pending_enumerate_devices_requests.append({ promise });return;}@@ -402,9 +399,7 @@ void MediaDevices::queue_get_user_media_task(GC::Ref<WebIDL::Promise> promise, OGC::Ref<MediaDevices> media_devices = *this;// FIXME: Add camera/video- Vector<Audio::AudioDeviceInfo> audio_input_devices;- if (!HTML::Window::in_test_mode())- audio_input_devices = Audio::AudioDevices::the().input_devices();+ auto audio_input_devices = Audio::AudioDevices::the().input_devices();Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(GC::Heap::the(), [promise = GC::Root(promise), media_devices, device_id_constraint = move(device_id_constraint), audio_input_devices = move(audio_input_devices)] mutable {auto& realm = WebIDL::promise_realm(*promise);@@ -601,7 +596,7 @@ void MediaDevices::process_pending_enumerate_devices_requests(){if (!device_enumeration_can_proceed())return;- if (!HTML::Window::in_test_mode() && !Audio::AudioDevices::the().has_device_list())+ if (!Audio::AudioDevices::the().has_device_list())return;run_device_change_notification_steps(current_audio_device_snapshot());@@ -615,7 +610,7 @@ void MediaDevices::process_pending_get_user_media_requests(){if (!get_user_media_can_proceed())return;- if (!HTML::Window::in_test_mode() && !Audio::AudioDevices::the().has_device_list())+ if (!Audio::AudioDevices::the().has_device_list())return;GC::ConservativeVector pending_requests { move(m_pending_get_user_media_requests) };@@ -753,7 +748,7 @@ void MediaDevices::get_user_media(Optional<MediaStreamConstraints> const& constrreturn;}- if (!HTML::Window::in_test_mode() && !Audio::AudioDevices::the().has_device_list()) {+ if (!Audio::AudioDevices::the().has_device_list()) {m_pending_get_user_media_requests.append({ .promise = promise, .device_id_constraint = move(device_id_constraint) });return;}@@ -775,9 +770,6 @@ void MediaDevices::set_ondevicechange(WebIDL::CallbackType* event_handler)Vector<MediaDevices::StoredDevice> MediaDevices::current_audio_device_snapshot(){Vector<StoredDevice> stored_devices;- if (HTML::Window::in_test_mode())- return stored_devices;-auto input_devices = Audio::AudioDevices::the().input_devices();auto output_devices = Audio::AudioDevices::the().output_devices();stored_devices.ensure_capacity(input_devices.size() + output_devices.size());@@ -818,7 +810,7 @@ void MediaDevices::did_observe_audio_device_cache_update()void MediaDevices::run_with_fresh_audio_device_list(GC::Ref<GC::Function<void()>> callback){- if (HTML::Window::in_test_mode() || !Audio::AudioDevices::the().request_fresh_device_list()) {+ if (!Audio::AudioDevices::the().request_fresh_device_list()) {callback->function()();return;}diff --git a/Services/AudioServer/ServerConnection.cpp b/Services/AudioServer/ServerConnection.cppindex d8510f429ab..3e45535881e 100644--- a/Services/AudioServer/ServerConnection.cpp+++ b/Services/AudioServer/ServerConnection.cpp@@ -12,12 +12,11 @@ namespace Audio {static MixerClientId s_next_mixer_client_id { 1 };-ServerConnection::ServerConnection(NonnullOwnPtr<IPC::Transport> transport, int client_id, NonnullRefPtr<PlaybackStreamMixer> mixer, DeviceEnumeration device_enumeration, NonnullRefPtr<CaptureDevices> capture_devices, CapturePermissionRequest request_capture_permission)+ServerConnection::ServerConnection(NonnullOwnPtr<IPC::Transport> transport, int client_id, NonnullRefPtr<PlaybackStreamMixer> mixer, NonnullRefPtr<CaptureDevices> capture_devices, CapturePermissionRequest request_capture_permission): IPC::ConnectionFromClient<AudioClientEndpoint, AudioServerEndpoint>(*this, move(transport), client_id), m_mixer(move(mixer)), m_capture_devices(move(capture_devices)), m_request_capture_permission(move(request_capture_permission))- , m_device_enumeration(device_enumeration){}@@ -233,10 +232,10 @@ void ServerConnection::set_stream_volume(u64 stream_id, float volume)void ServerConnection::watch_devices(){m_client_watches_devices = true;- if (m_device_enumeration == DeviceEnumeration::Platform && !m_devices_changed_listener_id.has_value())+ if (!m_devices_changed_listener_id.has_value())m_devices_changed_listener_id = AudioDevices::the().add_devices_changed_listener([this] { devices_changed(); });// A list not yet available follows as soon as the platform reports one.- if (m_device_enumeration == DeviceEnumeration::None || AudioDevices::the().has_device_list())+ if (AudioDevices::the().has_device_list())send_device_list();}@@ -249,10 +248,6 @@ static Vector<AudioDeviceInfo> reduced_to_their_existence(Vector<AudioDeviceInfovoid ServerConnection::send_device_list(){- if (m_device_enumeration == DeviceEnumeration::None) {- async_devices_changed({}, {});- return;- }auto& devices = AudioDevices::the();// A page without the permission may learn only that devices exist, so that is all its process learns.if (!m_capture_permitted) {diff --git a/Services/AudioServer/ServerConnection.h b/Services/AudioServer/ServerConnection.hindex befa615b8d2..31e03ed1cf8 100644--- a/Services/AudioServer/ServerConnection.h+++ b/Services/AudioServer/ServerConnection.h@@ -24,12 +24,6 @@ class ServerConnection finalC_OBJECT(ServerConnection);public:- // What the client is told about devices; a headless server has none to tell of.- enum class DeviceEnumeration : u8 {- Platform,- None,- };-// Asks the Browser whether the client's tab may capture from the device; the answer comes once, on this thread.using CapturePermissionRequest = Function<void(ByteString const& device_id, Function<void(bool allowed)> on_decided)>;@@ -43,7 +37,7 @@ public:void set_capture_permitted(bool);private:- ServerConnection(NonnullOwnPtr<IPC::Transport>, int client_id, NonnullRefPtr<PlaybackStreamMixer>, DeviceEnumeration, NonnullRefPtr<CaptureDevices>, CapturePermissionRequest);+ ServerConnection(NonnullOwnPtr<IPC::Transport>, int client_id, NonnullRefPtr<PlaybackStreamMixer>, NonnullRefPtr<CaptureDevices>, CapturePermissionRequest);virtual void die() override;void remove_streams_from_mixer();@@ -93,7 +87,6 @@ private:HashMap<u64, RecordStreamState> m_record_streams;bool m_capture_permitted { false };- DeviceEnumeration m_device_enumeration { DeviceEnumeration::None };Optional<AudioDevices::ListenerId> m_devices_changed_listener_id;bool m_client_watches_devices { false };};diff --git a/Services/AudioServer/Tabs.cpp b/Services/AudioServer/Tabs.cppindex fe5163b244f..04a08dedc2f 100644--- a/Services/AudioServer/Tabs.cpp+++ b/Services/AudioServer/Tabs.cpp@@ -9,6 +9,7 @@#include <AudioServer/ControlConnection.h>#include <AudioServer/PlatformAudio.h>#include <AudioServer/Tabs.h>+#include <LibAudio/AudioDevices.h>#include <LibAudio/NullPlaybackStream.h>#include <LibAudio/PlaybackStream.h>#include <LibAudio/RecordStream.h>@@ -69,6 +70,37 @@ static NonnullRefPtr<Audio::PlaybackStream::CreatePromise> create_device_stream(return promise;}+// The only input device of a headless AudioServer, which opens like any other and delivers nothing.+class NullRecordStream final : public Audio::RecordStream {+public:+ explicit NullRecordStream(Audio::SampleSpecification const& specification)+ : m_specification(specification)+ {+ }++ virtual Audio::SampleSpecification const& sample_specification() const override { return m_specification; }++private:+ Audio::SampleSpecification m_specification;+};++void Tabs::set_audio_output(Audio::AudioOutput audio_output)+{+ m_audio_output = audio_output;+ if (audio_output != Audio::AudioOutput::Null)+ return;++ Audio::AudioDeviceInfo null_input_device {+ .dom_device_id = "null:input",+ .label = "Null input",+ .group_id = {},+ .sample_rate_hz = 48000,+ .channel_count = 1,+ .is_default = true,+ };+ Audio::AudioDevices::the().report_device_list(Audio::AudioDeviceEnumeration { .inputs = { move(null_input_device) }, .outputs = {} });+}+Tabs::Tab& Tabs::tab_for(u64 tab_id){return m_tabs.ensure(tab_id, [this, tab_id] {@@ -96,7 +128,7 @@ Audio::CaptureDevices& Tabs::capture_devices()if (!m_capture_devices) {m_capture_devices = Audio::CaptureDevices::create([audio_output = m_audio_output](Audio::SampleSpecification const& specification, u32 fragment_size_bytes, StringView device_id, Audio::RecordStream::RecordCallback callback) {if (audio_output == Audio::AudioOutput::Null)- return Audio::RecordStream::CreatePromise::rejected(Error::from_string_literal("A headless AudioServer has no capture devices"));+ return Audio::RecordStream::CreatePromise::resolved(adopt_ref<Audio::RecordStream>(*new NullRecordStream(specification)));return Audio::create_platform_record_stream(specification, fragment_size_bytes, device_id, move(callback));});}@@ -110,10 +142,7 @@ ErrorOr<IPC::TransportHandle> Tabs::connect_client(u64 tab_id)auto& tab = tab_for(tab_id);auto client_id = m_next_client_id++;- auto device_enumeration = Audio::ServerConnection::DeviceEnumeration::Platform;- if (m_audio_output == Audio::AudioOutput::Null)- device_enumeration = Audio::ServerConnection::DeviceEnumeration::None;- auto connection = Audio::ServerConnection::construct(move(paired_transports.local), client_id, tab.mixer, device_enumeration, capture_devices(), [tab_id](ByteString const& device_id, Function<void(bool)> on_decided) {+ auto connection = Audio::ServerConnection::construct(move(paired_transports.local), client_id, tab.mixer, capture_devices(), [tab_id](ByteString const& device_id, Function<void(bool)> on_decided) {Tabs::the().request_capture_permission(tab_id, device_id, move(on_decided));});connection->set_capture_permitted(m_tabs_with_capture_permission.contains(tab_id));diff --git a/Services/AudioServer/Tabs.h b/Services/AudioServer/Tabs.hindex d84cb55afb0..5c6b2c6ea64 100644--- a/Services/AudioServer/Tabs.h+++ b/Services/AudioServer/Tabs.h@@ -30,9 +30,9 @@ class Tabs {public:static Tabs& the();- // Decides what every tab's device stream is; headless instances discard their mix into a null stream and have no- // devices to capture from.- void set_audio_output(Audio::AudioOutput audio_output) { m_audio_output = audio_output; }+ // Decides what every tab's devices are; headless instances discard their mix into a null stream and capture from a+ // null input device.+ void set_audio_output(Audio::AudioOutput);// The Browser rules on every capture request, and is asked over this connection.void set_control_connection(ControlConnection&);diff --git a/Tests/AudioServer/TestRemotePlaybackStream.cpp b/Tests/AudioServer/TestRemotePlaybackStream.cppindex 45939ea5e7b..93cc7d9ac43 100644--- a/Tests/AudioServer/TestRemotePlaybackStream.cpp+++ b/Tests/AudioServer/TestRemotePlaybackStream.cpp@@ -100,7 +100,7 @@ struct RemoteFixture {});mixer->set_group_authorization(AUTHORIZED_GROUP, Audio::PlaybackGroupAuthorization::Authorized);auto paired = MUST(IPC::Transport::create_paired());- server = Audio::ServerConnection::construct(move(paired.local), 1, *mixer, Audio::ServerConnection::DeviceEnumeration::Platform, Audio::CaptureDevices::create([](Audio::SampleSpecification const&, u32, StringView, Audio::RecordStream::RecordCallback) { return Audio::RecordStream::CreatePromise::rejected(Error::from_string_literal("No capture in this test")); }), [](ByteString const&, Function<void(bool)> on_decided) { on_decided(false); });+ server = Audio::ServerConnection::construct(move(paired.local), 1, *mixer, Audio::CaptureDevices::create([](Audio::SampleSpecification const&, u32, StringView, Audio::RecordStream::RecordCallback) { return Audio::RecordStream::CreatePromise::rejected(Error::from_string_literal("No capture in this test")); }), [](ByteString const&, Function<void(bool)> on_decided) { on_decided(false); });server->set_capture_permitted(true);server->on_death = [this] { server_died = true; };server_weak = server->make_weak_ptr<Audio::ServerConnection>();@@ -603,7 +603,7 @@ struct RemoteSinkFixture {if (!accepting_connections)return Error::from_string_literal("The fixture is no longer accepting connections");auto paired = TRY(IPC::Transport::create_paired());- servers.append(Audio::ServerConnection::construct(move(paired.local), static_cast<int>(connections_requested), *mixer, Audio::ServerConnection::DeviceEnumeration::Platform, Audio::CaptureDevices::create([](Audio::SampleSpecification const&, u32, StringView, Audio::RecordStream::RecordCallback) { return Audio::RecordStream::CreatePromise::rejected(Error::from_string_literal("No capture in this test")); }), [](ByteString const&, Function<void(bool)> on_decided) { on_decided(false); }));+ servers.append(Audio::ServerConnection::construct(move(paired.local), static_cast<int>(connections_requested), *mixer, Audio::CaptureDevices::create([](Audio::SampleSpecification const&, u32, StringView, Audio::RecordStream::RecordCallback) { return Audio::RecordStream::CreatePromise::rejected(Error::from_string_literal("No capture in this test")); }), [](ByteString const&, Function<void(bool)> on_decided) { on_decided(false); }));auto transport = TRY(paired.remote_handle.create_transport());return transport;});diff --git a/Tests/AudioServer/TestRemoteRecordStream.cpp b/Tests/AudioServer/TestRemoteRecordStream.cppindex 7db158d3b92..a418167d1cc 100644--- a/Tests/AudioServer/TestRemoteRecordStream.cpp+++ b/Tests/AudioServer/TestRemoteRecordStream.cpp@@ -76,7 +76,7 @@ struct RemoteCaptureFixture {return Audio::RecordStream::CreatePromise::resolved(NonnullRefPtr<Audio::RecordStream>(stream));});auto paired = MUST(IPC::Transport::create_paired());- server = Audio::ServerConnection::construct(move(paired.local), 1, *mixer, Audio::ServerConnection::DeviceEnumeration::None, *capture_devices, [this](ByteString const& device_id, Function<void(bool)> on_decided) {+ server = Audio::ServerConnection::construct(move(paired.local), 1, *mixer, *capture_devices, [this](ByteString const& device_id, Function<void(bool)> on_decided) {capture_requests.append(device_id);if (answer_capture_requests_at_once)on_decided(capture_permission_answer);diff --git a/Tests/LibWeb/Text/expected/wpt-import/mediacapture-streams/idlharness.https.window.txt b/Tests/LibWeb/Text/expected/wpt-import/mediacapture-streams/idlharness.https.window.txtindex 7654b9b5c83..9538793ec95 100644--- a/Tests/LibWeb/Text/expected/wpt-import/mediacapture-streams/idlharness.https.window.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/mediacapture-streams/idlharness.https.window.txt@@ -1,9 +1,9 @@Harness status: OK-Found 168 tests+Found 177 tests-103 Pass-65 Fail+143 Pass+34 FailPass idl_test setupPass idl_test validationPass Partial interface Navigator: original interface defined@@ -36,22 +36,22 @@ Pass MediaStream interface: operation clone()Pass MediaStream interface: attribute activePass MediaStream interface: attribute onaddtrackPass MediaStream interface: attribute onremovetrack-Fail MediaStream must be primary interface of stream-Fail Stringification of stream-Fail MediaStream interface: stream must inherit property "id" with the proper type-Fail MediaStream interface: stream must inherit property "getAudioTracks()" with the proper type-Fail MediaStream interface: stream must inherit property "getVideoTracks()" with the proper type-Fail MediaStream interface: stream must inherit property "getTracks()" with the proper type-Fail MediaStream interface: stream must inherit property "getTrackById(DOMString)" with the proper type-Fail MediaStream interface: calling getTrackById(DOMString) on stream with too few arguments must throw TypeError-Fail MediaStream interface: stream must inherit property "addTrack(MediaStreamTrack)" with the proper type-Fail MediaStream interface: calling addTrack(MediaStreamTrack) on stream with too few arguments must throw TypeError-Fail MediaStream interface: stream must inherit property "removeTrack(MediaStreamTrack)" with the proper type-Fail MediaStream interface: calling removeTrack(MediaStreamTrack) on stream with too few arguments must throw TypeError-Fail MediaStream interface: stream must inherit property "clone()" with the proper type-Fail MediaStream interface: stream must inherit property "active" with the proper type-Fail MediaStream interface: stream must inherit property "onaddtrack" with the proper type-Fail MediaStream interface: stream must inherit property "onremovetrack" with the proper type+Pass MediaStream must be primary interface of stream+Pass Stringification of stream+Pass MediaStream interface: stream must inherit property "id" with the proper type+Pass MediaStream interface: stream must inherit property "getAudioTracks()" with the proper type+Pass MediaStream interface: stream must inherit property "getVideoTracks()" with the proper type+Pass MediaStream interface: stream must inherit property "getTracks()" with the proper type+Pass MediaStream interface: stream must inherit property "getTrackById(DOMString)" with the proper type+Pass MediaStream interface: calling getTrackById(DOMString) on stream with too few arguments must throw TypeError+Pass MediaStream interface: stream must inherit property "addTrack(MediaStreamTrack)" with the proper type+Pass MediaStream interface: calling addTrack(MediaStreamTrack) on stream with too few arguments must throw TypeError+Pass MediaStream interface: stream must inherit property "removeTrack(MediaStreamTrack)" with the proper type+Pass MediaStream interface: calling removeTrack(MediaStreamTrack) on stream with too few arguments must throw TypeError+Pass MediaStream interface: stream must inherit property "clone()" with the proper type+Pass MediaStream interface: stream must inherit property "active" with the proper type+Pass MediaStream interface: stream must inherit property "onaddtrack" with the proper type+Pass MediaStream interface: stream must inherit property "onremovetrack" with the proper typePass MediaStream must be primary interface of new MediaStream()Pass Stringification of new MediaStream()Pass MediaStream interface: new MediaStream() must inherit property "id" with the proper type@@ -89,24 +89,24 @@ Pass MediaStreamTrack interface: operation getCapabilities()Pass MediaStreamTrack interface: operation getConstraints()Pass MediaStreamTrack interface: operation getSettings()Pass MediaStreamTrack interface: operation applyConstraints(optional MediaTrackConstraints)-Fail MediaStreamTrack must be primary interface of track-Fail Stringification of track-Fail MediaStreamTrack interface: track must inherit property "kind" with the proper type-Fail MediaStreamTrack interface: track must inherit property "id" with the proper type-Fail MediaStreamTrack interface: track must inherit property "label" with the proper type-Fail MediaStreamTrack interface: track must inherit property "enabled" with the proper type-Fail MediaStreamTrack interface: track must inherit property "muted" with the proper type+Pass MediaStreamTrack must be primary interface of track+Pass Stringification of track+Pass MediaStreamTrack interface: track must inherit property "kind" with the proper type+Pass MediaStreamTrack interface: track must inherit property "id" with the proper type+Pass MediaStreamTrack interface: track must inherit property "label" with the proper type+Pass MediaStreamTrack interface: track must inherit property "enabled" with the proper type+Pass MediaStreamTrack interface: track must inherit property "muted" with the proper typeFail MediaStreamTrack interface: track must inherit property "onmute" with the proper typeFail MediaStreamTrack interface: track must inherit property "onunmute" with the proper type-Fail MediaStreamTrack interface: track must inherit property "readyState" with the proper type+Pass MediaStreamTrack interface: track must inherit property "readyState" with the proper typeFail MediaStreamTrack interface: track must inherit property "onended" with the proper type-Fail MediaStreamTrack interface: track must inherit property "clone()" with the proper type-Fail MediaStreamTrack interface: track must inherit property "stop()" with the proper type-Fail MediaStreamTrack interface: track must inherit property "getCapabilities()" with the proper type-Fail MediaStreamTrack interface: track must inherit property "getConstraints()" with the proper type-Fail MediaStreamTrack interface: track must inherit property "getSettings()" with the proper type-Fail MediaStreamTrack interface: track must inherit property "applyConstraints(optional MediaTrackConstraints)" with the proper type-Fail MediaStreamTrack interface: calling applyConstraints(optional MediaTrackConstraints) on track with too few arguments must throw TypeError+Pass MediaStreamTrack interface: track must inherit property "clone()" with the proper type+Pass MediaStreamTrack interface: track must inherit property "stop()" with the proper type+Pass MediaStreamTrack interface: track must inherit property "getCapabilities()" with the proper type+Pass MediaStreamTrack interface: track must inherit property "getConstraints()" with the proper type+Pass MediaStreamTrack interface: track must inherit property "getSettings()" with the proper type+Pass MediaStreamTrack interface: track must inherit property "applyConstraints(optional MediaTrackConstraints)" with the proper type+Pass MediaStreamTrack interface: calling applyConstraints(optional MediaTrackConstraints) on track with too few arguments must throw TypeErrorPass MediaStreamTrackEvent interface: existence and properties of interface objectPass MediaStreamTrackEvent interface object lengthPass MediaStreamTrackEvent interface object name@@ -114,9 +114,9 @@ Pass MediaStreamTrackEvent interface: existence and properties of interface protPass MediaStreamTrackEvent interface: existence and properties of interface prototype object's "constructor" propertyPass MediaStreamTrackEvent interface: existence and properties of interface prototype object's @@unscopables propertyPass MediaStreamTrackEvent interface: attribute track-Fail MediaStreamTrackEvent must be primary interface of trackEvent-Fail Stringification of trackEvent-Fail MediaStreamTrackEvent interface: trackEvent must inherit property "track" with the proper type+Pass MediaStreamTrackEvent must be primary interface of trackEvent+Pass Stringification of trackEvent+Pass MediaStreamTrackEvent interface: trackEvent must inherit property "track" with the proper typeFail OverconstrainedError interface: existence and properties of interface objectFail OverconstrainedError interface object lengthFail OverconstrainedError interface object name@@ -162,6 +162,15 @@ Fail InputDeviceInfo interface: existence and properties of interface prototypeFail InputDeviceInfo interface: existence and properties of interface prototype object's "constructor" propertyFail InputDeviceInfo interface: existence and properties of interface prototype object's @@unscopables propertyFail InputDeviceInfo interface: operation getCapabilities()+Fail InputDeviceInfo must be primary interface of audioinput+Fail Stringification of audioinput+Fail InputDeviceInfo interface: audioinput must inherit property "getCapabilities()" with the proper type+Pass MediaDeviceInfo interface: audioinput must inherit property "deviceId" with the proper type+Pass MediaDeviceInfo interface: audioinput must inherit property "kind" with the proper type+Pass MediaDeviceInfo interface: audioinput must inherit property "label" with the proper type+Pass MediaDeviceInfo interface: audioinput must inherit property "groupId" with the proper type+Pass MediaDeviceInfo interface: audioinput must inherit property "toJSON()" with the proper type+Pass MediaDeviceInfo interface: default toJSON operation on audioinputFail DeviceChangeEvent interface: existence and properties of interface objectFail DeviceChangeEvent interface object lengthFail DeviceChangeEvent interface object name