1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
4 import { ModalDirective } from 'ng2-bootstrap/components/modal';
5 import { MetaService } from 'ng2-meta';
6 import * as videojs from 'video.js';
8 import { Video, VideoService } from '../shared';
9 import { WebTorrentService } from './webtorrent.service';
12 selector: 'my-video-watch',
13 templateUrl: './video-watch.component.html',
14 styleUrls: [ './video-watch.component.scss' ]
17 export class VideoWatchComponent implements OnInit, OnDestroy {
18 private static LOADTIME_TOO_LONG: number = 30000;
20 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
22 downloadSpeed: number;
23 error: boolean = false;
24 loading: boolean = false;
26 player: VideoJSPlayer;
30 private errorTimer: NodeJS.Timer;
32 private torrentInfosInterval: NodeJS.Timer;
35 private elementRef: ElementRef,
36 private ngZone: NgZone,
37 private route: ActivatedRoute,
38 private videoService: VideoService,
39 private metaService: MetaService,
40 private webTorrentService: WebTorrentService
44 this.sub = this.route.params.subscribe(routeParams => {
45 let id = routeParams['id'];
46 this.videoService.getVideo(id).subscribe(
49 this.setOpenGraphTags();
52 error => alert(error.text)
56 const videojsOptions = {
62 videojs('video-container', videojsOptions, function () {
68 console.log('Removing video from webtorrent.');
69 clearInterval(this.torrentInfosInterval);
70 clearTimeout(this.errorTimer);
71 this.webTorrentService.remove(this.video.magnetUri);
73 this.sub.unsubscribe();
77 console.log('<iframe width="560" height="315" src="' + window.location.origin + '/videos/embed/' + this.video.id + '" frameborder="0" allowfullscreen></iframe>');
81 // We are loading the video
84 console.log('Adding ' + this.video.magnetUri + '.');
86 // The callback might never return if there are network issues
87 // So we create a timer to inform the user the load is abnormally long
88 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
90 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
91 // Clear the error timer
92 clearTimeout(this.errorTimer);
93 // Maybe the error was fired by the timer, so reset it
96 // We are not loading the video anymore
99 console.log('Added ' + this.video.magnetUri + '.');
100 torrent.files[0].renderTo('#video-container video', { autoplay: true }, (err) => {
102 alert('Cannot append the file.');
107 this.runInProgress(torrent);
111 showMagnetUriModal() {
112 this.magnetUriModal.show();
115 hideMagnetUriModal() {
116 this.magnetUriModal.hide();
119 private loadTooLong() {
121 console.error('The video load seems to be abnormally long.');
124 private setOpenGraphTags() {
125 this.metaService.setTag('og:type', 'video');
127 this.metaService.setTag('og:title', this.video.name);
128 this.metaService.setTag('name', this.video.name);
130 this.metaService.setTag('og:description', this.video.description);
131 this.metaService.setTag('description', this.video.description);
133 this.metaService.setTag('og:image', this.video.thumbnailPath);
135 this.metaService.setTag('og:duration', this.video.duration);
137 this.metaService.setTag('og:site_name', 'PeerTube');
139 this.metaService.setTag('og:url', window.location.href);
140 this.metaService.setTag('url', window.location.href);
143 private runInProgress(torrent: any) {
144 // Refresh each second
145 this.torrentInfosInterval = setInterval(() => {
146 this.ngZone.run(() => {
147 this.downloadSpeed = torrent.downloadSpeed;
148 this.numPeers = torrent.numPeers;
149 this.uploadSpeed = torrent.uploadSpeed;