From 5f9fc5aedc571f3550cc6ef00ed723672d7e7bb0 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 4 Apr 2023 14:20:42 -0400 Subject: [PATCH] LibWeb: Indicate that we may be able to play video MIME types --- .../Libraries/LibWeb/HTML/HTMLMediaElement.cpp | 18 +++++++++++++----- .../Libraries/LibWeb/HTML/HTMLMediaElement.h | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp index 8765f11c82..0704d285a3 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace Web::HTML { @@ -66,8 +67,10 @@ void HTMLMediaElement::parse_attribute(DeprecatedFlyString const& name, Deprecat } // https://html.spec.whatwg.org/multipage/media.html#dom-navigator-canplaytype -Bindings::CanPlayTypeResult HTMLMediaElement::can_play_type(DeprecatedString const& type) const +WebIDL::ExceptionOr HTMLMediaElement::can_play_type(DeprecatedString const& type) const { + auto& vm = this->vm(); + // The canPlayType(type) method must: // - return the empty string if type is a type that the user agent knows it cannot render or is the type "application/octet-stream" // - return "probably" if the user agent is confident that the type represents a media resource that it can render if used in with this audio or video element @@ -75,10 +78,15 @@ Bindings::CanPlayTypeResult HTMLMediaElement::can_play_type(DeprecatedString con // Generally, a user agent should never return "probably" for a type that allows the codecs parameter if that parameter is not present. if (type == "application/octet-stream"sv) return Bindings::CanPlayTypeResult::Empty; - // FIXME: Eventually we should return `Maybe` here, but for now `Empty` is our best bet :^) - // Being honest here leads to some apps and frameworks skipping things like audio loading, - // which for the time being would create more issues than it solves - e.g. endless waiting - // for audio that will never load. + + auto mime_type = TRY_OR_THROW_OOM(vm, MimeSniff::MimeType::parse(type)); + + if (mime_type.has_value() && mime_type->type() == "video"sv) { + if (mime_type->subtype() == "webm"sv) + return Bindings::CanPlayTypeResult::Probably; + return Bindings::CanPlayTypeResult::Maybe; + } + return Bindings::CanPlayTypeResult::Empty; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h index ac994d1719..a47cf455fc 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h @@ -30,7 +30,7 @@ public: }; NetworkState network_state() const { return m_network_state; } - Bindings::CanPlayTypeResult can_play_type(DeprecatedString const& type) const; + WebIDL::ExceptionOr can_play_type(DeprecatedString const& type) const; void load() const; void pause() const;