]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-watch/video-watch.component.ts
Server: request scheduler refractoring
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
CommitLineData
3154f382 1import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
0629423c 2import { ActivatedRoute } from '@angular/router';
13fc89f4 3import { Subscription } from 'rxjs/Subscription';
8140a704 4
e31f6ad6 5import * as videojs from 'video.js';
7ddd02c9
C
6import { MetaService } from 'ng2-meta';
7import { NotificationsService } from 'angular2-notifications';
3154f382 8
4f8c0eb0 9import { AuthService } from '../../core';
cf02fbfb
C
10import { VideoMagnetComponent } from './video-magnet.component';
11import { VideoShareComponent } from './video-share.component';
4f8c0eb0 12import { VideoReportComponent } from './video-report.component';
ab32b0fc 13import { Video, VideoService } from '../shared';
d3ef341a 14import { WebTorrentService } from './webtorrent.service';
dc8bc31b 15
dc8bc31b
C
16@Component({
17 selector: 'my-video-watch',
ec8d8440
C
18 templateUrl: './video-watch.component.html',
19 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b
C
20})
21
0629423c 22export class VideoWatchComponent implements OnInit, OnDestroy {
0d4fb7e6 23 private static LOADTIME_TOO_LONG: number = 20000;
3ad109e4 24
cf02fbfb
C
25 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent;
26 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent;
4f8c0eb0 27 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent;
3154f382 28
8cfecb2a 29 downloadSpeed: number;
3ad109e4 30 error: boolean = false;
da932efc 31 loading: boolean = false;
4fd8aa32 32 numPeers: number;
e31f6ad6 33 player: VideoJSPlayer;
067e3f84 34 playerElement: Element;
4fd8aa32 35 uploadSpeed: number;
d1992b93 36 video: Video = null;
9c89a45c 37 videoNotFound = false;
dc8bc31b 38
0d4fb7e6 39 private errorTimer: number;
13fc89f4
C
40 private paramsSub: Subscription;
41 private errorsSub: Subscription;
42 private warningsSub: Subscription;
0d4fb7e6 43 private torrentInfosInterval: number;
dc8bc31b
C
44
45 constructor(
4fd8aa32 46 private elementRef: ElementRef,
c323efb9 47 private ngZone: NgZone,
0629423c 48 private route: ActivatedRoute,
d3ef341a 49 private videoService: VideoService,
3ec343a4 50 private metaService: MetaService,
4f8c0eb0 51 private webTorrentService: WebTorrentService,
7ddd02c9
C
52 private authService: AuthService,
53 private notificationsService: NotificationsService
d3ef341a 54 ) {}
dc8bc31b 55
d1992b93 56 ngOnInit() {
13fc89f4 57 this.paramsSub = this.route.params.subscribe(routeParams => {
d1992b93
C
58 let id = routeParams['id'];
59 this.videoService.getVideo(id).subscribe(
60 video => {
61 this.video = video;
3ec343a4 62 this.setOpenGraphTags();
d1992b93
C
63 this.loadVideo();
64 },
9c89a45c
C
65 error => {
66 this.videoNotFound = true;
67 }
d1992b93
C
68 );
69 });
e31f6ad6 70
067e3f84
C
71 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
72
e31f6ad6
C
73 const videojsOptions = {
74 controls: true,
75 autoplay: false
76 };
77
78 const self = this;
067e3f84 79 videojs(this.playerElement, videojsOptions, function () {
e31f6ad6
C
80 self.player = this;
81 });
13fc89f4
C
82
83 this.errorsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.error('Error', err.message));
84 this.warningsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.alert('Warning', err.message));
d1992b93
C
85 }
86
87 ngOnDestroy() {
067e3f84 88 // Remove WebTorrent stuff
d1992b93 89 console.log('Removing video from webtorrent.');
0d4fb7e6
C
90 window.clearInterval(this.torrentInfosInterval);
91 window.clearTimeout(this.errorTimer);
9c89a45c
C
92
93 if (this.video !== null) {
94 this.webTorrentService.remove(this.video.magnetUri);
95 }
d1992b93 96
067e3f84
C
97 // Remove player
98 videojs(this.playerElement).dispose();
99
13fc89f4
C
100 // Unsubscribe subscriptions
101 this.paramsSub.unsubscribe();
102 this.errorsSub.unsubscribe();
103 this.warningsSub.unsubscribe();
d1992b93
C
104 }
105
3ad109e4
C
106 loadVideo() {
107 // Reset the error
108 this.error = false;
109 // We are loading the video
da932efc 110 this.loading = true;
3ad109e4 111
2c4a0b5d 112 console.log('Adding ' + this.video.magnetUri + '.');
d3ef341a 113
3ad109e4
C
114 // The callback might never return if there are network issues
115 // So we create a timer to inform the user the load is abnormally long
0d4fb7e6 116 this.errorTimer = window.setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
3ad109e4 117
d3ef341a 118 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
3ad109e4 119 // Clear the error timer
0d4fb7e6 120 window.clearTimeout(this.errorTimer);
3ad109e4
C
121 // Maybe the error was fired by the timer, so reset it
122 this.error = false;
123
124 // We are not loading the video anymore
da932efc 125 this.loading = false;
3ad109e4 126
2c4a0b5d 127 console.log('Added ' + this.video.magnetUri + '.');
067e3f84 128 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
dc8bc31b 129 if (err) {
7ddd02c9 130 this.notificationsService.error('Error', 'Cannot append the file in the video element.');
dc8bc31b
C
131 console.error(err);
132 }
44124980 133 });
8cfecb2a 134
c323efb9 135 this.runInProgress(torrent);
44124980 136 });
dc8bc31b 137 }
98b01bac 138
4f8c0eb0
C
139 showReportModal(event: Event) {
140 event.preventDefault();
141 this.videoReportModal.show();
142 }
143
99cc4f49 144 showShareModal() {
cf02fbfb 145 this.videoShareModal.show();
99cc4f49
C
146 }
147
cf02fbfb
C
148 showMagnetUriModal() {
149 this.videoMagnetModal.show();
99cc4f49
C
150 }
151
4f8c0eb0
C
152 isUserLoggedIn() {
153 return this.authService.isLoggedIn();
154 }
155
3ad109e4
C
156 private loadTooLong() {
157 this.error = true;
158 console.error('The video load seems to be abnormally long.');
159 }
c323efb9 160
3ec343a4
C
161 private setOpenGraphTags() {
162 this.metaService.setTag('og:type', 'video');
163
164 this.metaService.setTag('og:title', this.video.name);
165 this.metaService.setTag('name', this.video.name);
166
167 this.metaService.setTag('og:description', this.video.description);
168 this.metaService.setTag('description', this.video.description);
169
170 this.metaService.setTag('og:image', this.video.thumbnailPath);
171
172 this.metaService.setTag('og:duration', this.video.duration);
173
174 this.metaService.setTag('og:site_name', 'PeerTube');
175
176 this.metaService.setTag('og:url', window.location.href);
177 this.metaService.setTag('url', window.location.href);
178 }
179
c323efb9
C
180 private runInProgress(torrent: any) {
181 // Refresh each second
0d4fb7e6 182 this.torrentInfosInterval = window.setInterval(() => {
c323efb9
C
183 this.ngZone.run(() => {
184 this.downloadSpeed = torrent.downloadSpeed;
185 this.numPeers = torrent.numPeers;
186 this.uploadSpeed = torrent.uploadSpeed;
187 });
188 }, 1000);
189 }
dc8bc31b 190}