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;
31 videoNotFound = false;
33 private errorTimer: NodeJS.Timer;
35 private torrentInfosInterval: NodeJS.Timer;
38 private elementRef: ElementRef,
39 private ngZone: NgZone,
40 private route: ActivatedRoute,
41 private videoService: VideoService,
42 private metaService: MetaService,
43 private webTorrentService: WebTorrentService
47 this.sub = this.route.params.subscribe(routeParams => {
48 let id = routeParams['id'];
49 this.videoService.getVideo(id).subscribe(
52 this.setOpenGraphTags();
56 this.videoNotFound = true;
61 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
63 const videojsOptions = {
69 videojs(this.playerElement, videojsOptions, function () {
75 // Remove WebTorrent stuff
76 console.log('Removing video from webtorrent.');
77 clearInterval(this.torrentInfosInterval);
78 clearTimeout(this.errorTimer);
80 if (this.video !== null) {
81 this.webTorrentService.remove(this.video.magnetUri);
85 videojs(this.playerElement).dispose();
87 // Unsubscribe route subscription
88 this.sub.unsubscribe();
94 // We are loading the video
97 console.log('Adding ' + this.video.magnetUri + '.');
99 // The callback might never return if there are network issues
100 // So we create a timer to inform the user the load is abnormally long
101 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
103 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
104 // Clear the error timer
105 clearTimeout(this.errorTimer);
106 // Maybe the error was fired by the timer, so reset it
109 // We are not loading the video anymore
110 this.loading = false;
112 console.log('Added ' + this.video.magnetUri + '.');
113 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
115 alert('Cannot append the file.');
120 this.runInProgress(torrent);
124 showMagnetUriModal() {
125 this.magnetUriModal.show();
128 hideMagnetUriModal() {
129 this.magnetUriModal.hide();
133 this.shareModal.show();
137 this.shareModal.hide();
140 getVideoIframeCode() {
141 return '<iframe width="560" height="315" ' +
142 'src="' + window.location.origin + '/videos/embed/' + this.video.id + '" ' +
143 'frameborder="0" allowfullscreen>' +
148 return window.location.href;
151 private loadTooLong() {
153 console.error('The video load seems to be abnormally long.');
156 private setOpenGraphTags() {
157 this.metaService.setTag('og:type', 'video');
159 this.metaService.setTag('og:title', this.video.name);
160 this.metaService.setTag('name', this.video.name);
162 this.metaService.setTag('og:description', this.video.description);
163 this.metaService.setTag('description', this.video.description);
165 this.metaService.setTag('og:image', this.video.thumbnailPath);
167 this.metaService.setTag('og:duration', this.video.duration);
169 this.metaService.setTag('og:site_name', 'PeerTube');
171 this.metaService.setTag('og:url', window.location.href);
172 this.metaService.setTag('url', window.location.href);
175 private runInProgress(torrent: any) {
176 // Refresh each second
177 this.torrentInfosInterval = setInterval(() => {
178 this.ngZone.run(() => {
179 this.downloadSpeed = torrent.downloadSpeed;
180 this.numPeers = torrent.numPeers;
181 this.uploadSpeed = torrent.uploadSpeed;