diff options
Diffstat (limited to 'client/src/app/videos/video-watch')
5 files changed, 123 insertions, 0 deletions
diff --git a/client/src/app/videos/video-watch/index.ts b/client/src/app/videos/video-watch/index.ts new file mode 100644 index 000000000..b17aaacf2 --- /dev/null +++ b/client/src/app/videos/video-watch/index.ts | |||
@@ -0,0 +1,2 @@ | |||
1 | export * from './video-watch.component'; | ||
2 | export * from './webtorrent.service'; | ||
diff --git a/client/src/app/videos/video-watch/video-watch.component.html b/client/src/app/videos/video-watch/video-watch.component.html new file mode 100644 index 000000000..6c36b27e2 --- /dev/null +++ b/client/src/app/videos/video-watch/video-watch.component.html | |||
@@ -0,0 +1,10 @@ | |||
1 | <my-loader [loading]="loading"></my-loader> | ||
2 | |||
3 | <div class="embed-responsive embed-responsive-19by9"> | ||
4 | </div> | ||
5 | |||
6 | <div id="torrent-info"> | ||
7 | <div id="torrent-info-download">Download: {{ downloadSpeed | bytes }}/s</div> | ||
8 | <div id="torrent-info-upload">Upload: {{ uploadSpeed | bytes }}/s</div> | ||
9 | <div id="torrent-info-peers">Number of peers: {{ numPeers }}</div> | ||
10 | <div> | ||
diff --git a/client/src/app/videos/video-watch/video-watch.component.scss b/client/src/app/videos/video-watch/video-watch.component.scss new file mode 100644 index 000000000..1228d42f4 --- /dev/null +++ b/client/src/app/videos/video-watch/video-watch.component.scss | |||
@@ -0,0 +1,13 @@ | |||
1 | .embed-responsive { | ||
2 | height: 500px; | ||
3 | } | ||
4 | |||
5 | #torrent-info { | ||
6 | font-size: 10px; | ||
7 | |||
8 | div { | ||
9 | display: inline-block; | ||
10 | width: 33%; | ||
11 | text-align: center; | ||
12 | } | ||
13 | } | ||
diff --git a/client/src/app/videos/video-watch/video-watch.component.ts b/client/src/app/videos/video-watch/video-watch.component.ts new file mode 100644 index 000000000..db82283b4 --- /dev/null +++ b/client/src/app/videos/video-watch/video-watch.component.ts | |||
@@ -0,0 +1,72 @@ | |||
1 | import { Component, ElementRef, OnInit } from '@angular/core'; | ||
2 | import { CanDeactivate, ComponentInstruction, RouteParams } from '@angular/router-deprecated'; | ||
3 | |||
4 | import { BytesPipe } from 'angular-pipes/src/math/bytes.pipe'; | ||
5 | |||
6 | import { LoaderComponent, Video, VideoService } from '../shared'; | ||
7 | import { WebTorrentService } from './webtorrent.service'; | ||
8 | |||
9 | @Component({ | ||
10 | selector: 'my-video-watch', | ||
11 | template: require('./video-watch.component.html'), | ||
12 | styles: [ require('./video-watch.component.scss') ], | ||
13 | providers: [ WebTorrentService ], | ||
14 | directives: [ LoaderComponent ], | ||
15 | pipes: [ BytesPipe ] | ||
16 | }) | ||
17 | |||
18 | export class VideoWatchComponent implements OnInit, CanDeactivate { | ||
19 | downloadSpeed: number; | ||
20 | loading: boolean = false; | ||
21 | numPeers: number; | ||
22 | uploadSpeed: number; | ||
23 | video: Video; | ||
24 | |||
25 | private interval: NodeJS.Timer; | ||
26 | |||
27 | constructor( | ||
28 | private elementRef: ElementRef, | ||
29 | private routeParams: RouteParams, | ||
30 | private videoService: VideoService, | ||
31 | private webTorrentService: WebTorrentService | ||
32 | ) {} | ||
33 | |||
34 | loadVideo(video: Video) { | ||
35 | this.loading = true; | ||
36 | this.video = video; | ||
37 | console.log('Adding ' + this.video.magnetUri + '.'); | ||
38 | |||
39 | this.webTorrentService.add(this.video.magnetUri, (torrent) => { | ||
40 | this.loading = false; | ||
41 | console.log('Added ' + this.video.magnetUri + '.'); | ||
42 | torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => { | ||
43 | if (err) { | ||
44 | alert('Cannot append the file.'); | ||
45 | console.error(err); | ||
46 | } | ||
47 | }); | ||
48 | |||
49 | // Refresh each second | ||
50 | this.interval = setInterval(() => { | ||
51 | this.downloadSpeed = torrent.downloadSpeed; | ||
52 | this.numPeers = torrent.numPeers; | ||
53 | this.uploadSpeed = torrent.uploadSpeed; | ||
54 | }, 1000); | ||
55 | }); | ||
56 | } | ||
57 | |||
58 | ngOnInit() { | ||
59 | let id = this.routeParams.get('id'); | ||
60 | this.videoService.getVideo(id).subscribe( | ||
61 | video => this.loadVideo(video), | ||
62 | error => alert(error) | ||
63 | ); | ||
64 | } | ||
65 | |||
66 | routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) { | ||
67 | console.log('Removing video from webtorrent.'); | ||
68 | clearInterval(this.interval); | ||
69 | this.webTorrentService.remove(this.video.magnetUri); | ||
70 | return true; | ||
71 | } | ||
72 | } | ||
diff --git a/client/src/app/videos/video-watch/webtorrent.service.ts b/client/src/app/videos/video-watch/webtorrent.service.ts new file mode 100644 index 000000000..bf38b5aaa --- /dev/null +++ b/client/src/app/videos/video-watch/webtorrent.service.ts | |||
@@ -0,0 +1,26 @@ | |||
1 | // Don't use webtorrent typings for now | ||
2 | // It misses some little things I'll fix later | ||
3 | // <reference path="../../../../typings/globals/webtorrent/index.d.ts" /> | ||
4 | |||
5 | import { Injectable } from '@angular/core'; | ||
6 | |||
7 | // import WebTorrent = require('webtorrent'); | ||
8 | declare var WebTorrent: any; | ||
9 | |||
10 | @Injectable() | ||
11 | export class WebTorrentService { | ||
12 | // private client: WebTorrent.Client; | ||
13 | private client: any; | ||
14 | |||
15 | constructor() { | ||
16 | this.client = new WebTorrent({ dht: false }); | ||
17 | } | ||
18 | |||
19 | add(magnetUri: string, callback: Function) { | ||
20 | return this.client.add(magnetUri, callback); | ||
21 | } | ||
22 | |||
23 | remove(magnetUri: string) { | ||
24 | return this.client.remove(magnetUri); | ||
25 | } | ||
26 | } | ||