|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { Injectable } from '@angular/core'; |
| 16 | +import { CorePromisedValue } from '@classes/promised-value'; |
| 17 | +import { CoreExternalContentDirective } from '@directives/external-content'; |
| 18 | +import { CoreLang } from '@services/lang'; |
| 19 | +import { CoreTextUtils } from '@services/utils/text'; |
| 20 | +import { CoreUrlUtils } from '@services/utils/url'; |
| 21 | +import { makeSingleton } from '@singletons'; |
| 22 | +import { CoreDirectivesRegistry } from '@singletons/directives-registry'; |
| 23 | +import { CoreEvents } from '@singletons/events'; |
| 24 | +import type videojs from 'video.js'; |
| 25 | + |
| 26 | +// eslint-disable-next-line no-duplicate-imports |
| 27 | +import type { VideoJSOptions, VideoJSPlayer } from 'video.js'; |
| 28 | + |
| 29 | +declare module '@singletons/events' { |
| 30 | + |
| 31 | + /** |
| 32 | + * Augment CoreEventsData interface with events specific to this service. |
| 33 | + * |
| 34 | + * @see https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation |
| 35 | + */ |
| 36 | + export interface CoreEventsData { |
| 37 | + [VIDEO_JS_PLAYER_CREATED]: CoreEventJSVideoPlayerCreated; |
| 38 | + } |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +export const VIDEO_JS_PLAYER_CREATED = 'video_js_player_created'; |
| 43 | + |
| 44 | +/** |
| 45 | + * Wrapper encapsulating videojs functionality. |
| 46 | + */ |
| 47 | +@Injectable({ providedIn: 'root' }) |
| 48 | +export class AddonFilterMediaPluginVideoJSService { |
| 49 | + |
| 50 | + protected videojs?: CorePromisedValue<typeof videojs>; |
| 51 | + |
| 52 | + /** |
| 53 | + * Create a VideoJS player. |
| 54 | + * |
| 55 | + * @param element Media element. |
| 56 | + */ |
| 57 | + async createPlayer(element: HTMLVideoElement | HTMLAudioElement): Promise<void> { |
| 58 | + // Wait for external-content to finish in the element and its sources. |
| 59 | + await Promise.all([ |
| 60 | + CoreDirectivesRegistry.waitDirectivesReady(element, undefined, CoreExternalContentDirective), |
| 61 | + CoreDirectivesRegistry.waitDirectivesReady(element, 'source', CoreExternalContentDirective), |
| 62 | + ]); |
| 63 | + |
| 64 | + // Create player. |
| 65 | + const videojs = await this.getVideoJS(); |
| 66 | + const dataSetupString = element.getAttribute('data-setup') || element.getAttribute('data-setup-lazy') || '{}'; |
| 67 | + const data = CoreTextUtils.parseJSON<VideoJSOptions>(dataSetupString, {}); |
| 68 | + const player = videojs( |
| 69 | + element, |
| 70 | + { |
| 71 | + controls: true, |
| 72 | + techOrder: ['OgvJS'], |
| 73 | + language: await CoreLang.getCurrentLanguage(), |
| 74 | + controlBar: { pictureInPictureToggle: false }, |
| 75 | + aspectRatio: data.aspectRatio, |
| 76 | + }, |
| 77 | + () => element.tagName === 'VIDEO' && this.fixVideoJSPlayerSize(player), |
| 78 | + ); |
| 79 | + |
| 80 | + CoreEvents.trigger(VIDEO_JS_PLAYER_CREATED, { |
| 81 | + element, |
| 82 | + player, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Find a VideoJS player by id. |
| 88 | + * |
| 89 | + * @param id Element id. |
| 90 | + * @returns VideoJS player. |
| 91 | + */ |
| 92 | + async findPlayer(id: string): Promise<VideoJSPlayer | null> { |
| 93 | + const videojs = await this.getVideoJS(); |
| 94 | + |
| 95 | + return videojs.getPlayer(id); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Treat Video JS Youtube video links and translate them to iframes. |
| 100 | + * |
| 101 | + * @param video Video element. |
| 102 | + */ |
| 103 | + treatYoutubeVideos(video: HTMLElement): void { |
| 104 | + if (!video.classList.contains('video-js')) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + const dataSetupString = video.getAttribute('data-setup') || video.getAttribute('data-setup-lazy') || '{}'; |
| 109 | + const data = CoreTextUtils.parseJSON<VideoJSOptions>(dataSetupString, {}); |
| 110 | + const youtubeUrl = data.techOrder?.[0] == 'youtube' && CoreUrlUtils.getYoutubeEmbedUrl(data.sources?.[0]?.src); |
| 111 | + |
| 112 | + if (!youtubeUrl) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const iframe = document.createElement('iframe'); |
| 117 | + iframe.id = video.id; |
| 118 | + iframe.src = youtubeUrl; |
| 119 | + iframe.setAttribute('frameborder', '0'); |
| 120 | + iframe.setAttribute('allowfullscreen', '1'); |
| 121 | + iframe.width = '100%'; |
| 122 | + iframe.height = '300'; |
| 123 | + |
| 124 | + // Replace video tag by the iframe. |
| 125 | + video.parentNode?.replaceChild(iframe, video); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Gets videojs instance. |
| 130 | + * |
| 131 | + * @returns VideoJS. |
| 132 | + */ |
| 133 | + protected async getVideoJS(): Promise<typeof videojs> { |
| 134 | + if (!this.videojs) { |
| 135 | + this.videojs = new CorePromisedValue(); |
| 136 | + |
| 137 | + // Inject CSS. |
| 138 | + const link = document.createElement('link'); |
| 139 | + |
| 140 | + link.rel = 'stylesheet'; |
| 141 | + link.href = 'assets/lib/video.js/video-js.min.css'; |
| 142 | + |
| 143 | + document.head.appendChild(link); |
| 144 | + |
| 145 | + // Load library. |
| 146 | + return import('@addons/filter/mediaplugin/utils/videojs').then(({ initializeVideoJSOgvJS, videojs }) => { |
| 147 | + initializeVideoJSOgvJS(); |
| 148 | + |
| 149 | + this.videojs?.resolve(videojs); |
| 150 | + |
| 151 | + return videojs; |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + return this.videojs; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Fix VideoJS player size. |
| 160 | + * If video width is wider than available width, video is cut off. Fix the dimensions in this case. |
| 161 | + * |
| 162 | + * @param player Player instance. |
| 163 | + */ |
| 164 | + protected fixVideoJSPlayerSize(player: VideoJSPlayer): void { |
| 165 | + const videoWidth = player.videoWidth(); |
| 166 | + const videoHeight = player.videoHeight(); |
| 167 | + const playerDimensions = player.currentDimensions(); |
| 168 | + if (!videoWidth || !videoHeight || !playerDimensions.width || videoWidth === playerDimensions.width) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + const candidateHeight = playerDimensions.width * videoHeight / videoWidth; |
| 173 | + if (!playerDimensions.height || Math.abs(candidateHeight - playerDimensions.height) > 1) { |
| 174 | + player.dimension('height', candidateHeight); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | +} |
| 179 | + |
| 180 | +export const AddonFilterMediaPluginVideoJS = makeSingleton(AddonFilterMediaPluginVideoJSService); |
| 181 | + |
| 182 | +/** |
| 183 | + * Data passed to VIDEO_JS_PLAYER_CREATED event. |
| 184 | + */ |
| 185 | +export type CoreEventJSVideoPlayerCreated = { |
| 186 | + element: HTMLAudioElement | HTMLVideoElement; |
| 187 | + player: VideoJSPlayer; |
| 188 | +}; |
0 commit comments