Skip to content

Commit 7327643

Browse files
authored
feat: Add support for configuring HLS.js options (#463)
Added hlsConfig optional config in the plugin options
1 parent 0a0b353 commit 7327643

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/components/Player.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import {Card, Text} from '@sanity/ui'
22
import React, {useEffect, useMemo, useRef} from 'react'
33

44
import {useCancelUpload} from '../hooks/useCancelUpload'
5-
import type {MuxInputProps, VideoAssetDocument} from '../util/types'
5+
import type {MuxInputProps, PluginConfig, VideoAssetDocument} from '../util/types'
66
import {TopControls} from './Player.styled'
77
import {UploadProgress} from './UploadProgress'
88
import VideoPlayer from './VideoPlayer'
99

1010
interface Props extends Pick<MuxInputProps, 'onChange' | 'readOnly'> {
1111
buttons?: React.ReactNode
1212
asset: VideoAssetDocument
13+
config?: PluginConfig
1314
}
1415

15-
const Player = ({asset, buttons, readOnly, onChange}: Props) => {
16+
const Player = ({asset, buttons, readOnly, onChange, config}: Props) => {
1617
const isLoading = useMemo<boolean | string>(() => {
1718
if (asset?.status === 'preparing') {
1819
return 'Preparing the video'
@@ -91,7 +92,7 @@ const Player = ({asset, buttons, readOnly, onChange}: Props) => {
9192
}
9293

9394
return (
94-
<VideoPlayer asset={asset}>
95+
<VideoPlayer asset={asset} hlsConfig={config?.hlsConfig}>
9596
{buttons && <TopControls slot="top-chrome">{buttons}</TopControls>}
9697
{isPreparingStaticRenditions && (
9798
<Card

src/components/Uploader.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ export default function Uploader(props: Props) {
435435
readOnly={props.readOnly}
436436
asset={props.asset}
437437
onChange={props.onChange}
438+
config={props.config}
438439
buttons={
439440
<PlayerActionsMenu
440441
accept={acceptMimeString}

src/components/VideoPlayer.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ export default function VideoPlayer({
1717
asset,
1818
thumbnailWidth = 250,
1919
children,
20+
hlsConfig,
2021
...props
2122
}: PropsWithChildren<
22-
{asset: VideoAssetDocument; thumbnailWidth?: number; forceAspectRatio?: number} & Partial<
23-
Pick<MuxPlayerProps, 'autoPlay'>
24-
>
23+
{
24+
asset: VideoAssetDocument
25+
thumbnailWidth?: number
26+
forceAspectRatio?: number
27+
hlsConfig?: MuxPlayerProps['_hlsConfig']
28+
} & Partial<Pick<MuxPlayerProps, 'autoPlay'>>
2529
>) {
2630
const client = useClient()
2731
const {dialogState} = useDialogStateContext()
@@ -110,6 +114,7 @@ export default function VideoPlayer({
110114
page_type: 'Preview Player',
111115
}}
112116
audio={isAudio}
117+
_hlsConfig={hlsConfig}
113118
style={{
114119
...(!isAudio && {height: '100%'}),
115120
width: '100%',

src/util/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {ObjectInputProps, PreviewLayoutKey, PreviewProps, SchemaType} from 'sanity'
22
import type {PartialDeep} from 'type-fest'
3+
import type MuxPlayerElement from '@mux/mux-player'
34

45
/**
56
* Standard static rendition options available for plugin configuration defaults
@@ -149,6 +150,23 @@ export interface MuxInputConfig {
149150
* @defaultValue undefined
150151
*/
151152
maxAssetDuration?: number
153+
154+
/**
155+
* HLS.js configuration options to be passed to the Mux Player.
156+
* These options allow you to customize the underlying HLS.js playback engine behavior.
157+
*
158+
* @see {@link https://github.com/video-dev/hls.js/blob/master/docs/API.md#fine-tuning}
159+
* @defaultValue undefined
160+
* @example
161+
* ```ts
162+
* {
163+
* maxBufferLength: 30,
164+
* lowLatencyMode: true,
165+
* capLevelToPlayerSize: true
166+
* }
167+
* ```
168+
*/
169+
hlsConfig?: MuxPlayerElement['_hlsConfig']
152170
}
153171

154172
export interface PluginConfig extends MuxInputConfig {

0 commit comments

Comments
 (0)