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