]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
Client: update ng2-meta
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
1 import { setInterval, setTimeout } from 'timers'
2 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
3 import { ActivatedRoute } from '@angular/router';
4
5 import { MetaService } from 'ng2-meta';
6 import * as videojs from 'video.js';
7
8 import { VideoMagnetComponent } from './video-magnet.component';
9 import { VideoShareComponent } from './video-share.component';
10 import { Video, VideoService } from '../shared';
11 import { WebTorrentService } from './webtorrent.service';
12
13 @Component({
14 selector: 'my-video-watch',
15 templateUrl: './video-watch.component.html',
16 styleUrls: [ './video-watch.component.scss' ]
17 })
18
19 export class VideoWatchComponent implements OnInit, OnDestroy {
20 private static LOADTIME_TOO_LONG: number = 30000;
21
22 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent;
23 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent;
24
25 downloadSpeed: number;
26 error: boolean = false;
27 loading: boolean = false;
28 numPeers: number;
29 player: VideoJSPlayer;
30 playerElement: Element;
31 uploadSpeed: number;
32 video: Video = null;
33 videoNotFound = false;
34
35 private errorTimer: NodeJS.Timer;
36 private sub: any;
37 private torrentInfosInterval: NodeJS.Timer;
38
39 constructor(
40 private elementRef: ElementRef,
41 private ngZone: NgZone,
42 private route: ActivatedRoute,
43 private videoService: VideoService,
44 private metaService: MetaService,
45 private webTorrentService: WebTorrentService
46 ) {}
47
48 ngOnInit() {
49 this.sub = this.route.params.subscribe(routeParams => {
50 let id = routeParams['id'];
51 this.videoService.getVideo(id).subscribe(
52 video => {
53 this.video = video;
54 this.setOpenGraphTags();
55 this.loadVideo();
56 },
57 error => {
58 this.videoNotFound = true;
59 }
60 );
61 });
62
63 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
64
65 const videojsOptions = {
66 controls: true,
67 autoplay: false
68 };
69
70 const self = this;
71 videojs(this.playerElement, videojsOptions, function () {
72 self.player = this;
73 });
74 }
75
76 ngOnDestroy() {
77 // Remove WebTorrent stuff
78 console.log('Removing video from webtorrent.');
79 clearInterval(this.torrentInfosInterval);
80 clearTimeout(this.errorTimer);
81
82 if (this.video !== null) {
83 this.webTorrentService.remove(this.video.magnetUri);
84 }
85
86 // Remove player
87 videojs(this.playerElement).dispose();
88
89 // Unsubscribe route subscription
90 this.sub.unsubscribe();
91 }
92
93 loadVideo() {
94 // Reset the error
95 this.error = false;
96 // We are loading the video
97 this.loading = true;
98
99 console.log('Adding ' + this.video.magnetUri + '.');
100
101 // The callback might never return if there are network issues
102 // So we create a timer to inform the user the load is abnormally long
103 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
104
105 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
106 // Clear the error timer
107 clearTimeout(this.errorTimer);
108 // Maybe the error was fired by the timer, so reset it
109 this.error = false;
110
111 // We are not loading the video anymore
112 this.loading = false;
113
114 console.log('Added ' + this.video.magnetUri + '.');
115 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
116 if (err) {
117 alert('Cannot append the file.');
118 console.error(err);
119 }
120 });
121
122 this.runInProgress(torrent);
123 });
124 }
125
126 showShareModal() {
127 this.videoShareModal.show();
128 }
129
130 showMagnetUriModal() {
131 this.videoMagnetModal.show();
132 }
133
134 private loadTooLong() {
135 this.error = true;
136 console.error('The video load seems to be abnormally long.');
137 }
138
139 private setOpenGraphTags() {
140 this.metaService.setTag('og:type', 'video');
141
142 this.metaService.setTag('og:title', this.video.name);
143 this.metaService.setTag('name', this.video.name);
144
145 this.metaService.setTag('og:description', this.video.description);
146 this.metaService.setTag('description', this.video.description);
147
148 this.metaService.setTag('og:image', this.video.thumbnailPath);
149
150 this.metaService.setTag('og:duration', this.video.duration);
151
152 this.metaService.setTag('og:site_name', 'PeerTube');
153
154 this.metaService.setTag('og:url', window.location.href);
155 this.metaService.setTag('url', window.location.href);
156 }
157
158 private runInProgress(torrent: any) {
159 // Refresh each second
160 this.torrentInfosInterval = setInterval(() => {
161 this.ngZone.run(() => {
162 this.downloadSpeed = torrent.downloadSpeed;
163 this.numPeers = torrent.numPeers;
164 this.uploadSpeed = torrent.uploadSpeed;
165 });
166 }, 1000);
167 }
168 }