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