1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
import Hlsjs from 'hls.js'
import videojs from 'video.js'
import { Events, Segment } from '@peertube/p2p-media-loader-core'
import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
import { logger } from '@root-helpers/logger'
import { addQueryParams } from '@shared/core-utils'
import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../../types'
import { SettingsButton } from '../settings/settings-menu-button'
const Plugin = videojs.getPlugin('plugin')
class P2pMediaLoaderPlugin extends Plugin {
private readonly options: P2PMediaLoaderPluginOptions
private hlsjs: Hlsjs
private p2pEngine: Engine
private statsP2PBytes = {
pendingDownload: [] as number[],
pendingUpload: [] as number[],
peersWithWebSeed: 0,
peersP2POnly: 0,
totalDownload: 0,
totalUpload: 0
}
private statsHTTPBytes = {
pendingDownload: [] as number[],
totalDownload: 0
}
private networkInfoInterval: any
constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
super(player)
this.options = options
// FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
if (!(videojs as any).Html5Hlsjs) {
if (player.canPlayType('application/vnd.apple.mpegurl')) {
this.fallbackToBuiltInIOS()
return
}
const message = 'HLS.js does not seem to be supported. Cannot fallback to built-in HLS'
logger.warn(message)
const error: MediaError = {
code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
message,
MEDIA_ERR_ABORTED: MediaError.MEDIA_ERR_ABORTED,
MEDIA_ERR_DECODE: MediaError.MEDIA_ERR_DECODE,
MEDIA_ERR_NETWORK: MediaError.MEDIA_ERR_NETWORK,
MEDIA_ERR_SRC_NOT_SUPPORTED: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED
}
player.ready(() => player.error(error))
return
}
// FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
(videojs as any).Html5Hlsjs.addHook('beforeinitialize', (_videojsPlayer: any, hlsjs: any) => {
this.hlsjs = hlsjs
})
initVideoJsContribHlsJsPlayer(player)
player.src({
type: options.type,
src: options.src
})
player.ready(() => {
this.initializePlugin()
})
}
dispose () {
this.p2pEngine?.removeAllListeners()
this.p2pEngine?.destroy()
this.hlsjs?.destroy()
this.options.segmentValidator?.destroy();
(videojs as any).Html5Hlsjs?.removeAllHooks()
clearInterval(this.networkInfoInterval)
super.dispose()
}
getCurrentLevel () {
if (!this.hlsjs) return undefined
return this.hlsjs.levels[this.hlsjs.currentLevel]
}
getLiveLatency () {
return Math.round(this.hlsjs.latency)
}
getHLSJS () {
return this.hlsjs
}
private initializePlugin () {
initHlsJsPlayer(this.hlsjs)
this.p2pEngine = this.options.loader.getEngine()
this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
if (navigator.onLine === false) return
logger.error(`Segment ${segment.id} error.`, err)
this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl)
})
this.statsP2PBytes.peersWithWebSeed = 1 + this.options.redundancyUrlManager.countBaseUrls()
this.runStats()
this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHED, () => this.player.trigger('engine-resolution-change'))
}
private runStats () {
this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => {
const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
elem.pendingDownload.push(bytes)
elem.totalDownload += bytes
})
this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => {
if (method !== 'p2p') {
logger.error(`Received upload from unknown method ${method}`)
return
}
this.statsP2PBytes.pendingUpload.push(bytes)
this.statsP2PBytes.totalUpload += bytes
})
this.p2pEngine.on(Events.PeerConnect, () => {
this.statsP2PBytes.peersWithWebSeed++
this.statsP2PBytes.peersP2POnly++
})
this.p2pEngine.on(Events.PeerClose, () => {
this.statsP2PBytes.peersWithWebSeed--
this.statsP2PBytes.peersP2POnly--
})
this.networkInfoInterval = setInterval(() => {
const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
this.statsP2PBytes.pendingDownload = []
this.statsP2PBytes.pendingUpload = []
this.statsHTTPBytes.pendingDownload = []
return this.player.trigger('network-info', {
source: 'p2p-media-loader',
bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8,
http: {
downloadSpeed: httpDownloadSpeed,
downloaded: this.statsHTTPBytes.totalDownload
},
p2p: this.options.p2pEnabled
? {
downloadSpeed: p2pDownloadSpeed,
uploadSpeed: p2pUploadSpeed,
peersWithWebSeed: this.statsP2PBytes.peersWithWebSeed,
peersP2POnly: this.statsP2PBytes.peersP2POnly,
downloaded: this.statsP2PBytes.totalDownload,
uploaded: this.statsP2PBytes.totalUpload
}
: undefined
} as PlayerNetworkInfo)
}, 1000)
}
private arraySum (data: number[]) {
return data.reduce((a: number, b: number) => a + b, 0)
}
private fallbackToBuiltInIOS () {
logger.info('HLS.js does not seem to be supported. Fallback to built-in HLS.')
this.player.src({
type: this.options.type,
src: addQueryParams(this.options.src, {
videoFileToken: this.options.videoFileToken(),
reinjectVideoFileToken: 'true'
})
})
// Resolution button is not supported in built-in HLS player
this.getResolutionButton().hide()
}
private getResolutionButton () {
const settingsButton = this.player.controlBar.getDescendant([ 'settingsButton' ]) as SettingsButton
return settingsButton.menu.getChild('resolutionMenuButton')
}
}
videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
export { P2pMediaLoaderPlugin }
|