From b66edc415369ec3bb6376c20d369aa07a1c31997 Mon Sep 17 00:00:00 2001 From: 4gray Date: Sun, 6 Oct 2019 21:44:48 +0200 Subject: [PATCH] refactor: code improvements and docs --- .../playlist-uploader.component.ts | 24 +++++++++++++----- .../video-player/video-player.component.ts | 25 ++++++------------- src/app/services/m3u-service.service.ts | 16 +++++------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/app/components/playlist-uploader/playlist-uploader.component.ts b/src/app/components/playlist-uploader/playlist-uploader.component.ts index 1171d1c0b..6ad432a10 100644 --- a/src/app/components/playlist-uploader/playlist-uploader.component.ts +++ b/src/app/components/playlist-uploader/playlist-uploader.component.ts @@ -4,7 +4,7 @@ import { UploadInput, UploadFile, humanizeBytes, - UploaderOptions + UploaderOptions, } from 'ngx-uploader'; import { ChannelStore, createChannel } from 'src/app/state'; import { M3uService } from 'src/app/services/m3u-service.service'; @@ -13,10 +13,9 @@ import { Router } from '@angular/router'; @Component({ selector: 'app-playlist-uploader', templateUrl: './playlist-uploader.component.html', - styleUrls: ['./playlist-uploader.component.css'] + styleUrls: ['./playlist-uploader.component.css'], }) export class PlaylistUploaderComponent { - url = 'http://localhost:4900/upload'; formData: FormData; files: UploadFile[]; uploadInput: EventEmitter; @@ -24,6 +23,12 @@ export class PlaylistUploaderComponent { dragOver: boolean; options: UploaderOptions; + /** + * Creates an instanceof PlaylistUploaderComponent + * @param channelStore channels store + * @param m3uService m3u service + * @param router angulars router + */ constructor( private channelStore: ChannelStore, private m3uService: M3uService, @@ -31,13 +36,17 @@ export class PlaylistUploaderComponent { ) { this.options = { concurrency: 1, - maxUploads: 1 + maxUploads: 1, }; this.files = []; this.uploadInput = new EventEmitter(); this.humanizeBytes = humanizeBytes; } + /** + * Handles file upload + * @param output + */ onUploadOutput(output: UploadOutput): void { if (output.type === 'allAddedToQueue') { if (this.files.length > 0) { @@ -45,12 +54,12 @@ export class PlaylistUploaderComponent { fileReader.onload = fileLoadedEvent => { const result = (fileLoadedEvent.target as FileReader) .result; - // console.log(result); + const array = (result as string).split('\n'); - // console.log(array); const playlist = this.m3uService.convertArrayToPlaylist( array ); + playlist.segments.forEach(element => { this.channelStore.add(createChannel(element)); this.navigateToPlayer(); @@ -91,6 +100,9 @@ export class PlaylistUploaderComponent { } } + /** + * Navigates to the video player route + */ navigateToPlayer(): void { this.router.navigateByUrl('/iptv', { skipLocationChange: true }); } diff --git a/src/app/components/video-player/video-player.component.ts b/src/app/components/video-player/video-player.component.ts index ba089b21e..0d283b314 100644 --- a/src/app/components/video-player/video-player.component.ts +++ b/src/app/components/video-player/video-player.component.ts @@ -47,24 +47,6 @@ export class VideoPlayerComponent implements OnInit { ) as HTMLVideoElement; } - /* playVideo(playlist: any): void { - if (Hls.isSupported()) { - this.hls.loadSource(channelUrl); - this.hls.attachMedia(this.videoPlayer); - - this.hls.on(Hls.Events.MANIFEST_PARSED, () => { - this.videoPlayer.play(); - }); - } else if ( - this.videoPlayer.canPlayType('application/vnd.apple.mpegurl') - ) { - this.videoPlayer.src = channelUrl; - this.videoPlayer.addEventListener('loadedmetadata', () => { - this.videoPlayer.play(); - }); - } - } */ - /** * Closes sidebar */ @@ -86,6 +68,13 @@ export class VideoPlayerComponent implements OnInit { this.hls.loadSource(channel.url); this.hls.attachMedia(this.videoPlayer); this.channelTitle = channel.title; + } else if ( + this.videoPlayer.canPlayType('application/vnd.apple.mpegurl') + ) { + this.videoPlayer.src = channel.url; + this.videoPlayer.addEventListener('loadedmetadata', () => { + this.videoPlayer.play(); + }); } } } diff --git a/src/app/services/m3u-service.service.ts b/src/app/services/m3u-service.service.ts index 56d68416b..6b03d7928 100644 --- a/src/app/services/m3u-service.service.ts +++ b/src/app/services/m3u-service.service.ts @@ -1,23 +1,19 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; -import { Observable } from 'rxjs'; import * as M3U8FileParser from 'm3u8-file-parser'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) export class M3uService { + /** + * m3u8 parser + */ m3u8FileParser = new M3U8FileParser(); - constructor(private http: HttpClient) {} - /** - * Returns playlist object + * Converts string based array to playlist object + * @param m3uArray m3u playlist as array with strings */ - /* getPlaylist(): Observable { - return this.http.get('../your.json'); - } */ - convertArrayToPlaylist(m3uArray: any[]): any { this.m3u8FileParser.read(m3uArray.join('\n')); return this.m3u8FileParser.getResult();