@microsoft/live-share-media 1.0.0-preview.8 → 1.0.0

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.
Files changed (86) hide show
  1. package/LICENSE +64 -61
  2. package/README.md +11 -15
  3. package/bin/IMediaPlayer.d.ts +20 -20
  4. package/bin/IMediaPlayer.js +6 -6
  5. package/bin/LiveMediaSession.d.ts +90 -94
  6. package/bin/LiveMediaSession.d.ts.map +1 -1
  7. package/bin/LiveMediaSession.js +228 -231
  8. package/bin/LiveMediaSession.js.map +1 -1
  9. package/bin/LiveMediaSessionCoordinator.d.ts +238 -227
  10. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -1
  11. package/bin/LiveMediaSessionCoordinator.js +476 -407
  12. package/bin/LiveMediaSessionCoordinator.js.map +1 -1
  13. package/bin/LiveMediaSessionCoordinatorSuspension.d.ts +19 -19
  14. package/bin/LiveMediaSessionCoordinatorSuspension.js +27 -27
  15. package/bin/MediaPlayerSynchronizer.d.ts +173 -163
  16. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  17. package/bin/MediaPlayerSynchronizer.js +491 -468
  18. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  19. package/bin/MediaSessionActionThrottler.d.ts +11 -11
  20. package/bin/MediaSessionActionThrottler.js +13 -13
  21. package/bin/MediaSessionExtensions.d.ts +34 -34
  22. package/bin/MediaSessionExtensions.d.ts.map +1 -1
  23. package/bin/MediaSessionExtensions.js +12 -12
  24. package/bin/RepeatedActionThrottler.d.ts +19 -19
  25. package/bin/RepeatedActionThrottler.js +62 -62
  26. package/bin/RepeatedActionThrottler.js.map +1 -1
  27. package/bin/VolumeManager.d.ts +83 -83
  28. package/bin/VolumeManager.js +174 -174
  29. package/bin/index.d.ts +13 -13
  30. package/bin/index.js +29 -29
  31. package/bin/internals/GroupCoordinatorState.d.ts +93 -92
  32. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  33. package/bin/internals/GroupCoordinatorState.js +407 -404
  34. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  35. package/bin/internals/GroupPlaybackPosition.d.ts +67 -66
  36. package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
  37. package/bin/internals/GroupPlaybackPosition.js +166 -166
  38. package/bin/internals/GroupPlaybackPosition.js.map +1 -1
  39. package/bin/internals/GroupPlaybackTrack.d.ts +45 -45
  40. package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
  41. package/bin/internals/GroupPlaybackTrack.js +121 -121
  42. package/bin/internals/GroupPlaybackTrackData.d.ts +39 -39
  43. package/bin/internals/GroupPlaybackTrackData.d.ts.map +1 -1
  44. package/bin/internals/GroupPlaybackTrackData.js +69 -69
  45. package/bin/internals/GroupTransportState.d.ts +49 -48
  46. package/bin/internals/GroupTransportState.d.ts.map +1 -1
  47. package/bin/internals/GroupTransportState.js +111 -111
  48. package/bin/internals/GroupTransportState.js.map +1 -1
  49. package/bin/internals/consts.d.ts +75 -72
  50. package/bin/internals/consts.d.ts.map +1 -1
  51. package/bin/internals/consts.js +84 -81
  52. package/bin/internals/consts.js.map +1 -1
  53. package/bin/internals/index.d.ts +10 -10
  54. package/bin/internals/index.js +26 -26
  55. package/package.json +9 -7
  56. package/src/LiveMediaSession.ts +24 -20
  57. package/src/LiveMediaSessionCoordinator.ts +101 -29
  58. package/src/MediaPlayerSynchronizer.ts +30 -14
  59. package/src/internals/GroupCoordinatorState.ts +49 -34
  60. package/src/internals/GroupPlaybackPosition.ts +7 -3
  61. package/src/internals/GroupPlaybackTrack.ts +2 -2
  62. package/src/internals/GroupPlaybackTrackData.ts +2 -2
  63. package/src/internals/GroupTransportState.ts +11 -4
  64. package/src/internals/consts.ts +4 -0
  65. package/typedoc.json +10 -0
  66. package/bin/EphemeralMediaSession.d.ts +0 -102
  67. package/bin/EphemeralMediaSession.d.ts.map +0 -1
  68. package/bin/EphemeralMediaSession.js +0 -226
  69. package/bin/EphemeralMediaSession.js.map +0 -1
  70. package/bin/EphemeralMediaSessionCoordinator.d.ts +0 -228
  71. package/bin/EphemeralMediaSessionCoordinator.d.ts.map +0 -1
  72. package/bin/EphemeralMediaSessionCoordinator.js +0 -396
  73. package/bin/EphemeralMediaSessionCoordinator.js.map +0 -1
  74. package/bin/EphemeralMediaSessionCoordinatorSuspension.d.ts +0 -20
  75. package/bin/EphemeralMediaSessionCoordinatorSuspension.d.ts.map +0 -1
  76. package/bin/EphemeralMediaSessionCoordinatorSuspension.js +0 -28
  77. package/bin/EphemeralMediaSessionCoordinatorSuspension.js.map +0 -1
  78. package/bin/VolumeLimiter.d.ts +0 -70
  79. package/bin/VolumeLimiter.d.ts.map +0 -1
  80. package/bin/VolumeLimiter.js +0 -170
  81. package/bin/VolumeLimiter.js.map +0 -1
  82. package/src/test/GroupPlaybackPosition.spec.ts +0 -600
  83. package/src/test/GroupPlaybackTrack.spec.ts +0 -122
  84. package/src/test/SharedMediaSession.spec.ts +0 -149
  85. package/src/test/SharedMediaSessionCoordinator.spec.ts +0 -271
  86. package/src/test/VolumeManager.spec.ts +0 -219
@@ -1,405 +1,408 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) Microsoft Corporation. All rights reserved.
4
- * Licensed under the Microsoft Live Share SDK License.
5
- */
6
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
7
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8
- return new (P || (P = Promise))(function (resolve, reject) {
9
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
10
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
11
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
12
- step((generator = generator.apply(thisArg, _arguments || [])).next());
13
- });
14
- };
15
- var __importDefault = (this && this.__importDefault) || function (mod) {
16
- return (mod && mod.__esModule) ? mod : { "default": mod };
17
- };
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.GroupCoordinatorState = exports.GroupCoordinatorStateEvents = void 0;
20
- const live_share_1 = require("@microsoft/live-share");
21
- const events_1 = __importDefault(require("events"));
22
- const GroupPlaybackTrack_1 = require("./GroupPlaybackTrack");
23
- const GroupTransportState_1 = require("./GroupTransportState");
24
- const GroupPlaybackPosition_1 = require("./GroupPlaybackPosition");
25
- const GroupTransportState_2 = require("./GroupTransportState");
26
- const GroupPlaybackTrackData_1 = require("./GroupPlaybackTrackData");
27
- const consts_1 = require("./consts");
28
- /**
29
- * @hidden
30
- */
31
- var GroupCoordinatorStateEvents;
32
- (function (GroupCoordinatorStateEvents) {
33
- GroupCoordinatorStateEvents["newwaitpoint"] = "newwaitpoint";
34
- GroupCoordinatorStateEvents["triggeraction"] = "triggeraction";
35
- })(GroupCoordinatorStateEvents = exports.GroupCoordinatorStateEvents || (exports.GroupCoordinatorStateEvents = {}));
36
- /**
37
- * @hidden
38
- */
39
- class GroupCoordinatorState extends events_1.default {
40
- constructor(runtime, maxPlaybackDrift, positionUpdateInterval, getMediaPlayerState) {
41
- super();
42
- // Suspension tracking
43
- this._suspensionCnt = 0;
44
- // "Soft Suspension" tracking
45
- // - Soft suspensions temporarily disconnect the local media session when transitioning between
46
- // paused, playing, and none states. This is to give the local client time to finish any inprogress
47
- // operations like sending a transport command or ending a suspension.
48
- this._lastStateChange = "none";
49
- this._lastStateChangeTime = 0;
50
- this._runtime = runtime;
51
- this._logger = new live_share_1.LiveTelemetryLogger(runtime);
52
- this._maxPlaybackDrift = maxPlaybackDrift;
53
- this._playbackTrack = new GroupPlaybackTrack_1.GroupPlaybackTrack(getMediaPlayerState);
54
- this._playbackTrackData = new GroupPlaybackTrackData_1.GroupPlaybackTrackData(this._playbackTrack);
55
- this._transportState = new GroupTransportState_1.GroupTransportState(this._playbackTrack, getMediaPlayerState);
56
- this._playbackPosition = new GroupPlaybackPosition_1.GroupPlaybackPosition(this._transportState, this._runtime, positionUpdateInterval);
57
- this._getMediaPlayerState = getMediaPlayerState;
58
- // Listen track related events
59
- this._playbackTrack.on(GroupPlaybackTrack_1.GroupPlaybackTrackEvents.trackChange, (evt) => {
60
- if (!this.isSuspended) {
61
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackChanged);
62
- this.emitSetTrack(evt.metadata);
63
- }
64
- else {
65
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackChangeDelayed);
66
- }
67
- });
68
- // Listen for track data changes
69
- this._playbackTrackData.on(GroupPlaybackTrackData_1.PlaybackTrackDataEvents.dataChange, (evt) => {
70
- if (!this.isSuspended && !this.isWaiting) {
71
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackDataChanged);
72
- this.emitTriggerAction({
73
- action: "datachange",
74
- data: evt.data,
75
- });
76
- }
77
- else {
78
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackDataChangeDelayed);
79
- }
80
- });
81
- // Listen to transport related events
82
- this._transportState.on(GroupTransportState_2.GroupTransportStateEvents.transportStateChange, (evt) => {
83
- if (!this.isSuspended && !this.isWaiting) {
84
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TransportStateChanged, null, {
85
- action: evt.action,
86
- seekTime: evt.seekTime,
87
- });
88
- // Trigger action
89
- switch (evt.action) {
90
- case "play":
91
- this.emitTriggerAction({
92
- action: "play",
93
- seekTime: evt.seekTime,
94
- });
95
- break;
96
- case "pause":
97
- this.emitTriggerAction({
98
- action: "pause",
99
- seekTime: evt.seekTime,
100
- });
101
- break;
102
- case "seekto":
103
- this.emitTriggerAction({
104
- action: "seekto",
105
- seekTime: evt.seekTime,
106
- });
107
- break;
108
- }
109
- }
110
- else {
111
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator
112
- .TransportStateChangeDelayed, null, {
113
- action: evt.action,
114
- seekTime: evt.seekTime,
115
- });
116
- }
117
- });
118
- }
119
- get playbackTrack() {
120
- return this._playbackTrack;
121
- }
122
- get playbackTrackData() {
123
- return this._playbackTrackData;
124
- }
125
- get transportState() {
126
- return this._transportState;
127
- }
128
- get playbackPosition() {
129
- return this._playbackPosition;
130
- }
131
- get waitingAt() {
132
- return this._waitPoint;
133
- }
134
- get isSuspended() {
135
- return this._suspensionCnt > 0;
136
- }
137
- get isWaiting() {
138
- return !!this._waitPoint;
139
- }
140
- beginSuspension(waitPoint) {
141
- this._suspensionCnt++;
142
- if (waitPoint) {
143
- // Save most recent wait point
144
- this._waitPoint = waitPoint;
145
- // Add to track state if dynamic wait point
146
- this.playbackTrack.addWaitPoint(waitPoint);
147
- }
148
- }
149
- endSuspension(syncState) {
150
- if (this._suspensionCnt == 0) {
151
- throw new Error(`Too many suspensions.`);
152
- }
153
- this._suspensionCnt--;
154
- if (this._suspensionCnt == 0 && syncState) {
155
- // Sync media session to group
156
- this.syncLocalMediaSession();
157
- }
158
- }
159
- createPositionUpdateEvent(state) {
160
- const { positionState, playbackState } = state;
161
- if (this.isSuspended) {
162
- // Report suspension state
163
- return {
164
- track: this.playbackTrack.current,
165
- trackData: this.playbackTrackData.current,
166
- transport: this.transportState.current,
167
- playbackState: "suspended",
168
- position: (positionState === null || positionState === void 0 ? void 0 : positionState.position) || 0.0,
169
- waitPoint: this._waitPoint,
170
- };
171
- }
172
- else {
173
- // Report current position
174
- return {
175
- track: this.playbackTrack.current,
176
- trackData: this.playbackTrackData.current,
177
- transport: this.transportState.current,
178
- playbackState: this._waitPoint ? "waiting" : playbackState,
179
- position: (positionState === null || positionState === void 0 ? void 0 : positionState.position) || 0.0,
180
- waitPoint: this._waitPoint,
181
- };
182
- }
183
- }
184
- handleSetTrack(event, local) {
185
- // Update shared track
186
- // - Will trigger 'trackChange' event to update media session
187
- const updated = this.playbackTrack.updateTrack({
188
- metadata: event.metadata,
189
- waitPoints: event.waitPoints,
190
- timestamp: event.timestamp,
191
- clientId: event.clientId || "",
192
- });
193
- if (updated) {
194
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator.RemoteSetTrackReceived, null, {
195
- correlationId: live_share_1.LiveTelemetryLogger.formatCorrelationId(event.clientId, event.timestamp),
196
- });
197
- }
198
- }
199
- handleSetTrackData(event, local) {
200
- // Update shared track data
201
- // - Will trigger 'dataChange' event to update media session
202
- const updated = this.playbackTrackData.updateData({
203
- data: event.data,
204
- timestamp: event.timestamp,
205
- clientId: event.clientId || "",
206
- });
207
- if (updated) {
208
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator.RemoteSetTrackReceived, null, {
209
- correlationId: live_share_1.LiveTelemetryLogger.formatCorrelationId(event.clientId, event.timestamp),
210
- });
211
- }
212
- }
213
- handleTransportCommand(event, local) {
214
- // Ensure change is for current track
215
- // - Will trigger a 'trackChange' event if newer track.
216
- this.playbackTrack.updateTrack(event.track);
217
- if (this.playbackTrack.compare(event.track.metadata)) {
218
- // Update playback state
219
- let playbackState = this.transportState.playbackState;
220
- switch (event.name) {
221
- case "play":
222
- playbackState = "playing";
223
- break;
224
- case "pause":
225
- playbackState = "paused";
226
- break;
227
- }
228
- // Try to update playback state
229
- const newState = {
230
- playbackState: playbackState,
231
- startPosition: event.position,
232
- timestamp: event.timestamp,
233
- clientId: event.clientId || "",
234
- };
235
- const updated = this.transportState.updateState(newState);
236
- if (updated) {
237
- const correlationId = live_share_1.LiveTelemetryLogger.formatCorrelationId(event.clientId, event.timestamp);
238
- switch (event.name) {
239
- case "play":
240
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator
241
- .RemotePlayReceived, null, {
242
- correlationId: correlationId,
243
- });
244
- break;
245
- case "pause":
246
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator
247
- .RemotePauseReceived, null, {
248
- correlationId: correlationId,
249
- });
250
- break;
251
- case "seekTo":
252
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator
253
- .RemoteSeekToReceived, null, {
254
- correlationId: correlationId,
255
- });
256
- break;
257
- }
258
- }
259
- }
260
- }
261
- handlePositionUpdate(event, local) {
262
- // Ensure change is for current track
263
- // - Will trigger a 'trackChange' event if newer track.
264
- this.playbackTrack.updateTrack(event.track);
265
- if (this.playbackTrack.compare(event.track.metadata)) {
266
- // Ensure we have the latest track data
267
- this.playbackTrackData.updateData(event.trackData);
268
- // Update transport state if needed
269
- // - Ignore transport state changes if the client has ended as this will cause the local
270
- // player to start playback even though the video may have ended for everyone.
271
- if (event.playbackState != "ended") {
272
- this.transportState.updateState(event.transport);
273
- }
274
- // Ensure change is for current transport state
275
- if (this.transportState.compare(event.transport)) {
276
- // Update playback position
277
- const position = {
278
- playbackState: event.playbackState,
279
- waitPoint: event.waitPoint,
280
- position: event.position,
281
- timestamp: event.timestamp,
282
- clientId: event.clientId || "",
283
- };
284
- this.playbackPosition.UpdatePlaybackPosition(position);
285
- // Ensure local media session is in sync with group
286
- if (local && !this.isSuspended) {
287
- // Begin a "soft suspension" on select state changes
288
- // - This is needed because the catchup logic can try to sink the client with
289
- // the rest of the group while the local player is trying to to seek to a
290
- // new position.
291
- switch (event.playbackState) {
292
- case "none":
293
- case "paused":
294
- case "playing":
295
- if (event.playbackState != this._lastStateChange) {
296
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator
297
- .BeginSoftSuspension, null, { playbackState: event.playbackState });
298
- this._lastStateChange = event.playbackState;
299
- this._lastStateChangeTime =
300
- live_share_1.LiveEvent.getTimestamp();
301
- }
302
- break;
303
- }
304
- // Are we waiting?
305
- if (this.isWaiting) {
306
- // Check for end of waiting
307
- if (this.playbackPosition.clientsWaiting == 0) {
308
- this._waitPoint = undefined;
309
- // Start syncing again
310
- this.syncLocalMediaSession();
311
- }
312
- }
313
- else {
314
- // Sync client with group
315
- this.syncLocalMediaSession();
316
- }
317
- }
318
- }
319
- }
320
- }
321
- syncLocalMediaSession() {
322
- return __awaiter(this, void 0, void 0, function* () {
323
- // Skip further syncs if we're waiting or in a "soft suspension".
324
- const softSuspensionDelta = live_share_1.LiveEvent.getTimestamp() - this._lastStateChangeTime;
325
- if (!this.isWaiting && softSuspensionDelta >= 1000) {
326
- let { metadata, trackData, positionState, playbackState } = this._getMediaPlayerState();
327
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.CheckingForSyncIssues);
328
- // Check for track change
329
- if (!this.playbackTrack.compare(metadata)) {
330
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackOutOfSync);
331
- this.emitSetTrack(this.playbackTrack.metadata);
332
- return;
333
- }
334
- // Once local playback has ended there's no need to further sync the client. In fact
335
- // doing so could cause the player to loop.
336
- // - The playbackPosition.trackEnded check is to catch late joiners and prevents syncing if everyone
337
- // else has finished the video.
338
- // - Should another user seek or press play after the video has ended that will cause
339
- // a transport action to trigger which will take the player out of the ended state.
340
- if (playbackState != "ended" &&
341
- !this._playbackPosition.trackEnded) {
342
- // Ensure we have a position
343
- let position = positionState === null || positionState === void 0 ? void 0 : positionState.position;
344
- if (position == undefined) {
345
- position = 0.0;
346
- }
347
- // Check for catchup
348
- // - Target can return -1 in cases where there is no position tracking data
349
- const target = this.playbackPosition.targetPosition;
350
- if (target >= 0.0 &&
351
- Math.abs(target - position) >=
352
- this._maxPlaybackDrift.seconds) {
353
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.PositionOutOfSync, null, {
354
- current: position,
355
- target: target,
356
- });
357
- yield this.emitTriggerAction({
358
- action: "catchup",
359
- seekTime: target,
360
- });
361
- }
362
- // Sync transport state
363
- if (this.transportState.playbackState != playbackState) {
364
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TransportOutOfSync, null, {
365
- current: playbackState,
366
- target: this.transportState.playbackState,
367
- });
368
- switch (this.transportState.playbackState) {
369
- case "playing":
370
- yield this.emitTriggerAction({ action: "play" });
371
- break;
372
- case "paused":
373
- yield this.emitTriggerAction({ action: "pause" });
374
- break;
375
- }
376
- }
377
- // Sync track data
378
- if (JSON.stringify(this.playbackTrackData.data) !=
379
- JSON.stringify(trackData)) {
380
- this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackDataOutOfSync);
381
- yield this.emitTriggerAction({
382
- action: "datachange",
383
- data: this.playbackTrackData.data,
384
- });
385
- }
386
- }
387
- }
388
- });
389
- }
390
- emitSetTrack(metadata) {
391
- // Reset tracking states
392
- this._waitPoint = undefined;
393
- // Trigger settrack action
394
- this.emitTriggerAction({ action: "settrack", metadata: metadata });
395
- }
396
- emitTriggerAction(details) {
397
- const evt = {
398
- name: GroupCoordinatorStateEvents.triggeraction,
399
- details: details,
400
- };
401
- this.emit(GroupCoordinatorStateEvents.triggeraction, evt);
402
- }
403
- }
404
- exports.GroupCoordinatorState = GroupCoordinatorState;
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation. All rights reserved.
4
+ * Licensed under the Microsoft Live Share SDK License.
5
+ */
6
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
7
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8
+ return new (P || (P = Promise))(function (resolve, reject) {
9
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
10
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
11
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
12
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
13
+ });
14
+ };
15
+ var __importDefault = (this && this.__importDefault) || function (mod) {
16
+ return (mod && mod.__esModule) ? mod : { "default": mod };
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.GroupCoordinatorState = exports.GroupCoordinatorStateEvents = void 0;
20
+ const live_share_1 = require("@microsoft/live-share");
21
+ const events_1 = __importDefault(require("events"));
22
+ const GroupPlaybackTrack_1 = require("./GroupPlaybackTrack");
23
+ const GroupTransportState_1 = require("./GroupTransportState");
24
+ const GroupPlaybackPosition_1 = require("./GroupPlaybackPosition");
25
+ const GroupTransportState_2 = require("./GroupTransportState");
26
+ const GroupPlaybackTrackData_1 = require("./GroupPlaybackTrackData");
27
+ const consts_1 = require("./consts");
28
+ /**
29
+ * @hidden
30
+ */
31
+ var GroupCoordinatorStateEvents;
32
+ (function (GroupCoordinatorStateEvents) {
33
+ GroupCoordinatorStateEvents["newwaitpoint"] = "newwaitpoint";
34
+ GroupCoordinatorStateEvents["triggeraction"] = "triggeraction";
35
+ })(GroupCoordinatorStateEvents = exports.GroupCoordinatorStateEvents || (exports.GroupCoordinatorStateEvents = {}));
36
+ /**
37
+ * @hidden
38
+ */
39
+ class GroupCoordinatorState extends events_1.default {
40
+ constructor(runtime, liveRuntime, maxPlaybackDrift, positionUpdateInterval, getMediaPlayerState) {
41
+ super();
42
+ // Suspension tracking
43
+ this._suspensionCnt = 0;
44
+ // "Soft Suspension" tracking
45
+ // - Soft suspensions temporarily disconnect the local media session when transitioning between
46
+ // paused, playing, and none states. This is to give the local client time to finish any inprogress
47
+ // operations like sending a transport command or ending a suspension.
48
+ this._lastStateChange = "none";
49
+ this._lastStateChangeTime = 0;
50
+ this._runtime = runtime;
51
+ this._liveRuntime = liveRuntime;
52
+ this._logger = new live_share_1.LiveTelemetryLogger(runtime, liveRuntime);
53
+ this._maxPlaybackDrift = maxPlaybackDrift;
54
+ this._playbackTrack = new GroupPlaybackTrack_1.GroupPlaybackTrack(getMediaPlayerState);
55
+ this._playbackTrackData = new GroupPlaybackTrackData_1.GroupPlaybackTrackData(this._playbackTrack);
56
+ this._transportState = new GroupTransportState_1.GroupTransportState(this._playbackTrack, getMediaPlayerState, this._liveRuntime);
57
+ this._playbackPosition = new GroupPlaybackPosition_1.GroupPlaybackPosition(this._transportState, this._runtime, this._liveRuntime, positionUpdateInterval);
58
+ this._getMediaPlayerState = getMediaPlayerState;
59
+ // Listen track related events
60
+ this._playbackTrack.on(GroupPlaybackTrack_1.GroupPlaybackTrackEvents.trackChange, (evt) => {
61
+ if (!this.isSuspended) {
62
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackChanged);
63
+ this.emitSetTrack(evt.metadata);
64
+ }
65
+ else {
66
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackChangeDelayed);
67
+ }
68
+ });
69
+ // Listen for track data changes
70
+ this._playbackTrackData.on(GroupPlaybackTrackData_1.PlaybackTrackDataEvents.dataChange, (evt) => {
71
+ if (!this.isSuspended && !this.isWaiting) {
72
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackDataChanged);
73
+ this.emitTriggerAction({
74
+ action: "datachange",
75
+ data: evt.data,
76
+ });
77
+ }
78
+ else {
79
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackDataChangeDelayed);
80
+ }
81
+ });
82
+ // Listen to transport related events
83
+ this._transportState.on(GroupTransportState_2.GroupTransportStateEvents.transportStateChange, (evt) => {
84
+ if (!this.isSuspended && !this.isWaiting) {
85
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TransportStateChanged, null, {
86
+ action: evt.action,
87
+ seekTime: evt.seekTime,
88
+ });
89
+ // Trigger action
90
+ switch (evt.action) {
91
+ case "play":
92
+ this.emitTriggerAction({
93
+ action: "play",
94
+ seekTime: evt.seekTime,
95
+ });
96
+ break;
97
+ case "pause":
98
+ this.emitTriggerAction({
99
+ action: "pause",
100
+ seekTime: evt.seekTime,
101
+ });
102
+ break;
103
+ case "seekto":
104
+ this.emitTriggerAction({
105
+ action: "seekto",
106
+ seekTime: evt.seekTime,
107
+ });
108
+ break;
109
+ }
110
+ }
111
+ else {
112
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator
113
+ .TransportStateChangeDelayed, null, {
114
+ action: evt.action,
115
+ seekTime: evt.seekTime,
116
+ });
117
+ }
118
+ });
119
+ }
120
+ get playbackTrack() {
121
+ return this._playbackTrack;
122
+ }
123
+ get playbackTrackData() {
124
+ return this._playbackTrackData;
125
+ }
126
+ get transportState() {
127
+ return this._transportState;
128
+ }
129
+ get playbackPosition() {
130
+ return this._playbackPosition;
131
+ }
132
+ get waitingAt() {
133
+ return this._waitPoint;
134
+ }
135
+ get isSuspended() {
136
+ return this._suspensionCnt > 0;
137
+ }
138
+ get isWaiting() {
139
+ return !!this._waitPoint;
140
+ }
141
+ beginSuspension(waitPoint) {
142
+ this._suspensionCnt++;
143
+ if (waitPoint) {
144
+ // Save most recent wait point
145
+ this._waitPoint = waitPoint;
146
+ // Add to track state if dynamic wait point
147
+ this.playbackTrack.addWaitPoint(waitPoint);
148
+ }
149
+ }
150
+ endSuspension(syncState) {
151
+ if (this._suspensionCnt == 0) {
152
+ throw new Error(`Too many suspensions.`);
153
+ }
154
+ this._suspensionCnt--;
155
+ if (this._suspensionCnt == 0 && syncState) {
156
+ // Sync media session to group
157
+ this.syncLocalMediaSession();
158
+ }
159
+ }
160
+ createPositionUpdateEvent(state) {
161
+ const { positionState, playbackState } = state;
162
+ if (this.isSuspended) {
163
+ // Report suspension state
164
+ return {
165
+ track: this.playbackTrack.current,
166
+ trackData: this.playbackTrackData.current,
167
+ transport: this.transportState.current,
168
+ playbackState: "suspended",
169
+ position: (positionState === null || positionState === void 0 ? void 0 : positionState.position) || 0.0,
170
+ waitPoint: this._waitPoint,
171
+ };
172
+ }
173
+ else {
174
+ // Report current position
175
+ return {
176
+ track: this.playbackTrack.current,
177
+ trackData: this.playbackTrackData.current,
178
+ transport: this.transportState.current,
179
+ playbackState: this._waitPoint ? "waiting" : playbackState,
180
+ position: (positionState === null || positionState === void 0 ? void 0 : positionState.position) || 0.0,
181
+ waitPoint: this._waitPoint,
182
+ };
183
+ }
184
+ }
185
+ handleSetTrack(event, local) {
186
+ // Update shared track
187
+ // - Will trigger 'trackChange' event to update media session
188
+ const updated = this.playbackTrack.updateTrack({
189
+ metadata: event.data.metadata,
190
+ waitPoints: event.data.waitPoints,
191
+ timestamp: event.timestamp,
192
+ clientId: event.clientId || "",
193
+ });
194
+ if (updated) {
195
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator.RemoteSetTrackReceived, null, {
196
+ correlationId: live_share_1.LiveTelemetryLogger.formatCorrelationId(event.clientId, event.timestamp),
197
+ });
198
+ }
199
+ }
200
+ handleSetTrackData(event, local) {
201
+ // Update shared track data
202
+ // - Will trigger 'dataChange' event to update media session
203
+ const updated = this.playbackTrackData.updateData({
204
+ data: event.data.data,
205
+ timestamp: event.timestamp,
206
+ clientId: event.clientId || "",
207
+ });
208
+ if (updated) {
209
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator.RemoteSetTrackReceived, null, {
210
+ correlationId: live_share_1.LiveTelemetryLogger.formatCorrelationId(event.clientId, event.timestamp),
211
+ });
212
+ }
213
+ }
214
+ handleTransportCommand(event, local) {
215
+ // Ensure change is for current track
216
+ // - Will trigger a 'trackChange' event if newer track.
217
+ this.playbackTrack.updateTrack(event.data.track);
218
+ if (this.playbackTrack.compare(event.data.track.metadata)) {
219
+ // Update playback state
220
+ let playbackState = this.transportState.playbackState;
221
+ switch (event.name) {
222
+ case "play":
223
+ playbackState = "playing";
224
+ break;
225
+ case "pause":
226
+ playbackState = "paused";
227
+ break;
228
+ }
229
+ // Try to update playback state
230
+ const newState = {
231
+ playbackState: playbackState,
232
+ startPosition: event.data.position,
233
+ timestamp: event.timestamp,
234
+ clientId: event.clientId || "",
235
+ };
236
+ const updated = this.transportState.updateState(newState);
237
+ if (updated) {
238
+ const correlationId = live_share_1.LiveTelemetryLogger.formatCorrelationId(event.clientId, event.timestamp);
239
+ switch (event.name) {
240
+ case "play":
241
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator
242
+ .RemotePlayReceived, null, {
243
+ correlationId: correlationId,
244
+ });
245
+ break;
246
+ case "pause":
247
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator
248
+ .RemotePauseReceived, null, {
249
+ correlationId: correlationId,
250
+ });
251
+ break;
252
+ case "seekTo":
253
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.SessionCoordinator
254
+ .RemoteSeekToReceived, null, {
255
+ correlationId: correlationId,
256
+ });
257
+ break;
258
+ }
259
+ }
260
+ }
261
+ }
262
+ handlePositionUpdate(event, local) {
263
+ // Ensure change is for current track
264
+ // - Will trigger a 'trackChange' event if newer track.
265
+ this.playbackTrack.updateTrack(event.data.track);
266
+ if (this.playbackTrack.compare(event.data.track.metadata)) {
267
+ // Ensure we have the latest track data
268
+ this.playbackTrackData.updateData(event.data.trackData);
269
+ // Update transport state if needed
270
+ // - Ignore transport state changes if the client has ended as this will cause the local
271
+ // player to start playback even though the video may have ended for everyone.
272
+ if (event.data.playbackState != "ended") {
273
+ this.transportState.updateState(event.data.transport);
274
+ }
275
+ // Ensure change is for current transport state
276
+ if (this.transportState.compare(event.data.transport)) {
277
+ // Update playback position
278
+ const position = {
279
+ playbackState: event.data.playbackState,
280
+ waitPoint: event.data.waitPoint,
281
+ position: event.data.position,
282
+ timestamp: event.timestamp,
283
+ clientId: event.clientId || "",
284
+ };
285
+ this.playbackPosition.UpdatePlaybackPosition(position);
286
+ // Ensure local media session is in sync with group
287
+ if (local && !this.isSuspended) {
288
+ // Begin a "soft suspension" on select state changes
289
+ // - This is needed because the catchup logic can try to sink the client with
290
+ // the rest of the group while the local player is trying to to seek to a
291
+ // new position.
292
+ switch (event.data.playbackState) {
293
+ case "none":
294
+ case "paused":
295
+ case "playing":
296
+ if (event.data.playbackState !=
297
+ this._lastStateChange) {
298
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator
299
+ .BeginSoftSuspension, null, { playbackState: event.data.playbackState });
300
+ this._lastStateChange =
301
+ event.data.playbackState;
302
+ this._lastStateChangeTime =
303
+ this._liveRuntime.getTimestamp();
304
+ }
305
+ break;
306
+ }
307
+ // Are we waiting?
308
+ if (this.isWaiting) {
309
+ // Check for end of waiting
310
+ if (this.playbackPosition.clientsWaiting == 0) {
311
+ this._waitPoint = undefined;
312
+ // Start syncing again
313
+ this.syncLocalMediaSession();
314
+ }
315
+ }
316
+ else {
317
+ // Sync client with group
318
+ this.syncLocalMediaSession();
319
+ }
320
+ }
321
+ }
322
+ }
323
+ }
324
+ syncLocalMediaSession() {
325
+ return __awaiter(this, void 0, void 0, function* () {
326
+ // Skip further syncs if we're waiting or in a "soft suspension".
327
+ const softSuspensionDelta = this._liveRuntime.getTimestamp() - this._lastStateChangeTime;
328
+ if (!this.isWaiting && softSuspensionDelta >= 1000) {
329
+ let { metadata, trackData, positionState, playbackState } = this._getMediaPlayerState();
330
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.CheckingForSyncIssues);
331
+ // Check for track change
332
+ if (!this.playbackTrack.compare(metadata)) {
333
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackOutOfSync);
334
+ this.emitSetTrack(this.playbackTrack.metadata);
335
+ return;
336
+ }
337
+ // Once local playback has ended there's no need to further sync the client. In fact
338
+ // doing so could cause the player to loop.
339
+ // - The playbackPosition.trackEnded check is to catch late joiners and prevents syncing if everyone
340
+ // else has finished the video.
341
+ // - Should another user seek or press play after the video has ended that will cause
342
+ // a transport action to trigger which will take the player out of the ended state.
343
+ if (playbackState != "ended" &&
344
+ !this._playbackPosition.trackEnded) {
345
+ // Ensure we have a position
346
+ let position = positionState === null || positionState === void 0 ? void 0 : positionState.position;
347
+ if (position == undefined) {
348
+ position = 0.0;
349
+ }
350
+ // Check for catchup
351
+ // - Target can return -1 in cases where there is no position tracking data
352
+ const target = this.playbackPosition.targetPosition;
353
+ if (target >= 0.0 &&
354
+ Math.abs(target - position) >=
355
+ this._maxPlaybackDrift.seconds) {
356
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.PositionOutOfSync, null, {
357
+ current: position,
358
+ target: target,
359
+ });
360
+ yield this.emitTriggerAction({
361
+ action: "catchup",
362
+ seekTime: target,
363
+ });
364
+ }
365
+ // Sync transport state
366
+ if (this.transportState.playbackState != playbackState) {
367
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TransportOutOfSync, null, {
368
+ current: playbackState,
369
+ target: this.transportState.playbackState,
370
+ });
371
+ switch (this.transportState.playbackState) {
372
+ case "playing":
373
+ yield this.emitTriggerAction({ action: "play" });
374
+ break;
375
+ case "paused":
376
+ yield this.emitTriggerAction({ action: "pause" });
377
+ break;
378
+ }
379
+ }
380
+ // Sync track data
381
+ if (JSON.stringify(this.playbackTrackData.data) !=
382
+ JSON.stringify(trackData)) {
383
+ this._logger.sendTelemetryEvent(consts_1.TelemetryEvents.GroupCoordinator.TrackDataOutOfSync);
384
+ yield this.emitTriggerAction({
385
+ action: "datachange",
386
+ data: this.playbackTrackData.data,
387
+ });
388
+ }
389
+ }
390
+ }
391
+ });
392
+ }
393
+ emitSetTrack(metadata) {
394
+ // Reset tracking states
395
+ this._waitPoint = undefined;
396
+ // Trigger settrack action
397
+ this.emitTriggerAction({ action: "settrack", metadata: metadata });
398
+ }
399
+ emitTriggerAction(details) {
400
+ const evt = {
401
+ name: GroupCoordinatorStateEvents.triggeraction,
402
+ details: details,
403
+ };
404
+ this.emit(GroupCoordinatorStateEvents.triggeraction, evt);
405
+ }
406
+ }
407
+ exports.GroupCoordinatorState = GroupCoordinatorState;
405
408
  //# sourceMappingURL=GroupCoordinatorState.js.map