Skip to content

Commit

Permalink
feat(hls): add forceHLS option
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiya committed Nov 14, 2022
1 parent d84916f commit e4d5c65
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/hls/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ let importedHls: typeof import('hls.js/dist/hls.light.min.js') = globalThis.Hls

type hlsPluginOptions = {
hlsConfig?: Partial<HlsConfig>
matcher?: (video: HTMLVideoElement, source: Source) => boolean
matcher?: (video: HTMLVideoElement, source: Source, force?: boolean) => boolean
options?: Options
}

type Options = {
/**
* @default: false
*/
forceHLS?: boolean
/**
* @default: true
*/
Expand All @@ -42,11 +46,12 @@ type Options = {
showWarning?: boolean
}

const defaultMatcher: hlsPluginOptions['matcher'] = (video, source) =>
!(
Boolean(video.canPlayType('application/x-mpegURL')) ||
Boolean(video.canPlayType('application/vnd.apple.mpegURL'))
) &&
const defaultMatcher: hlsPluginOptions['matcher'] = (video, source, force) =>
(force ||
!(
Boolean(video.canPlayType('application/x-mpegURL')) ||
Boolean(video.canPlayType('application/vnd.apple.mpegURL'))
)) &&
(source.format === 'm3u8' ||
((source.format === 'auto' || typeof source.format === 'undefined') &&
/m3u8(#|\?|$)/i.test(source.src)))
Expand Down Expand Up @@ -139,7 +144,7 @@ const hlsPlugin = ({
return {
name: PLUGIN_NAME,
load: async (player, source, options) => {
const isMatch = matcher(player.$video, source)
const isMatch = matcher(player.$video, source, pluginOptions.forceHLS)

if (options.loader || !isMatch) {
hlsInstance?.destroy()
Expand Down

0 comments on commit e4d5c65

Please sign in to comment.