@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 syncDashJs = 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 syncDashJs = 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 syncDashJs = 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 syncDashJs = 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 syncDashJs = 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 syncDashJs = 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 buffering = (event) => {
185
185
  if(event.mediaType === 'video' || event.mediaType === 'audio') {
186
186
  if(event.state === 'bufferStalled') {
@@ -188,7 +188,7 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
188
188
  }
189
189
  }
190
190
  };
191
-
191
+
192
192
  const parseDataEvents = (msg = {}) => {
193
193
  if(msg.videoroom === 'sync_source_set' && msg.wt_channel_id != "") {
194
194
  emitter.emit('changePlayerSource', msg.wt_channel_id);
@@ -197,12 +197,12 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
197
197
  roomSyncSend(msg.sync_slave_id).catch(()=>{});
198
198
  }
199
199
  };
200
-
200
+
201
201
  const getCurrentSegmentPosition = () => {
202
202
  let position = _videoElement && _videoElement.currentTime;
203
203
  return isNaN(position) ? 0 : position * 1000
204
204
  };
205
-
205
+
206
206
  const calculateSyncDifferenceTime = (fragmentSn, fragmentPos, ping) => {
207
207
  let seekRanges = _videoElement.buffered;
208
208
  let seekRange = {};
@@ -228,7 +228,7 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
228
228
  } else
229
229
  return {position: null, realPosition:position, isBufferSufficient: false}
230
230
  };
231
-
231
+
232
232
  const seekTo = (time) => {
233
233
  return new Promise((resolve, reject) => {
234
234
  if(_videoElement.currentTime !== time) {
@@ -280,7 +280,7 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
280
280
  } else resolve()
281
281
  });
282
282
  };
283
-
283
+
284
284
  const handlePlaying = () => {
285
285
  if(!isProgrammaticallySeeked) {
286
286
  room._log('Handle playing');
@@ -290,13 +290,13 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
290
290
  isPreloading = _videoElement.paused;
291
291
  isPlaying = !_videoElement.paused;
292
292
  };
293
-
293
+
294
294
  const handlePause = () => {
295
295
  stopSyncLoop()
296
296
  clientPaused().catch(() => {});
297
297
  isPlaying = !_videoElement.paused;
298
298
  };
299
-
299
+
300
300
  const handleStalledWaiting = () => {
301
301
  room._log('handleStalledWaiting');
302
302
  if(!isProgrammaticallySeeked) {
@@ -305,9 +305,9 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
305
305
  isPreloading = _videoElement.paused;
306
306
  }
307
307
  };
308
-
308
+
309
309
  const roomSyncSend = (slaveId) => {
310
-
310
+
311
311
  if(!_libraryInstance || !_videoElement) {
312
312
  room._log(
313
313
  `I've been asked for position even if we don't have player attached.
@@ -315,12 +315,12 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
315
315
  );
316
316
  return Promise.resolve();
317
317
  }
318
-
318
+
319
319
  let fragmentPosition = parseInt(getCurrentSegmentPosition());
320
-
320
+
321
321
  room._log(`Sending my position to ${slaveId}`);
322
322
  room._log(`Current time: ${fragmentPosition}`);
323
-
323
+
324
324
  return room.sendMessage(room.handleId, {
325
325
  body : {
326
326
  request: "sync_response",
@@ -331,23 +331,23 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
331
331
  slave_id: room.webrtcVersion > 1000 ? String(slaveId) : Number(slaveId)
332
332
  }});
333
333
  };
334
-
334
+
335
335
  const getSyncData = () => {
336
-
336
+
337
337
  room._log('Sending roomSync request');
338
338
  let roomId = room.roomId;
339
339
  let fragmentPosition = parseInt(getCurrentSegmentPosition());
340
-
340
+
341
341
  return new Promise((resolve, reject) => {
342
-
342
+
343
343
  let now = new Date().getTime();
344
344
  let ping = null;
345
-
345
+
346
346
  let sid = setTimeout(() => {
347
347
  room.off('data', fn, this);
348
348
  reject('Timeout');
349
349
  }, 3000);
350
-
350
+
351
351
  let body = {
352
352
  request: "sync",
353
353
  room: roomId,
@@ -355,32 +355,32 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
355
355
  fragment: String("0"),
356
356
  fragment_pos: Number(fragmentPosition)
357
357
  };
358
-
358
+
359
359
  let fn = (msg) => {
360
-
360
+
361
361
  if(msg.videoroom && ['sync', 'sync_response'].includes(msg.videoroom)) {
362
-
362
+
363
363
  if(msg.sync_master_await) {
364
364
  room._log('Waiting for master position');
365
365
  if(!ping) {
366
366
  ping = (new Date().getTime() - now);
367
367
  }
368
368
  }
369
-
369
+
370
370
  else if(msg.sync_master_fragment || msg.sync_master_fragment_pos) {
371
371
  room._log('Got master position data');
372
372
  if(!ping) {
373
373
  ping = (new Date().getTime() - now);
374
374
  }
375
-
375
+
376
376
  room._log(`I'm master: ${!!msg.sync_master_self}`);
377
377
  room._log(`Ping: ${ping}`);
378
378
  room._log(`Master fragment: ${msg.sync_master_fragment}`);
379
379
  room._log(`Master fragment position: ${msg.sync_master_fragment_pos}`);
380
-
380
+
381
381
  room.off('data', fn, this);
382
382
  clearTimeout(sid);
383
-
383
+
384
384
  resolve({
385
385
  isMaster: !!msg.sync_master_self,
386
386
  ping: ping,
@@ -388,7 +388,7 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
388
388
  masterFragmentSn: parseInt(msg.sync_master_fragment),
389
389
  });
390
390
  }
391
-
391
+
392
392
  else {
393
393
  clearTimeout(sid);
394
394
  reject('Master lost connection')
@@ -403,14 +403,14 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
403
403
  });
404
404
  });
405
405
  };
406
-
406
+
407
407
  const clientPaused = () => {
408
408
  room._log('Sending client paused');
409
-
409
+
410
410
  if(!isConnected()) {
411
411
  return Promise.resolve();
412
412
  }
413
-
413
+
414
414
  return room.sendMessage(room.handleId, {
415
415
  body:{
416
416
  request: "sync_paused",
@@ -420,7 +420,7 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
420
420
  fragment_pos:0
421
421
  }});
422
422
  };
423
-
423
+
424
424
  const propagateMasterFunc = () => {
425
425
  room._log('Propagating master');
426
426
  if(!isConnected()) {
@@ -442,34 +442,34 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
442
442
  __events: ['playerSyncing'],
443
443
 
444
444
  initialize: ({dashInstance, libraryInstance, propagateMaster = false} = {}) => {
445
-
445
+
446
446
  _libraryInstance = dashInstance || libraryInstance; // backwards comp;
447
447
  _videoElement = _libraryInstance.getVideoElement();
448
-
448
+
449
449
  if(!_libraryInstance) {
450
450
  room._log('No dash player instance!');
451
451
  return;
452
452
  }
453
-
453
+
454
454
  shouldPropagateMaster = propagateMaster;
455
455
  isPlaying = _videoElement.paused === false;
456
-
456
+
457
457
  room.on('disconnect', stopSyncLoop);
458
458
  room.on('removeLocalParticipant', stopSyncLoop);
459
459
  room.on('addLocalParticipant', handleAddLocalParticipant);
460
460
  room.on('addRemoteParticipant', handleAddRemoteParticipant);
461
461
  room.on('data', parseDataEvents);
462
-
462
+
463
463
  if(shouldPropagateMaster) {
464
464
  propagateMasterFunc().catch(() => {});
465
465
  }
466
-
466
+
467
467
  if(_videoElement && _videoElement.paused === false) {
468
468
  _videoElement.addEventListener('progress', () => {
469
469
  restartSyncLoop();
470
470
  }, {once:true});
471
471
  }
472
-
472
+
473
473
  _libraryInstance.on('bufferStateChanged', buffering);
474
474
  _videoElement.addEventListener('playing', handlePlaying);
475
475
  _videoElement.addEventListener('pause', handlePause);
@@ -478,22 +478,22 @@ const syncDashJs = function ({room, wt, roomSession, emitter} = {}) {
478
478
 
479
479
  destroy: () => {
480
480
  stopSyncLoop();
481
-
481
+
482
482
  room.off('disconnect', stopSyncLoop);
483
483
  room.off('removeLocalParticipant', stopSyncLoop);
484
484
  room.off('addLocalParticipant', handleAddLocalParticipant);
485
485
  room.off('addRemoteParticipant', handleAddRemoteParticipant);
486
486
  room.off('data', parseDataEvents);
487
-
487
+
488
488
  if(_libraryInstance) {
489
489
  _libraryInstance.off('bufferStateChanged', buffering);
490
490
  }
491
-
491
+
492
492
  if(_videoElement) {
493
493
  _videoElement.removeEventListener('playing', handlePlaying);
494
494
  _videoElement.removeEventListener('pause', handlePause);
495
495
  }
496
-
496
+
497
497
  _libraryInstance = null;
498
498
  }
499
499
  };