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;
21 @ViewChild('shareModal') shareModal: ModalDirective;
23 downloadSpeed: number;
24 error: boolean = false;
25 loading: boolean = false;
27 player: VideoJSPlayer;
31 private errorTimer: NodeJS.Timer;
33 private torrentInfosInterval: NodeJS.Timer;
36 private elementRef: ElementRef,
37 private ngZone: NgZone,
38 private route: ActivatedRoute,
39 private videoService: VideoService,
40 private metaService: MetaService,
41 private webTorrentService: WebTorrentService
45 this.sub = this.route.params.subscribe(routeParams => {
46 let id = routeParams['id'];
47 this.videoService.getVideo(id).subscribe(
50 this.setOpenGraphTags();
53 error => alert(error.text)
57 const videojsOptions = {
63 videojs('video-container', videojsOptions, function () {
69 console.log('Removing video from webtorrent.');
70 clearInterval(this.torrentInfosInterval);
71 clearTimeout(this.errorTimer);
72 this.webTorrentService.remove(this.video.magnetUri);
74 this.sub.unsubscribe();
80 // We are loading the video
83 console.log('Adding ' + this.video.magnetUri + '.');
85 // The callback might never return if there are network issues
86 // So we create a timer to inform the user the load is abnormally long
87 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
89 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
90 // Clear the error timer
91 clearTimeout(this.errorTimer);
92 // Maybe the error was fired by the timer, so reset it
95 // We are not loading the video anymore
98 console.log('Added ' + this.video.magnetUri + '.');
99 torrent.files[0].renderTo('#video-container video', { autoplay: true }, (err) => {
101 alert('Cannot append the file.');
106 this.runInProgress(torrent);
110 showMagnetUriModal() {
111 this.magnetUriModal.show();
114 hideMagnetUriModal() {
115 this.magnetUriModal.hide();
119 this.shareModal.show();
123 this.shareModal.hide();
126 getVideoIframeCode() {
127 return '<iframe width="560" height="315" ' +
128 'src="' + window.location.origin + '/videos/embed/' + this.video.id + '" ' +
129 'frameborder="0" allowfullscreen>' +
134 return window.location.href;
137 private loadTooLong() {
139 console.error('The video load seems to be abnormally long.');
142 private setOpenGraphTags() {
143 this.metaService.setTag('og:type', 'video');
145 this.metaService.setTag('og:title', this.video.name);
146 this.metaService.setTag('name', this.video.name);
148 this.metaService.setTag('og:description', this.video.description);
149 this.metaService.setTag('description', this.video.description);
151 this.metaService.setTag('og:image', this.video.thumbnailPath);
153 this.metaService.setTag('og:duration', this.video.duration);
155 this.metaService.setTag('og:site_name', 'PeerTube');
157 this.metaService.setTag('og:url', window.location.href);
158 this.metaService.setTag('url', window.location.href);
161 private runInProgress(torrent: any) {
162 // Refresh each second
163 this.torrentInfosInterval = setInterval(() => {
164 this.ngZone.run(() => {
165 this.downloadSpeed = torrent.downloadSpeed;
166 this.numPeers = torrent.numPeers;
167 this.uploadSpeed = torrent.uploadSpeed;