@remotion/player 4.0.295 → 4.0.297

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.
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usePlayback = void 0;
4
+ /* eslint-disable @typescript-eslint/no-use-before-define */
5
+ const react_1 = require("react");
6
+ const react_dom_1 = require("react-dom");
7
+ const remotion_1 = require("remotion");
8
+ const browser_mediasession_js_1 = require("./browser-mediasession.js");
9
+ const calculate_next_frame_js_1 = require("./calculate-next-frame.js");
10
+ const is_backgrounded_js_1 = require("./is-backgrounded.js");
11
+ const use_player_js_1 = require("./use-player.js");
12
+ const usePlayback = ({ loop, playbackRate, moveToBeginningWhenEnded, inFrame, outFrame, browserMediaControlsBehavior, getCurrentFrame, }) => {
13
+ const config = remotion_1.Internals.useUnsafeVideoConfig();
14
+ const frame = remotion_1.Internals.Timeline.useTimelinePosition();
15
+ const { playing, pause, emitter } = (0, use_player_js_1.usePlayer)();
16
+ const setFrame = remotion_1.Internals.Timeline.useTimelineSetFrame();
17
+ const buffering = (0, react_1.useRef)(null);
18
+ // requestAnimationFrame() does not work if the tab is not active.
19
+ // This means that audio will keep playing even if it has ended.
20
+ // In that case, we use setTimeout() instead.
21
+ const isBackgroundedRef = (0, is_backgrounded_js_1.useIsBackgrounded)();
22
+ const lastTimeUpdateEvent = (0, react_1.useRef)(null);
23
+ const context = (0, react_1.useContext)(remotion_1.Internals.BufferingContextReact);
24
+ if (!context) {
25
+ throw new Error('Missing the buffering context. Most likely you have a Remotion version mismatch.');
26
+ }
27
+ (0, browser_mediasession_js_1.useBrowserMediaSession)({
28
+ browserMediaControlsBehavior,
29
+ playbackRate,
30
+ videoConfig: config,
31
+ });
32
+ // complete code for media session API
33
+ (0, react_1.useEffect)(() => {
34
+ const onBufferClear = context.listenForBuffering(() => {
35
+ buffering.current = performance.now();
36
+ });
37
+ const onResumeClear = context.listenForResume(() => {
38
+ buffering.current = null;
39
+ });
40
+ return () => {
41
+ onBufferClear.remove();
42
+ onResumeClear.remove();
43
+ };
44
+ }, [context]);
45
+ (0, react_1.useEffect)(() => {
46
+ if (!config) {
47
+ return;
48
+ }
49
+ if (!playing) {
50
+ return;
51
+ }
52
+ let hasBeenStopped = false;
53
+ let reqAnimFrameCall = null;
54
+ let startedTime = performance.now();
55
+ let framesAdvanced = 0;
56
+ const cancelQueuedFrame = () => {
57
+ if (reqAnimFrameCall !== null) {
58
+ if (reqAnimFrameCall.type === 'raf') {
59
+ cancelAnimationFrame(reqAnimFrameCall.id);
60
+ }
61
+ else {
62
+ clearTimeout(reqAnimFrameCall.id);
63
+ }
64
+ }
65
+ };
66
+ const stop = () => {
67
+ hasBeenStopped = true;
68
+ cancelQueuedFrame();
69
+ };
70
+ const callback = () => {
71
+ const time = performance.now() - startedTime;
72
+ const actualLastFrame = outFrame !== null && outFrame !== void 0 ? outFrame : config.durationInFrames - 1;
73
+ const actualFirstFrame = inFrame !== null && inFrame !== void 0 ? inFrame : 0;
74
+ const currentFrame = getCurrentFrame();
75
+ const { nextFrame, framesToAdvance, hasEnded } = (0, calculate_next_frame_js_1.calculateNextFrame)({
76
+ time,
77
+ currentFrame,
78
+ playbackSpeed: playbackRate,
79
+ fps: config.fps,
80
+ actualFirstFrame,
81
+ actualLastFrame,
82
+ framesAdvanced,
83
+ shouldLoop: loop,
84
+ });
85
+ framesAdvanced += framesToAdvance;
86
+ if (nextFrame !== getCurrentFrame() &&
87
+ (!hasEnded || moveToBeginningWhenEnded)) {
88
+ (0, react_dom_1.flushSync)(() => {
89
+ setFrame((c) => ({ ...c, [config.id]: nextFrame }));
90
+ });
91
+ }
92
+ if (hasEnded) {
93
+ stop();
94
+ pause();
95
+ emitter.dispatchEnded();
96
+ return;
97
+ }
98
+ if (!hasBeenStopped) {
99
+ queueNextFrame();
100
+ }
101
+ };
102
+ const queueNextFrame = () => {
103
+ if (buffering.current) {
104
+ const stopListening = context.listenForResume(() => {
105
+ stopListening.remove();
106
+ if (hasBeenStopped) {
107
+ return;
108
+ }
109
+ startedTime = performance.now();
110
+ framesAdvanced = 0;
111
+ callback();
112
+ });
113
+ return;
114
+ }
115
+ if (isBackgroundedRef.current) {
116
+ reqAnimFrameCall = {
117
+ type: 'timeout',
118
+ // Note: Most likely, this will not be 1000 / fps, but the browser will throttle it to ~1/sec.
119
+ id: setTimeout(callback, 1000 / config.fps),
120
+ };
121
+ }
122
+ else {
123
+ reqAnimFrameCall = { type: 'raf', id: requestAnimationFrame(callback) };
124
+ }
125
+ };
126
+ queueNextFrame();
127
+ const onVisibilityChange = () => {
128
+ if (document.visibilityState === 'visible') {
129
+ return;
130
+ }
131
+ // If tab goes into the background, cancel requestAnimationFrame() and update immediately.
132
+ // , so the transition to setTimeout() can be fulfilled.
133
+ cancelQueuedFrame();
134
+ callback();
135
+ };
136
+ window.addEventListener('visibilitychange', onVisibilityChange);
137
+ return () => {
138
+ window.removeEventListener('visibilitychange', onVisibilityChange);
139
+ stop();
140
+ };
141
+ }, [
142
+ config,
143
+ loop,
144
+ pause,
145
+ playing,
146
+ setFrame,
147
+ emitter,
148
+ playbackRate,
149
+ inFrame,
150
+ outFrame,
151
+ moveToBeginningWhenEnded,
152
+ isBackgroundedRef,
153
+ getCurrentFrame,
154
+ buffering,
155
+ context,
156
+ ]);
157
+ (0, react_1.useEffect)(() => {
158
+ const interval = setInterval(() => {
159
+ if (lastTimeUpdateEvent.current === getCurrentFrame()) {
160
+ return;
161
+ }
162
+ emitter.dispatchTimeUpdate({ frame: getCurrentFrame() });
163
+ lastTimeUpdateEvent.current = getCurrentFrame();
164
+ }, 250);
165
+ return () => clearInterval(interval);
166
+ }, [emitter, getCurrentFrame]);
167
+ (0, react_1.useEffect)(() => {
168
+ emitter.dispatchFrameUpdate({ frame });
169
+ }, [emitter, frame]);
170
+ };
171
+ exports.usePlayback = usePlayback;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "url": "https://github.com/remotion-dev/remotion/tree/main/packages/player"
4
4
  },
5
5
  "name": "@remotion/player",
6
- "version": "4.0.295",
6
+ "version": "4.0.297",
7
7
  "description": "React component for embedding a Remotion preview into your app",
8
8
  "main": "dist/cjs/index.js",
9
9
  "types": "dist/cjs/index.d.ts",
@@ -28,7 +28,7 @@
28
28
  ],
29
29
  "license": "SEE LICENSE IN LICENSE.md",
30
30
  "dependencies": {
31
- "remotion": "4.0.295"
31
+ "remotion": "4.0.297"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "react": ">=16.8.0",
@@ -43,7 +43,7 @@
43
43
  "webpack": "5.96.1",
44
44
  "zod": "3.22.3",
45
45
  "eslint": "9.19.0",
46
- "@remotion/eslint-config-internal": "4.0.295"
46
+ "@remotion/eslint-config-internal": "4.0.297"
47
47
  },
48
48
  "keywords": [
49
49
  "remotion",