]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/video-watch/video-watch.component.ts
Client: use videojs as player
[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
3154f382 4import { ModalDirective } from 'ng2-bootstrap/components/modal';
3ec343a4 5import { MetaService } from 'ng2-meta';
e31f6ad6 6import * as videojs from 'video.js';
3154f382 7
ab32b0fc 8import { Video, VideoService } from '../shared';
d3ef341a 9import { WebTorrentService } from './webtorrent.service';
dc8bc31b 10
dc8bc31b
C
11@Component({
12 selector: 'my-video-watch',
ec8d8440
C
13 templateUrl: './video-watch.component.html',
14 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b
C
15})
16
0629423c 17export class VideoWatchComponent implements OnInit, OnDestroy {
3ad109e4
C
18 private static LOADTIME_TOO_LONG: number = 30000;
19
3154f382
C
20 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
21
8cfecb2a 22 downloadSpeed: number;
3ad109e4 23 error: boolean = false;
da932efc 24 loading: boolean = false;
4fd8aa32 25 numPeers: number;
e31f6ad6 26 player: VideoJSPlayer;
4fd8aa32 27 uploadSpeed: number;
d1992b93 28 video: Video = null;
dc8bc31b 29
3ad109e4 30 private errorTimer: NodeJS.Timer;
0629423c 31 private sub: any;
3ad109e4 32 private torrentInfosInterval: NodeJS.Timer;
dc8bc31b
C
33
34 constructor(
4fd8aa32 35 private elementRef: ElementRef,
c323efb9 36 private ngZone: NgZone,
0629423c 37 private route: ActivatedRoute,
d3ef341a 38 private videoService: VideoService,
3ec343a4 39 private metaService: MetaService,
d3ef341a
C
40 private webTorrentService: WebTorrentService
41 ) {}
dc8bc31b 42
d1992b93
C
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;
3ec343a4 49 this.setOpenGraphTags();
d1992b93
C
50 this.loadVideo();
51 },
52 error => alert(error.text)
53 );
54 });
e31f6ad6
C
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 });
d1992b93
C
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
3ad109e4 76 loadVideo() {
3bb2c7f9
C
77 console.log('<iframe width="560" height="315" src="' + window.location.origin + '/videos/embed/' + this.video.id + '" frameborder="0" allowfullscreen></iframe>');
78
3ad109e4
C
79 // Reset the error
80 this.error = false;
81 // We are loading the video
da932efc 82 this.loading = true;
3ad109e4 83
2c4a0b5d 84 console.log('Adding ' + this.video.magnetUri + '.');
d3ef341a 85
3ad109e4
C
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
d3ef341a 90 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
3ad109e4
C
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
da932efc 97 this.loading = false;
3ad109e4 98
2c4a0b5d 99 console.log('Added ' + this.video.magnetUri + '.');
e31f6ad6 100 torrent.files[0].renderTo('#video-container video', { autoplay: true }, (err) => {
dc8bc31b
C
101 if (err) {
102 alert('Cannot append the file.');
103 console.error(err);
104 }
44124980 105 });
8cfecb2a 106
c323efb9 107 this.runInProgress(torrent);
44124980 108 });
dc8bc31b 109 }
98b01bac 110
3154f382
C
111 showMagnetUriModal() {
112 this.magnetUriModal.show();
113 }
114
115 hideMagnetUriModal() {
116 this.magnetUriModal.hide();
117 }
118
3ad109e4
C
119 private loadTooLong() {
120 this.error = true;
121 console.error('The video load seems to be abnormally long.');
122 }
c323efb9 123
3ec343a4
C
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
c323efb9
C
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 }
dc8bc31b 153}