aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/shared/p2p-media-loader
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-12-02 17:54:23 +0100
committerChocobozzz <me@florianbigard.com>2022-12-02 17:54:23 +0100
commitb8598d40f650a31fe09a4a5426dcdc2c5c0d566c (patch)
treeee41176817ef13525aadc4f0b37fc9391364d5c9 /client/src/assets/player/shared/p2p-media-loader
parent190ac9df7c95cdae5294596764afae7ce78d108d (diff)
parentbd09dfaf8dcb0ca4cd5dac9f13e3117486f3bcce (diff)
downloadPeerTube-b8598d40f650a31fe09a4a5426dcdc2c5c0d566c.tar.gz
PeerTube-b8598d40f650a31fe09a4a5426dcdc2c5c0d566c.tar.zst
PeerTube-b8598d40f650a31fe09a4a5426dcdc2c5c0d566c.zip
Merge branch 'release/5.0.0' into develop
Diffstat (limited to 'client/src/assets/player/shared/p2p-media-loader')
-rw-r--r--client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts76
-rw-r--r--client/src/assets/player/shared/p2p-media-loader/segment-validator.ts3
2 files changed, 44 insertions, 35 deletions
diff --git a/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts b/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
index b608ee3e2..e6f525fea 100644
--- a/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
+++ b/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
@@ -3,7 +3,7 @@ import videojs from 'video.js'
3import { Events, Segment } from '@peertube/p2p-media-loader-core' 3import { Events, Segment } from '@peertube/p2p-media-loader-core'
4import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs' 4import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
5import { logger } from '@root-helpers/logger' 5import { logger } from '@root-helpers/logger'
6import { timeToInt } from '@shared/core-utils' 6import { addQueryParams, timeToInt } from '@shared/core-utils'
7import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../../types' 7import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../../types'
8import { registerConfigPlugin, registerSourceHandler } from './hls-plugin' 8import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
9 9
@@ -39,46 +39,37 @@ class P2pMediaLoaderPlugin extends Plugin {
39 super(player) 39 super(player)
40 40
41 this.options = options 41 this.options = options
42 this.startTime = timeToInt(options.startTime)
42 43
43 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080 44 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
44 if (!(videojs as any).Html5Hlsjs) { 45 if (!(videojs as any).Html5Hlsjs) {
45 logger.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.') 46 if (player.canPlayType('application/vnd.apple.mpegurl')) {
46 47 this.fallbackToBuiltInIOS()
47 let message: string 48 return
48 if (!player.canPlayType('application/vnd.apple.mpegurl')) {
49 message = 'Cannot fallback to built-in HLS'
50 } else if (options.requiresAuth) {
51 message = 'Video requires auth which is not compatible to build-in HLS player'
52 } 49 }
53 50
54 if (message) { 51 const message = 'HLS.js does not seem to be supported. Cannot fallback to built-in HLS'
55 logger.warn(message) 52 logger.warn(message)
56 53
57 const error: MediaError = { 54 const error: MediaError = {
58 code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED, 55 code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
59 message, 56 message,
60 MEDIA_ERR_ABORTED: MediaError.MEDIA_ERR_ABORTED, 57 MEDIA_ERR_ABORTED: MediaError.MEDIA_ERR_ABORTED,
61 MEDIA_ERR_DECODE: MediaError.MEDIA_ERR_DECODE, 58 MEDIA_ERR_DECODE: MediaError.MEDIA_ERR_DECODE,
62 MEDIA_ERR_NETWORK: MediaError.MEDIA_ERR_NETWORK, 59 MEDIA_ERR_NETWORK: MediaError.MEDIA_ERR_NETWORK,
63 MEDIA_ERR_SRC_NOT_SUPPORTED: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED 60 MEDIA_ERR_SRC_NOT_SUPPORTED: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED
64 }
65
66 player.ready(() => player.error(error))
67 return
68 } 61 }
69 62
70 // Workaround to force video.js to not re create a video element 63 player.ready(() => player.error(error))
71 (this.player as any).playerElIngest_ = this.player.el().parentNode 64 return
72 } else {
73 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
74 (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
75 this.hlsjs = hlsjs
76 })
77
78 initVideoJsContribHlsJsPlayer(player)
79 } 65 }
80 66
81 this.startTime = timeToInt(options.startTime) 67 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
68 (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (_videojsPlayer: any, hlsjs: any) => {
69 this.hlsjs = hlsjs
70 })
71
72 initVideoJsContribHlsJsPlayer(player)
82 73
83 player.src({ 74 player.src({
84 type: options.type, 75 type: options.type,
@@ -88,9 +79,7 @@ class P2pMediaLoaderPlugin extends Plugin {
88 player.ready(() => { 79 player.ready(() => {
89 this.initializeCore() 80 this.initializeCore()
90 81
91 if ((videojs as any).Html5Hlsjs) { 82 this.initializePlugin()
92 this.initializePlugin()
93 }
94 }) 83 })
95 } 84 }
96 85
@@ -199,6 +188,25 @@ class P2pMediaLoaderPlugin extends Plugin {
199 private arraySum (data: number[]) { 188 private arraySum (data: number[]) {
200 return data.reduce((a: number, b: number) => a + b, 0) 189 return data.reduce((a: number, b: number) => a + b, 0)
201 } 190 }
191
192 private fallbackToBuiltInIOS () {
193 logger.info('HLS.js does not seem to be supported. Fallback to built-in HLS.');
194
195 // Workaround to force video.js to not re create a video element
196 (this.player as any).playerElIngest_ = this.player.el().parentNode
197
198 this.player.src({
199 type: this.options.type,
200 src: addQueryParams(this.options.src, {
201 videoFileToken: this.options.videoFileToken(),
202 reinjectVideoFileToken: 'true'
203 })
204 })
205
206 this.player.ready(() => {
207 this.initializeCore()
208 })
209 }
202} 210}
203 211
204videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin) 212videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
diff --git a/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts b/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts
index a7ee91950..3c76d63f7 100644
--- a/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts
+++ b/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts
@@ -2,6 +2,7 @@ import { basename } from 'path'
2import { Segment } from '@peertube/p2p-media-loader-core' 2import { Segment } from '@peertube/p2p-media-loader-core'
3import { logger } from '@root-helpers/logger' 3import { logger } from '@root-helpers/logger'
4import { wait } from '@root-helpers/utils' 4import { wait } from '@root-helpers/utils'
5import { removeQueryParams } from '@shared/core-utils'
5import { isSameOrigin } from '../common' 6import { isSameOrigin } from '../common'
6 7
7type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } } 8type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } }
@@ -24,7 +25,7 @@ function segmentValidatorFactory (options: {
24 // Wait for hash generation from the server 25 // Wait for hash generation from the server
25 if (isLive) await wait(1000) 26 if (isLive) await wait(1000)
26 27
27 const filename = basename(segment.url) 28 const filename = basename(removeQueryParams(segment.url))
28 29
29 const segmentValue = (await segmentsJSON)[filename] 30 const segmentValue = (await segmentsJSON)[filename]
30 31