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