@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,11 +1,11 @@
1
- import {setExactTimeout} from "../wt-utils";
1
+ import {setExactTimeout} from "../../models/utils";
2
2
 
3
3
  const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
4
-
4
+
5
5
  //SYNC VARS
6
6
  let _libraryInstance = null;
7
7
  let _videoElement = null;
8
-
8
+
9
9
  const syncDefaultWaitTime = 60000;
10
10
  const syncShortWaitTime = 10000;
11
11
  const maxSyncThreshold = 0.5;
@@ -15,39 +15,39 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
15
15
  let syncNextWaitTime = null;
16
16
  let stopFlag = false;
17
17
  let isSyncing = false;
18
-
18
+
19
19
  let playbackRate = 2;
20
20
  let isPlaying = false;
21
21
  let isPreloading = true;
22
22
  let isProgrammaticallySeeked = false;
23
23
  let shouldPropagateMaster = false;
24
-
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 syncDaznDash = 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 syncDaznDash = 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 syncDaznDash = 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 syncDaznDash = 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);
@@ -189,12 +189,12 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
189
189
  roomSyncSend(msg.sync_slave_id).catch(()=>{});
190
190
  }
191
191
  };
192
-
192
+
193
193
  const getCurrentSegmentPosition = () => {
194
194
  let position = _videoElement && _videoElement.currentTime;
195
195
  return isNaN(position) ? 0 : position * 1000
196
196
  };
197
-
197
+
198
198
  const calculateSyncDifferenceTime = (fragmentSn, fragmentPos, ping) => {
199
199
  let seekRanges = _videoElement.buffered;
200
200
  let seekRange = {};
@@ -220,7 +220,7 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
220
220
  } else
221
221
  return {position: null, realPostion: position, isBufferSufficient: false}
222
222
  };
223
-
223
+
224
224
  const seekTo = (time) => {
225
225
  return new Promise((resolve, reject) => {
226
226
  if(_videoElement.currentTime !== time) {
@@ -272,7 +272,7 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
272
272
  } else resolve()
273
273
  });
274
274
  };
275
-
275
+
276
276
  // const seekTo = (time) => {
277
277
  // return new Promise((resolve, reject) => {
278
278
  // if(_videoElement.currentTime !== time) {
@@ -287,7 +287,7 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
287
287
  // } else resolve()
288
288
  // });
289
289
  // };
290
-
290
+
291
291
  const handlePlaying = () => {
292
292
  if(!isProgrammaticallySeeked) {
293
293
  room._log('Handle playing');
@@ -297,13 +297,13 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
297
297
  isPreloading = _videoElement.paused;
298
298
  isPlaying = !_videoElement.paused;
299
299
  };
300
-
300
+
301
301
  const handlePause = () => {
302
302
  stopSyncLoop()
303
303
  clientPaused().catch(() => {});
304
304
  isPlaying = !_videoElement.paused;
305
305
  };
306
-
306
+
307
307
  const handleStalledWaiting = () => {
308
308
  room._log('handleStalledWaiting');
309
309
  if(!isProgrammaticallySeeked) {
@@ -312,9 +312,9 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
312
312
  isPreloading = _videoElement.paused;
313
313
  }
314
314
  };
315
-
315
+
316
316
  const roomSyncSend = (slaveId) => {
317
-
317
+
318
318
  if(!_libraryInstance || !_videoElement) {
319
319
  room._log(
320
320
  `I've been asked for position even if we don't have player attached.
@@ -322,12 +322,12 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
322
322
  );
323
323
  return Promise.resolve();
324
324
  }
325
-
325
+
326
326
  let fragmentPosition = parseInt(getCurrentSegmentPosition());
327
-
327
+
328
328
  room._log(`Sending my position to ${slaveId}`);
329
329
  room._log(`Current time: ${fragmentPosition}`);
330
-
330
+
331
331
  return room.sendMessage(room.handleId, {
332
332
  body : {
333
333
  request: "sync_response",
@@ -338,23 +338,23 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
338
338
  slave_id: room.webrtcVersion > 1000 ? String(slaveId) : Number(slaveId)
339
339
  }});
340
340
  };
341
-
341
+
342
342
  const getSyncData = () => {
343
-
343
+
344
344
  room._log('Sending roomSync request');
345
345
  let roomId = room.roomId;
346
346
  let fragmentPosition = parseInt(getCurrentSegmentPosition());
347
-
347
+
348
348
  return new Promise((resolve, reject) => {
349
-
349
+
350
350
  let now = new Date().getTime();
351
351
  let ping = null;
352
-
352
+
353
353
  let sid = setTimeout(() => {
354
354
  room.off('data', fn, this);
355
355
  reject('Timeout');
356
356
  }, 3000);
357
-
357
+
358
358
  let body = {
359
359
  request: "sync",
360
360
  room: roomId,
@@ -362,32 +362,32 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
362
362
  fragment: String("0"),
363
363
  fragment_pos: Number(fragmentPosition)
364
364
  };
365
-
365
+
366
366
  let fn = (msg) => {
367
-
367
+
368
368
  if(msg.videoroom && ['sync', 'sync_response'].includes(msg.videoroom)) {
369
-
369
+
370
370
  if(msg.sync_master_await) {
371
371
  room._log('Waiting for master position');
372
372
  if(!ping) {
373
373
  ping = (new Date().getTime() - now);
374
374
  }
375
375
  }
376
-
376
+
377
377
  else if(msg.sync_master_fragment || msg.sync_master_fragment_pos) {
378
378
  room._log('Got master position data');
379
379
  if(!ping) {
380
380
  ping = (new Date().getTime() - now);
381
381
  }
382
-
382
+
383
383
  room._log(`I'm master: ${!!msg.sync_master_self}`);
384
384
  room._log(`Ping: ${ping}`);
385
385
  room._log(`Master fragment: ${msg.sync_master_fragment}`);
386
386
  room._log(`Master fragment position: ${msg.sync_master_fragment_pos}`);
387
-
387
+
388
388
  room.off('data', fn, this);
389
389
  clearTimeout(sid);
390
-
390
+
391
391
  resolve({
392
392
  isMaster: !!msg.sync_master_self,
393
393
  ping: ping,
@@ -395,7 +395,7 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
395
395
  masterFragmentSn: parseInt(msg.sync_master_fragment),
396
396
  });
397
397
  }
398
-
398
+
399
399
  else {
400
400
  clearTimeout(sid);
401
401
  reject('Master lost connection')
@@ -410,14 +410,14 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
410
410
  });
411
411
  });
412
412
  };
413
-
413
+
414
414
  const clientPaused = () => {
415
415
  room._log('Sending client paused');
416
-
416
+
417
417
  if(!isConnected()) {
418
418
  return Promise.resolve();
419
419
  }
420
-
420
+
421
421
  return room.sendMessage(room.handleId, {
422
422
  body:{
423
423
  request: "sync_paused",
@@ -427,7 +427,7 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
427
427
  fragment_pos:0
428
428
  }});
429
429
  };
430
-
430
+
431
431
  const propagateMasterFunc = () => {
432
432
  room._log('Propagating master');
433
433
  if(!isConnected()) {
@@ -443,65 +443,65 @@ const syncDaznDash = function ({room, wt, roomSession, emitter} = {}) {
443
443
  fragment_pos:0
444
444
  }});
445
445
  };
446
-
446
+
447
447
  return {
448
-
448
+
449
449
  __events: ['playerSyncing'],
450
-
450
+
451
451
  initialize: ({libraryInstance, propagateMaster = false} = {}) => {
452
-
452
+
453
453
  _libraryInstance = libraryInstance;
454
454
  _videoElement = _libraryInstance.videoElement;
455
-
455
+
456
456
  shouldPropagateMaster = propagateMaster;
457
457
  isPlaying = _videoElement.paused === false;
458
-
458
+
459
459
  room.on('disconnect', stopSyncLoop);
460
460
  room.on('removeLocalParticipant', stopSyncLoop);
461
461
  room.on('addLocalParticipant', handleAddLocalParticipant);
462
462
  room.on('addRemoteParticipant', handleAddRemoteParticipant);
463
463
  room.on('data', parseDataEvents);
464
-
464
+
465
465
  if(shouldPropagateMaster) {
466
466
  propagateMasterFunc().catch(() => {});
467
467
  }
468
-
468
+
469
469
  if(_videoElement && _videoElement.paused === false) {
470
470
  _videoElement.addEventListener('progress', () => {
471
471
  restartSyncLoop();
472
472
  }, {once:true});
473
473
  }
474
-
474
+
475
475
  _libraryInstance.addEventListener('buffer', handleStalledWaiting);
476
476
  _videoElement.addEventListener('playing', handlePlaying);
477
477
  _videoElement.addEventListener('pause', handlePause);
478
-
479
-
478
+
479
+
480
480
  },
481
-
481
+
482
482
  destroy: () => {
483
-
483
+
484
484
  stopSyncLoop();
485
-
485
+
486
486
  room.off('disconnect', stopSyncLoop);
487
487
  room.off('removeLocalParticipant', stopSyncLoop);
488
488
  room.off('addLocalParticipant', handleAddLocalParticipant);
489
489
  room.off('addRemoteParticipant', handleAddRemoteParticipant);
490
490
  room.off('data', parseDataEvents);
491
-
491
+
492
492
  if(_libraryInstance) {
493
493
  _libraryInstance.removeEventListener('buffer', handleStalledWaiting);
494
494
  }
495
-
495
+
496
496
  if(_videoElement) {
497
497
  _videoElement.removeEventListener('playing', handlePlaying);
498
498
  _videoElement.removeEventListener('pause', handlePause);
499
499
  }
500
-
500
+
501
501
  _libraryInstance = null;
502
502
  }
503
503
  };
504
-
504
+
505
505
  };
506
506
 
507
507
  export default syncDaznDash;