2016-07-08 17:15:14 +02:00
|
|
|
import { Component, ElementRef, OnDestroy, OnInit } from '@angular/core';
|
|
|
|
import { ActivatedRoute } from '@angular/router';
|
2016-05-23 09:50:26 +02:00
|
|
|
|
2016-05-13 14:18:37 +02:00
|
|
|
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
|
2016-03-14 13:50:19 +01:00
|
|
|
|
2016-06-03 22:08:03 +02:00
|
|
|
import { LoaderComponent, Video, VideoService } from '../shared';
|
2016-05-31 22:39:36 +02:00
|
|
|
import { WebTorrentService } from './webtorrent.service';
|
2016-03-14 13:50:19 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-video-watch',
|
2016-06-03 22:08:03 +02:00
|
|
|
template: require('./video-watch.component.html'),
|
|
|
|
styles: [ require('./video-watch.component.scss') ],
|
2016-05-31 22:39:36 +02:00
|
|
|
providers: [ WebTorrentService ],
|
2016-05-26 20:40:03 +02:00
|
|
|
directives: [ LoaderComponent ],
|
2016-04-28 20:40:56 +02:00
|
|
|
pipes: [ BytesPipe ]
|
2016-03-14 13:50:19 +01:00
|
|
|
})
|
|
|
|
|
2016-07-08 17:15:14 +02:00
|
|
|
export class VideoWatchComponent implements OnInit, OnDestroy {
|
2016-06-05 12:20:25 +02:00
|
|
|
private static LOADTIME_TOO_LONG: number = 30000;
|
|
|
|
|
2016-04-28 20:40:56 +02:00
|
|
|
downloadSpeed: number;
|
2016-06-05 12:20:25 +02:00
|
|
|
error: boolean = false;
|
2016-04-29 14:18:14 +02:00
|
|
|
loading: boolean = false;
|
2016-05-27 17:49:18 +02:00
|
|
|
numPeers: number;
|
|
|
|
uploadSpeed: number;
|
|
|
|
video: Video;
|
2016-03-14 13:50:19 +01:00
|
|
|
|
2016-06-05 12:20:25 +02:00
|
|
|
private errorTimer: NodeJS.Timer;
|
2016-07-08 17:15:14 +02:00
|
|
|
private sub: any;
|
2016-06-05 12:20:25 +02:00
|
|
|
private torrentInfosInterval: NodeJS.Timer;
|
2016-03-14 13:50:19 +01:00
|
|
|
|
|
|
|
constructor(
|
2016-05-27 17:49:18 +02:00
|
|
|
private elementRef: ElementRef,
|
2016-07-08 17:15:14 +02:00
|
|
|
private route: ActivatedRoute,
|
2016-05-31 22:39:36 +02:00
|
|
|
private videoService: VideoService,
|
|
|
|
private webTorrentService: WebTorrentService
|
|
|
|
) {}
|
2016-03-14 13:50:19 +01:00
|
|
|
|
2016-06-05 12:20:25 +02:00
|
|
|
loadVideo() {
|
|
|
|
// Reset the error
|
|
|
|
this.error = false;
|
|
|
|
// We are loading the video
|
2016-04-29 14:18:14 +02:00
|
|
|
this.loading = true;
|
2016-06-05 12:20:25 +02:00
|
|
|
|
2016-03-15 13:16:54 +01:00
|
|
|
console.log('Adding ' + this.video.magnetUri + '.');
|
2016-05-31 22:39:36 +02:00
|
|
|
|
2016-06-05 12:20:25 +02:00
|
|
|
// The callback might never return if there are network issues
|
|
|
|
// So we create a timer to inform the user the load is abnormally long
|
|
|
|
this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
|
|
|
|
|
2016-05-31 22:39:36 +02:00
|
|
|
this.webTorrentService.add(this.video.magnetUri, (torrent) => {
|
2016-06-05 12:20:25 +02:00
|
|
|
// Clear the error timer
|
|
|
|
clearTimeout(this.errorTimer);
|
|
|
|
// Maybe the error was fired by the timer, so reset it
|
|
|
|
this.error = false;
|
|
|
|
|
|
|
|
// We are not loading the video anymore
|
2016-04-29 14:18:14 +02:00
|
|
|
this.loading = false;
|
2016-06-05 12:20:25 +02:00
|
|
|
|
2016-03-15 13:16:54 +01:00
|
|
|
console.log('Added ' + this.video.magnetUri + '.');
|
2016-05-27 17:25:52 +02:00
|
|
|
torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
|
2016-03-14 13:50:19 +01:00
|
|
|
if (err) {
|
|
|
|
alert('Cannot append the file.');
|
|
|
|
console.error(err);
|
|
|
|
}
|
2016-04-08 20:58:07 +02:00
|
|
|
});
|
2016-04-28 20:40:56 +02:00
|
|
|
|
|
|
|
// Refresh each second
|
2016-06-05 12:20:25 +02:00
|
|
|
this.torrentInfosInterval = setInterval(() => {
|
2016-04-28 20:40:56 +02:00
|
|
|
this.downloadSpeed = torrent.downloadSpeed;
|
|
|
|
this.numPeers = torrent.numPeers;
|
2016-05-27 17:49:18 +02:00
|
|
|
this.uploadSpeed = torrent.uploadSpeed;
|
2016-04-28 20:40:56 +02:00
|
|
|
}, 1000);
|
2016-04-08 20:58:07 +02:00
|
|
|
});
|
2016-03-14 13:50:19 +01:00
|
|
|
}
|
2016-03-14 22:16:43 +01:00
|
|
|
|
2016-07-08 17:15:14 +02:00
|
|
|
ngOnDestroy() {
|
2016-03-14 22:16:43 +01:00
|
|
|
console.log('Removing video from webtorrent.');
|
2016-06-05 12:20:25 +02:00
|
|
|
clearInterval(this.torrentInfosInterval);
|
2016-05-31 22:39:36 +02:00
|
|
|
this.webTorrentService.remove(this.video.magnetUri);
|
2016-07-08 17:15:14 +02:00
|
|
|
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.sub = this.route.params.subscribe(routeParams => {
|
|
|
|
let id = routeParams['id'];
|
|
|
|
this.videoService.getVideo(id).subscribe(
|
|
|
|
video => {
|
|
|
|
this.video = video;
|
|
|
|
this.loadVideo();
|
|
|
|
},
|
|
|
|
error => alert(error)
|
|
|
|
);
|
|
|
|
});
|
2016-03-14 22:16:43 +01:00
|
|
|
}
|
2016-06-05 12:20:25 +02:00
|
|
|
|
|
|
|
private loadTooLong() {
|
|
|
|
this.error = true;
|
|
|
|
console.error('The video load seems to be abnormally long.');
|
|
|
|
}
|
2016-03-14 13:50:19 +01:00
|
|
|
}
|