]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'; | |
2 | import { ActivatedRoute } from '@angular/router'; | |
3 | import { Subscription } from 'rxjs/Subscription'; | |
4 | ||
5 | import * as videojs from 'video.js'; | |
6 | import { MetaService } from 'ng2-meta'; | |
7 | import { NotificationsService } from 'angular2-notifications'; | |
8 | ||
9 | import { AuthService } from '../../core'; | |
10 | import { VideoMagnetComponent } from './video-magnet.component'; | |
11 | import { VideoShareComponent } from './video-share.component'; | |
12 | import { VideoReportComponent } from './video-report.component'; | |
13 | import { RateType, Video, VideoService } from '../shared'; | |
14 | import { WebTorrentService } from './webtorrent.service'; | |
15 | ||
16 | @Component({ | |
17 | selector: 'my-video-watch', | |
18 | templateUrl: './video-watch.component.html', | |
19 | styleUrls: [ './video-watch.component.scss' ] | |
20 | }) | |
21 | ||
22 | export class VideoWatchComponent implements OnInit, OnDestroy { | |
23 | private static LOADTIME_TOO_LONG: number = 20000; | |
24 | ||
25 | @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent; | |
26 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent; | |
27 | @ViewChild('videoReportModal') videoReportModal: VideoReportComponent; | |
28 | ||
29 | downloadSpeed: number; | |
30 | error: boolean = false; | |
31 | loading: boolean = false; | |
32 | numPeers: number; | |
33 | player: VideoJSPlayer; | |
34 | playerElement: Element; | |
35 | uploadSpeed: number; | |
36 | userRating: RateType = null; | |
37 | video: Video = null; | |
38 | videoNotFound = false; | |
39 | ||
40 | private errorTimer: number; | |
41 | private paramsSub: Subscription; | |
42 | private errorsSub: Subscription; | |
43 | private warningsSub: Subscription; | |
44 | private torrentInfosInterval: number; | |
45 | ||
46 | constructor( | |
47 | private elementRef: ElementRef, | |
48 | private ngZone: NgZone, | |
49 | private route: ActivatedRoute, | |
50 | private videoService: VideoService, | |
51 | private metaService: MetaService, | |
52 | private webTorrentService: WebTorrentService, | |
53 | private authService: AuthService, | |
54 | private notificationsService: NotificationsService | |
55 | ) {} | |
56 | ||
57 | ngOnInit() { | |
58 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
59 | let id = routeParams['id']; | |
60 | this.videoService.getVideo(id).subscribe( | |
61 | video => { | |
62 | this.video = video; | |
63 | this.setOpenGraphTags(); | |
64 | this.loadVideo(); | |
65 | this.checkUserRating(); | |
66 | }, | |
67 | error => { | |
68 | this.videoNotFound = true; | |
69 | } | |
70 | ); | |
71 | }); | |
72 | ||
73 | this.playerElement = this.elementRef.nativeElement.querySelector('#video-container'); | |
74 | ||
75 | const videojsOptions = { | |
76 | controls: true, | |
77 | autoplay: false | |
78 | }; | |
79 | ||
80 | const self = this; | |
81 | videojs(this.playerElement, videojsOptions, function () { | |
82 | self.player = this; | |
83 | }); | |
84 | ||
85 | this.errorsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.error('Error', err.message)); | |
86 | this.warningsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.alert('Warning', err.message)); | |
87 | } | |
88 | ||
89 | ngOnDestroy() { | |
90 | // Remove WebTorrent stuff | |
91 | console.log('Removing video from webtorrent.'); | |
92 | window.clearInterval(this.torrentInfosInterval); | |
93 | window.clearTimeout(this.errorTimer); | |
94 | ||
95 | if (this.video !== null) { | |
96 | this.webTorrentService.remove(this.video.magnetUri); | |
97 | } | |
98 | ||
99 | // Remove player | |
100 | videojs(this.playerElement).dispose(); | |
101 | ||
102 | // Unsubscribe subscriptions | |
103 | this.paramsSub.unsubscribe(); | |
104 | this.errorsSub.unsubscribe(); | |
105 | this.warningsSub.unsubscribe(); | |
106 | } | |
107 | ||
108 | loadVideo() { | |
109 | // Reset the error | |
110 | this.error = false; | |
111 | // We are loading the video | |
112 | this.loading = true; | |
113 | ||
114 | console.log('Adding ' + this.video.magnetUri + '.'); | |
115 | ||
116 | // The callback might never return if there are network issues | |
117 | // So we create a timer to inform the user the load is abnormally long | |
118 | this.errorTimer = window.setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG); | |
119 | ||
120 | this.webTorrentService.add(this.video.magnetUri, (torrent) => { | |
121 | // Clear the error timer | |
122 | window.clearTimeout(this.errorTimer); | |
123 | // Maybe the error was fired by the timer, so reset it | |
124 | this.error = false; | |
125 | ||
126 | // We are not loading the video anymore | |
127 | this.loading = false; | |
128 | ||
129 | console.log('Added ' + this.video.magnetUri + '.'); | |
130 | torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => { | |
131 | if (err) { | |
132 | this.notificationsService.error('Error', 'Cannot append the file in the video element.'); | |
133 | console.error(err); | |
134 | } | |
135 | }); | |
136 | ||
137 | this.runInProgress(torrent); | |
138 | }); | |
139 | } | |
140 | ||
141 | setLike() { | |
142 | if (this.isUserLoggedIn() === false) return; | |
143 | // Already liked this video | |
144 | if (this.userRating === 'like') return; | |
145 | ||
146 | this.videoService.setVideoLike(this.video.id) | |
147 | .subscribe( | |
148 | () => { | |
149 | // Update the video like attribute | |
150 | this.updateVideoRating(this.userRating, 'like'); | |
151 | this.userRating = 'like'; | |
152 | }, | |
153 | ||
154 | err => this.notificationsService.error('Error', err.text) | |
155 | ); | |
156 | } | |
157 | ||
158 | setDislike() { | |
159 | if (this.isUserLoggedIn() === false) return; | |
160 | // Already disliked this video | |
161 | if (this.userRating === 'dislike') return; | |
162 | ||
163 | this.videoService.setVideoDislike(this.video.id) | |
164 | .subscribe( | |
165 | () => { | |
166 | // Update the video dislike attribute | |
167 | this.updateVideoRating(this.userRating, 'dislike'); | |
168 | this.userRating = 'dislike'; | |
169 | }, | |
170 | ||
171 | err => this.notificationsService.error('Error', err.text) | |
172 | ); | |
173 | } | |
174 | ||
175 | showReportModal(event: Event) { | |
176 | event.preventDefault(); | |
177 | this.videoReportModal.show(); | |
178 | } | |
179 | ||
180 | showShareModal() { | |
181 | this.videoShareModal.show(); | |
182 | } | |
183 | ||
184 | showMagnetUriModal(event: Event) { | |
185 | event.preventDefault(); | |
186 | this.videoMagnetModal.show(); | |
187 | } | |
188 | ||
189 | isUserLoggedIn() { | |
190 | return this.authService.isLoggedIn(); | |
191 | } | |
192 | ||
193 | private checkUserRating() { | |
194 | // Unlogged users do not have ratings | |
195 | if (this.isUserLoggedIn() === false) return; | |
196 | ||
197 | this.videoService.getUserVideoRating(this.video.id) | |
198 | .subscribe( | |
199 | ratingObject => { | |
200 | if (ratingObject) { | |
201 | this.userRating = ratingObject.rating; | |
202 | } | |
203 | }, | |
204 | ||
205 | err => this.notificationsService.error('Error', err.text) | |
206 | ); | |
207 | } | |
208 | ||
209 | private updateVideoRating(oldRating: RateType, newRating: RateType) { | |
210 | let likesToIncrement = 0; | |
211 | let dislikesToIncrement = 0; | |
212 | ||
213 | if (oldRating) { | |
214 | if (oldRating === 'like') likesToIncrement--; | |
215 | if (oldRating === 'dislike') dislikesToIncrement--; | |
216 | } | |
217 | ||
218 | if (newRating === 'like') likesToIncrement++; | |
219 | if (newRating === 'dislike') dislikesToIncrement++; | |
220 | ||
221 | this.video.likes += likesToIncrement; | |
222 | this.video.dislikes += dislikesToIncrement; | |
223 | } | |
224 | ||
225 | private loadTooLong() { | |
226 | this.error = true; | |
227 | console.error('The video load seems to be abnormally long.'); | |
228 | } | |
229 | ||
230 | private setOpenGraphTags() { | |
231 | this.metaService.setTag('og:type', 'video'); | |
232 | ||
233 | this.metaService.setTag('og:title', this.video.name); | |
234 | this.metaService.setTag('name', this.video.name); | |
235 | ||
236 | this.metaService.setTag('og:description', this.video.description); | |
237 | this.metaService.setTag('description', this.video.description); | |
238 | ||
239 | this.metaService.setTag('og:image', this.video.thumbnailPath); | |
240 | ||
241 | this.metaService.setTag('og:duration', this.video.duration); | |
242 | ||
243 | this.metaService.setTag('og:site_name', 'PeerTube'); | |
244 | ||
245 | this.metaService.setTag('og:url', window.location.href); | |
246 | this.metaService.setTag('url', window.location.href); | |
247 | } | |
248 | ||
249 | private runInProgress(torrent: any) { | |
250 | // Refresh each second | |
251 | this.torrentInfosInterval = window.setInterval(() => { | |
252 | this.ngZone.run(() => { | |
253 | this.downloadSpeed = torrent.downloadSpeed; | |
254 | this.numPeers = torrent.numPeers; | |
255 | this.uploadSpeed = torrent.uploadSpeed; | |
256 | }); | |
257 | }, 1000); | |
258 | } | |
259 | } |