Skip to content

Commit

Permalink
hls error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiya committed May 7, 2024
1 parent 9491e84 commit 060aa83
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
2 changes: 0 additions & 2 deletions packages/dash/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const PLUGIN_NAME = 'oplayer-plugin-dash'

export type Matcher = (video: HTMLVideoElement, source: Source) => boolean

export type Active = (instance: MediaPlayerClass, library: typeof import('dashjs')) => void

export interface DashPluginOptions {
library?: string
matcher?: Matcher
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/public/oplayer.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected].3/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected].4/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>

Expand Down
9 changes: 6 additions & 3 deletions packages/hls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface HlsPluginOptions {
}
```

## Handle Hls.js Error **v1.2.24.beta.0+**
## Handle Hls.js Error

```ts
OPlayer.make('#oplayer', {
Expand All @@ -99,11 +99,14 @@ OPlayer.make('#oplayer', {
})
.use([
OHls({
errorHandler(player, data, cb) {
errorHandler(player, data) {
// skip bufferAppendError
if (data.details == 'bufferAppendError') return

cb(player, data)
player.emit('error', {
...data,
message: data.type + ': ' + data.details
})
}
})
])
Expand Down
2 changes: 1 addition & 1 deletion packages/hls/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oplayer/hls",
"version": "1.2.26-beta.3",
"version": "1.2.26-beta.4",
"description": "Hls plugin for oplayer",
"type": "module",
"main": "./dist/index.es.js",
Expand Down
35 changes: 18 additions & 17 deletions packages/hls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@ const PLUGIN_NAME = 'oplayer-plugin-hls'

export type Matcher = (video: HTMLVideoElement, source: Source, forceHLS: boolean) => boolean

export type Active = (instance: Hls, library: typeof import('hls.js/dist/hls.min.js')) => void

export interface HlsPluginOptions {
library?: string

matcher?: Matcher

/**
* custom handle hls fatal error
* @param {Events}
* @param {ErrorData}
* @returns {boolean}
*/
errorHandler?: (player: Player, data: ErrorData, cb: typeof defaultErrorHandler) => void
errorHandler?: (player: Player, data: ErrorData) => void
/**
* config for hls.js
*
Expand Down Expand Up @@ -70,12 +65,6 @@ const defaultMatcher: Matcher = (video, source, forceHLS) => {
)
}

const defaultErrorHandler = (player: Player, data: ErrorData) => {
const { type, details } = data
player.hasError = true
player.emit('error', { ...data, pluginName: PLUGIN_NAME, message: type + ': ' + details })
}

class HlsPlugin implements PlayerPlugin {
key = 'hls'
name = PLUGIN_NAME
Expand All @@ -87,16 +76,15 @@ class HlsPlugin implements PlayerPlugin {

instance?: Hls

options: RequiredPartial<HlsPluginOptions, 'library'> = {
options: RequiredPartial<HlsPluginOptions, 'library' | 'errorHandler'> = {
config: {},
forceHLS: false,
textControl: true,
audioControl: true,
qualityControl: true,
withBitrate: false,
qualitySwitch: 'immediate',
matcher: defaultMatcher,
errorHandler: defaultErrorHandler
matcher: defaultMatcher
}

constructor(options?: HlsPluginOptions) {
Expand Down Expand Up @@ -134,7 +122,7 @@ class HlsPlugin implements PlayerPlugin {
$source.setAttribute('data-hls', '')
$video.append($source)

instance.once(HlsPlugin.library.Events.DESTROYING, () => {
instance.on(HlsPlugin.library.Events.DESTROYING, () => {
$source.remove()
})

Expand All @@ -145,8 +133,21 @@ class HlsPlugin implements PlayerPlugin {
instance.recoverMediaError()
break
case 'networkError':
if (!(data.networkDetails?.status === 404)) {
instance.startLoad()
}
break
default:
errorHandler(player, data, defaultErrorHandler)
if (errorHandler) {
errorHandler(player, data)
} else {
player.hasError = true
player.emit('error', {
...data,
pluginName: PLUGIN_NAME,
message: data.type + ': ' + data.details
})
}
break
}
}
Expand Down

0 comments on commit 060aa83

Please sign in to comment.