MediaUtils.sys.mjs (2051B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 export const MediaUtils = { 6 getMetadata(aElement) { 7 if (!aElement) { 8 return null; 9 } 10 return { 11 src: aElement.currentSrc ?? aElement.src, 12 width: aElement.videoWidth ?? 0, 13 height: aElement.videoHeight ?? 0, 14 duration: aElement.duration, 15 seekable: !!aElement.seekable, 16 audioTrackCount: 17 (aElement.audioTracks?.length ?? 18 aElement.mozHasAudio ?? 19 aElement.webkitAudioDecodedByteCount ?? 20 MediaUtils.isAudioElement(aElement)) 21 ? 1 22 : 0, 23 videoTrackCount: 24 (aElement.videoTracks?.length ?? MediaUtils.isVideoElement(aElement)) 25 ? 1 26 : 0, 27 }; 28 }, 29 30 isVideoElement(aElement) { 31 return ( 32 aElement && ChromeUtils.getClassName(aElement) === "HTMLVideoElement" 33 ); 34 }, 35 36 isAudioElement(aElement) { 37 return ( 38 aElement && ChromeUtils.getClassName(aElement) === "HTMLAudioElement" 39 ); 40 }, 41 42 isMediaElement(aElement) { 43 return ( 44 MediaUtils.isVideoElement(aElement) || MediaUtils.isAudioElement(aElement) 45 ); 46 }, 47 48 findMediaElement(aElement) { 49 return ( 50 MediaUtils.findVideoElement(aElement) ?? 51 MediaUtils.findAudioElement(aElement) 52 ); 53 }, 54 55 findVideoElement(aElement) { 56 if (!aElement) { 57 return null; 58 } 59 if (MediaUtils.isVideoElement(aElement)) { 60 return aElement; 61 } 62 const childrenMedia = aElement.getElementsByTagName("video"); 63 if (childrenMedia && childrenMedia.length) { 64 return childrenMedia[0]; 65 } 66 return null; 67 }, 68 69 findAudioElement(aElement) { 70 if (!aElement || MediaUtils.isAudioElement(aElement)) { 71 return aElement; 72 } 73 const childrenMedia = aElement.getElementsByTagName("audio"); 74 if (childrenMedia && childrenMedia.length) { 75 return childrenMedia[0]; 76 } 77 return null; 78 }, 79 };