aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/angular/videos/components/watch/videos-watch.component.ts
blob: 3d1829b99047fb057cad6ce60412aaaa59d7846c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Component, OnInit, ElementRef } from '@angular/core';
import { RouteParams, CanDeactivate, ComponentInstruction } from '@angular/router-deprecated';
import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe';

// TODO import it with systemjs
declare var WebTorrent: any;

import { Video } from '../../models/video';
import { VideosService } from '../../services/videos.service';

@Component({
  selector: 'my-video-watch',
  templateUrl: 'app/angular/videos/components/watch/videos-watch.component.html',
  styleUrls: [ 'app/angular/videos/components/watch/videos-watch.component.css' ],
  pipes: [ BytesPipe ]
})

export class VideosWatchComponent implements OnInit, CanDeactivate {
  video: Video;
  downloadSpeed: number;
  uploadSpeed: number;
  numPeers: number;
  loading: boolean = false;

  private _interval: NodeJS.Timer;
  private client: any;

  constructor(
    private _videosService: VideosService,
    private _routeParams: RouteParams,
    private _elementRef: ElementRef
  ) {
    // TODO: use a service
    this.client = new WebTorrent({ dht: false });
  }

  ngOnInit() {
    let id = this._routeParams.get('id');
    this._videosService.getVideo(id).subscribe(
      video => this.loadVideo(video),
      error => alert(error)
    );
  }

  loadVideo(video: Video) {
    this.loading = true;
    this.video = video;
    console.log('Adding ' + this.video.magnetUri + '.');
    this.client.add(this.video.magnetUri, (torrent) => {
      this.loading = false;
      console.log('Added ' + this.video.magnetUri + '.');
      torrent.files[0].appendTo(this._elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
        if (err) {
          alert('Cannot append the file.');
          console.error(err);
        }
      });

      // Refresh each second
      this._interval = setInterval(() => {
        this.downloadSpeed = torrent.downloadSpeed;
        this.uploadSpeed = torrent.uploadSpeed;
        this.numPeers = torrent.numPeers;
      }, 1000);
    });
  }

  routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
    console.log('Removing video from webtorrent.');
    clearInterval(this._interval);
    this.client.remove(this.video.magnetUri);
    return true;
  }
}