@banou/media-player 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/package.json +51 -51
- package/src/assets/picture-in-picture.svg +5 -5
- package/src/components/chrome.tsx +95 -95
- package/src/components/control-bar.tsx +333 -333
- package/src/components/overlay.tsx +91 -91
- package/src/components/playback-slider.tsx +174 -174
- package/src/components/progress-bar.tsx +251 -251
- package/src/components/settings.tsx +321 -321
- package/src/components/sound.tsx +100 -100
- package/src/components/tooltip-display.tsx +83 -83
- package/src/components/volume-slider.tsx +156 -156
- package/src/index.tsx +195 -195
- package/src/main.tsx +169 -169
- package/src/state-machines/data-source.ts +84 -84
- package/src/state-machines/index.ts +5 -5
- package/src/state-machines/media-properties.ts +82 -82
- package/src/state-machines/media-source.ts +129 -129
- package/src/state-machines/media.ts +255 -255
- package/src/state-machines/subtitles.ts +285 -285
- package/src/state-machines/thumbnails.ts +123 -123
- package/src/state-machines/utils.ts +94 -94
- package/src/utils/actor-utils.ts +19 -19
- package/src/utils/colors.ts +8 -8
- package/src/utils/context.ts +23 -23
- package/src/utils/fonts.ts +159 -159
- package/src/utils/index.ts +229 -229
- package/src/utils/languages.ts +262 -262
- package/src/utils/mp4box.ts +74 -74
- package/src/utils/time.ts +15 -15
- package/src/utils/use-local-storage.ts +30 -30
- package/src/utils/use-scrub.ts +40 -40
- package/src/utils/volume-utils.ts +39 -39
- package/src/utils/window-height.ts +19 -19
- package/src/vite-env.d.ts +1 -1
- package/tsconfig.json +28 -28
- package/tsconfig.node.json +9 -9
package/src/index.tsx
CHANGED
|
@@ -1,195 +1,195 @@
|
|
|
1
|
-
/// <reference types="@emotion/react/types/css-prop" />
|
|
2
|
-
import type { ClassAttributes, MutableRefObject, ReactNode, RefCallback } from 'react'
|
|
3
|
-
import type { MediaPlayerContextType } from './utils/context'
|
|
4
|
-
|
|
5
|
-
import { useCallback, useContext, useEffect, useState } from 'react'
|
|
6
|
-
import { css } from '@emotion/react'
|
|
7
|
-
|
|
8
|
-
import { MediaMachineContext } from './state-machines'
|
|
9
|
-
import { MediaPlayerContext, DownloadedRange } from './utils/context'
|
|
10
|
-
import useLocalStorage, { booleanType } from './utils/use-local-storage'
|
|
11
|
-
import Chrome from './components/chrome'
|
|
12
|
-
|
|
13
|
-
const BUFFER_SIZE = 2_500_000
|
|
14
|
-
|
|
15
|
-
const FKNVideoRootStyle = css`
|
|
16
|
-
display: flex;
|
|
17
|
-
justify-content: center;
|
|
18
|
-
background-color: #111;
|
|
19
|
-
height: 100%;
|
|
20
|
-
width: 100%;
|
|
21
|
-
overflow: hidden;
|
|
22
|
-
`
|
|
23
|
-
|
|
24
|
-
export type FKNVideoOptions = {
|
|
25
|
-
title?: string
|
|
26
|
-
downloadedRanges?: DownloadedRange[]
|
|
27
|
-
read?: (offset: number, size: number) => Promise<ArrayBuffer>
|
|
28
|
-
size?: number
|
|
29
|
-
bufferSize?: number
|
|
30
|
-
autoplay?: boolean
|
|
31
|
-
mediaInformation?: ReactNode
|
|
32
|
-
loadingInformation?: ReactNode
|
|
33
|
-
publicPath: string
|
|
34
|
-
jassubWorkerUrl: string
|
|
35
|
-
jassubWasmUrl: string
|
|
36
|
-
jassubModernWasmUrl: string
|
|
37
|
-
libavWorkerUrl: string
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const FKNVideoRoot = (
|
|
41
|
-
{ options, videoElement, children }:
|
|
42
|
-
{ options: FKNVideoOptions, videoElement: HTMLVideoElement | undefined, children: ReactNode }
|
|
43
|
-
) => {
|
|
44
|
-
const mediaPlayerContext = useContext(MediaPlayerContext)
|
|
45
|
-
const mediaActor = MediaMachineContext.useActorRef()
|
|
46
|
-
const status = MediaMachineContext.useSelector((state) => state.value)
|
|
47
|
-
const volume = MediaMachineContext.useSelector((state) => state.context.media.volume)
|
|
48
|
-
const muted = MediaMachineContext.useSelector((state) => state.context.media.muted)
|
|
49
|
-
const isReady = MediaMachineContext.useSelector((state) => state.context.isReady)
|
|
50
|
-
const duration = MediaMachineContext.useSelector((state) => state.context.media.duration)
|
|
51
|
-
const [mediaVolume, setMediaVolume] = useLocalStorage('mediaVolume', '1') as [string, (newValue: string) => void]
|
|
52
|
-
const [mediaMute, setMediaMute] = useLocalStorage('mediaMute', 'false') as [booleanType, (newValue: booleanType) => void]
|
|
53
|
-
const [firstRender, setFirstRender] = useState(true)
|
|
54
|
-
|
|
55
|
-
useEffect(() => {
|
|
56
|
-
mediaActor.send({
|
|
57
|
-
type: 'MEDIA_SOURCE_OPTIONS',
|
|
58
|
-
mediaSourceOptions: {}
|
|
59
|
-
})
|
|
60
|
-
}, [])
|
|
61
|
-
|
|
62
|
-
useEffect(() => {
|
|
63
|
-
if (!options.autoplay || duration === undefined) return
|
|
64
|
-
mediaActor.send({ type: 'PLAY' })
|
|
65
|
-
}, [duration, options.autoplay])
|
|
66
|
-
|
|
67
|
-
useEffect(
|
|
68
|
-
() => {
|
|
69
|
-
if (!mediaActor || !isReady || !mediaPlayerContext.downloadedRanges) return
|
|
70
|
-
mediaActor.send({
|
|
71
|
-
type: 'DOWNLOADED_RANGES_UPDATED',
|
|
72
|
-
downloadedRanges: mediaPlayerContext.downloadedRanges
|
|
73
|
-
})
|
|
74
|
-
},
|
|
75
|
-
[
|
|
76
|
-
mediaActor,
|
|
77
|
-
isReady,
|
|
78
|
-
(
|
|
79
|
-
mediaPlayerContext
|
|
80
|
-
.downloadedRanges
|
|
81
|
-
?.map(range => `${range.startByteOffset}-${range.endByteOffset}`)
|
|
82
|
-
?? []
|
|
83
|
-
).join(',')
|
|
84
|
-
]
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
useEffect(() => {
|
|
88
|
-
const { size, read, publicPath, libavWorkerUrl, jassubWasmUrl } = options
|
|
89
|
-
if (!read || !size || !publicPath || !libavWorkerUrl || !jassubWasmUrl) return
|
|
90
|
-
|
|
91
|
-
mediaActor.send({
|
|
92
|
-
type: 'REMUXER_OPTIONS',
|
|
93
|
-
remuxerOptions: {
|
|
94
|
-
publicPath,
|
|
95
|
-
workerUrl: libavWorkerUrl,
|
|
96
|
-
bufferSize: options.bufferSize ?? BUFFER_SIZE,
|
|
97
|
-
length: size,
|
|
98
|
-
read
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
}, [options.read, options.size, options.publicPath, options.libavWorkerUrl, options.bufferSize])
|
|
102
|
-
|
|
103
|
-
useEffect(() => {
|
|
104
|
-
const { jassubWorkerUrl, jassubWasmUrl, jassubModernWasmUrl } = options
|
|
105
|
-
if (!jassubWorkerUrl || !jassubWasmUrl || !jassubModernWasmUrl) return
|
|
106
|
-
|
|
107
|
-
mediaActor.send({
|
|
108
|
-
type: 'SUBTITLES_RENDERER_OPTIONS',
|
|
109
|
-
subtitlesRendererOptions: {
|
|
110
|
-
workerUrl: jassubWorkerUrl,
|
|
111
|
-
wasmUrl: jassubWasmUrl,
|
|
112
|
-
modernWasmUrl: jassubModernWasmUrl
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
}, [options.publicPath, options.jassubWorkerUrl, options.jassubWasmUrl])
|
|
116
|
-
|
|
117
|
-
useEffect(() => {
|
|
118
|
-
if (!videoElement) return
|
|
119
|
-
mediaActor.send({
|
|
120
|
-
type: 'SET_VIDEO_ELEMENT',
|
|
121
|
-
videoElement,
|
|
122
|
-
})
|
|
123
|
-
}, [videoElement])
|
|
124
|
-
|
|
125
|
-
useEffect(() => {
|
|
126
|
-
if (!isReady) return
|
|
127
|
-
if (firstRender) {
|
|
128
|
-
mediaActor.send({
|
|
129
|
-
type: 'SET_VOLUME',
|
|
130
|
-
muted: mediaMute === 'true',
|
|
131
|
-
volume:
|
|
132
|
-
isNaN(Number(mediaVolume))
|
|
133
|
-
? 1
|
|
134
|
-
: Number(mediaVolume)
|
|
135
|
-
})
|
|
136
|
-
setFirstRender(false)
|
|
137
|
-
} else {
|
|
138
|
-
setMediaVolume(volume.toString())
|
|
139
|
-
setMediaMute(muted ? 'true' : 'false')
|
|
140
|
-
}
|
|
141
|
-
}, [isReady, volume, muted, firstRender])
|
|
142
|
-
|
|
143
|
-
useEffect(() => {
|
|
144
|
-
if (status !== 'OK') return
|
|
145
|
-
return () => {
|
|
146
|
-
mediaActor.send({ type: 'DESTROY' })
|
|
147
|
-
}
|
|
148
|
-
}, [status])
|
|
149
|
-
|
|
150
|
-
return (
|
|
151
|
-
<div css={FKNVideoRootStyle}>
|
|
152
|
-
<Chrome mediaInformation={options.mediaInformation} loadingInformation={options.loadingInformation}>
|
|
153
|
-
{children}
|
|
154
|
-
</Chrome>
|
|
155
|
-
</div>
|
|
156
|
-
)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const FKNVideo = (
|
|
160
|
-
{ ref, ...options }:
|
|
161
|
-
FKNVideoOptions & { ref?: RefCallback<HTMLVideoElement> | MutableRefObject<HTMLVideoElement | null> }
|
|
162
|
-
) => {
|
|
163
|
-
const updateContextFunction = (context: Parameters<MediaPlayerContextType['update']>[0]) => setMediaPlayerContext({ ...context, update: updateContextFunction })
|
|
164
|
-
const [chromeContext, setMediaPlayerContext] = useState<MediaPlayerContextType>({ update: updateContextFunction } as MediaPlayerContextType)
|
|
165
|
-
|
|
166
|
-
const [videoElement, setVideoElement] = useState<HTMLVideoElement | undefined>()
|
|
167
|
-
|
|
168
|
-
const refFunction: ClassAttributes<HTMLVideoElement>['ref'] = useCallback((element: HTMLVideoElement | null) => {
|
|
169
|
-
if (typeof ref === 'function') ref(element)
|
|
170
|
-
else if (ref && 'current' in ref) ref.current = element
|
|
171
|
-
setVideoElement(element ?? undefined)
|
|
172
|
-
}, [])
|
|
173
|
-
|
|
174
|
-
useEffect(() => {
|
|
175
|
-
setMediaPlayerContext((previousContext) => ({
|
|
176
|
-
...previousContext,
|
|
177
|
-
videoElement,
|
|
178
|
-
title: options?.title,
|
|
179
|
-
size: options?.size,
|
|
180
|
-
downloadedRanges: options?.downloadedRanges
|
|
181
|
-
}))
|
|
182
|
-
}, [videoElement, options?.title, options?.size, options?.downloadedRanges])
|
|
183
|
-
|
|
184
|
-
return (
|
|
185
|
-
<MediaPlayerContext.Provider value={chromeContext}>
|
|
186
|
-
<MediaMachineContext.Provider>
|
|
187
|
-
<FKNVideoRoot options={options} videoElement={videoElement}>
|
|
188
|
-
<video ref={refFunction} controls={false}/>
|
|
189
|
-
</FKNVideoRoot>
|
|
190
|
-
</MediaMachineContext.Provider>
|
|
191
|
-
</MediaPlayerContext.Provider>
|
|
192
|
-
)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
export default FKNVideo
|
|
1
|
+
/// <reference types="@emotion/react/types/css-prop" />
|
|
2
|
+
import type { ClassAttributes, MutableRefObject, ReactNode, RefCallback } from 'react'
|
|
3
|
+
import type { MediaPlayerContextType } from './utils/context'
|
|
4
|
+
|
|
5
|
+
import { useCallback, useContext, useEffect, useState } from 'react'
|
|
6
|
+
import { css } from '@emotion/react'
|
|
7
|
+
|
|
8
|
+
import { MediaMachineContext } from './state-machines'
|
|
9
|
+
import { MediaPlayerContext, DownloadedRange } from './utils/context'
|
|
10
|
+
import useLocalStorage, { booleanType } from './utils/use-local-storage'
|
|
11
|
+
import Chrome from './components/chrome'
|
|
12
|
+
|
|
13
|
+
const BUFFER_SIZE = 2_500_000
|
|
14
|
+
|
|
15
|
+
const FKNVideoRootStyle = css`
|
|
16
|
+
display: flex;
|
|
17
|
+
justify-content: center;
|
|
18
|
+
background-color: #111;
|
|
19
|
+
height: 100%;
|
|
20
|
+
width: 100%;
|
|
21
|
+
overflow: hidden;
|
|
22
|
+
`
|
|
23
|
+
|
|
24
|
+
export type FKNVideoOptions = {
|
|
25
|
+
title?: string
|
|
26
|
+
downloadedRanges?: DownloadedRange[]
|
|
27
|
+
read?: (offset: number, size: number) => Promise<ArrayBuffer>
|
|
28
|
+
size?: number
|
|
29
|
+
bufferSize?: number
|
|
30
|
+
autoplay?: boolean
|
|
31
|
+
mediaInformation?: ReactNode
|
|
32
|
+
loadingInformation?: ReactNode
|
|
33
|
+
publicPath: string
|
|
34
|
+
jassubWorkerUrl: string
|
|
35
|
+
jassubWasmUrl: string
|
|
36
|
+
jassubModernWasmUrl: string
|
|
37
|
+
libavWorkerUrl: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const FKNVideoRoot = (
|
|
41
|
+
{ options, videoElement, children }:
|
|
42
|
+
{ options: FKNVideoOptions, videoElement: HTMLVideoElement | undefined, children: ReactNode }
|
|
43
|
+
) => {
|
|
44
|
+
const mediaPlayerContext = useContext(MediaPlayerContext)
|
|
45
|
+
const mediaActor = MediaMachineContext.useActorRef()
|
|
46
|
+
const status = MediaMachineContext.useSelector((state) => state.value)
|
|
47
|
+
const volume = MediaMachineContext.useSelector((state) => state.context.media.volume)
|
|
48
|
+
const muted = MediaMachineContext.useSelector((state) => state.context.media.muted)
|
|
49
|
+
const isReady = MediaMachineContext.useSelector((state) => state.context.isReady)
|
|
50
|
+
const duration = MediaMachineContext.useSelector((state) => state.context.media.duration)
|
|
51
|
+
const [mediaVolume, setMediaVolume] = useLocalStorage('mediaVolume', '1') as [string, (newValue: string) => void]
|
|
52
|
+
const [mediaMute, setMediaMute] = useLocalStorage('mediaMute', 'false') as [booleanType, (newValue: booleanType) => void]
|
|
53
|
+
const [firstRender, setFirstRender] = useState(true)
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
mediaActor.send({
|
|
57
|
+
type: 'MEDIA_SOURCE_OPTIONS',
|
|
58
|
+
mediaSourceOptions: {}
|
|
59
|
+
})
|
|
60
|
+
}, [])
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (!options.autoplay || duration === undefined) return
|
|
64
|
+
mediaActor.send({ type: 'PLAY' })
|
|
65
|
+
}, [duration, options.autoplay])
|
|
66
|
+
|
|
67
|
+
useEffect(
|
|
68
|
+
() => {
|
|
69
|
+
if (!mediaActor || !isReady || !mediaPlayerContext.downloadedRanges) return
|
|
70
|
+
mediaActor.send({
|
|
71
|
+
type: 'DOWNLOADED_RANGES_UPDATED',
|
|
72
|
+
downloadedRanges: mediaPlayerContext.downloadedRanges
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
[
|
|
76
|
+
mediaActor,
|
|
77
|
+
isReady,
|
|
78
|
+
(
|
|
79
|
+
mediaPlayerContext
|
|
80
|
+
.downloadedRanges
|
|
81
|
+
?.map(range => `${range.startByteOffset}-${range.endByteOffset}`)
|
|
82
|
+
?? []
|
|
83
|
+
).join(',')
|
|
84
|
+
]
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
const { size, read, publicPath, libavWorkerUrl, jassubWasmUrl } = options
|
|
89
|
+
if (!read || !size || !publicPath || !libavWorkerUrl || !jassubWasmUrl) return
|
|
90
|
+
|
|
91
|
+
mediaActor.send({
|
|
92
|
+
type: 'REMUXER_OPTIONS',
|
|
93
|
+
remuxerOptions: {
|
|
94
|
+
publicPath,
|
|
95
|
+
workerUrl: libavWorkerUrl,
|
|
96
|
+
bufferSize: options.bufferSize ?? BUFFER_SIZE,
|
|
97
|
+
length: size,
|
|
98
|
+
read
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
}, [options.read, options.size, options.publicPath, options.libavWorkerUrl, options.bufferSize])
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
const { jassubWorkerUrl, jassubWasmUrl, jassubModernWasmUrl } = options
|
|
105
|
+
if (!jassubWorkerUrl || !jassubWasmUrl || !jassubModernWasmUrl) return
|
|
106
|
+
|
|
107
|
+
mediaActor.send({
|
|
108
|
+
type: 'SUBTITLES_RENDERER_OPTIONS',
|
|
109
|
+
subtitlesRendererOptions: {
|
|
110
|
+
workerUrl: jassubWorkerUrl,
|
|
111
|
+
wasmUrl: jassubWasmUrl,
|
|
112
|
+
modernWasmUrl: jassubModernWasmUrl
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
}, [options.publicPath, options.jassubWorkerUrl, options.jassubWasmUrl])
|
|
116
|
+
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
if (!videoElement) return
|
|
119
|
+
mediaActor.send({
|
|
120
|
+
type: 'SET_VIDEO_ELEMENT',
|
|
121
|
+
videoElement,
|
|
122
|
+
})
|
|
123
|
+
}, [videoElement])
|
|
124
|
+
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
if (!isReady) return
|
|
127
|
+
if (firstRender) {
|
|
128
|
+
mediaActor.send({
|
|
129
|
+
type: 'SET_VOLUME',
|
|
130
|
+
muted: mediaMute === 'true',
|
|
131
|
+
volume:
|
|
132
|
+
isNaN(Number(mediaVolume))
|
|
133
|
+
? 1
|
|
134
|
+
: Number(mediaVolume)
|
|
135
|
+
})
|
|
136
|
+
setFirstRender(false)
|
|
137
|
+
} else {
|
|
138
|
+
setMediaVolume(volume.toString())
|
|
139
|
+
setMediaMute(muted ? 'true' : 'false')
|
|
140
|
+
}
|
|
141
|
+
}, [isReady, volume, muted, firstRender])
|
|
142
|
+
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
if (status !== 'OK') return
|
|
145
|
+
return () => {
|
|
146
|
+
mediaActor.send({ type: 'DESTROY' })
|
|
147
|
+
}
|
|
148
|
+
}, [status])
|
|
149
|
+
|
|
150
|
+
return (
|
|
151
|
+
<div css={FKNVideoRootStyle}>
|
|
152
|
+
<Chrome mediaInformation={options.mediaInformation} loadingInformation={options.loadingInformation}>
|
|
153
|
+
{children}
|
|
154
|
+
</Chrome>
|
|
155
|
+
</div>
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const FKNVideo = (
|
|
160
|
+
{ ref, ...options }:
|
|
161
|
+
FKNVideoOptions & { ref?: RefCallback<HTMLVideoElement> | MutableRefObject<HTMLVideoElement | null> }
|
|
162
|
+
) => {
|
|
163
|
+
const updateContextFunction = (context: Parameters<MediaPlayerContextType['update']>[0]) => setMediaPlayerContext({ ...context, update: updateContextFunction })
|
|
164
|
+
const [chromeContext, setMediaPlayerContext] = useState<MediaPlayerContextType>({ update: updateContextFunction } as MediaPlayerContextType)
|
|
165
|
+
|
|
166
|
+
const [videoElement, setVideoElement] = useState<HTMLVideoElement | undefined>()
|
|
167
|
+
|
|
168
|
+
const refFunction: ClassAttributes<HTMLVideoElement>['ref'] = useCallback((element: HTMLVideoElement | null) => {
|
|
169
|
+
if (typeof ref === 'function') ref(element)
|
|
170
|
+
else if (ref && 'current' in ref) ref.current = element
|
|
171
|
+
setVideoElement(element ?? undefined)
|
|
172
|
+
}, [])
|
|
173
|
+
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
setMediaPlayerContext((previousContext) => ({
|
|
176
|
+
...previousContext,
|
|
177
|
+
videoElement,
|
|
178
|
+
title: options?.title,
|
|
179
|
+
size: options?.size,
|
|
180
|
+
downloadedRanges: options?.downloadedRanges
|
|
181
|
+
}))
|
|
182
|
+
}, [videoElement, options?.title, options?.size, options?.downloadedRanges])
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<MediaPlayerContext.Provider value={chromeContext}>
|
|
186
|
+
<MediaMachineContext.Provider>
|
|
187
|
+
<FKNVideoRoot options={options} videoElement={videoElement}>
|
|
188
|
+
<video ref={refFunction} controls={false}/>
|
|
189
|
+
</FKNVideoRoot>
|
|
190
|
+
</MediaMachineContext.Provider>
|
|
191
|
+
</MediaPlayerContext.Provider>
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export default FKNVideo
|