Skip to content

Commit

Permalink
refactor: code improvements and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
4gray committed Oct 6, 2019
1 parent bec61e9 commit d4d51a0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -13,44 +13,53 @@ 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<UploadInput>;
humanizeBytes: Function;
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,
private router: Router
) {
this.options = {
concurrency: 1,
maxUploads: 1
maxUploads: 1,
};
this.files = [];
this.uploadInput = new EventEmitter<UploadInput>();
this.humanizeBytes = humanizeBytes;
}

/**
* Handles file upload
* @param output
*/
onUploadOutput(output: UploadOutput): void {
if (output.type === 'allAddedToQueue') {
if (this.files.length > 0) {
const fileReader = new FileReader();
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();
Expand Down Expand Up @@ -91,6 +100,9 @@ export class PlaylistUploaderComponent {
}
}

/**
* Navigates to the video player route
*/
navigateToPlayer(): void {
this.router.navigateByUrl('/iptv', { skipLocationChange: true });
}
Expand Down
25 changes: 7 additions & 18 deletions src/app/components/video-player/video-player.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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();
});
}
}
}
16 changes: 6 additions & 10 deletions src/app/services/m3u-service.service.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
return this.http.get('../your.json');
} */

convertArrayToPlaylist(m3uArray: any[]): any {
this.m3u8FileParser.read(m3uArray.join('\n'));
return this.m3u8FileParser.getResult();
Expand Down

0 comments on commit d4d51a0

Please sign in to comment.