@microsoft/live-share-media 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,450 +1,450 @@
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
- }
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
450
  }