1
0
Fork 0
peertube/client/src/app/videos/video-watch/video-watch.component.ts

99 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Component, ElementRef, OnInit } from '@angular/core';
2016-06-10 15:43:40 +00:00
import { CanDeactivate, RouteSegment } from '@angular/router';
2016-05-13 12:18:37 +00:00
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';
2016-03-14 12:50:19 +00:00
import { LoaderComponent, Video, VideoService } from '../shared';
2016-05-31 20:39:36 +00:00
import { WebTorrentService } from './webtorrent.service';
2016-03-14 12:50:19 +00:00
@Component({
selector: 'my-video-watch',
template: require('./video-watch.component.html'),
styles: [ require('./video-watch.component.scss') ],
2016-05-31 20:39:36 +00:00
providers: [ WebTorrentService ],
directives: [ LoaderComponent ],
pipes: [ BytesPipe ]
2016-03-14 12:50:19 +00:00
})
export class VideoWatchComponent implements OnInit, CanDeactivate {
private static LOADTIME_TOO_LONG: number = 30000;
downloadSpeed: number;
error: boolean = false;
2016-04-29 12:18:14 +00:00
loading: boolean = false;
2016-05-27 15:49:18 +00:00
numPeers: number;
uploadSpeed: number;
video: Video;
2016-03-14 12:50:19 +00:00
private errorTimer: NodeJS.Timer;
private torrentInfosInterval: NodeJS.Timer;
2016-03-14 12:50:19 +00:00
constructor(
2016-05-27 15:49:18 +00:00
private elementRef: ElementRef,
2016-06-10 15:43:40 +00:00
private routeSegment: RouteSegment,
2016-05-31 20:39:36 +00:00
private videoService: VideoService,
private webTorrentService: WebTorrentService
) {}
2016-03-14 12:50:19 +00:00
loadVideo() {
// Reset the error
this.error = false;
// We are loading the video
2016-04-29 12:18:14 +00:00
this.loading = true;
2016-03-15 12:16:54 +00:00
console.log('Adding ' + this.video.magnetUri + '.');
2016-05-31 20:39:36 +00: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 20:39:36 +00:00
this.webTorrentService.add(this.video.magnetUri, (torrent) => {
// 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 12:18:14 +00:00
this.loading = false;
2016-03-15 12:16:54 +00:00
console.log('Added ' + this.video.magnetUri + '.');
2016-05-27 15:25:52 +00:00
torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
2016-03-14 12:50:19 +00:00
if (err) {
alert('Cannot append the file.');
console.error(err);
}
2016-04-08 18:58:07 +00:00
});
// Refresh each second
this.torrentInfosInterval = setInterval(() => {
this.downloadSpeed = torrent.downloadSpeed;
this.numPeers = torrent.numPeers;
2016-05-27 15:49:18 +00:00
this.uploadSpeed = torrent.uploadSpeed;
}, 1000);
2016-04-08 18:58:07 +00:00
});
2016-03-14 12:50:19 +00:00
}
2016-03-14 21:16:43 +00:00
2016-05-27 15:49:18 +00:00
ngOnInit() {
2016-06-10 15:43:40 +00:00
let id = this.routeSegment.getParam('id');
2016-05-27 15:49:18 +00:00
this.videoService.getVideo(id).subscribe(
video => {
this.video = video;
this.loadVideo();
},
2016-05-27 15:49:18 +00:00
error => alert(error)
);
}
2016-06-10 15:43:40 +00:00
routerCanDeactivate() {
2016-03-14 21:16:43 +00:00
console.log('Removing video from webtorrent.');
clearInterval(this.torrentInfosInterval);
2016-05-31 20:39:36 +00:00
this.webTorrentService.remove(this.video.magnetUri);
2016-06-10 15:43:40 +00:00
return Promise.resolve(true);
2016-03-14 21:16:43 +00:00
}
private loadTooLong() {
this.error = true;
console.error('The video load seems to be abnormally long.');
}
2016-03-14 12:50:19 +00:00
}