@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
@@ -1,450 +1,636 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the Microsoft Live Share SDK License.
4
- */
5
-
6
- import { EphemeralTelemetryLogger, IEvent, TimeInterval } from '@microsoft/live-share';
7
- import EventEmitter from 'events';
8
- import { ExtendedMediaSessionAction, ExtendedMediaSessionPlaybackState, ExtendedMediaMetadata, CoordinationWaitPoint, ExtendedMediaSessionActionDetails, MediaSessionCoordinatorEvents, MediaSessionCoordinatorSuspension } from './MediaSessionExtensions';
9
- import { EphemeralMediaSession } from './EphemeralMediaSession';
10
- import { VolumeLimiter } from './VolumeLimiter';
11
- import { IMediaPlayer } from './IMediaPlayer';
12
- import { TelemetryEvents } from './internals';
13
-
14
- /**
15
- * Event data returned by `MediaPlayerSynchronizer` object.
16
- */
17
- export interface IMediaPlayerSynchronizerEvent extends IEvent {
18
- /**
19
- * Event details.
20
- */
21
- details: ExtendedMediaSessionActionDetails;
22
- /**
23
- * Action errors, including DOMException errors thrown by HTMLMediaElement
24
- */
25
- error?: Error;
26
- }
27
-
28
- /**
29
- * Events supported by `MediaPlayerSynchronizer` object.
30
- */
31
- export enum MediaPlayerSynchronizerEvents {
32
- /**
33
- *
34
- */
35
- coordinatorstatechange = 'coordinatorstatechange',
36
- groupaction = 'groupaction',
37
- useraction = 'useraction'
38
- }
39
-
40
- /**
41
- * Synchronizes a local HTML Media Element with a group of remote HTML Media Elements.
42
- *
43
- * @remarks
44
- * All of an apps transport control commands should be routed through the synchronizer. If the
45
- * app is not currently joined to the group media session, the commands will be applied directly
46
- * to the local player. When the group session is joined the commands will be broadcast to the
47
- * group in addition to being applied to the local player.
48
- */
49
- export class MediaPlayerSynchronizer extends EventEmitter {
50
- private static SESSION_ACTIONS: ExtendedMediaSessionAction[] = ['play', 'pause', 'seekto', 'settrack', 'datachange', 'catchup', 'wait'];
51
- private static PLAYER_EVENTS: string[] = ['playing', 'pause', 'ratechange', 'timeupdate', 'ended', 'loadedmetadata', 'blocked'];
52
-
53
- private _logger: EphemeralTelemetryLogger;
54
- private _player: IMediaPlayer;
55
- private _mediaSession: EphemeralMediaSession;
56
- private _volumeLimiter: VolumeLimiter;
57
- private _onEnd?: () => void;
58
- private _onPlayerEvent: EventListener;
59
- private _seekSuspension?: MediaSessionCoordinatorSuspension;
60
- private _viewOnly = false;
61
- private _expectedPlaybackState: ExtendedMediaSessionPlaybackState = 'none';
62
- private _trackData: object|null = null;
63
-
64
- /**
65
- * Creates a new `MediaElementSynchronizer` instance.
66
- * @param player Media player element. This can be an HTML Media Element or any player that looks like an HTML Media Element.
67
- * @param mediaSession Group MediaSession object being used.
68
- * @param onEnd Optional. Function to call when synchronizers `end()` method is called.
69
- */
70
- constructor(player: IMediaPlayer, mediaSession: EphemeralMediaSession, onEnd: () => void) {
71
- super();
72
- this._player = player;
73
- this._mediaSession = mediaSession;
74
- this._logger = mediaSession.logger;
75
- this._volumeLimiter = new VolumeLimiter(player);
76
- this._onEnd = onEnd;
77
-
78
- // Listen for player state requests
79
- mediaSession.setRequestPlayerStateHandler(() => {
80
- // Find playback state
81
- let playbackState: ExtendedMediaSessionPlaybackState;
82
- const src = this._player.currentSrc || this._player.src;
83
- if (this._player.ended) {
84
- playbackState = 'ended';
85
- } else if (!src || (this._player.paused && this._player.currentTime < 1.0)) {
86
- playbackState = 'none';
87
- } else if (this._player.paused) {
88
- playbackState = 'paused';
89
- } else {
90
- playbackState = 'playing';
91
- }
92
-
93
- const state = {
94
- metadata: { trackIdentifier: src } as ExtendedMediaMetadata,
95
- playbackState: playbackState,
96
- positionState: {
97
- position: this._player.currentTime,
98
- playbackRate: this._player.playbackRate,
99
- duration: this._player.duration
100
- },
101
- trackData: this._trackData
102
- };
103
-
104
- return state;
105
- });
106
-
107
- // Listen for player events
108
- this._onPlayerEvent = (evt: Event) => {
109
- if (evt.type != 'timeupdate') {
110
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.PlayerEvent, null, {type: evt.type});
111
- }
112
-
113
- switch (evt.type) {
114
- case 'loadedmetadata':
115
- const src = this._player.currentSrc || this._player.src;
116
- this._trackData = null;
117
- break;
118
- case 'playing':
119
- // Handle case for YouTube player where user can pause/play video by clicking on it.
120
- // - Videos don't always start at 0.0 seconds.
121
- if (this._expectedPlaybackState != 'playing') {
122
- if (this._mediaSession.coordinator.canPlayPause && this._player.currentTime < 1.0) {
123
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.UserTappedVideoToPlay);
124
- this.play();
125
- }
126
- }
127
- // block play if player state is playing when expected synced state is paused
128
- // needed for YouTube because cannot tell if its a user initiated event, so disallow play (except when starting)
129
- if (this._expectedPlaybackState === 'paused') {
130
- this._player.pause();
131
- }
132
- break;
133
- case 'pause':
134
- // block pause if player state is paused when expected synced state is playing
135
- // needed for YouTube because cannot tell if its a user initiated event, so disallow pause
136
- if (this._expectedPlaybackState === 'playing') {
137
- this._player.play();
138
- }
139
- break;
140
- case 'ratechange':
141
- // Block rate changes unless suspended.
142
- if (this._player.playbackRate != 1.0 && !this._mediaSession.coordinator.isSuspended) {
143
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.PlaybackRateChangeBlocked);
144
- this._player.playbackRate = 1.0;
145
- }
146
- break;
147
- case 'blocked':
148
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.PlaybackRateChangeBlocked);
149
- break;
150
- }
151
- };
152
-
153
- // Register for coordinator state changes
154
- this._mediaSession.coordinator.on(MediaSessionCoordinatorEvents.coordinatorstatechange, evt => this.emit(evt.type, evt));
155
-
156
- // Register media session actions
157
- for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
158
- this._mediaSession.setActionHandler(action, async (details: ExtendedMediaSessionActionDetails) => {
159
- let error: Error | undefined;
160
- try {
161
- switch (details.action) {
162
- case 'play':
163
- this._expectedPlaybackState = 'playing';
164
- if (this._player.paused) {
165
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.PlayAction);
166
- if (typeof details.seekTime == 'number' && this._player.currentTime < 1.0) {
167
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SeekingPlayerToStartPosition, null, {position: details.seekTime})
168
- this._player.currentTime = details.seekTime!;
169
- }
170
- // Reference: https://developer.mozilla.org/docs/Web/API/HTMLMediaElement/play#exceptions
171
- await this._player.play();
172
- }
173
- break;
174
- case 'pause':
175
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.PauseAction);
176
- this._expectedPlaybackState = 'paused';
177
- if (typeof details.seekTime == 'number' && this._player.currentTime < 1.0) {
178
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SeekingPlayerToStartPosition, null, {position: details.seekTime})
179
- this._player.currentTime = details.seekTime!;
180
- }
181
- this._player.pause();
182
- break;
183
- case 'seekto':
184
- if (typeof details.seekTime == 'number') {
185
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SeekToAction, null, {position: details.seekTime});
186
- this._player.currentTime = details.seekTime!;
187
- }
188
- break;
189
- case 'settrack':
190
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SetTrackAction);
191
- this._expectedPlaybackState = 'none';
192
- this._player.src = details.metadata!.trackIdentifier;
193
- this._player.load();
194
- break;
195
- case 'datachange':
196
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.DataChangeAction);
197
- break;
198
- case 'catchup':
199
- if (typeof details.seekTime == 'number') {
200
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.CatchupAction, null, {position: details.seekTime});
201
- this.catchupPlayer(details.seekTime!);
202
- }
203
- break;
204
- }
205
- } catch (err: any) {
206
- if (err instanceof Error) {
207
- error = err;
208
- } else {
209
- error = new Error(err?.message ?? "An unknown error occurred after processing the group session action.");
210
- }
211
- }
212
-
213
- this.dispatchGroupAction(details, false, error);
214
- });
215
- }
216
-
217
- // Subscribe to player events
218
- for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
219
- this._player.addEventListener(event, this._onPlayerEvent);
220
- }
221
-
222
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SynchronizationStarted);
223
- }
224
-
225
- /**
226
- * Media player being synchronized.
227
- */
228
- public get player(): IMediaPlayer {
229
- return this._player;
230
- }
231
-
232
- /**
233
- * Synchronizers media session.
234
- */
235
- public get mediaSession(): EphemeralMediaSession {
236
- return this._mediaSession;
237
- }
238
-
239
- /**
240
- * If true the client is in a view only mode.
241
- *
242
- * @remarks
243
- * Toggling this value to true results in `mediaSession.coordinator.canPlayPause`,
244
- * `mediaSession.coordinator.canSeek`, `mediaSession.coordinator.canSetTrack`, and
245
- * `mediaSession.coordinator.canSetTrackData` all being set to false. For more fine
246
- * grained control over the local clients policies, call the `mediaSession.coordinator`
247
- * directly.
248
- */
249
- public get viewOnly(): boolean {
250
- return this._viewOnly;
251
- }
252
-
253
- public set viewOnly(value: boolean) {
254
- this._viewOnly = value;
255
- this.mediaSession.coordinator.canPlayPause = !value;
256
- this.mediaSession.coordinator.canSeek = !value;
257
- this.mediaSession.coordinator.canSetTrack = !value;
258
- this.mediaSession.coordinator.canSetTrackData = !value;
259
- }
260
-
261
- /**
262
- * Volume limiter used to temporarily reduce the videos volume when someone speaks in a meeting.
263
- */
264
- public get volumeLimiter(): VolumeLimiter {
265
- return this._volumeLimiter;
266
- }
267
-
268
- /**
269
- * Registers a new event listener.
270
- * @param event Name of the event to add.
271
- * @param listener Function to call when the event is triggered.
272
- */
273
- public addEventListener(event: MediaPlayerSynchronizerEvents, listener: (evt: IMediaPlayerSynchronizerEvent) => void): this {
274
- this.on(event, listener);
275
- return this;
276
- }
277
-
278
- /**
279
- * Un-registers an existing event listener.
280
- * @param event Name of the event to remove.
281
- * @param listener Function that was registered in call to `addEventListener()`.
282
- */
283
- public removeEventListener(event: MediaPlayerSynchronizerEvents, listener: (evt: IMediaPlayerSynchronizerEvent) => void): this {
284
- this.off(event, listener);
285
- return this;
286
- }
287
-
288
- /**
289
- * Ends synchronization of the current media player.
290
- */
291
- public end(): void {
292
- if (this._onEnd) {
293
- try {
294
- // Clear media session actions
295
- for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
296
- this._mediaSession.setActionHandler(action, null);
297
- }
298
-
299
- // Subscribe to player events
300
- for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
301
- this._player.removeEventListener(event, this._onPlayerEvent);
302
- }
303
-
304
- // Notify parent
305
- this._onEnd();
306
- } finally {
307
- this._onEnd = undefined;
308
- }
309
- }
310
- }
311
-
312
- /**
313
- * Begin a local seek operation.
314
- *
315
- * @remarks
316
- * UI can call this when a user grabs a timeline scrubber and starts scrubbing the video to a
317
- * new playback position. The synchronizer will being a new suspension which temporarily
318
- * disconnects the client for the rest of the group for synchronization purposes. Calling
319
- * `endSeek()` will end the suspension and seek the group to the users final seek position.
320
- */
321
- public beginSeek(): void {
322
- if (this._seekSuspension) {
323
- throw new Error(`MediaPlayerSynchronizer: cannot begin seek. A seek is already in progress.`);
324
- }
325
-
326
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.BeginSeekCalled);
327
- this._seekSuspension = this._mediaSession.coordinator.beginSuspension();
328
- }
329
-
330
- /**
331
- * Ends a seek operation that was started by calling `beginSeek()`.
332
- * @param seekTo Playback position in seconds to seek the group to.
333
- */
334
- public endSeek(seekTo: number): void {
335
- if (!this._seekSuspension) {
336
- throw new Error(`MediaPlayerSynchronizer: cannot end seek. No seek is in progress.`);
337
- }
338
-
339
- // Seek player to new time immediately.
340
- // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
341
- // location.
342
- this._player.currentTime = seekTo;
343
-
344
- const suspension = this._seekSuspension;
345
- this._seekSuspension = undefined;
346
-
347
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.EndSeekCalled, null, {position: seekTo});
348
- suspension.end(seekTo);
349
-
350
- this.dispatchUserAction({action: 'seekto', seekTime: seekTo});
351
- }
352
-
353
- /**
354
- * Tells the group to begin playing the current video.
355
- *
356
- * @remarks
357
- * For proper operation apps should avoid calling `mediaSession.coordinator.play()` directly
358
- * and instead use the synchronizers `play()` method.
359
- */
360
- public play(): void {
361
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.PlayCalled);
362
- this._expectedPlaybackState = 'playing';
363
- this._mediaSession.coordinator.play();
364
-
365
- this.dispatchUserAction({action: 'play'});
366
- }
367
-
368
- /**
369
- * Tells the group to pause the current video.
370
- *
371
- * @remarks
372
- * For proper operation apps should avoid calling `mediaSession.coordinator.pause()` directly
373
- * and instead use the synchronizers `pause()` method.
374
- */
375
- public pause(): void {
376
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.PauseCalled);
377
- this._expectedPlaybackState = 'paused';
378
- this._mediaSession.coordinator.pause();
379
-
380
- this.dispatchUserAction({action: 'pause'});
381
- }
382
-
383
- /**
384
- * Tells the group to seek the current video to a new playback position.
385
- *
386
- * @remarks
387
- * For proper operation apps should avoid calling `mediaSession.coordinator.seekTo()` directly
388
- * and instead use the synchronizers `seekTo()` method.
389
- */
390
- public seekTo(time: number): void {
391
- // Always seek player to new time.
392
- // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
393
- // location.
394
- this._player.currentTime = time;
395
-
396
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SeekToCalled, null, {position: time});
397
- this._mediaSession.coordinator.seekTo(time);
398
-
399
- this.dispatchUserAction({action: 'seekto', seekTime: time});
400
- }
401
-
402
- /**
403
- * Tells the group to change to a new track.
404
- *
405
- * @remarks
406
- * For proper operation apps should avoid calling `mediaSession.coordinator.setTrack()` directly
407
- * and instead use the synchronizers `setTrack()` method.
408
- */
409
- public setTrack(track: ExtendedMediaMetadata, waitPoints?: CoordinationWaitPoint[]): void {
410
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SetTrackCalled);
411
- this._mediaSession.coordinator.setTrack(track, waitPoints);
412
-
413
- this.dispatchUserAction({action: 'settrack', metadata: track});
414
- }
415
-
416
- /**
417
- * Updates the current tracks data object.
418
- *
419
- * @remarks
420
- * For proper operation apps should avoid calling `mediaSession.coordinator.setTrackData()` directly
421
- * and instead use the synchronizers `setTrackData()` method.
422
- */
423
- public setTrackData(data: object|null): void {
424
- this._logger.sendTelemetryEvent(TelemetryEvents.MediaPlayerSynchronizer.SetTrackDataCalled);
425
- this._trackData = data;
426
- this._mediaSession.coordinator.setTrackData(data);
427
-
428
- this.dispatchUserAction({action: 'datachange', data: data});
429
- }
430
-
431
- private dispatchGroupAction(details: ExtendedMediaSessionActionDetails, delay = false, error: Error | undefined): void {
432
- if (delay) {
433
- setTimeout(() => this.emit(MediaPlayerSynchronizerEvents.groupaction, {type: MediaPlayerSynchronizerEvents.groupaction, details: details, playerError: error}), 50);
434
- } else {
435
- this.emit(MediaPlayerSynchronizerEvents.groupaction, {type: MediaPlayerSynchronizerEvents.groupaction, details: details, error});
436
- }
437
- }
438
-
439
- private dispatchUserAction(details: ExtendedMediaSessionActionDetails, delay = false): void {
440
- if (delay) {
441
- setTimeout(() => this.emit(MediaPlayerSynchronizerEvents.useraction, {type: MediaPlayerSynchronizerEvents.useraction, details: details}), 50);
442
- } else {
443
- this.emit(MediaPlayerSynchronizerEvents.useraction, {type: MediaPlayerSynchronizerEvents.useraction, details: details});
444
- }
445
- }
446
-
447
- private async catchupPlayer(time: number): Promise<void> {
448
- this._player.currentTime = time;
449
- }
450
- }
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the Microsoft Live Share SDK License.
4
+ */
5
+ import { LiveTelemetryLogger, IEvent } from "@microsoft/live-share";
6
+ import EventEmitter from "events";
7
+ import {
8
+ ExtendedMediaSessionAction,
9
+ ExtendedMediaSessionPlaybackState,
10
+ ExtendedMediaMetadata,
11
+ CoordinationWaitPoint,
12
+ ExtendedMediaSessionActionDetails,
13
+ MediaSessionCoordinatorEvents,
14
+ MediaSessionCoordinatorSuspension,
15
+ } from "./MediaSessionExtensions";
16
+ import { VolumeManager } from "./VolumeManager";
17
+ import { LiveMediaSession } from "./LiveMediaSession";
18
+ import { IMediaPlayer } from "./IMediaPlayer";
19
+ import { TelemetryEvents } from "./internals";
20
+
21
+ /**
22
+ * Event data returned by `MediaPlayerSynchronizer` object.
23
+ */
24
+ export interface IMediaPlayerSynchronizerEvent extends IEvent {
25
+ /**
26
+ * Event details.
27
+ */
28
+ details: ExtendedMediaSessionActionDetails;
29
+ /**
30
+ * Action errors, including DOMException errors thrown by HTMLMediaElement
31
+ */
32
+ error?: Error;
33
+ }
34
+
35
+ /**
36
+ * Events supported by `MediaPlayerSynchronizer` object.
37
+ */
38
+ export enum MediaPlayerSynchronizerEvents {
39
+ /**
40
+ *
41
+ */
42
+ coordinatorstatechange = "coordinatorstatechange",
43
+ groupaction = "groupaction",
44
+ useraction = "useraction",
45
+ }
46
+
47
+ /**
48
+ * Synchronizes a local HTML Media Element with a group of remote HTML Media Elements.
49
+ *
50
+ * @remarks
51
+ * All of an apps transport control commands should be routed through the synchronizer. If the
52
+ * app is not currently joined to the group media session, the commands will be applied directly
53
+ * to the local player. When the group session is joined the commands will be broadcast to the
54
+ * group in addition to being applied to the local player.
55
+ */
56
+ export class MediaPlayerSynchronizer extends EventEmitter {
57
+ private static SESSION_ACTIONS: ExtendedMediaSessionAction[] = [
58
+ "play",
59
+ "pause",
60
+ "seekto",
61
+ "settrack",
62
+ "datachange",
63
+ "catchup",
64
+ "wait",
65
+ ];
66
+ private static PLAYER_EVENTS: string[] = [
67
+ "playing",
68
+ "pause",
69
+ "ratechange",
70
+ "timeupdate",
71
+ "ended",
72
+ "loadedmetadata",
73
+ "blocked",
74
+ ];
75
+
76
+ private _logger: LiveTelemetryLogger;
77
+ private _player: IMediaPlayer;
78
+ private _mediaSession: LiveMediaSession;
79
+ private _volumeManager: VolumeManager;
80
+ private _onEnd?: () => void;
81
+ private _onPlayerEvent: EventListener;
82
+ private _seekSuspension?: MediaSessionCoordinatorSuspension;
83
+ private _viewOnly = false;
84
+ private _expectedPlaybackState: ExtendedMediaSessionPlaybackState = "none";
85
+ private _trackData: object | null = null;
86
+
87
+ /**
88
+ * Creates a new `MediaElementSynchronizer` instance.
89
+ * @param player Media player element. This can be an HTML Media Element or any player that looks like an HTML Media Element.
90
+ * @param mediaSession Group MediaSession object being used.
91
+ * @param onEnd Optional. Function to call when synchronizers `end()` method is called.
92
+ */
93
+ constructor(
94
+ player: IMediaPlayer,
95
+ mediaSession: LiveMediaSession,
96
+ onEnd: () => void
97
+ ) {
98
+ super();
99
+ this._player = player;
100
+ this._mediaSession = mediaSession;
101
+ this._logger = mediaSession.logger;
102
+ this._volumeManager = new VolumeManager(player);
103
+ this._onEnd = onEnd;
104
+
105
+ // Listen for player state requests
106
+ mediaSession.setRequestPlayerStateHandler(() => {
107
+ // Find playback state
108
+ let playbackState: ExtendedMediaSessionPlaybackState;
109
+ const src = this._player.currentSrc || this._player.src;
110
+ if (this._player.ended) {
111
+ playbackState = "ended";
112
+ } else if (
113
+ !src ||
114
+ (this._player.paused && this._player.currentTime < 1.0)
115
+ ) {
116
+ playbackState = "none";
117
+ } else if (this._player.paused) {
118
+ playbackState = "paused";
119
+ } else {
120
+ playbackState = "playing";
121
+ }
122
+
123
+ const state = {
124
+ metadata: { trackIdentifier: src } as ExtendedMediaMetadata,
125
+ playbackState: playbackState,
126
+ positionState: {
127
+ position: this._player.currentTime,
128
+ playbackRate: this._player.playbackRate,
129
+ duration: this._player.duration,
130
+ },
131
+ trackData: this._trackData,
132
+ };
133
+
134
+ return state;
135
+ });
136
+
137
+ // Listen for player events
138
+ this._onPlayerEvent = (evt: Event) => {
139
+ if (evt.type != "timeupdate") {
140
+ this._logger.sendTelemetryEvent(
141
+ TelemetryEvents.MediaPlayerSynchronizer.PlayerEvent,
142
+ null,
143
+ {
144
+ type: evt.type,
145
+ }
146
+ );
147
+ }
148
+
149
+ switch (evt.type) {
150
+ case "loadedmetadata":
151
+ // eslint-disable-next-line no-case-declarations
152
+ const src = this._player.currentSrc || this._player.src;
153
+ this._trackData = null;
154
+ break;
155
+ case "playing":
156
+ // Handle case for YouTube player where user can pause/play video by clicking on it.
157
+ // - Videos don't always start at 0.0 seconds.
158
+ if (this._expectedPlaybackState != "playing") {
159
+ if (
160
+ this._mediaSession.coordinator.canPlayPause &&
161
+ this._player.currentTime < 1.0
162
+ ) {
163
+ this._logger.sendTelemetryEvent(
164
+ TelemetryEvents.MediaPlayerSynchronizer
165
+ .UserTappedVideoToPlay
166
+ );
167
+ this.play();
168
+ }
169
+ }
170
+ // block play if player state is playing when expected synced state is paused and coordinator is not suspended.
171
+ // needed because cannot tell if its a user initiated event, so disallow play
172
+ if (
173
+ this._expectedPlaybackState === "paused" &&
174
+ !this._mediaSession.coordinator.isSuspended
175
+ ) {
176
+ this._player.pause();
177
+ }
178
+
179
+ // block play if player state is playing when expected synced state is none and coordinator is not suspended.
180
+ // needed because user who is not in control should not be able to start, so disallow play
181
+ if (
182
+ this._expectedPlaybackState === "none" &&
183
+ !this._mediaSession.coordinator.isSuspended
184
+ ) {
185
+ this._player.pause();
186
+ }
187
+ break;
188
+ case "pause":
189
+ // block pause if player state is paused when expected synced state is playing and coordinator is not suspended.
190
+ // needed because cannot tell if its a user initiated event, so disallow pause
191
+ if (
192
+ this._expectedPlaybackState === "playing" &&
193
+ !this._mediaSession.coordinator.isSuspended
194
+ ) {
195
+ this._player.play();
196
+ }
197
+ break;
198
+ case "ratechange":
199
+ // Block rate changes unless suspended.
200
+ if (
201
+ this._player.playbackRate != 1.0 &&
202
+ !this._mediaSession.coordinator.isSuspended
203
+ ) {
204
+ this._logger.sendTelemetryEvent(
205
+ TelemetryEvents.MediaPlayerSynchronizer
206
+ .PlaybackRateChangeBlocked
207
+ );
208
+ this._player.playbackRate = 1.0;
209
+ }
210
+ break;
211
+ case "blocked":
212
+ this._logger.sendTelemetryEvent(
213
+ TelemetryEvents.MediaPlayerSynchronizer
214
+ .PlaybackRateChangeBlocked
215
+ );
216
+ break;
217
+ }
218
+ };
219
+
220
+ // Register for coordinator state changes
221
+ this._mediaSession.coordinator.on(
222
+ MediaSessionCoordinatorEvents.coordinatorstatechange,
223
+ (evt) => this.emit(evt.type, evt)
224
+ );
225
+
226
+ // Register media session actions
227
+ for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
228
+ this._mediaSession.setActionHandler(
229
+ action,
230
+ async (details: ExtendedMediaSessionActionDetails) => {
231
+ let error: Error | undefined;
232
+ try {
233
+ switch (details.action) {
234
+ case "play":
235
+ this._expectedPlaybackState = "playing";
236
+ if (this._player.paused) {
237
+ this._logger.sendTelemetryEvent(
238
+ TelemetryEvents.MediaPlayerSynchronizer
239
+ .PlayAction
240
+ );
241
+ if (
242
+ typeof details.seekTime == "number" &&
243
+ this._player.currentTime < 1.0
244
+ ) {
245
+ this._logger.sendTelemetryEvent(
246
+ TelemetryEvents
247
+ .MediaPlayerSynchronizer
248
+ .SeekingPlayerToStartPosition,
249
+ null,
250
+ { position: details.seekTime }
251
+ );
252
+ this._player.currentTime =
253
+ details.seekTime!;
254
+ }
255
+ // Reference: https://developer.mozilla.org/docs/Web/API/HTMLMediaElement/play#exceptions
256
+ await this._player.play();
257
+ }
258
+ break;
259
+ case "pause":
260
+ this._logger.sendTelemetryEvent(
261
+ TelemetryEvents.MediaPlayerSynchronizer
262
+ .PauseAction
263
+ );
264
+ this._expectedPlaybackState = "paused";
265
+ if (
266
+ typeof details.seekTime == "number" &&
267
+ this._player.currentTime < 1.0
268
+ ) {
269
+ this._logger.sendTelemetryEvent(
270
+ TelemetryEvents.MediaPlayerSynchronizer
271
+ .SeekingPlayerToStartPosition,
272
+ null,
273
+ { position: details.seekTime }
274
+ );
275
+ this._player.currentTime =
276
+ details.seekTime!;
277
+ }
278
+ this._player.pause();
279
+ break;
280
+ case "seekto":
281
+ if (typeof details.seekTime == "number") {
282
+ this._logger.sendTelemetryEvent(
283
+ TelemetryEvents.MediaPlayerSynchronizer
284
+ .SeekToAction,
285
+ null,
286
+ { position: details.seekTime }
287
+ );
288
+ this._player.currentTime =
289
+ details.seekTime!;
290
+ }
291
+ break;
292
+ case "settrack":
293
+ this._logger.sendTelemetryEvent(
294
+ TelemetryEvents.MediaPlayerSynchronizer
295
+ .SetTrackAction
296
+ );
297
+ this._expectedPlaybackState = "none";
298
+ this._player.src =
299
+ details.metadata!.trackIdentifier;
300
+ this._player.load();
301
+ break;
302
+ case "datachange":
303
+ this._logger.sendTelemetryEvent(
304
+ TelemetryEvents.MediaPlayerSynchronizer
305
+ .DataChangeAction
306
+ );
307
+ break;
308
+ case "catchup":
309
+ if (typeof details.seekTime == "number") {
310
+ this._logger.sendTelemetryEvent(
311
+ TelemetryEvents.MediaPlayerSynchronizer
312
+ .CatchupAction,
313
+ null,
314
+ { position: details.seekTime }
315
+ );
316
+ this.catchupPlayer(details.seekTime!);
317
+ }
318
+ break;
319
+ }
320
+ } catch (err: any) {
321
+ if (err instanceof Error) {
322
+ error = err;
323
+ } else {
324
+ error = new Error(
325
+ err?.message ??
326
+ "An unknown error occurred after processing the group session action."
327
+ );
328
+ }
329
+ }
330
+
331
+ this.dispatchGroupAction(details, false, error);
332
+ }
333
+ );
334
+ }
335
+
336
+ // Subscribe to player events
337
+ for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
338
+ this._player.addEventListener(event, this._onPlayerEvent);
339
+ }
340
+
341
+ this._logger.sendTelemetryEvent(
342
+ TelemetryEvents.MediaPlayerSynchronizer.SynchronizationStarted
343
+ );
344
+ }
345
+
346
+ /**
347
+ * Media player being synchronized.
348
+ */
349
+ public get player(): IMediaPlayer {
350
+ return this._player;
351
+ }
352
+
353
+ /**
354
+ * Synchronizers media session.
355
+ */
356
+ public get mediaSession(): LiveMediaSession {
357
+ return this._mediaSession;
358
+ }
359
+
360
+ /**
361
+ * If true the client is in a view only mode.
362
+ *
363
+ * @remarks
364
+ * Toggling this value to true results in `mediaSession.coordinator.canPlayPause`,
365
+ * `mediaSession.coordinator.canSeek`, `mediaSession.coordinator.canSetTrack`, and
366
+ * `mediaSession.coordinator.canSetTrackData` all being set to false. For more fine
367
+ * grained control over the local clients policies, call the `mediaSession.coordinator`
368
+ * directly.
369
+ */
370
+ public get viewOnly(): boolean {
371
+ return this._viewOnly;
372
+ }
373
+
374
+ public set viewOnly(value: boolean) {
375
+ this._viewOnly = value;
376
+ this.mediaSession.coordinator.canPlayPause = !value;
377
+ this.mediaSession.coordinator.canSeek = !value;
378
+ this.mediaSession.coordinator.canSetTrack = !value;
379
+ this.mediaSession.coordinator.canSetTrackData = !value;
380
+ }
381
+
382
+ /**
383
+ * Volume limiter used to temporarily reduce the videos volume when someone speaks in a meeting.
384
+ */
385
+ public get volumeManager(): VolumeManager {
386
+ return this._volumeManager;
387
+ }
388
+
389
+ /**
390
+ * Registers a new event listener.
391
+ * @param event Name of the event to add.
392
+ * @param listener Function to call when the event is triggered.
393
+ */
394
+ public addEventListener(
395
+ event: MediaPlayerSynchronizerEvents,
396
+ listener: (evt: IMediaPlayerSynchronizerEvent) => void
397
+ ): this {
398
+ this.on(event, listener);
399
+ return this;
400
+ }
401
+
402
+ /**
403
+ * Un-registers an existing event listener.
404
+ * @param event Name of the event to remove.
405
+ * @param listener Function that was registered in call to `addEventListener()`.
406
+ */
407
+ public removeEventListener(
408
+ event: MediaPlayerSynchronizerEvents,
409
+ listener: (evt: IMediaPlayerSynchronizerEvent) => void
410
+ ): this {
411
+ this.off(event, listener);
412
+ return this;
413
+ }
414
+
415
+ /**
416
+ * Ends synchronization of the current media player.
417
+ */
418
+ public end(): void {
419
+ if (this._onEnd) {
420
+ try {
421
+ // Clear media session actions
422
+ for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
423
+ this._mediaSession.setActionHandler(action, null);
424
+ }
425
+
426
+ // Subscribe to player events
427
+ for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
428
+ this._player.removeEventListener(
429
+ event,
430
+ this._onPlayerEvent
431
+ );
432
+ }
433
+
434
+ // Notify parent
435
+ this._onEnd();
436
+ } finally {
437
+ this._onEnd = undefined;
438
+ }
439
+ }
440
+ }
441
+
442
+ /**
443
+ * Begin a local seek operation.
444
+ *
445
+ * @remarks
446
+ * UI can call this when a user grabs a timeline scrubber and starts scrubbing the video to a
447
+ * new playback position. The synchronizer will being a new suspension which temporarily
448
+ * disconnects the client for the rest of the group for synchronization purposes. Calling
449
+ * `endSeek()` will end the suspension and seek the group to the users final seek position.
450
+ */
451
+ public beginSeek(): void {
452
+ if (this._seekSuspension) {
453
+ throw new Error(
454
+ `MediaPlayerSynchronizer: cannot begin seek. A seek is already in progress.`
455
+ );
456
+ }
457
+
458
+ this._logger.sendTelemetryEvent(
459
+ TelemetryEvents.MediaPlayerSynchronizer.BeginSeekCalled
460
+ );
461
+ this._seekSuspension = this._mediaSession.coordinator.beginSuspension();
462
+ }
463
+
464
+ /**
465
+ * Ends a seek operation that was started by calling `beginSeek()`.
466
+ * @param seekTo Playback position in seconds to seek the group to.
467
+ */
468
+ public endSeek(seekTo: number): void {
469
+ if (!this._seekSuspension) {
470
+ throw new Error(
471
+ `MediaPlayerSynchronizer: cannot end seek. No seek is in progress.`
472
+ );
473
+ }
474
+
475
+ // Seek player to new time immediately.
476
+ // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
477
+ // location.
478
+ this._player.currentTime = seekTo;
479
+
480
+ const suspension = this._seekSuspension;
481
+ this._seekSuspension = undefined;
482
+
483
+ this._logger.sendTelemetryEvent(
484
+ TelemetryEvents.MediaPlayerSynchronizer.EndSeekCalled,
485
+ null,
486
+ {
487
+ position: seekTo,
488
+ }
489
+ );
490
+ suspension.end(seekTo);
491
+
492
+ this.dispatchUserAction({ action: "seekto", seekTime: seekTo });
493
+ }
494
+
495
+ /**
496
+ * Tells the group to begin playing the current video.
497
+ *
498
+ * @remarks
499
+ * For proper operation apps should avoid calling `mediaSession.coordinator.play()` directly
500
+ * and instead use the synchronizers `play()` method.
501
+ */
502
+ public play(): void {
503
+ this._logger.sendTelemetryEvent(
504
+ TelemetryEvents.MediaPlayerSynchronizer.PlayCalled
505
+ );
506
+ this._expectedPlaybackState = "playing";
507
+ this._mediaSession.coordinator.play();
508
+
509
+ this.dispatchUserAction({ action: "play" });
510
+ }
511
+
512
+ /**
513
+ * Tells the group to pause the current video.
514
+ *
515
+ * @remarks
516
+ * For proper operation apps should avoid calling `mediaSession.coordinator.pause()` directly
517
+ * and instead use the synchronizers `pause()` method.
518
+ */
519
+ public pause(): void {
520
+ this._logger.sendTelemetryEvent(
521
+ TelemetryEvents.MediaPlayerSynchronizer.PauseCalled
522
+ );
523
+ this._expectedPlaybackState = "paused";
524
+ this._mediaSession.coordinator.pause();
525
+
526
+ this.dispatchUserAction({ action: "pause" });
527
+ }
528
+
529
+ /**
530
+ * Tells the group to seek the current video to a new playback position.
531
+ *
532
+ * @remarks
533
+ * For proper operation apps should avoid calling `mediaSession.coordinator.seekTo()` directly
534
+ * and instead use the synchronizers `seekTo()` method.
535
+ */
536
+ public seekTo(time: number): void {
537
+ // Always seek player to new time.
538
+ // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
539
+ // location.
540
+ this._player.currentTime = time;
541
+
542
+ this._logger.sendTelemetryEvent(
543
+ TelemetryEvents.MediaPlayerSynchronizer.SeekToCalled,
544
+ null,
545
+ { position: time }
546
+ );
547
+ this._mediaSession.coordinator.seekTo(time);
548
+
549
+ this.dispatchUserAction({ action: "seekto", seekTime: time });
550
+ }
551
+
552
+ /**
553
+ * Tells the group to change to a new track.
554
+ *
555
+ * @remarks
556
+ * For proper operation apps should avoid calling `mediaSession.coordinator.setTrack()` directly
557
+ * and instead use the synchronizers `setTrack()` method.
558
+ */
559
+ public setTrack(
560
+ track: ExtendedMediaMetadata,
561
+ waitPoints?: CoordinationWaitPoint[]
562
+ ): void {
563
+ this._logger.sendTelemetryEvent(
564
+ TelemetryEvents.MediaPlayerSynchronizer.SetTrackCalled
565
+ );
566
+ this._mediaSession.coordinator.setTrack(track, waitPoints);
567
+
568
+ this.dispatchUserAction({ action: "settrack", metadata: track });
569
+ }
570
+
571
+ /**
572
+ * Updates the current tracks data object.
573
+ *
574
+ * @remarks
575
+ * For proper operation apps should avoid calling `mediaSession.coordinator.setTrackData()` directly
576
+ * and instead use the synchronizers `setTrackData()` method.
577
+ */
578
+ public setTrackData(data: object | null): void {
579
+ this._logger.sendTelemetryEvent(
580
+ TelemetryEvents.MediaPlayerSynchronizer.SetTrackDataCalled
581
+ );
582
+ this._trackData = data;
583
+ this._mediaSession.coordinator.setTrackData(data);
584
+
585
+ this.dispatchUserAction({ action: "datachange", data: data });
586
+ }
587
+
588
+ private dispatchGroupAction(
589
+ details: ExtendedMediaSessionActionDetails,
590
+ delay = false,
591
+ error: Error | undefined
592
+ ): void {
593
+ if (delay) {
594
+ setTimeout(
595
+ () =>
596
+ this.emit(MediaPlayerSynchronizerEvents.groupaction, {
597
+ type: MediaPlayerSynchronizerEvents.groupaction,
598
+ details: details,
599
+ playerError: error,
600
+ }),
601
+ 50
602
+ );
603
+ } else {
604
+ this.emit(MediaPlayerSynchronizerEvents.groupaction, {
605
+ type: MediaPlayerSynchronizerEvents.groupaction,
606
+ details: details,
607
+ error,
608
+ });
609
+ }
610
+ }
611
+
612
+ private dispatchUserAction(
613
+ details: ExtendedMediaSessionActionDetails,
614
+ delay = false
615
+ ): void {
616
+ if (delay) {
617
+ setTimeout(
618
+ () =>
619
+ this.emit(MediaPlayerSynchronizerEvents.useraction, {
620
+ type: MediaPlayerSynchronizerEvents.useraction,
621
+ details: details,
622
+ }),
623
+ 50
624
+ );
625
+ } else {
626
+ this.emit(MediaPlayerSynchronizerEvents.useraction, {
627
+ type: MediaPlayerSynchronizerEvents.useraction,
628
+ details: details,
629
+ });
630
+ }
631
+ }
632
+
633
+ private async catchupPlayer(time: number): Promise<void> {
634
+ this._player.currentTime = time;
635
+ }
636
+ }