2016-11-04 15:23:18 +00:00
|
|
|
import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
|
2016-07-08 15:15:14 +00:00
|
|
|
import { ActivatedRoute } from '@angular/router';
|
2016-05-23 07:50:26 +00:00
|
|
|
|
2016-11-04 15:23:18 +00:00
|
|
|
import { ModalDirective } from 'ng2-bootstrap/components/modal';
|
2016-11-04 16:37:44 +00:00
|
|
|
import { MetaService } from 'ng2-meta';
|
2016-11-04 15:23:18 +00:00
|
|
|
|
2016-09-06 20:40:57 +00:00
|
|
|
import { 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',
|
2016-09-19 20:49:31 +00:00
|
|
|
templateUrl: './video-watch.component.html',
|
|
|
|
styleUrls: [ './video-watch.component.scss' ]
|
2016-03-14 12:50:19 +00:00
|
|
|
})
|
|
|
|
|
2016-07-08 15:15:14 +00:00
|
|
|
export class VideoWatchComponent implements OnInit, OnDestroy {
|
2016-06-05 10:20:25 +00:00
|
|
|
private static LOADTIME_TOO_LONG: number = 30000;
|
|
|
|
|
2016-11-04 15:23:18 +00:00
|
|
|
@ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
|
|
|
|
|
2016-04-28 18:40:56 +00:00
|
|
|
downloadSpeed: number;
|
2016-06-05 10:20:25 +00:00
|
|
|
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;
|
2016-11-04 15:04:50 +00:00
|
|
|
video: Video = null;
|
2016-03-14 12:50:19 +00:00
|
|
|
|
2016-06-05 10:20:25 +00:00
|
|
|
private errorTimer: NodeJS.Timer;
|
2016-07-08 15:15:14 +00:00
|
|
|
private sub: any;
|
2016-06-05 10:20:25 +00:00
|
|
|
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-08-12 15:35:00 +00:00
|
|
|
private ngZone: NgZone,
|
2016-07-08 15:15:14 +00:00
|
|
|
private route: ActivatedRoute,
|
2016-05-31 20:39:36 +00:00
|
|
|
private videoService: VideoService,
|
2016-11-04 16:37:44 +00:00
|
|
|
private metaService: MetaService,
|
2016-05-31 20:39:36 +00:00
|
|
|
private webTorrentService: WebTorrentService
|
|
|
|
) {}
|
2016-03-14 12:50:19 +00:00
|
|
|
|
2016-11-04 15:04:50 +00:00
|
|
|
ngOnInit() {
|
|
|
|
this.sub = this.route.params.subscribe(routeParams => {
|
|
|
|
let id = routeParams['id'];
|
|
|
|
this.videoService.getVideo(id).subscribe(
|
|
|
|
video => {
|
|
|
|
this.video = video;
|
2016-11-04 16:37:44 +00:00
|
|
|
this.setOpenGraphTags();
|
2016-11-04 15:04:50 +00:00
|
|
|
this.loadVideo();
|
|
|
|
},
|
|
|
|
error => alert(error.text)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
console.log('Removing video from webtorrent.');
|
|
|
|
clearInterval(this.torrentInfosInterval);
|
|
|
|
clearTimeout(this.errorTimer);
|
|
|
|
this.webTorrentService.remove(this.video.magnetUri);
|
|
|
|
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2016-06-05 10:20:25 +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-06-05 10:20:25 +00:00
|
|
|
|
2016-03-15 12:16:54 +00:00
|
|
|
console.log('Adding ' + this.video.magnetUri + '.');
|
2016-05-31 20:39:36 +00:00
|
|
|
|
2016-06-05 10:20:25 +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) => {
|
2016-06-05 10:20:25 +00: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 12:18:14 +00:00
|
|
|
this.loading = false;
|
2016-06-05 10:20:25 +00:00
|
|
|
|
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
|
|
|
});
|
2016-04-28 18:40:56 +00:00
|
|
|
|
2016-08-12 15:35:00 +00:00
|
|
|
this.runInProgress(torrent);
|
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-11-04 15:23:18 +00:00
|
|
|
showMagnetUriModal() {
|
|
|
|
this.magnetUriModal.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
hideMagnetUriModal() {
|
|
|
|
this.magnetUriModal.hide();
|
|
|
|
}
|
|
|
|
|
2016-06-05 10:20:25 +00:00
|
|
|
private loadTooLong() {
|
|
|
|
this.error = true;
|
|
|
|
console.error('The video load seems to be abnormally long.');
|
|
|
|
}
|
2016-08-12 15:35:00 +00:00
|
|
|
|
2016-11-04 16:37:44 +00:00
|
|
|
private setOpenGraphTags() {
|
|
|
|
this.metaService.setTag('og:type', 'video');
|
|
|
|
|
|
|
|
this.metaService.setTag('og:title', this.video.name);
|
|
|
|
this.metaService.setTag('name', this.video.name);
|
|
|
|
|
|
|
|
this.metaService.setTag('og:description', this.video.description);
|
|
|
|
this.metaService.setTag('description', this.video.description);
|
|
|
|
|
|
|
|
this.metaService.setTag('og:image', this.video.thumbnailPath);
|
|
|
|
|
|
|
|
this.metaService.setTag('og:duration', this.video.duration);
|
|
|
|
|
|
|
|
this.metaService.setTag('og:site_name', 'PeerTube');
|
|
|
|
|
|
|
|
this.metaService.setTag('og:url', window.location.href);
|
|
|
|
this.metaService.setTag('url', window.location.href);
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:35:00 +00:00
|
|
|
private runInProgress(torrent: any) {
|
|
|
|
// Refresh each second
|
|
|
|
this.torrentInfosInterval = setInterval(() => {
|
|
|
|
this.ngZone.run(() => {
|
|
|
|
this.downloadSpeed = torrent.downloadSpeed;
|
|
|
|
this.numPeers = torrent.numPeers;
|
|
|
|
this.uploadSpeed = torrent.uploadSpeed;
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
}
|
2016-03-14 12:50:19 +00:00
|
|
|
}
|