ladybird/Tests/LibWeb/Text/input/wpt-import/common/media.js
Prajjwal e1d2582680 LibWeb: Resolve FIXME in media select resource algorithm
Fixes at least three WPT test that were previously timing out:
- html/semantics/embedded-content/media-elements/error-codes/error.html
- html/semantics/embedded-content/media-elements/location-of-the-media-resource/currentSrc.html
- html/semantics/embedded-content/the-video-element/video_crash_empty_src.html
2025-06-16 23:28:10 +12:00

57 lines
1.3 KiB
JavaScript

/**
* Returns the URL of a supported video source based on the user agent
* @param {string} base - media URL without file extension
* @returns {string}
*/
function getVideoURI(base)
{
var extension = '.mp4';
var videotag = document.createElement("video");
if ( videotag.canPlayType )
{
if (videotag.canPlayType('video/webm; codecs="vp9, opus"') )
{
extension = '.webm';
}
}
return base + extension;
}
/**
* Returns the URL of a supported audio source based on the user agent
* @param {string} base - media URL without file extension
* @returns {string}
*/
function getAudioURI(base)
{
var extension = '.mp3';
var audiotag = document.createElement("audio");
if ( audiotag.canPlayType &&
audiotag.canPlayType('audio/ogg') )
{
extension = '.oga';
}
return base + extension;
}
/**
* Returns the MIME type for a media URL based on the file extension.
* @param {string} url
* @returns {string}
*/
function getMediaContentType(url) {
var extension = new URL(url, location).pathname.split(".").pop();
var map = {
"mp4" : "video/mp4",
"webm": "video/webm",
"mp3" : "audio/mp3",
"oga" : "application/ogg",
};
return map[extension];
}