aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts')
-rw-r--r--client/src/assets/player/shared/p2p-media-loader/p2p-media-loader-plugin.ts76
1 files changed, 42 insertions, 34 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)