@banou/media-player 0.8.10 → 0.8.11
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/dist/index.d.ts +1 -1
- package/dist/index.js +475 -302
- package/dist/react/components/errors.d.ts +15 -0
- package/dist/react/player.d.ts +1 -0
- package/dist/react/source-feature.d.ts +36 -0
- package/package.json +1 -1
- package/src/lib/index.tsx +1 -1
- package/src/lib/react/components/control-bar.tsx +4 -2
- package/src/lib/react/components/errors.tsx +194 -0
- package/src/lib/react/components/track-menu.tsx +2 -2
- package/src/lib/react/hooks/use-playback.ts +87 -20
- package/src/lib/react/source-feature.ts +29 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { PlaybackErrorEntry } from '../source-feature';
|
|
2
|
+
/** What lands on the clipboard: the same thing the panel shows, in the order it happened. */
|
|
3
|
+
export declare const formatErrors: (errors: PlaybackErrorEntry[]) => string;
|
|
4
|
+
/**
|
|
5
|
+
* The failures this session had, offered only once there has been one.
|
|
6
|
+
*
|
|
7
|
+
* It exists because the interesting failures are now the ones the viewer never sees: a media element
|
|
8
|
+
* that firefox wedged is rebuilt underneath them and playback carries on, which is the right
|
|
9
|
+
* behaviour and also erases the evidence. Without this, "it glitched and carried on" is unreportable.
|
|
10
|
+
*
|
|
11
|
+
* Copy is the point of the whole control. A decoder message is two hundred characters of C++ and
|
|
12
|
+
* nobody is going to retype it into an issue.
|
|
13
|
+
*/
|
|
14
|
+
export declare const ErrorsAction: () => import("@emotion/react/jsx-runtime").JSX.Element | null;
|
|
15
|
+
export default ErrorsAction;
|
package/dist/react/player.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ export declare const Player: import("@videojs/react").CreatePlayerResult<import(
|
|
|
18
18
|
pictureInPictureMode: import("../engine").PictureInPictureMode | null;
|
|
19
19
|
burnedInSubtitles: boolean;
|
|
20
20
|
playbackError: unknown;
|
|
21
|
+
playbackErrors: import("./source-feature").PlaybackErrorEntry[];
|
|
21
22
|
ready: boolean;
|
|
22
23
|
setSourceState: (partial: Partial<import("./source-feature").SourceState>) => void;
|
|
23
24
|
}>]>>;
|
|
@@ -18,6 +18,24 @@ export type TrackChoice = {
|
|
|
18
18
|
*/
|
|
19
19
|
disabled?: boolean;
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* One failure, kept so it can be read back and copied out long after playback recovered from it.
|
|
23
|
+
*
|
|
24
|
+
* Flattened to strings at the moment it happens rather than held as the `Error`: this is a report,
|
|
25
|
+
* the cause chain is most of what makes it useful, and an `Error` in a store is a live object whose
|
|
26
|
+
* `cause` may be a `MediaError` that reads differently once the element has moved on.
|
|
27
|
+
*/
|
|
28
|
+
export type PlaybackErrorEntry = {
|
|
29
|
+
/** Wall clock, so the report can be read next to a console log or a torrent's timeline. */
|
|
30
|
+
at: number;
|
|
31
|
+
/** Seconds into the media, which is usually the first question asked of a playback failure. */
|
|
32
|
+
atMediaTime?: number;
|
|
33
|
+
message: string;
|
|
34
|
+
/** The `cause` chain, already unwound, one line per level. */
|
|
35
|
+
detail?: string;
|
|
36
|
+
/** Whether the pipeline came back from it by itself. */
|
|
37
|
+
recovered: boolean;
|
|
38
|
+
};
|
|
21
39
|
/**
|
|
22
40
|
* A byte span of the file the consumer has in hand, mapped onto the timeline through the keyframe
|
|
23
41
|
* index, because a file's download percentage is not its playback percentage.
|
|
@@ -88,6 +106,15 @@ export type SourceState = {
|
|
|
88
106
|
burnedInSubtitles: boolean;
|
|
89
107
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
90
108
|
playbackError: unknown;
|
|
109
|
+
/**
|
|
110
|
+
* Every failure this source has had, oldest first, whether or not the viewer ever saw one.
|
|
111
|
+
*
|
|
112
|
+
* `playbackError` is the CURRENT state and is cleared on recovery, so on its own it hides exactly
|
|
113
|
+
* the failures worth knowing about: a media element that firefox wedged is rebuilt and playback
|
|
114
|
+
* carries on, leaving no trace anywhere. This is the record, and the control bar offers it only
|
|
115
|
+
* once there is something in it.
|
|
116
|
+
*/
|
|
117
|
+
playbackErrors: PlaybackErrorEntry[];
|
|
91
118
|
/** Whether the engine has produced its first media segment. */
|
|
92
119
|
ready: boolean;
|
|
93
120
|
/**
|
|
@@ -154,6 +181,15 @@ export declare const sourceFeature: import("@videojs/react").PlayerFeature<{
|
|
|
154
181
|
burnedInSubtitles: boolean;
|
|
155
182
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
156
183
|
playbackError: unknown;
|
|
184
|
+
/**
|
|
185
|
+
* Every failure this source has had, oldest first, whether or not the viewer ever saw one.
|
|
186
|
+
*
|
|
187
|
+
* `playbackError` is the CURRENT state and is cleared on recovery, so on its own it hides exactly
|
|
188
|
+
* the failures worth knowing about: a media element that firefox wedged is rebuilt and playback
|
|
189
|
+
* carries on, leaving no trace anywhere. This is the record, and the control bar offers it only
|
|
190
|
+
* once there is something in it.
|
|
191
|
+
*/
|
|
192
|
+
playbackErrors: PlaybackErrorEntry[];
|
|
157
193
|
/** Whether the engine has produced its first media segment. */
|
|
158
194
|
ready: boolean;
|
|
159
195
|
/**
|
package/package.json
CHANGED
package/src/lib/index.tsx
CHANGED
|
@@ -24,7 +24,7 @@ export type { PlayerStore } from './react/player'
|
|
|
24
24
|
// Source state lives on the player store next to the built-in playback state, so `usePlayer` is the
|
|
25
25
|
// only hook the chrome needs.
|
|
26
26
|
export { sourceFeature } from './react/source-feature'
|
|
27
|
-
export type { DownloadedRange, SourceState } from './react/source-feature'
|
|
27
|
+
export type { DownloadedRange, PlaybackErrorEntry, SourceState } from './react/source-feature'
|
|
28
28
|
|
|
29
29
|
export { useSeekThumbnails } from './react/hooks/use-thumbnails'
|
|
30
30
|
export { usePictureInPicture } from './react/hooks/use-picture-in-picture'
|
|
@@ -11,6 +11,7 @@ import { TooltipDisplay } from './tooltip-display'
|
|
|
11
11
|
import { ProgressBar } from './progress-bar'
|
|
12
12
|
import pictureInPicture from '../../assets/picture-in-picture.svg'
|
|
13
13
|
import { SubtitlesInPicture } from './icons'
|
|
14
|
+
import ErrorsAction from './errors'
|
|
14
15
|
import SettingsAction from './settings'
|
|
15
16
|
import SubtitlesAction from './subtitles'
|
|
16
17
|
import colors from '../../utils/colors'
|
|
@@ -106,7 +107,7 @@ const style = css`
|
|
|
106
107
|
stroke: ${colors.accent};
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
.play, .sound, .time, .subtitles, .settings, .picture-in-picture, .full-screen {
|
|
110
|
+
.play, .sound, .time, .errors, .subtitles, .settings, .picture-in-picture, .full-screen {
|
|
110
111
|
display: flex;
|
|
111
112
|
align-items: center;
|
|
112
113
|
|
|
@@ -124,7 +125,7 @@ const style = css`
|
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
|
|
127
|
-
.play, .sound, .subtitles, .settings, .picture-in-picture, .full-screen {
|
|
128
|
+
.play, .sound, .errors, .subtitles, .settings, .picture-in-picture, .full-screen {
|
|
128
129
|
border-radius: 4px;
|
|
129
130
|
|
|
130
131
|
padding: 8px;
|
|
@@ -307,6 +308,7 @@ export const ControlBar = () => {
|
|
|
307
308
|
</div>
|
|
308
309
|
</div>
|
|
309
310
|
<div className='right'>
|
|
311
|
+
<ErrorsAction />
|
|
310
312
|
<SubtitlesAction />
|
|
311
313
|
<SettingsAction />
|
|
312
314
|
{togglePictureInPicture
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/// <reference types="@emotion/react/types/css-prop" />
|
|
2
|
+
import type { PlaybackErrorEntry } from '../source-feature'
|
|
3
|
+
|
|
4
|
+
import { useState } from 'react'
|
|
5
|
+
import { css } from '@emotion/react'
|
|
6
|
+
import { AlertTriangle, Check, Copy } from 'react-feather'
|
|
7
|
+
|
|
8
|
+
import { fonts } from '../../utils/fonts'
|
|
9
|
+
import { formatTime } from '../../utils/time'
|
|
10
|
+
import { usePlayer } from '../player'
|
|
11
|
+
import { TooltipDisplay } from './tooltip-display'
|
|
12
|
+
import { popoverStyle, useTrackMenu } from './track-menu'
|
|
13
|
+
|
|
14
|
+
const style = css`
|
|
15
|
+
/* Not a containing block, for the reason given on popoverStyle: the menu anchors to the control bar,
|
|
16
|
+
which is the width of the player box, so it can be clamped to it. */
|
|
17
|
+
position: static;
|
|
18
|
+
|
|
19
|
+
.errors {
|
|
20
|
+
/* the icon keeps its size, the pressable box grows around it */
|
|
21
|
+
@media (pointer: coarse) {
|
|
22
|
+
box-sizing: border-box;
|
|
23
|
+
justify-content: center;
|
|
24
|
+
|
|
25
|
+
min-width: 44px;
|
|
26
|
+
min-height: 44px;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
${popoverStyle}
|
|
31
|
+
|
|
32
|
+
.popover.error-list {
|
|
33
|
+
/* Wider than a track menu and taller, because these rows are sentences rather than labels, and a
|
|
34
|
+
decoder message that has been ellipsized is not worth copying. */
|
|
35
|
+
width: 420px;
|
|
36
|
+
max-height: min(300px, calc(100cqh - 100% - 16px));
|
|
37
|
+
|
|
38
|
+
/* track-menu's header is a left-aligned label row; here it carries a control too, so the two
|
|
39
|
+
ends go to the two ends */
|
|
40
|
+
.back {
|
|
41
|
+
justify-content: space-between;
|
|
42
|
+
gap: 8px;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.copy {
|
|
46
|
+
/* sits in the header row, which track-menu styles as .back */
|
|
47
|
+
display: flex;
|
|
48
|
+
align-items: center;
|
|
49
|
+
gap: 6px;
|
|
50
|
+
|
|
51
|
+
background: none;
|
|
52
|
+
border: none;
|
|
53
|
+
padding: 4px 6px;
|
|
54
|
+
border-radius: 4px;
|
|
55
|
+
|
|
56
|
+
color: #fff;
|
|
57
|
+
cursor: pointer;
|
|
58
|
+
${fonts.bSmall.regular}
|
|
59
|
+
|
|
60
|
+
&:hover {
|
|
61
|
+
background-color: rgba(255,255,255,.1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
svg {
|
|
65
|
+
width: 14px;
|
|
66
|
+
height: 14px;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.entry {
|
|
71
|
+
/* one failure is a block of text, so it stacks instead of sitting on one line */
|
|
72
|
+
display: flex;
|
|
73
|
+
flex-direction: column;
|
|
74
|
+
align-items: flex-start;
|
|
75
|
+
gap: 2px;
|
|
76
|
+
|
|
77
|
+
.when {
|
|
78
|
+
color: #bbb;
|
|
79
|
+
${fonts.bSmall.regular}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.what {
|
|
83
|
+
/* the decoder's own words, which are long and must not be cut */
|
|
84
|
+
overflow-wrap: anywhere;
|
|
85
|
+
white-space: pre-wrap;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.cause {
|
|
89
|
+
color: #ddd;
|
|
90
|
+
overflow-wrap: anywhere;
|
|
91
|
+
white-space: pre-wrap;
|
|
92
|
+
${fonts.bSmall.regular}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
`
|
|
97
|
+
|
|
98
|
+
const clock = (at: number) => new Date(at).toLocaleTimeString()
|
|
99
|
+
|
|
100
|
+
// the chrome's own formatter, so a position in the log reads exactly like the one on the seekbar
|
|
101
|
+
const mediaTime = (seconds: number | undefined) =>
|
|
102
|
+
seconds === undefined || !Number.isFinite(seconds) ? undefined : formatTime(Math.max(0, seconds))
|
|
103
|
+
|
|
104
|
+
/** What lands on the clipboard: the same thing the panel shows, in the order it happened. */
|
|
105
|
+
export const formatErrors = (errors: PlaybackErrorEntry[]) =>
|
|
106
|
+
errors
|
|
107
|
+
.map((entry, index) => {
|
|
108
|
+
const at = mediaTime(entry.atMediaTime)
|
|
109
|
+
const head = `${index + 1}. ${new Date(entry.at).toISOString()}${at ? ` (at ${at})` : ''}${entry.recovered ? ' [recovered]' : ''}`
|
|
110
|
+
return [head, entry.message, entry.detail].filter(Boolean).join('\n')
|
|
111
|
+
})
|
|
112
|
+
.join('\n\n')
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The failures this session had, offered only once there has been one.
|
|
116
|
+
*
|
|
117
|
+
* It exists because the interesting failures are now the ones the viewer never sees: a media element
|
|
118
|
+
* that firefox wedged is rebuilt underneath them and playback carries on, which is the right
|
|
119
|
+
* behaviour and also erases the evidence. Without this, "it glitched and carried on" is unreportable.
|
|
120
|
+
*
|
|
121
|
+
* Copy is the point of the whole control. A decoder message is two hundred characters of C++ and
|
|
122
|
+
* nobody is going to retype it into an issue.
|
|
123
|
+
*/
|
|
124
|
+
export const ErrorsAction = () => {
|
|
125
|
+
const errors = usePlayer((state) => state.playbackErrors)
|
|
126
|
+
const { open, toggle, containerRef } = useTrackMenu()
|
|
127
|
+
const [copied, setCopied] = useState(false)
|
|
128
|
+
|
|
129
|
+
// After every hook, never before. Nothing to report means no button at all, rather than a control
|
|
130
|
+
// that is present and says "no errors": the bar is not the place to advertise that.
|
|
131
|
+
if (errors.length === 0) return null
|
|
132
|
+
|
|
133
|
+
const copy = () => {
|
|
134
|
+
// Deliberately unawaited state: the tick is feedback, not a promise the viewer waits on. A
|
|
135
|
+
// clipboard the browser refuses (no permission, insecure context) simply never ticks.
|
|
136
|
+
navigator.clipboard?.writeText(formatErrors(errors)).then(
|
|
137
|
+
() => {
|
|
138
|
+
setCopied(true)
|
|
139
|
+
setTimeout(() => setCopied(false), 2_000)
|
|
140
|
+
},
|
|
141
|
+
(error) => console.warn('[media-player] could not copy the errors:', error),
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const label = `Playback errors (${errors.length})`
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<div css={style} ref={containerRef}>
|
|
149
|
+
<TooltipDisplay
|
|
150
|
+
id='errors'
|
|
151
|
+
disabled={open}
|
|
152
|
+
text={
|
|
153
|
+
<button
|
|
154
|
+
className='errors'
|
|
155
|
+
type='button'
|
|
156
|
+
onClick={toggle}
|
|
157
|
+
aria-label={label}
|
|
158
|
+
aria-expanded={open}
|
|
159
|
+
>
|
|
160
|
+
<AlertTriangle className='alert-triangle' />
|
|
161
|
+
</button>
|
|
162
|
+
}
|
|
163
|
+
toolTipText={<span>{label}</span>}
|
|
164
|
+
/>
|
|
165
|
+
{
|
|
166
|
+
open && (
|
|
167
|
+
<div className='popover error-list'>
|
|
168
|
+
{/* `no-hover` because the header is a label with a control in it, not a row to click */}
|
|
169
|
+
<div className='back no-hover'>
|
|
170
|
+
<span>{label}</span>
|
|
171
|
+
<button className='copy' type='button' onClick={copy} aria-label='Copy the errors'>
|
|
172
|
+
{copied ? <Check /> : <Copy />}
|
|
173
|
+
<span>{copied ? 'Copied' : 'Copy'}</span>
|
|
174
|
+
</button>
|
|
175
|
+
</div>
|
|
176
|
+
{errors.map((entry, index) => (
|
|
177
|
+
<div className='entry no-hover' key={`${entry.at}-${index}`}>
|
|
178
|
+
<span className='when'>
|
|
179
|
+
{clock(entry.at)}
|
|
180
|
+
{mediaTime(entry.atMediaTime) ? ` at ${mediaTime(entry.atMediaTime)}` : ''}
|
|
181
|
+
{entry.recovered ? ' recovered' : ''}
|
|
182
|
+
</span>
|
|
183
|
+
<span className='what'>{entry.message}</span>
|
|
184
|
+
{entry.detail ? <span className='cause'>{entry.detail}</span> : null}
|
|
185
|
+
</div>
|
|
186
|
+
))}
|
|
187
|
+
</div>
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
</div>
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export default ErrorsAction
|
|
@@ -83,10 +83,10 @@ export const popoverStyle = css`
|
|
|
83
83
|
:last-of-type {
|
|
84
84
|
border-radius: 0 0 8px 8px;
|
|
85
85
|
}
|
|
86
|
-
|
|
86
|
+
&:not(.no-hover) {
|
|
87
87
|
cursor: pointer;
|
|
88
88
|
}
|
|
89
|
-
|
|
89
|
+
&:not(.no-hover):hover {
|
|
90
90
|
background-color: rgba(255,255,255,.1);
|
|
91
91
|
}
|
|
92
92
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PlaybackController } from '../../engine'
|
|
2
|
+
import type { PlaybackErrorEntry } from '../source-feature'
|
|
2
3
|
import type { MediaPlayerLocalOptions } from '../video-player'
|
|
3
4
|
|
|
4
5
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
@@ -7,10 +8,47 @@ import { isMediaElementError, startPlayback } from '../../engine'
|
|
|
7
8
|
import { usePlayer } from '../player'
|
|
8
9
|
import { toNamedTracks } from '../../utils/track-label'
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
/**
|
|
12
|
+
* A wedged element is rebuilt, however many times it takes.
|
|
13
|
+
*
|
|
14
|
+
* There is deliberately no ceiling. The failure this exists for is a firefox decoder that will not
|
|
15
|
+
* take another packet, it is not the file's fault, and a viewer forty minutes into an episode is
|
|
16
|
+
* not helped by a budget running out. Every rebuild is recorded instead, and the control bar offers
|
|
17
|
+
* the record, so a file that genuinely cannot play is visible as a wall of identical entries rather
|
|
18
|
+
* than hidden behind a counter.
|
|
19
|
+
*
|
|
20
|
+
* The backoff is the whole guard: rebuilds that keep failing immediately slow down, so a source that
|
|
21
|
+
* fails deterministically settles into a slow retry instead of a hot loop that pins a core and
|
|
22
|
+
* reloads libav as fast as it can.
|
|
23
|
+
*/
|
|
24
|
+
const RESTART_BACKOFF_MS = [0, 250, 1_000, 3_000, 10_000]
|
|
25
|
+
// far enough back that an unrelated hiccup later in an episode starts from no delay again
|
|
26
|
+
const RESTART_SETTLED_MS = 60_000
|
|
27
|
+
|
|
28
|
+
const messageOf = (error: unknown) =>
|
|
29
|
+
error instanceof Error ? error.message : String(error)
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The `cause` chain, unwound to text at the moment it happened.
|
|
33
|
+
*
|
|
34
|
+
* The reason a decode failure is worth copying out at all is almost always one level down: the top
|
|
35
|
+
* line says the media element failed, and the cause carries what the decoder actually said. A
|
|
36
|
+
* `MediaError` is not an `Error`, so it is read for its own two fields rather than skipped.
|
|
37
|
+
*/
|
|
38
|
+
const causeChain = (error: unknown) => {
|
|
39
|
+
const lines: string[] = []
|
|
40
|
+
let cause: unknown = (error as { cause?: unknown })?.cause
|
|
41
|
+
// bounded, because a cause chain can be circular and this runs inside an error path
|
|
42
|
+
for (let depth = 0; cause != null && depth < 8; depth++) {
|
|
43
|
+
if (typeof MediaError !== 'undefined' && cause instanceof MediaError) {
|
|
44
|
+
lines.push(`MediaError code ${cause.code}${cause.message ? `: ${cause.message}` : ''}`)
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
lines.push(messageOf(cause))
|
|
48
|
+
cause = (cause as { cause?: unknown })?.cause
|
|
49
|
+
}
|
|
50
|
+
return lines.length ? lines.join('\n') : undefined
|
|
51
|
+
}
|
|
14
52
|
|
|
15
53
|
/**
|
|
16
54
|
* Owns the engine for the life of a source: start, teardown, and every piece of state the pipeline
|
|
@@ -39,18 +77,26 @@ export const usePlayback = (
|
|
|
39
77
|
*
|
|
40
78
|
* Firefox can wedge its own decoder: when the source buffer runs dry it drains the decoder so the
|
|
41
79
|
* frames still inside it get shown, and clearing that drain needs a decoded sample to resume
|
|
42
|
-
* from. A seek into an empty buffer
|
|
43
|
-
*
|
|
44
|
-
*
|
|
80
|
+
* from. A seek into an empty buffer has none, so the drain is never cleared and every packet
|
|
81
|
+
* after it comes back `avcodec_send_packet error: End of file`. The element is finished at that
|
|
82
|
+
* point and no append can revive it. It is not ours: it reproduces on this player as it stood in
|
|
83
|
+
* October 2025, on a local file, and on every version since.
|
|
45
84
|
*
|
|
46
85
|
* Rebuilding is the cure, and this hook already does exactly that for an audio track change,
|
|
47
86
|
* position and all, so the recovery is a dep rather than a second teardown path.
|
|
48
87
|
*/
|
|
49
88
|
const [restartToken, setRestartToken] = useState(0)
|
|
50
89
|
const restarts = useRef({ count: 0, at: 0 })
|
|
51
|
-
|
|
52
|
-
//
|
|
53
|
-
|
|
90
|
+
const restartTimer = useRef<ReturnType<typeof setTimeout>>(undefined)
|
|
91
|
+
// The streak belongs to one media, not to the player, and a pending rebuild of the old one must
|
|
92
|
+
// not land on the new one.
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
restarts.current = { count: 0, at: 0 }
|
|
95
|
+
player.setSourceState({ playbackErrors: [] })
|
|
96
|
+
return () => { if (restartTimer.current) clearTimeout(restartTimer.current) }
|
|
97
|
+
// `player` is stable; listing it would not re-run this, and the reset belongs to the media
|
|
98
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
99
|
+
}, [size])
|
|
54
100
|
|
|
55
101
|
const controllerRef = useRef<PlaybackController | null>(null)
|
|
56
102
|
/**
|
|
@@ -104,20 +150,41 @@ export const usePlayback = (
|
|
|
104
150
|
if (!video || !canvas || !size || !read) return
|
|
105
151
|
let cancelled = false
|
|
106
152
|
player.setSourceState({ playbackError: null, ready: false })
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Keep the failure, whether or not the viewer is about to be told about it.
|
|
156
|
+
*
|
|
157
|
+
* Appended rather than replaced, and never cleared by a recovery: a rebuild that works leaves
|
|
158
|
+
* `playbackError` null and would otherwise erase the only evidence that anything went wrong.
|
|
159
|
+
*/
|
|
160
|
+
const record = (error: unknown, recovered: boolean) => {
|
|
161
|
+
const entry: PlaybackErrorEntry = {
|
|
162
|
+
at: Date.now(),
|
|
163
|
+
atMediaTime: Number.isFinite(video.currentTime) ? video.currentTime : undefined,
|
|
164
|
+
message: messageOf(error),
|
|
165
|
+
detail: causeChain(error),
|
|
166
|
+
recovered,
|
|
167
|
+
}
|
|
168
|
+
player.setSourceState({ playbackErrors: [...player.playbackErrors, entry] })
|
|
169
|
+
}
|
|
107
170
|
const fail = (error: unknown) => {
|
|
108
171
|
if (cancelled) return
|
|
172
|
+
const recoverable = isMediaElementError(error)
|
|
173
|
+
record(error, recoverable)
|
|
109
174
|
// Not something the viewer can act on and not something an append can survive: rebuild the
|
|
110
|
-
// element instead of putting a dead player behind an error message.
|
|
111
|
-
|
|
112
|
-
if (isMediaElementError(error)) {
|
|
175
|
+
// element instead of putting a dead player behind an error message.
|
|
176
|
+
if (recoverable) {
|
|
113
177
|
const now = performance.now()
|
|
114
|
-
|
|
115
|
-
if (restarts.current.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
178
|
+
// a failure long after the last one is not part of a streak, so it pays no delay
|
|
179
|
+
if (now - restarts.current.at > RESTART_SETTLED_MS) restarts.current = { count: 0, at: now }
|
|
180
|
+
const delay = RESTART_BACKOFF_MS[Math.min(restarts.current.count, RESTART_BACKOFF_MS.length - 1)]!
|
|
181
|
+
restarts.current = { count: restarts.current.count + 1, at: now }
|
|
182
|
+
console.warn(`the media element failed; rebuilding the pipeline${delay ? ` in ${delay}ms` : ''}`, error)
|
|
183
|
+
if (restartTimer.current) clearTimeout(restartTimer.current)
|
|
184
|
+
// still queued through a timer at zero delay, so the rebuild never runs inside the callback
|
|
185
|
+
// that reported the failure
|
|
186
|
+
restartTimer.current = setTimeout(() => setRestartToken((token) => token + 1), delay)
|
|
187
|
+
return
|
|
121
188
|
}
|
|
122
189
|
console.error('playback failed', error)
|
|
123
190
|
player.setSourceState({ playbackError: error })
|
|
@@ -22,6 +22,25 @@ export type TrackChoice = {
|
|
|
22
22
|
disabled?: boolean
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* One failure, kept so it can be read back and copied out long after playback recovered from it.
|
|
27
|
+
*
|
|
28
|
+
* Flattened to strings at the moment it happens rather than held as the `Error`: this is a report,
|
|
29
|
+
* the cause chain is most of what makes it useful, and an `Error` in a store is a live object whose
|
|
30
|
+
* `cause` may be a `MediaError` that reads differently once the element has moved on.
|
|
31
|
+
*/
|
|
32
|
+
export type PlaybackErrorEntry = {
|
|
33
|
+
/** Wall clock, so the report can be read next to a console log or a torrent's timeline. */
|
|
34
|
+
at: number
|
|
35
|
+
/** Seconds into the media, which is usually the first question asked of a playback failure. */
|
|
36
|
+
atMediaTime?: number
|
|
37
|
+
message: string
|
|
38
|
+
/** The `cause` chain, already unwound, one line per level. */
|
|
39
|
+
detail?: string
|
|
40
|
+
/** Whether the pipeline came back from it by itself. */
|
|
41
|
+
recovered: boolean
|
|
42
|
+
}
|
|
43
|
+
|
|
25
44
|
/**
|
|
26
45
|
* A byte span of the file the consumer has in hand, mapped onto the timeline through the keyframe
|
|
27
46
|
* index, because a file's download percentage is not its playback percentage.
|
|
@@ -100,6 +119,15 @@ export type SourceState = {
|
|
|
100
119
|
|
|
101
120
|
/** Set when the pipeline fails. Cleared when it recovers. */
|
|
102
121
|
playbackError: unknown
|
|
122
|
+
/**
|
|
123
|
+
* Every failure this source has had, oldest first, whether or not the viewer ever saw one.
|
|
124
|
+
*
|
|
125
|
+
* `playbackError` is the CURRENT state and is cleared on recovery, so on its own it hides exactly
|
|
126
|
+
* the failures worth knowing about: a media element that firefox wedged is rebuilt and playback
|
|
127
|
+
* carries on, leaving no trace anywhere. This is the record, and the control bar offers it only
|
|
128
|
+
* once there is something in it.
|
|
129
|
+
*/
|
|
130
|
+
playbackErrors: PlaybackErrorEntry[]
|
|
103
131
|
/** Whether the engine has produced its first media segment. */
|
|
104
132
|
ready: boolean
|
|
105
133
|
|
|
@@ -128,6 +156,7 @@ const initialState: SourceState = {
|
|
|
128
156
|
pictureInPictureMode: null,
|
|
129
157
|
burnedInSubtitles: false,
|
|
130
158
|
playbackError: null,
|
|
159
|
+
playbackErrors: [],
|
|
131
160
|
ready: false,
|
|
132
161
|
setSourceState: () => {},
|
|
133
162
|
}
|