@reactoo/watchtogether-sdk-js 2.7.80 → 2.7.82

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.
@@ -1,10 +1,10 @@
1
- import {setExactTimeout} from "../wt-utils";
1
+ import {setExactTimeout} from "../../models/utils";
2
2
 
3
3
  const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
4
-
4
+
5
5
  //SYNC VARS
6
6
  let _videoElement = null;
7
-
7
+
8
8
  const syncDefaultWaitTime = 60000;
9
9
  const syncShortWaitTime = 10000;
10
10
  const maxSyncThreshold = 0.5;
@@ -14,40 +14,40 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
14
14
  let syncNextWaitTime = null;
15
15
  let stopFlag = false;
16
16
  let isSyncing = false;
17
-
17
+
18
18
  let playbackRate = 2;
19
19
  let isPlaying = false;
20
20
  let isPreloading = true;
21
21
  let isProgrammaticallySeeked = false;
22
22
  let shouldPropagateMaster = false;
23
-
24
-
23
+
24
+
25
25
  const startSyncLoop = () => {
26
-
26
+
27
27
  if(!isConnected()) {
28
28
  room._log('--- Sync loop will not start due to user not connected yet ---');
29
29
  return
30
30
  }
31
-
31
+
32
32
  if(syncWaitId) {
33
33
  room._log('--- Sync loop already running ---');
34
34
  return
35
35
  }
36
-
36
+
37
37
  room._log('--- Sync enabled ---');
38
-
38
+
39
39
  stopFlag = false;
40
-
40
+
41
41
  const loop = () => {
42
-
42
+
43
43
  isSyncing = true;
44
44
  emitter.emit('playerSyncing', true);
45
-
45
+
46
46
  sync().finally(() => {
47
-
47
+
48
48
  isSyncing = false;
49
49
  emitter.emit('playerSyncing', false);
50
-
50
+
51
51
  if(isConnected() && !stopFlag) {
52
52
  syncWaitId = setTimeout(loop, syncNextWaitTime);
53
53
  }
@@ -55,36 +55,36 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
55
55
  room._log('--- Automatic stop due to user not connected or stop flag enabled ---');
56
56
  stopSyncLoop();
57
57
  }
58
-
58
+
59
59
  })
60
-
60
+
61
61
  };
62
-
62
+
63
63
  loop();
64
-
64
+
65
65
  };
66
-
66
+
67
67
  const stopSyncLoop = () => {
68
-
68
+
69
69
  room._log('--- Sync disabled ---');
70
-
70
+
71
71
  clearTimeout(syncWaitId);
72
72
  syncWaitId = null;
73
73
  currentSyncRetry = 0
74
74
  stopFlag = true;
75
-
75
+
76
76
  };
77
-
77
+
78
78
  const restartSyncLoop = () => {
79
-
79
+
80
80
  room._log('--- Sync restarting ---');
81
81
  stopSyncLoop();
82
82
  startSyncLoop();
83
-
83
+
84
84
  };
85
-
85
+
86
86
  const setNextWaitTime = (didSyncFail = false) => {
87
-
87
+
88
88
  if(!didSyncFail) {
89
89
  syncNextWaitTime = syncDefaultWaitTime;
90
90
  currentSyncRetry = 0
@@ -97,50 +97,50 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
97
97
  syncNextWaitTime = syncShortWaitTime;
98
98
  }
99
99
  }
100
-
100
+
101
101
  room._log('--- Next sync will occur in ' + syncNextWaitTime / 1000 + ' seconds ---');
102
-
102
+
103
103
  }
104
-
104
+
105
105
  const sync = () => {
106
-
106
+
107
107
  return getSyncData()
108
108
  .then(syncData => {
109
-
109
+
110
110
  if(syncData.isMaster) {
111
111
  if(_videoElement.paused) _videoElement.play();
112
112
  setNextWaitTime(false);
113
113
  return Promise.resolve();
114
114
  }
115
-
115
+
116
116
  else if(shouldPropagateMaster) {
117
117
  setNextWaitTime(false);
118
118
  return propagateMasterFunc();
119
119
  }
120
-
120
+
121
121
  else {
122
-
122
+
123
123
  const syncStartTime = Date.now();
124
124
  const {position, realPosition, isBufferSufficient} = calculateSyncDifferenceTime(syncData.masterFragmentSn, syncData.masterFragmentPos, syncData.ping);
125
125
  const currentPosition = getCurrentSegmentPosition() / 1000;
126
-
127
-
126
+
127
+
128
128
  if(position && Math.abs(position - currentPosition) <= maxSyncThreshold) {
129
129
  room._log(`We're within max sync threshold, no need to resync now`);
130
130
  setNextWaitTime(false);
131
131
  return Promise.resolve();
132
132
  }
133
-
133
+
134
134
  if(position !== null) {
135
135
  return seekTo(position)
136
136
  .then(() => {
137
137
  const seekDuration = (Date.now() - syncStartTime) / 1000;
138
138
  const syncPrecision = Math.abs((realPosition + seekDuration) - (getCurrentSegmentPosition() / 1000))
139
-
139
+
140
140
  room._log(`Insufficient buffer: `, !isBufferSufficient);
141
141
  room._log(`Seek duration is ${seekDuration}`);
142
142
  room._log(`Sync precision should be ${syncPrecision}`);
143
-
143
+
144
144
  const didSyncFail = syncPrecision > maxSyncThreshold
145
145
  setNextWaitTime(didSyncFail);
146
146
  return Promise.resolve();
@@ -159,7 +159,7 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
159
159
  return Promise.reject();
160
160
  })
161
161
  }
162
-
162
+
163
163
  const handleAddLocalParticipant = () => {
164
164
  if(shouldPropagateMaster) {
165
165
  propagateMasterFunc();
@@ -170,17 +170,17 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
170
170
  }, {once:true});
171
171
  }
172
172
  };
173
-
173
+
174
174
  const handleAddRemoteParticipant = () => {
175
175
  if(shouldPropagateMaster) {
176
176
  propagateMasterFunc();
177
177
  }
178
178
  };
179
-
179
+
180
180
  const isConnected = () => {
181
181
  return room._isDataChannelOpen && room.isConnected;
182
182
  };
183
-
183
+
184
184
  const parseDataEvents = (msg = {}) => {
185
185
  if(msg.videoroom === 'sync_source_set' && msg.wt_channel_id != "") {
186
186
  emitter.emit('changePlayerSource', msg.wt_channel_id);
@@ -194,8 +194,8 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
194
194
  let position = _videoElement && _videoElement.currentTime;
195
195
  return isNaN(position) ? 0 : position * 1000
196
196
  };
197
-
198
-
197
+
198
+
199
199
  const calculateSyncDifferenceTime = (fragmentSn, fragmentPos, ping) => {
200
200
  let seekRanges = _videoElement.buffered;
201
201
  let seekRange = {};
@@ -221,7 +221,7 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
221
221
  } else
222
222
  return {position: null, realPosition: position, isBufferSufficient: false}
223
223
  };
224
-
224
+
225
225
  const seekTo = (time) => {
226
226
  return new Promise((resolve, reject) => {
227
227
  if(_videoElement.currentTime !== time) {
@@ -273,7 +273,7 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
273
273
  } else resolve()
274
274
  });
275
275
  };
276
-
276
+
277
277
  const handlePlaying = () => {
278
278
  if(!isProgrammaticallySeeked) {
279
279
  room._log('Handle playing');
@@ -283,13 +283,13 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
283
283
  isPreloading = _videoElement.paused;
284
284
  isPlaying = !_videoElement.paused;
285
285
  };
286
-
286
+
287
287
  const handlePause = () => {
288
288
  stopSyncLoop()
289
289
  clientPaused().catch(() => {});
290
290
  isPlaying = !_videoElement.paused;
291
291
  };
292
-
292
+
293
293
  const handleStalledWaiting = () => {
294
294
  room._log('handleStalledWaiting');
295
295
  if(!isProgrammaticallySeeked) {
@@ -308,7 +308,7 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
308
308
  }
309
309
  }, 1000);
310
310
  };
311
-
311
+
312
312
  const roomSyncSend = (slaveId) => {
313
313
 
314
314
  if(!_videoElement) {
@@ -334,23 +334,23 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
334
334
  slave_id: room.webrtcVersion > 1000 ? String(slaveId) : Number(slaveId)
335
335
  }});
336
336
  };
337
-
337
+
338
338
  const getSyncData = () => {
339
-
339
+
340
340
  room._log('Sending roomSync request');
341
341
  let roomId = room.roomId;
342
342
  let fragmentPosition = parseInt(getCurrentSegmentPosition());
343
-
343
+
344
344
  return new Promise((resolve, reject) => {
345
-
345
+
346
346
  let now = new Date().getTime();
347
347
  let ping = null;
348
-
348
+
349
349
  let sid = setTimeout(() => {
350
350
  room.off('data', fn, this);
351
351
  reject('Timeout');
352
352
  }, 3000);
353
-
353
+
354
354
  let body = {
355
355
  request: "sync",
356
356
  room: roomId,
@@ -358,32 +358,32 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
358
358
  fragment: String("0"),
359
359
  fragment_pos: Number(fragmentPosition)
360
360
  };
361
-
361
+
362
362
  let fn = (msg) => {
363
-
363
+
364
364
  if(msg.videoroom && ['sync', 'sync_response'].includes(msg.videoroom)) {
365
-
365
+
366
366
  if(msg.sync_master_await) {
367
367
  room._log('Waiting for master position');
368
368
  if(!ping) {
369
369
  ping = (new Date().getTime() - now);
370
370
  }
371
371
  }
372
-
372
+
373
373
  else if(msg.sync_master_fragment || msg.sync_master_fragment_pos) {
374
374
  room._log('Got master position data');
375
375
  if(!ping) {
376
376
  ping = (new Date().getTime() - now);
377
377
  }
378
-
378
+
379
379
  room._log(`I'm master: ${!!msg.sync_master_self}`);
380
380
  room._log(`Ping: ${ping}`);
381
381
  room._log(`Master fragment: ${msg.sync_master_fragment}`);
382
382
  room._log(`Master fragment position: ${msg.sync_master_fragment_pos}`);
383
-
383
+
384
384
  room.off('data', fn, this);
385
385
  clearTimeout(sid);
386
-
386
+
387
387
  resolve({
388
388
  isMaster: !!msg.sync_master_self,
389
389
  ping: ping,
@@ -391,7 +391,7 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
391
391
  masterFragmentSn: parseInt(msg.sync_master_fragment),
392
392
  });
393
393
  }
394
-
394
+
395
395
  else {
396
396
  clearTimeout(sid);
397
397
  reject('Master lost connection')
@@ -406,14 +406,14 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
406
406
  });
407
407
  });
408
408
  };
409
-
409
+
410
410
  const clientPaused = () => {
411
411
  room._log('Sending client paused');
412
-
412
+
413
413
  if(!isConnected()) {
414
414
  return Promise.resolve();
415
415
  }
416
-
416
+
417
417
  return room.sendMessage(room.handleId, {
418
418
  body:{
419
419
  request: "sync_paused",
@@ -423,7 +423,7 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
423
423
  fragment_pos:0
424
424
  }});
425
425
  };
426
-
426
+
427
427
  const propagateMasterFunc = () => {
428
428
  room._log('Propagating master');
429
429
  if(!isConnected()) {
@@ -445,51 +445,51 @@ const syncNativeHls = function ({room, wt, roomSession, emitter} = {}) {
445
445
  __events: ['playerSyncing'],
446
446
 
447
447
  initialize: ({videoElement, syncOnLevelSwitch = false , propagateMaster = false} = {}) => {
448
-
449
-
448
+
449
+
450
450
  _videoElement = videoElement;
451
-
451
+
452
452
  shouldPropagateMaster = propagateMaster;
453
453
  isPlaying = _videoElement.paused === false;
454
-
454
+
455
455
  room.on('disconnect', stopSyncLoop);
456
456
  room.on('removeLocalParticipant', stopSyncLoop);
457
457
  room.on('addLocalParticipant', handleAddLocalParticipant);
458
458
  room.on('addRemoteParticipant', handleAddRemoteParticipant);
459
459
  room.on('data', parseDataEvents);
460
-
460
+
461
461
  if(shouldPropagateMaster) {
462
462
  propagateMasterFunc().catch(() => {});
463
463
  }
464
-
464
+
465
465
  if(_videoElement && _videoElement.paused === false) {
466
466
  _videoElement.addEventListener('progress', () => {
467
467
  restartSyncLoop();
468
468
  }, {once:true});
469
469
  }
470
-
470
+
471
471
  _videoElement.addEventListener('timeupdate', stalledGenerator);
472
472
  _videoElement.addEventListener('playing', handlePlaying);
473
473
  _videoElement.addEventListener('pause', handlePause);
474
-
474
+
475
475
  },
476
476
 
477
477
  destroy: () => {
478
-
478
+
479
479
  stopSyncLoop();
480
-
480
+
481
481
  room.off('disconnect', stopSyncLoop);
482
482
  room.off('removeLocalParticipant', stopSyncLoop);
483
483
  room.off('addLocalParticipant', handleAddLocalParticipant);
484
484
  room.off('addRemoteParticipant', handleAddRemoteParticipant);
485
485
  room.off('data', parseDataEvents);
486
-
486
+
487
487
  if(_videoElement) {
488
488
  _videoElement.removeEventListener('timeupdate', stalledGenerator);
489
489
  _videoElement.removeEventListener('playing', handlePlaying);
490
490
  _videoElement.removeEventListener('pause', handlePause);
491
491
  }
492
-
492
+
493
493
  }
494
494
  };
495
495