]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
3dab2bbb7f1432262bdec720d65f497b72528497
[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 videoNotFound = false;
32
33 private errorTimer: NodeJS.Timer;
34 private sub: any;
35 private torrentInfosInterval: NodeJS.Timer;
36
37 constructor(
38 private elementRef: ElementRef,
39 private ngZone: NgZone,
40 private route: ActivatedRoute,
41 private videoService: VideoService,
42 private metaService: MetaService,
43 private webTorrentService: WebTorrentService
44 ) {}
45
46 ngOnInit() {
47 this.sub = this.route.params.subscribe(routeParams => {
48 let id = routeParams['id'];
49 this.videoService.getVideo(id).subscribe(
50 video => {
51 this.video = video;
52 this.setOpenGraphTags();
53 this.loadVideo();
54 },
55 error => {
56 this.videoNotFound = true;
57 }
58 );
59 });
60
61 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
62
63 const videojsOptions = {
64 controls: true,
65 autoplay: false
66 };
67
68 const self = this;
69 videojs(this.playerElement, videojsOptions, function () {
70 self.player = this;
71 });
72 }
73
74 ngOnDestroy() {
75 // Remove WebTorrent stuff
76 console.log('Removing video from webtorrent.');
77 clearInterval(this.torrentInfosInterval);
78 clearTimeout(this.errorTimer);
79
80 if (this.video !== null) {
81 this.webTorrentService.remove(this.video.magnetUri);
82 }
83
84 // Remove player
85 videojs(this.playerElement).dispose();
86
87 // Unsubscribe route subscription
88 this.sub.unsubscribe();
89 }
90
91 loadVideo() {
92 // Reset the error
93 this.error = false;
94 // We are loading the video
95 this.loading = true;
96
97 console.log('Adding ' + this.video.magnetUri + '.');
98
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);
102
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
107 this.error = false;
108
109 // We are not loading the video anymore
110 this.loading = false;
111
112 console.log('Added ' + this.video.magnetUri + '.');
113 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
114 if (err) {
115 alert('Cannot append the file.');
116 console.error(err);
117 }
118 });
119
120 this.runInProgress(torrent);
121 });
122 }
123
124 showMagnetUriModal() {
125 this.magnetUriModal.show();
126 }
127
128 hideMagnetUriModal() {
129 this.magnetUriModal.hide();
130 }
131
132 showShareModal() {
133 this.shareModal.show();
134 }
135
136 hideShareModal() {
137 this.shareModal.hide();
138 }
139
140 getVideoIframeCode() {
141 return '<iframe width="560" height="315" ' +
142 'src="' + window.location.origin + '/videos/embed/' + this.video.id + '" ' +
143 'frameborder="0" allowfullscreen>' +
144 '</iframe>';
145 }
146
147 getVideoUrl() {
148 return window.location.href;
149 }
150
151 private loadTooLong() {
152 this.error = true;
153 console.error('The video load seems to be abnormally long.');
154 }
155
156 private setOpenGraphTags() {
157 this.metaService.setTag('og:type', 'video');
158
159 this.metaService.setTag('og:title', this.video.name);
160 this.metaService.setTag('name', this.video.name);
161
162 this.metaService.setTag('og:description', this.video.description);
163 this.metaService.setTag('description', this.video.description);
164
165 this.metaService.setTag('og:image', this.video.thumbnailPath);
166
167 this.metaService.setTag('og:duration', this.video.duration);
168
169 this.metaService.setTag('og:site_name', 'PeerTube');
170
171 this.metaService.setTag('og:url', window.location.href);
172 this.metaService.setTag('url', window.location.href);
173 }
174
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;
182 });
183 }, 1000);
184 }
185 }