aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/shared/p2p-media-loader/hls-plugin.ts
blob: d05d6193c4a0566c6013d526799a9b8cfadcdfc7 (plain) (blame)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Thanks https://github.com/streamroot/videojs-hlsjs-plugin
// We duplicated this plugin to choose the hls.js version we want, because streamroot only provide a bundled file

import Hlsjs, { ErrorData, HlsConfig, Level, LevelSwitchingData, ManifestParsedData } from 'hls.js'
import videojs from 'video.js'
import { logger } from '@root-helpers/logger'
import { HlsjsConfigHandlerOptions, PeerTubeResolution, VideoJSTechHLS } from '../../types'

type ErrorCounts = {
  [ type: string ]: number
}

type Metadata = {
  levels: Level[]
}

type HookFn = (player: videojs.Player, hljs: Hlsjs) => void

const registerSourceHandler = function (vjs: typeof videojs) {
  if (!Hlsjs.isSupported()) {
    logger.warn('Hls.js is not supported in this browser!')
    return
  }

  const html5 = vjs.getTech('Html5')

  if (!html5) {
    logger.error('No Hml5 tech found in videojs')
    return
  }

  // FIXME: typings
  (html5 as any).registerSourceHandler({
    canHandleSource: function (source: videojs.Tech.SourceObject) {
      const hlsTypeRE = /^application\/x-mpegURL|application\/vnd\.apple\.mpegurl$/i
      const hlsExtRE = /\.m3u8/i

      if (hlsTypeRE.test(source.type)) return 'probably'
      if (hlsExtRE.test(source.src)) return 'maybe'

      return ''
    },

    handleSource: function (source: videojs.Tech.SourceObject, tech: VideoJSTechHLS) {
      if (tech.hlsProvider) {
        tech.hlsProvider.dispose()
      }

      tech.hlsProvider = new Html5Hlsjs(vjs, source, tech)

      return tech.hlsProvider
    }
  }, 0);

  // FIXME: typings
  (vjs as any).Html5Hlsjs = Html5Hlsjs
}

function hlsjsConfigHandler (this: videojs.Player, options: HlsjsConfigHandlerOptions) {
  const player = this

  if (!options) return

  if (!player.srOptions_) {
    player.srOptions_ = {}
  }

  if (!player.srOptions_.hlsjsConfig) {
    player.srOptions_.hlsjsConfig = options.hlsjsConfig
  }

  if (options.levelLabelHandler && !player.srOptions_.levelLabelHandler) {
    player.srOptions_.levelLabelHandler = options.levelLabelHandler
  }
}

const registerConfigPlugin = function (vjs: typeof videojs) {
  // Used in Brightcove since we don't pass options directly there
  const registerVjsPlugin = vjs.registerPlugin || vjs.plugin
  registerVjsPlugin('hlsjs', hlsjsConfigHandler)
}

class Html5Hlsjs {
  private static readonly hooks: { [id: string]: HookFn[] } = {}

  private readonly videoElement: HTMLVideoElement
  private readonly errorCounts: ErrorCounts = {}
  private readonly player: videojs.Player
  private readonly tech: videojs.Tech
  private readonly source: videojs.Tech.SourceObject
  private readonly vjs: typeof videojs

  private maxNetworkErrorRecovery = 5

  private hls: Hlsjs
  private hlsjsConfig: Partial<HlsConfig & { cueHandler: any }> = null

  private _duration: number = null
  private metadata: Metadata = null
  private isLive: boolean = null
  private dvrDuration: number = null
  private edgeMargin: number = null

  private handlers: { [ id in 'play' ]: EventListener } = {
    play: null
  }

  constructor (vjs: typeof videojs, source: videojs.Tech.SourceObject, tech: videojs.Tech) {
    this.vjs = vjs
    this.source = source

    this.tech = tech;
    (this.tech as any).name_ = 'Hlsjs'

    this.videoElement = tech.el() as HTMLVideoElement
    this.player = vjs((tech.options_ as any).playerId)

    this.videoElement.addEventListener('error', event => {
      let errorTxt: string
      const mediaError = ((event.currentTarget || event.target) as HTMLVideoElement).error

      if (!mediaError) return

      logger.info(mediaError)
      switch (mediaError.code) {
        case mediaError.MEDIA_ERR_ABORTED:
          errorTxt = 'You aborted the video playback'
          break
        case mediaError.MEDIA_ERR_DECODE:
          errorTxt = 'The video playback was aborted due to a corruption problem or because the video used features ' +
                     'your browser did not support'
          this._handleMediaError(mediaError)
          break
        case mediaError.MEDIA_ERR_NETWORK:
          errorTxt = 'A network error caused the video download to fail part-way'
          break
        case mediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
          errorTxt = 'The video could not be loaded, either because the server or network failed or because the format is not supported'
          break

        default:
          errorTxt = mediaError.message
      }

      logger.error(`MEDIA_ERROR: ${errorTxt}`)
    })

    this.initialize()
  }

  duration () {
    if (this._duration === Infinity) return Infinity
    if (!isNaN(this.videoElement.duration)) return this.videoElement.duration

    return this._duration || 0
  }

  seekable () {
    if (this.hls.media) {
      if (!this.isLive) {
        return this.vjs.createTimeRanges(0, this.hls.media.duration)
      }

      // Video.js doesn't seem to like floating point timeranges
      const startTime = Math.round(this.hls.media.duration - this.dvrDuration)
      const endTime = Math.round(this.hls.media.duration - this.edgeMargin)

      return this.vjs.createTimeRanges(startTime, endTime)
    }

    return this.vjs.createTimeRanges()
  }

  // See comment for `initialize` method.
  dispose () {
    this.videoElement.removeEventListener('play', this.handlers.play)

    // FIXME: https://github.com/video-dev/hls.js/issues/4092
    const untypedHLS = this.hls as any
    untypedHLS.log = untypedHLS.warn = () => {
      // empty
    }

    this.hls.destroy()
  }

  static addHook (type: string, callback: HookFn) {
    Html5Hlsjs.hooks[type] = this.hooks[type] || []
    Html5Hlsjs.hooks[type].push(callback)
  }

  static removeHook (type: string, callback: HookFn) {
    if (Html5Hlsjs.hooks[type] === undefined) return false

    const index = Html5Hlsjs.hooks[type].indexOf(callback)
    if (index === -1) return false

    Html5Hlsjs.hooks[type].splice(index, 1)

    return true
  }

  private _executeHooksFor (type: string) {
    if (Html5Hlsjs.hooks[type] === undefined) {
      return
    }

    // ES3 and IE < 9
    for (let i = 0; i < Html5Hlsjs.hooks[type].length; i++) {
      Html5Hlsjs.hooks[type][i](this.player, this.hls)
    }
  }

  private _getHumanErrorMsg (error: { message: string, code?: number }) {
    switch (error.code) {
      default:
        return error.message
    }
  }

  private _handleUnrecovarableError (error: any) {
    if (this.hls.levels.filter(l => l.id > -1).length > 1) {
      this._removeQuality(this.hls.loadLevel)
      return
    }

    this.hls.destroy()
    logger.info('bubbling error up to VIDEOJS')
    this.tech.error = () => ({
      ...error,
      message: this._getHumanErrorMsg(error)
    })
    this.tech.trigger('error')
  }

  private _handleMediaError (error: any) {
    if (this.errorCounts[Hlsjs.ErrorTypes.MEDIA_ERROR] === 1) {
      logger.info('trying to recover media error')
      this.hls.recoverMediaError()
      return
    }

    if (this.errorCounts[Hlsjs.ErrorTypes.MEDIA_ERROR] === 2) {
      logger.info('2nd try to recover media error (by swapping audio codec')
      this.hls.swapAudioCodec()
      this.hls.recoverMediaError()
      return
    }

    if (this.errorCounts[Hlsjs.ErrorTypes.MEDIA_ERROR] > 2) {
      this._handleUnrecovarableError(error)
    }
  }

  private _handleNetworkError (error: any) {
    if (navigator.onLine === false) return

    if (this.errorCounts[Hlsjs.ErrorTypes.NETWORK_ERROR] <= this.maxNetworkErrorRecovery) {
      logger.info('trying to recover network error')

      // Wait 1 second and retry
      setTimeout(() => this.hls.startLoad(), 1000)

      // Reset error count on success
      this.hls.once(Hlsjs.Events.FRAG_LOADED, () => {
        this.errorCounts[Hlsjs.ErrorTypes.NETWORK_ERROR] = 0
      })

      return
    }

    this._handleUnrecovarableError(error)
  }

  private _onError (_event: any, data: ErrorData) {
    const error: { message: string, code?: number } = {
      message: `HLS.js error: ${data.type} - fatal: ${data.fatal} - ${data.details}`
    }

    // increment/set error count
    if (this.errorCounts[data.type]) this.errorCounts[data.type] += 1
    else this.errorCounts[data.type] = 1

    if (data.fatal) logger.error(error.message, { currentTime: this.player.currentTime(), data })
    else logger.warn(error.message)

    if (data.type === Hlsjs.ErrorTypes.NETWORK_ERROR) {
      error.code = 2
      this._handleNetworkError(error)
    } else if (data.fatal && data.type === Hlsjs.ErrorTypes.MEDIA_ERROR && data.details !== 'manifestIncompatibleCodecsError') {
      error.code = 3
      this._handleMediaError(error)
    } else if (data.fatal) {
      this._handleUnrecovarableError(error)
    }
  }

  private buildLevelLabel (level: Level) {
    if (this.player.srOptions_.levelLabelHandler) {
      return this.player.srOptions_.levelLabelHandler(level as any)
    }

    if (level.height) return level.height + 'p'
    if (level.width) return Math.round(level.width * 9 / 16) + 'p'
    if (level.bitrate) return (level.bitrate / 1000) + 'kbps'

    return '0'
  }

  private _removeQuality (index: number) {
    this.hls.removeLevel(index)
    this.player.peertubeResolutions().remove(index)
    this.hls.currentLevel = -1
  }

  private _notifyVideoQualities () {
    if (!this.metadata) return

    const resolutions: PeerTubeResolution[] = []

    this.metadata.levels.forEach((level, index) => {
      resolutions.push({
        id: index,
        height: level.height,
        width: level.width,
        bitrate: level.bitrate,
        label: this.buildLevelLabel(level),
        selected: level.id === this.hls.manualLevel,

        selectCallback: () => {
          this.hls.currentLevel = index
        }
      })
    })

    resolutions.push({
      id: -1,
      label: this.player.localize('Auto'),
      selected: true,
      selectCallback: () => this.hls.currentLevel = -1
    })

    this.player.peertubeResolutions().add(resolutions)
  }

  private _startLoad () {
    this.hls.startLoad(-1)
    this.videoElement.removeEventListener('play', this.handlers.play)
  }

  private _oneLevelObjClone (obj: { [ id: string ]: any }) {
    const result: { [id: string]: any } = {}
    const objKeys = Object.keys(obj)
    for (let i = 0; i < objKeys.length; i++) {
      result[objKeys[i]] = obj[objKeys[i]]
    }

    return result
  }

  private _onMetaData (_event: any, data: ManifestParsedData) {
    // This could arrive before 'loadedqualitydata' handlers is registered, remember it so we can raise it later
    this.metadata = data
    this._notifyVideoQualities()
  }

  private _initHlsjs () {
    const techOptions = this.tech.options_ as HlsjsConfigHandlerOptions
    const srOptions_ = this.player.srOptions_

    const hlsjsConfigRef = srOptions_?.hlsjsConfig || techOptions.hlsjsConfig
    // Hls.js will write to the reference thus change the object for later streams
    this.hlsjsConfig = hlsjsConfigRef ? this._oneLevelObjClone(hlsjsConfigRef) : {}

    if ([ '', 'auto' ].includes(this.videoElement.preload) && !this.videoElement.autoplay && this.hlsjsConfig.autoStartLoad === undefined) {
      this.hlsjsConfig.autoStartLoad = false
    }

    // If the user explicitly sets autoStartLoad to false, we're not going to enter the if block above
    // That's why we have a separate if block here to set the 'play' listener
    if (this.hlsjsConfig.autoStartLoad === false) {
      this.handlers.play = this._startLoad.bind(this)
      this.videoElement.addEventListener('play', this.handlers.play)
    }

    this.hls = new Hlsjs(this.hlsjsConfig)

    this._executeHooksFor('beforeinitialize')

    this.hls.on(Hlsjs.Events.ERROR, (event, data) => this._onError(event, data))
    this.hls.on(Hlsjs.Events.MANIFEST_PARSED, (event, data) => this._onMetaData(event, data))
    this.hls.on(Hlsjs.Events.LEVEL_LOADED, (event, data) => {
      // The DVR plugin will auto seek to "live edge" on start up
      if (this.hlsjsConfig.liveSyncDuration) {
        this.edgeMargin = this.hlsjsConfig.liveSyncDuration
      } else if (this.hlsjsConfig.liveSyncDurationCount) {
        this.edgeMargin = this.hlsjsConfig.liveSyncDurationCount * data.details.targetduration
      }

      this.isLive = data.details.live
      this.dvrDuration = data.details.totalduration

      this._duration = this.isLive ? Infinity : data.details.totalduration

      // Increase network error recovery for lives since they can be broken (server restart, stream interruption etc)
      if (this.isLive) this.maxNetworkErrorRecovery = 30
    })

    this.hls.once(Hlsjs.Events.FRAG_LOADED, () => {
      // Emit custom 'loadedmetadata' event for parity with `videojs-contrib-hls`
      // Ref: https://github.com/videojs/videojs-contrib-hls#loadedmetadata
      this.tech.trigger('loadedmetadata')
    })

    this.hls.on(Hlsjs.Events.LEVEL_SWITCHING, (_e, data: LevelSwitchingData) => {
      const resolutionId = this.hls.autoLevelEnabled
        ? -1
        : data.level

      const autoResolutionChosenId = this.hls.autoLevelEnabled
        ? data.level
        : -1

      this.player.peertubeResolutions().select({ id: resolutionId, autoResolutionChosenId, byEngine: true })
    })

    this.hls.attachMedia(this.videoElement)

    this.hls.loadSource(this.source.src)
  }

  private initialize () {
    this._initHlsjs()
  }
}

export {
  Html5Hlsjs,
  registerSourceHandler,
  registerConfigPlugin
}