@microsoft/live-share-media 1.2.2 → 1.3.1

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 (54) hide show
  1. package/.nycrc.json +10 -10
  2. package/LICENSE +90 -90
  3. package/README.md +135 -135
  4. package/bin/LiveMediaSession.d.ts.map +1 -1
  5. package/bin/LiveMediaSession.js +4 -6
  6. package/bin/LiveMediaSession.js.map +1 -1
  7. package/bin/LiveMediaSessionCoordinator.d.ts +4 -3
  8. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -1
  9. package/bin/LiveMediaSessionCoordinator.js +56 -69
  10. package/bin/LiveMediaSessionCoordinator.js.map +1 -1
  11. package/bin/MediaPlayerSynchronizer.d.ts +12 -0
  12. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  13. package/bin/MediaPlayerSynchronizer.js +26 -6
  14. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  15. package/bin/MediaSessionExtensions.d.ts +0 -4
  16. package/bin/MediaSessionExtensions.d.ts.map +1 -1
  17. package/bin/MediaSessionExtensions.js.map +1 -1
  18. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  19. package/bin/internals/GroupCoordinatorState.js +1 -1
  20. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  21. package/bin/internals/consts.d.ts +2 -2
  22. package/bin/internals/consts.d.ts.map +1 -1
  23. package/bin/internals/consts.js +2 -2
  24. package/bin/internals/consts.js.map +1 -1
  25. package/bin/internals/errors.d.ts +17 -0
  26. package/bin/internals/errors.d.ts.map +1 -0
  27. package/bin/internals/errors.js +34 -0
  28. package/bin/internals/errors.js.map +1 -0
  29. package/bin/internals/index.d.ts +1 -0
  30. package/bin/internals/index.d.ts.map +1 -1
  31. package/bin/internals/index.js +1 -0
  32. package/bin/internals/index.js.map +1 -1
  33. package/package.json +45 -45
  34. package/src/IMediaPlayer.ts +29 -29
  35. package/src/LiveMediaSession.ts +337 -334
  36. package/src/LiveMediaSessionCoordinator.ts +711 -671
  37. package/src/LiveMediaSessionCoordinatorSuspension.ts +39 -39
  38. package/src/MediaPlayerSynchronizer.ts +714 -690
  39. package/src/MediaSessionActionThrottler.ts +19 -19
  40. package/src/MediaSessionExtensions.ts +182 -187
  41. package/src/RepeatedActionThrottler.ts +76 -76
  42. package/src/VolumeManager.ts +200 -200
  43. package/src/index.ts +14 -14
  44. package/src/internals/GroupCoordinatorState.ts +685 -683
  45. package/src/internals/GroupPlaybackPosition.ts +228 -228
  46. package/src/internals/GroupPlaybackTrack.ts +182 -182
  47. package/src/internals/GroupPlaybackTrackData.ts +104 -104
  48. package/src/internals/GroupTransportState.ts +170 -170
  49. package/src/internals/consts.ts +103 -102
  50. package/src/internals/errors.ts +42 -0
  51. package/src/internals/index.ts +12 -11
  52. package/tsconfig.build.json +8 -8
  53. package/tsconfig.json +76 -76
  54. package/typedoc.json +10 -10
@@ -1,334 +1,337 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import { DataObjectFactory } from "@fluidframework/aqueduct";
7
- import {
8
- DynamicObjectRegistry,
9
- LiveDataObject,
10
- LiveDataObjectInitializeState,
11
- LiveTelemetryLogger,
12
- UserMeetingRole,
13
- } from "@microsoft/live-share";
14
- import { MediaPlayerSynchronizer } from "./MediaPlayerSynchronizer";
15
- import { ITriggerActionEvent, TelemetryEvents } from "./internals";
16
- import {
17
- MediaSessionCoordinatorEvents,
18
- ExtendedMediaSessionAction,
19
- ExtendedMediaSessionActionDetails,
20
- ExtendedMediaSessionActionHandler,
21
- } from "./MediaSessionExtensions";
22
- import {
23
- LiveMediaSessionCoordinator,
24
- IMediaPlayerState,
25
- } from "./LiveMediaSessionCoordinator";
26
- import { MediaSessionActionThrottler } from "./MediaSessionActionThrottler";
27
- import { RepeatedActionThrottler } from "./RepeatedActionThrottler";
28
- import { IMediaPlayer } from "./IMediaPlayer";
29
-
30
- /**
31
- * Live fluid object that synchronizes media playback across multiple clients.
32
- */
33
- export class LiveMediaSession extends LiveDataObject {
34
- private _actionThrottler: MediaSessionActionThrottler =
35
- new RepeatedActionThrottler();
36
- private _logger?: LiveTelemetryLogger;
37
- private _requestPlayerStateHandler?: () => IMediaPlayerState;
38
- private _coordinator?: LiveMediaSessionCoordinator;
39
- private readonly _actionHandlers: Map<
40
- string,
41
- MediaSessionActionHandler | ExtendedMediaSessionActionHandler
42
- > = new Map();
43
- private _synchronizing?: MediaPlayerSynchronizer;
44
-
45
- // Position update timer
46
- private _updateTimer: any;
47
- private _lastUpdateTime: number = 0;
48
-
49
- /**
50
- * The objects fluid type name.
51
- */
52
- public static readonly TypeName = `@microsoft/fluid-media:SharedMediaSession`;
53
-
54
- /**
55
- * The objects fluid type factory.
56
- */
57
- public static readonly factory = new DataObjectFactory(
58
- LiveMediaSession.TypeName,
59
- LiveMediaSession,
60
- [],
61
- {}
62
- );
63
-
64
- /**
65
- * Extension point that lets applications replace the default logic for throttling the sessions
66
- * local sync behavior.
67
- *
68
- * @remarks
69
- * The `LiveMediaCoordinator` is fairly aggressive at wanting to keep the local media player
70
- * in sync with the rest of the group. This aggressiveness can result in the coordinator sending
71
- * new sync actions before the local player has finished responding to the previous sync action.
72
- * The `ActionThrottler` gives apps fine grain control over how aggressive they want sync to be.
73
- *
74
- * By default, a `RepeatedAction` throttler is used which prevents the same sync action from
75
- * being sent within an adjustable time period.
76
- */
77
- public get actionThrottler(): MediaSessionActionThrottler {
78
- return this._actionThrottler;
79
- }
80
-
81
- public set actionThrottler(value: MediaSessionActionThrottler) {
82
- this._actionThrottler = value;
83
- }
84
-
85
- /**
86
- * The group coordinator for the session.
87
- */
88
- public get coordinator(): LiveMediaSessionCoordinator {
89
- return this._coordinator!;
90
- }
91
-
92
- /**
93
- * Returns the logger used by the session and coordinator.
94
- *
95
- * @remarks
96
- * This is used by the `MediaPlayerSynchronizer` to log events.
97
- */
98
- public get logger(): LiveTelemetryLogger {
99
- return this._logger!;
100
- }
101
-
102
- /**
103
- * Initialize the object to begin sending/receiving group playback updates through this DDS.
104
- *
105
- * @param allowedRoles Optional. List of roles allowed to group transport operations like play/pause/seek/setTrack.
106
- *
107
- * @returns a void promise that resolves once complete.
108
- *
109
- * @throws error when `.initialize()` has already been called for this class instance.
110
- */
111
- public async initialize(allowedRoles?: UserMeetingRole[]): Promise<void> {
112
- if (this.initializeState !== LiveDataObjectInitializeState.needed) {
113
- throw new Error(`LiveMediaSession already started.`);
114
- }
115
- // Update initialize state as pending
116
- this.initializeState = LiveDataObjectInitializeState.pending;
117
- if (allowedRoles) {
118
- this._allowedRoles = allowedRoles;
119
- }
120
- try {
121
- await this.coordinator.initialize(allowedRoles);
122
- } catch (error: unknown) {
123
- this.initializeState = LiveDataObjectInitializeState.needed;
124
- throw error;
125
- }
126
-
127
- // Update initialize state as succeeded
128
- this.initializeState = LiveDataObjectInitializeState.succeeded;
129
- }
130
-
131
- /**
132
- * Registers an action handler with the session.
133
- * @param action Name of the action to register a handler for.
134
- * @param handler Function called when the action is triggered.
135
- */
136
- public setActionHandler(
137
- action: ExtendedMediaSessionAction,
138
- handler:
139
- | ExtendedMediaSessionActionHandler
140
- | MediaSessionActionHandler
141
- | null
142
- ): void {
143
- if (handler) {
144
- // add handler
145
- this._actionHandlers.set(action, handler);
146
- } else if (this._actionHandlers.has(action)) {
147
- // remove handler
148
- this._actionHandlers.delete(action);
149
- }
150
- }
151
-
152
- /**
153
- * Registers a handler that will be queried anytime the group coordinate needs to know the
154
- * local players transport state and position.
155
- */
156
- public setRequestPlayerStateHandler(handler: () => IMediaPlayerState) {
157
- this._requestPlayerStateHandler = handler;
158
- }
159
-
160
- /**
161
- * Begins synchronizing the playback of a media element.
162
- * @param player Something that "looks like" and HTML Media Element.
163
- * @returns A new synchronizer instance. Call `synchronizer.end()` to stop synchronizing the elements playback.
164
- */
165
- public synchronize(player: IMediaPlayer): MediaPlayerSynchronizer {
166
- // A session can only synchronize one player at a time
167
- if (this._synchronizing) {
168
- this._synchronizing.end();
169
- }
170
-
171
- // Start position update timer
172
- if (!this._updateTimer) {
173
- this.logger.sendTelemetryEvent(
174
- TelemetryEvents.MediaSession.BeginPositionUpdateTimer
175
- );
176
- this._updateTimer = setInterval(() => {
177
- // Get current state
178
- const state = this.getCurrentPlayerState();
179
-
180
- // Check for hit wait point
181
- this.checkWaitPointHit(state);
182
-
183
- // Send position update if interval hit
184
- const now = new Date().getTime();
185
- const delta = (now - this._lastUpdateTime) / 1000;
186
- if (
187
- this.coordinator.isInitialized &&
188
- delta >= this.coordinator.positionUpdateInterval
189
- ) {
190
- this._lastUpdateTime = now;
191
- this.coordinator.sendPositionUpdate(state);
192
- }
193
- }, 500);
194
- }
195
-
196
- return new MediaPlayerSynchronizer(player, this, this.runtime, () => {
197
- this._synchronizing = undefined;
198
- });
199
- }
200
-
201
- protected async hasInitialized(): Promise<void> {
202
- this._logger = new LiveTelemetryLogger(this.runtime, this.liveRuntime);
203
-
204
- // Create coordinator and listen for triggered actions
205
- this._coordinator = new LiveMediaSessionCoordinator(
206
- this.runtime,
207
- this.liveRuntime,
208
- () => this.getCurrentPlayerState()
209
- );
210
- this._coordinator.on(
211
- MediaSessionCoordinatorEvents.triggeraction,
212
- (event: ITriggerActionEvent) => {
213
- // Pre-process actions
214
- const details = event.details;
215
- const seekTime =
216
- typeof details.seekTime == "number" ? details.seekTime : -1;
217
- switch (details.action) {
218
- case "play":
219
- this.logger.sendTelemetryEvent(
220
- TelemetryEvents.MediaSession.PlayAction,
221
- null,
222
- {
223
- position: seekTime,
224
- }
225
- );
226
- break;
227
- case "pause":
228
- this.logger.sendTelemetryEvent(
229
- TelemetryEvents.MediaSession.PauseAction,
230
- null,
231
- {
232
- position: seekTime,
233
- }
234
- );
235
- break;
236
- case "seekto":
237
- this.logger.sendTelemetryEvent(
238
- TelemetryEvents.MediaSession.SeekToAction,
239
- null,
240
- {
241
- position: seekTime,
242
- }
243
- );
244
- break;
245
- case "settrack":
246
- this.logger.sendTelemetryEvent(
247
- TelemetryEvents.MediaSession.SetTrackAction
248
- );
249
- break;
250
- case "datachange":
251
- this.logger.sendTelemetryEvent(
252
- TelemetryEvents.MediaSession.DataChangeAction
253
- );
254
- break;
255
- case "catchup":
256
- // Default to seekto if catchup not implemented
257
- this.logger.sendTelemetryEvent(
258
- TelemetryEvents.MediaSession.CatchUpAction,
259
- null,
260
- {
261
- position: seekTime,
262
- }
263
- );
264
- if (!this._actionHandlers.has("catchup")) {
265
- details.action = "seekto";
266
- }
267
- break;
268
- }
269
-
270
- // Dispatch action
271
- this.dispatchAction(details);
272
- }
273
- );
274
- }
275
-
276
- private checkWaitPointHit(state: IMediaPlayerState): void {
277
- // Was a wait point hit?
278
- if (state.positionState && this.coordinator.isInitialized) {
279
- const waitPoint = this.coordinator.findNextWaitPoint();
280
- if (
281
- waitPoint &&
282
- state.positionState.position != undefined &&
283
- state.positionState.position >= waitPoint.position
284
- ) {
285
- // Ensure handler registered
286
- if (!this._actionHandlers.has("wait")) {
287
- throw new Error(
288
- `SharedMediaSession: wait point hit but no 'wait' action registered.`
289
- );
290
- }
291
-
292
- // Begin suspension for wait point
293
- this.waitUntilConnected().then((clientId: string) => {
294
- const suspension =
295
- this.coordinator.beginSuspension(waitPoint);
296
- this.dispatchAction({
297
- action: "wait",
298
- source: "system",
299
- clientId,
300
- local: true,
301
- suspension,
302
- });
303
- });
304
- }
305
- }
306
- }
307
-
308
- private getCurrentPlayerState(): IMediaPlayerState {
309
- if (!this._requestPlayerStateHandler) {
310
- throw new Error(
311
- `LiveMediaSession: no getPlayerState callback configured.`
312
- );
313
- }
314
-
315
- return this._requestPlayerStateHandler();
316
- }
317
-
318
- private dispatchAction(
319
- details: MediaSessionActionDetails | ExtendedMediaSessionActionDetails
320
- ): void {
321
- if (this._actionHandlers.has(details.action)) {
322
- const handler = this._actionHandlers.get(details.action);
323
- this._actionThrottler.throttled(details, handler);
324
- }
325
- }
326
- }
327
-
328
- /**
329
- * Register `LiveMediaSession` as an available `LoadableObjectClass` for use in packages that support dynamic object loading, such as `@microsoft/live-share-turbo`.
330
- */
331
- DynamicObjectRegistry.registerObjectClass(
332
- LiveMediaSession,
333
- LiveMediaSession.TypeName
334
- );
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import { DataObjectFactory } from "@fluidframework/aqueduct";
7
+ import {
8
+ DynamicObjectRegistry,
9
+ LiveDataObject,
10
+ LiveDataObjectInitializeNotNeededError,
11
+ LiveDataObjectInitializeState,
12
+ LiveTelemetryLogger,
13
+ UserMeetingRole,
14
+ } from "@microsoft/live-share";
15
+ import { MediaPlayerSynchronizer } from "./MediaPlayerSynchronizer";
16
+ import { ITriggerActionEvent, TelemetryEvents } from "./internals";
17
+ import {
18
+ MediaSessionCoordinatorEvents,
19
+ ExtendedMediaSessionAction,
20
+ ExtendedMediaSessionActionDetails,
21
+ ExtendedMediaSessionActionHandler,
22
+ } from "./MediaSessionExtensions";
23
+ import {
24
+ LiveMediaSessionCoordinator,
25
+ IMediaPlayerState,
26
+ } from "./LiveMediaSessionCoordinator";
27
+ import { MediaSessionActionThrottler } from "./MediaSessionActionThrottler";
28
+ import { RepeatedActionThrottler } from "./RepeatedActionThrottler";
29
+ import { IMediaPlayer } from "./IMediaPlayer";
30
+
31
+ /**
32
+ * Live fluid object that synchronizes media playback across multiple clients.
33
+ */
34
+ export class LiveMediaSession extends LiveDataObject {
35
+ private _actionThrottler: MediaSessionActionThrottler =
36
+ new RepeatedActionThrottler();
37
+ private _logger?: LiveTelemetryLogger;
38
+ private _requestPlayerStateHandler?: () => IMediaPlayerState;
39
+ private _coordinator?: LiveMediaSessionCoordinator;
40
+ private readonly _actionHandlers: Map<
41
+ string,
42
+ MediaSessionActionHandler | ExtendedMediaSessionActionHandler
43
+ > = new Map();
44
+ private _synchronizing?: MediaPlayerSynchronizer;
45
+
46
+ // Position update timer
47
+ private _updateTimer: any;
48
+ private _lastUpdateTime: number = 0;
49
+
50
+ /**
51
+ * The objects fluid type name.
52
+ */
53
+ public static readonly TypeName = `@microsoft/fluid-media:SharedMediaSession`;
54
+
55
+ /**
56
+ * The objects fluid type factory.
57
+ */
58
+ public static readonly factory = new DataObjectFactory(
59
+ LiveMediaSession.TypeName,
60
+ LiveMediaSession,
61
+ [],
62
+ {}
63
+ );
64
+
65
+ /**
66
+ * Extension point that lets applications replace the default logic for throttling the sessions
67
+ * local sync behavior.
68
+ *
69
+ * @remarks
70
+ * The `LiveMediaCoordinator` is fairly aggressive at wanting to keep the local media player
71
+ * in sync with the rest of the group. This aggressiveness can result in the coordinator sending
72
+ * new sync actions before the local player has finished responding to the previous sync action.
73
+ * The `ActionThrottler` gives apps fine grain control over how aggressive they want sync to be.
74
+ *
75
+ * By default, a `RepeatedAction` throttler is used which prevents the same sync action from
76
+ * being sent within an adjustable time period.
77
+ */
78
+ public get actionThrottler(): MediaSessionActionThrottler {
79
+ return this._actionThrottler;
80
+ }
81
+
82
+ public set actionThrottler(value: MediaSessionActionThrottler) {
83
+ this._actionThrottler = value;
84
+ }
85
+
86
+ /**
87
+ * The group coordinator for the session.
88
+ */
89
+ public get coordinator(): LiveMediaSessionCoordinator {
90
+ return this._coordinator!;
91
+ }
92
+
93
+ /**
94
+ * Returns the logger used by the session and coordinator.
95
+ *
96
+ * @remarks
97
+ * This is used by the `MediaPlayerSynchronizer` to log events.
98
+ */
99
+ public get logger(): LiveTelemetryLogger {
100
+ return this._logger!;
101
+ }
102
+
103
+ /**
104
+ * Initialize the object to begin sending/receiving group playback updates through this DDS.
105
+ *
106
+ * @param allowedRoles Optional. List of roles allowed to group transport operations like play/pause/seek/setTrack.
107
+ *
108
+ * @returns a void promise that resolves once complete.
109
+ *
110
+ * @throws error when `.initialize()` has already been called for this class instance.
111
+ */
112
+ public async initialize(allowedRoles?: UserMeetingRole[]): Promise<void> {
113
+ LiveDataObjectInitializeNotNeededError.assert(
114
+ "LiveMediaSession:initialize",
115
+ this.initializeState
116
+ );
117
+ // Update initialize state as pending
118
+ this.initializeState = LiveDataObjectInitializeState.pending;
119
+ if (allowedRoles) {
120
+ this._allowedRoles = allowedRoles;
121
+ }
122
+ try {
123
+ await this.coordinator.initialize(allowedRoles);
124
+ } catch (error: unknown) {
125
+ this.initializeState = LiveDataObjectInitializeState.needed;
126
+ throw error;
127
+ }
128
+
129
+ // Update initialize state as succeeded
130
+ this.initializeState = LiveDataObjectInitializeState.succeeded;
131
+ }
132
+
133
+ /**
134
+ * Registers an action handler with the session.
135
+ * @param action Name of the action to register a handler for.
136
+ * @param handler Function called when the action is triggered.
137
+ */
138
+ public setActionHandler(
139
+ action: ExtendedMediaSessionAction,
140
+ handler:
141
+ | ExtendedMediaSessionActionHandler
142
+ | MediaSessionActionHandler
143
+ | null
144
+ ): void {
145
+ if (handler) {
146
+ // add handler
147
+ this._actionHandlers.set(action, handler);
148
+ } else if (this._actionHandlers.has(action)) {
149
+ // remove handler
150
+ this._actionHandlers.delete(action);
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Registers a handler that will be queried anytime the group coordinate needs to know the
156
+ * local players transport state and position.
157
+ */
158
+ public setRequestPlayerStateHandler(handler: () => IMediaPlayerState) {
159
+ this._requestPlayerStateHandler = handler;
160
+ }
161
+
162
+ /**
163
+ * Begins synchronizing the playback of a media element.
164
+ * @param player Something that "looks like" and HTML Media Element.
165
+ * @returns A new synchronizer instance. Call `synchronizer.end()` to stop synchronizing the elements playback.
166
+ */
167
+ public synchronize(player: IMediaPlayer): MediaPlayerSynchronizer {
168
+ // A session can only synchronize one player at a time
169
+ if (this._synchronizing) {
170
+ this._synchronizing.end();
171
+ }
172
+
173
+ // Start position update timer
174
+ if (!this._updateTimer) {
175
+ this.logger.sendTelemetryEvent(
176
+ TelemetryEvents.MediaSession.BeginPositionUpdateTimer
177
+ );
178
+ this._updateTimer = setInterval(() => {
179
+ // Get current state
180
+ const state = this.getCurrentPlayerState();
181
+
182
+ // Check for hit wait point
183
+ this.checkWaitPointHit(state);
184
+
185
+ // Send position update if interval hit
186
+ const now = new Date().getTime();
187
+ const delta = (now - this._lastUpdateTime) / 1000;
188
+ if (
189
+ this.coordinator.isInitialized &&
190
+ delta >= this.coordinator.positionUpdateInterval
191
+ ) {
192
+ this._lastUpdateTime = now;
193
+ this.coordinator.sendPositionUpdate(state);
194
+ }
195
+ }, 500);
196
+ }
197
+
198
+ return new MediaPlayerSynchronizer(player, this, this.runtime, () => {
199
+ this._synchronizing = undefined;
200
+ });
201
+ }
202
+
203
+ protected async hasInitialized(): Promise<void> {
204
+ this._logger = new LiveTelemetryLogger(this.runtime, this.liveRuntime);
205
+
206
+ // Create coordinator and listen for triggered actions
207
+ this._coordinator = new LiveMediaSessionCoordinator(
208
+ this.id,
209
+ this.runtime,
210
+ this.liveRuntime,
211
+ () => this.getCurrentPlayerState()
212
+ );
213
+ this._coordinator.on(
214
+ MediaSessionCoordinatorEvents.triggeraction,
215
+ (event: ITriggerActionEvent) => {
216
+ // Pre-process actions
217
+ const details = event.details;
218
+ const seekTime =
219
+ typeof details.seekTime == "number" ? details.seekTime : -1;
220
+ switch (details.action) {
221
+ case "play":
222
+ this.logger.sendTelemetryEvent(
223
+ TelemetryEvents.MediaSession.PlayAction,
224
+ null,
225
+ {
226
+ position: seekTime,
227
+ }
228
+ );
229
+ break;
230
+ case "pause":
231
+ this.logger.sendTelemetryEvent(
232
+ TelemetryEvents.MediaSession.PauseAction,
233
+ null,
234
+ {
235
+ position: seekTime,
236
+ }
237
+ );
238
+ break;
239
+ case "seekto":
240
+ this.logger.sendTelemetryEvent(
241
+ TelemetryEvents.MediaSession.SeekToAction,
242
+ null,
243
+ {
244
+ position: seekTime,
245
+ }
246
+ );
247
+ break;
248
+ case "settrack":
249
+ this.logger.sendTelemetryEvent(
250
+ TelemetryEvents.MediaSession.SetTrackAction
251
+ );
252
+ break;
253
+ case "datachange":
254
+ this.logger.sendTelemetryEvent(
255
+ TelemetryEvents.MediaSession.DataChangeAction
256
+ );
257
+ break;
258
+ case "catchup":
259
+ // Default to seekto if catchup not implemented
260
+ this.logger.sendTelemetryEvent(
261
+ TelemetryEvents.MediaSession.CatchUpAction,
262
+ null,
263
+ {
264
+ position: seekTime,
265
+ }
266
+ );
267
+ if (!this._actionHandlers.has("catchup")) {
268
+ details.action = "seekto";
269
+ }
270
+ break;
271
+ }
272
+
273
+ // Dispatch action
274
+ this.dispatchAction(details);
275
+ }
276
+ );
277
+ }
278
+
279
+ private checkWaitPointHit(state: IMediaPlayerState): void {
280
+ // Was a wait point hit?
281
+ if (state.positionState && this.coordinator.isInitialized) {
282
+ const waitPoint = this.coordinator.findNextWaitPoint();
283
+ if (
284
+ waitPoint &&
285
+ state.positionState.position != undefined &&
286
+ state.positionState.position >= waitPoint.position
287
+ ) {
288
+ // Ensure handler registered
289
+ if (!this._actionHandlers.has("wait")) {
290
+ throw new Error(
291
+ `LiveMediaSession:checkWaitPointHit - wait point hit but no 'wait' action registered.\nTo fix this error, set a \`wait\` action using the \`setActionHandler()\` function.`
292
+ );
293
+ }
294
+
295
+ // Begin suspension for wait point
296
+ this.waitUntilConnected().then((clientId: string) => {
297
+ const suspension =
298
+ this.coordinator.beginSuspension(waitPoint);
299
+ this.dispatchAction({
300
+ action: "wait",
301
+ source: "system",
302
+ clientId,
303
+ local: true,
304
+ suspension,
305
+ });
306
+ });
307
+ }
308
+ }
309
+ }
310
+
311
+ private getCurrentPlayerState(): IMediaPlayerState {
312
+ if (!this._requestPlayerStateHandler) {
313
+ throw new Error(
314
+ "LiveMediaSession:getCurrentPlayerState - no `requestPlayerStateHandler` callback configured.\nTo fix this error, ensure `setRequestPlayerStateHandler()` has been set with a delegate to retrieve your player's state, or use the `synchronize()` function to create a new `MediaPlayerSynchronizer` instance tied to your `IMediaPlayer` instance."
315
+ );
316
+ }
317
+
318
+ return this._requestPlayerStateHandler();
319
+ }
320
+
321
+ private dispatchAction(
322
+ details: MediaSessionActionDetails | ExtendedMediaSessionActionDetails
323
+ ): void {
324
+ if (this._actionHandlers.has(details.action)) {
325
+ const handler = this._actionHandlers.get(details.action);
326
+ this._actionThrottler.throttled(details, handler);
327
+ }
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Register `LiveMediaSession` as an available `LoadableObjectClass` for use in packages that support dynamic object loading, such as `@microsoft/live-share-turbo`.
333
+ */
334
+ DynamicObjectRegistry.registerObjectClass(
335
+ LiveMediaSession,
336
+ LiveMediaSession.TypeName
337
+ );