]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
9a36c17e254bb004ca490ea20d361cdab113298d
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
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 = null;
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 ngOnInit() {
36 this.sub = this.route.params.subscribe(routeParams => {
37 let id = routeParams['id'];
38 this.videoService.getVideo(id).subscribe(
39 video => {
40 this.video = video;
41 this.loadVideo();
42 },
43 error => alert(error.text)
44 );
45 });
46 }
47
48 ngOnDestroy() {
49 console.log('Removing video from webtorrent.');
50 clearInterval(this.torrentInfosInterval);
51 clearTimeout(this.errorTimer);
52 this.webTorrentService.remove(this.video.magnetUri);
53
54 this.sub.unsubscribe();
55 }
56
57 loadVideo() {
58 // Reset the error
59 this.error = false;
60 // We are loading the video
61 this.loading = true;
62
63 console.log('Adding ' + this.video.magnetUri + '.');
64
65 // The callback might never return if there are network issues
66 // So we create a timer to inform the user the load is abnormally long
67 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
68
69 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
70 // Clear the error timer
71 clearTimeout(this.errorTimer);
72 // Maybe the error was fired by the timer, so reset it
73 this.error = false;
74
75 // We are not loading the video anymore
76 this.loading = false;
77
78 console.log('Added ' + this.video.magnetUri + '.');
79 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
80 if (err) {
81 alert('Cannot append the file.');
82 console.error(err);
83 }
84 });
85
86 this.runInProgress(torrent);
87 });
88 }
89
90 private loadTooLong() {
91 this.error = true;
92 console.error('The video load seems to be abnormally long.');
93 }
94
95 private runInProgress(torrent: any) {
96 // Refresh each second
97 this.torrentInfosInterval = setInterval(() => {
98 this.ngZone.run(() => {
99 this.downloadSpeed = torrent.downloadSpeed;
100 this.numPeers = torrent.numPeers;
101 this.uploadSpeed = torrent.uploadSpeed;
102 });
103 }, 1000);
104 }
105 }