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;
28 playerElement: Element;
32 private errorTimer: NodeJS.Timer;
34 private torrentInfosInterval: NodeJS.Timer;
37 private elementRef: ElementRef,
38 private ngZone: NgZone,
39 private route: ActivatedRoute,
40 private videoService: VideoService,
41 private metaService: MetaService,
42 private webTorrentService: WebTorrentService
46 this.sub = this.route.params.subscribe(routeParams => {
47 let id = routeParams['id'];
48 this.videoService.getVideo(id).subscribe(
51 this.setOpenGraphTags();
54 error => alert(error.text)
58 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
60 const videojsOptions = {
66 videojs(this.playerElement, videojsOptions, function () {
72 // Remove WebTorrent stuff
73 console.log('Removing video from webtorrent.');
74 clearInterval(this.torrentInfosInterval);
75 clearTimeout(this.errorTimer);
76 this.webTorrentService.remove(this.video.magnetUri);
79 videojs(this.playerElement).dispose();
81 // Unsubscribe route subscription
82 this.sub.unsubscribe();
88 // We are loading the video
91 console.log('Adding ' + this.video.magnetUri + '.');
93 // The callback might never return if there are network issues
94 // So we create a timer to inform the user the load is abnormally long
95 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
97 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
98 // Clear the error timer
99 clearTimeout(this.errorTimer);
100 // Maybe the error was fired by the timer, so reset it
103 // We are not loading the video anymore
104 this.loading = false;
106 console.log('Added ' + this.video.magnetUri + '.');
107 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
109 alert('Cannot append the file.');
114 this.runInProgress(torrent);
118 showMagnetUriModal() {
119 this.magnetUriModal.show();
122 hideMagnetUriModal() {
123 this.magnetUriModal.hide();
127 this.shareModal.show();
131 this.shareModal.hide();
134 getVideoIframeCode() {
135 return '<iframe width="560" height="315" ' +
136 'src="' + window.location.origin + '/videos/embed/' + this.video.id + '" ' +
137 'frameborder="0" allowfullscreen>' +
142 return window.location.href;
145 private loadTooLong() {
147 console.error('The video load seems to be abnormally long.');
150 private setOpenGraphTags() {
151 this.metaService.setTag('og:type', 'video');
153 this.metaService.setTag('og:title', this.video.name);
154 this.metaService.setTag('name', this.video.name);
156 this.metaService.setTag('og:description', this.video.description);
157 this.metaService.setTag('description', this.video.description);
159 this.metaService.setTag('og:image', this.video.thumbnailPath);
161 this.metaService.setTag('og:duration', this.video.duration);
163 this.metaService.setTag('og:site_name', 'PeerTube');
165 this.metaService.setTag('og:url', window.location.href);
166 this.metaService.setTag('url', window.location.href);
169 private runInProgress(torrent: any) {
170 // Refresh each second
171 this.torrentInfosInterval = setInterval(() => {
172 this.ngZone.run(() => {
173 this.downloadSpeed = torrent.downloadSpeed;
174 this.numPeers = torrent.numPeers;
175 this.uploadSpeed = torrent.uploadSpeed;