-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathtiming-src-connector.js
More file actions
227 lines (208 loc) · 8.87 KB
/
timing-src-connector.js
File metadata and controls
227 lines (208 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { subscribe } from './functions';
/** @typedef {import('./timing-src-connector.types').PlayerControls} PlayerControls */
/** @typedef {import('timing-object').ITimingObject} TimingObject */
/** @typedef {import('./timing-src-connector.types').TimingSrcConnectorOptions} TimingSrcConnectorOptions */
/** @typedef {(msg: string) => any} Logger */
/** @typedef {import('timing-object').TConnectionState} TConnectionState */
/**
* @type {TimingSrcConnectorOptions}
*
* For details on these properties and their effects, see the typescript definition referenced above.
*/
const defaultOptions = {
role: 'viewer',
autoPlayMuted: true,
allowedDrift: 0.3,
maxAllowedDrift: 1,
minCheckInterval: 0.1,
maxRateAdjustment: 0.2,
maxTimeToCatchUp: 1
};
/**
* There's a proposed W3C spec for the Timing Object which would introduce a new set of APIs that would simplify time-synchronization tasks for browser applications.
*
* Proposed spec: https://webtiming.github.io/timingobject/
* V3 Spec: https://timingsrc.readthedocs.io/en/latest/
* Demuxed talk: https://www.youtube.com/watch?v=cZSjDaGDmX8
*
* This class makes it easy to connect Vimeo.Player to a provided TimingObject via Vimeo.Player.setTimingSrc(myTimingObject, options) and the synchronization will be handled automatically.
*
* There are 5 general responsibilities in TimingSrcConnector:
*
* 1. `updatePlayer()` which sets the player's currentTime, playbackRate and pause/play state based on current state of the TimingObject.
* 2. `updateTimingObject()` which sets the TimingObject's position and velocity from the player's state.
* 3. `playerUpdater` which listens for change events on the TimingObject and will respond by calling updatePlayer.
* 4. `timingObjectUpdater` which listens to the player events of seeked, play and pause and will respond by calling `updateTimingObject()`.
* 5. `maintainPlaybackPosition` this is code that constantly monitors the player to make sure it's always in sync with the TimingObject. This is needed because videos will generally not play with precise time accuracy and there will be some drift which becomes more noticeable over longer periods (as noted in the timing-object spec). More details on this method below.
*/
export class TimingSrcConnector extends EventTarget {
logger;
/**
* @param {PlayerControls} player
* @param {TimingObject} timingObject
* @param {TimingSrcConnectorOptions} options
* @param {Logger} logger
*/
constructor(player, timingObject, options = {}, logger) {
super();
this.logger = logger;
this.init(timingObject, player, { ...defaultOptions, ...options });
}
disconnect() {
this.dispatchEvent(new Event('disconnect'));
}
/**
* @param {TimingObject} timingObject
* @param {PlayerControls} player
* @param {TimingSrcConnectorOptions} options
* @return {Promise<void>}
*/
async init(timingObject, player, options) {
await this.waitForTOReadyState(timingObject, 'open');
if (options.role === 'viewer') {
await this.updatePlayer(timingObject, player, options);
const playerUpdater = subscribe(timingObject, 'change', () => this.updatePlayer(timingObject, player, options));
const positionSync = this.maintainPlaybackPosition(timingObject, player, options);
this.addEventListener('disconnect', () => {
positionSync.cancel();
playerUpdater.cancel();
});
}
else {
await this.updateTimingObject(timingObject, player);
const timingObjectUpdater = subscribe(player, ['seeked', 'play', 'pause', 'ratechange'], () => this.updateTimingObject(timingObject, player), 'on', 'off');
this.addEventListener('disconnect', () => timingObjectUpdater.cancel());
}
}
/**
* Sets the TimingObject's state to reflect that of the player
*
* @param {TimingObject} timingObject
* @param {PlayerControls} player
* @return {Promise<void>}
*/
async updateTimingObject(timingObject, player) {
const [
position,
isPaused,
playbackRate
] = await Promise.all([player.getCurrentTime(), player.getPaused(), player.getPlaybackRate()]);
timingObject.update({
position,
velocity: isPaused ? 0 : playbackRate
});
}
/**
* Sets the player's timing state to reflect that of the TimingObject
*
* @param {TimingObject} timingObject
* @param {PlayerControls} player
* @param {TimingSrcConnectorOptions} options
* @return {Promise<void>}
*/
async updatePlayer(timingObject, player, options) {
const { position, velocity } = timingObject.query();
if (typeof position === 'number') {
player.setCurrentTime(position);
}
if (typeof velocity === 'number') {
if (velocity === 0) {
if ((await player.getPaused()) === false) {
player.pause();
}
}
else if (velocity > 0) {
if ((await player.getPaused()) === true) {
await player.play()
.catch(async(err) => {
if (err.name === 'NotAllowedError' && options.autoPlayMuted) {
await player.setMuted(true);
await player.play().catch((err2) => console.error('Couldn\'t play the video from TimingSrcConnector. Error:', err2));
}
});
this.updatePlayer(timingObject, player, options);
}
if ((await player.getPlaybackRate()) !== velocity) {
player.setPlaybackRate(velocity);
}
}
}
}
/**
* Since video players do not play with 100% time precision, we need to closely monitor
* our player to be sure it remains in sync with the TimingObject.
*
* If out of sync, we use the current conditions and the options provided to determine
* whether to re-sync via setting currentTime or adjusting the playbackRate
*
* @param {TimingObject} timingObject
* @param {PlayerControls} player
* @param {TimingSrcConnectorOptions} options
* @return {{cancel: (function(): void)}}
*/
maintainPlaybackPosition(timingObject, player, options) {
const { allowedDrift, maxAllowedDrift, minCheckInterval, maxRateAdjustment, maxTimeToCatchUp } = options;
const syncInterval = Math.min(maxTimeToCatchUp, Math.max(minCheckInterval, maxAllowedDrift)) * 1000;
const check = async() => {
if (timingObject.query().velocity === 0 || await player.getPaused() === true) {
return;
}
const diff = timingObject.query().position - (await player.getCurrentTime());
const diffAbs = Math.abs(diff);
this.log(`Drift: ${diff}`);
if (diffAbs > maxAllowedDrift) {
await this.adjustSpeed(player, 0);
player.setCurrentTime(timingObject.query().position);
this.log('Resync by currentTime');
}
else if (diffAbs > allowedDrift) {
const min = diffAbs / maxTimeToCatchUp;
const max = maxRateAdjustment;
const adjustment = min < max ? (max - min) / 2 : max;
await this.adjustSpeed(player, adjustment * Math.sign(diff));
this.log('Resync by playbackRate');
}
};
const interval = setInterval(() => check(), syncInterval);
return { cancel: () => clearInterval(interval) };
}
/**
* @param {string} msg
*/
log(msg) {
this.logger?.(`TimingSrcConnector: ${msg}`);
}
speedAdjustment = 0;
/**
* @param {PlayerControls} player
* @param {number} newAdjustment
* @return {Promise<void>}
*/
adjustSpeed = async(player, newAdjustment) => {
if (this.speedAdjustment === newAdjustment) {
return;
}
const newPlaybackRate = (await player.getPlaybackRate()) - this.speedAdjustment + newAdjustment;
this.log(`New playbackRate: ${newPlaybackRate}`);
await player.setPlaybackRate(newPlaybackRate);
this.speedAdjustment = newAdjustment;
};
/**
* @param {TimingObject} timingObject
* @param {TConnectionState} state
* @return {Promise<void>}
*/
waitForTOReadyState(timingObject, state) {
return new Promise((resolve) => {
const check = () => {
if (timingObject.readyState === state) {
resolve();
}
else {
timingObject.addEventListener('readystatechange', check, { once: true });
}
};
check();
});
}
}