]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
6012daa5c833cc42bd438ff4f938022195cb49a6
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
2 import { ActivatedRoute } from '@angular/router';
3
4 import { ModalDirective } from 'ng2-bootstrap/components/modal';
5 import { MetaService } from 'ng2-meta';
6 import * as videojs from 'video.js';
7
8 import { Video, VideoService } from '../shared';
9 import { WebTorrentService } from './webtorrent.service';
10
11 @Component({
12 selector: 'my-video-watch',
13 templateUrl: './video-watch.component.html',
14 styleUrls: [ './video-watch.component.scss' ]
15 })
16
17 export class VideoWatchComponent implements OnInit, OnDestroy {
18 private static LOADTIME_TOO_LONG: number = 30000;
19
20 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
21
22 downloadSpeed: number;
23 error: boolean = false;
24 loading: boolean = false;
25 numPeers: number;
26 player: VideoJSPlayer;
27 uploadSpeed: number;
28 video: Video = null;
29
30 private errorTimer: NodeJS.Timer;
31 private sub: any;
32 private torrentInfosInterval: NodeJS.Timer;
33
34 constructor(
35 private elementRef: ElementRef,
36 private ngZone: NgZone,
37 private route: ActivatedRoute,
38 private videoService: VideoService,
39 private metaService: MetaService,
40 private webTorrentService: WebTorrentService
41 ) {}
42
43 ngOnInit() {
44 this.sub = this.route.params.subscribe(routeParams => {
45 let id = routeParams['id'];
46 this.videoService.getVideo(id).subscribe(
47 video => {
48 this.video = video;
49 this.setOpenGraphTags();
50 this.loadVideo();
51 },
52 error => alert(error.text)
53 );
54 });
55
56 const videojsOptions = {
57 controls: true,
58 autoplay: false
59 };
60
61 const self = this;
62 videojs('video-container', videojsOptions, function () {
63 self.player = this;
64 });
65 }
66
67 ngOnDestroy() {
68 console.log('Removing video from webtorrent.');
69 clearInterval(this.torrentInfosInterval);
70 clearTimeout(this.errorTimer);
71 this.webTorrentService.remove(this.video.magnetUri);
72
73 this.sub.unsubscribe();
74 }
75
76 loadVideo() {
77 console.log('<iframe width="560" height="315" src="' + window.location.origin + '/videos/embed/' + this.video.id + '" frameborder="0" allowfullscreen></iframe>');
78
79 // Reset the error
80 this.error = false;
81 // We are loading the video
82 this.loading = true;
83
84 console.log('Adding ' + this.video.magnetUri + '.');
85
86 // The callback might never return if there are network issues
87 // So we create a timer to inform the user the load is abnormally long
88 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
89
90 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
91 // Clear the error timer
92 clearTimeout(this.errorTimer);
93 // Maybe the error was fired by the timer, so reset it
94 this.error = false;
95
96 // We are not loading the video anymore
97 this.loading = false;
98
99 console.log('Added ' + this.video.magnetUri + '.');
100 torrent.files[0].renderTo('#video-container video', { autoplay: true }, (err) => {
101 if (err) {
102 alert('Cannot append the file.');
103 console.error(err);
104 }
105 });
106
107 this.runInProgress(torrent);
108 });
109 }
110
111 showMagnetUriModal() {
112 this.magnetUriModal.show();
113 }
114
115 hideMagnetUriModal() {
116 this.magnetUriModal.hide();
117 }
118
119 private loadTooLong() {
120 this.error = true;
121 console.error('The video load seems to be abnormally long.');
122 }
123
124 private setOpenGraphTags() {
125 this.metaService.setTag('og:type', 'video');
126
127 this.metaService.setTag('og:title', this.video.name);
128 this.metaService.setTag('name', this.video.name);
129
130 this.metaService.setTag('og:description', this.video.description);
131 this.metaService.setTag('description', this.video.description);
132
133 this.metaService.setTag('og:image', this.video.thumbnailPath);
134
135 this.metaService.setTag('og:duration', this.video.duration);
136
137 this.metaService.setTag('og:site_name', 'PeerTube');
138
139 this.metaService.setTag('og:url', window.location.href);
140 this.metaService.setTag('url', window.location.href);
141 }
142
143 private runInProgress(torrent: any) {
144 // Refresh each second
145 this.torrentInfosInterval = setInterval(() => {
146 this.ngZone.run(() => {
147 this.downloadSpeed = torrent.downloadSpeed;
148 this.numPeers = torrent.numPeers;
149 this.uploadSpeed = torrent.uploadSpeed;
150 });
151 }, 1000);
152 }
153 }