]>
Commit | Line | Data |
---|---|---|
2adfc7ea C |
1 | // FIXME: something weird with our path definition in tsconfig and typings |
2 | // @ts-ignore | |
3 | import * as videojs from 'video.js' | |
09209296 C |
4 | import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo, VideoJSComponentInterface } from '../peertube-videojs-typings' |
5 | import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs' | |
6 | import { Events } from 'p2p-media-loader-core' | |
2adfc7ea C |
7 | |
8 | // videojs-hlsjs-plugin needs videojs in window | |
9 | window['videojs'] = videojs | |
4348a27d | 10 | require('@streamroot/videojs-hlsjs-plugin') |
2adfc7ea | 11 | |
2adfc7ea C |
12 | const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') |
13 | class P2pMediaLoaderPlugin extends Plugin { | |
14 | ||
3b6f205c C |
15 | private readonly CONSTANTS = { |
16 | INFO_SCHEDULER: 1000 // Don't change this | |
17 | } | |
09209296 | 18 | private readonly options: P2PMediaLoaderPluginOptions |
3b6f205c | 19 | |
4348a27d | 20 | private hlsjs: any // Don't type hlsjs to not bundle the module |
3b6f205c C |
21 | private p2pEngine: Engine |
22 | private statsP2PBytes = { | |
23 | pendingDownload: [] as number[], | |
24 | pendingUpload: [] as number[], | |
25 | numPeers: 0, | |
26 | totalDownload: 0, | |
27 | totalUpload: 0 | |
28 | } | |
09209296 C |
29 | private statsHTTPBytes = { |
30 | pendingDownload: [] as number[], | |
31 | pendingUpload: [] as number[], | |
32 | totalDownload: 0, | |
33 | totalUpload: 0 | |
34 | } | |
3b6f205c C |
35 | |
36 | private networkInfoInterval: any | |
37 | ||
2adfc7ea C |
38 | constructor (player: videojs.Player, options: P2PMediaLoaderPluginOptions) { |
39 | super(player, options) | |
40 | ||
09209296 C |
41 | this.options = options |
42 | ||
96cb4527 C |
43 | if (!videojs.Html5Hlsjs) { |
44 | const message = 'HLS.js does not seem to be supported.' | |
45 | console.warn(message) | |
46 | ||
47 | player.ready(() => player.trigger('error', new Error(message))) | |
48 | return | |
49 | } | |
50 | ||
4348a27d | 51 | videojs.Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => { |
3b6f205c | 52 | this.hlsjs = hlsjs |
3b6f205c C |
53 | }) |
54 | ||
55 | initVideoJsContribHlsJsPlayer(player) | |
2adfc7ea C |
56 | |
57 | player.src({ | |
58 | type: options.type, | |
59 | src: options.src | |
60 | }) | |
09209296 | 61 | |
6ec0b75b C |
62 | player.on('play', () => { |
63 | player.addClass('vjs-has-big-play-button-clicked') | |
64 | }) | |
65 | ||
09209296 | 66 | player.ready(() => this.initialize()) |
2adfc7ea C |
67 | } |
68 | ||
3b6f205c | 69 | dispose () { |
6ec0b75b C |
70 | if (this.hlsjs) this.hlsjs.destroy() |
71 | if (this.p2pEngine) this.p2pEngine.destroy() | |
72 | ||
3b6f205c C |
73 | clearInterval(this.networkInfoInterval) |
74 | } | |
75 | ||
76 | private initialize () { | |
09209296 C |
77 | initHlsJsPlayer(this.hlsjs) |
78 | ||
6ec0b75b C |
79 | const tech = this.player.tech_ |
80 | this.p2pEngine = tech.options_.hlsjsConfig.loader.getEngine() | |
3b6f205c | 81 | |
4348a27d C |
82 | // Avoid using constants to not import hls.hs |
83 | // https://github.com/video-dev/hls.js/blob/master/src/events.js#L37 | |
84 | this.hlsjs.on('hlsLevelSwitching', (_: any, data: any) => { | |
3b6f205c C |
85 | this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height }) |
86 | }) | |
87 | ||
09209296 C |
88 | this.p2pEngine.on(Events.SegmentError, (segment, err) => { |
89 | console.error('Segment error.', segment, err) | |
90 | }) | |
91 | ||
92 | this.statsP2PBytes.numPeers = 1 + this.options.redundancyBaseUrls.length | |
93 | ||
3b6f205c C |
94 | this.runStats() |
95 | } | |
96 | ||
97 | private runStats () { | |
98 | this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => { | |
09209296 C |
99 | const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes |
100 | ||
101 | elem.pendingDownload.push(size) | |
102 | elem.totalDownload += size | |
3b6f205c C |
103 | }) |
104 | ||
105 | this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => { | |
09209296 C |
106 | const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes |
107 | ||
108 | elem.pendingUpload.push(size) | |
109 | elem.totalUpload += size | |
3b6f205c C |
110 | }) |
111 | ||
112 | this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++) | |
113 | this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--) | |
114 | ||
115 | this.networkInfoInterval = setInterval(() => { | |
09209296 C |
116 | const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload) |
117 | const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload) | |
118 | ||
119 | const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload) | |
120 | const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload) | |
3b6f205c C |
121 | |
122 | this.statsP2PBytes.pendingDownload = [] | |
123 | this.statsP2PBytes.pendingUpload = [] | |
09209296 C |
124 | this.statsHTTPBytes.pendingDownload = [] |
125 | this.statsHTTPBytes.pendingUpload = [] | |
3b6f205c C |
126 | |
127 | return this.player.trigger('p2pInfo', { | |
09209296 C |
128 | http: { |
129 | downloadSpeed: httpDownloadSpeed, | |
130 | uploadSpeed: httpUploadSpeed, | |
131 | downloaded: this.statsHTTPBytes.totalDownload, | |
132 | uploaded: this.statsHTTPBytes.totalUpload | |
133 | }, | |
3b6f205c | 134 | p2p: { |
09209296 C |
135 | downloadSpeed: p2pDownloadSpeed, |
136 | uploadSpeed: p2pUploadSpeed, | |
3b6f205c C |
137 | numPeers: this.statsP2PBytes.numPeers, |
138 | downloaded: this.statsP2PBytes.totalDownload, | |
139 | uploaded: this.statsP2PBytes.totalUpload | |
140 | } | |
141 | } as PlayerNetworkInfo) | |
142 | }, this.CONSTANTS.INFO_SCHEDULER) | |
143 | } | |
09209296 C |
144 | |
145 | private arraySum (data: number[]) { | |
146 | return data.reduce((a: number, b: number) => a + b, 0) | |
147 | } | |
2adfc7ea C |
148 | } |
149 | ||
150 | videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin) | |
151 | export { P2pMediaLoaderPlugin } |