]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-watch/video-watch.component.ts
Adapt requests controller/front to new informations
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / video-watch / video-watch.component.ts
CommitLineData
c16ce1de 1import { setInterval, setTimeout } from 'timers'
3154f382 2import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core';
0629423c 3import { ActivatedRoute } from '@angular/router';
8140a704 4
cdcbc810 5import { MetaService } from 'ng2-meta';
e31f6ad6 6import * as videojs from 'video.js';
3154f382 7
cf02fbfb
C
8import { VideoMagnetComponent } from './video-magnet.component';
9import { VideoShareComponent } from './video-share.component';
ab32b0fc 10import { Video, VideoService } from '../shared';
d3ef341a 11import { WebTorrentService } from './webtorrent.service';
dc8bc31b 12
dc8bc31b
C
13@Component({
14 selector: 'my-video-watch',
ec8d8440
C
15 templateUrl: './video-watch.component.html',
16 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b
C
17})
18
0629423c 19export class VideoWatchComponent implements OnInit, OnDestroy {
3ad109e4
C
20 private static LOADTIME_TOO_LONG: number = 30000;
21
cf02fbfb
C
22 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent;
23 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent;
3154f382 24
8cfecb2a 25 downloadSpeed: number;
3ad109e4 26 error: boolean = false;
da932efc 27 loading: boolean = false;
4fd8aa32 28 numPeers: number;
e31f6ad6 29 player: VideoJSPlayer;
067e3f84 30 playerElement: Element;
4fd8aa32 31 uploadSpeed: number;
d1992b93 32 video: Video = null;
9c89a45c 33 videoNotFound = false;
dc8bc31b 34
3ad109e4 35 private errorTimer: NodeJS.Timer;
0629423c 36 private sub: any;
3ad109e4 37 private torrentInfosInterval: NodeJS.Timer;
dc8bc31b
C
38
39 constructor(
4fd8aa32 40 private elementRef: ElementRef,
c323efb9 41 private ngZone: NgZone,
0629423c 42 private route: ActivatedRoute,
d3ef341a 43 private videoService: VideoService,
3ec343a4 44 private metaService: MetaService,
d3ef341a
C
45 private webTorrentService: WebTorrentService
46 ) {}
dc8bc31b 47
d1992b93
C
48 ngOnInit() {
49 this.sub = this.route.params.subscribe(routeParams => {
50 let id = routeParams['id'];
51 this.videoService.getVideo(id).subscribe(
52 video => {
53 this.video = video;
3ec343a4 54 this.setOpenGraphTags();
d1992b93
C
55 this.loadVideo();
56 },
9c89a45c
C
57 error => {
58 this.videoNotFound = true;
59 }
d1992b93
C
60 );
61 });
e31f6ad6 62
067e3f84
C
63 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
64
e31f6ad6
C
65 const videojsOptions = {
66 controls: true,
67 autoplay: false
68 };
69
70 const self = this;
067e3f84 71 videojs(this.playerElement, videojsOptions, function () {
e31f6ad6
C
72 self.player = this;
73 });
d1992b93
C
74 }
75
76 ngOnDestroy() {
067e3f84 77 // Remove WebTorrent stuff
d1992b93
C
78 console.log('Removing video from webtorrent.');
79 clearInterval(this.torrentInfosInterval);
80 clearTimeout(this.errorTimer);
9c89a45c
C
81
82 if (this.video !== null) {
83 this.webTorrentService.remove(this.video.magnetUri);
84 }
d1992b93 85
067e3f84
C
86 // Remove player
87 videojs(this.playerElement).dispose();
88
89 // Unsubscribe route subscription
d1992b93
C
90 this.sub.unsubscribe();
91 }
92
3ad109e4
C
93 loadVideo() {
94 // Reset the error
95 this.error = false;
96 // We are loading the video
da932efc 97 this.loading = true;
3ad109e4 98
2c4a0b5d 99 console.log('Adding ' + this.video.magnetUri + '.');
d3ef341a 100
3ad109e4
C
101 // The callback might never return if there are network issues
102 // So we create a timer to inform the user the load is abnormally long
103 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
104
d3ef341a 105 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
3ad109e4
C
106 // Clear the error timer
107 clearTimeout(this.errorTimer);
108 // Maybe the error was fired by the timer, so reset it
109 this.error = false;
110
111 // We are not loading the video anymore
da932efc 112 this.loading = false;
3ad109e4 113
2c4a0b5d 114 console.log('Added ' + this.video.magnetUri + '.');
067e3f84 115 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
dc8bc31b
C
116 if (err) {
117 alert('Cannot append the file.');
118 console.error(err);
119 }
44124980 120 });
8cfecb2a 121
c323efb9 122 this.runInProgress(torrent);
44124980 123 });
dc8bc31b 124 }
98b01bac 125
99cc4f49 126 showShareModal() {
cf02fbfb 127 this.videoShareModal.show();
99cc4f49
C
128 }
129
cf02fbfb
C
130 showMagnetUriModal() {
131 this.videoMagnetModal.show();
99cc4f49
C
132 }
133
3ad109e4
C
134 private loadTooLong() {
135 this.error = true;
136 console.error('The video load seems to be abnormally long.');
137 }
c323efb9 138
3ec343a4
C
139 private setOpenGraphTags() {
140 this.metaService.setTag('og:type', 'video');
141
142 this.metaService.setTag('og:title', this.video.name);
143 this.metaService.setTag('name', this.video.name);
144
145 this.metaService.setTag('og:description', this.video.description);
146 this.metaService.setTag('description', this.video.description);
147
148 this.metaService.setTag('og:image', this.video.thumbnailPath);
149
150 this.metaService.setTag('og:duration', this.video.duration);
151
152 this.metaService.setTag('og:site_name', 'PeerTube');
153
154 this.metaService.setTag('og:url', window.location.href);
155 this.metaService.setTag('url', window.location.href);
156 }
157
c323efb9
C
158 private runInProgress(torrent: any) {
159 // Refresh each second
160 this.torrentInfosInterval = setInterval(() => {
161 this.ngZone.run(() => {
162 this.downloadSpeed = torrent.downloadSpeed;
163 this.numPeers = torrent.numPeers;
164 this.uploadSpeed = torrent.uploadSpeed;
165 });
166 }, 1000);
167 }
dc8bc31b 168}