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