]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
0309938c0d3bfecb902fcc5342d86eca215fe9f3
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
3
4 import { ModalDirective } from 'ng2-bootstrap/components/modal';
5 import { MetaService } from 'ng2-meta';
6 import * as videojs from 'video.js';
7
8 import { Video, VideoService } from '../shared';
9 import { WebTorrentService } from './webtorrent.service';
10
11 @Component({
12 selector: 'my-video-watch',
13 templateUrl: './video-watch.component.html',
14 styleUrls: [ './video-watch.component.scss' ]
15 })
16
17 export class VideoWatchComponent implements OnInit, OnDestroy {
18 private static LOADTIME_TOO_LONG: number = 30000;
19
20 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
21 @ViewChild('shareModal') shareModal: ModalDirective;
22
23 downloadSpeed: number;
24 error: boolean = false;
25 loading: boolean = false;
26 numPeers: number;
27 player: VideoJSPlayer;
28 playerElement: Element;
29 uploadSpeed: number;
30 video: Video = null;
31
32 private errorTimer: NodeJS.Timer;
33 private sub: any;
34 private torrentInfosInterval: NodeJS.Timer;
35
36 constructor(
37 private elementRef: ElementRef,
38 private ngZone: NgZone,
39 private route: ActivatedRoute,
40 private videoService: VideoService,
41 private metaService: MetaService,
42 private webTorrentService: WebTorrentService
43 ) {}
44
45 ngOnInit() {
46 this.sub = this.route.params.subscribe(routeParams => {
47 let id = routeParams['id'];
48 this.videoService.getVideo(id).subscribe(
49 video => {
50 this.video = video;
51 this.setOpenGraphTags();
52 this.loadVideo();
53 },
54 error => alert(error.text)
55 );
56 });
57
58 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
59
60 const videojsOptions = {
61 controls: true,
62 autoplay: false
63 };
64
65 const self = this;
66 videojs(this.playerElement, videojsOptions, function () {
67 self.player = this;
68 });
69 }
70
71 ngOnDestroy() {
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);
77
78 // Remove player
79 videojs(this.playerElement).dispose();
80
81 // Unsubscribe route subscription
82 this.sub.unsubscribe();
83 }
84
85 loadVideo() {
86 // Reset the error
87 this.error = false;
88 // We are loading the video
89 this.loading = true;
90
91 console.log('Adding ' + this.video.magnetUri + '.');
92
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);
96
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
101 this.error = false;
102
103 // We are not loading the video anymore
104 this.loading = false;
105
106 console.log('Added ' + this.video.magnetUri + '.');
107 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
108 if (err) {
109 alert('Cannot append the file.');
110 console.error(err);
111 }
112 });
113
114 this.runInProgress(torrent);
115 });
116 }
117
118 showMagnetUriModal() {
119 this.magnetUriModal.show();
120 }
121
122 hideMagnetUriModal() {
123 this.magnetUriModal.hide();
124 }
125
126 showShareModal() {
127 this.shareModal.show();
128 }
129
130 hideShareModal() {
131 this.shareModal.hide();
132 }
133
134 getVideoIframeCode() {
135 return '<iframe width="560" height="315" ' +
136 'src="' + window.location.origin + '/videos/embed/' + this.video.id + '" ' +
137 'frameborder="0" allowfullscreen>' +
138 '</iframe>';
139 }
140
141 getVideoUrl() {
142 return window.location.href;
143 }
144
145 private loadTooLong() {
146 this.error = true;
147 console.error('The video load seems to be abnormally long.');
148 }
149
150 private setOpenGraphTags() {
151 this.metaService.setTag('og:type', 'video');
152
153 this.metaService.setTag('og:title', this.video.name);
154 this.metaService.setTag('name', this.video.name);
155
156 this.metaService.setTag('og:description', this.video.description);
157 this.metaService.setTag('description', this.video.description);
158
159 this.metaService.setTag('og:image', this.video.thumbnailPath);
160
161 this.metaService.setTag('og:duration', this.video.duration);
162
163 this.metaService.setTag('og:site_name', 'PeerTube');
164
165 this.metaService.setTag('og:url', window.location.href);
166 this.metaService.setTag('url', window.location.href);
167 }
168
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;
176 });
177 }, 1000);
178 }
179 }