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