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