From 1624a3d169d2c18a24b329cf4b05464a0f1c0323 Mon Sep 17 00:00:00 2001 From: 4gray <4gray@users.noreply.github.com> Date: Sun, 9 Jul 2023 21:58:18 +0200 Subject: [PATCH] refactor: make html-video-player standalone --- .../html-video-player.component.spec.ts | 25 +++++++---------- .../html-video-player.component.ts | 27 ++++++++++++++++--- src/app/player/player.module.ts | 2 +- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/app/player/components/html-video-player/html-video-player.component.spec.ts b/src/app/player/components/html-video-player/html-video-player.component.spec.ts index 09a5c1501..c3b41278c 100644 --- a/src/app/player/components/html-video-player/html-video-player.component.spec.ts +++ b/src/app/player/components/html-video-player/html-video-player.component.spec.ts @@ -1,7 +1,7 @@ -import { MockPipe } from 'ng-mocks'; -import { TranslatePipe } from '@ngx-translate/core'; -import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { SimpleChange } from '@angular/core'; +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; +import { TranslatePipe } from '@ngx-translate/core'; +import { MockPipe } from 'ng-mocks'; import { HtmlVideoPlayerComponent } from './html-video-player.component'; describe('HtmlVideoPlayerComponent', () => { @@ -9,23 +9,18 @@ describe('HtmlVideoPlayerComponent', () => { let fixture: ComponentFixture; const TEST_CHANNEL = { id: '1234', - url: 'http://test', + url: 'http://test.ts', name: 'Test channel', group: { title: 'News group', }, }; - beforeEach( - waitForAsync(() => { - TestBed.configureTestingModule({ - declarations: [ - HtmlVideoPlayerComponent, - MockPipe(TranslatePipe), - ], - }).compileComponents(); - }) - ); + beforeEach(waitForAsync(() => { + TestBed.configureTestingModule({ + declarations: [HtmlVideoPlayerComponent, MockPipe(TranslatePipe)], + }).compileComponents(); + })); beforeEach(() => { fixture = TestBed.createComponent(HtmlVideoPlayerComponent); @@ -45,8 +40,6 @@ describe('HtmlVideoPlayerComponent', () => { }); fixture.detectChanges(); - expect(component.playChannel).toHaveBeenCalledTimes(1); expect(component.playChannel).toHaveBeenCalledWith(TEST_CHANNEL); - expect(console.error).toHaveBeenCalledTimes(1); }); }); diff --git a/src/app/player/components/html-video-player/html-video-player.component.ts b/src/app/player/components/html-video-player/html-video-player.component.ts index cef1bdc45..e4ac2b9c6 100644 --- a/src/app/player/components/html-video-player/html-video-player.component.ts +++ b/src/app/player/components/html-video-player/html-video-player.component.ts @@ -9,6 +9,7 @@ import { } from '@angular/core'; import Hls from 'hls.js'; import { Channel } from '../../../../../shared/channel.interface'; +import { getExtensionFromUrl } from '../../../../../shared/playlist.utils'; /** * This component contains the implementation of HTML5 based video player @@ -17,13 +18,14 @@ import { Channel } from '../../../../../shared/channel.interface'; selector: 'app-html-video-player', templateUrl: './html-video-player.component.html', styleUrls: ['./html-video-player.component.scss'], + standalone: true, }) export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy { /** Channel to play */ @Input() channel: Channel; /** Video player DOM element */ - @ViewChild('videoPlayer', { static: false }) + @ViewChild('videoPlayer', { static: true }) videoPlayer: ElementRef; /** HLS object */ @@ -49,8 +51,14 @@ export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy { playChannel(channel: Channel): void { if (this.hls) this.hls.destroy(); if (channel.url) { - const url = channel.url + channel.epgParams; - if (Hls && Hls.isSupported()) { + const url = channel.url + (channel.epgParams ?? ''); + const extension = getExtensionFromUrl(channel.url); + if ( + extension !== 'mp4' && + extension !== 'mpv' && + Hls && + Hls.isSupported() + ) { console.log('... switching channel to ', channel.name, url); this.hls = new Hls(); this.hls.attachMedia(this.videoPlayer.nativeElement); @@ -58,10 +66,23 @@ export class HtmlVideoPlayerComponent implements OnChanges, OnDestroy { this.handlePlayOperation(); } else { console.error('something wrong with hls.js init...'); + this.addSourceToVideo( + this.videoPlayer.nativeElement, + url, + 'video/mp4' + ); + this.videoPlayer.nativeElement.play(); } } } + addSourceToVideo(element: HTMLVideoElement, url: string, type: string) { + const source = document.createElement('source'); + source.src = url; + source.type = type; + element.appendChild(source); + } + /** * Disables text based captions based on the global settings */ diff --git a/src/app/player/player.module.ts b/src/app/player/player.module.ts index b413e4e4f..3a75d1662 100644 --- a/src/app/player/player.module.ts +++ b/src/app/player/player.module.ts @@ -21,6 +21,7 @@ const routes: Routes = [{ path: '', component: VideoPlayerComponent }]; @NgModule({ imports: [ CommonModule, + HtmlVideoPlayerComponent, OverlayModule, RouterModule.forChild(routes), SharedModule, @@ -33,7 +34,6 @@ const routes: Routes = [{ path: '', component: VideoPlayerComponent }]; EpgListComponent, EpgListItemComponent, InfoOverlayComponent, - HtmlVideoPlayerComponent, MultiEpgContainerComponent, SidebarComponent, ToolbarComponent,