2016-11-04 16:23:18 +01:00
|
|
|
import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
|
2016-07-08 17:15:14 +02:00
|
|
|
import { ActivatedRoute } from '@angular/router';
|
2016-05-23 09:50:26 +02:00
|
|
|
|
2017-01-13 14:31:33 +01:00
|
|
|
import { MetaService } from 'ng2-meta';
|
2016-11-08 20:49:43 +01:00
|
|
|
import * as videojs from 'video.js';
|
2016-11-04 16:23:18 +01:00
|
|
|
|
2017-01-20 19:22:15 +01:00
|
|
|
import { AuthService } from '../../core';
|
2016-11-20 16:09:38 +01:00
|
|
|
import { VideoMagnetComponent } from './video-magnet.component';
|
|
|
|
import { VideoShareComponent } from './video-share.component';
|
2017-01-20 19:22:15 +01:00
|
|
|
import { VideoReportComponent } from './video-report.component';
|
2016-09-06 22:40:57 +02:00
|
|
|
import { 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-09-19 22:49:31 +02:00
|
|
|
templateUrl: './video-watch.component.html',
|
|
|
|
styleUrls: [ './video-watch.component.scss' ]
|
2016-03-14 13:50:19 +01:00
|
|
|
})
|
|
|
|
|
2016-07-08 17:15:14 +02:00
|
|
|
export class VideoWatchComponent implements OnInit, OnDestroy {
|
2017-01-27 11:30:36 +01:00
|
|
|
private static LOADTIME_TOO_LONG: number = 20000;
|
2016-06-05 12:20:25 +02:00
|
|
|
|
2016-11-20 16:09:38 +01:00
|
|
|
@ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent;
|
|
|
|
@ViewChild('videoShareModal') videoShareModal: VideoShareComponent;
|
2017-01-20 19:22:15 +01:00
|
|
|
@ViewChild('videoReportModal') videoReportModal: VideoReportComponent;
|
2016-11-04 16:23:18 +01:00
|
|
|
|
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;
|
2016-11-08 20:49:43 +01:00
|
|
|
player: VideoJSPlayer;
|
2016-11-08 21:17:17 +01:00
|
|
|
playerElement: Element;
|
2016-05-27 17:49:18 +02:00
|
|
|
uploadSpeed: number;
|
2016-11-04 16:04:50 +01:00
|
|
|
video: Video = null;
|
2016-11-16 20:09:36 +01:00
|
|
|
videoNotFound = false;
|
2016-03-14 13:50:19 +01:00
|
|
|
|
2017-01-27 11:30:36 +01:00
|
|
|
private errorTimer: number;
|
2016-07-08 17:15:14 +02:00
|
|
|
private sub: any;
|
2017-01-27 11:30:36 +01:00
|
|
|
private torrentInfosInterval: number;
|
2016-03-14 13:50:19 +01:00
|
|
|
|
|
|
|
constructor(
|
2016-05-27 17:49:18 +02:00
|
|
|
private elementRef: ElementRef,
|
2016-08-12 17:35:00 +02:00
|
|
|
private ngZone: NgZone,
|
2016-07-08 17:15:14 +02:00
|
|
|
private route: ActivatedRoute,
|
2016-05-31 22:39:36 +02:00
|
|
|
private videoService: VideoService,
|
2016-11-04 17:37:44 +01:00
|
|
|
private metaService: MetaService,
|
2017-01-20 19:22:15 +01:00
|
|
|
private webTorrentService: WebTorrentService,
|
|
|
|
private authService: AuthService
|
2016-05-31 22:39:36 +02:00
|
|
|
) {}
|
2016-03-14 13:50:19 +01:00
|
|
|
|
2016-11-04 16:04:50 +01: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 17:37:44 +01:00
|
|
|
this.setOpenGraphTags();
|
2016-11-04 16:04:50 +01:00
|
|
|
this.loadVideo();
|
|
|
|
},
|
2016-11-16 20:09:36 +01:00
|
|
|
error => {
|
|
|
|
this.videoNotFound = true;
|
|
|
|
}
|
2016-11-04 16:04:50 +01:00
|
|
|
);
|
|
|
|
});
|
2016-11-08 20:49:43 +01:00
|
|
|
|
2016-11-08 21:17:17 +01:00
|
|
|
this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
|
|
|
|
|
2016-11-08 20:49:43 +01:00
|
|
|
const videojsOptions = {
|
|
|
|
controls: true,
|
|
|
|
autoplay: false
|
|
|
|
};
|
|
|
|
|
|
|
|
const self = this;
|
2016-11-08 21:17:17 +01:00
|
|
|
videojs(this.playerElement, videojsOptions, function () {
|
2016-11-08 20:49:43 +01:00
|
|
|
self.player = this;
|
|
|
|
});
|
2016-11-04 16:04:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2016-11-08 21:17:17 +01:00
|
|
|
// Remove WebTorrent stuff
|
2016-11-04 16:04:50 +01:00
|
|
|
console.log('Removing video from webtorrent.');
|
2017-01-27 11:30:36 +01:00
|
|
|
window.clearInterval(this.torrentInfosInterval);
|
|
|
|
window.clearTimeout(this.errorTimer);
|
2016-11-16 20:09:36 +01:00
|
|
|
|
|
|
|
if (this.video !== null) {
|
|
|
|
this.webTorrentService.remove(this.video.magnetUri);
|
|
|
|
}
|
2016-11-04 16:04:50 +01:00
|
|
|
|
2016-11-08 21:17:17 +01:00
|
|
|
// Remove player
|
|
|
|
videojs(this.playerElement).dispose();
|
|
|
|
|
|
|
|
// Unsubscribe route subscription
|
2016-11-04 16:04:50 +01:00
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
|
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
|
2017-01-27 11:30:36 +01:00
|
|
|
this.errorTimer = window.setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
|
2016-06-05 12:20:25 +02:00
|
|
|
|
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
|
2017-01-27 11:30:36 +01:00
|
|
|
window.clearTimeout(this.errorTimer);
|
2016-06-05 12:20:25 +02:00
|
|
|
// 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-11-08 21:17:17 +01:00
|
|
|
torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (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
|
|
|
|
2016-08-12 17:35:00 +02:00
|
|
|
this.runInProgress(torrent);
|
2016-04-08 20:58:07 +02:00
|
|
|
});
|
2016-03-14 13:50:19 +01:00
|
|
|
}
|
2016-03-14 22:16:43 +01:00
|
|
|
|
2017-01-20 19:22:15 +01:00
|
|
|
showReportModal(event: Event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.videoReportModal.show();
|
|
|
|
}
|
|
|
|
|
2016-11-08 21:11:57 +01:00
|
|
|
showShareModal() {
|
2016-11-20 16:09:38 +01:00
|
|
|
this.videoShareModal.show();
|
2016-11-08 21:11:57 +01:00
|
|
|
}
|
|
|
|
|
2016-11-20 16:09:38 +01:00
|
|
|
showMagnetUriModal() {
|
|
|
|
this.videoMagnetModal.show();
|
2016-11-08 21:11:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 19:22:15 +01:00
|
|
|
isUserLoggedIn() {
|
|
|
|
return this.authService.isLoggedIn();
|
|
|
|
}
|
|
|
|
|
2016-06-05 12:20:25 +02:00
|
|
|
private loadTooLong() {
|
|
|
|
this.error = true;
|
|
|
|
console.error('The video load seems to be abnormally long.');
|
|
|
|
}
|
2016-08-12 17:35:00 +02:00
|
|
|
|
2016-11-04 17:37:44 +01: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 17:35:00 +02:00
|
|
|
private runInProgress(torrent: any) {
|
|
|
|
// Refresh each second
|
2017-01-27 11:30:36 +01:00
|
|
|
this.torrentInfosInterval = window.setInterval(() => {
|
2016-08-12 17:35:00 +02:00
|
|
|
this.ngZone.run(() => {
|
|
|
|
this.downloadSpeed = torrent.downloadSpeed;
|
|
|
|
this.numPeers = torrent.numPeers;
|
|
|
|
this.uploadSpeed = torrent.uploadSpeed;
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
}
|
2016-03-14 13:50:19 +01:00
|
|
|
}
|