@banou/media-player 0.10.37 → 0.10.39

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.
@@ -167,11 +167,15 @@ export type MediaPlayerLocalOptions = CommonOptions & MediaPlayerSource & {
167
167
  jassubWorkerUrl: string;
168
168
  /** jassub's SIMD build, `jassub-worker-modern.wasm`. */
169
169
  jassubWasmUrl: string;
170
+ /** `jassub-worker-modern.js`, the script that runs `jassubWasmUrl`, from the same jassub version. */
171
+ jassubWasmWorkerUrl: string;
170
172
  /**
171
- * jassub's non-SIMD build, `jassub-worker.wasm`, for Safari before 16.4.
172
- * Without it subtitles fail outright on those browsers rather than falling back to the slower build.
173
+ * jassub's non-SIMD build, `jassub-worker.wasm`, for browsers without relaxed SIMD and for devices
174
+ * reporting 2 GB of memory or less.
173
175
  */
174
176
  jassubLegacyWasmUrl?: string;
177
+ /** `jassub-worker.js`, the script that runs `jassubLegacyWasmUrl`. Pass both or neither. */
178
+ jassubLegacyWasmWorkerUrl?: string;
175
179
  /** Fallback face for `liberation sans`, used when a subtitle track names a font the file does not carry. */
176
180
  defaultFontUrl?: string;
177
181
  /** `@banou/media-player/noto-sans-jp.otf`, the face for CJK text, fetched once the selected track shows some. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@banou/media-player",
3
- "version": "0.10.37",
3
+ "version": "0.10.39",
4
4
  "description": "A video player for containers and codecs the browser cannot play natively, remuxed on the fly",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -53,7 +53,7 @@
53
53
  "@videojs/core": "10.0.0-beta.26",
54
54
  "@videojs/react": "10.0.0-beta.26",
55
55
  "ass-compiler": "^0.1.16",
56
- "jassub": "^2.5.23",
56
+ "jassub": "^2.5.37",
57
57
  "libav-wasm": "^0.8.14",
58
58
  "lucide-react": "^0.536.0",
59
59
  "osra": "^0.6.12",
@@ -13,8 +13,23 @@ const DEFAULT_MAX_WIDTH = 1280
13
13
  * thumbnail-sized window does. A 4K source is still halved; see the note on `compositeWidth`.
14
14
  */
15
15
  const BURN_IN_MAX_WIDTH = 1920
16
- /** The frame callback fires only for presented frames, so a paused video needs its own repaint. */
17
- const PAUSED_REPAINT_INTERVAL = 250
16
+ /**
17
+ * Frame callbacks follow the BROWSER window's rendering, not the picture in picture window's: none with that window
18
+ * minimized or off screen (one a second on niri), while the picture in picture window is still shown. A timer paints
19
+ * once they have been silent this long. A paused video presents no frames either, and is repainted only every
20
+ * `PAUSED_REPAINT_MS`, for a seek or a subtitle change. `scripts/measure-picture-in-picture.mjs`, 2026-11-09, Chrome 156
21
+ * on weston, a 24 fps video, minimized or off screen: 0 different frames a second in the window before, 24 with this
22
+ * and `hidePicture`.
23
+ */
24
+ const STALLED_MS = 100
25
+ const REPAINT_TICK_MS = 1000 / 60
26
+ const PAUSED_REPAINT_MS = 250
27
+ /**
28
+ * The subtitle surface is rendered on the same frame callbacks, from a worker whose frames reach the page only while
29
+ * the page renders, so with the browser window off screen it keeps its last line. Drawn only while it is at most this
30
+ * far behind the video's time, which a page rendering once a second still is.
31
+ */
32
+ const STALE_SUBTITLES_S = 2
18
33
  /** Long enough for a paused mirror to present one frame of a fresh seek before it stops again. */
19
34
  const FLUSH_FRAME_MS = 100
20
35
 
@@ -139,7 +154,11 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
139
154
 
140
155
  let session: Session | undefined
141
156
  let handle: number | undefined
142
- let pausedRepaint: ReturnType<typeof setInterval> | undefined
157
+ let repaint: ReturnType<typeof setInterval> | undefined
158
+ let lastFrame = 0
159
+ let lastPaint = 0
160
+ /** The video's time at the last frame callback, which the subtitle surface was rendered for. */
161
+ let frameTime = 0
143
162
  let syncing = false
144
163
  /** Burn-in only: a deliberate play/pause pair to push one frame, which must not reach the video. */
145
164
  let flushing = false
@@ -162,7 +181,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
162
181
  // jassub has transferred reports the size of the last frame its worker committed, so the zero
163
182
  // check still keeps drawImage off a source with no dimensions to read.
164
183
  const surface = subtitles.querySelector('canvas')
165
- if (surface && surface.width > 0 && surface.height > 0) {
184
+ if (surface && surface.width > 0 && surface.height > 0 && Math.abs(video.currentTime - frameTime) <= STALE_SUBTITLES_S) {
166
185
  context.drawImage(surface, 0, 0, composite.width, composite.height)
167
186
  }
168
187
  }
@@ -181,12 +200,15 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
181
200
  if (video.videoWidth && (composite.width !== width || composite.height !== height)) {
182
201
  Object.assign(composite, { width, height })
183
202
  }
203
+ lastPaint = performance.now()
184
204
  draw(composite, context)
185
205
  }
186
206
 
187
207
  const useFrameCallback = 'requestVideoFrameCallback' in video
188
208
 
189
209
  const loop = () => {
210
+ lastFrame = performance.now()
211
+ frameTime = video.currentTime
190
212
  paint()
191
213
  schedule()
192
214
  }
@@ -201,9 +223,9 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
201
223
  if (useFrameCallback) video.cancelVideoFrameCallback(handle)
202
224
  else cancelAnimationFrame(handle)
203
225
  }
204
- if (pausedRepaint !== undefined) clearInterval(pausedRepaint)
226
+ if (repaint !== undefined) clearInterval(repaint)
205
227
  handle = undefined
206
- pausedRepaint = undefined
228
+ repaint = undefined
207
229
  }
208
230
 
209
231
  const publishState = () => {
@@ -286,8 +308,13 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
286
308
  }
287
309
 
288
310
  const start = () => {
311
+ frameTime = video.currentTime
289
312
  schedule()
290
- pausedRepaint = setInterval(() => { if (video.paused) paint() }, PAUSED_REPAINT_INTERVAL)
313
+ repaint = setInterval(() => {
314
+ const now = performance.now()
315
+ if (now - lastFrame < STALLED_MS || (video.paused && now - lastPaint < PAUSED_REPAINT_MS)) return
316
+ paint()
317
+ }, REPAINT_TICK_MS)
291
318
  }
292
319
 
293
320
  const listen = (mirror: HTMLVideoElement) => {
@@ -330,6 +357,21 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
330
357
 
331
358
  const onLeave = () => teardown()
332
359
 
360
+ /**
361
+ * Window only, while it is open. A video shown in the page moves its picture on the page's rendering only, so with the
362
+ * browser window minimized or off screen every read got the same frame for 250 ms: 4 different frames a second
363
+ * however fast the timer painted. A hidden one gives a fresh frame on each read. Chrome's own picture in picture
364
+ * leaves only a placeholder in the page too.
365
+ */
366
+ const hidePicture = () => {
367
+ const visibility = video.style.visibility
368
+ video.style.visibility = 'hidden'
369
+ restore = () => {
370
+ restore = undefined
371
+ video.style.visibility = visibility
372
+ }
373
+ }
374
+
333
375
  /**
334
376
  * Swap the composite in as the player's picture.
335
377
  *
@@ -409,6 +451,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
409
451
  }
410
452
 
411
453
  await mirror.requestPictureInPicture()
454
+ if (session === mine) hidePicture()
412
455
  }
413
456
 
414
457
  const toggle = async () => {
@@ -450,6 +493,7 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
450
493
  taken.context.fillRect(0, 0, taken.composite.width, taken.composite.height)
451
494
  start()
452
495
  listen(taken.mirror)
496
+ hidePicture()
453
497
  }
454
498
 
455
499
  return {
@@ -457,7 +501,11 @@ export const createPictureInPicture = (options: PictureInPictureOptions): Pictur
457
501
  mode,
458
502
  destroy: () => {
459
503
  destroyed = true
460
- if (mode === 'window' && active()) return handOver(detach()!)
504
+ if (mode === 'window' && active()) {
505
+ const held = detach()!
506
+ restore?.()
507
+ return handOver(held)
508
+ }
461
509
  teardown()
462
510
  if (mode === 'burn-in') options.onBurnedInChange?.(false)
463
511
  },
@@ -53,8 +53,12 @@ export type PlaybackOptions = {
53
53
  libavWorkerUrl: string
54
54
  jassubWorkerUrl: string
55
55
  jassubWasmUrl: string
56
- /** jassub's non-SIMD build; without it subtitles fail rather than degrade on Safari before 16.4 */
56
+ /** `jassub/dist/wasm/jassub-worker-modern.js`, the script that runs `jassubWasmUrl` */
57
+ jassubWasmWorkerUrl: string
58
+ /** jassub's non-SIMD build, for browsers without relaxed SIMD and devices with 2 GB of memory or less */
57
59
  jassubLegacyWasmUrl?: string
60
+ /** `jassub/dist/wasm/jassub-worker.js`, the script that runs `jassubLegacyWasmUrl` */
61
+ jassubLegacyWasmWorkerUrl?: string
58
62
  defaultFontUrl?: string
59
63
  cjkFontUrl?: string
60
64
  bufferSize?: number
@@ -165,7 +169,8 @@ export const terminateRemuxer = (remuxer: { worker: Worker, destroy: () => Promi
165
169
  export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackController> => {
166
170
  const {
167
171
  videoElement, subtitleContainer, read, length, publicPath, libavWorkerUrl,
168
- jassubWorkerUrl, jassubWasmUrl, jassubLegacyWasmUrl, defaultFontUrl, cjkFontUrl, bufferSize = DEFAULT_BUFFER_SIZE,
172
+ jassubWorkerUrl, jassubWasmUrl, jassubWasmWorkerUrl, jassubLegacyWasmUrl, jassubLegacyWasmWorkerUrl,
173
+ defaultFontUrl, cjkFontUrl, bufferSize = DEFAULT_BUFFER_SIZE,
169
174
  audioStreamIndex, chooseAudioStream, onReady, onError, onRecovered, onSeek, onSubtitleStreams,
170
175
  onAudioStreams, onOutput,
171
176
  } = options
@@ -212,7 +217,9 @@ export const startPlayback = async (options: PlaybackOptions): Promise<PlaybackC
212
217
  container: subtitleContainer,
213
218
  workerUrl: jassubWorkerUrl,
214
219
  wasmUrl: jassubWasmUrl,
220
+ wasmWorkerUrl: jassubWasmWorkerUrl,
215
221
  legacyWasmUrl: jassubLegacyWasmUrl,
222
+ legacyWasmWorkerUrl: jassubLegacyWasmWorkerUrl,
216
223
  defaultFontUrl,
217
224
  cjkFontUrl,
218
225
  onStreams: onSubtitleStreams,
@@ -6,7 +6,7 @@ import { parse } from 'ass-compiler'
6
6
 
7
7
  export type SubtitleStream = { streamIndex: number, title: string, language: string }
8
8
 
9
- /** -1 turns subtitles off. It frees the track and matches no header, so nothing is set. */
9
+ /** -1 turns subtitles off. It empties the track and matches no header, so nothing is set. */
10
10
  export const SUBTITLES_OFF = -1
11
11
 
12
12
  type SubtitleHeaderPart = { type: 'header', streamIndex: number, content: string, eventsContent: string, styles: Map<string, number> }
@@ -27,16 +27,26 @@ export type SubtitleRendererOptions = {
27
27
  */
28
28
  container: HTMLElement
29
29
  workerUrl: string
30
- /** The SIMD build, `jassub-worker-modern.wasm`. Used wherever WebAssembly SIMD is available. */
30
+ /**
31
+ * The SIMD build, `jassub-worker-modern.wasm`. Used wherever relaxed SIMD is available and the device
32
+ * reports more than 2 GB of memory.
33
+ */
31
34
  wasmUrl: string
32
35
  /**
33
- * The non-SIMD build, `jassub-worker.wasm`, for anything without WebAssembly SIMD.
36
+ * The script that runs `wasmUrl`, `jassub-worker-modern.js` from the same jassub version, since each wasm
37
+ * build has its own. The worker imports it and starts its threads from it, so it is a same-origin url.
38
+ */
39
+ wasmWorkerUrl: string
40
+ /**
41
+ * The non-SIMD build, `jassub-worker.wasm`, for anything without relaxed SIMD or with 2 GB of memory or less.
34
42
  *
35
43
  * Optional in practice as well as in the type, unlike under jassub 1: v2 defaults both wasm urls to
36
44
  * its own files resolved against `import.meta.url`, so leaving this unset degrades to whatever the
37
45
  * bundler emitted rather than failing outright.
38
46
  */
39
47
  legacyWasmUrl?: string
48
+ /** The script that runs `legacyWasmUrl`, `jassub-worker.js`. Pass both or neither. */
49
+ legacyWasmWorkerUrl?: string
40
50
  /**
41
51
  * Fallback face for `liberation sans`. Without it jassub falls back to whatever the wasm build embeds.
42
52
  *
@@ -169,7 +179,9 @@ export type SubtitleRenderer = ReturnType<typeof createSubtitleRenderer>
169
179
  export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
170
180
  const { video, container, workerUrl } = options
171
181
  const wasmUrl = workerFetched(options.wasmUrl)!
182
+ const wasmWorkerUrl = workerFetched(options.wasmWorkerUrl)!
172
183
  const legacyWasmUrl = workerFetched(options.legacyWasmUrl)
184
+ const legacyWasmWorkerUrl = workerFetched(options.legacyWasmWorkerUrl)
173
185
  const defaultFontUrl = workerFetched(options.defaultFontUrl)
174
186
  const cjkFontUrl = workerFetched(options.cjkFontUrl)
175
187
  let cjkFontRequested = false
@@ -195,6 +207,8 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
195
207
  const rendererFailed = (error: unknown) => {
196
208
  console.warn('the subtitle renderer failed; the file plays without subtitles', error)
197
209
  }
210
+ // a renderer that never started is reported once, by `bootJassub`, not again by everything that waited on it
211
+ const startFailed = () => undefined
198
212
 
199
213
  /**
200
214
  * Run something against the worker, once there is a worker to run it against.
@@ -218,7 +232,7 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
218
232
  const instance = jassub
219
233
  if (!instance) return
220
234
  void instance.ready
221
- .then(() => (jassub === instance ? run(instance.renderer) : undefined))
235
+ .then(() => (jassub === instance ? run(instance.renderer) : undefined), startFailed)
222
236
  .catch(rendererFailed)
223
237
  }
224
238
 
@@ -257,9 +271,11 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
257
271
  expectedDisplayTime: performance.now(),
258
272
  width: video.videoWidth,
259
273
  height: video.videoHeight,
260
- mediaTime: video.currentTime,
274
+ // Never exactly 0: from 2.5.30 jassub redraws for a colour space it learns late only `if (this._lastDrawTime)`,
275
+ // so a picture paused at the start kept its unconverted colours. libass rounds to the millisecond.
276
+ mediaTime: video.currentTime || Number.MIN_VALUE,
261
277
  }, true)
262
- })
278
+ }, startFailed)
263
279
  .catch(rendererFailed)
264
280
  })
265
281
  }
@@ -277,8 +293,8 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
277
293
  * `subtitle-paused-repaint.browser.test.tsx`'s turn-off case four runs out of four, while adding
278
294
  * back EITHER of the other two passes. Neither of those events fires in that test, so what the
279
295
  * extra listener buys is timing rather than a trigger, and the honest conclusion is that the clear
280
- * after a `freeTrack` is more delicate than it looks. Do not shorten this list without running that
281
- * case several times.
296
+ * when subtitles are turned off is more delicate than it looks. Do not shorten this list without
297
+ * running that case several times.
282
298
  */
283
299
  const REPAINT_ON = ['loadedmetadata', 'seeked', 'pause'] as const
284
300
  const onRepaintEvent = () => repaint()
@@ -288,12 +304,13 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
288
304
  container.append(canvas)
289
305
  try {
290
306
  jassub = new JASSUB({
291
- video,
292
307
  canvas,
293
308
  subContent: header.content,
294
309
  workerUrl,
295
310
  modernWasmUrl: wasmUrl,
311
+ modernWasmWorkerUrl: wasmWorkerUrl,
296
312
  ...legacyWasmUrl ? { wasmUrl: legacyWasmUrl } : {},
313
+ ...legacyWasmWorkerUrl ? { wasmWorkerUrl: legacyWasmWorkerUrl } : {},
297
314
  fonts: attachments.map(([, data]) => data),
298
315
  availableFonts: {
299
316
  ...Object.fromEntries(attachments),
@@ -309,9 +326,9 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
309
326
  rendererFailed(error)
310
327
  return
311
328
  }
312
- // A boot that fails worker-side rejects `ready`, and jassub's own ResizeObserver awaits `ready`
313
- // on every layout change, so an instance left in that state throws a fresh unhandled rejection
314
- // for the rest of the session.
329
+ // Not handed to the constructor, which attaches it with a `setVideo` nothing catches: that rejects with `ready`, so a
330
+ // renderer that cannot start would leave an unhandled rejection in the host page.
331
+ void jassub.setVideo(video).catch(startFailed)
315
332
  void jassub.ready.catch(rendererFailed)
316
333
  for (const type of REPAINT_ON) video.addEventListener(type, onRepaintEvent)
317
334
  /*
@@ -390,12 +407,10 @@ export const createSubtitleRenderer = (options: SubtitleRendererOptions) => {
390
407
  const header = headers.get(next)
391
408
  const events = [...dialogues.get(next)?.values() ?? []].map((part) => part.assEvent)
392
409
  onRenderer((renderer) => {
393
- const calls: unknown[] = [renderer.freeTrack()]
394
- if (header) {
395
- calls.push(renderer.setTrack(header.content))
396
- for (const event of events) calls.push(renderer.createEvent(event))
397
- }
398
- return Promise.all(calls)
410
+ // Off empties the track rather than freeing it. jassub redoes a draw asked for while one is in flight without
411
+ // its repaint flag, and from 2.5.30 an unforced draw of a freed track reports no change, so the line stayed up.
412
+ if (!header) return renderer.removeAllEvents()
413
+ return Promise.all([renderer.freeTrack(), renderer.setTrack(header.content), ...events.map((event) => renderer.createEvent(event))])
399
414
  })
400
415
  loadCjkFontFor(events)
401
416
  // outside the callback so that turning subtitles OFF while paused clears the picture too
@@ -79,11 +79,11 @@ export const usePlayback = (
79
79
  onOutput?: (output: PlaybackOutput) => void,
80
80
  ) => {
81
81
  const player = usePlayer()
82
- // The four asset urls are required on the local arm, so a default only ever applies when options is
82
+ // The five asset urls are required on the local arm, so a default only ever applies when options is
83
83
  // null, and the effect below returns before touching them in that case.
84
84
  const {
85
- read, size, publicPath = '', libavWorkerUrl = '', jassubWorkerUrl = '', jassubWasmUrl = '',
86
- jassubLegacyWasmUrl, defaultFontUrl, cjkFontUrl, bufferSize, autoplay = false, chapters,
85
+ read, size, publicPath = '', libavWorkerUrl = '', jassubWorkerUrl = '', jassubWasmUrl = '', jassubWasmWorkerUrl = '',
86
+ jassubLegacyWasmUrl, jassubLegacyWasmWorkerUrl, defaultFontUrl, cjkFontUrl, bufferSize, autoplay = false, chapters,
87
87
  seekPrepareBudgetMs = SEEK_PREPARE_BUDGET_MS,
88
88
  } = options ?? ({} as Partial<MediaPlayerLocalOptions>)
89
89
 
@@ -378,7 +378,9 @@ export const usePlayback = (
378
378
  libavWorkerUrl,
379
379
  jassubWorkerUrl,
380
380
  jassubWasmUrl,
381
+ jassubWasmWorkerUrl,
381
382
  jassubLegacyWasmUrl,
383
+ jassubLegacyWasmWorkerUrl,
382
384
  defaultFontUrl,
383
385
  cjkFontUrl,
384
386
  bufferSize,
@@ -447,6 +449,7 @@ export const usePlayback = (
447
449
  // times a second, and the restart loop reads as "Loading metadata" forever at a flat 0 B/s.
448
450
  }, [
449
451
  player, playingAudio, video, subtitles, size, publicPath, libavWorkerUrl, jassubWorkerUrl, jassubWasmUrl,
450
- jassubLegacyWasmUrl, defaultFontUrl, cjkFontUrl, bufferSize, audioStreamIndex, autoplay, restartToken,
452
+ jassubWasmWorkerUrl, jassubLegacyWasmUrl, jassubLegacyWasmWorkerUrl, defaultFontUrl, cjkFontUrl, bufferSize,
453
+ audioStreamIndex, autoplay, restartToken,
451
454
  ])
452
455
  }
@@ -193,11 +193,15 @@ export type MediaPlayerLocalOptions =
193
193
  jassubWorkerUrl: string
194
194
  /** jassub's SIMD build, `jassub-worker-modern.wasm`. */
195
195
  jassubWasmUrl: string
196
+ /** `jassub-worker-modern.js`, the script that runs `jassubWasmUrl`, from the same jassub version. */
197
+ jassubWasmWorkerUrl: string
196
198
  /**
197
- * jassub's non-SIMD build, `jassub-worker.wasm`, for Safari before 16.4.
198
- * Without it subtitles fail outright on those browsers rather than falling back to the slower build.
199
+ * jassub's non-SIMD build, `jassub-worker.wasm`, for browsers without relaxed SIMD and for devices
200
+ * reporting 2 GB of memory or less.
199
201
  */
200
202
  jassubLegacyWasmUrl?: string
203
+ /** `jassub-worker.js`, the script that runs `jassubLegacyWasmUrl`. Pass both or neither. */
204
+ jassubLegacyWasmWorkerUrl?: string
201
205
  /** Fallback face for `liberation sans`, used when a subtitle track names a font the file does not carry. */
202
206
  defaultFontUrl?: string
203
207
  /** `@banou/media-player/noto-sans-jp.otf`, the face for CJK text, fetched once the selected track shows some. */