]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, NgZone, OnDestroy, OnInit } from '@angular/core'; | |
2 | import { ActivatedRoute } from '@angular/router'; | |
3 | ||
4 | import { Video, VideoService } from '../shared'; | |
5 | import { WebTorrentService } from './webtorrent.service'; | |
6 | ||
7 | @Component({ | |
8 | selector: 'my-video-watch', | |
9 | templateUrl: './video-watch.component.html', | |
10 | styleUrls: [ './video-watch.component.scss' ] | |
11 | }) | |
12 | ||
13 | export class VideoWatchComponent implements OnInit, OnDestroy { | |
14 | private static LOADTIME_TOO_LONG: number = 30000; | |
15 | ||
16 | downloadSpeed: number; | |
17 | error: boolean = false; | |
18 | loading: boolean = false; | |
19 | numPeers: number; | |
20 | uploadSpeed: number; | |
21 | video: Video; | |
22 | ||
23 | private errorTimer: NodeJS.Timer; | |
24 | private sub: any; | |
25 | private torrentInfosInterval: NodeJS.Timer; | |
26 | ||
27 | constructor( | |
28 | private elementRef: ElementRef, | |
29 | private ngZone: NgZone, | |
30 | private route: ActivatedRoute, | |
31 | private videoService: VideoService, | |
32 | private webTorrentService: WebTorrentService | |
33 | ) {} | |
34 | ||
35 | loadVideo() { | |
36 | // Reset the error | |
37 | this.error = false; | |
38 | // We are loading the video | |
39 | this.loading = true; | |
40 | ||
41 | console.log('Adding ' + this.video.magnetUri + '.'); | |
42 | ||
43 | // The callback might never return if there are network issues | |
44 | // So we create a timer to inform the user the load is abnormally long | |
45 | this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG); | |
46 | ||
47 | this.webTorrentService.add(this.video.magnetUri, (torrent) => { | |
48 | // Clear the error timer | |
49 | clearTimeout(this.errorTimer); | |
50 | // Maybe the error was fired by the timer, so reset it | |
51 | this.error = false; | |
52 | ||
53 | // We are not loading the video anymore | |
54 | this.loading = false; | |
55 | ||
56 | console.log('Added ' + this.video.magnetUri + '.'); | |
57 | torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { | |
58 | if (err) { | |
59 | alert('Cannot append the file.'); | |
60 | console.error(err); | |
61 | } | |
62 | }); | |
63 | ||
64 | this.runInProgress(torrent); | |
65 | }); | |
66 | } | |
67 | ||
68 | ngOnDestroy() { | |
69 | console.log('Removing video from webtorrent.'); | |
70 | clearInterval(this.torrentInfosInterval); | |
71 | this.webTorrentService.remove(this.video.magnetUri); | |
72 | ||
73 | this.sub.unsubscribe(); | |
74 | } | |
75 | ||
76 | ngOnInit() { | |
77 | this.sub = this.route.params.subscribe(routeParams => { | |
78 | let id = routeParams['id']; | |
79 | this.videoService.getVideo(id).subscribe( | |
80 | video => { | |
81 | this.video = video; | |
82 | this.loadVideo(); | |
83 | }, | |
84 | error => alert(error.text) | |
85 | ); | |
86 | }); | |
87 | } | |
88 | ||
89 | private loadTooLong() { | |
90 | this.error = true; | |
91 | console.error('The video load seems to be abnormally long.'); | |
92 | } | |
93 | ||
94 | private runInProgress(torrent: any) { | |
95 | // Refresh each second | |
96 | this.torrentInfosInterval = setInterval(() => { | |
97 | this.ngZone.run(() => { | |
98 | this.downloadSpeed = torrent.downloadSpeed; | |
99 | this.numPeers = torrent.numPeers; | |
100 | this.uploadSpeed = torrent.uploadSpeed; | |
101 | }); | |
102 | }, 1000); | |
103 | } | |
104 | } |