@hanifhan1f/vidstack-react 1.12.24 → 1.12.25

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.
Files changed (58) hide show
  1. package/dev/chunks/vidstack-BNwnoM-l.js +84 -0
  2. package/dev/chunks/vidstack-BOPSNTgH.js +668 -0
  3. package/dev/chunks/vidstack-BSZY6sbC.js +180 -0
  4. package/dev/chunks/vidstack-BVVxkUlq.js +288 -0
  5. package/dev/chunks/vidstack-BXgKy_7V.js +11693 -0
  6. package/dev/chunks/vidstack-BrqDQG-r.js +643 -0
  7. package/dev/chunks/vidstack-C3QPOZNd.js +476 -0
  8. package/dev/chunks/vidstack-C7BwfK5c.js +1537 -0
  9. package/dev/chunks/vidstack-CEUjDh4x.js +34 -0
  10. package/dev/chunks/vidstack-CQSHFVu7.js +401 -0
  11. package/dev/chunks/vidstack-Csfg08VS.js +551 -0
  12. package/dev/chunks/vidstack-DTow20pt.js +189 -0
  13. package/dev/chunks/vidstack-L5mw2iPb.js +375 -0
  14. package/dev/chunks/vidstack-iRuTLfhk.js +61 -0
  15. package/dev/player/vidstack-default-components.js +5 -5
  16. package/dev/player/vidstack-default-layout.js +5 -5
  17. package/dev/player/vidstack-plyr-layout.js +4 -4
  18. package/dev/player/vidstack-remotion.js +3 -3
  19. package/dev/vidstack.js +7 -7
  20. package/package.json +1 -1
  21. package/prod/chunks/vidstack-58ZavMvv.js +159 -0
  22. package/prod/chunks/vidstack-BS445j5D.js +84 -0
  23. package/prod/chunks/vidstack-B_9VGrZQ.js +504 -0
  24. package/prod/chunks/vidstack-Bm2UemPE.js +470 -0
  25. package/prod/chunks/vidstack-Bp_hAwzI.js +61 -0
  26. package/prod/chunks/vidstack-CQll06Hv.js +34 -0
  27. package/prod/chunks/vidstack-D3cSYtez.js +663 -0
  28. package/prod/chunks/vidstack-D_ijTIbV.js +11198 -0
  29. package/prod/chunks/vidstack-DgGDsAKh.js +375 -0
  30. package/prod/chunks/vidstack-DrEorv9m.js +189 -0
  31. package/prod/chunks/vidstack-ehqxnvc9.js +1537 -0
  32. package/prod/chunks/vidstack-lc8NHly9.js +288 -0
  33. package/prod/chunks/vidstack-rKV98aQH.js +545 -0
  34. package/prod/chunks/vidstack-uA7h-Bsq.js +386 -0
  35. package/prod/player/vidstack-default-components.js +5 -5
  36. package/prod/player/vidstack-default-layout.js +5 -5
  37. package/prod/player/vidstack-plyr-layout.js +4 -4
  38. package/prod/player/vidstack-remotion.js +3 -3
  39. package/prod/vidstack.js +7 -7
  40. package/server/chunks/vidstack-B3AXUfgF.js +189 -0
  41. package/server/chunks/vidstack-BJCx78pm.js +386 -0
  42. package/server/chunks/vidstack-BtitkRvR.js +11198 -0
  43. package/server/chunks/vidstack-BweZhuNd.js +1537 -0
  44. package/server/chunks/vidstack-CSiPajWY.js +470 -0
  45. package/server/chunks/vidstack-CgXa6YO3.js +61 -0
  46. package/server/chunks/vidstack-DH6N0AoF.js +375 -0
  47. package/server/chunks/vidstack-DKr7br9D.js +34 -0
  48. package/server/chunks/vidstack-Dbs_rXUT.js +663 -0
  49. package/server/chunks/vidstack-E7eUOyFt.js +288 -0
  50. package/server/chunks/vidstack-JkJEYEQM.js +159 -0
  51. package/server/chunks/vidstack-KhtuR229.js +504 -0
  52. package/server/chunks/vidstack-X1Hex9PH.js +84 -0
  53. package/server/chunks/vidstack-yf18YVAb.js +545 -0
  54. package/server/player/vidstack-default-components.js +5 -5
  55. package/server/player/vidstack-default-layout.js +5 -5
  56. package/server/player/vidstack-plyr-layout.js +4 -4
  57. package/server/player/vidstack-remotion.js +3 -3
  58. package/server/vidstack.js +7 -7
@@ -0,0 +1,504 @@
1
+ "use client"
2
+
3
+ import * as React from 'react';
4
+ import { createDisposalBin, listenEvent, createScope, signal, peek, effect, tick, isFunction, useSignal, deferredPromise } from './vidstack-CZgUA94N.js';
5
+ import { Internals } from 'remotion';
6
+ import { REMOTION_PROVIDER_ID, RemotionLayoutEngine, RemotionContextProvider, ErrorBoundary } from '../player/vidstack-remotion.js';
7
+ import { isRemotionSrc, TimeRange } from './vidstack-D_ijTIbV.js';
8
+ import { NoReactInternals } from 'remotion/no-react';
9
+ import './vidstack-vqp2QaHR.js';
10
+ import '@floating-ui/dom';
11
+
12
+ class RemotionPlaybackEngine {
13
+ #src;
14
+ #onFrameChange;
15
+ #onEnd;
16
+ #disposal = createDisposalBin();
17
+ #frame = 0;
18
+ #framesAdvanced = 0;
19
+ #playbackRate = 1;
20
+ #playing = false;
21
+ #rafId = -1;
22
+ #timerId = -1;
23
+ #startedAt = 0;
24
+ #isRunningInBackground = false;
25
+ get frame() {
26
+ return this.#frame;
27
+ }
28
+ set frame(frame) {
29
+ this.#frame = frame;
30
+ this.#onFrameChange(frame);
31
+ }
32
+ constructor(src, onFrameChange, onEnd) {
33
+ this.#src = src;
34
+ this.#onFrameChange = onFrameChange;
35
+ this.#onEnd = onEnd;
36
+ this.#frame = src.initialFrame ?? 0;
37
+ this.#disposal.add(
38
+ listenEvent(document, "visibilitychange", this.#onVisibilityChange.bind(this))
39
+ );
40
+ }
41
+ play() {
42
+ this.#framesAdvanced = 0;
43
+ this.#playing = true;
44
+ this.#startedAt = performance.now();
45
+ this.#tick();
46
+ }
47
+ stop() {
48
+ this.#playing = false;
49
+ if (this.#rafId >= 0) {
50
+ cancelAnimationFrame(this.#rafId);
51
+ this.#rafId = -1;
52
+ }
53
+ if (this.#timerId >= 0) {
54
+ clearTimeout(this.#timerId);
55
+ this.#timerId = -1;
56
+ }
57
+ }
58
+ setPlaybackRate(rate) {
59
+ this.#playbackRate = rate;
60
+ }
61
+ destroy() {
62
+ this.#disposal.empty();
63
+ this.stop();
64
+ }
65
+ #update() {
66
+ const { nextFrame, framesToAdvance, ended } = this.#calculateNextFrame();
67
+ this.#framesAdvanced += framesToAdvance;
68
+ if (nextFrame !== this.#frame) {
69
+ this.#onFrameChange(nextFrame);
70
+ this.#frame = nextFrame;
71
+ }
72
+ if (ended) {
73
+ this.#frame = this.#src.outFrame;
74
+ this.stop();
75
+ this.#onEnd();
76
+ }
77
+ }
78
+ #tick = () => {
79
+ this.#update();
80
+ if (this.#playing) {
81
+ this.#queueNextFrame(this.#tick);
82
+ }
83
+ };
84
+ #queueNextFrame(callback) {
85
+ if (this.#isRunningInBackground) {
86
+ this.#timerId = window.setTimeout(callback, 1e3 / this.#src.fps);
87
+ } else {
88
+ this.#rafId = requestAnimationFrame(callback);
89
+ }
90
+ }
91
+ #calculateNextFrame() {
92
+ const round = this.#playbackRate < 0 ? Math.ceil : Math.floor, time = performance.now() - this.#startedAt, framesToAdvance = round(time * this.#playbackRate / (1e3 / this.#src.fps)) - this.#framesAdvanced, nextFrame = framesToAdvance + this.#frame, isCurrentFrameOutOfBounds = this.#frame > this.#src.outFrame || this.#frame < this.#src.inFrame, isNextFrameOutOfBounds = nextFrame > this.#src.outFrame || nextFrame < this.#src.inFrame, ended = isNextFrameOutOfBounds && !isCurrentFrameOutOfBounds;
93
+ if (this.#playbackRate > 0 && !ended) {
94
+ if (isNextFrameOutOfBounds) {
95
+ return {
96
+ nextFrame: this.#src.inFrame,
97
+ framesToAdvance,
98
+ ended
99
+ };
100
+ }
101
+ return { nextFrame, framesToAdvance, ended };
102
+ }
103
+ if (isNextFrameOutOfBounds) {
104
+ return {
105
+ nextFrame: this.#src.outFrame,
106
+ framesToAdvance,
107
+ ended
108
+ };
109
+ }
110
+ return { nextFrame, framesToAdvance, ended };
111
+ }
112
+ #onVisibilityChange() {
113
+ this.#isRunningInBackground = document.visibilityState === "hidden";
114
+ if (this.#playing) {
115
+ this.stop();
116
+ this.play();
117
+ }
118
+ }
119
+ }
120
+
121
+ function validateRemotionResource({
122
+ src,
123
+ compositionWidth: width,
124
+ compositionHeight: height,
125
+ fps,
126
+ durationInFrames,
127
+ initialFrame,
128
+ inFrame,
129
+ outFrame,
130
+ numberOfSharedAudioTags
131
+ }) {
132
+ return;
133
+ }
134
+ NoReactInternals.validateFps;
135
+ NoReactInternals.validateDimension;
136
+ NoReactInternals.validateDurationInFrames;
137
+
138
+ class RemotionProvider {
139
+ $$PROVIDER_TYPE = "REMOTION";
140
+ scope = createScope();
141
+ #src = signal(null);
142
+ #setup = false;
143
+ #loadStart = false;
144
+ #audio = null;
145
+ #waiting = signal(false);
146
+ #waitingPromise = null;
147
+ #mediaTags = signal([]);
148
+ #mediaElements = signal([]);
149
+ #bufferingElements = /* @__PURE__ */ new Set();
150
+ #timeline = null;
151
+ #frame = signal({ [REMOTION_PROVIDER_ID]: 0 });
152
+ #layoutEngine = new RemotionLayoutEngine();
153
+ #playbackEngine = null;
154
+ #container;
155
+ #ctx;
156
+ #setTimeline;
157
+ #setMediaVolume = {
158
+ setMediaMuted: this.setMuted.bind(this),
159
+ setMediaVolume: this.setVolume.bind(this)
160
+ };
161
+ get type() {
162
+ return "remotion";
163
+ }
164
+ get currentSrc() {
165
+ return peek(this.#src);
166
+ }
167
+ get frame() {
168
+ return this.#frame();
169
+ }
170
+ constructor(container, ctx) {
171
+ this.#container = container;
172
+ this.#ctx = ctx;
173
+ this.#setTimeline = {
174
+ setFrame: this.#setFrame.bind(this),
175
+ setPlaying: this.#setPlaying.bind(this)
176
+ };
177
+ this.#layoutEngine.setContainer(container);
178
+ }
179
+ setup() {
180
+ effect(this.#watchWaiting.bind(this));
181
+ effect(this.#watchMediaTags.bind(this));
182
+ effect(this.#watchMediaElements.bind(this));
183
+ }
184
+ #watchMediaTags() {
185
+ this.#mediaTags();
186
+ this.#discoverMediaElements();
187
+ }
188
+ #discoverMediaElements() {
189
+ const elements = [...this.#container.querySelectorAll("audio,video")];
190
+ this.#mediaElements.set(elements);
191
+ }
192
+ #watchMediaElements() {
193
+ const elements = this.#mediaElements();
194
+ for (const tag of elements) {
195
+ const onWait = this.#onWaitFor.bind(this, tag), onStopWaiting = this.#onStopWaitingFor.bind(this, tag);
196
+ if (tag.currentSrc && tag.readyState < 4) {
197
+ this.#onWaitFor(tag);
198
+ listenEvent(tag, "canplay", onStopWaiting);
199
+ }
200
+ listenEvent(tag, "waiting", onWait);
201
+ listenEvent(tag, "playing", onStopWaiting);
202
+ }
203
+ for (const el of this.#bufferingElements) {
204
+ if (!elements.includes(el)) this.#onStopWaitingFor(el);
205
+ }
206
+ }
207
+ #onFrameChange(frame) {
208
+ const { inFrame, fps } = this.#src(), { seeking } = this.#ctx.$state, time = Math.max(0, frame - inFrame) / fps;
209
+ this.#frame.set((record) => ({
210
+ ...record,
211
+ [REMOTION_PROVIDER_ID]: frame
212
+ }));
213
+ this.#ctx.notify("time-change", time);
214
+ if (seeking()) {
215
+ tick();
216
+ this.#ctx.notify("seeked", time);
217
+ }
218
+ }
219
+ #onFrameEnd() {
220
+ this.pause();
221
+ this.#ctx.notify("end");
222
+ }
223
+ async play() {
224
+ const { ended } = this.#ctx.$state;
225
+ if (peek(ended)) {
226
+ this.#setFrame({ [REMOTION_PROVIDER_ID]: 0 });
227
+ }
228
+ try {
229
+ const mediaElements = peek(this.#mediaElements);
230
+ if (mediaElements.length) {
231
+ await Promise.all(mediaElements.map((media) => media.play()));
232
+ }
233
+ this.#ctx.notify("play");
234
+ tick();
235
+ if (this.#waitingPromise) {
236
+ this.#ctx.notify("waiting");
237
+ return this.#waitingPromise.promise;
238
+ } else {
239
+ this.#playbackEngine?.play();
240
+ this.#ctx.notify("playing");
241
+ }
242
+ } catch (error) {
243
+ throw error;
244
+ }
245
+ }
246
+ async pause() {
247
+ const { paused } = this.#ctx.$state;
248
+ this.#playbackEngine?.stop();
249
+ this.#ctx.notify("pause");
250
+ }
251
+ setMuted(value) {
252
+ if (!this.#ctx) return;
253
+ const { muted, volume } = this.#ctx.$state;
254
+ if (isFunction(value)) {
255
+ this.setMuted(value(muted()));
256
+ return;
257
+ }
258
+ this.#ctx.notify("volume-change", {
259
+ volume: peek(volume),
260
+ muted: value
261
+ });
262
+ }
263
+ setCurrentTime(time) {
264
+ const { fps } = this.#src(), frame = time * fps;
265
+ this.#ctx.notify("seeking", time);
266
+ this.#setFrame({ [REMOTION_PROVIDER_ID]: frame });
267
+ }
268
+ setVolume(value) {
269
+ if (!this.#ctx) return;
270
+ const { volume, muted } = this.#ctx.$state;
271
+ if (isFunction(value)) {
272
+ this.setVolume(value(volume()));
273
+ return;
274
+ }
275
+ this.#ctx.notify("volume-change", {
276
+ volume: value,
277
+ muted: peek(muted)
278
+ });
279
+ }
280
+ setPlaybackRate(rate) {
281
+ if (isFunction(rate)) {
282
+ const { playbackRate } = this.#ctx.$state;
283
+ this.setPlaybackRate(rate(peek(playbackRate)));
284
+ return;
285
+ }
286
+ this.#playbackEngine?.setPlaybackRate(rate);
287
+ this.#ctx.notify("rate-change", rate);
288
+ }
289
+ async loadSource(src) {
290
+ if (!isRemotionSrc(src)) return;
291
+ const onUserError = src.onError, resolvedSrc = {
292
+ compositionWidth: 1920,
293
+ compositionHeight: 1080,
294
+ fps: 30,
295
+ initialFrame: 0,
296
+ inFrame: 0,
297
+ outFrame: src.durationInFrames,
298
+ numberOfSharedAudioTags: 5,
299
+ inputProps: {},
300
+ ...src,
301
+ onError: (error) => {
302
+ this.pause();
303
+ this.#ctx.notify("error", {
304
+ message: error.message,
305
+ code: 1
306
+ });
307
+ onUserError?.(error);
308
+ }
309
+ };
310
+ this.#src.set(resolvedSrc);
311
+ for (const prop of Object.keys(resolvedSrc)) {
312
+ src[prop] = resolvedSrc[prop];
313
+ }
314
+ this.changeSrc(resolvedSrc);
315
+ }
316
+ destroy() {
317
+ this.changeSrc(null);
318
+ }
319
+ changeSrc(src) {
320
+ this.#playbackEngine?.destroy();
321
+ this.#waiting.set(false);
322
+ this.#waitingPromise?.reject("src changed");
323
+ this.#waitingPromise = null;
324
+ this.#audio = null;
325
+ this.#timeline = null;
326
+ this.#playbackEngine = null;
327
+ this.#mediaTags.set([]);
328
+ this.#bufferingElements.clear();
329
+ this.#frame.set({ [REMOTION_PROVIDER_ID]: 0 });
330
+ this.#layoutEngine.setSrc(src);
331
+ if (src) {
332
+ this.#timeline = this.#createTimelineContextValue();
333
+ this.#playbackEngine = new RemotionPlaybackEngine(
334
+ src,
335
+ this.#onFrameChange.bind(this),
336
+ this.#onFrameEnd.bind(this)
337
+ );
338
+ }
339
+ }
340
+ render = () => {
341
+ const $src = useSignal(this.#src);
342
+ if (!$src) {
343
+ throw Error(
344
+ "[vidstack] no src"
345
+ );
346
+ }
347
+ React.useEffect(() => {
348
+ if (!isRemotionSrc($src)) return;
349
+ validateRemotionResource($src);
350
+ const rafId = requestAnimationFrame(() => {
351
+ if (!this.#setup) {
352
+ this.#ctx.notify("provider-setup", this);
353
+ this.#setup = true;
354
+ }
355
+ if (!this.#loadStart) {
356
+ this.#ctx.notify("load-start");
357
+ this.#loadStart = true;
358
+ }
359
+ this.#discoverMediaElements();
360
+ tick();
361
+ if (!this.#waiting()) this.#ready($src);
362
+ });
363
+ return () => {
364
+ cancelAnimationFrame(rafId);
365
+ this.#loadStart = false;
366
+ };
367
+ }, [$src]);
368
+ const Component = Internals.useLazyComponent({
369
+ component: $src.src
370
+ });
371
+ const { $state } = this.#ctx, $volume = useSignal($state.volume), $isMuted = useSignal($state.muted);
372
+ const mediaVolume = React.useMemo(() => {
373
+ const { muted, volume } = this.#ctx.$state;
374
+ return { mediaMuted: muted(), mediaVolume: volume() };
375
+ }, [$isMuted, $volume]);
376
+ return /* @__PURE__ */ React.createElement(
377
+ RemotionContextProvider,
378
+ {
379
+ src: $src,
380
+ component: Component,
381
+ timeline: this.#timeline,
382
+ mediaVolume,
383
+ setMediaVolume: this.#setMediaVolume
384
+ },
385
+ /* @__PURE__ */ React.createElement(Internals.Timeline.SetTimelineContext.Provider, { value: this.#setTimeline }, React.createElement(this.renderVideo, { src: $src }))
386
+ );
387
+ };
388
+ renderVideo = ({ src }) => {
389
+ const video = Internals.useVideo(), Video = video ? video.component : null, audioContext = React.useContext(Internals.SharedAudioContext);
390
+ const { $state } = this.#ctx;
391
+ useSignal(this.#frame);
392
+ useSignal($state.playing);
393
+ useSignal($state.playbackRate);
394
+ React.useEffect(() => {
395
+ this.#audio = audioContext;
396
+ return () => {
397
+ this.#audio = null;
398
+ };
399
+ }, [audioContext]);
400
+ const LoadingContent = React.useMemo(() => src.renderLoading?.(), [src]);
401
+ const Content = Video ? /* @__PURE__ */ React.createElement(ErrorBoundary, { fallback: src.errorFallback, onError: src.onError }, /* @__PURE__ */ React.createElement(Internals.ClipComposition, null, /* @__PURE__ */ React.createElement(Video, { ...video?.props, ...src.inputProps }))) : null;
402
+ return /* @__PURE__ */ React.createElement(React.Suspense, { fallback: LoadingContent }, Content);
403
+ };
404
+ #ready(src) {
405
+ if (!src) return;
406
+ const { outFrame, inFrame, fps } = src, duration = (outFrame - inFrame) / fps;
407
+ this.#ctx.notify("loaded-metadata");
408
+ this.#ctx.notify("loaded-data");
409
+ this.#ctx.delegate.ready({
410
+ duration,
411
+ seekable: new TimeRange(0, duration),
412
+ buffered: new TimeRange(0, duration)
413
+ });
414
+ if (src.initialFrame) {
415
+ this.#setFrame({
416
+ [REMOTION_PROVIDER_ID]: src.initialFrame
417
+ });
418
+ }
419
+ }
420
+ #onWaitFor(el) {
421
+ this.#bufferingElements.add(el);
422
+ this.#waiting.set(true);
423
+ if (!this.#waitingPromise) {
424
+ this.#waitingPromise = deferredPromise();
425
+ }
426
+ }
427
+ #onStopWaitingFor(el) {
428
+ this.#bufferingElements.delete(el);
429
+ if (this.#bufferingElements.size) return;
430
+ this.#waiting.set(false);
431
+ this.#waitingPromise?.resolve();
432
+ this.#waitingPromise = null;
433
+ const { canPlay } = this.#ctx.$state;
434
+ if (!peek(canPlay)) {
435
+ this.#ready(peek(this.#src));
436
+ }
437
+ }
438
+ #watchWaiting() {
439
+ this.#waiting();
440
+ const { paused } = this.#ctx.$state;
441
+ if (peek(paused)) return;
442
+ if (this.#waiting()) {
443
+ this.#playbackEngine?.stop();
444
+ this.#ctx.notify("waiting");
445
+ } else {
446
+ this.#playbackEngine?.play();
447
+ this.#ctx.notify("playing");
448
+ }
449
+ }
450
+ #setFrame(value) {
451
+ if (isFunction(value)) {
452
+ this.#setFrame(value(this.#frame()));
453
+ return;
454
+ }
455
+ this.#frame.set((record) => ({ ...record, ...value }));
456
+ const nextFrame = value[REMOTION_PROVIDER_ID];
457
+ if (this.#playbackEngine && this.#playbackEngine.frame !== nextFrame) {
458
+ this.#playbackEngine.frame = nextFrame;
459
+ }
460
+ }
461
+ #setPlaying(value) {
462
+ const { playing } = this.#ctx.$state;
463
+ if (isFunction(value)) {
464
+ this.#setPlaying(value(playing()));
465
+ return;
466
+ }
467
+ if (value) {
468
+ this.play();
469
+ } else if (!value) {
470
+ this.pause();
471
+ }
472
+ }
473
+ #createTimelineContextValue() {
474
+ const { playing, playbackRate } = this.#ctx.$state, frame = this.#frame, mediaTags = this.#mediaTags, setPlaybackRate = this.setPlaybackRate.bind(this);
475
+ return {
476
+ rootId: REMOTION_PROVIDER_ID,
477
+ get frame() {
478
+ return frame();
479
+ },
480
+ get playing() {
481
+ return playing();
482
+ },
483
+ get playbackRate() {
484
+ return playbackRate();
485
+ },
486
+ imperativePlaying: {
487
+ get current() {
488
+ return playing();
489
+ }
490
+ },
491
+ setPlaybackRate,
492
+ audioAndVideoTags: {
493
+ get current() {
494
+ return mediaTags();
495
+ },
496
+ set current(tags) {
497
+ mediaTags.set(tags);
498
+ }
499
+ }
500
+ };
501
+ }
502
+ }
503
+
504
+ export { RemotionProvider };