@microsoft/live-share-media 0.5.1 → 1.0.0-preview.10

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 (88) hide show
  1. package/.nycrc.json +10 -10
  2. package/LICENSE +86 -86
  3. package/README.md +141 -137
  4. package/bin/IMediaPlayer.d.ts.map +1 -1
  5. package/bin/LiveMediaSession.d.ts +95 -0
  6. package/bin/LiveMediaSession.d.ts.map +1 -0
  7. package/bin/LiveMediaSession.js +232 -0
  8. package/bin/LiveMediaSession.js.map +1 -0
  9. package/bin/LiveMediaSessionCoordinator.d.ts +228 -0
  10. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -0
  11. package/bin/LiveMediaSessionCoordinator.js +408 -0
  12. package/bin/LiveMediaSessionCoordinator.js.map +1 -0
  13. package/bin/LiveMediaSessionCoordinatorSuspension.d.ts +20 -0
  14. package/bin/LiveMediaSessionCoordinatorSuspension.d.ts.map +1 -0
  15. package/bin/LiveMediaSessionCoordinatorSuspension.js +28 -0
  16. package/bin/LiveMediaSessionCoordinatorSuspension.js.map +1 -0
  17. package/bin/MediaPlayerSynchronizer.d.ts +11 -11
  18. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  19. package/bin/MediaPlayerSynchronizer.js +136 -76
  20. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  21. package/bin/MediaSessionActionThrottler.d.ts +1 -1
  22. package/bin/MediaSessionActionThrottler.d.ts.map +1 -1
  23. package/bin/MediaSessionActionThrottler.js.map +1 -1
  24. package/bin/MediaSessionExtensions.d.ts.map +1 -1
  25. package/bin/RepeatedActionThrottler.d.ts +2 -2
  26. package/bin/RepeatedActionThrottler.d.ts.map +1 -1
  27. package/bin/RepeatedActionThrottler.js +9 -3
  28. package/bin/RepeatedActionThrottler.js.map +1 -1
  29. package/bin/VolumeManager.d.ts +84 -0
  30. package/bin/VolumeManager.d.ts.map +1 -0
  31. package/bin/VolumeManager.js +175 -0
  32. package/bin/VolumeManager.js.map +1 -0
  33. package/bin/index.d.ts +9 -9
  34. package/bin/index.d.ts.map +1 -1
  35. package/bin/index.js +4 -4
  36. package/bin/index.js.map +1 -1
  37. package/bin/internals/GroupCoordinatorState.d.ts +12 -12
  38. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  39. package/bin/internals/GroupCoordinatorState.js +94 -56
  40. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  41. package/bin/internals/GroupPlaybackPosition.d.ts +3 -3
  42. package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
  43. package/bin/internals/GroupPlaybackPosition.js +24 -15
  44. package/bin/internals/GroupPlaybackPosition.js.map +1 -1
  45. package/bin/internals/GroupPlaybackTrack.d.ts +4 -4
  46. package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
  47. package/bin/internals/GroupPlaybackTrack.js +14 -4
  48. package/bin/internals/GroupPlaybackTrack.js.map +1 -1
  49. package/bin/internals/GroupPlaybackTrackData.d.ts +3 -3
  50. package/bin/internals/GroupPlaybackTrackData.d.ts.map +1 -1
  51. package/bin/internals/GroupPlaybackTrackData.js +8 -4
  52. package/bin/internals/GroupPlaybackTrackData.js.map +1 -1
  53. package/bin/internals/GroupTransportState.d.ts +5 -5
  54. package/bin/internals/GroupTransportState.d.ts.map +1 -1
  55. package/bin/internals/GroupTransportState.js +28 -13
  56. package/bin/internals/GroupTransportState.js.map +1 -1
  57. package/bin/internals/consts.d.ts.map +1 -1
  58. package/bin/internals/consts.js +54 -54
  59. package/bin/internals/consts.js.map +1 -1
  60. package/bin/internals/index.d.ts +6 -6
  61. package/package.json +38 -38
  62. package/src/IMediaPlayer.ts +29 -21
  63. package/src/LiveMediaSession.ts +305 -0
  64. package/src/LiveMediaSessionCoordinator.ts +621 -0
  65. package/src/{EphemeralMediaSessionCoordinatorSuspension.ts → LiveMediaSessionCoordinatorSuspension.ts} +39 -31
  66. package/src/MediaPlayerSynchronizer.ts +636 -450
  67. package/src/MediaSessionActionThrottler.ts +16 -14
  68. package/src/MediaSessionExtensions.ts +52 -42
  69. package/src/RepeatedActionThrottler.ts +74 -56
  70. package/src/VolumeManager.ts +197 -0
  71. package/src/index.ts +14 -15
  72. package/src/internals/GroupCoordinatorState.ts +606 -446
  73. package/src/internals/GroupPlaybackPosition.ts +227 -193
  74. package/src/internals/GroupPlaybackTrack.ts +176 -147
  75. package/src/internals/GroupPlaybackTrackData.ts +98 -90
  76. package/src/internals/GroupTransportState.ts +157 -127
  77. package/src/internals/consts.ts +98 -80
  78. package/src/internals/index.ts +11 -11
  79. package/src/test/GroupPlaybackPosition.spec.ts +600 -340
  80. package/src/test/GroupPlaybackTrack.spec.ts +122 -66
  81. package/src/test/SharedMediaSession.spec.ts +149 -113
  82. package/src/test/SharedMediaSessionCoordinator.spec.ts +271 -231
  83. package/src/test/VolumeManager.spec.ts +219 -0
  84. package/tsconfig.build.json +8 -8
  85. package/tsconfig.json +76 -75
  86. package/src/EphemeralMediaSession.ts +0 -248
  87. package/src/EphemeralMediaSessionCoordinator.ts +0 -461
  88. package/src/VolumeLimiter.ts +0 -182
@@ -0,0 +1,621 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ LiveEventScope,
8
+ LiveTelemetryLogger,
9
+ LiveEventTarget,
10
+ ILiveEvent,
11
+ IRuntimeSignaler,
12
+ TimeInterval,
13
+ UserMeetingRole,
14
+ } from "@microsoft/live-share";
15
+ import {
16
+ CoordinationWaitPoint,
17
+ ExtendedMediaSessionActionDetails,
18
+ ExtendedMediaMetadata,
19
+ ExtendedMediaSessionPlaybackState,
20
+ MediaSessionCoordinatorEvents,
21
+ MediaSessionCoordinatorState,
22
+ MediaSessionCoordinatorSuspension,
23
+ } from "./MediaSessionExtensions";
24
+ import {
25
+ TelemetryEvents,
26
+ ITransportCommandEvent,
27
+ ISetTrackEvent,
28
+ IPositionUpdateEvent,
29
+ GroupCoordinatorState,
30
+ GroupCoordinatorStateEvents,
31
+ ITriggerActionEvent,
32
+ ISetTrackDataEvent,
33
+ } from "./internals";
34
+ import { LiveMediaSessionCoordinatorSuspension } from "./LiveMediaSessionCoordinatorSuspension";
35
+ import EventEmitter from "events";
36
+
37
+ /**
38
+ * Most recent state of the media session.
39
+ */
40
+ export interface IMediaPlayerState {
41
+ /**
42
+ * Metadata for the sessions current track.
43
+ */
44
+ metadata: ExtendedMediaMetadata | null;
45
+
46
+ /**
47
+ * Optional track data object being synchronized.
48
+ *
49
+ * @remarks
50
+ * This can be used to sync things like pitch, roll, and yaw when watching 360 videos together.
51
+ */
52
+ trackData: object | null;
53
+
54
+ /**
55
+ * Sessions current playback state.
56
+ */
57
+ playbackState: ExtendedMediaSessionPlaybackState;
58
+
59
+ /**
60
+ * Sessions current position state if known.
61
+ */
62
+ positionState?: MediaPositionState;
63
+ }
64
+
65
+ /**
66
+ * The `LiveMediaSessionCoordinator` tracks the playback & position state of all other
67
+ * clients being synchronized with. It is responsible for keeping the local media player
68
+ * in sync with the group.
69
+ */
70
+ export class LiveMediaSessionCoordinator extends EventEmitter {
71
+ private readonly _runtime: IRuntimeSignaler;
72
+ private readonly _logger: LiveTelemetryLogger;
73
+ private readonly _getPlayerState: () => IMediaPlayerState;
74
+ private _positionUpdateInterval = new TimeInterval(2000);
75
+ private _maxPlaybackDrift = new TimeInterval(1000);
76
+ private _lastWaitPoint?: CoordinationWaitPoint;
77
+ private _hasInitialized = false;
78
+
79
+ // Broadcast events
80
+ private _playEvent?: LiveEventTarget<ITransportCommandEvent>;
81
+ private _pauseEvent?: LiveEventTarget<ITransportCommandEvent>;
82
+ private _seekToEvent?: LiveEventTarget<ITransportCommandEvent>;
83
+ private _setTrackEvent?: LiveEventTarget<ISetTrackEvent>;
84
+ private _setTrackDataEvent?: LiveEventTarget<ISetTrackDataEvent>;
85
+ private _positionUpdateEvent?: LiveEventTarget<IPositionUpdateEvent>;
86
+ private _joinedEvent?: LiveEventTarget<ILiveEvent>;
87
+
88
+ // Distributed state
89
+ private _groupState?: GroupCoordinatorState;
90
+
91
+ /**
92
+ * @hidden
93
+ * Applications shouldn't directly create new coordinator instances.
94
+ */
95
+ constructor(
96
+ runtime: IRuntimeSignaler,
97
+ getPlayerState: () => IMediaPlayerState
98
+ ) {
99
+ super();
100
+ this._runtime = runtime;
101
+ this._logger = new LiveTelemetryLogger(runtime);
102
+ this._getPlayerState = getPlayerState;
103
+ }
104
+
105
+ /**
106
+ * Controls whether or not the local client is allowed to instruct the group to play or pause.
107
+ *
108
+ * @remarks
109
+ * This flag largely meant to influence decisions made by the coordinator and can be used by
110
+ * the UI to determine what controls should be shown to the user. It does not provide any
111
+ * security in itself.
112
+ *
113
+ * If your app is running in a semi-trusted environment where only some clients are allowed
114
+ * to play/pause media, you should use "role based verification" to enforce those policies.
115
+ */
116
+ public canPlayPause: boolean = true;
117
+
118
+ /**
119
+ * Controls whether or not the local client is allowed to seek the group to a new playback
120
+ * position.
121
+ *
122
+ * @remarks
123
+ * This flag largely meant to influence decisions made by the coordinator and can be used by
124
+ * the UI to determine what controls should be shown to the user. It does not provide any
125
+ * security in itself.
126
+ *
127
+ * If your app is running in a semi-trusted environment where only some clients are allowed
128
+ * to change the playback position, you should use "role based verification" to enforce those policies.
129
+ */
130
+ public canSeek: boolean = true;
131
+
132
+ /**
133
+ * Controls whether or not the local client is allowed to change tracks.
134
+ *
135
+ * @remarks
136
+ * This flag largely meant to influence decisions made by the coordinator and can be used by
137
+ * the UI to determine what controls should be shown to the user. It does not provide any
138
+ * security in itself.
139
+ *
140
+ * If your app is running in a semi-trusted environment where only some clients are allowed
141
+ * to change tracks, you should use "role based verification" to enforce those policies.
142
+ */
143
+ public canSetTrack: boolean = true;
144
+
145
+ /**
146
+ * Controls whether or not the local client is allowed to change the tracks custom data object.
147
+ *
148
+ * @remarks
149
+ * This flag largely meant to influence decisions made by the coordinator and can be used by
150
+ * the UI to determine what controls should be shown to the user. It does not provide any
151
+ * security in itself.
152
+ *
153
+ * If your app is running in a semi-trusted environment where only some clients are allowed
154
+ * to change the tracks data object, you should use "role based verification" to enforce those
155
+ * policies.
156
+ */
157
+ public canSetTrackData: boolean = true;
158
+
159
+ /**
160
+ * Returns true if the local client is in a suspended state.
161
+ */
162
+ public get isSuspended(): boolean {
163
+ return this._groupState ? this._groupState.isSuspended : false;
164
+ }
165
+
166
+ /**
167
+ * Max amount of playback drift allowed in seconds.
168
+ *
169
+ * @remarks
170
+ * Should the local clients playback position lag by more than the specified value, the
171
+ * coordinator will trigger a `catchup` action.
172
+ *
173
+ * Defaults to a value of `1` second.
174
+ */
175
+ public get maxPlaybackDrift(): number {
176
+ return this._maxPlaybackDrift.seconds;
177
+ }
178
+
179
+ public set maxPlaybackDrift(value: number) {
180
+ this._maxPlaybackDrift.seconds = value;
181
+ }
182
+
183
+ /**
184
+ * Frequency with which position updates are broadcast to the rest of the group in
185
+ * seconds.
186
+ *
187
+ * @remarks
188
+ * Defaults to a value of `2` seconds.
189
+ */
190
+ public get positionUpdateInterval(): number {
191
+ return this._positionUpdateInterval.seconds;
192
+ }
193
+
194
+ public set positionUpdateInterval(value: number) {
195
+ this._positionUpdateInterval.seconds = value;
196
+ }
197
+
198
+ /**
199
+ * Instructs the group to play the current track.
200
+ *
201
+ * @remarks
202
+ * Throws an exception if the session/coordinator hasn't been initialized, no track has been
203
+ * loaded, or `canPlayPause` is false.
204
+ */
205
+ public play(): void {
206
+ if (!this._hasInitialized) {
207
+ throw new Error(
208
+ `LiveMediaSessionCoordinator.play() called before initialize() called.`
209
+ );
210
+ }
211
+
212
+ if (!this._groupState?.playbackTrack.current.metadata) {
213
+ throw new Error(
214
+ `LiveMediaSessionCoordinator.play() called before MediaSession.metadata assigned.`
215
+ );
216
+ }
217
+
218
+ if (!this.canPlayPause) {
219
+ throw new Error(
220
+ `LiveMediaSessionCoordinator.play() operation blocked.`
221
+ );
222
+ }
223
+
224
+ // Get projected position
225
+ const position = this.getPlayerPosition();
226
+
227
+ // Send transport command
228
+ this._logger.sendTelemetryEvent(
229
+ TelemetryEvents.SessionCoordinator.PlayCalled,
230
+ null,
231
+ { position: position }
232
+ );
233
+ this._playEvent!.sendEvent({
234
+ track: this._groupState.playbackTrack.current,
235
+ position: position,
236
+ });
237
+ }
238
+
239
+ /**
240
+ * Instructs the group to pause the current track.
241
+ *
242
+ * @remarks
243
+ * Throws an exception if the session/coordinator hasn't been initialized, no track has been
244
+ * loaded, or `canPlayPause` is false.
245
+ */
246
+ public pause(): void {
247
+ if (!this._hasInitialized) {
248
+ throw new Error(
249
+ `LiveMediaSessionCoordinator.pause() called before initialize() called.`
250
+ );
251
+ }
252
+
253
+ if (!this._groupState?.playbackTrack.current.metadata) {
254
+ throw new Error(
255
+ `LiveMediaSessionCoordinator.pause() called before MediaSession.metadata assigned.`
256
+ );
257
+ }
258
+
259
+ if (!this.canPlayPause) {
260
+ throw new Error(
261
+ `LiveMediaSessionCoordinator.pause() operation blocked.`
262
+ );
263
+ }
264
+
265
+ // Get projected position
266
+ const position = this.getPlayerPosition();
267
+
268
+ // Send transport command
269
+ this._logger.sendTelemetryEvent(
270
+ TelemetryEvents.SessionCoordinator.PauseCalled,
271
+ null,
272
+ { position: position }
273
+ );
274
+ this._pauseEvent!.sendEvent({
275
+ track: this._groupState.playbackTrack.current,
276
+ position: position,
277
+ });
278
+ }
279
+
280
+ /**
281
+ * Instructs the group to seek to a new position within the current track.
282
+ *
283
+ * @remarks
284
+ * Throws an exception if the session/coordinator hasn't been initialized, no track has been
285
+ * loaded, or `canSeek` is false.
286
+ * @param time Playback position in seconds to seek to.
287
+ */
288
+ public seekTo(time: number): void {
289
+ if (!this._hasInitialized) {
290
+ throw new Error(
291
+ `LiveMediaSessionCoordinator.seekTo() called before initialize() called.`
292
+ );
293
+ }
294
+
295
+ if (!this._groupState?.playbackTrack.current.metadata) {
296
+ throw new Error(
297
+ `LiveMediaSessionCoordinator.seekTo() called before MediaSession.metadata assigned.`
298
+ );
299
+ }
300
+
301
+ if (!this.canSeek) {
302
+ throw new Error(
303
+ `LiveMediaSessionCoordinator.seekTo() operation blocked.`
304
+ );
305
+ }
306
+
307
+ // Send transport command
308
+ this._logger.sendTelemetryEvent(
309
+ TelemetryEvents.SessionCoordinator.SeekToCalled,
310
+ null,
311
+ { position: time }
312
+ );
313
+ this._seekToEvent!.sendEvent({
314
+ track: this._groupState.playbackTrack.current,
315
+ position: time,
316
+ });
317
+ }
318
+
319
+ /**
320
+ * Instructs the group to load a new track.
321
+ *
322
+ * @remarks
323
+ * Throws an exception if the session/coordinator hasn't been initialized or `canSetTrack` is
324
+ * false.
325
+ * @param metadata The track to load or `null` to indicate that the end of the track is reached.
326
+ * @param waitPoints Optional. List of static wait points to configure for the track. Dynamic wait points can be added via the `beginSuspension()` call.
327
+ */
328
+ public setTrack(
329
+ metadata: ExtendedMediaMetadata | null,
330
+ waitPoints?: CoordinationWaitPoint[]
331
+ ): void {
332
+ if (!this._hasInitialized) {
333
+ throw new Error(
334
+ `LiveMediaSessionCoordinator.setTrack() called before initialize() called.`
335
+ );
336
+ }
337
+
338
+ if (!this.canSetTrack) {
339
+ throw new Error(
340
+ `LiveMediaSessionCoordinator.setTrack() operation blocked.`
341
+ );
342
+ }
343
+
344
+ // Send transport command
345
+ this._logger.sendTelemetryEvent(
346
+ TelemetryEvents.SessionCoordinator.SetTrackCalled
347
+ );
348
+ this._setTrackEvent!.sendEvent({
349
+ metadata: metadata,
350
+ waitPoints: waitPoints || [],
351
+ });
352
+ }
353
+
354
+ /**
355
+ * Updates the track data object for the current track.
356
+ *
357
+ * @remarks
358
+ * The track data object can be used by applications to synchronize things like pitch, roll,
359
+ * and yaw of a 360 video. This data object will be reset to null anytime the track changes.
360
+ *
361
+ * Throws an exception if the session/coordinator hasn't been initialized or `canSetTrackData` is
362
+ * false.
363
+ * @param data New data object to sync with the group. This value will be synchronized using a last writer wins strategy.
364
+ */
365
+ public setTrackData(data: object | null): void {
366
+ if (!this._hasInitialized) {
367
+ throw new Error(
368
+ `LiveMediaSessionCoordinator.setTrackData() called before initialize() called.`
369
+ );
370
+ }
371
+
372
+ if (!this.canSetTrackData) {
373
+ throw new Error(
374
+ `LiveMediaSessionCoordinator.setTrackData() operation blocked.`
375
+ );
376
+ }
377
+
378
+ // Send transport command
379
+ this._logger.sendTelemetryEvent(
380
+ TelemetryEvents.SessionCoordinator.SetTrackDataCalled
381
+ );
382
+ this._setTrackDataEvent!.sendEvent({
383
+ data: data,
384
+ });
385
+ }
386
+
387
+ /**
388
+ * Begins a new local suspension.
389
+ *
390
+ * @remarks
391
+ * Suspension temporarily suspend the clients local synchronization with the group. This can
392
+ * be useful for displaying ads to users or temporarily disconnecting from the session while
393
+ * the user seeks the video using a timeline scrubber.
394
+ *
395
+ * Multiple simultaneous suspensions are allowed and when the last suspension ends the local
396
+ * client will be immediately re-synchronized with the group.
397
+ *
398
+ * A "Dynamic Wait Point" can be specified when `beginSuspension()` is called and the wait
399
+ * point will be broadcast to all other clients in the group. Those clients will then
400
+ * automatically enter a suspension state once they reach the positions specified by the
401
+ * wait point. Clients that are passed the wait point will immediately suspend.
402
+ *
403
+ * Any wait point based suspension (dynamic or static) will result in all clients remaining
404
+ * in a suspension state until the list client ends their suspension. This behavior can be
405
+ * conditionally bypassed by settings the wait points `maxClients` value.
406
+ *
407
+ * Throws an exception if the session/coordinator hasn't been initialized.
408
+ * @param waitPoint Optional. Dynamic wait point to broadcast to all of the clients.
409
+ * @returns The suspension object. Call `end()` on the returned suspension to end the suspension.
410
+ */
411
+ public beginSuspension(
412
+ waitPoint?: CoordinationWaitPoint
413
+ ): MediaSessionCoordinatorSuspension {
414
+ if (!this._hasInitialized) {
415
+ throw new Error(
416
+ `LiveMediaSessionCoordinator.beginSuspension() called before initialize() called.`
417
+ );
418
+ }
419
+
420
+ if (!this._groupState?.playbackTrack.current.metadata) {
421
+ throw new Error(
422
+ `LiveMediaSessionCoordinator.beginSuspension() called before MediaSession.metadata assigned.`
423
+ );
424
+ }
425
+
426
+ // Tell group state that suspension is started
427
+ if (waitPoint) {
428
+ this._logger.sendTelemetryEvent(
429
+ TelemetryEvents.SessionCoordinator.BeginSuspensionAndWait,
430
+ null,
431
+ {
432
+ position: waitPoint.position,
433
+ maxClients: waitPoint.maxClients,
434
+ }
435
+ );
436
+ this._lastWaitPoint = waitPoint;
437
+ this._groupState.beginSuspension(waitPoint);
438
+ } else {
439
+ this._logger.sendTelemetryEvent(
440
+ TelemetryEvents.SessionCoordinator.BeginSuspension
441
+ );
442
+ this._groupState.beginSuspension();
443
+ }
444
+
445
+ // Return new suspension object
446
+ return new LiveMediaSessionCoordinatorSuspension(waitPoint, (time) => {
447
+ if (waitPoint) {
448
+ this._logger.sendTelemetryEvent(
449
+ TelemetryEvents.SessionCoordinator.EndSuspensionAndWait,
450
+ null,
451
+ {
452
+ position: waitPoint.position,
453
+ maxClients: waitPoint.maxClients,
454
+ }
455
+ );
456
+ } else {
457
+ this._logger.sendTelemetryEvent(
458
+ TelemetryEvents.SessionCoordinator.EndSuspension
459
+ );
460
+ }
461
+ this._groupState!.endSuspension(time == undefined);
462
+ if (
463
+ time != undefined &&
464
+ !this._groupState?.isSuspended &&
465
+ !this._groupState?.isWaiting
466
+ ) {
467
+ // Seek to new position
468
+ this.seekTo(Math.max(time, 0.0));
469
+ }
470
+ });
471
+ }
472
+
473
+ /**
474
+ * @hidden
475
+ * Called by MediaSession to verify the coordinator has been initialized.
476
+ */
477
+ public get isInitialized(): boolean {
478
+ return this._hasInitialized;
479
+ }
480
+
481
+ /**
482
+ * @hidden
483
+ * Called by MediaSession to start coordinator.
484
+ */
485
+ public async initialize(
486
+ acceptTransportChangesFrom?: UserMeetingRole[]
487
+ ): Promise<void> {
488
+ if (this._hasInitialized) {
489
+ throw new Error(
490
+ `LiveMediaSessionCoordinator.initialize() already initialized.`
491
+ );
492
+ }
493
+
494
+ // Create children
495
+ await this.createChildren(acceptTransportChangesFrom);
496
+ this._hasInitialized = true;
497
+ }
498
+
499
+ /**
500
+ * @hidden
501
+ * Called by the MediaSession to detect if a wait point has been hit.
502
+ */
503
+ public findNextWaitPoint(): CoordinationWaitPoint | null {
504
+ return (
505
+ this._groupState?.playbackTrack.findNextWaitPoint(
506
+ this._lastWaitPoint
507
+ ) || null
508
+ );
509
+ }
510
+
511
+ /**
512
+ * @hidden
513
+ * Called by MediaSession to trigger the sending of a position update.
514
+ */
515
+ public sendPositionUpdate(state: IMediaPlayerState): void {
516
+ if (!this._hasInitialized) {
517
+ throw new Error(
518
+ `LiveMediaSessionCoordinator.sendPositionUpdate() called before initialize() called.`
519
+ );
520
+ }
521
+
522
+ // Send position update event
523
+ const evt = this._groupState!.createPositionUpdateEvent(state);
524
+ this._positionUpdateEvent?.sendEvent(evt);
525
+ }
526
+
527
+ protected async createChildren(
528
+ acceptTransportChangesFrom?: UserMeetingRole[]
529
+ ): Promise<void> {
530
+ // Create event scopes
531
+ const scope = new LiveEventScope(
532
+ this._runtime,
533
+ acceptTransportChangesFrom
534
+ );
535
+ const unrestrictedScope = new LiveEventScope(this._runtime);
536
+
537
+ // Initialize internal coordinator state
538
+ this._groupState = new GroupCoordinatorState(
539
+ this._runtime,
540
+ this._maxPlaybackDrift,
541
+ this._positionUpdateInterval,
542
+ this._getPlayerState
543
+ );
544
+ this._groupState.on(GroupCoordinatorStateEvents.triggeraction, (evt) =>
545
+ this.emit(evt.name, evt)
546
+ );
547
+
548
+ // Listen for track changes
549
+ this._setTrackEvent = new LiveEventTarget<ISetTrackEvent>(
550
+ scope,
551
+ "setTrack",
552
+ (event, local) => this._groupState!.handleSetTrack(event, local)
553
+ );
554
+ this._setTrackDataEvent = new LiveEventTarget<ISetTrackDataEvent>(
555
+ scope,
556
+ "setTrackData",
557
+ (event, local) => this._groupState!.handleSetTrackData(event, local)
558
+ );
559
+
560
+ // Listen for transport commands
561
+ this._playEvent = new LiveEventTarget(scope, "play", (event, local) =>
562
+ this._groupState!.handleTransportCommand(event, local)
563
+ );
564
+ this._pauseEvent = new LiveEventTarget(scope, "pause", (event, local) =>
565
+ this._groupState!.handleTransportCommand(event, local)
566
+ );
567
+ this._seekToEvent = new LiveEventTarget(
568
+ scope,
569
+ "seekTo",
570
+ (event, local) =>
571
+ this._groupState!.handleTransportCommand(event, local)
572
+ );
573
+
574
+ // Listen for position updates
575
+ this._positionUpdateEvent = new LiveEventTarget(
576
+ unrestrictedScope,
577
+ "positionUpdate",
578
+ (event, local) =>
579
+ this._groupState!.handlePositionUpdate(event, local)
580
+ );
581
+
582
+ // Listen for joined event
583
+ this._joinedEvent = new LiveEventTarget(
584
+ unrestrictedScope,
585
+ "joined",
586
+ (evt, local) => {
587
+ // Immediately send a position update
588
+ this._logger.sendTelemetryEvent(
589
+ TelemetryEvents.SessionCoordinator.RemoteJoinReceived,
590
+ null,
591
+ {
592
+ correlationId: LiveTelemetryLogger.formatCorrelationId(
593
+ evt.clientId,
594
+ evt.timestamp
595
+ ),
596
+ }
597
+ );
598
+ const state = this._getPlayerState();
599
+ const update =
600
+ this._groupState!.createPositionUpdateEvent(state);
601
+ this._positionUpdateEvent?.sendEvent(update);
602
+ }
603
+ );
604
+
605
+ // Send initial joined event
606
+ this._joinedEvent.sendEvent({});
607
+ }
608
+
609
+ private getPlayerPosition(): number {
610
+ const { positionState, playbackState } = this._getPlayerState();
611
+ switch (playbackState) {
612
+ case "none":
613
+ case "ended":
614
+ return 0.0;
615
+ default:
616
+ return positionState && positionState.position != undefined
617
+ ? positionState.position
618
+ : 0.0;
619
+ }
620
+ }
621
+ }
@@ -1,31 +1,39 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import { MediaSessionCoordinatorSuspension, CoordinationWaitPoint } from './MediaSessionExtensions';
7
-
8
- export class EphemeralMediaSessionCoordinatorSuspension implements MediaSessionCoordinatorSuspension {
9
- private _waitPoint?: CoordinationWaitPoint;
10
- private _onEnd: (seekTo?: number) => void;
11
-
12
- constructor(waitPoint: CoordinationWaitPoint|undefined, onEnd: (seekTo?: number) => void) {
13
- this._waitPoint = waitPoint;
14
- this._onEnd = onEnd;
15
- }
16
-
17
- /**
18
- * Returns an optional wait point associated with the suspension.
19
- */
20
- public get waitPoint(): CoordinationWaitPoint|undefined {
21
- return this._waitPoint;
22
- }
23
-
24
- /**
25
- * Ends the suspension and optionally proposes a time to seek to.
26
- * @param seekTo Optional. Proposed time to seek to. Ignored for suspensions at wait points.
27
- */
28
- public end(seekTo?: number): void {
29
- this._onEnd(seekTo);
30
- }
31
- }
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+
6
+ import {
7
+ MediaSessionCoordinatorSuspension,
8
+ CoordinationWaitPoint,
9
+ } from "./MediaSessionExtensions";
10
+
11
+ export class LiveMediaSessionCoordinatorSuspension
12
+ implements MediaSessionCoordinatorSuspension
13
+ {
14
+ private _waitPoint?: CoordinationWaitPoint;
15
+ private _onEnd: (seekTo?: number) => void;
16
+
17
+ constructor(
18
+ waitPoint: CoordinationWaitPoint | undefined,
19
+ onEnd: (seekTo?: number) => void
20
+ ) {
21
+ this._waitPoint = waitPoint;
22
+ this._onEnd = onEnd;
23
+ }
24
+
25
+ /**
26
+ * Returns an optional wait point associated with the suspension.
27
+ */
28
+ public get waitPoint(): CoordinationWaitPoint | undefined {
29
+ return this._waitPoint;
30
+ }
31
+
32
+ /**
33
+ * Ends the suspension and optionally proposes a time to seek to.
34
+ * @param seekTo Optional. Proposed time to seek to. Ignored for suspensions at wait points.
35
+ */
36
+ public end(seekTo?: number): void {
37
+ this._onEnd(seekTo);
38
+ }
39
+ }