@microsoft/live-share-media 1.3.0 → 1.4.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 (78) 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 +9 -1
  6. package/bin/LiveMediaSession.js.map +1 -1
  7. package/bin/LiveMediaSessionCoordinator.d.ts +18 -2
  8. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -1
  9. package/bin/LiveMediaSessionCoordinator.js +52 -13
  10. package/bin/LiveMediaSessionCoordinator.js.map +1 -1
  11. package/bin/MediaPlayerSynchronizer.d.ts +6 -4
  12. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  13. package/bin/MediaPlayerSynchronizer.js +41 -12
  14. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  15. package/bin/MediaSessionExtensions.d.ts +17 -8
  16. package/bin/MediaSessionExtensions.d.ts.map +1 -1
  17. package/bin/MediaSessionExtensions.js +4 -0
  18. package/bin/MediaSessionExtensions.js.map +1 -1
  19. package/bin/internals/GroupCoordinatorState.d.ts +15 -1
  20. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  21. package/bin/internals/GroupCoordinatorState.js +123 -57
  22. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  23. package/bin/internals/GroupPlaybackPosition.d.ts +3 -1
  24. package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
  25. package/bin/internals/GroupPlaybackPosition.js +7 -3
  26. package/bin/internals/GroupPlaybackPosition.js.map +1 -1
  27. package/bin/internals/GroupPlaybackRate.d.ts +39 -0
  28. package/bin/internals/GroupPlaybackRate.d.ts.map +1 -0
  29. package/bin/internals/GroupPlaybackRate.js +62 -0
  30. package/bin/internals/GroupPlaybackRate.js.map +1 -0
  31. package/bin/internals/GroupPlaybackTrack.d.ts +2 -2
  32. package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
  33. package/bin/internals/GroupPlaybackTrack.js +3 -2
  34. package/bin/internals/GroupPlaybackTrack.js.map +1 -1
  35. package/bin/internals/GroupPlaybackTrackData.d.ts +3 -3
  36. package/bin/internals/GroupPlaybackTrackData.d.ts.map +1 -1
  37. package/bin/internals/GroupPlaybackTrackData.js +12 -11
  38. package/bin/internals/GroupPlaybackTrackData.js.map +1 -1
  39. package/bin/internals/GroupTransportState.d.ts +6 -3
  40. package/bin/internals/GroupTransportState.d.ts.map +1 -1
  41. package/bin/internals/GroupTransportState.js +16 -11
  42. package/bin/internals/GroupTransportState.js.map +1 -1
  43. package/bin/internals/consts.d.ts +10 -3
  44. package/bin/internals/consts.d.ts.map +1 -1
  45. package/bin/internals/consts.js +10 -3
  46. package/bin/internals/consts.js.map +1 -1
  47. package/bin/internals/index.d.ts +1 -0
  48. package/bin/internals/index.d.ts.map +1 -1
  49. package/bin/internals/index.js +1 -0
  50. package/bin/internals/index.js.map +1 -1
  51. package/bin/internals/interfaces.d.ts +10 -0
  52. package/bin/internals/interfaces.d.ts.map +1 -0
  53. package/bin/internals/interfaces.js +3 -0
  54. package/bin/internals/interfaces.js.map +1 -0
  55. package/package.json +45 -45
  56. package/src/IMediaPlayer.ts +29 -29
  57. package/src/LiveMediaSession.ts +349 -336
  58. package/src/LiveMediaSessionCoordinator.ts +760 -678
  59. package/src/LiveMediaSessionCoordinatorSuspension.ts +39 -39
  60. package/src/MediaPlayerSynchronizer.ts +754 -714
  61. package/src/MediaSessionActionThrottler.ts +19 -19
  62. package/src/MediaSessionExtensions.ts +198 -187
  63. package/src/RepeatedActionThrottler.ts +76 -76
  64. package/src/VolumeManager.ts +200 -200
  65. package/src/index.ts +14 -14
  66. package/src/internals/GroupCoordinatorState.ts +802 -685
  67. package/src/internals/GroupPlaybackPosition.ts +235 -228
  68. package/src/internals/GroupPlaybackRate.ts +90 -0
  69. package/src/internals/GroupPlaybackTrack.ts +183 -182
  70. package/src/internals/GroupPlaybackTrackData.ts +105 -104
  71. package/src/internals/GroupTransportState.ts +179 -170
  72. package/src/internals/consts.ts +115 -102
  73. package/src/internals/errors.ts +42 -42
  74. package/src/internals/index.ts +13 -12
  75. package/src/internals/interfaces.ts +10 -0
  76. package/tsconfig.build.json +8 -8
  77. package/tsconfig.json +76 -76
  78. package/typedoc.json +10 -10
@@ -1,336 +1,349 @@
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.runtime,
209
- this.liveRuntime,
210
- () => this.getCurrentPlayerState()
211
- );
212
- this._coordinator.on(
213
- MediaSessionCoordinatorEvents.triggeraction,
214
- (event: ITriggerActionEvent) => {
215
- // Pre-process actions
216
- const details = event.details;
217
- const seekTime =
218
- typeof details.seekTime == "number" ? details.seekTime : -1;
219
- switch (details.action) {
220
- case "play":
221
- this.logger.sendTelemetryEvent(
222
- TelemetryEvents.MediaSession.PlayAction,
223
- null,
224
- {
225
- position: seekTime,
226
- }
227
- );
228
- break;
229
- case "pause":
230
- this.logger.sendTelemetryEvent(
231
- TelemetryEvents.MediaSession.PauseAction,
232
- null,
233
- {
234
- position: seekTime,
235
- }
236
- );
237
- break;
238
- case "seekto":
239
- this.logger.sendTelemetryEvent(
240
- TelemetryEvents.MediaSession.SeekToAction,
241
- null,
242
- {
243
- position: seekTime,
244
- }
245
- );
246
- break;
247
- case "settrack":
248
- this.logger.sendTelemetryEvent(
249
- TelemetryEvents.MediaSession.SetTrackAction
250
- );
251
- break;
252
- case "datachange":
253
- this.logger.sendTelemetryEvent(
254
- TelemetryEvents.MediaSession.DataChangeAction
255
- );
256
- break;
257
- case "catchup":
258
- // Default to seekto if catchup not implemented
259
- this.logger.sendTelemetryEvent(
260
- TelemetryEvents.MediaSession.CatchUpAction,
261
- null,
262
- {
263
- position: seekTime,
264
- }
265
- );
266
- if (!this._actionHandlers.has("catchup")) {
267
- details.action = "seekto";
268
- }
269
- break;
270
- }
271
-
272
- // Dispatch action
273
- this.dispatchAction(details);
274
- }
275
- );
276
- }
277
-
278
- private checkWaitPointHit(state: IMediaPlayerState): void {
279
- // Was a wait point hit?
280
- if (state.positionState && this.coordinator.isInitialized) {
281
- const waitPoint = this.coordinator.findNextWaitPoint();
282
- if (
283
- waitPoint &&
284
- state.positionState.position != undefined &&
285
- state.positionState.position >= waitPoint.position
286
- ) {
287
- // Ensure handler registered
288
- if (!this._actionHandlers.has("wait")) {
289
- throw new Error(
290
- `LiveMediaSession:checkWaitPointHit - wait point hit but no 'wait' action registered.\nTo fix this error, set a \`wait\` action using the \`setActionHandler()\` function.`
291
- );
292
- }
293
-
294
- // Begin suspension for wait point
295
- this.waitUntilConnected().then((clientId: string) => {
296
- const suspension =
297
- this.coordinator.beginSuspension(waitPoint);
298
- this.dispatchAction({
299
- action: "wait",
300
- source: "system",
301
- clientId,
302
- local: true,
303
- suspension,
304
- });
305
- });
306
- }
307
- }
308
- }
309
-
310
- private getCurrentPlayerState(): IMediaPlayerState {
311
- if (!this._requestPlayerStateHandler) {
312
- throw new Error(
313
- "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."
314
- );
315
- }
316
-
317
- return this._requestPlayerStateHandler();
318
- }
319
-
320
- private dispatchAction(
321
- details: MediaSessionActionDetails | ExtendedMediaSessionActionDetails
322
- ): void {
323
- if (this._actionHandlers.has(details.action)) {
324
- const handler = this._actionHandlers.get(details.action);
325
- this._actionThrottler.throttled(details, handler);
326
- }
327
- }
328
- }
329
-
330
- /**
331
- * Register `LiveMediaSession` as an available `LoadableObjectClass` for use in packages that support dynamic object loading, such as `@microsoft/live-share-turbo`.
332
- */
333
- DynamicObjectRegistry.registerObjectClass(
334
- LiveMediaSession,
335
- LiveMediaSession.TypeName
336
- );
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 "ratechange":
240
+ if (!details.playbackRate) {
241
+ break;
242
+ }
243
+ this.logger.sendTelemetryEvent(
244
+ TelemetryEvents.MediaSession.RateChangeAction,
245
+ null,
246
+ {
247
+ playbackRate: details.playbackRate,
248
+ }
249
+ );
250
+ break;
251
+ case "seekto":
252
+ this.logger.sendTelemetryEvent(
253
+ TelemetryEvents.MediaSession.SeekToAction,
254
+ null,
255
+ {
256
+ position: seekTime,
257
+ }
258
+ );
259
+ break;
260
+ case "settrack":
261
+ this.logger.sendTelemetryEvent(
262
+ TelemetryEvents.MediaSession.SetTrackAction
263
+ );
264
+ break;
265
+ case "datachange":
266
+ this.logger.sendTelemetryEvent(
267
+ TelemetryEvents.MediaSession.DataChangeAction
268
+ );
269
+ break;
270
+ case "catchup":
271
+ // Default to seekto if catchup not implemented
272
+ this.logger.sendTelemetryEvent(
273
+ TelemetryEvents.MediaSession.CatchUpAction,
274
+ null,
275
+ {
276
+ position: seekTime,
277
+ }
278
+ );
279
+ if (!this._actionHandlers.has("catchup")) {
280
+ details.action = "seekto";
281
+ }
282
+ break;
283
+ }
284
+
285
+ // Dispatch action
286
+ this.dispatchAction(details);
287
+ }
288
+ );
289
+ }
290
+
291
+ private checkWaitPointHit(state: IMediaPlayerState): void {
292
+ // Was a wait point hit?
293
+ if (state.positionState && this.coordinator.isInitialized) {
294
+ const waitPoint = this.coordinator.findNextWaitPoint();
295
+ if (
296
+ waitPoint &&
297
+ state.positionState.position != undefined &&
298
+ state.positionState.position >= waitPoint.position
299
+ ) {
300
+ // Ensure handler registered
301
+ if (!this._actionHandlers.has("wait")) {
302
+ throw new Error(
303
+ `LiveMediaSession:checkWaitPointHit - wait point hit but no 'wait' action registered.\nTo fix this error, set a \`wait\` action using the \`setActionHandler()\` function.`
304
+ );
305
+ }
306
+
307
+ // Begin suspension for wait point
308
+ this.waitUntilConnected().then((clientId: string) => {
309
+ const suspension =
310
+ this.coordinator.beginSuspension(waitPoint);
311
+ this.dispatchAction({
312
+ action: "wait",
313
+ source: "system",
314
+ clientId,
315
+ local: true,
316
+ suspension,
317
+ });
318
+ });
319
+ }
320
+ }
321
+ }
322
+
323
+ private getCurrentPlayerState(): IMediaPlayerState {
324
+ if (!this._requestPlayerStateHandler) {
325
+ throw new Error(
326
+ "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."
327
+ );
328
+ }
329
+
330
+ return this._requestPlayerStateHandler();
331
+ }
332
+
333
+ private dispatchAction(
334
+ details: MediaSessionActionDetails | ExtendedMediaSessionActionDetails
335
+ ): void {
336
+ if (this._actionHandlers.has(details.action)) {
337
+ const handler = this._actionHandlers.get(details.action);
338
+ this._actionThrottler.throttled(details, handler);
339
+ }
340
+ }
341
+ }
342
+
343
+ /**
344
+ * Register `LiveMediaSession` as an available `LoadableObjectClass` for use in packages that support dynamic object loading, such as `@microsoft/live-share-turbo`.
345
+ */
346
+ DynamicObjectRegistry.registerObjectClass(
347
+ LiveMediaSession,
348
+ LiveMediaSession.TypeName
349
+ );