@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,4 +1,4 @@
1
- import {wait} from "../wt-utils";
1
+ import {wait} from "../../models/utils";
2
2
 
3
3
  const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
4
4
 
@@ -7,7 +7,7 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
7
7
  let _videoElement = null;
8
8
  let _roomId = null;
9
9
  let _syncDisabled = false;
10
-
10
+
11
11
  const syncDefaultWaitTime = 60000;
12
12
  const syncShortWaitTime = 10000;
13
13
  const maxSyncThreshold = 0.5;
@@ -19,42 +19,42 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
19
19
  let isSyncing = false;
20
20
  let seekingDebounceId = null
21
21
  let seekingDebounceTimeout = 300;
22
-
22
+
23
23
  let isPlaying = false;
24
24
  let isProgrammaticallySeeked = false;
25
25
  let isForcedMaster = false;
26
-
26
+
27
27
  const startSyncLoop = () => {
28
-
28
+
29
29
  if(_syncDisabled) {
30
30
  room._log('--- Sync loop will not start due to sync force disabled ---');
31
31
  return
32
32
  }
33
-
33
+
34
34
  if(!isConnected()) {
35
35
  room._log('--- Sync loop will not start due to user not connected yet ---');
36
36
  return
37
37
  }
38
-
38
+
39
39
  if(syncWaitId) {
40
40
  room._log('--- Sync loop already running ---');
41
41
  return
42
42
  }
43
-
43
+
44
44
  room._log('--- Sync enabled ---');
45
-
45
+
46
46
  stopFlag = false;
47
-
47
+
48
48
  const loop = () => {
49
-
49
+
50
50
  isSyncing = true;
51
51
  emitter.emit('playerSyncing', true);
52
-
52
+
53
53
  sync().finally(() => {
54
-
54
+
55
55
  isSyncing = false;
56
56
  emitter.emit('playerSyncing', false);
57
-
57
+
58
58
  if(isConnected() && !stopFlag) {
59
59
  syncWaitId = setTimeout(loop, syncNextWaitTime);
60
60
  }
@@ -62,36 +62,36 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
62
62
  room._log('--- Automatic stop due to user not connected or stop flag enabled ---');
63
63
  stopSyncLoop();
64
64
  }
65
-
65
+
66
66
  })
67
-
67
+
68
68
  };
69
-
69
+
70
70
  loop();
71
-
71
+
72
72
  };
73
-
73
+
74
74
  const stopSyncLoop = () => {
75
-
75
+
76
76
  room._log('--- Sync disabled ---');
77
-
77
+
78
78
  clearTimeout(syncWaitId);
79
79
  syncWaitId = null;
80
80
  currentSyncRetry = 0
81
81
  stopFlag = true;
82
-
82
+
83
83
  };
84
-
84
+
85
85
  const restartSyncLoop = () => {
86
-
86
+
87
87
  room._log('--- Sync restarting ---');
88
88
  stopSyncLoop();
89
89
  startSyncLoop();
90
-
90
+
91
91
  };
92
-
92
+
93
93
  const setNextWaitTime = (didSyncFail = false) => {
94
-
94
+
95
95
  if(!didSyncFail) {
96
96
  syncNextWaitTime = syncDefaultWaitTime;
97
97
  currentSyncRetry = 0
@@ -104,54 +104,54 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
104
104
  syncNextWaitTime = syncShortWaitTime;
105
105
  }
106
106
  }
107
-
107
+
108
108
  room._log('--- Next sync will occur in ' + syncNextWaitTime / 1000 + ' seconds ---');
109
-
109
+
110
110
  }
111
-
111
+
112
112
  const sync = () => {
113
-
113
+
114
114
  return getSyncData()
115
115
  .then(syncData => {
116
-
116
+
117
117
  if(syncData.isMaster || stopFlag) {
118
118
  setNextWaitTime(false);
119
119
  return Promise.resolve();
120
120
  }
121
-
121
+
122
122
  else if(isForcedMaster) {
123
123
  setNextWaitTime(false);
124
124
  return propagateMasterFunc();
125
125
  }
126
-
126
+
127
127
  else {
128
-
128
+
129
129
  const syncStartTime = Date.now();
130
130
  const {position, isBufferSufficient} = calculateSyncDifferenceTime(syncData.masterFragmentSn, syncData.masterFragmentPos, syncData.ping);
131
131
  const currentPosition = getCurrentSegmentPosition() / 1000;
132
-
132
+
133
133
  if(syncData.masterFragmentSn) {
134
134
  playMedia()
135
135
  } else {
136
136
  pauseMedia()
137
137
  }
138
-
138
+
139
139
  if(position && Math.abs(position - currentPosition) <= maxSyncThreshold) {
140
140
  room._log(`We're within max sync threshold, no need to resync now`);
141
141
  setNextWaitTime(false);
142
142
  return Promise.resolve();
143
143
  }
144
-
144
+
145
145
  if(position !== null) {
146
146
  return seekTo(position)
147
147
  .then(() => {
148
148
  const seekDuration = (Date.now() - syncStartTime) / 1000;
149
149
  const syncPrecision = Math.abs((position + (isPlaying ? seekDuration : 0)) - (getCurrentSegmentPosition() / 1000))
150
-
150
+
151
151
  room._log(`Insufficient buffer: `, !isBufferSufficient);
152
152
  room._log(`Seek duration is ${seekDuration}`);
153
153
  room._log(`Sync precision should be ${syncPrecision}`);
154
-
154
+
155
155
  const didSyncFail = syncPrecision > maxSyncThreshold
156
156
  setNextWaitTime(didSyncFail);
157
157
  return Promise.resolve();
@@ -169,8 +169,8 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
169
169
  setNextWaitTime(true);
170
170
  })
171
171
  }
172
-
173
-
172
+
173
+
174
174
  const handleAddLocalParticipant = () => {
175
175
  if(isForcedMaster) {
176
176
  propagateMasterFunc();
@@ -182,13 +182,13 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
182
182
  }, 1000);
183
183
  }
184
184
  };
185
-
185
+
186
186
  const handleAddRemoteParticipant = () => {
187
187
  if(isForcedMaster) {
188
188
  propagateMasterFunc();
189
189
  }
190
190
  };
191
-
191
+
192
192
  const handleRemoveRemoteParticipant = () => {
193
193
  if(isForcedMaster) {
194
194
  propagateMasterFunc();
@@ -198,7 +198,7 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
198
198
  const isConnected = () => {
199
199
  return room._isDataChannelOpen && room.isConnected;
200
200
  };
201
-
201
+
202
202
  const buffering = (event) => {
203
203
  if(event.mediaType === 'video' || event.mediaType === 'audio') {
204
204
  if(event.state === 'bufferStalled') {
@@ -206,22 +206,22 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
206
206
  }
207
207
  }
208
208
  };
209
-
209
+
210
210
  const executePlayerIotEvents = (data) => {
211
211
  let rid = _roomId || room.roomId;
212
212
  if(data.attributeName === 'playerReplay' && (!rid || data?.roomId === rid)) {
213
213
  seekTo(0).then(() => playMedia());
214
214
  }
215
-
215
+
216
216
  if(data.attributeName === 'playerPlay' && (!rid || data?.roomId === rid)) {
217
217
  playMedia();
218
218
  }
219
-
219
+
220
220
  if(data.attributeName === 'playerPause' && (!rid || data?.roomId === rid)) {
221
221
  pauseMedia();
222
222
  }
223
223
  };
224
-
224
+
225
225
  const parseIotEvents = (data) => {
226
226
  if(data.event === 'template_updated') {
227
227
  if(data.operation === 'multi') {
@@ -236,9 +236,9 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
236
236
  }
237
237
  }
238
238
  };
239
-
239
+
240
240
  const parseDataEvents = (msg = {}) => {
241
-
241
+
242
242
  if(msg.videoroom === 'sync_source_set' && msg.wt_channel_id != "") {
243
243
  emitter.emit('changePlayerSource', msg.wt_channel_id);
244
244
  }
@@ -246,24 +246,24 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
246
246
  roomSyncSend(msg.sync_slave_id).catch(()=>{});
247
247
  }
248
248
  else if(msg.videoroom === 'message') {
249
-
249
+
250
250
  // ignoring non studio commands if master
251
251
  if(isForcedMaster && msg.from !== null) {
252
252
  return;
253
253
  }
254
-
254
+
255
255
  if(_syncDisabled) {
256
256
  return;
257
257
  }
258
-
258
+
259
259
  if(msg.user_action === 'sync_seeking') {
260
260
  seekMedia(msg)
261
261
  .catch(() => {
262
-
262
+
263
263
  });
264
264
  }
265
265
  else if(msg.user_action === 'sync_seeked') {
266
-
266
+
267
267
  }
268
268
  else if(msg.user_action === 'sync_play' || msg.action === 'sync_play') {
269
269
  playMedia();
@@ -543,22 +543,22 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
543
543
  __events: ['playerSyncing'],
544
544
 
545
545
  initialize: ({dashInstance, libraryInstance, propagateMaster = false, isStudio = false, roomId, syncDisabled} = {}) => {
546
-
546
+
547
547
  _libraryInstance = dashInstance || libraryInstance;
548
548
  _videoElement = _libraryInstance.getVideoElement();
549
549
  _roomId = roomId;
550
550
  _syncDisabled = syncDisabled;
551
-
551
+
552
552
  if(!_libraryInstance) {
553
553
  console.log('No player instance');
554
554
  return;
555
555
  }
556
556
 
557
557
  emitter.emit('playerSyncing', false);
558
-
558
+
559
559
  isForcedMaster = propagateMaster;
560
560
  isPlaying = _videoElement.paused === false;
561
-
561
+
562
562
  room.on('disconnect', stopSyncLoop);
563
563
  room.on('addLocalParticipant', handleAddLocalParticipant);
564
564
  room.on('removeLocalParticipant', stopSyncLoop);
@@ -566,7 +566,7 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
566
566
  room.on('removeRemoteParticipant', handleRemoveRemoteParticipant);
567
567
  room.on('data', parseDataEvents);
568
568
  wt.iot.$on('message', parseIotEvents);
569
-
569
+
570
570
  if(isForcedMaster) {
571
571
  propagateMasterFunc().catch(() => {});
572
572
  }
@@ -590,7 +590,7 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
590
590
 
591
591
  destroy: () => {
592
592
  stopSyncLoop();
593
-
593
+
594
594
  room.off('disconnect', stopSyncLoop);
595
595
  room.off('addLocalParticipant', handleAddLocalParticipant);
596
596
  room.off('removeLocalParticipant', stopSyncLoop);
@@ -610,7 +610,7 @@ const syncVodDashJs = function ({room, wt, roomSession, emitter} = {}) {
610
610
  _videoElement.removeEventListener('seeked', handleSeeked);
611
611
  _videoElement.removeEventListener('ended', handleEnded);
612
612
  }
613
-
613
+
614
614
  _libraryInstance = null;
615
615
  _videoElement = null;
616
616
  }